pub struct Tensor { /* private fields */ }Expand description
An n-dimensional, immutable tensor backed by shared Storage.
The layout follows DLPack conventions: C-order shape, element-unit
strides (None means C-contiguous), and a byte offset into the backing
storage. Constructors validate that every addressable element falls
inside the storage, so accessors never fail.
Equality is logical: two tensors are equal when dtype, shape, and element bytes (in C order) match, regardless of how they are laid out in storage.
Implementations§
Source§impl Tensor
impl Tensor
Sourcepub fn from_vec(
data: Vec<u8>,
shape: Vec<i64>,
dtype: DType,
) -> Result<Self, TensorError>
pub fn from_vec( data: Vec<u8>, shape: Vec<i64>, dtype: DType, ) -> Result<Self, TensorError>
Adopt data as a C-contiguous tensor without copying.
data.len() must equal exactly numel * dtype_size. The buffer keeps
its original allocation, so no alignment is guaranteed.
Sourcepub fn from_slice(
data: &[u8],
shape: &[i64],
dtype: DType,
) -> Result<Self, TensorError>
pub fn from_slice( data: &[u8], shape: &[i64], dtype: DType, ) -> Result<Self, TensorError>
Copy data into a fresh 64-byte-aligned C-contiguous tensor.
data.len() must equal exactly numel * dtype_size.
Sourcepub fn zeros(shape: &[i64], dtype: DType) -> Result<Self, TensorError>
pub fn zeros(shape: &[i64], dtype: DType) -> Result<Self, TensorError>
A zero-filled, 64-byte-aligned C-contiguous tensor.
Sourcepub fn from_storage(
storage: Storage,
dtype: DType,
shape: Vec<i64>,
strides: Option<Vec<i64>>,
byte_offset: usize,
) -> Result<Self, TensorError>
pub fn from_storage( storage: Storage, dtype: DType, shape: Vec<i64>, strides: Option<Vec<i64>>, byte_offset: usize, ) -> Result<Self, TensorError>
A tensor view over storage with full layout control.
strides are in element units; None means C-contiguous. Dims and
strides must be non-negative and every addressable element must fall
inside the storage.
Sourcepub fn strides(&self) -> Option<&[i64]>
pub fn strides(&self) -> Option<&[i64]>
Explicit element-unit strides, or None when C-contiguous.
Sourcepub fn effective_strides(&self) -> Cow<'_, [i64]>
pub fn effective_strides(&self) -> Cow<'_, [i64]>
Element-unit strides, materializing the C-contiguous default.
Sourcepub fn byte_offset(&self) -> usize
pub fn byte_offset(&self) -> usize
Offset in bytes from the start of the storage to the first element.
Sourcepub fn device(&self) -> Device
pub fn device(&self) -> Device
Device the storage lives on. Always Device::Cpu today.
Sourcepub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
Whether elements are laid out C-contiguously.
Sourcepub fn reshape(&self, shape: &[i64]) -> Result<Self, TensorError>
pub fn reshape(&self, shape: &[i64]) -> Result<Self, TensorError>
A tensor with the same elements and a new shape.
At most one dimension may be -1, which is inferred from the
element count. Returns a zero-copy view sharing this tensor’s
storage when the layout is contiguous, and a contiguous copy
otherwise.
Sourcepub fn to_contiguous_bytes(&self) -> Cow<'_, [u8]>
pub fn to_contiguous_bytes(&self) -> Cow<'_, [u8]>
The element bytes in C order.
Borrows from storage when the layout is contiguous; gathers into a fresh buffer otherwise.