Skip to content

lite πŸ”—

Tensorflow Lite operations and objects.

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
FFIError

If the C operation fails.

Source code in vaccel/ops/tf/lite/mixin.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:
        FFIError: If the C operation fails.
    """
    ret = lib.vaccel_tflite_session_delete(
        self._c_ptr_or_raise, 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
FFIError

If the C operation fails.

Source code in vaccel/ops/tf/lite/mixin.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:
        FFIError: If the C operation fails.
    """
    ret = lib.vaccel_tflite_session_load(
        self._c_ptr_or_raise, 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
FFIError

If the C operation fails.

Source code in vaccel/ops/tf/lite/mixin.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:
        FFIError: If the C operation fails.
    """
    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_or_raise,
        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/tf/lite/tensor.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_data = None
    self._c_obj_ptr = ffi.NULL
    self._c_obj_data = ffi.NULL
    super().__init__()

c_size property πŸ”—

c_size: int

Returns the size of the object in bytes.

data property πŸ”—

data: list

The tensor data.

Returns:

Type Description
list

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.

shape property πŸ”—

shape: list[int]

The tensor shape.

Alias of Tensor.dims.

Returns:

Type Description
list[int]

The shape 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`

as_bytelike πŸ”—

as_bytelike() -> bytes | bytearray | memoryview

Returns the tensor data buffer as a byte-like object.

Returns:

Type Description
bytes | bytearray | memoryview

The data of the tensor as a byte-like object.

Source code in vaccel/ops/tf/lite/tensor.py
def as_bytelike(self) -> bytes | bytearray | memoryview:
    """Returns the tensor data buffer as a byte-like object.

    Returns:
        The data of the tensor as a byte-like object.
    """
    if self._c_data is not None:
        if isinstance(self._c_data, CBytes):
            return self._c_data.value
        return self._c_data.as_memoryview()
    return CBytes.from_c_obj(
        self._c_ptr_or_raise.data, self._c_ptr_or_raise.size
    ).value

as_memoryview πŸ”—

as_memoryview() -> memoryview

Returns the tensor data buffer as memoryview.

Returns:

Type Description
memoryview

The data of the tensor as memoryview.

Source code in vaccel/ops/tf/lite/tensor.py
def as_memoryview(self) -> memoryview:
    """Returns the tensor data buffer as memoryview.

    Returns:
        The data of the tensor as memoryview.
    """
    data = self.as_bytelike()
    return data if isinstance(data, memoryview) else memoryview(data)

as_numpy πŸ”—

as_numpy() -> ndarray

Returns the tensor data buffer as a NumPy array.

Returns:

Type Description
ndarray

The data of the tensor as a NumPy array.

Source code in vaccel/ops/tf/lite/tensor.py
def as_numpy(self) -> "np.ndarray":
    """Returns the tensor data buffer as a NumPy array.

    Returns:
        The data of the tensor as a NumPy array.
    """
    if not HAS_NUMPY:
        msg = "NumPy is not available"
        raise NotImplementedError(msg)

    if isinstance(self._c_data, CNumpyArray):
        return self._c_data.value

    dtype = TensorTypeMapper.type_to_numpy(self.data_type)
    return CNumpyArray.from_c_obj(
        self._c_ptr_or_raise.data,
        self._c_ptr_or_raise.size,
        self.shape,
        dtype,
    ).value

empty classmethod πŸ”—

empty() -> Tensor

Initializes a new empty Tensor object.

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

Returns:

Type Description
Tensor

A new Tensor object

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

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

    Returns:
        A new `Tensor` object
    """
    inst = cls.__new__(cls)
    inst._dims = None
    inst._data = None
    inst._data_type = None
    inst._c_data = None
    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_buffer classmethod πŸ”—

from_buffer(
    dims: list[int], data_type: TensorType, data: bytes | bytearray | memoryview
) -> Tensor

Initializes a new Tensor object from byte-like data.

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 πŸ”—

bytes | bytearray | memoryview

The data to be passed to the C struct.

required

Returns:

Type Description
Tensor

A new Tensor object

Source code in vaccel/ops/tf/lite/tensor.py
@classmethod
def from_buffer(
    cls,
    dims: list[int],
    data_type: TensorType,
    data: bytes | bytearray | memoryview,
) -> "Tensor":
    """Initializes a new `Tensor` object from byte-like data.

    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.

    Returns:
        A new `Tensor` object
    """
    inst = cls.__new__(cls)
    inst._dims = dims
    inst._data = data
    inst._data_type = data_type
    inst._c_data = CBytes(inst._data)
    inst._c_obj_ptr = ffi.NULL
    inst._c_obj_data = inst._c_data._c_ptr
    super().__init__(inst)
    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/tf/lite/tensor.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._dims = None
    inst._data = None
    inst._data_type = None
    inst._c_data = None
    inst._c_obj_ptr = ffi.NULL
    inst._c_obj_data = ffi.NULL
    inst._c_obj = c_obj
    inst._c_size = ffi.sizeof(inst._c_obj)
    return inst

from_numpy classmethod πŸ”—

from_numpy(data: ndarray) -> Tensor

Initializes a new Tensor object from a NumPy array.

Parameters:

Name Type Description Default

data πŸ”—

ndarray

The NumPy array containing the tensor data.

required

Returns:

Type Description
Tensor

A new Tensor object

Raises:

Type Description
NotImplementedError

If NumPy is not installed.

Source code in vaccel/ops/tf/lite/tensor.py
@classmethod
def from_numpy(cls, data: "np.ndarray") -> "Tensor":
    """Initializes a new `Tensor` object from a NumPy array.

    Args:
        data: The NumPy array containing the tensor data.

    Returns:
        A new `Tensor` object

    Raises:
        NotImplementedError: If NumPy is not installed.
    """
    if not HAS_NUMPY:
        msg = "NumPy is not available"
        raise NotImplementedError(msg)

    inst = cls.__new__(cls)
    inst._dims = list(data.shape)
    inst._data = data
    inst._data_type = TensorTypeMapper.type_from_numpy(inst._data.dtype)
    inst._c_data = CNumpyArray(inst._data)
    inst._c_obj_ptr = ffi.NULL
    inst._c_obj_data = inst._c_data._c_ptr
    super().__init__(inst)
    return inst

to_bytes πŸ”—

to_bytes() -> bytes

Returns the tensor data buffer as bytes.

Returns:

Type Description
bytes

The data of the tensor as bytes.

Source code in vaccel/ops/tf/lite/tensor.py
def to_bytes(self) -> bytes:
    """Returns the tensor data buffer as bytes.

    Returns:
        The data of the tensor as bytes.
    """
    data = self.as_bytelike()
    return data if isinstance(data, bytes) else bytes(data)