Skip to main content

Tensor

Struct Tensor 

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

A tensor: shared storage viewed through a layout.

Cloning a tensor is cheap — it clones the layout and bumps the storage refcount, never the data. View operations (reshape, permute, narrow, …) produce new tensors over the same storage whenever the layout arithmetic allows it.

Implementations§

Source§

impl Tensor

Source

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

Elementwise negation.

Source

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

Elementwise e^x.

Source

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

Elementwise natural logarithm.

Source

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

Elementwise absolute value.

Source

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

Elementwise square root.

Source

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

Elementwise sine.

Source

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

Elementwise cosine.

Source

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

Elementwise hyperbolic tangent.

Source

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

Elementwise rectified linear unit.

Source

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

Elementwise GELU (tanh approximation).

Source

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

Elementwise logistic sigmoid.

Source

pub fn scalar_on(like: &Tensor, value: f32) -> Result<Tensor>

A scalar constant on the same device as like (plumbing for VJPs and scalar operator overloads).

Source

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

Elementwise addition, broadcasting.

Source

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

Elementwise subtraction, broadcasting.

Source

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

Elementwise multiplication, broadcasting.

Source

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

Elementwise division, broadcasting.

Source

pub fn pow(&self, rhs: &Tensor) -> Result<Tensor>

Elementwise power, broadcasting.

Source

pub fn maximum(&self, rhs: &Tensor) -> Result<Tensor>

Elementwise maximum, broadcasting.

Source

pub fn minimum(&self, rhs: &Tensor) -> Result<Tensor>

Elementwise minimum, broadcasting.

Source

pub fn gt_mask(&self, rhs: &Tensor) -> Result<Tensor>

Elementwise a > b as a 0.0/1.0 mask. Not differentiable.

Source

pub fn eq_mask(&self, rhs: &Tensor) -> Result<Tensor>

Elementwise a == b as a 0.0/1.0 mask. Not differentiable.

Source

pub fn add_scalar(&self, s: f32) -> Result<Tensor>

Add a scalar, broadcasting.

Source

pub fn mul_scalar(&self, s: f32) -> Result<Tensor>

Multiply by a scalar, broadcasting.

Source

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

Matrix product with NumPy/PyTorch batch semantics.

Operands are rank 2 ([m, k]) or rank 3 ([b, m, k]); a rank-2 operand behaves as batch 1, and batch dimensions broadcast (1 against b). The result is rank 2 only when both operands are. [2, 2, 3] x [1, 3, 2] is [2, 2, 2]; [m, k] x [b, k, n] is [b, m, n]. See plan_matmul for the exact contract every backend implements.

Source

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

Sum over axes (empty means all), removing them from the shape.

Source

pub fn sum_keepdim(&self, axes: &[usize], keepdim: bool) -> Result<Tensor>

Sum over axes with explicit keepdim.

Source

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

Maximum over axes (empty means all).

Source

pub fn max_keepdim(&self, axes: &[usize], keepdim: bool) -> Result<Tensor>

Maximum over axes with explicit keepdim.

Source

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

Minimum over axes (empty means all).

Source

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

Mean over axes (empty means all) — composite, so its gradient flows through sum and scalar multiply.

Source

pub fn mean_keepdim(&self, axes: &[usize], keepdim: bool) -> Result<Tensor>

Mean over axes with explicit keepdim.

Source

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

Index of the maximum along dim, as an I64 tensor. Not differentiable.

Source

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

Numerically stable softmax along dim — composite.

Source

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

Numerically stable log-softmax along dim — composite.

Source

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

Rows of self along dim selected by indices (I64).

Source

pub fn index_add( &self, dim: usize, indices: &Tensor, src: &Tensor, ) -> Result<Tensor>

out[indices[i]] += src[i] along dim, on a fresh copy of self.

Source

pub fn to_device(&self, device: Device) -> Result<Tensor>

This tensor’s data on device (a cheap clone when already there).

Source§

impl Tensor

Source

pub fn from_storage(storage: Arc<Storage>, layout: Layout) -> Result<Self>

A tensor over existing storage with an explicit layout.

Errors when the layout addresses elements outside the storage.

Source

pub fn from_vec_f32(data: Vec<f32>, shape: impl Into<Shape>) -> Result<Self>

A contiguous CPU tensor holding data with shape shape.

Errors when data.len() does not equal shape.numel().

Source

pub fn from_slice(data: &[f32], shape: impl Into<Shape>) -> Result<Self>

A contiguous CPU tensor copying data with shape shape.

Source

