Expand description
Fused operations with dedicated GPU kernels, as MLX’s mx.fast: rms_norm,
layer_norm, rope, cross_entropy, scaled_dot_product_attention and
gated_delta (mlx-lm’s Gated DeltaNet kernels) (and,
through crate::softmax and
crate::logsumexp over the last axis, MLX’s softmax and log-sum-exp kernels).
Each operation is one GPU kernel (MLX’s, ported) with an equivalent graph of ordinary operations, its fallback. As in MLX, the fallback runs on the CPU stream, and it defines the forward-mode derivative and batching; reverse mode uses dedicated kernels where MLX has them.
use nuvec_core::{Array, Result, fast};
let x = Array::from_slice(&[1.0f32, 2.0, 3.0, 4.0], &[2, 2])?;
let y = fast::rms_norm(&x, None, 1e-5)?; // x / sqrt(mean(x²) + eps), per row
let v = y.to_vec::<f32>()?;
assert!((v[0] - 1.0 / 2.5f32.sqrt()).abs() < 1e-5);Structs§
- Kernel
Call - A recorded call of a custom kernel.
- Kernel
Config - How a
MetalKernelis launched. - Metal
Kernel - A Metal kernel built from source, as MLX’s
mx.fast.metal_kernel: a name, the names of its inputs and outputs, and the body of a kernel function. Calling it generates the function’s signature as MLX does:
Enums§
- Attention
Mask - The mask of
scaled_dot_product_attention. - FastOp
- A fused operation (see the module documentation).
- Template
Arg - A template argument of a custom kernel.
Functions§
- cross_
entropy - Cross entropy over the last axis of
logitsagainst integer class indicestargets(the logits’ shape without the last axis; negative indices count from the end), as MLX’sfast.cross_entropy:logsumexp(logits) - logits[target]per row, in float32. - gated_
delta - The gated delta rule, Gated DeltaNet’s recurrence (as mlx-lm’s
gated_delta_updateonce its gates are computed). For each steptand value head, withSits state ([Dv, Dk]): - layer_
norm - Layer normalisation over the last axis,
(x - mean) / sqrt(var + eps) · weight + bias, as MLX’sfast.layer_norm: statistics in float32, the result in the promoted type. - rms_
norm - Root-mean-square normalisation over the last axis,
x / sqrt(mean(x²) + eps) · weight, as MLX’sfast.rms_norm: statistics in float32, the result in the promoted type ofxandweight(1-D, the last axis’s size). - rope
- Rotary position embeddings, as MLX’s
fast.rope, ofx(at least 3-D:[B, ..., T, D], positions alongT): the firstdimsfeatures (even) are rotated in pairs, the two halves (or interleaved pairs,traditional) by angle(offset + t) · scale · θᵢ, withθᵢ = base^(-2i/dims)or1 / freqs[i]. Exactly one ofbaseandfreqsis given.offsetis an integer scalar or one per batch entry. - scaled_
dot_ product_ attention - Scaled dot-product attention, as MLX’s
fast.scaled_dot_product_attention:softmax(scale · q kᵀ + mask) vover[B, H, L, D]arrays.