Skip to content
View as Markdown

tinker.types.TensorData

Generated from tinker 0.30.4 at commit 1e5777e. Source links point at that snapshot.

class tinker.types.TensorData()

Fields:

  • dtype (TensorDtype)
  • shape (Optional[List[int]]) – Optional.

    The shape of the tensor (see PyTorch tensor.shape). The shape of a one-dimensional list of length N is (N,). Can usually be inferred if not provided, and is generally inferred as a 1D tensor.

  • sparse_crow_indices (Optional[List[int]]) – Optional CSR compressed row pointers. When set, this tensor is sparse CSR:

    • data contains only the listed values (flattened); every other entry takes the pad_value given when converting to or from a dense tensor
    • sparse_crow_indices contains the row pointers (length = nrows + 1)
    • sparse_col_indices contains the column indices (length = nnz)
    • shape is required and specifies the dense shape
  • sparse_col_indices (Optional[List[int]]) – Optional CSR column indices. Must be set together with sparse_crow_indices.

property data

Flattened tensor data as array of numbers.

Returns: Union[List[int], List[float]]

from_torch_sparse(tensor, pad_value=0)

Create a sparse CSR TensorData from a dense 2-D torch tensor.

Entries equal to pad_value are left out; the rest are stored as CSR values. Automatically detects sparsity and encodes as CSR when it saves space. Falls back to dense if the tensor is 1-D or mostly non-pad.

pad_value must be an integer: the tensor is shifted by it so torch's zero-based CSR conversion can be reused, and an integer shift is exact for int64 tensors. For float32 tensors with a non-zero pad it may round values whose magnitude is far below pad_value.

Parameters:

Returns: TensorData

to_numpy(pad_value=0)

Convert TensorData to numpy array.

A sparse CSR tensor is densified with pad_value in every unlisted entry.

Parameters:

Returns: npt.NDArray[Any]

to_torch(pad_value=0)

Convert TensorData to torch tensor.

A sparse CSR tensor is densified with pad_value in every unlisted entry. pad_value must be an integer so the shift around torch's zero-based densification is exact for int64 tensors.

Parameters:

Returns: torch.Tensor

Referenced by