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
impl Tensor
Sourcepub fn new(shape: impl Into<Vec<usize>>, data: TensorData) -> Result<Self>
pub fn new(shape: impl Into<Vec<usize>>, data: TensorData) -> Result<Self>
Build a tensor from shape and typed data, checking length against shape.
Sourcepub fn data(&self) -> &TensorData
pub fn data(&self) -> &TensorData
Contiguous payload.
Sourcepub fn from_f32(
shape: impl Into<Vec<usize>>,
data: impl Into<Vec<f32>>,
) -> Result<Self>
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.
Sourcepub fn from_f64(
shape: impl Into<Vec<usize>>,
data: impl Into<Vec<f64>>,
) -> Result<Self>
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.
Sourcepub fn from_i64(
shape: impl Into<Vec<usize>>,
data: impl Into<Vec<i64>>,
) -> Result<Self>
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.
Sourcepub fn from_bool(
shape: impl Into<Vec<usize>>,
data: impl Into<Vec<bool>>,
) -> Result<Self>
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.
Sourcepub fn scalar_f32(value: f32) -> Self
pub fn scalar_f32(value: f32) -> Self
Rank-0 (scalar) f32 tensor.
Sourcepub fn scalar_f64(value: f64) -> Self
pub fn scalar_f64(value: f64) -> Self
Rank-0 (scalar) f64 tensor.
Sourcepub fn scalar_i64(value: i64) -> Self
pub fn scalar_i64(value: i64) -> Self
Rank-0 (scalar) i64 tensor.
Sourcepub fn zeros_like(&self) -> Self
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.
Sourcepub fn ones_like(&self) -> Self
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.