Skip to main content

Tensor

Struct Tensor 

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

Dense, row-major tensor (shape + contiguous typed data).

Shape is listed outer-to-inner (C-order), matching typical NumPy layout.

Element dtype is always derived from the payload via Tensor::dtype.

Shape and data are private so callers cannot break the shape product == data.len() invariant after construction. Use Tensor::shape / Tensor::data accessors.

§PartialEq

Equality is exact element-wise (IEEE). In particular, NaN != NaN, matching Rust’s default float PartialEq.

§Serde

With the serde feature, tensors encode as { "shape": ..., "data": ... }. Deserialization always calls Tensor::new, preserving the private-field invariant. JSON is debug-only and cannot faithfully represent non-finite floats; HDF5 .nir remains the NIR interchange format.

Implementations§

Source§

impl Tensor

Source

pub fn new(shape: impl Into<Vec<usize>>, data: TensorData) -> Result<Self>

Build a tensor from shape and typed data, checking length against shape.

Source

pub const fn dtype(&self) -> DType

Element dtype of this tensor (derived from the payload).

Source

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

Axis lengths (outer-to-inner / C-order).

Source

pub fn data(&self) -> &TensorData

Contiguous payload.

Source

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

f32 tensor; data.len() must equal the product of shape.

Source

pub fn from_f64( shape: impl Into<Vec<usize>>, data: impl Into<Vec<f64>>, ) -> Result<Self>

f64 tensor; data.len() must equal the product of shape.

Source

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

i64 tensor; data.len() must equal the product of shape.

Source

pub fn from_bool( shape: impl Into<Vec<usize>>, data: impl Into<Vec<bool>>, ) -> Result<Self>

bool tensor; data.len() must equal the product of shape.

Source

pub fn scalar_f32(value: f32) -> Self

Rank-0 (scalar) f32 tensor.

Source

pub fn scalar_f64(value: f64) -> Self

Rank-0 (scalar) f64 tensor.

Source

pub fn scalar_i64(value: i64) -> Self

Rank-0 (scalar) i64 tensor.

Source

pub fn zeros_like(&self) -> Self

A tensor of zeros with the same shape and dtype as self.

Mirrors numpy.zeros_like, which upstream NIR uses to default absent optional wire fields (v_reset). DType::Bool zeroes to false.

Source

pub fn ones_like(&self) -> Self

A tensor of ones with the same shape and dtype as self.

Mirrors numpy.ones_like, which upstream NIR uses to default an absent w_in. DType::Bool ones to true.

Source

pub fn numel(&self) -> usize

Number of elements implied by shape (empty shape → 1).

Source

pub fn ndim(&self) -> usize

Number of dimensions.

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: &Tensor) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Tensor

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.