Skip to main content

Tensor

Struct Tensor 

Source
pub struct Tensor<T> {
    pub storage: TensorStorage<T>,
    /* private fields */
}
Expand description

Core tensor structure that holds data and metadata

Fields§

§storage: TensorStorage<T>

Implementations§

Source§

impl<T> Tensor<T>
where T: Float + FromPrimitive + Clone + Send + Sync + Default + 'static,

Source

pub fn mul_scalar(&self, scalar: T) -> Result<Tensor<T>>

Convenience method to multiply tensor by a scalar

Source§

impl<T> Tensor<T>
where T: Clone + Default + Zero + One + Send + Sync + 'static + Pod + Zeroable,

Source

pub fn eq(&self, other: &Self) -> Result<Tensor<bool>>
where T: PartialEq,

Element-wise equality comparison

Source

pub fn ne(&self, other: &Self) -> Result<Tensor<bool>>
where T: PartialEq,

Element-wise not-equal comparison

Source

pub fn gt(&self, other: &Self) -> Result<Tensor<bool>>
where T: PartialOrd,

Element-wise greater-than comparison

Source

pub fn ge(&self, other: &Self) -> Result<Tensor<bool>>
where T: PartialOrd,

Element-wise greater-than-or-equal comparison

Source

pub fn lt(&self, other: &Self) -> Result<Tensor<bool>>
where T: PartialOrd,

Element-wise less-than comparison

Source

pub fn le(&self, other: &Self) -> Result<Tensor<bool>>
where T: PartialOrd,

Element-wise less-than-or-equal comparison

Source§

impl Tensor<bool>

Source

pub fn cast_to_u8(&self) -> Result<Tensor<u8>>

Cast boolean tensor to u8 tensor (false -> 0, true -> 1)

Source

pub fn logical_and(&self, other: &Self) -> Result<Self>

Element-wise logical AND operation

Source

pub fn logical_or(&self, other: &Self) -> Result<Self>

Element-wise logical OR operation

Source

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

Element-wise logical NOT operation

Source

pub fn logical_xor(&self, other: &Self) -> Result<Self>

Element-wise logical XOR operation

Source

