Skip to main content

Module ops

Module ops 

Source
Expand description

Array operations as free functions with MLX’s names: the mirrors of the Array methods.

Every operation whose first argument is an array is a method, and the method is the canonical, documented form: x.sum(-1), x.matmul(&w.t()), m.select(&a, 0.0). This module holds the same operations as free functions with MLX’s names and the same parameters (the receiver becomes the first argument), so MLX code ports line by line: sum(&x, -1), matmul(&x, &w.t()), where_(&m, &a, 0.0). A few operations exist only here because their first argument is not one array: concatenate, stack, where_ and the constructors (zeros, ones, full, arange).

Operations return Array, never Result: invalid arguments give a failed array carrying crate::Error::Op with the operation’s name and the caller’s source location, and every array computed from it fails with the same error, surfacing at Array::check, Array::eval, Array::item or Array::to_vec.

use nuvec_core::{Array, array, ops};

let x = array![[1.0, 2.0], [3.0, 4.0]];
let a = x.exp().sum(-1);
let b = ops::sum(&ops::exp(&x), -1);
assert_eq!(a.to_vec::<f32>().unwrap(), b.to_vec::<f32>().unwrap());

Functions§

abs
Element-wise abs; see Array::abs.
add
Element-wise add; see Array::add. Either operand may be a weak Rust number.
addmm
alpha · (a @ b) + beta · c; see Array::addmm.
all
Whether every element is non-zero over axes; see Array::all.
all_keepdims
Whether every element is non-zero over axes, keeping them as size-1 dimensions; see Array::all_keepdims.
any
Whether any element is non-zero over axes; see Array::any.
any_keepdims
Whether any element is non-zero over axes, keeping them as size-1 dimensions; see Array::any_keepdims.
arange
Evenly spaced values start, start + step, … below stop (above it for a negative step), as MLX: int32 when every argument is an integer (int64 when they do not fit), float32 when any is a float. Use Array::astype for another dtype.
argmax
Indices (uint32) of the maxima along one axis (..: the flattened array); see Array::argmax.
argmax_keepdims
argmax keeping the reduced axis; see Array::argmax_keepdims.
argmin
Indices (uint32) of the minima along one axis (..: the flattened array); see Array::argmin.
argmin_keepdims
argmin keeping the reduced axis; see Array::argmin_keepdims.
argpartition
The indices (uint32) that partition the array; see Array::argpartition.
argsort
The indices (uint32) that sort the array along one axis; see Array::argsort.
astype
The elements converted to dtype; see Array::astype.
broadcast_to
The array broadcast to shape; see Array::broadcast_to.
concatenate
arrays joined along axis (MLX’s concatenate); dtypes are promoted.
conjugate
Element-wise conjugate; see Array::conjugate.
contiguous
Element-wise contiguous; see Array::contiguous.
conv1d
A 1-D convolution of [N, L, C_in] with [C_out, K, C_in / groups]; see Array::conv1d.
copy
Element-wise copy; see Array::copy.
cos
Element-wise cos; see Array::cos.
cummax
The running maximum along one axis (..: the flattened array); see Array::cummax.
cummax_with
The running maximum with ScanOptions; see Array::cummax_with.
cummin
The running minimum along one axis (..: the flattened array); see Array::cummin.
cummin_with
The running minimum with ScanOptions; see Array::cummin_with.
cumprod
The cumulative product along one axis (..: the flattened array); see Array::cumprod.
cumprod_with
The cumulative product with ScanOptions; see Array::cumprod_with.
cumsum
The cumulative sum along one axis (..: the flattened array); see Array::cumsum.
cumsum_with
The cumulative sum with ScanOptions; see Array::cumsum_with.
depends
x, evaluated only after deps (MLX’s depends): evaluating the result evaluates the dependencies first, without using their values. Its gradient goes to x.
dequantize
The weights of quantize restored, in dtype (default: the scales’ dtype for affine, bfloat16 for the floating-point modes); MLX’s dequantize. See also Quantized::dequantize.
divide
Element-wise divide; see Array::divide. Either operand may be a weak Rust number.
equal
Element-wise equal; see Array::equal. Either operand may be a weak Rust number.
erfinv
Element-wise erfinv; see Array::erfinv.
exp
Element-wise exp; see Array::exp.
expand_dims
Size-1 dimensions inserted at axes (positions in the result); see Array::expand_dims.
floor_divide
Element-wise floor_divide; see Array::floor_divide. Either operand may be a weak Rust number.
from_fp8
E4M3 bytes as dtype floats (MLX’s from_fp8); see Array::from_fp8.
full
An array of shape with every element value: a Rust number is weak, as in MLX (0.5 gives float32, 1 int32), and an array keeps its dtype (Array::from(7u8) gives uint8).
full_like
value (a weak scalar takes a’s dtype) in a’s shape; see Array::full_like.
gather_mm
Products of gathered matrices; see Array::gather_mm.
gather_qmm
Quantized products of gathered matrices (mixture-of-experts layers); see Array::gather_qmm.
greater
Element-wise greater; see Array::greater. Either operand may be a weak Rust number.
greater_equal
Element-wise greater_equal; see Array::greater_equal. Either operand may be a weak Rust number.
less
Element-wise less; see Array::less. Either operand may be a weak Rust number.
less_equal
Element-wise less_equal; see Array::less_equal. Either operand may be a weak Rust number.
log
Element-wise log; see Array::log.
logaddexp
Element-wise logaddexp; see Array::logaddexp. Either operand may be a weak Rust number.
logcumsumexp
The running log(Σ exp) along one axis (..: the flattened array); see Array::logcumsumexp.
logcumsumexp_with
The running log(Σ exp) with ScanOptions; see Array::logcumsumexp_with.
logsumexp
log(sum(exp(a))), computed stably, over axes; see Array::logsumexp.
logsumexp_keepdims
log(sum(exp(a))), computed stably, over axes, keeping them as size-1 dimensions; see Array::logsumexp_keepdims.
matmul
Matrix product; see Array::matmul.
max
The maximum over axes; see Array::max.
max_keepdims
The maximum over axes, keeping them as size-1 dimensions; see Array::max_keepdims.
maximum
Element-wise maximum; see Array::maximum. Either operand may be a weak Rust number.
mean
The mean over axes; see Array::mean.
mean_keepdims
The mean over axes, keeping them as size-1 dimensions; see Array::mean_keepdims.
min
The minimum over axes; see Array::min.
min_keepdims
The minimum over axes, keeping them as size-1 dimensions; see Array::min_keepdims.
minimum
Element-wise minimum; see Array::minimum. Either operand may be a weak Rust number.
multiply
Element-wise multiply; see Array::multiply. Either operand may be a weak Rust number.
negative
Element-wise negative; see Array::negative.
not_equal
Element-wise not_equal; see Array::not_equal. Either operand may be a weak Rust number.
ones
An array of ones of shape.
ones_like
Ones of a’s shape and dtype; see Array::ones_like.
partition
The array partitioned around position kth along one axis; see Array::partition.
power
Element-wise power; see Array::power. Either operand may be a weak Rust number.
prod
The product over axes; see Array::prod.
prod_keepdims
The product over axes, keeping them as size-1 dimensions; see Array::prod_keepdims.
put_along_axis
A copy with values placed at indices along axis; see Array::put_along_axis.
quantize
w quantized along its last axis; see Array::quantize.
quantized_matmul
x · dequantize(w)ᵀ (or without the transpose); see Array::quantized_matmul.
repeat
Each element repeated repeats times along one axis; see Array::repeat.
reshape
The same elements in another shape (one -1 inferred); see Array::reshape.
scatter_add_along_axis
A copy with values added at indices along axis (repeated indices accumulate); see Array::scatter_add_along_axis.
sigmoid
Element-wise sigmoid; see Array::sigmoid.
sign
Element-wise sign; see Array::sign.
sin
Element-wise sin; see Array::sin.
slice_update
A copy of a with the region at index replaced by update; see Array::slice_update.
softmax
exp(a) / sum(exp(a)) over axes; see Array::softmax.
sort
The array sorted along one axis; see Array::sort.
split
The array split into parts equal parts along axis; see Array::split.
split_at
The array split before each of indices along axis; see Array::split_at.
split_n
The array split into N equal parts along axis; see Array::split_n.
sqrt
Element-wise sqrt; see Array::sqrt.
squeeze
Size-1 dimensions removed (..: all of them); see Array::squeeze.
stack
arrays (one shape) stacked along a new axis axis (MLX’s stack).
std
The standard deviation over axes; see Array::std.
std_keepdims
The standard deviation over axes, keeping them as size-1 dimensions; see Array::std_keepdims.
std_with
The standard deviation over axes with VarOptions; see Array::std_with.
stop_gradient
Element-wise stop_gradient; see Array::stop_gradient.
subtract
Element-wise subtract; see Array::subtract. Either operand may be a weak Rust number.
sum
The sum over axes; see Array::sum.
sum_keepdims
The sum over axes, keeping them as size-1 dimensions; see Array::sum_keepdims.
take
The elements at indices along one axis (..: the flattened array); see Array::take.
take_along_axis
The elements at indices along axis; see Array::take_along_axis.
tanh
Element-wise tanh; see Array::tanh.
to_fp8
Floats as E4M3 bytes (MLX’s to_fp8); see Array::to_fp8.
topk
The k largest elements along one axis; see Array::topk.
transpose
The axes reordered (..: reversed); see Array::transpose.
var
The variance over axes; see Array::var.
var_keepdims
The variance over axes, keeping them as size-1 dimensions; see Array::var_keepdims.
var_with
The variance over axes with VarOptions; see Array::var_with.
view
x’s bits as dtype (MLX’s view); see Array::view.
where_
Element-wise selection: a where condition is true, else b (MLX’s where); see Array::select. Any operand may be a weak Rust number.
zeros
An array of zeros of shape.
zeros_like
Zeros of a’s shape and dtype; see Array::zeros_like.