Tensor

Struct Tensor 

Source
pub struct Tensor { /* private fields */ }
Expand description

The core Tensor type

Tensors are multi-dimensional arrays with:

  • Shared storage (enables zero-copy views)
  • Shape and strides (enables non-contiguous layouts)
  • Optional gradient tracking for autograd

Implementations§

Source§

impl Tensor

Source

pub fn from_slice<T: TensorElement>(data: &[T], shape: &[usize]) -> Result<Self>

Create a new tensor from a flat slice and shape

Source

pub fn zeros(shape: &[usize]) -> Self

Create a tensor filled with zeros

Source

pub fn ones(shape: &[usize]) -> Self

Create a tensor filled with ones

Source

pub fn full<T: TensorElement>(shape: &[usize], value: T) -> Self

Create a tensor filled with a constant value

Source

pub fn rand(shape: &[usize]) -> Self

Create a tensor with random values from uniform distribution [0, 1)

Source

pub fn randn(shape: &[usize]) -> Self

Create a tensor with random values from standard normal distribution

Source

pub fn eye(n: usize) -> Self

Create an identity matrix

Source

pub fn arange(start: f32, end: f32, step: f32) -> Self

Create a 1D tensor with evenly spaced values

Source

pub fn linspace(start: f32, end: f32, n: usize) -> Self

Create a 1D tensor with n evenly spaced values between start and end

Source

pub fn shape(&self) -> &Shape

Get the shape of the tensor

Source

pub fn dims(&self) -> &[usize]

Get dimensions as slice

Source

pub fn ndim(&self) -> usize

Number of dimensions

Source

pub fn numel(&self) -> usize

Total number of elements

Source

pub fn dtype(&self) -> DType

Get the data type

Source

pub fn strides(&self) -> &Strides

Get strides

Source

pub fn is_contiguous(&self) -> bool

Check if tensor is contiguous in memory

Source

pub fn requires_grad(&self) -> bool

Check if gradient tracking is enabled

Source

pub fn set_requires_grad(&mut self, requires_grad: bool)

Enable gradient tracking

Source

pub fn grad(&self) -> Option<Tensor>

Get gradient if available

Source

pub fn storage(&self) -> &Storage

Get reference to underlying storage

Source

pub fn set_grad(&mut self, grad: Tensor)

Set gradient

Source

pub fn zero_grad(&mut self)

Zero out gradient

Source

pub fn data_f32(&self) -> Vec<f32>

Get data as f32 slice (for f32 tensors)

Source

pub fn reshape(&self, new_shape: &[usize]) -> Result<Tensor>

Reshape tensor to new shape (must have same numel)

Source

pub fn flatten(&self) -> Result<Tensor>

Flatten tensor to 1D

Source

pub fn transpose(&self, dim0: usize, dim1: usize) -> Result<Tensor>

Transpose dimensions

Source

pub fn t(&self) -> Result<Tensor>

Transpose for 2D tensors (matrix transpose)

Source

pub fn squeeze(&self) -> Tensor

Squeeze: remove dimensions of size 1

Source

pub fn unsqueeze(&self, dim: usize) -> Result<Tensor>

Unsqueeze: add dimension of size 1 at position

Source

pub fn deep_clone(&self) -> Self

Deep clone (copies data)

Source§

impl Tensor

Source

pub fn add(&self, other: &Tensor) -> Result<Tensor>

Element-wise addition

Source

pub fn sub(&self, other: &Tensor) -> Result<Tensor>

Element-wise subtraction

Source

pub fn mul(&self, other: &Tensor) -> Result<Tensor>

Element-wise multiplication

Source

pub fn div(&self, other: &Tensor) -> Result<Tensor>

Element-wise division

Source

pub fn add_scalar(&self, scalar: f32) -> Tensor

Add scalar

Source

pub fn sub_scalar(&self, scalar: f32) -> Tensor

Subtract scalar

Source

pub fn mul_scalar(&self, scalar: f32) -> Tensor

Multiply by scalar

Source

pub fn div_scalar(&self, scalar: f32) -> Tensor

Divide by scalar

Source

pub fn neg(&self) -> Tensor

Negation

Source

pub fn abs(&self) -> Tensor

Absolute value

Source

pub fn pow(&self, exp: f32) -> Tensor

Power

Source

pub fn sqrt(&self) -> Tensor

Square root

Source

pub fn exp(&self) -> Tensor

Exponential

Source

pub fn log(&self) -> Tensor

Natural logarithm

Source

pub fn clamp(&self, min: f32, max: f32) -> Tensor

Clamp values to range

Source§

impl Tensor

Source

pub fn sum(&self) -> Tensor

Sum all elements

Source

pub fn sum_dim(&self, dim: usize, keepdim: bool) -> Result<Tensor>

Sum along dimension

Source

pub fn mean(&self) -> Tensor

Mean of all elements

Source

pub fn mean_dim(&self, dim: usize, keepdim: bool) -> Result<Tensor>

Mean along dimension

Source

pub fn max(&self) -> Tensor

Maximum element

Source

pub fn max_dim(&self, dim: usize, keepdim: bool) -> Result<Tensor>

Maximum along dimension

Source

pub fn min(&self) -> Tensor

Minimum element

Source

pub fn min_dim(&self, dim: usize, keepdim: bool) -> Result<Tensor>

Minimum along dimension

Source

pub fn prod(&self) -> Tensor

Product of all elements

Source

