Skip to content

tensor πŸ”—

Interface to the struct vaccel_tf_tensor C object.

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/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_tf_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/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/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/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/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_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_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/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_tf_tensor C object.

required

Returns:

Type Description
Tensor

A new Tensor object

Source code in vaccel/ops/tf/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_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._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/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/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)

TensorTypeMapper πŸ”—

Utility for mapping between TensorType and other common types.

type_from_numpy classmethod πŸ”—

type_from_numpy(dtype: dtype) -> TensorType

Converts a NumPy dtype to TensorType.

Parameters:

Name Type Description Default

dtype πŸ”—

dtype

A NumPy dtype object or something convertible to np.dtype.

required

Returns:

Type Description
TensorType

A corresponding tensor type value.

Raises:

Type Description
NotImplementedError

If NumPy is not installed.

ValueError

If the dtype value is not supported.

Source code in vaccel/ops/tf/tensor.py
@classmethod
def type_from_numpy(cls, dtype: "np.dtype") -> TensorType:
    """Converts a NumPy `dtype` to `TensorType`.

    Args:
        dtype: A NumPy `dtype` object or something convertible to
            `np.dtype`.

    Returns:
        A corresponding tensor type value.

    Raises:
        NotImplementedError: If NumPy is not installed.
        ValueError: If the `dtype` value is not supported.
    """
    if not HAS_NUMPY:
        msg = "NumPy is not available"
        raise NotImplementedError(msg)

    dtype = np.dtype(dtype)
    if dtype not in cls._NUMPY_TO_TENSOR_TYPE:
        supported = ", ".join(str(d) for d in cls._NUMPY_TO_TENSOR_TYPE)
        msg = f"Unsupported NumPy dtype: {dtype}. Supported: {supported}"
        raise ValueError(msg)

    return cls._NUMPY_TO_TENSOR_TYPE[dtype]

type_to_c_size classmethod πŸ”—

type_to_c_size(tensor_type: TensorType) -> int

Converts a TensorType to a C type size (in bytes).

Parameters:

Name Type Description Default

tensor_type πŸ”—

TensorType

The tensor type value.

required

Returns:

Type Description
int

A corresponding C type size in bytes.

Raises:

Type Description
ValueError

If the tensor_type value is not supported.

Source code in vaccel/ops/tf/tensor.py
@classmethod
def type_to_c_size(cls, tensor_type: TensorType) -> int:
    """Converts a `TensorType` to a C type size (in bytes).

    Args:
        tensor_type: The tensor type value.

    Returns:
        A corresponding C type size in bytes.

    Raises:
        ValueError: If the `tensor_type` value is not supported.
    """
    if tensor_type not in cls._TENSOR_TYPE_TO_C:
        supported = ", ".join(str(d) for d in cls._TENSOR_TYPE_TO_C)
        msg = (
            f"Unsupported TensorType: {tensor_type}. Supported: {supported}"
        )
        raise ValueError(msg)
    return cls._TENSOR_TYPE_TO_C[tensor_type][1]

type_to_c_type classmethod πŸ”—

type_to_c_type(tensor_type: TensorType) -> str

Converts a TensorType to a C type string.

Parameters:

Name Type Description Default

tensor_type πŸ”—

TensorType

The tensor type value.

required

Returns:

Type Description
str

A corresponding C type as a string (e.g., "float", "int64_t").

Raises:

Type Description
ValueError

If the tensor_type value is not supported.

Source code in vaccel/ops/tf/tensor.py
@classmethod
def type_to_c_type(cls, tensor_type: TensorType) -> str:
    """Converts a `TensorType` to a C type string.

    Args:
        tensor_type: The tensor type value.

    Returns:
        A corresponding C type as a string (e.g., "float", "int64_t").

    Raises:
        ValueError: If the `tensor_type` value is not supported.
    """
    if tensor_type not in cls._TENSOR_TYPE_TO_C:
        supported = ", ".join(str(d) for d in cls._TENSOR_TYPE_TO_C)
        msg = (
            f"Unsupported TensorType: {tensor_type}. Supported: {supported}"
        )
        raise ValueError(msg)
    return cls._TENSOR_TYPE_TO_C[tensor_type][0]

type_to_numpy classmethod πŸ”—

type_to_numpy(ttype: TensorType) -> dtype

Converts a TensorType to a NumPy dtype.

Parameters:

Name Type Description Default

ttype πŸ”—

TensorType

A TensorType enum value.

required

Returns:

Type Description
dtype

A corresponding NumPy dtype object.

Raises:

Type Description
NotImplementedError

If NumPy is not installed.

ValueError

If the TensorType value is not supported.

Source code in vaccel/ops/tf/tensor.py
@classmethod
def type_to_numpy(cls, ttype: TensorType) -> "np.dtype":
    """Converts a `TensorType` to a NumPy `dtype`.

    Args:
        ttype: A `TensorType` enum value.

    Returns:
        A corresponding NumPy `dtype` object.

    Raises:
        NotImplementedError: If NumPy is not installed.
        ValueError: If the `TensorType` value is not supported.
    """
    if not HAS_NUMPY:
        msg = "NumPy is not available"
        raise NotImplementedError(msg)

    if ttype not in cls._TENSOR_TYPE_TO_NUMPY:
        supported = ", ".join(str(t) for t in cls._TENSOR_TYPE_TO_NUMPY)
        msg = f"Unsupported TensorType: {ttype}. Supported: {supported}"
        raise ValueError(msg)

    return cls._TENSOR_TYPE_TO_NUMPY[ttype]