pub fn from_vec_i64(data: Vec<i64>, shape: impl Into<Shape>) -> Result<Self>

A contiguous CPU I64 tensor holding data (indices, targets).

Source

pub fn zeros(shape: impl Into<Shape>) -> Self

A CPU tensor of zeros.

Source

pub fn ones(shape: impl Into<Shape>) -> Self

A CPU tensor of ones.

Source

pub fn full(shape: impl Into<Shape>, value: f32) -> Self

A CPU tensor filled with value.

Source

pub fn scalar(value: f32) -> Self

A rank-0 scalar tensor.

Source

pub fn randn(shape: impl Into<Shape>) -> Self

Standard-normal random CPU tensor, seeded from the OS.

Source

pub fn randn_with_seed(shape: impl Into<Shape>, seed: u64) -> Self

Standard-normal random CPU tensor with a fixed seed, for reproducible tests and examples.

Source

pub fn shape(&self) -> &Shape

The shape of this view.

Source

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

The dimension extents, outermost first.

Source

pub fn ndim(&self) -> usize

The rank (number of dimensions).

Source

pub fn numel(&self) -> usize

The total number of elements.

Source

pub fn layout(&self) -> &Layout

The full layout of this view.

Source

pub fn dtype(&self) -> DType

The element type.

Source

pub fn device(&self) -> Device

The device the storage lives on.

Source

pub fn storage(&self) -> &Arc<Storage> ⓘ

The shared storage behind this view.

Source

pub fn get_f32(&self, index: &[usize]) -> Result<f32>

The element at a logical index, as f32.

Errors on rank mismatch, out-of-bounds, non-float dtype, or non-CPU storage.

Source

pub fn get_i64(&self, index: &[usize]) -> Result<i64>

The element at a logical index, as i64.

Source

pub fn to_vec_f32(&self) -> Result<Vec<f32>>

Every element in logical (row-major) order, as f32, from CPU storage.

Source

pub fn to_vec_i64(&self) -> Result<Vec<i64>>

Every element in logical (row-major) order, as i64.

Source

pub fn reshape(&self, shape: impl Into<Shape>) -> Result<Self>

A view (or copy, when this view is not contiguous) with the same elements in a new shape.

Source

pub fn permute(&self, perm: &[usize]) -> Result<Self>

A view with dimensions reordered by perm (a permutation of 0..ndim).

Source

pub fn transpose(&self, d0: usize, d1: usize) -> Result<Self>

A view with dimensions d0 and d1 swapped.

Source

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

The matrix transpose: the last two dimensions swapped.

Source

pub fn narrow(&self, dim: usize, start: usize, len: usize) -> Result<Self>

A view of len elements of dimension dim starting at start.

Source

pub fn slice(&self, dim: usize, range: Range<usize>) -> Result<Self>

A view of range along dim — sugar over Tensor::narrow.

Source

pub fn broadcast_to(&self, shape: impl Into<Shape>) -> Result<Self>

A zero-copy broadcast view to shape (stride 0 on expanded axes).

Source

pub fn broadcast_view(&self, shape: &Shape) -> Result<Self>

A broadcast view that records nothing on the tape — backend plumbing; prefer Tensor::broadcast_to in user code.

Source

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

A view with a new size-1 dimension inserted at dim.

Source

pub fn contiguous(&self) -> Result<Self>

This tensor’s elements, in logical order, in fresh contiguous storage on the same device. A no-op clone when already contiguous.

Source

pub fn contiguous_untracked(&self) -> Result<Self>

The contiguous copy without autograd recording — backend plumbing; prefer Tensor::contiguous in user code.

Source

pub fn requires_grad_(self, requires: bool) -> Self

Mark (or unmark) this tensor as a gradient-accumulating leaf, in place, returning it for chaining.

Source

pub fn requires_grad(&self) -> bool

Whether gradients accumulate on this tensor during backward — true for leaves marked with requires_grad_.

This answers a narrower question than PyTorch’s requires_grad: a tensor computed from such a leaf is on the tape but does not accumulate a gradient of its own, so it reports false here. Ask is_tracked for “is this on the graph at all”.

Source

pub fn is_tracked(&self) -> bool

Whether this tensor participates in the autograd tape at all — true for a leaf that requires grad and for anything computed from one while recording was enabled; false for constants and for everything produced under no_grad.

This is the predicate that observes no_grad:

use oxmera_tensor::tensor::Tensor;
use oxmera_tensor::autograd::no_grad;

