Skip to content

tf 🔗

Tensorflow operations.

Buffer 🔗

Buffer(data: bytes | bytearray)

Bases: CType

Wrapper for the struct vaccel_tf_buffer C object.

Manages the creation and initialization of a C struct vaccel_tf_buffer and provides access to it through Python properties.

Inherits

CType: Abstract base class for defining C data types.

Parameters:

Name Type Description Default

data 🔗

bytes | bytearray

The buffer data to be passed to the C struct.

required
Source code in vaccel/ops/tf.py
def __init__(self, data: bytes | bytearray):
    """Initializes a new `Buffer` object.

    Args:
        data: The buffer data to be passed to the C struct.
    """
    self._data = data
    self._c_data = None
    self._c_obj_ptr = None
    super().__init__()

c_size property 🔗

c_size: int

Returns the size of the object in bytes.

data property 🔗

data: bytes

The buffer data.

Returns:

Type Description
bytes

The data of the buffer.

size property 🔗

size: int

The buffer size.

Returns:

Type Description
int

The size of the buffer.

value property 🔗

value: CData

Returns the value of the underlying C struct.

Returns:

Type Description
CData

The dereferenced 'struct vaccel_tf_buffer`

Node 🔗

Node(name: str, id_: int)

Bases: CType

Wrapper for the struct vaccel_tf_node C object.

Manages the creation and initialization of a C struct vaccel_tf_node and provides access to it through Python properties.

Inherits

CType: Abstract base class for defining C data types.

Parameters:

Name Type Description Default

name 🔗

str

The name to be passed to the C struct.

required

id_ 🔗

int

The ID to be passed to the C struct.

required
Source code in vaccel/ops/tf.py
def __init__(self, name: str, id_: int):
    """Initializes a new `Node` object.

    Args:
        name: The name to be passed to the C struct.
        id_: The ID to be passed to the C struct.
    """
    self._name = name
    self._id = id_
    self._c_obj_ptr = None
    super().__init__()

c_size property 🔗

c_size: int

Returns the size of the object in bytes.

id property 🔗

id: int

The node identifier.

Returns:

Type Description
int

The node's ID.

name property 🔗

name: str

The node name.

Returns:

Type Description
str

The node's name.

value property 🔗

value: CData

Returns the value of the underlying C struct.

Returns:

Type Description
CData

The dereferenced 'struct vaccel_tf_node`

Status 🔗

Status(error_code: int = 0, message: str = '')

Bases: CType

Wrapper for the struct vaccel_tf_status C object.

Manages the creation and initialization of a C struct vaccel_tf_status and provides access to it through Python properties.

Inherits

CType: Abstract base class for defining C data types.

Parameters:

Name Type Description Default

error_code 🔗

int

The error code to be passed to the C struct. Defaults to 0.

0

message 🔗

str

The message to be passed to the C struct. Defaults to "".

''
Source code in vaccel/ops/tf.py
def __init__(self, error_code: int = 0, message: str = ""):
    """Initializes a new `Status` object.

    Args:
        error_code: The error code to be passed to the C struct. Defaults
            to 0.
        message: The message to be passed to the C struct. Defaults to "".
    """
    self._error_code = error_code
    self._message = message
    self._c_obj_ptr = None
    super().__init__()

c_size property 🔗

c_size: int

Returns the size of the object in bytes.

code property 🔗

code: int

The status error code.

Returns:

Type Description
int

The code of the status.

message property 🔗

message: str

The status message.

Returns:

Type Description
str

The message of the status.

value property 🔗

value: CData

Returns the value of the underlying C struct.

Returns:

Type Description
CData

The dereferenced 'struct vaccel_tf_status`

TFMixin 🔗

Mixin providing Tensorflow operations for a Session.

This mixin is intended to be used in combination with BaseSession and should not be instantiated on its own.

Intended usage

class Session(BaseSession, TensorflowMixin): ...

tf_model_delete 🔗

tf_model_delete(resource: Resource) -> Status

Performs the Tensorflow model deletion operation.

Wraps the vaccel_tf_session_delete() C operation.

Parameters:

Name Type Description Default

resource 🔗

Resource

A resource with the model to unload.

