oa::matrix
matrix module in the OA Core public surface.
MdRust public API
Public Types
Public Functions
pub fn from_f32_on( engine: &Engine, shape: impl Into<Vec<usize>>, values: &[f32], ) -> Result<Matrix>pub fn masked_categorical_accuracy_count( logits: &Matrix, labels: &Matrix, mask: &Matrix, ) -> Result<Matrix>pub fn moe_routing_bias_update( selection_mask: &Matrix, bias: &Matrix, experts_per_token: usize, gamma: f32, ) -> Result<()>Function Documentation
Take the absolute value of every FP32 matrix element. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Add two matrices elementwise using multidirectional broadcasting. Shapes use multidirectional broadcasting over at most eight axes. The I32 route uses two’s-complement wrapping arithmetic. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Add a scalar to every FP32 matrix element. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Count rows whose last-axis FP32 argmax equals the integer class label. The returned [1] U32 Matrix remains device-resident. Reading it is an explicit synchronization boundary. Errors Returns an error when logits are not a nonempty rank-two-or-greater FP32 Matrix, labels do not match the leading logits shape or use U8/U32/I32, inputs belong to different engines, a size exceeds the shader ABI, allocation fails, or recording fails.
Cap every FP32 matrix element at an upper bound. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Raise every FP32 matrix element to a lower bound. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Concatenate one or more same-dtype matrices along one axis. This is the direct Rust spelling of OA C++ FnMatrix::concat. One semantic operation lowers to one copy-region dispatch per nonempty input. Errors Returns an error when there are no inputs, rank is outside one through four, dtype, engine, rank, or non-concatenated extents differ, the axis is invalid, shape or shader-ABI arithmetic overflows, allocation fails, or recording fails.
Deep-copy a dense matrix into independent device storage. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Take the cosine of every FP32 matrix element. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Divide two FP32 matrices elementwise using multidirectional broadcasting. Shapes use multidirectional broadcasting over at most eight axes. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Divide every FP32 matrix element by a scalar. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Apply inverted Dropout with a Philox-generated mask. Kept values are scaled by 1 / (1 - probability). The effective seed is retained for the reverse pass, and captured execution advances its random counter once per replay without rebuilding the graph. Errors Returns an error unless input is FP32, probability is finite and in [0, 1), or runtime allocation/recording fails.
Compare every FP32 element with one scalar and return an FP32 zero/one mask. Equality follows the device’s ordinary IEEE comparison: NaN never compares equal, while positive and negative zero compare equal. The result is intentionally detached because a discrete mask has no admitted adjoint. Errors Returns an error when input is not FP32, its element count exceeds the shader ABI, allocation fails, or runtime recording fails.
Take the exponential of every FP32 matrix element. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Create an FP32 matrix on this thread’s default engine. Errors Returns an error when the default engine cannot be initialized, the shape does not match values, or allocation and upload fail.
Create an FP32 matrix on a particular engine. Errors Returns an error when the shape does not match values, or allocation and upload fail.
Create an FP32 matrix filled with value. Errors Returns an error when the shape or storage size overflows, or allocation and upload fail.
Gather rows from an FP32 table using arbitrary-shape integer indices. The table must be [rows,width]; indices are flattened and output is [num_indices,width], matching OA C++ FnMatrix::gather. U8, U32, and I32 indices use one bounded kernel. Invalid rows produce NaN without reading outside the table. Errors Returns an error for incompatible rank, dtype, Engine ownership, shader ABI, allocation, or recording.
Gather I32-selected elements from every row’s final axis. input is FP32 [R, C] and indices is I32 [R, K]; the result is FP32 [R, K]. Out-of-range indices produce zero, matching OA C++. Repeated indices accumulate deterministically in the reverse pass. Errors Returns an error unless both inputs are rank two on the same engine, row counts agree, dtypes match the contract, the input width is positive, sizes fit the shader ABI, allocation fails, or runtime recording fails.
Take the natural logarithm of every FP32 matrix element. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Stable LogSoftmax over dim, where -1 selects the last dimension. The operation records asynchronously and returns its output immediately. Errors Returns an error when the input is empty or not FP32, dim is invalid, an axis extent or dispatch count exceeds the shader ABI, allocation fails, or runtime recording fails.
Count correct categorical predictions only where the FP32 mask is nonzero. The returned [1] U32 Matrix remains device-resident. Reading it is an explicit synchronization boundary. Errors Returns an error under the same conditions as categorical_accuracy_count, or when the mask is not FP32, does not match the labels shape, or belongs to another engine.
Multiply A by the transpose of B using the OA weight-layout convention. left has shape [M, K], right has shape [N, K], and the returned matrix has shape [M, N]. This matches OA’s native weight-layout convention and does not transpose storage. The call records device work and returns without a host wait. Errors Returns an error when either input is not a rank-two FP32 matrix, their K extents differ, they belong to different engines, a dimension exceeds the admitted shader ABI, or allocation or recording fails.
Build a stable dropless expert-major plan from I32 [T, K] indices. Within every expert, routes preserve their original token-major order. The operation accepts at most 256 experts, matching the shader’s shared state. Errors Returns an error unless indices are nonempty rank-two I32, the expert count is in 1..=256, sizes fit the admitted ABI, allocation succeeds, or runtime recording succeeds.
Update an FP32 expert-routing bias from a dense FP32 selection mask. selection_mask is [T, E]; bias may be [E] or [1, E]. Each expert receives -gamma, +gamma, or no update when its observed route count is above, below, or equal to the balanced target T * K / E. The update stays device-resident and mutates bias through its existing value identity. Errors Returns an error unless both matrices are FP32 on the same engine, the mask and bias shapes agree, experts_per_token is in 1..=E, gamma is finite and positive, dimensions fit the shader ABI, or runtime recording fails.
Multiply two FP32 matrices elementwise. Shapes use multidirectional broadcasting over at most eight axes. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Negate every FP32 matrix element. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Create an FP32 matrix filled with ones. Errors Returns an error when the shape or storage size overflows, or allocation and upload fail.
Generate normally distributed FP32 values with input’s shape. Errors Returns an error unless input is FP32, mean and stddev are finite, stddev is nonnegative, or runtime allocation/recording fails.
Generate uniformly distributed FP32 values with input’s shape. input supplies shape, dtype, engine, and semantic lineage; its values are not read. A nonzero seed is exact. Zero consumes the thread-local seed sequence controlled by set_rng_seed. Errors Returns an error unless input is FP32, low < high, both bounds are finite, or runtime allocation/recording fails.
Raise every FP32 matrix element to a scalar exponent. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Take the reciprocal of every FP32 matrix element. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Repeat every FP32 element along one matrix axis. This is the direct Rust spelling of OA C++ FnMatrix::repeatInterleave. Rank-one through rank-four matrices are currently admitted. Errors Returns an error for an unsupported rank or dtype, non-positive repeat count, invalid axis, shape/ABI overflow, allocation failure, or recording failure.
Return a zero-copy dense view with a different shape. The result owns a new semantic value identity but shares storage, pending execution state, dtype, and element order with input. When a gradient tape is active, its adjoint reshapes the incoming gradient back to the input shape. Errors Returns an error when the requested shape overflows or changes the number of elements.
Sample one I32 class index from each final-axis row of FP32 logits. Nonpositive temperature selects the first maximum deterministically. Positive temperature uses the donor Philox categorical route; positive top_k restricts candidates and top_p applies nucleus truncation after descending TopK. Rank-one input is treated as one row. Errors Returns an error when logits are not nonempty rank-one or rank-two FP32, a sorted candidate count exceeds 1024, sizes do not fit the shader ABI, allocation fails, or runtime recording fails.
Multiply every FP32 matrix element by a scalar. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Replace the current thread’s implicit random seed source. Explicit nonzero operation seeds bypass this state. Passing zero to a Philox operation consumes one value from this deterministic thread-local sequence.
Take the sine of every FP32 matrix element. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Materialize a half-open interval along one matrix axis. This is the direct Rust spelling of OA C++ FnMatrix::slice. The current checkpoint admits rank-one through rank-four FP32 matrices. Errors Returns an error when the rank, dtype, axis, range, shader ABI, allocation, or runtime recording contract is invalid.
Stable Softmax over dim, where -1 selects the last dimension. The operation records asynchronously and returns its output immediately. Errors Returns an error when the input is empty or not FP32, dim is invalid, an axis extent or dispatch count exceeds the shader ABI, allocation fails, or runtime recording fails.
Take the square root of every FP32 matrix element. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Subtract two FP32 matrices elementwise using multidirectional broadcasting. Shapes use multidirectional broadcasting over at most eight axes. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Subtract a scalar from every FP32 matrix element. The call records device work and returns without a host wait. Errors Returns an error when the shape, dtype, ownership, allocation, or recording contract fails.
Sum all values when dim is -1, or keep and reduce one selected axis. A full reduction returns shape [1]; an axis reduction preserves rank and replaces the selected extent with one. The operation records asynchronously. Errors Returns an error when the input is empty or not FP32, dim is invalid, a size exceeds the admitted shader ABI, allocation fails, or recording fails.
Return the largest k FP32 values and their I32 indices on the last axis. Rank-one and rank-two inputs are admitted. dim may be -1 or the exact last-axis index. Values are sorted descending; ties select lower indices. k is clamped to the last-axis extent, matching OA C++. Errors Returns an error for an unsupported rank, dtype, axis, negative k, a request above the admitted 1024-entry GPU limit, an empty last axis, size overflow, allocation failure, or runtime recording failure.
Expand I32 [T, K] expert indices into an exact FP32 [T, E] mask. Errors Returns an error unless the input is nonempty rank-two I32, num_experts is positive, all sizes fit the shader ABI, or runtime recording fails.
Materialize a swap of the last two axes of a rank-two or rank-three Matrix. dim0 and dim1 accept positive or negative axes, but the current donor contract only admits the final two axes in either order. The result owns new storage and preserves the input dtype and batch order. Errors Returns an error unless the input is rank-two or rank-three F32, the axes select the distinct final two dimensions, all geometry fits the shader ABI, allocation succeeds, and runtime recording succeeds.