Skip to main content

Tensor

Enum Tensor 

Source
pub enum Tensor {
    F32(TypedTensor<f32>),
    F64(TypedTensor<f64>),
    I32(TypedTensor<i32>),
    I64(TypedTensor<i64>),
    Bool(TypedTensor<bool>),
    C32(TypedTensor<Complex<f32>>),
    C64(TypedTensor<Complex<f64>>),
}
Expand description

Dynamic tensor enum over the supported scalar types.

The enum keeps dtype dynamic and rank dynamic. Use TypedTensor<T, R> directly when the scalar type or rank should be represented in Rust’s type system.

§Examples

use tenferro_tensor::{Tensor, TypedTensor};

let t = Tensor::F64(TypedTensor::from_vec_col_major(vec![2], vec![1.0, 2.0]).unwrap());
assert_eq!(t.shape(), &[2]);

let erased = Tensor::from_vec_col_major(vec![1, 2], vec![1.0_f64, 2.0]).unwrap();
assert_eq!(erased.shape().len(), 2);

Variants§

Implementations§

Source§

impl Tensor

Source

pub fn linear_offset(&self, indices: &[usize]) -> Result<usize>

Compute the linear physical-buffer offset for a logical index.

§Examples
use tenferro_tensor::Tensor;

let t = Tensor::from_vec_col_major(vec![2, 3], vec![0.0_f64; 6]).unwrap();
assert_eq!(t.linear_offset(&[1, 2])?, 5);
Source

pub fn linear_offset2(&self, i: usize, j: usize) -> Result<usize>

Compute the linear physical-buffer offset for a rank-2 logical index.

§Examples
use tenferro_tensor::Tensor;

let t = Tensor::from_vec_col_major(vec![2, 3], vec![0.0_f64; 6]).unwrap();
assert_eq!(t.linear_offset2(1, 2)?, 5);
Source

pub fn linear_offset3(&self, i: usize, j: usize, k: usize) -> Result<usize>

Compute the linear physical-buffer offset for a rank-3 logical index.

§Examples
use tenferro_tensor::Tensor;

let t = Tensor::from_vec_col_major(vec![2, 3, 2], vec![0.0_f64; 12]).unwrap();
assert_eq!(t.linear_offset3(1, 2, 1)?, 11);
Source

pub fn get<T: TensorScalar>(&self, indices: &[usize]) -> Result<&T>

Borrow a single typed element by multi-index.

§Examples
use tenferro_tensor::Tensor;

let t = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0]).unwrap();
assert_eq!(t.get::<f64>(&[1])?, &2.0);
assert!(t.get::<f32>(&[1]).is_err());
Source

pub fn get_mut<T: TensorScalar>(&mut self, indices: &[usize]) -> Result<&mut T>

Mutably borrow a single typed element by multi-index.

§Examples
use tenferro_tensor::Tensor;

let mut t = Tensor::from_vec_col_major(vec![1], vec![1.0_f64]).unwrap();
*t.get_mut::<f64>(&[0])? = 2.0;
assert_eq!(t.as_slice::<f64>()?, &[2.0]);
Source

pub unsafe fn get_unchecked<T: TensorScalar>( &self, indices: &[usize], ) -> Result<&T>

Try to borrow a single typed element by multi-index without release-mode bounds checks.

Debug builds still validate the rank and bounds. Dtype and backend host-access failures are still reported as errors.

§Safety

indices must have the same rank as this tensor and every index must be in bounds.

§Examples
use tenferro_tensor::Tensor;

let t = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0]).unwrap();
assert_eq!(unsafe { *t.get_unchecked::<f64>(&[1])? }, 2.0);
Source

pub unsafe fn get_unchecked_mut<T: TensorScalar>( &mut self, indices: &[usize], ) -> Result<&mut T>

Try to mutably borrow a single typed element by multi-index without release-mode bounds checks.

Debug builds still validate the rank and bounds. Dtype and backend host-access failures are still reported as errors.

§Safety

indices must have the same rank as this tensor and every index must be in bounds.

§Examples
use tenferro_tensor::Tensor;

let mut t = Tensor::from_vec_col_major(vec![1], vec![1.0_f64]).unwrap();
unsafe {
    *t.get_unchecked_mut::<f64>(&[0])? = 2.0;
}
assert_eq!(t.as_slice::<f64>()?, &[2.0]);
Source

pub fn as_slice_mut<T: TensorScalar>(&mut self) -> Result<&mut [T]>

Mutably borrow the host data as a typed slice.

§Examples
use tenferro_tensor::Tensor;

