#[non_exhaustive]pub enum Primitive {
Show 30 variants
Unary(UnaryOp),
Binary(BinaryOp),
Select,
AsType(DType),
Reshape(Vec<usize>),
Transpose(Vec<usize>),
Slice {
start: Vec<usize>,
step: Vec<isize>,
len: Vec<usize>,
},
Broadcast(Vec<usize>),
StopGradient,
SliceUpdate {
start: Vec<usize>,
step: Vec<isize>,
len: Vec<usize>,
},
Reduce {
op: ReduceOp,
axes: Vec<usize>,
},
ArgReduce {
op: ArgReduceOp,
axis: usize,
},
GatherAxis {
axis: usize,
},
ScatterAxis {
axis: usize,
reduce: ScatterReduce,
},
Sort {
axis: usize,
},
ArgSort {
axis: usize,
},
Scan {
op: ScanOp,
axis: usize,
reverse: bool,
inclusive: bool,
},
Concatenate {
axis: usize,
},
Matmul,
AddMM {
alpha: u32,
beta: u32,
},
Quantize {
group_size: usize,
bits: u8,
},
Dequantize {
group_size: usize,
bits: u8,
},
QuantizedMatmul {
group_size: usize,
bits: u8,
transpose: bool,
},
Fast(FastOp),
Compiled(Fused),
RandomBits {
shape: Vec<usize>,
width: u8,
},
Custom(CustomCall),
MetalKernel(KernelCall),
Copy,
Depends {
n_deps: usize,
},
}Expand description
An operation recorded in the graph, with its parameters.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Unary(UnaryOp)
Element-wise function of one operand.
Binary(BinaryOp)
Element-wise function of two operands of equal shape and dtype.
Select
where(condition, a, b): a boolean condition selects from a or b.
AsType(DType)
Conversion to another element type.
Reshape(Vec<usize>)
The same elements in another shape. A view when the layout permits, else a copy.
Transpose(Vec<usize>)
Axes reordered: output axis i is input axis axes[i]. A view.
Slice
A normalised slice per dimension. A view.
Fields
Broadcast(Vec<usize>)
Presented as a larger shape with zero strides. A view.
StopGradient
The input unchanged, but no gradient flows through it. A view.
SliceUpdate
(base, update): a copy of base whose region selected as by Slice is replaced by
update (of shape len).
Fields
Reduce
Reduction over sorted, unique axes; reduced dimensions become size 1.
ArgReduce
Index of an extreme value along axis (uint32); the axis becomes size 1.
GatherAxis
(src, indices): the elements of src at indices along axis (MLX’s
GatherAxis, take_along_axis). indices (integers) has src’s shape except
along axis and gives the output’s shape. Negative indices count from the end;
out-of-range indices are clamped.
ScatterAxis
(dst, indices, updates): a copy of dst with updates placed at indices along
axis (MLX’s ScatterAxis, put_along_axis). indices (integers) and updates
share a shape, which is dst’s except along axis. Indices as in GatherAxis.
Sort
The input sorted along axis (ascending, stable, NaN last).
ArgSort
The indices (uint32) that sort the input along axis, as Primitive::Sort.
Scan
A cumulative scan along axis.
Fields
Concatenate
The inputs (one dtype, equal shapes except along axis) joined along axis.
Matmul
Batched matrix product of [B..., M, K] and [B..., K, N] with equal batch
dimensions.
AddMM
alpha · (a @ b) + beta · c for inputs (a, b, c): a Matmul whose c has the
output’s shape. The factors are stored as f32 bits (so the enum stays Eq + Hash).
Quantize
Affine quantization of the last axis in groups (MLX’s quantize). Outputs the packed
codes (uint32, bits per value), the scales and the biases (the input’s dtype).
Fields
Dequantize
(w, scales, biases): code · scale + bias per value, in the scales’ dtype.
QuantizedMatmul
(x, w, scales, biases): x · dequantize(w)ᵀ (transpose) or x · dequantize(w).
w is two-dimensional, or has x’s batch dimensions.
Fields
Fast(FastOp)
A fused operation with a dedicated GPU kernel (crate::fast).
Compiled(Fused)
Element-wise operations fused into one generated kernel (crate::graph::Fused), as
compile builds them. The inputs share the output’s shape.
RandomBits
Random bits from threefry2x32 keys of shape [..., 2] (uint32): the output has shape
keys.shape[..-1] + shape and width bytes per element (MLX’s layout).
Custom(CustomCall)
A call of a crate::custom_function: the inputs are the primals followed by the
function’s outputs, which this node presents unchanged (views); transforms use the
function’s custom rules.
MetalKernel(KernelCall)
A custom Metal kernel (crate::fast::MetalKernel).
Copy
A row-contiguous copy of the input into a new buffer. Never simplified away (as a same-type conversion or identity view would be).
Depends
(x, deps...): x unchanged (sharing its data), evaluated only after the
n_deps other inputs. Gradients flow to x only. It orders work, as MLX’s
depends: a recomputation that depends on cotangents cannot be merged with the
forward pass.
Implementations§
Trait Implementations§
impl Eq for Primitive
impl StructuralPartialEq for Primitive
Auto Trait Implementations§
impl !RefUnwindSafe for Primitive
impl !UnwindSafe for Primitive
impl Freeze for Primitive
impl Send for Primitive
impl Sync for Primitive
impl Unpin for Primitive
impl UnsafeUnpin for Primitive
Blanket Implementations§
impl<T> AutoreleaseSafe for Twhere
T: ?Sized,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more