Skip to main content

Tensor

Struct Tensor 

Source
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

Source

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.

Source

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.

Source

pub fn zeros(shape: &[i64], dtype: DType) -> Result<Self, TensorError>

A zero-filled, 64-byte-aligned C-contiguous tensor.

Source

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.

Source

pub fn dtype(&self) -> DType

Element data type.

Source

pub fn shape(&self) -> &[i64]

Dimension sizes in C order.

Source

pub fn strides(&self) -> Option<&[i64]>

Explicit element-unit strides, or None when C-contiguous.

Source

pub fn effective_strides(&self) -> Cow<'_, [i64]>

Element-unit strides, materializing the C-contiguous default.

Source

pub fn byte_offset(&self) -> usize

Offset in bytes from the start of the storage to the first element.

Source

pub fn device(&self) -> Device

Device the storage lives on. Always Device::Cpu today.

Source

pub fn storage(&self) -> &Storage

The shared backing storage.

Source

pub fn numel(&self) -> usize

Number of elements.

Source

pub fn nbytes(&self) -> usize

Logical size of the element data in bytes (numel * dtype_size).

Source

pub fn is_contiguous(&self) -> bool

Whether elements are laid out C-contiguously.

Source

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.

Source

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.

Source

pub fn stack(tensors: &[Tensor]) -> Result<Tensor, TensorError>

Stack tensors of identical dtype and shape along a new leading axis.

The result is a fresh 64-byte-aligned contiguous tensor of shape [tensors.len(), ..shape].

Source

pub fn unstack(&self) -> Result<Vec<Tensor>, TensorError>

Split along axis 0 into zero-copy views sharing this storage.

Trait Implementations§

Source§

impl Clone for Tensor

Source§

fn clone(&self) -> Tensor

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Tensor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Tensor

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.