Skip to content

tflite πŸ”—

Tensorflow Lite operations.

TFLiteMixin πŸ”—

Mixin providing Tensorflow Lite 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, TensorflowLiteMixin): ...

tflite_model_delete πŸ”—

tflite_model_delete(resource: Resource) -> None

Performs the Tensorflow Lite model deletion operation.

Wraps the vaccel_tflite_session_delete() C operation.

Parameters:

Name Type Description Default

resource πŸ”—

Resource

A resource with the model to unload.

required

Returns:

Type Description
None

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/tflite.py
def tflite_model_delete(self, resource: Resource) -> None:
    """Performs the Tensorflow Lite model deletion operation.

    Wraps the `vaccel_tflite_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)

    ret = lib.vaccel_tflite_session_delete(self._c_ptr, resource._c_ptr)
    if ret != 0:
        raise FFIError(ret, "Tensorflow Lite model delete failed")

tflite_model_load πŸ”—

tflite_model_load(resource: Resource) -> None

Performs the Tensorflow Lite model loading operation.

Wraps the vaccel_tflite_session_load() C operation.

Parameters:

Name Type Description Default

resource πŸ”—

Resource

A resource with the model to load.

required

Raises:

Type Description
RuntimeError

If the Session is uninitialized.

FFIError

If the C operation fails.

Source code in vaccel/ops/tflite.py
def tflite_model_load(self, resource: Resource) -> None:
    """Performs the Tensorflow Lite model loading operation.

    Wraps the `vaccel_tflite_session_load()` C operation.

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

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

    ret = lib.vaccel_tflite_session_load(self._c_ptr, resource._c_ptr)
    if ret != 0:
        raise FFIError(ret, "Tensorflow Lite model loading failed")

tflite_model_run πŸ”—

tflite_model_run(
    resource: Resource, in_tensors: list[Tensor], nr_out_tensors: int = 1
) -> (list[Tensor], int)

Performs the Tensorflow Lite model run operation.

Wraps the vaccel_tflite_session_run() C operation.

Parameters:

Name Type Description Default

resource πŸ”—

Resource

A resource with the model to run.

required

in_tensors πŸ”—

list[Tensor]

The input tensors for the inference.

required

nr_out_tensors πŸ”—

int

The number of output tensors. Defaults to 1.

1

Returns:

Type Description
(list[Tensor], int)

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/tflite.py
def tflite_model_run(
    self,
    resource: Resource,
    in_tensors: list[Tensor],
    nr_out_tensors: int = 1,
) -> (list[Tensor], int):
    """Performs the Tensorflow Lite model run operation.

    Wraps the `vaccel_tflite_session_run()` C operation.

    Args:
        resource: A resource with the model to run.
        in_tensors: The input tensors for the inference.
        nr_out_tensors: The number of output tensors. Defaults to 1.

    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)

    c_in_tensors = CList.from_ptrs(in_tensors)
    c_out_tensors = CList.from_ptrs([Tensor.empty()] * nr_out_tensors)
    status = CInt(0, "uint8_t")

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

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

Tensor πŸ”—

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

Bases: CType

Wrapper for the struct vaccel_tflite_tensor C object.

Manages the creation and initialization of a C struct vaccel_tflite_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/tflite.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_tflite_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/tflite.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_tflite_tensor **")
    inst._c_obj = inst._c_obj_ptr[0]
    inst._c_obj_ptr[0] = ffi.NULL
    inst._c_size = ffi.sizeof("struct vaccel_tflite_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_tflite_tensor C object.

required

Returns:

Type Description
Tensor

A new Tensor object

Source code in vaccel/ops/tflite.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_tflite_tensor` C object.

    Returns:
        A new `Tensor` object
    """
    type_str = ffi.getctype(ffi.typeof(c_obj))
    if type_str != "struct vaccel_tflite_tensor *":
        msg = f"Expected 'struct vaccel_tflite_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