let a = Tensor::from_slice(&[1.0, 2.0], [2]).unwrap().requires_grad_(true);
assert!(a.mul_scalar(3.0).unwrap().is_tracked());
assert!(!no_grad(|| a.mul_scalar(3.0).unwrap()).is_tracked());
Source

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

The accumulated gradient, if a backward pass has produced one.

Source

pub fn zero_grad(&self)

Clear this tensor’s accumulated gradient.

Source

pub fn detach(&self) -> Self

The same view without any tape connection.

Source

pub fn backward(&self) -> Result<()>

Propagate gradients from this scalar through the recorded tape.

Errors when the tensor is not a scalar; use Tensor::backward_with to seed a non-scalar output.

Source

pub fn backward_with(&self, seed: Tensor) -> Result<()>

Propagate gradients seeding this tensor’s gradient with seed.

Trait Implementations§

Source§

impl Add for Tensor

Source§

type Output = Tensor

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Tensor) -> Tensor

Performs the + operation. Read more
Source§

impl Add<&Tensor> for &Tensor

Source§

type Output = Tensor

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add<&Tensor> for Tensor

Source§

type Output = Tensor

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add<&Tensor> for f32

Source§

type Output = Tensor

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add<Tensor> for &Tensor

Source§

type Output = Tensor

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Tensor) -> Tensor

Performs the + operation. Read more
Source§

impl Add<Tensor> for f32

Source§

type Output = Tensor

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Tensor) -> Tensor

Performs the + operation. Read more
Source§

impl Add<f32> for &Tensor

Source§

type Output = Tensor

The resulting type after applying the + operator.
Source§

fn add(self, rhs: f32) -> Tensor

Performs the + operation. Read more
Source§

impl Add<f32> for Tensor

Source§

type Output = Tensor

The resulting type after applying the + operator.
Source§

fn add(self, rhs: f32) -> Tensor

Performs the + operation. Read more
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 Div for Tensor

Source§

type Output = Tensor

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Tensor) -> Tensor

Performs the / operation. Read more
Source§

impl Div<&Tensor> for &Tensor

Source§

type Output = Tensor

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl Div<&Tensor> for Tensor

Source§

type Output = Tensor

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl Div<&Tensor> for f32

Source§

type Output = Tensor

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl Div<Tensor> for &Tensor

Source§

type Output = Tensor

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Tensor) -> Tensor

Performs the / operation. Read more
Source§

impl Div<Tensor> for f32

Source§

type Output = Tensor

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Tensor) -> Tensor

Performs the / operation. Read more
Source§

impl Div<f32> for &Tensor

Source§

type Output = Tensor

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f32) -> Tensor

Performs the / operation. Read more
Source§

impl Div<f32> for Tensor

Source§

type Output = Tensor

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f32) -> Tensor

Performs the / operation. Read more
Source§

impl Mul for Tensor

Source§

type Output = Tensor

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Tensor) -> Tensor

Performs the * operation. Read more
Source§

impl Mul<&Tensor> for &Tensor

Source§

type Output = Tensor

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<&Tensor> for Tensor

Source§

type Output = Tensor

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<&Tensor> for f32

Source§

type Output = Tensor

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<Tensor> for &Tensor

Source§

type Output = Tensor

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Tensor) -> Tensor

Performs the * operation. Read more
Source§

impl Mul<Tensor> for f32

Source§

type Output = Tensor

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Tensor) -> Tensor

Performs the * operation. Read more
Source§

impl Mul<f32> for &Tensor

Source§

type Output = Tensor

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f32) -> Tensor

Performs the * operation. Read more
Source§

impl Mul<f32> for Tensor

Source§

type Output = Tensor

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f32) -> 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 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, rhs: Tensor) -> Tensor

Performs the - operation. Read more
Source§

impl Sub<&Tensor> for &Tensor

Source§

type Output = Tensor

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub<&Tensor> for Tensor

Source§

type Output = Tensor

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub<&Tensor> for f32

Source§

type Output = Tensor

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub<Tensor> for &Tensor

Source§

type Output = Tensor

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Tensor) -> Tensor

Performs the - operation. Read more
Source§

impl Sub<Tensor> for f32

Source§

type Output = Tensor

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Tensor) -> Tensor

Performs the - operation. Read more
Source§

impl Sub<f32> for &Tensor

Source§

type Output = Tensor

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: f32) -> Tensor

Performs the - operation. Read more
Source§

impl Sub<f32> for Tensor

Source§

type Output = Tensor

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: f32) -> Tensor

Performs the - operation. Read more

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> 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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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