let mut t = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0]).unwrap();
t.as_slice_mut::<f64>()?[0] = 3.0;
assert_eq!(t.as_slice::<f64>()?, &[3.0, 2.0]);
assert!(t.as_slice_mut::<f32>().is_err());
Source

pub fn iter<T: TensorScalar>(&self) -> Result<Iter<'_, T>>

Iterate over the contiguous host buffer in physical memory order.

§Examples
use tenferro_tensor::Tensor;

let t = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0]).unwrap();
let sum: f64 = t.iter::<f64>()?.copied().sum();
assert_eq!(sum, 3.0);
assert!(t.iter::<f32>().is_err());
Source

pub fn iter_mut<T: TensorScalar>(&mut self) -> Result<IterMut<'_, T>>

Mutably iterate over the contiguous host buffer in physical memory order.

§Examples
use tenferro_tensor::Tensor;

let mut t = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0]).unwrap();
for value in t.iter_mut::<f64>()? {
    *value += 1.0;
}
assert_eq!(t.as_slice::<f64>()?, &[2.0, 3.0]);
assert!(t.iter_mut::<f32>().is_err());
Source§

impl Tensor

Source

pub fn index_select( &self, axis: isize, positions: &[usize], ctx: &mut impl TensorBackend, ) -> Result<Self>

Select entries from one axis using host-known positions.

§Examples
use tenferro_tensor::{Tensor, TensorBackend};

fn select_last_axis<B: TensorBackend>(
    backend: &mut B,
    x: &Tensor,
) -> tenferro_tensor::Result<Tensor> {
    x.index_select(-1, &[2, 0], backend)
}
Source

pub fn stack( tensors: &[&Self], dim: isize, ctx: &mut impl TensorBackend, ) -> Result<Self>

Stack tensors along a newly inserted axis.

§Examples
use tenferro_tensor::{Tensor, TensorBackend};

fn stack_scalars<B: TensorBackend>(
    backend: &mut B,
    a: &Tensor,
    b: &Tensor,
) -> tenferro_tensor::Result<Tensor> {
    Tensor::stack(&[a, b], -1, backend)
}
Source§

impl Tensor

Source

pub fn from_vec_col_major<T: TensorScalar>( shape: Vec<usize>, data: Vec<T>, ) -> Result<Self>

Create a tensor from a shape and column-major flat data.

This is the Tensor-level equivalent of TypedTensor::<T>::from_vec_col_major.

§Examples
use tenferro_tensor::Tensor;

let t = Tensor::from_vec_col_major(vec![2, 2], vec![1.0_f64, 3.0, 2.0, 4.0]).unwrap();
assert_eq!(t.shape(), &[2, 2]);
assert_eq!(t.as_slice::<f64>().unwrap(), &[1.0, 3.0, 2.0, 4.0]);
Source

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

Tensor shape.

§Examples
use tenferro_tensor::{Tensor, TypedTensor};

let t = Tensor::F64(TypedTensor::from_vec_col_major(vec![2], vec![1.0, 2.0]).unwrap());
assert_eq!(t.shape(), &[2]);
Source

pub fn dtype(&self) -> DType

Tensor dtype tag.

§Examples
use tenferro_tensor::{DType, Tensor, TypedTensor};

let t = Tensor::F64(TypedTensor::from_vec_col_major(vec![], vec![1.0]).unwrap());
assert_eq!(t.dtype(), DType::F64);
Source

pub fn placement(&self) -> &Placement

Return placement metadata for this dtype-erased tensor.

§Examples
use tenferro_tensor::{MemoryKind, Tensor};

let t = Tensor::from_vec_col_major(vec![1], vec![1.0_f64]).unwrap();
assert_eq!(t.placement().memory_kind, MemoryKind::UnpinnedHost);
Source

pub fn is_backend_buffer(&self) -> bool

Return whether this tensor is backed by backend-native storage.

§Examples
use tenferro_tensor::Tensor;

let t = Tensor::from_vec_col_major(vec![1], vec![1.0_f64]).unwrap();
assert!(!t.is_backend_buffer());
Source

pub fn as_slice<T: TensorScalar>(&self) -> Result<&[T]>

Try to borrow the host data as a typed slice.

Returns an error if the tensor dtype does not match T.

§Examples
use tenferro_tensor::{Tensor, TypedTensor};

let t = Tensor::F64(TypedTensor::from_vec_col_major(vec![3], vec![1.0, 2.0, 3.0]).unwrap());
assert_eq!(t.as_slice::<f64>().unwrap(), [1.0, 2.0, 3.0].as_slice());
assert!(t.as_slice::<f32>().is_err());
Source