pub fn all(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>

Reduce tensor using logical AND along specified axes

Source

pub fn any(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>

Reduce tensor using logical OR along specified axes

Source§

impl Tensor<u8>

Source

pub fn cast_to_bool(&self) -> Result<Tensor<bool>>

Cast u8 tensor to boolean tensor (0 -> false, non-zero -> true)

Source§

impl<T> Tensor<T>

Source

pub fn shape(&self) -> &Shape

Get the shape of the tensor

Source

pub fn device(&self) -> &Device

Get the device where the tensor is located

Source

pub fn dtype(&self) -> DType
where T: 'static,

Get the data type of the tensor

Source

pub fn requires_grad(&self) -> bool

Check if tensor requires gradient computation

Source

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

Set whether tensor requires gradient computation

Source

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

Get the gradient tensor if it exists

Source

pub fn set_grad(&mut self, grad: Option<Tensor<T>>)

Set the gradient tensor

Source

pub fn data(&self) -> &[T]

Get a reference to the underlying data (CPU only)

Source

pub fn get(&self, index: &[usize]) -> Option<T>
where T: Clone,

Get the value at a specific index (for CPU tensors only)

Source

pub fn as_slice(&self) -> Option<&[T]>

Get the underlying data as a slice (CPU tensors only)

Source

pub fn is_empty(&self) -> bool

Check if tensor is empty (has no elements)

Source

pub fn memory_usage(&self) -> usize

Get memory usage in bytes

Source

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

Check if two tensors have the same shape

Source

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

Check if tensors are broadcastable

Source

pub fn summary(&self) -> String
where T: Display + Clone,

Get tensor summary statistics as a formatted string

Source

pub fn size(&self) -> usize

Get the total number of elements (alias for size)

Source

pub fn numel(&self) -> usize

Get the total number of elements

Source

pub fn rank(&self) -> usize

Get the number of dimensions (rank)

Source

pub fn ndim(&self) -> usize

Get the number of dimensions (alias for rank)

Source

pub fn is_scalar(&self) -> bool

Check if tensor is a scalar (0-dimensional)

Source

pub fn is_vector(&self) -> bool

Check if tensor is a vector (1-dimensional)

Source

pub fn is_matrix(&self) -> bool

Check if tensor is a matrix (2-dimensional)

Source

pub fn is_contiguous(&self) -> bool

Check if tensor data is contiguous in memory

Source§

impl<T> Tensor<T>
where T: Clone + Pod + Zeroable + Send + Sync + 'static,

Source

pub fn map_inplace<F>(&mut self, f: F) -> Result<()>
where F: Fn(&T) -> T,

Apply a function to each element of the tensor

Source§

impl<T: Clone + Default> Tensor<T>

Source

pub fn zeros(shape: &[usize]) -> Self
where T: Zero,

Create a tensor filled with zeros

Source

pub fn ones(shape: &[usize]) -> Self
where T: One,

Create a tensor filled with ones

Source

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

Create a tensor from raw data vector with specified shape

Source

pub fn from_array(array: ArrayD<T>) -> Self

Create a tensor from an existing ndarray

Source

pub fn randn(shape: &[usize]) -> Result<Self>
where T: Clone + Default + From<f32>,

Create a tensor filled with random values from normal distribution

Source

pub fn from_storage(storage: TensorStorage<T>, device: Device) -> Self

Create a tensor from storage and device

Source

pub fn from_scalar(value: T) -> Self

Create a scalar tensor from a single value

Source

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

Create a tensor from a vector of data with specified shape

Source

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

Create a tensor filled with a specific value

Source

pub fn eye(n: usize) -> Self
where T: Zero + One + Clone,

Create an identity matrix tensor

Source

pub fn arange(start: T, end: T, step: T) -> Result<Self>

Create a tensor with evenly spaced values in a given interval

Source

pub fn linspace(start: T, end: T, steps: usize) -> Result<Self>

Create a tensor with linearly spaced values between start and end

Source§

impl<T: Clone> Tensor<T>

Source

pub fn to(&self, device: Device) -> Result<Self>
where T: Default + Pod + Zeroable + Send + Sync + 'static,

Transfer tensor to specified device

Source

pub fn to_device(&self, target_device: Device) -> Result<Self>
where T: Clone + Default + Send + Sync + 'static + Pod,

Transfer tensor to a different device

Source

pub fn to_cpu(&self) -> Result<Self>
where T: Clone + Default + Send + Sync + 'static + Pod,

Move tensor to CPU

Source

pub fn copy_from_device(&mut self, src: &Self) -> Result<()>
where T: Clone + Default + Send + Sync + 'static + Pod,

Copy tensor data from another device (for collective operations)

Source

pub fn can_transfer_to(&self, target_device: Device) -> bool
where T: Pod,

Check if tensor can be transferred to target device

Source§

impl<T: Clone> Tensor<T>

Source

pub fn backward(&self) -> Result<()>
where T: Clone + Default + Zero + One,

Perform backward pass for gradient computation

Source

pub fn backward_with_options( &self, retain_graph: bool, create_graph: bool, ) -> Result<()>
where T: Clone + Default + Zero + One,

Enhanced backward pass with additional autograd options

Source§

impl<T> Tensor<T>
where T: Clone + Default + Zero + One + Send + Sync + 'static + Pod + Zeroable,

Source

pub fn add(&self, other: &Self) -> Result<Self>
where T: Add<Output = T>,

Element-wise addition

Source

pub fn sub(&self, other: &Self) -> Result<Self>
where T: Sub<Output = T>,

Element-wise subtraction

Source

pub fn mul(&self, other: &Self) -> Result<Self>
where T: Mul<Output = T>,

Element-wise multiplication

Source

pub fn div(&self, other: &Self) -> Result<Self>
where T: Div<Output = T>,

Element-wise division

Source

pub fn pow(&self, other: &Self) -> Result<Self>
where T: Float,

Element-wise power operation

Source

pub fn log(&self) -> Result<Self>
where T: Float,

Element-wise natural logarithm

Source

pub fn neg(&self) -> Result<Self>
where T: Neg<Output = T>,

Element-wise negation

Source

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

Matrix multiplication

Source

pub fn relu(&self) -> Result<Self>
where T: PartialOrd + Zero + Pod + Zeroable,

ReLU activation function

Source

pub fn sigmoid(&self) -> Result<Self>
where T: Float + Pod + Zeroable,

Sigmoid activation function

Source

pub fn tanh(&self) -> Result<Self>
where T: Float + Pod + Zeroable,

Hyperbolic tangent activation function

Source

pub fn gelu(&self) -> Result<Self>
where T: Float + Pod,

GELU activation function

Source

pub fn swish(&self) -> Result<Self>
where T: Float + Pod,

Swish activation function

Source

pub fn mish(&self) -> Result<Self>
where T: Float + Send + Sync + 'static + Pod + Zeroable,

Mish activation function

Source

pub fn softmax(&self, axis: Option<i32>) -> Result<Self>
where T: Float + Sub<Output = T> + Add<Output = T> + Div<Output = T> + Sum + Send + Sync + Pod,

Softmax activation function

Source

pub fn elu(&self, alpha: T) -> Result<Self>
where T: Float + PartialOrd + Pod,

ELU activation function

Source

pub fn leaky_relu(&self, alpha: T) -> Result<Self>
where T: Float + PartialOrd + Pod,

Leaky ReLU activation function

Source

pub fn hard_swish(&self) -> Result<Self>
where T: Float + PartialOrd,

Hard Swish activation function

Source

pub fn prelu(&self, alpha: &Self) -> Result<Self>
where T: Float + PartialOrd,

Parametric ReLU activation function

Source

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

Reshape tensor to new shape

Source

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

Transpose tensor (swap last two dimensions)

Source

pub fn slice(&self, ranges: &[Range<usize>]) -> Result<Self>

Slice tensor along specified ranges

Source

pub fn slice_with_stride(&self, slice_params: &[SliceParams]) -> Result<Self>

Slice tensor with stride parameters

Source

pub fn sum(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>
where T: Zero,

Sum tensor along specified axes

Source

pub fn mean(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>
where T: Float + FromPrimitive,

Mean tensor along specified axes

Source

pub fn max(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>
where T: PartialOrd,

Maximum values along specified axes

Source

pub fn min(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>
where T: PartialOrd,

Minimum values along specified axes

Source

pub fn sqrt(&self) -> Result<Self>
where T: Float,

Element-wise square root

Source

pub fn abs(&self) -> Result<Self>
where T: Signed,

Element-wise absolute value

Source

pub fn exp(&self) -> Result<Self>
where T: Float,

Element-wise exponential function

Source

pub fn sin(&self) -> Result<Self>
where T: Float,

Element-wise sine function

Source

pub fn cos(&self) -> Result<Self>
where T: Float,

Element-wise cosine function

Source

pub fn tan(&self) -> Result<Self>
where T: Float,

Element-wise tangent function

Source

pub fn recip(&self) -> Result<Self>
where T: Float,

Element-wise reciprocal function

Source

pub fn squeeze(&self, axes: Option<&[usize]>) -> Result<Self>
where T: Clone + Default + Zero + Send + Sync + 'static,

Squeeze tensor - remove dimensions of size 1

Source

pub fn unsqueeze(&self, axes: &[usize]) -> Result<Self>
where T: Clone + Default + Zero + Send + Sync + 'static,

Unsqueeze tensor - add dimensions of size 1

Source

pub fn scalar_mul(&self, scalar: T) -> Result<Self>
where T: Clone + Default + Mul<Output = T> + Send + Sync + 'static,

Scalar multiplication

Source

pub fn to_vec(&self) -> Result<Vec<T>>
where T: Clone + Default + Send + Sync + 'static + Zero + One,

Convert tensor to vector

Source

pub fn max_axis(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>
where T: Clone + Default + PartialOrd + Send + Sync + 'static,

Maximum values along specified axes

Source

pub fn sum_axis(&self, axes: Option<&[i32]>, keepdims: bool) -> Result<Self>
where T: Clone + Default + Zero + Add<Output = T> + Send + Sync + 'static,

Sum along specified axes

Source

pub fn clamp(&self, min: T, max: T) -> Result<Self>
where T: PartialOrd + Clone,

Clamp tensor values between min and max

Source

pub fn allclose(&self, other: &Self, rtol: T, atol: T) -> Result<bool>
where T: Float + Clone,

Check if all elements are close to another tensor within tolerance

Source

pub fn fill_(&mut self, value: T) -> Result<()>
where T: Clone,

Fill tensor with specified value

Source

pub fn to_scalar(&self) -> Result<T>
where T: Clone,

Extract scalar value from a 0-dimensional tensor

Source

pub fn argmax(&self, axis: i32) -> Result<Tensor<usize>>
where T: PartialOrd + Clone,

Find the indices of the maximum values along the specified axis

Source

pub fn flatten(&self) -> Result<Self>
where T: Clone + Default + Zero + Send + Sync + 'static,

Flatten the tensor into a 1D tensor

This operation reshapes the tensor into a 1-dimensional tensor containing the same elements in row-major (C-style) order.

§Returns

A 1D tensor containing all elements from the input tensor

§Examples
use tenflowers_core::Tensor;

let tensor = Tensor::<f32>::from_vec(vec![1.0, 2.0, 3.0, 4.0], &[2, 2]).expect("from_vec should succeed");
let flattened = tensor.flatten().expect("flatten should not fail");
assert_eq!(flattened.shape().dims(), &[4]);
Source

pub fn cumsum(&self, axis: Option<i32>) -> Result<Self>
where T: Clone + Default + Add<Output = T> + Zero + Send + Sync + 'static,

Compute the cumulative sum of elements along the given axis

§Arguments
  • axis - Axis along which to compute the cumulative sum. If None, flatten the tensor first.
§Returns

A tensor with cumulative sums along the specified axis

§Examples
use tenflowers_core::Tensor;

let tensor = Tensor::<f32>::from_vec(vec![1.0, 2.0, 3.0, 4.0], &[2, 2]).expect("from_vec should succeed");
let cumsum = tensor.cumsum(Some(0)).expect("operation should succeed");
Source

pub fn cumprod(&self, axis: Option<i32>) -> Result<Self>
where T: Clone + Default + Mul<Output = T> + One + Send + Sync + 'static,

Compute the cumulative product of elements along the given axis

§Arguments
  • axis - Axis along which to compute the cumulative product. If None, flatten the tensor first.
§Returns

A tensor with cumulative products along the specified axis

§Examples
use tenflowers_core::Tensor;

let tensor = Tensor::<f32>::from_vec(vec![1.0, 2.0, 3.0, 4.0], &[2, 2]).expect("from_vec should succeed");
let cumprod = tensor.cumprod(Some(0)).expect("operation should succeed");
Source

pub fn tile(&self, multiples: &[usize]) -> Result<Self>
where T: Clone + Default + Zero + Send + Sync + 'static,

Tile the tensor by repeating it along each axis

§Arguments
  • multiples - The number of repetitions along each axis
§Returns

A tensor with the input tiled according to the multiples

§Examples
use tenflowers_core::Tensor;

let tensor = Tensor::<f32>::from_vec(vec![1.0, 2.0], &[1, 2]).expect("from_vec should succeed");
let tiled = tensor.tile(&[2, 3]).expect("tile should succeed");
assert_eq!(tiled.shape().dims(), &[2, 6]);
Source

pub fn repeat(&self, repeats: usize, axis: Option<usize>) -> Result<Self>
where T: Clone + Default + Zero + Send + Sync + 'static,

Repeat elements of the tensor

§Arguments
  • repeats - The number of repetitions for each element
  • axis - The axis along which to repeat values. If None, the input tensor is flattened first.
§Returns

A tensor with repeated elements

§Examples
use tenflowers_core::Tensor;

let tensor = Tensor::<f32>::from_vec(vec![1.0, 2.0, 3.0], &[3]).expect("from_vec should succeed");
let repeated = tensor.repeat(2, Some(0)).expect("operation should succeed");
assert_eq!(repeated.shape().dims(), &[6]);
Source

pub fn broadcast_to(&self, target_shape: &[usize]) -> Result<Self>
where T: Clone + Default + Zero + Send + Sync + 'static,

Broadcast the tensor to a new shape

§Arguments
  • target_shape - The shape to broadcast to
§Returns

A tensor broadcasted to the target shape

§Examples
use tenflowers_core::Tensor;

let tensor = Tensor::<f32>::from_vec(vec![1.0, 2.0], &[1, 2]).expect("from_vec should succeed");
let broadcasted = tensor.broadcast_to(&[3, 2]).expect("broadcast_to should succeed");
assert_eq!(broadcasted.shape().dims(), &[3, 2]);
Source

pub fn expand_as(&self, target: &Self) -> Result<Self>
where T: Clone + Default + Zero + Send + Sync + 'static,

Expand tensor dimensions to match another tensor’s shape

§Arguments
  • target - The tensor whose shape to match
§Returns

A tensor expanded to match the target tensor’s shape

§Examples
use tenflowers_core::Tensor;

let tensor = Tensor::<f32>::from_vec(vec![1.0, 2.0], &[1, 2]).expect("from_vec should succeed");
let target = Tensor::<f32>::zeros(&[3, 2]);
let expanded = tensor.expand_as(&target).expect("expand_as should succeed");
assert_eq!(expanded.shape().dims(), &[3, 2]);
Source

pub fn multiply_scalar(&self, scalar: T) -> Result<Self>
where T: Clone + Mul<Output = T>,

Scalar multiplication

Source

pub fn dot(&self, other: &Self) -> Result<Self>
where T: Clone + Default + Zero + One + Add<Output = T> + Mul<Output = T>,

Dot product of two 1D tensors

Source

pub fn outer(&self, other: &Self) -> Result<Self>
where T: Clone + Default + Zero + One + Add<Output = T> + Mul<Output = T>,

Outer product of two 1D tensors

Trait Implementations§

Source§

impl<T: Clone> Clone for Tensor<T>

Source§

fn clone(&self) -> Tensor<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Tensor<T>

Source§

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

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

impl<T: Clone> Index<&[usize]> for Tensor<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: &[usize]) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T: Clone> Index<usize> for Tensor<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Tensor<T>

§

impl<T> RefUnwindSafe for Tensor<T>
where T: RefUnwindSafe,

§

impl<T> Send for Tensor<T>
where T: Send + Sync,

§

impl<T> Sync for Tensor<T>
where T: Sync + Send,

§

impl<T> Unpin for Tensor<T>

§

impl<T> UnsafeUnpin for Tensor<T>

§

impl<T> UnwindSafe for Tensor<T>
where T: RefUnwindSafe,

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, 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