pub fn var(&self, unbiased: bool) -> Tensor

Variance of all elements

Source

pub fn std(&self, unbiased: bool) -> Tensor

Standard deviation

Source

pub fn argmax(&self) -> Tensor

Argmax - index of maximum element

Source

pub fn argmax_dim(&self, dim: usize, keepdim: bool) -> Result<Tensor>

Argmax along dimension

Source

pub fn argmin(&self) -> Tensor

Argmin - index of minimum element

Source

pub fn norm(&self) -> Tensor

L2 norm

Source

pub fn norm_l1(&self) -> Tensor

L1 norm

Source§

impl Tensor

Source

pub fn relu(&self) -> Tensor

ReLU activation: max(0, x)

Source

pub fn leaky_relu(&self, alpha: f32) -> Tensor

Leaky ReLU: max(alpha * x, x)

Source

pub fn elu(&self, alpha: f32) -> Tensor

ELU activation

Source

pub fn selu(&self) -> Tensor

SELU activation (self-normalizing)

Source

pub fn sigmoid(&self) -> Tensor

Sigmoid activation: 1 / (1 + exp(-x))

Source

pub fn tanh(&self) -> Tensor

Tanh activation

Source

pub fn gelu(&self) -> Tensor

GELU activation (Gaussian Error Linear Unit)

Source

pub fn silu(&self) -> Tensor

SiLU / Swish activation: x * sigmoid(x)

Source

pub fn mish(&self) -> Tensor

Mish activation: x * tanh(softplus(x))

Source

pub fn softplus(&self) -> Tensor

Softplus: log(1 + exp(x))

Source

pub fn softsign(&self) -> Tensor

Softsign: x / (1 + |x|)

Source

pub fn softmax(&self, dim: i32) -> Tensor

Softmax along last dimension

Source

pub fn log_softmax(&self, dim: i32) -> Tensor

Log softmax (numerically stable)

Source

pub fn hardtanh(&self, min_val: f32, max_val: f32) -> Tensor

Hardtanh: clamp(x, min, max)

Source

pub fn hardsigmoid(&self) -> Tensor

Hard sigmoid: clamp((x + 3) / 6, 0, 1)

Source

pub fn hardswish(&self) -> Tensor

Hard swish: x * hardsigmoid(x)

Source§

impl Tensor

Source

pub fn matmul(&self, other: &Tensor) -> Result<Tensor>

Matrix multiplication Supports:

  • 2D x 2D: standard matmul
  • Batched: broadcast batch dimensions
Source

pub fn dot(&self, other: &Tensor) -> Result<Tensor>

Vector dot product

Source

pub fn outer(&self, other: &Tensor) -> Result<Tensor>

Outer product of two vectors

Source

pub fn mv(&self, vec: &Tensor) -> Result<Tensor>

Matrix-vector multiplication

Source

pub fn bmm(&self, other: &Tensor) -> Result<Tensor>

Batch matrix-matrix multiplication (bmm)

Source

pub fn trace(&self) -> Result<Tensor>

Compute trace of a matrix

Source

pub fn diag(&self) -> Result<Tensor>

Compute diagonal of a matrix

Source§

impl Tensor

Tensor operations with NEON acceleration

Source

pub fn add_neon(&self, other: &Tensor) -> Result<Tensor>

Add two tensors using NEON

Source

pub fn mul_neon(&self, other: &Tensor) -> Result<Tensor>

Multiply two tensors using NEON

Source

pub fn relu_neon(&self) -> Tensor

ReLU activation using NEON

Trait Implementations§

Source§

impl Add for &Tensor

Source§

type Output = Tensor

The resulting type after applying the + operator.
Source§

fn add(self, other: &Tensor) -> Tensor

Performs the + operation. Read more
Source§

impl Clone for Tensor

Source§

fn clone(&self) -> Self

Shallow clone (shares storage)

1.0.0 · 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 Display for Tensor

Source§

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

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

impl Div for &Tensor

Source§

type Output = Tensor

The resulting type after applying the / operator.
Source§

fn div(self, other: &Tensor) -> Tensor

Performs the / operation. Read more
Source§

impl HardwareOps for Tensor

Source§

fn matmul_hw(&self, other: &Tensor, device: &HardwareDevice) -> Result<Tensor>

Matrix multiplication on hardware
Source§

fn conv2d_hw(&self, kernel: &Tensor, device: &HardwareDevice) -> Result<Tensor>

Convolution on hardware
Source§

fn elementwise_hw( &self, op: ElementwiseOp, device: &HardwareDevice, ) -> Result<Tensor>

Element-wise operations on hardware
Source§

impl Mul for &Tensor

Source§

type Output = Tensor

The resulting type after applying the * operator.
Source§

fn mul(self, other: &Tensor) -> Tensor

Performs the * operation. Read more
Source§

impl Neg for &Tensor

Source§

type Output = Tensor

The resulting type after applying the - operator.
Source§

fn neg(self) -> Tensor

Performs the unary - operation. Read more
Source§

impl Sub for &Tensor

Source§

type Output = Tensor

The resulting type after applying the - operator.
Source§

fn sub(self, other: &Tensor) -> Tensor

Performs the - operation. Read more

Auto Trait Implementations§

§

impl Freeze for Tensor

§

impl !RefUnwindSafe for Tensor

§

impl Send for Tensor

§

impl Sync for Tensor

§

impl Unpin for Tensor

§

impl !UnwindSafe for Tensor

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<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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V