pub struct Tensor { /* private fields */ }Expand description
A compact, contiguous multi-dtype tensor representation.
Default dtype is F32. FP16 and BF16 dtypes are stored natively as u16 bit patterns
and can be created via from_f16 / from_bf16 or converted via to_dtype.
Implementations§
Source§impl Tensor
impl Tensor
Sourcepub fn trace(&self) -> Result<f32, TensorError>
pub fn trace(&self) -> Result<f32, TensorError>
Sum of diagonal elements of a 2D square matrix.
Sourcepub fn dot(&self, rhs: &Self) -> Result<f32, TensorError>
pub fn dot(&self, rhs: &Self) -> Result<f32, TensorError>
Dot product of two 1D tensors.
Sourcepub fn cross(&self, rhs: &Self) -> Result<Self, TensorError>
pub fn cross(&self, rhs: &Self) -> Result<Self, TensorError>
Cross product of two 3-element 1D tensors.
Sourcepub fn det(&self) -> Result<f32, TensorError>
pub fn det(&self) -> Result<f32, TensorError>
Determinant of a square matrix (LU-based).
Sourcepub fn inv(&self) -> Result<Self, TensorError>
pub fn inv(&self) -> Result<Self, TensorError>
Inverse of a square matrix (Gauss-Jordan elimination).
Sourcepub fn solve(&self, b: &Self) -> Result<Self, TensorError>
pub fn solve(&self, b: &Self) -> Result<Self, TensorError>
Solve linear system Ax = b. self is A, rhs is b.
Sourcepub fn qr(&self) -> Result<(Self, Self), TensorError>
pub fn qr(&self) -> Result<(Self, Self), TensorError>
QR decomposition via Householder reflections. Returns (Q, R).
Sourcepub fn cholesky(&self) -> Result<Self, TensorError>
pub fn cholesky(&self) -> Result<Self, TensorError>
Cholesky decomposition of a symmetric positive-definite matrix. Returns lower triangular L.
Source§impl Tensor
impl Tensor
Sourcepub fn add(&self, rhs: &Self) -> Result<Self, TensorError>
pub fn add(&self, rhs: &Self) -> Result<Self, TensorError>
Element-wise addition with NumPy-style broadcasting.
Sourcepub fn sub(&self, rhs: &Self) -> Result<Self, TensorError>
pub fn sub(&self, rhs: &Self) -> Result<Self, TensorError>
Element-wise subtraction with NumPy-style broadcasting.
Sourcepub fn mul(&self, rhs: &Self) -> Result<Self, TensorError>
pub fn mul(&self, rhs: &Self) -> Result<Self, TensorError>
Element-wise multiplication with NumPy-style broadcasting.
Sourcepub fn div(&self, rhs: &Self) -> Result<Self, TensorError>
pub fn div(&self, rhs: &Self) -> Result<Self, TensorError>
Element-wise division with NumPy-style broadcasting.
Sourcepub fn pow(&self, rhs: &Self) -> Result<Self, TensorError>
pub fn pow(&self, rhs: &Self) -> Result<Self, TensorError>
Element-wise power with NumPy-style broadcasting.
Fast paths:
- Constant exponent 2.0 →
mul(x, x)(SIMD-accelerated). - Constant exponent 0.5 →
sqrt(x)(SIMD-accelerated). - Same-shape general case → SIMD
exp(exp * ln(base)).
Sourcepub fn atan2(&self, rhs: &Self) -> Result<Self, TensorError>
pub fn atan2(&self, rhs: &Self) -> Result<Self, TensorError>
Element-wise atan2(self, other), with broadcasting.
Same-shape case uses a SIMD-friendly polynomial approximation of atan
with quadrant correction, avoiding scalar f32::atan2.
Sourcepub fn minimum(&self, rhs: &Self) -> Result<Self, TensorError>
pub fn minimum(&self, rhs: &Self) -> Result<Self, TensorError>
Element-wise minimum with NumPy-style broadcasting.
Sourcepub fn maximum(&self, rhs: &Self) -> Result<Self, TensorError>
pub fn maximum(&self, rhs: &Self) -> Result<Self, TensorError>
Element-wise maximum with NumPy-style broadcasting.
Sourcepub fn reciprocal(&self) -> Self
pub fn reciprocal(&self) -> Self
Element-wise reciprocal (1 / x).
Sourcepub fn scale(&self, factor: f32) -> Self
pub fn scale(&self, factor: f32) -> Self
Scalar multiplication (broadcast multiply by a constant).
Sourcepub fn add_scalar(&self, value: f32) -> Self
pub fn add_scalar(&self, value: f32) -> Self
Add a scalar to all elements.
Sourcepub fn max_value(&self) -> f32
pub fn max_value(&self) -> f32
Global max reduction. Returns f32::NEG_INFINITY for empty tensors.
Sourcepub fn sum_axis(&self, axis: usize) -> Result<Self, TensorError>
pub fn sum_axis(&self, axis: usize) -> Result<Self, TensorError>
Sum reduction over one axis. Reduced axis is removed from output shape.
Sourcepub fn mean_axis(&self, axis: usize) -> Result<Self, TensorError>
pub fn mean_axis(&self, axis: usize) -> Result<Self, TensorError>
Mean reduction over one axis. Reduced axis is removed from output shape.
Sourcepub fn max_axis(&self, axis: usize) -> Result<Self, TensorError>
pub fn max_axis(&self, axis: usize) -> Result<Self, TensorError>
Max reduction over one axis. Reduced axis is removed from output shape.
Sourcepub fn min_axis(&self, axis: usize) -> Result<Self, TensorError>
pub fn min_axis(&self, axis: usize) -> Result<Self, TensorError>
Min reduction over one axis. Reduced axis is removed from output shape.
Sourcepub fn var_axis(&self, axis: usize) -> Result<Self, TensorError>
pub fn var_axis(&self, axis: usize) -> Result<Self, TensorError>
Variance reduction over one axis (population variance).
Sourcepub fn median_axis(&self, axis: usize) -> Result<Self, TensorError>
pub fn median_axis(&self, axis: usize) -> Result<Self, TensorError>
Median along a given axis.
Sourcepub fn transpose_2d(&self) -> Result<Self, TensorError>
pub fn transpose_2d(&self) -> Result<Self, TensorError>
2D matrix transpose. Requires rank-2 input.
§Safety
AlignedVec::uninitialized allocates without zeroing. The tiled loop
writes every element before anything reads from the buffer.
Sourcepub fn permute(&self, axes: &[usize]) -> Result<Self, TensorError>
pub fn permute(&self, axes: &[usize]) -> Result<Self, TensorError>
General axis permutation (like NumPy transpose/permute).
Sourcepub fn unsqueeze(&self, axis: usize) -> Result<Self, TensorError>
pub fn unsqueeze(&self, axis: usize) -> Result<Self, TensorError>
Insert a length-1 dimension at the given axis.
Sourcepub fn squeeze(&self, axis: usize) -> Result<Self, TensorError>
pub fn squeeze(&self, axis: usize) -> Result<Self, TensorError>
Remove a length-1 dimension at the given axis.
Sourcepub fn cat(tensors: &[&Self], axis: usize) -> Result<Self, TensorError>
pub fn cat(tensors: &[&Self], axis: usize) -> Result<Self, TensorError>
Concatenate tensors along an axis. All tensors must have the same shape except along the concatenation axis.
Sourcepub fn stack(tensors: &[&Self], axis: usize) -> Result<Self, TensorError>
pub fn stack(tensors: &[&Self], axis: usize) -> Result<Self, TensorError>
Stack tensors along a new axis. All tensors must have identical shapes.
Sourcepub fn select(&self, axis: usize, index: usize) -> Result<Self, TensorError>
pub fn select(&self, axis: usize, index: usize) -> Result<Self, TensorError>
Select a single slice along an axis, removing that axis from the output.
Sourcepub fn narrow(
&self,
axis: usize,
start: usize,
length: usize,
) -> Result<Self, TensorError>
pub fn narrow( &self, axis: usize, start: usize, length: usize, ) -> Result<Self, TensorError>
Narrow (slice) along an axis: extract elements start..start+length.
Sourcepub fn eq_tensor(&self, rhs: &Self) -> Result<Self, TensorError>
pub fn eq_tensor(&self, rhs: &Self) -> Result<Self, TensorError>
Element-wise equality check: 1.0 where equal, 0.0 otherwise.
Sourcepub fn gt_tensor(&self, rhs: &Self) -> Result<Self, TensorError>
pub fn gt_tensor(&self, rhs: &Self) -> Result<Self, TensorError>
Element-wise greater-than: 1.0 where self > rhs, 0.0 otherwise.
Sourcepub fn lt_tensor(&self, rhs: &Self) -> Result<Self, TensorError>
pub fn lt_tensor(&self, rhs: &Self) -> Result<Self, TensorError>
Element-wise less-than: 1.0 where self < rhs, 0.0 otherwise.
Sourcepub fn gt_tensor_into(&self, rhs: &Self, output: &mut Self)
pub fn gt_tensor_into(&self, rhs: &Self, output: &mut Self)
Element-wise greater-than writing into a pre-allocated output tensor.
self, rhs, and output must all have the same shape.
Sourcepub fn eq_tensor_into(&self, rhs: &Self, output: &mut Self)
pub fn eq_tensor_into(&self, rhs: &Self, output: &mut Self)
Element-wise equality check writing into a pre-allocated output tensor.
self, rhs, and output must all have the same shape.
Sourcepub fn lt_tensor_into(&self, rhs: &Self, output: &mut Self)
pub fn lt_tensor_into(&self, rhs: &Self, output: &mut Self)
Element-wise less-than writing into a pre-allocated output tensor.
self, rhs, and output must all have the same shape.
Sourcepub fn ne_tensor(&self, rhs: &Self) -> Result<Self, TensorError>
pub fn ne_tensor(&self, rhs: &Self) -> Result<Self, TensorError>
Element-wise not-equal: 1.0 where not equal (diff.abs() >= 1e-7), 0.0 otherwise.
Sourcepub fn le_tensor(&self, rhs: &Self) -> Result<Self, TensorError>
pub fn le_tensor(&self, rhs: &Self) -> Result<Self, TensorError>
Element-wise less-than-or-equal: 1.0 where self <= rhs, 0.0 otherwise.
Sourcepub fn ge_tensor(&self, rhs: &Self) -> Result<Self, TensorError>
pub fn ge_tensor(&self, rhs: &Self) -> Result<Self, TensorError>
Element-wise greater-than-or-equal: 1.0 where self >= rhs, 0.0 otherwise.
Sourcepub fn all_finite(&self) -> bool
pub fn all_finite(&self) -> bool
Returns true if all elements are finite (no NaN or Inf).
Sourcepub fn where_cond(
&self,
condition: &Self,
other: &Self,
) -> Result<Self, TensorError>
pub fn where_cond( &self, condition: &Self, other: &Self, ) -> Result<Self, TensorError>
Element-wise where: condition ? self : other.
condition has 1.0 for true, 0.0 for false.
Sourcepub fn masked_fill(&self, mask: &Self, value: f32) -> Result<Self, TensorError>
pub fn masked_fill(&self, mask: &Self, value: f32) -> Result<Self, TensorError>
Replace elements where mask != 0 with value.
Sourcepub fn scatter(
&self,
axis: usize,
index: &Self,
src: &Self,
) -> Result<Self, TensorError>
pub fn scatter( &self, axis: usize, index: &Self, src: &Self, ) -> Result<Self, TensorError>
Scatter values into self along axis at positions given by index.
src provides the values. index has same shape as src.
Sourcepub fn gather(&self, axis: usize, index: &Self) -> Result<Self, TensorError>
pub fn gather(&self, axis: usize, index: &Self) -> Result<Self, TensorError>
Gather elements along axis at positions given by index.
Sourcepub fn topk(&self, k: usize) -> Result<(Self, Self), TensorError>
pub fn topk(&self, k: usize) -> Result<(Self, Self), TensorError>
Returns the top-k values and their indices along the last axis.
Sourcepub fn triu(&self, diagonal: i64) -> Result<Self, TensorError>
pub fn triu(&self, diagonal: i64) -> Result<Self, TensorError>
Upper triangular mask: zero below diagonal, keep above.
diagonal shifts: 0 = main, positive = above, negative = below.
Sourcepub fn tril(&self, diagonal: i64) -> Result<Self, TensorError>
pub fn tril(&self, diagonal: i64) -> Result<Self, TensorError>
Lower triangular mask: zero above diagonal, keep below.
Sourcepub fn eye(n: usize) -> Result<Self, TensorError>
pub fn eye(n: usize) -> Result<Self, TensorError>
Identity matrix [n, n].
Sourcepub fn diag(vector: &Tensor) -> Result<Self, TensorError>
pub fn diag(vector: &Tensor) -> Result<Self, TensorError>
Create a diagonal matrix from a 1D vector.
Sourcepub fn diag_extract(&self) -> Result<Self, TensorError>
pub fn diag_extract(&self) -> Result<Self, TensorError>
Extract the diagonal of a 2D matrix as a 1D vector.
Sourcepub fn pad(
&self,
padding: &[(usize, usize)],
value: f32,
) -> Result<Self, TensorError>
pub fn pad( &self, padding: &[(usize, usize)], value: f32, ) -> Result<Self, TensorError>
Pad the tensor with a constant value. padding is a slice of (before, after) per dimension.
Sourcepub fn repeat(&self, counts: &[usize]) -> Result<Self, TensorError>
pub fn repeat(&self, counts: &[usize]) -> Result<Self, TensorError>
Repeat tensor along each axis by the given counts.
Sourcepub fn cumsum(&self, axis: usize) -> Result<Self, TensorError>
pub fn cumsum(&self, axis: usize) -> Result<Self, TensorError>
Cumulative sum along an axis.
Sourcepub fn cumprod(&self, axis: usize) -> Result<Self, TensorError>
pub fn cumprod(&self, axis: usize) -> Result<Self, TensorError>
Cumulative product along an axis.
Sourcepub fn to_fp16(&self) -> Vec<u16>
pub fn to_fp16(&self) -> Vec<u16>
Convert all elements to IEEE 754 half-precision (FP16) bytes.
Returns Vec<u16> where each u16 is an FP16 bit pattern.
Sourcepub fn from_fp16(
shape: Vec<usize>,
fp16_data: &[u16],
) -> Result<Self, TensorError>
pub fn from_fp16( shape: Vec<usize>, fp16_data: &[u16], ) -> Result<Self, TensorError>
Create a tensor from FP16 bit patterns.
Sourcepub fn relu_inplace(&mut self)
pub fn relu_inplace(&mut self)
In-place ReLU: clamp negative values to zero.
Sourcepub fn add_inplace(&mut self, rhs: &Self)
pub fn add_inplace(&mut self, rhs: &Self)
In-place element-wise add from another tensor (must have same shape).
Sourcepub fn add_scalar_inplace(&mut self, s: f32)
pub fn add_scalar_inplace(&mut self, s: f32)
In-place add scalar to all elements.
Sourcepub fn mul_scalar_inplace(&mut self, s: f32)
pub fn mul_scalar_inplace(&mut self, s: f32)
In-place multiply all elements by scalar.
Sourcepub fn add_into(output: &mut Self, lhs: &Self, rhs: &Self)
pub fn add_into(output: &mut Self, lhs: &Self, rhs: &Self)
Element-wise addition writing into a pre-allocated output tensor.
output, lhs, and rhs must all have the same shape.
Source§impl Tensor
impl Tensor
Sourcepub fn sort(
&self,
dim: usize,
descending: bool,
) -> Result<(Self, Self), TensorError>
pub fn sort( &self, dim: usize, descending: bool, ) -> Result<(Self, Self), TensorError>
Sort along dim. Returns (sorted_values, sorted_indices).
If descending is true, values are sorted largest-first.
Sourcepub fn argsort(&self, dim: usize, descending: bool) -> Result<Self, TensorError>
pub fn argsort(&self, dim: usize, descending: bool) -> Result<Self, TensorError>
Return indices that would sort along dim.
Sourcepub fn unique(&self) -> (Self, Self, Self)
pub fn unique(&self) -> (Self, Self, Self)
Return unique elements (sorted), inverse indices, and counts.
Sourcepub fn flip(&self, dims: &[usize]) -> Result<Self, TensorError>
pub fn flip(&self, dims: &[usize]) -> Result<Self, TensorError>
Reverse elements along the given dimensions.
Sourcepub fn roll(&self, shift: i64, dim: usize) -> Result<Self, TensorError>
pub fn roll(&self, shift: i64, dim: usize) -> Result<Self, TensorError>
Circular shift elements along dim by shift positions.
Sourcepub fn linspace(start: f32, end: f32, steps: usize) -> Result<Self, TensorError>
pub fn linspace(start: f32, end: f32, steps: usize) -> Result<Self, TensorError>
Create a 1-D tensor of steps evenly spaced values from start to end (inclusive).
Sourcepub fn arange(start: f32, end: f32, step: f32) -> Result<Self, TensorError>
pub fn arange(start: f32, end: f32, step: f32) -> Result<Self, TensorError>
Create a 1-D tensor with values in [start, end) with given step.
Sourcepub fn meshgrid(tensors: &[Self]) -> Result<Vec<Self>, TensorError>
pub fn meshgrid(tensors: &[Self]) -> Result<Vec<Self>, TensorError>
Create coordinate grids from 1-D tensors (numpy-style meshgrid with indexing='ij').
Sourcepub fn boolean_mask(&self, mask: &Self) -> Result<Self, TensorError>
pub fn boolean_mask(&self, mask: &Self) -> Result<Self, TensorError>
Select elements where mask (f32, nonzero = true) is true, returned as 1-D.
Sourcepub fn index_select(
&self,
dim: usize,
indices: &Self,
) -> Result<Self, TensorError>
pub fn index_select( &self, dim: usize, indices: &Self, ) -> Result<Self, TensorError>
Select slices along dim using integer indices tensor (1-D).
Sourcepub fn rand(shape: Vec<usize>, seed: u64) -> Result<Self, TensorError>
pub fn rand(shape: Vec<usize>, seed: u64) -> Result<Self, TensorError>
Create a tensor filled with uniform random values in [0, 1).
Sourcepub fn randn(shape: Vec<usize>, seed: u64) -> Result<Self, TensorError>
pub fn randn(shape: Vec<usize>, seed: u64) -> Result<Self, TensorError>
Create a tensor filled with normally distributed random values (mean=0, std=1). Uses Box-Muller transform.
Source§impl Tensor
impl Tensor
Sourcepub fn step_slice(
&self,
dim: usize,
start: usize,
end: usize,
step: usize,
) -> Result<Self, TensorError>
pub fn step_slice( &self, dim: usize, start: usize, end: usize, step: usize, ) -> Result<Self, TensorError>
Slice with step: extract every step-th element along dim from start to end.
Sourcepub fn einsum(
equation: &str,
tensors: &[&Tensor],
) -> Result<Tensor, TensorError>
pub fn einsum( equation: &str, tensors: &[&Tensor], ) -> Result<Tensor, TensorError>
Einstein summation for common patterns.
Supported equations:
"ij,jk->ik"— matrix multiply"ij->ji"— transpose"ii->i"— diagonal"ij->i"— row sum"ij->j"— column sum"ij->"— total sum"i,i->"— dot product"ij,ij->"— Frobenius inner product
Sourcepub fn chunk(
&self,
n_chunks: usize,
axis: usize,
) -> Result<Vec<Self>, TensorError>
pub fn chunk( &self, n_chunks: usize, axis: usize, ) -> Result<Vec<Self>, TensorError>
Split tensor into n_chunks pieces along axis. Last chunk may be smaller.
Sourcepub fn histogram(
&self,
bins: usize,
min: f32,
max: f32,
) -> Result<Self, TensorError>
pub fn histogram( &self, bins: usize, min: f32, max: f32, ) -> Result<Self, TensorError>
Counts elements in each bin, returns 1D tensor of shape [bins].
Bins are evenly spaced between min and max.
Sourcepub fn bincount(&self, num_bins: usize) -> Result<Self, TensorError>
pub fn bincount(&self, num_bins: usize) -> Result<Self, TensorError>
Treats values as integer indices, counts occurrences.
Returns 1D tensor of shape [num_bins].
Sourcepub fn item(&self) -> Result<f32, TensorError>
pub fn item(&self) -> Result<f32, TensorError>
Returns the single scalar value if tensor has exactly one element. Errors if tensor has more than one element.
Sourcepub fn scatter_add(
&self,
dim: usize,
index: &Self,
src: &Self,
) -> Result<Self, TensorError>
pub fn scatter_add( &self, dim: usize, index: &Self, src: &Self, ) -> Result<Self, TensorError>
Like scatter but adds instead of replacing values.
For dim=1: self[i][index[i][j][k]][k] += src[i][j][k]
Source§impl Tensor
impl Tensor
Sourcepub fn from_raw_parts(
shape: &[usize],
strides: &[usize],
data: AlignedVec<f32>,
) -> Self
pub fn from_raw_parts( shape: &[usize], strides: &[usize], data: AlignedVec<f32>, ) -> Self
Creates a tensor from pre-validated shape, strides, and data. No validation, no heap allocation (shape/strides stored inline for ≤6D).
Sourcepub fn from_aligned(
shape: Vec<usize>,
data: AlignedVec<f32>,
) -> Result<Self, TensorError>
pub fn from_aligned( shape: Vec<usize>, data: AlignedVec<f32>, ) -> Result<Self, TensorError>
Builds a tensor from shape and a pre-filled AlignedVec.
This avoids the extra copy that from_vec performs when
converting a Vec<f32> into aligned storage. Use this when the output
buffer was already allocated as an AlignedVec (e.g. via
AlignedVec::uninitialized filled by a BLAS/SIMD kernel).
Sourcepub fn from_vec(shape: Vec<usize>, data: Vec<f32>) -> Result<Self, TensorError>
pub fn from_vec(shape: Vec<usize>, data: Vec<f32>) -> Result<Self, TensorError>
Builds a tensor from shape and raw contiguous f32 data.
Sourcepub fn from_f16(shape: Vec<usize>, data: Vec<u16>) -> Result<Self, TensorError>
pub fn from_f16(shape: Vec<usize>, data: Vec<u16>) -> Result<Self, TensorError>
Builds a tensor from shape and raw FP16 bit patterns (u16).
Sourcepub fn from_bf16(shape: Vec<usize>, data: Vec<u16>) -> Result<Self, TensorError>
pub fn from_bf16(shape: Vec<usize>, data: Vec<u16>) -> Result<Self, TensorError>
Builds a tensor from shape and raw BF16 bit patterns (u16).
Sourcepub fn from_slice(data: &[f32]) -> Self
pub fn from_slice(data: &[f32]) -> Self
Builds a 1-D tensor from a slice. Equivalent to from_vec(vec![data.len()], data.to_vec()).
Sourcepub fn filled(shape: Vec<usize>, value: f32) -> Result<Self, TensorError>
pub fn filled(shape: Vec<usize>, value: f32) -> Result<Self, TensorError>
Builds a value-initialized tensor for a given shape.
Alias: full is provided as a more familiar name.
Sourcepub fn zeros(shape: Vec<usize>) -> Result<Self, TensorError>
pub fn zeros(shape: Vec<usize>) -> Result<Self, TensorError>
Builds a zero-initialized tensor for a given shape.
Uses alloc_zeroed under the hood so the OS can provide pre-zeroed pages
without writing every byte — significantly faster for large tensors.
Sourcepub fn ones(shape: Vec<usize>) -> Result<Self, TensorError>
pub fn ones(shape: Vec<usize>) -> Result<Self, TensorError>
Builds a one-initialized tensor for a given shape.
Sourcepub fn full(shape: Vec<usize>, value: f32) -> Result<Self, TensorError>
pub fn full(shape: Vec<usize>, value: f32) -> Result<Self, TensorError>
Builds a tensor filled with value. Alias for filled.
Sourcepub fn to_device(&self, device: Device) -> Self
pub fn to_device(&self, device: Device) -> Self
Returns a copy of this tensor tagged with the given device.
This is currently a metadata-only operation — no actual data transfer
occurs. GPU data movement is handled by GpuSession in yscv-kernels.
Sourcepub fn try_data(&self) -> Result<&[f32], TensorError>
pub fn try_data(&self) -> Result<&[f32], TensorError>
Fallible version of data() — returns an error for non-F32 tensors.
Sourcepub fn try_data_mut(&mut self) -> Result<&mut [f32], TensorError>
pub fn try_data_mut(&mut self) -> Result<&mut [f32], TensorError>
Fallible version of data_mut() — returns an error for non-F32 tensors.
Sourcepub fn data(&self) -> &[f32]
pub fn data(&self) -> &[f32]
Returns an immutable view over contiguous f32 storage.
§Panics
Panics if the tensor dtype is not F32. Use try_data() for a fallible version.
Sourcepub fn data_mut(&mut self) -> &mut [f32]
pub fn data_mut(&mut self) -> &mut [f32]
Returns a mutable view over contiguous f32 storage.
§Panics
Panics if the tensor dtype is not F32. Use try_data_mut() for a fallible version.
Sourcepub fn try_data_f32(&self) -> Result<&[f32], TensorError>
pub fn try_data_f32(&self) -> Result<&[f32], TensorError>
Alias for try_data() for backward compatibility.
Sourcepub fn data_f16(&self) -> Result<&[u16], TensorError>
pub fn data_f16(&self) -> Result<&[u16], TensorError>
Returns raw FP16 bit-pattern data if dtype is F16.
Sourcepub fn data_bf16(&self) -> Result<&[u16], TensorError>
pub fn data_bf16(&self) -> Result<&[u16], TensorError>
Returns raw BF16 bit-pattern data if dtype is BF16.
Sourcepub fn to_dtype(&self, target: DType) -> Self
pub fn to_dtype(&self, target: DType) -> Self
Converts this tensor to the specified dtype, returning a new tensor. Converting to the same dtype is a no-op clone.
Sourcepub fn get(&self, indices: &[usize]) -> Result<f32, TensorError>
pub fn get(&self, indices: &[usize]) -> Result<f32, TensorError>
Reads one element by multi-dimensional index (always returns f32).
Sourcepub fn set(&mut self, indices: &[usize], value: f32) -> Result<(), TensorError>
pub fn set(&mut self, indices: &[usize], value: f32) -> Result<(), TensorError>
Writes one element by multi-dimensional index (stores as native dtype).
Sourcepub fn reshape(&self, new_shape: Vec<usize>) -> Result<Self, TensorError>
pub fn reshape(&self, new_shape: Vec<usize>) -> Result<Self, TensorError>
Returns a reshaped tensor view with copied metadata and data.
Sourcepub fn into_reshape(self, new_shape: Vec<usize>) -> Result<Self, TensorError>
pub fn into_reshape(self, new_shape: Vec<usize>) -> Result<Self, TensorError>
Consumes the tensor and returns a reshaped version without copying data.
Trait Implementations§
impl StructuralPartialEq for Tensor
Auto Trait Implementations§
impl Freeze for Tensor
impl RefUnwindSafe for Tensor
impl Send for Tensor
impl Sync for Tensor
impl Unpin for Tensor
impl UnsafeUnpin for Tensor
impl UnwindSafe for Tensor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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