required

Returns:

Type Description
Status

The status of the operation execution.

Raises:

Type Description
RuntimeError

If the Session is uninitialized.

FFIError

If the C operation fails.

Source code in vaccel/ops/tf.py
def tf_model_delete(self, resource: Resource) -> Status:
    """Performs the Tensorflow model deletion operation.

    Wraps the `vaccel_tf_session_delete()` C operation.

    Args:
        resource: A resource with the model to unload.

    Returns:
        The status of the operation execution.

    Raises:
        RuntimeError: If the `Session` is uninitialized.
        FFIError: If the C operation fails.
    """
    if not self._c_ptr:
        msg = "Uninitialized session"
        raise RuntimeError(msg)

    status = Status()
    ret = lib.vaccel_tf_session_delete(
        self._c_ptr, resource._c_ptr, status._c_ptr
    )
    if ret != 0:
        raise FFIError(ret, "Tensorflow model delete failed")
    return status

tf_model_load 🔗

tf_model_load(resource: Resource) -> Status

Performs the Tensorflow model loading operation.

Wraps the vaccel_tf_session_load() C operation.

Parameters:

Name Type Description Default

resource 🔗

Resource

A resource with the model to load.

required

Returns:

Type Description
Status

The status of the operation execution.

Raises:

Type Description
RuntimeError

If the Session is uninitialized.

FFIError

If the C operation fails.

Source code in vaccel/ops/tf.py
def tf_model_load(self, resource: Resource) -> Status:
    """Performs the Tensorflow model loading operation.

    Wraps the `vaccel_tf_session_load()` C operation.

    Args:
        resource: A resource with the model to load.

    Returns:
        The status of the operation execution.

    Raises:
        RuntimeError: If the `Session` is uninitialized.
        FFIError: If the C operation fails.
    """
    if not self._c_ptr:
        msg = "Uninitialized session"
        raise RuntimeError(msg)

    status = Status()
    ret = lib.vaccel_tf_session_load(
        self._c_ptr, resource._c_ptr, status._c_ptr
    )
    if ret != 0:
        raise FFIError(ret, "Tensorflow model loading failed")
    return status

tf_model_run 🔗

Performs the Tensorflow model run operation.

Wraps the vaccel_tf_session_run() C operation.

Parameters:

Name Type Description Default

resource 🔗

Resource

A resource with the model to run.

required

in_nodes 🔗

list[Node]

The input nodes for the inference.

required

in_tensors 🔗

list[Tensor]

The input tensors for the inference.

required

out_nodes 🔗

list[Node]

The output nodes for the inference.

required

run_options 🔗

Buffer | None

The inference options.

None

Returns:

Type Description
(list[Tensor], Status)

A tuple containing: - The output tensors - The status of the operation execution.

Raises:

Type Description
RuntimeError

If the Session is uninitialized.

FFIError

If the C operation fails.

Source code in vaccel/ops/tf.py
def tf_model_run(
    self,
    resource: Resource,
    in_nodes: list[Node],
    in_tensors: list[Tensor],
    out_nodes: list[Node],
    run_options: Buffer | None = None,
) -> (list[Tensor], Status):
    """Performs the Tensorflow model run operation.

    Wraps the `vaccel_tf_session_run()` C operation.

    Args:
        resource: A resource with the model to run.
        in_nodes: The input nodes for the inference.
        in_tensors: The input tensors for the inference.
        out_nodes: The output nodes for the inference.
        run_options: The inference options.

    Returns:
        A tuple containing:
            - The output tensors
            - The status of the operation execution.

    Raises:
        RuntimeError: If the `Session` is uninitialized.
        FFIError: If the C operation fails.
    """
    if not self._c_ptr:
        msg = "Uninitialized session"
        raise RuntimeError(msg)

    run_options_ptr = (
        ffi.NULL if run_options is None else run_options._c_ptr
    )
    c_in_nodes = CList(in_nodes)
    c_in_tensors = CList.from_ptrs(in_tensors)
    c_out_nodes = CList(out_nodes)
    c_out_tensors = CList.from_ptrs([Tensor.empty()] * len(c_out_nodes))
    status = Status()

    ret = lib.vaccel_tf_session_run(
        self._c_ptr,
        resource._c_ptr,
        run_options_ptr,
        c_in_nodes._c_ptr,
        c_in_tensors._c_ptr,
        len(c_in_nodes),
        c_out_nodes._c_ptr,
        c_out_tensors._c_ptr,
        len(c_out_nodes),
        status._c_ptr,
    )
    if ret != 0:
        raise FFIError(ret, "Tensorflow model run failed")

    out_tensors = [Tensor.from_c_obj(t) for t in c_out_tensors.value]
    return (out_tensors, status)

