pub struct Matrix { /* private fields */ }Expand description
Device-visible numeric storage with explicit shape and dtype semantics.
Arithmetic operators delegate to crate::matrix and return Result<Matrix>:
use let sum = (&left + &right)?;. They preserve validation, allocation, and
recording failures without panicking. Multiplication is elementwise; use
crate::matrix::mat_mul_nt for matrix multiplication.
Implementations§
Source§impl Matrix
impl Matrix
Sourcepub fn new_from_slice<T: Element>(
shape: impl Into<Vec<usize>>,
values: &[T],
) -> Result<Self>
pub fn new_from_slice<T: Element>( shape: impl Into<Vec<usize>>, values: &[T], ) -> Result<Self>
Create a dense matrix from an admitted Rust scalar slice.
The element type selects the matrix dtype exactly; this function performs no numeric conversion.
§Errors
Returns an error when the shape overflows, its element count differs from
values, or storage allocation/upload fails.
This variant uploads on this thread’s default engine. An engine initialization failure is returned before the upload starts.
Sourcepub fn new_from_slice_on<T: Element>(
engine: &Engine,
shape: impl Into<Vec<usize>>,
values: &[T],
) -> Result<Self>
pub fn new_from_slice_on<T: Element>( engine: &Engine, shape: impl Into<Vec<usize>>, values: &[T], ) -> Result<Self>
Create a dense matrix on a particular engine.
This is the new_from_slice contract evaluated
on engine instead of this thread’s default engine.
§Errors
Returns the same errors as new_from_slice.
Sourcepub fn new_from_slice_on_same_engine<T: Element>(
&self,
shape: impl Into<Vec<usize>>,
values: &[T],
) -> Result<Self>
pub fn new_from_slice_on_same_engine<T: Element>( &self, shape: impl Into<Vec<usize>>, values: &[T], ) -> Result<Self>
Create a dense matrix on the engine that owns self.
The element type selects the matrix dtype exactly; this function performs no numeric conversion.
§Errors
Returns an error when the shape overflows, its element count differs from
values, or storage allocation/upload fails.
Sourcepub fn same_engine(&self, other: &Matrix) -> bool
pub fn same_engine(&self, other: &Matrix) -> bool
Return whether other is owned by the same engine as self.
Sourcepub fn new_from_f32(
shape: impl Into<Vec<usize>>,
values: &[f32],
) -> Result<Self>
pub fn new_from_f32( shape: impl Into<Vec<usize>>, values: &[f32], ) -> Result<Self>
Create an FP32 matrix and initialize its device-visible storage.
§Errors
Returns an error when the shape overflows, its element count differs from
values, or storage allocation/upload fails.
This variant uploads on this thread’s default engine. An engine initialization failure is returned before the upload starts.
Sourcepub fn new_from_f32_on(
engine: &Engine,
shape: impl Into<Vec<usize>>,
values: &[f32],
) -> Result<Self>
pub fn new_from_f32_on( engine: &Engine, shape: impl Into<Vec<usize>>, values: &[f32], ) -> Result<Self>
Create an FP32 matrix and initialize its device-visible storage on a particular engine.
This is the new_from_f32 contract evaluated on
engine instead of this thread’s default engine.
§Errors
Returns an error when the shape overflows, its element count differs from
values, or storage allocation/upload fails.
Sourcepub const fn num_elements(&self) -> usize
pub const fn num_elements(&self) -> usize
Return the number of logical dense elements.
Sourcepub fn flatten(&self) -> Result<Self>
pub fn flatten(&self) -> Result<Self>
Return a zero-copy one-dimensional view over all elements.
Equivalent to self.reshape([self.num_elements()]).
§Errors
Returns an error when the element count overflows or storage allocation fails.
Sourcepub fn unsqueeze(&self, dim: usize) -> Result<Self>
pub fn unsqueeze(&self, dim: usize) -> Result<Self>
Return a zero-copy view with a new size-one axis inserted at dim.
dim must be in 0..=self.shape().len(). Negative indices are not
supported; use self.shape().len() to append at the end.
§Errors
Returns an error when dim is out of range.
Sourcepub fn squeeze(&self, dim: usize) -> Result<Self>
pub fn squeeze(&self, dim: usize) -> Result<Self>
Return a zero-copy view with one size-one axis removed.
dim must be in 0..self.shape().len() and the selected axis must have
extent one. Negative indices are not supported.
§Errors
Returns an error when dim is out of range or the selected axis is not
size one.
Sourcepub fn read_f32(&self) -> Result<Vec<f32>>
pub fn read_f32(&self) -> Result<Vec<f32>>
Read all FP32 values back to host memory.
§Errors
This is an explicit host-observation boundary: it submits the pending eager batch when this matrix is still recorded, then waits for the exact GPU event that produced it before mapping its storage. Returns an error when this is not an FP32 matrix or when submission, completion, mapping, or cache invalidation fails.
Sourcepub fn try_read_f32(&self) -> Result<Vec<f32>>
pub fn try_read_f32(&self) -> Result<Vec<f32>>
Attempt to read all FP32 values without waiting for pending GPU work.
§Errors
Returns an error when this is not an FP32 matrix,
crate::ErrorKind::NotReady when GPU production is pending, or an error
when mapping or cache invalidation fails.
Sourcepub fn read<T: Element>(&self) -> Result<Vec<T>>
pub fn read<T: Element>(&self) -> Result<Vec<T>>
Read all values into their exact admitted Rust scalar representation.
§Errors
Returns an error before submission or waiting when T does not match this
matrix’s dtype. Otherwise this submits its pending eager batch when needed,
waits for the exact producing event, and returns an error when submission,
completion, mapping, or cache invalidation fails.
Sourcepub fn try_read<T: Element>(&self) -> Result<Vec<T>>
pub fn try_read<T: Element>(&self) -> Result<Vec<T>>
Attempt typed readback without waiting for pending GPU work.
§Errors
Returns an error when T does not match this matrix’s dtype,
crate::ErrorKind::NotReady when production is pending, or an error
when mapping or cache invalidation fails.
Sourcepub fn value_id(&self) -> u64
pub fn value_id(&self) -> u64
Stable identity of this value for plan-validity comparisons.
Two matrices constructed independently never share an id; views and clones of one allocation do. Compiled-plan owners (OA-CC-20) compare identities to reject replaying a captured plan against different storage.
Source§impl Matrix
impl Matrix
Sourcepub fn add(&self, right: &Matrix) -> Result<Self>
pub fn add(&self, right: &Matrix) -> Result<Self>
Add two matrices elementwise using multidirectional broadcasting.
Delegates to crate::matrix::add.
Sourcepub fn bias_add(&self, right: &Matrix) -> Result<Self>
pub fn bias_add(&self, right: &Matrix) -> Result<Self>
Add a bias matrix to another matrix elementwise using multidirectional broadcasting.
Delegates to crate::matrix::bias_add.
Sourcepub fn sub(&self, right: &Matrix) -> Result<Self>
pub fn sub(&self, right: &Matrix) -> Result<Self>
Subtract two FP32 matrices elementwise using multidirectional broadcasting.
Delegates to crate::matrix::sub.
Sourcepub fn mul(&self, right: &Matrix) -> Result<Self>
pub fn mul(&self, right: &Matrix) -> Result<Self>
Multiply two FP32 matrices elementwise.
Delegates to crate::matrix::mul.
Sourcepub fn div(&self, right: &Matrix) -> Result<Self>
pub fn div(&self, right: &Matrix) -> Result<Self>
Divide two FP32 matrices elementwise using multidirectional broadcasting.
Delegates to crate::matrix::div.
Sourcepub fn scale(&self, scalar: f32) -> Result<Self>
pub fn scale(&self, scalar: f32) -> Result<Self>
Multiply every FP32 matrix element by a scalar.
Delegates to crate::matrix::scale.
Sourcepub fn neg(&self) -> Result<Self>
pub fn neg(&self) -> Result<Self>
Negate every FP32 matrix element.
Delegates to crate::matrix::neg.
Sourcepub fn abs(&self) -> Result<Self>
pub fn abs(&self) -> Result<Self>
Take the absolute value of every FP32 matrix element.
Delegates to crate::matrix::abs.
Sourcepub fn log(&self) -> Result<Self>
pub fn log(&self) -> Result<Self>
Take the natural logarithm of every FP32 matrix element.
Delegates to crate::matrix::log.
Sourcepub fn sqrt(&self) -> Result<Self>
pub fn sqrt(&self) -> Result<Self>
Take the square root of every FP32 matrix element.
Delegates to crate::matrix::sqrt.
Sourcepub fn pow(&self, exponent: f32) -> Result<Self>
pub fn pow(&self, exponent: f32) -> Result<Self>
Raise every FP32 matrix element to a scalar exponent.
Delegates to crate::matrix::pow.
Sourcepub fn add_scalar(&self, scalar: f32) -> Result<Self>
pub fn add_scalar(&self, scalar: f32) -> Result<Self>
Add a scalar to every FP32 matrix element.
Delegates to crate::matrix::add_scalar.
Sourcepub fn sub_scalar(&self, scalar: f32) -> Result<Self>
pub fn sub_scalar(&self, scalar: f32) -> Result<Self>
Subtract a scalar from every FP32 matrix element.
Delegates to crate::matrix::sub_scalar.
Sourcepub fn div_scalar(&self, scalar: f32) -> Result<Self>
pub fn div_scalar(&self, scalar: f32) -> Result<Self>
Divide every FP32 matrix element by a scalar.
Delegates to crate::matrix::div_scalar.
Sourcepub fn exp(&self) -> Result<Self>
pub fn exp(&self) -> Result<Self>
Take the exponential of every FP32 matrix element.
Delegates to crate::matrix::exp.
Sourcepub fn sin(&self) -> Result<Self>
pub fn sin(&self) -> Result<Self>
Take the sine of every FP32 matrix element.
Delegates to crate::matrix::sin.
Sourcepub fn cos(&self) -> Result<Self>
pub fn cos(&self) -> Result<Self>
Take the cosine of every FP32 matrix element.
Delegates to crate::matrix::cos.
Sourcepub fn reciprocal(&self) -> Result<Self>
pub fn reciprocal(&self) -> Result<Self>
Take the reciprocal of every FP32 matrix element.
Delegates to crate::matrix::reciprocal.
Sourcepub fn clamp_max(&self, max: f32) -> Result<Self>
pub fn clamp_max(&self, max: f32) -> Result<Self>
Cap every FP32 matrix element at an upper bound.
Delegates to crate::matrix::clamp_max.
Sourcepub fn clamp_min(&self, min: f32) -> Result<Self>
pub fn clamp_min(&self, min: f32) -> Result<Self>
Raise every FP32 matrix element to a lower bound.
Delegates to crate::matrix::clamp_min.
Sourcepub fn copy(&self) -> Result<Self>
pub fn copy(&self) -> Result<Self>
Deep-copy a dense matrix into independent device storage.
Delegates to crate::matrix::copy.
Sourcepub fn mat_mul_nt(&self, right: &Matrix) -> Result<Self>
pub fn mat_mul_nt(&self, right: &Matrix) -> Result<Self>
Multiply A by the transpose of B using the OA weight-layout convention.
Delegates to crate::matrix::mat_mul_nt.
Sourcepub fn softmax(&self, dim: i32) -> Result<Self>
pub fn softmax(&self, dim: i32) -> Result<Self>
Delegates to crate::matrix::softmax.
Sourcepub fn log_softmax(&self, dim: i32) -> Result<Self>
pub fn log_softmax(&self, dim: i32) -> Result<Self>
Delegates to crate::matrix::log_softmax.
Sourcepub fn sum(&self, dim: i32) -> Result<Self>
pub fn sum(&self, dim: i32) -> Result<Self>
Delegates to crate::matrix::sum.
Sourcepub fn mean(&self, dim: i32) -> Result<Self>
pub fn mean(&self, dim: i32) -> Result<Self>
Delegates to crate::matrix::mean.
Sourcepub fn max(&self, dim: i32) -> Result<Self>
pub fn max(&self, dim: i32) -> Result<Self>
Delegates to crate::matrix::max.
Sourcepub fn categorical_accuracy_count(&self, labels: &Matrix) -> Result<Self>
pub fn categorical_accuracy_count(&self, labels: &Matrix) -> Result<Self>
Delegates to crate::matrix::categorical_accuracy_count.
Sourcepub fn masked_categorical_accuracy_count(
&self,
labels: &Matrix,
mask: &Matrix,
) -> Result<Self>
pub fn masked_categorical_accuracy_count( &self, labels: &Matrix, mask: &Matrix, ) -> Result<Self>
Delegates to crate::matrix::masked_categorical_accuracy_count.
Sourcepub fn philox_uniform(&self, low: f32, high: f32, seed: u64) -> Result<Self>
pub fn philox_uniform(&self, low: f32, high: f32, seed: u64) -> Result<Self>
Delegates to crate::matrix::philox_uniform.
Sourcepub fn philox_normal(&self, mean: f32, stddev: f32, seed: u64) -> Result<Self>
pub fn philox_normal(&self, mean: f32, stddev: f32, seed: u64) -> Result<Self>
Delegates to crate::matrix::philox_normal.
Sourcepub fn dropout(&self, probability: f32, seed: u64) -> Result<Self>
pub fn dropout(&self, probability: f32, seed: u64) -> Result<Self>
Delegates to crate::matrix::dropout.
Sourcepub fn sample_logits(
&self,
temperature: f32,
top_k: i32,
top_p: f32,
seed: u64,
) -> Result<Self>
pub fn sample_logits( &self, temperature: f32, top_k: i32, top_p: f32, seed: u64, ) -> Result<Self>
Delegates to crate::matrix::sample_logits.
Sourcepub fn top_k(&self, k: i32, dim: i32) -> Result<TopKResult>
pub fn top_k(&self, k: i32, dim: i32) -> Result<TopKResult>
Delegates to crate::matrix::top_k.
Sourcepub fn top_k_mask(&self, num_experts: usize) -> Result<Self>
pub fn top_k_mask(&self, num_experts: usize) -> Result<Self>
Delegates to crate::matrix::top_k_mask.
Sourcepub fn moe_expert_plan(&self, num_experts: usize) -> Result<MoeExpertPlan>
pub fn moe_expert_plan(&self, num_experts: usize) -> Result<MoeExpertPlan>
Delegates to crate::matrix::moe_expert_plan.
Sourcepub fn moe_routing_bias_update(
&self,
bias: &Matrix,
experts_per_token: usize,
gamma: f32,
) -> Result<()>
pub fn moe_routing_bias_update( &self, bias: &Matrix, experts_per_token: usize, gamma: f32, ) -> Result<()>
Delegates to crate::matrix::moe_routing_bias_update.
Sourcepub fn slice(&self, dim: i32, start: i64, end: i64) -> Result<Self>
pub fn slice(&self, dim: i32, start: i64, end: i64) -> Result<Self>
Delegates to crate::matrix::slice.
Sourcepub fn pad(&self, pads: &[usize], mode: PadMode, fill: f32) -> Result<Self>
pub fn pad(&self, pads: &[usize], mode: PadMode, fill: f32) -> Result<Self>
Delegates to crate::matrix::pad.
Sourcepub fn repeat_interleave(&self, repeats: usize, dim: i32) -> Result<Self>
pub fn repeat_interleave(&self, repeats: usize, dim: i32) -> Result<Self>
Delegates to crate::matrix::repeat_interleave.
Sourcepub fn gather(&self, indices: &Matrix) -> Result<Self>
pub fn gather(&self, indices: &Matrix) -> Result<Self>
Delegates to crate::matrix::gather.
Sourcepub fn gather_last_dim(&self, indices: &Matrix) -> Result<Self>
pub fn gather_last_dim(&self, indices: &Matrix) -> Result<Self>
Delegates to crate::matrix::gather_last_dim.
Sourcepub fn transpose(&self, dim0: i32, dim1: i32) -> Result<Self>
pub fn transpose(&self, dim0: i32, dim1: i32) -> Result<Self>
Delegates to crate::matrix::transpose.
Sourcepub fn equal(&self, value: f32) -> Result<Self>
pub fn equal(&self, value: f32) -> Result<Self>
Delegates to crate::matrix::equal.
Sourcepub fn causal_mask(&self) -> Result<Self>
pub fn causal_mask(&self) -> Result<Self>
Delegates to crate::matrix::causal_mask.
Sourcepub fn greater_equal(&self, value: f32) -> Result<Self>
pub fn greater_equal(&self, value: f32) -> Result<Self>
Delegates to crate::matrix::greater_equal.
Sourcepub fn split(&self, sizes: &[i64], dim: i32) -> Result<Vec<Matrix>>
pub fn split(&self, sizes: &[i64], dim: i32) -> Result<Vec<Matrix>>
Delegates to crate::matrix::split.
Sourcepub fn compact_rows(&self, mask: &Matrix) -> Result<CompactRowsResult>
pub fn compact_rows(&self, mask: &Matrix) -> Result<CompactRowsResult>
Delegates to crate::matrix::compact_rows.
Sourcepub fn scatter_rows(
&self,
source: &Matrix,
row_map: &Matrix,
count: &Matrix,
) -> Result<Self>
pub fn scatter_rows( &self, source: &Matrix, row_map: &Matrix, count: &Matrix, ) -> Result<Self>
Delegates to crate::matrix::scatter_rows.
Sourcepub fn scatter_rows_indirect(
&self,
source: &Matrix,
plan: &CompactRowsResult,
) -> Result<Self>
pub fn scatter_rows_indirect( &self, source: &Matrix, plan: &CompactRowsResult, ) -> Result<Self>
Delegates to crate::matrix::scatter_rows_indirect.
Sourcepub fn cast_f32_i32(&self) -> Result<Self>
pub fn cast_f32_i32(&self) -> Result<Self>
Delegates to crate::matrix::cast_f32_i32.
Sourcepub fn cast_i32_f32(&self) -> Result<Self>
pub fn cast_i32_f32(&self) -> Result<Self>
Delegates to crate::matrix::cast_i32_f32.
Trait Implementations§
Source§impl Add<&Matrix> for f32
scalar + &mat — add a scalar to every element (left-hand scalar form).
impl Add<&Matrix> for f32
scalar + &mat — add a scalar to every element (left-hand scalar form).
Source§impl Neg for Matrix
-mat — elementwise negate of an owned matrix (avoids a & at call sites
where the value is already a temporary, e.g. -(&a * &b)?).
impl Neg for Matrix
-mat — elementwise negate of an owned matrix (avoids a & at call sites
where the value is already a temporary, e.g. -(&a * &b)?).