VarLenTensor#
- class sdm.tensor.VarLenTensor(data: Tensor, offset: Tensor, valid: Tensor | None, size: Sequence[int], stride: Sequence[int] | None = None, storage_offset: int = 0)#
Bases:
TensorA
torch.Tensorfor rectangular variable-length values.Values are stored in a flat contiguous
datatensor and indexed by anoffsettensor.import torch from sdm import VarLenTensor tensor = VarLenTensor( data=torch.tensor([1, 2, 3, 4, 5, 6]), offset=torch.tensor([0, 2, 5, 5, 6]), valid=None, size=(2, 2), )
- Parameters:
data (Tensor) – Flat contiguous tensor containing all element values.
offset (Tensor) – One-dimensional offsets into
data.valid (Tensor | None) – One-dimensional mask indicating valid, non-null element values.
size (Sequence[int]) – The shape of the tensor.
stride (Sequence[int] | None) – The stride of the tensor.
storage_offset (int) – The offset into the logical
offsetstorage.
- Return type:
Self
- classmethod from_tensor(tensor: Tensor, *, offset_dtype: dtype = torch.int64) Self#
Wrap a dense tensor as fixed-size variable-length elements.
- classmethod from_arrow(array: Array | ChunkedArray, *, size: Sequence[int] | None = None, device: device | str | None = None) Self#
Create tensor from a list
pyarrow.Array.import pyarrow as pa from sdm import VarLenTensor array = pa.array([[1, 2], [3, 4, 5], [], None, [6]]) tensor = VarLenTensor.from_arrow(array)
- Parameters:
array (Array | ChunkedArray) – The list
pyarrow.Arrayorpyarrow.ChunkedArray.
- Return type:
- to_arrow() Array#
Convert this tensor to a flat
pyarrow.Array.- Return type:
- classmethod from_list(values: Sequence[Any], *, dtype: dtype | None = None, device: device | str | None = None, offset_dtype: dtype = torch.int64) Self#
Create tensor from a rectangular Python list.
from sdm import VarLenTensor tensor = VarLenTensor.from_list([ [[1, 2], [3, 4, 5]], [[], [6]], [[7, 8], None], ])
- property data_offset: tuple[Tensor, Tensor]#
Return contiguous data and normalized offsets.
- Returns:
(data, offset)tuple.