Tensor 🔗

Tensor(dims: list[int], data_type: TensorType, data: list[Any])

Bases: CType

Wrapper for the struct vaccel_tf_tensor C object.

Manages the creation and initialization of a C struct vaccel_tf_tensor and provides access to it through Python properties.

Inherits

CType: Abstract base class for defining C data types.

Parameters:

Name Type Description Default

dims 🔗

list[int]

The dims to be passed to the C struct.

required

data_type 🔗

TensorType

The data_type to be passed to the C struct.

required

data 🔗

list[Any]

The data to be passed to the C struct.

required
Source code in vaccel/ops/tf.py
def __init__(self, dims: list[int], data_type: TensorType, data: list[Any]):
    """Initializes a new `Tensor` object.

    Args:
        dims: The dims to be passed to the C struct.
        data_type: The data_type to be passed to the C struct.
        data: The data to be passed to the C struct.
    """
    self._dims = dims
    self._data = data
    self._data_type = data_type
    self._c_obj_ptr = None
    self._c_obj_data = None
    super().__init__()

c_size property 🔗

c_size: int

Returns the size of the object in bytes.

data property 🔗

data: list | str

The tensor data.

Returns:

Type Description
list | str

The data of the tensor.

data_type property 🔗

data_type: TensorType

The tensor data type.

Returns:

Type Description
TensorType

The data type of the tensor.

dims property 🔗

dims: list[int]

The tensor dims.

Returns:

Type Description
list[int]

The dims of the tensor.

value property 🔗

value: CData

Returns the value of the underlying C struct.

Returns:

Type Description
CData

The dereferenced 'struct vaccel_tf_tensor`

empty classmethod 🔗

empty() -> Tensor

Initializes a new empty Tensor object.

The object a NULL pointer in place of the C struct.

Returns:

Type Description
Tensor

A new Tensor object

Source code in vaccel/ops/tf.py
@classmethod
def empty(cls) -> "Tensor":
    """Initializes a new empty `Tensor` object.

    The object a NULL pointer in place of the C struct.

    Returns:
        A new `Tensor` object
    """
    inst = cls.__new__(cls)
    inst._c_obj_ptr = ffi.new("struct vaccel_tf_tensor **")
    inst._c_obj = inst._c_obj_ptr[0]
    inst._c_obj_ptr[0] = ffi.NULL
    inst._c_size = ffi.sizeof("struct vaccel_tf_tensor *")
    return inst

from_c_obj classmethod 🔗

from_c_obj(c_obj: CData) -> Tensor

Initializes a new Tensor object from an existing C struct.

Parameters:

Name Type Description Default

c_obj 🔗

CData

A pointer to a struct vaccel_tf_tensor C object.

required

Returns:

Type Description
Tensor

A new Tensor object

Source code in vaccel/ops/tf.py
@classmethod
def from_c_obj(cls, c_obj: ffi.CData) -> "Tensor":
    """Initializes a new `Tensor` object from an existing C struct.

    Args:
        c_obj: A pointer to a `struct vaccel_tf_tensor` C object.

    Returns:
        A new `Tensor` object
    """
    type_str = ffi.getctype(ffi.typeof(c_obj))
    if type_str != "struct vaccel_tf_tensor *":
        msg = f"Expected 'struct vaccel_tf_tensor *', got '{type_str}'"
        raise TypeError(msg)

    inst = cls.__new__(cls)
    inst._c_obj = c_obj
    inst._c_size = ffi.sizeof(inst._c_obj)
    inst._dims = None
    inst._data = None
    inst._data_type = None
    inst._c_obj_ptr = None
    return inst