pub fn into_vec_col_major<T: TensorScalar>(self) -> Result<(Vec<usize>, Vec<T>)>

Consume this tensor and return its owned column-major buffer when the dtype matches.

§Examples
use tenferro_tensor::Tensor;

let t = Tensor::from_vec_col_major(vec![1], vec![2.0_f64]).unwrap();
assert_eq!(t.into_vec_col_major::<f64>().unwrap().1, vec![2.0]);

Trait Implementations§

Source§

impl Clone for Tensor

Source§

fn clone(&self) -> Tensor

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 Debug for Tensor

Source§

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

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

impl From<TypedTensor<Complex<f32>>> for Tensor

Wrap a Complex32 TypedTensor into the corresponding Tensor variant.

§Examples

use num_complex::Complex32;
use tenferro_tensor::{Tensor, TypedTensor};

let typed = TypedTensor::from_vec_col_major(
    vec![1],
    vec![Complex32::new(1.0, 2.0)],
).unwrap();
let tensor: Tensor = typed.into();
assert_eq!(tensor.shape(), &[1]);
Source§

fn from(t: TypedTensor<Complex<f32>>) -> Self

Converts to this type from the input type.
Source§

impl From<TypedTensor<Complex<f64>>> for Tensor

Wrap a Complex64 TypedTensor into the corresponding Tensor variant.

§Examples

use num_complex::Complex64;
use tenferro_tensor::{Tensor, TypedTensor};

let typed = TypedTensor::from_vec_col_major(
    vec![1],
    vec![Complex64::new(1.0, 2.0)],
).unwrap();
let tensor: Tensor = typed.into();
assert_eq!(tensor.shape(), &[1]);
Source§

fn from(t: TypedTensor<Complex<f64>>) -> Self

Converts to this type from the input type.
Source§

impl From<TypedTensor<bool>> for Tensor

Wrap a bool TypedTensor into the corresponding Tensor variant.

§Examples

use tenferro_tensor::{DType, Tensor, TypedTensor};

let typed = TypedTensor::from_vec_col_major(vec![2], vec![true, false]).unwrap();
let tensor: Tensor = typed.into();
assert_eq!(tensor.dtype(), DType::Bool);
assert_eq!(tensor.shape(), &[2]);
Source§

fn from(t: TypedTensor<bool>) -> Self

Converts to this type from the input type.
Source§

impl From<TypedTensor<f32>> for Tensor

Wrap an f32 TypedTensor into the corresponding Tensor variant.

§Examples

use tenferro_tensor::{Tensor, TypedTensor};

let typed = TypedTensor::from_vec_col_major(vec![2], vec![1.0_f32, 2.0]).unwrap();
let tensor: Tensor = typed.into();
assert_eq!(tensor.shape(), &[2]);
Source§

fn from(t: TypedTensor<f32>) -> Self

Converts to this type from the input type.
Source§

impl From<TypedTensor<f64>> for Tensor

Wrap an f64 TypedTensor into the corresponding Tensor variant.

§Examples

use tenferro_tensor::{Tensor, TypedTensor};

let typed = TypedTensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0]).unwrap();
let tensor: Tensor = typed.into();
assert_eq!(tensor.shape(), &[2]);
Source§

fn from(t: TypedTensor<f64>) -> Self

Converts to this type from the input type.
Source§

impl From<TypedTensor<i32>> for Tensor

Wrap an i32 TypedTensor into the corresponding Tensor variant.

§Examples

use tenferro_tensor::{DType, Tensor, TypedTensor};

let typed = TypedTensor::from_vec_col_major(vec![2], vec![1_i32, 2]).unwrap();
let tensor: Tensor = typed.into();
assert_eq!(tensor.dtype(), DType::I32);
assert_eq!(tensor.shape(), &[2]);
Source§

fn from(t: TypedTensor<i32>) -> Self

Converts to this type from the input type.
Source§

impl From<TypedTensor<i64>> for Tensor

Wrap an i64 TypedTensor into the corresponding Tensor variant.

§Examples

use tenferro_tensor::{DType, Tensor, TypedTensor};

let typed = TypedTensor::from_vec_col_major(vec![2], vec![1_i64, 2]).unwrap();
let tensor: Tensor = typed.into();
assert_eq!(tensor.dtype(), DType::I64);
assert_eq!(tensor.shape(), &[2]);
Source§

fn from(t: TypedTensor<i64>) -> Self

Converts to this type from the input type.

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