Expand description
Lazy array graph, primitives, dtypes, streams and evaluation: the core of nuvec.
Operations on Arrays record a graph and return immediately. Nothing is computed
until an array is evaluated, explicitly or by reading it back:
use nuvec_core::{Array, Result};
let a = Array::from_slice(&[1.0f32, 2.0, 3.0], &[3])?;
let b = Array::full(&[3], 10.0f32)?;
let c = (&a + &b) * &a; // lazy: nothing computed yet
assert!(!c.is_evaluated());
assert_eq!(c.to_vec::<f32>()?, vec![11.0, 24.0, 39.0]);Binary operations promote types (MLX’s table) and broadcast shapes (NumPy rules); views
(reshape, transpose, slice, broadcast_to, expand_dims, squeeze) share data.
Reductions (sum, mean, max, argmax, …) work over any axes and layouts;
matmul broadcasts batch dimensions and uses a SIMD GEMM on the CPU.
Arrays live in unified memory. Operations on the GPU stream run as Metal kernels; anything without a GPU kernel (for example float64) runs on the CPU over the same memory:
use nuvec_core::{Array, Device, Stream, set_default_stream};
set_default_stream(Stream::default_for(Device::gpu(0)));
let x = Array::from_slice(&[1.0f32, 2.0, 3.0], &[3])?;
let y = &x * &x; // a Metal kernel on a Mac; an `UnsupportedDevice` error elsewhereStatus: pre-alpha. The default stream is the CPU.
Modules§
- fast
- Fused operations with dedicated GPU kernels, as MLX’s
mx.fast:rms_norm,layer_norm,rope,cross_entropy,scaled_dot_product_attentionandgated_delta(mlx-lm’s Gated DeltaNet kernels); nuvec’sadd_rms_norm(a residual connection and its norm) andnorm_rope(norm, RoPE and a cache write); and, throughcrate::softmaxandcrate::logsumexpover the last axis, MLX’s softmax and log-sum-exp kernels. - random
- Random numbers from explicit keys or a per-thread key sequence, bit for bit as in MLX.
Structs§
- Array
- A lazily evaluated n-dimensional array.
- Complex64
- A complex number with
f32parts, the element type ofDType::Complex64. - Custom
Call - One call of a custom function, as recorded in the graph: its rules and the number of primal inputs. Equal (and hashed) by the identity of the function.
- Custom
Function - A function with custom transform rules; see
custom_function. - Device
- A processor that executes array operations.
- Flags
- How an array’s elements are laid out in memory.
- Fused
- A graph of element-wise operations evaluated as one kernel, with one or more of its steps as results. Cheap to clone; equal graphs compare and hash equal.
- Mapped
File - A file’s bytes in array storage: on a Mac with a GPU, the file is memory-mapped and wrapped as GPU-visible memory without a copy, so arrays made from it are views whose pages are read when first touched (by the CPU or the GPU). Elsewhere the file is read into memory. The mapping lives as long as any array made from it.
- Scheduled
- Arrays whose evaluation is put in order but not started (
schedule). - Slice
Arg - One slice specification per dimension:
start:stop:stepwith Python semantics. - Stream
- An ordered queue of operations on a device.
- bf16
- A 16-bit floating point type implementing the
bfloat16format. - f16
- A 16-bit floating point type implementing the IEEE 754-2008 standard
binary16a.k.a “half” format.
Enums§
- ArgReduce
Op - The position of an extreme value along one axis.
- Binary
Op - An element-wise function of two operands.
- Category
- The broad kind of an element type, used for type promotion.
- DType
- The element type of an array.
- Device
Kind - The kind of processor a device is.
- Error
- Why an operation failed.
- Fused
Ref - An operand of a fused step: an input of the fused computation or an earlier step’s result.
- Primitive
- An operation recorded in the graph, with its parameters.
- Reduce
Op - A reduction over a set of axes.
- Scalar
- A number whose dtype is decided by the array it is combined with.
- Scalar
OrArray - An operand of a binary operation: an array, or a weak scalar.
- ScanOp
- The combination a cumulative scan applies.
- Scatter
Reduce - How a scatter combines an update with the value it lands on.
- UnaryOp
- An element-wise function of one operand.
Constants§
- VERSION
- The version of this crate.
Traits§
- Element
- A Rust type that can be stored in an array. Implemented for the element types of
every
DType; sealed. - Scalar
Operand - A Rust number usable as a weak operand of
+ - * /on the right of an array.
Functions§
- abs
- Element-wise absolute value; the float32 magnitude for complex numbers.
- add
- Element-wise
a + b, with promotion and broadcasting. Integers wrap; for booleans this is logical OR. - addmm
alpha · (a @ b) + beta · c, withcbroadcast to the product’s shape, as MLX’saddmm. On the GPU it is one kernel (the GEMM’s epilogue), soLinearlayers use it for their bias.- all
- Whether every element over
axes(empty = all) is non-zero. - any
- Whether any element over
axes(empty = all) is non-zero. - arange
- Evenly spaced values
start, start + step, …belowstop(above it for negative steps), converted todtype. The length isceil((stop - start) / step), as in MLX. - argmax
- Index (uint32) of the maximum along
axis, or over the flattened array ifNone. The first occurrence wins; the first NaN wins over any number. - argmin
- Index (uint32) of the minimum along
axis, or over the flattened array ifNone. The first occurrence wins; the first NaN wins over any number. - argpartition
- The indices (uint32) that partition
a, aspartition. - argsort
- The indices (uint32) that sort
aalongaxis, assort. - astype
- Converts the elements to
dtype. Float-to-integer conversion saturates (NaN becomes 0); conversion to bool tests for non-zero; complex-to-real keeps the real part. - async_
eval - Starts evaluating
arraysand returns without waiting for GPU work: reading the arrays (or evaluating them) later waits for exactly the work they need. As in MLX, use it to overlap building the next computation with the GPU running this one. - at_
least_ float dif it is floating point or complex, otherwise the promotion ofdwith float32. Transcendental functions and division compute in this type.- broadcast_
to - Presents
aas the largershape(NumPy broadcasting rules). Shares the data. - checkpoint
- Returns
fwith gradient checkpointing: values and derivatives equalf’s, butf’s intermediates are not kept for the backward pass;fruns again to differentiate. - concatenate
arraysjoined alongaxis(MLX’sconcatenate): equal shapes except alongaxis, with dtypes promoted to a common one.- conjugate
- Element-wise complex conjugate. Real arrays are returned unchanged.
- contiguous
ain row-major memory: itself if it already is, else a copy (MLX’scontiguous).- conv1d
- The 1-D convolution (cross-correlation) of
input([N, L, C_in]) withweight([C_out, K, C_in / groups]), as MLX’sconv1d: output[N, L_out, C_out]withL_out = (L + 2·padding − dilation·(K − 1) − 1) / stride + 1. Channels split intogroupsindependent groups (groups = C_in = C_outis a depthwise convolution). Products accumulate in float32 and round once, as MLX’s kernels. - cos
- Element-wise cosine, in at least float32.
- cummax
- The running maximum (NaN propagates), as
cumsum. - cummin
- The running minimum (NaN propagates), as
cumsum. - cumprod
- The cumulative product, as
cumsum. - cumsum
- The cumulative sum along
axis(flattened without one):reversescans from the end; an exclusive scan (inclusive = false) leaves each element out of its own output. Booleans sum as int32. - custom_
function - Wraps
fso that transforms use custom rules for it, set withCustomFunction::with_vjp,CustomFunction::with_jvpandCustomFunction::with_vmap. - default_
device - The default device: the GPU when Metal is available, else the CPU (as in MLX).
- default_
stream - The stream new operations on this thread are recorded on: the default device’s first
stream until
set_default_streamis called on this thread. - dequantize
- Restores the values packed by
quantize:code · scale + bias, in the scales’ dtype. - divide
- Element-wise true division
a / b. Integer operands are promoted to floating point. - equal
- Element-wise
a == bas a bool array. - erfinv
- Element-wise inverse error function, in at least float32.
- eval
- Evaluates
arraysand everything they depend on, and waits for them. Each node runs at most once, even across threads. - exp
- Element-wise
e^a, in at least float32. - expand_
dims - Inserts a dimension of size 1 at
axis(negative counts from the end of the result). Shares the data. - gather_
mm - Matrix products of gathered batches, as MLX’s
gather_mm(mixture-of-experts layers in full precision): output batchiisa[lhs_indices[i]] · b[rhs_indices[i]]over the flattened batch dimensions ofa([..., M, K]) andb([..., K, N]), with the indices broadcast together and either defaulting to every batch.sorted_indicespromises sortedrhs_indices, which lets one kernel walk each batch ofbonce. - gather_
qmm - Quantized matrix products of gathered batches, as MLX’s
gather_qmm(mixture-of-experts layers): output batchiisx[lhs_indices[i]] · dequantize(w)[rhs_indices[i]]ᵀ(or without the transpose), indexing the flattened batch dimensions ofx([..., M, K]) and ofw,scalesandbiases. The indices (integers) broadcast together; either defaults to every batch of its operand in order.sorted_indicespromises thatrhs_indicesis sorted (as after grouping rows by expert), which lets one kernel walk each weight batch once. - grad
- Returns the gradient function of a scalar function of one array.
- greater
- Element-wise
a > bas a bool array. Not defined for complex numbers. - greater_
equal - Element-wise
a >= bas a bool array. Not defined for complex numbers. - jvp
- Forward mode: runs
fonprimalsand returns its outputs together with the Jacobian-vector product, one tangent per output, for one tangent per primal. - less
- Element-wise
a < bas a bool array. Not defined for complex numbers. - less_
equal - Element-wise
a <= bas a bool array. Not defined for complex numbers. - log
- Element-wise natural logarithm, in at least float32.
- logaddexp
- Element-wise
log(exp(a) + exp(b)), computed stably as MLX does: the larger operand pluslog1p(exp(smaller − larger)), or the larger one when the smaller is −∞ or the larger +∞. Integers are promoted to floating point; NaN propagates. Not defined for complex numbers. - logcumsumexp
- The running
log(Σ exp), ascumsum(computed in floating point). - logsumexp
log(sum(exp(a)))overaxes(all axes if empty), computed stably as MLX does: the maximum is subtracted first (without gradient), and rows whose maximum is infinite return that maximum.- matmul
- Matrix product of
[..., M, K]and[..., K, N], broadcasting batch dimensions. - max
- Maximum over
axes(empty = all); NaN propagates. Zero-length axes are an error. - maximum
- Element-wise maximum; NaN propagates. Not defined for complex numbers.
- mean
- Mean over
axes(empty = all), in at least float32. Empty reductions are NaN. - min
- Minimum over
axes(empty = all); NaN propagates. Zero-length axes are an error. - minimum
- Element-wise minimum; NaN propagates. Not defined for complex numbers.
- multiply
- Element-wise
a * b, with promotion and broadcasting. Integers wrap; for booleans this is logical AND. - negative
- Element-wise
-a. Integers wrap. Not defined for booleans. - not_
equal - Element-wise
a != bas a bool array. - ones
- An array of ones.
- partition
apartitioned alongaxis(flattened without one): the element at positionkthis the one a sort would put there, smaller ones come before it and larger ones after (MLX’spartition).- prod
- Product over
axes(empty = all), with the same widening assum. - promote_
types - The element type two operands are converted to before a binary operation, following MLX’s promotion table.
- put_
along_ axis - A copy of
awithvaluesplaced atindicesalongaxis(MLX’sput_along_axis).indicesbroadcasts againstaon the other axes,valuesagainstindices; with repeated indices, one of the values wins. - quantize
- Quantizes the last axis of
w(float16, bfloat16 or float32, at least 2-D, the last dimension divisible bygroup_size) in groups ofgroup_size(32, 64 or 128) values withbits(1–6 or 8) bits each. Returns the packed codes (uint32, last dimension· bits / 32), the scales and the biases (w’s dtype, last dimension/ group_size), equal to MLX’s bit for bit. - quantized_
matmul - The matrix product of
xwith quantized weights,x · dequantize(w)ᵀiftranspose(MLX’s default, for weights stored as[out, in]) orx · dequantize(w), without materialising the dequantized matrix. - repeat
awith each element repeatedrepeatstimes alongaxis(of the flattened array without one), as MLX’srepeat.- reshape
- The same elements in another shape; one dimension may be
-1(inferred). Shares the data when the layout permits, otherwise copies. - scatter_
add_ along_ axis - A copy of
awithvaluesadded atindicesalongaxis; repeated indices accumulate (MLX’sscatter_add_axis, the gradient oftake_along_axis). - schedule
arrayswith the work they need put in order, to be started later byScheduled::async_evalorScheduled::evalwith less delay: ordering a large graph takes a while, which can then overlap other work, such as the GPU running the previous step while the next one waits for its result.- set_
default_ stream - Sets the stream new operations on this thread are recorded on.
- sigmoid
- Element-wise logistic function
1 / (1 + e^-a), in at least float32. - sign
- Element-wise sign:
-1,0or1ina’s dtype, as MLX’ssign((a > 0) - (a < 0), so NaN gives 0). Unsigned integers and booleans givea != 0. Complex numbers are not supported. - sin
- Element-wise sine, in at least float32.
- slice
- Selects
start:stop:stepin every dimension, with Python semantics (negative indices count from the end, bounds are clamped, steps may be negative). Shares the data. - slice_
update aunchanged, but no gradient flows through it incrate::vjp,crate::jvpand A copy ofawith the region selected as byslice()replaced byupdate(MLX’sslice_update,a[...] = update):updatebroadcasts to the region’s shape and takesa’s dtype.- softmax
exp(a) / sum(exp(a))overaxes(all axes if empty), with the maximum subtracted first (without gradient), as MLX does.- sort
asorted alongaxis(flattened without one): ascending, stable, NaN last.- split
asplit intopartsequal parts alongaxis(MLX’ssplitwith a count); views.- split_
at asplit alongaxisbefore each of the (non-decreasing) positionsindices, as MLX’ssplitwith indices:indices.len() + 1views.- sqrt
- Element-wise square root, in at least float32.
- squeeze
- Removes dimensions of size 1: the given
axes, or all of them ifaxesis empty. Shares the data. - stack
arrays(one shape) stacked along a new axisaxis(MLX’sstack).- stop_
gradient crate::grad. Shares the data.- subtract
- Element-wise
a - b, with promotion and broadcasting. Integers wrap; for booleans this is logical XOR. - sum
- Sum over
axes(empty = all). Integers and bools widen to 32 or 64 bits (as in MLX); half and single precision accumulate in double precision. - take
- The elements of
aatindicesalongaxis(MLX’stake): the axis is replaced byindices’ dimensions. Without an axis,ais flattened first. - take_
along_ axis - The elements of
aatindicesalongaxis(MLX’stake_along_axis):indiceshasa’s number of dimensions, and both broadcast on the other axes. - tanh
- Element-wise hyperbolic tangent, in at least float32.
- topk
- The
klargest elements ofaalongaxis(flattened without one), in ascending order (MLX’stopk; MLX leaves the order unspecified). - transpose
- Reorders the axes: output axis
iis input axisaxes[i]. Shares the data. - value_
and_ grad - Returns a function computing
f’s value and its gradients with respect to the arguments at positionsargnums. - vjp
- Reverse mode: runs
fonprimalsand returns its outputs together with the vector-Jacobian product, one gradient per primal, for one cotangent per output. - vmap
- Returns
fvectorized over a batch axis. - where_
- Element-wise selection:
awhereconditionis true, otherwiseb. - zeros
- An array of zeros.