Skip to main content

Primitive

Enum Primitive 

Source
#[non_exhaustive]
pub enum Primitive {
Show 37 variants Unary(UnaryOp), Binary(BinaryOp), Select, AsType(DType), Reshape(Vec<usize>), Squeeze(Vec<usize>), ExpandDims(Vec<usize>), Transpose(Vec<usize>), Slice { start: Vec<usize>, step: Vec<isize>, len: Vec<usize>, }, Broadcast(Vec<usize>), StopGradient, View(DType), ToFp8, FromFp8(DType), SortedOffsets { groups: usize, }, 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 { quant: Quant, }, Dequantize { quant: Quant, dtype: DType, }, QuantizedMatmul { quant: Quant, transpose: bool, }, Fast(FastOp), Compiled(Fused), RandomBits { shape: Vec<usize>, width: u8, }, Custom(CustomCall), MetalKernel(KernelCall), Copy, Depends { n_deps: usize, }, Collective { op: CollectiveOp, group: usize, },
}
Expand description

An operation recorded in the graph, with its parameters.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

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.

§

Squeeze(Vec<usize>)

The input without its size-1 dimensions at axes (sorted, unique). A view. Unlike Reshape, the output shape follows the input’s (MLX’s Squeeze, which shapeless compile replays on any shape).

§

ExpandDims(Vec<usize>)

The input with size-1 dimensions inserted at the output’s axes (sorted, unique). A view (MLX’s ExpandDims).

§

Transpose(Vec<usize>)

Axes reordered: output axis i is input axis axes[i]. A view.

§

Slice

A normalised slice per dimension. A view.

Fields

§start: Vec<usize>

First index per dimension.

§step: Vec<isize>

Step per dimension (non-zero, may be negative).

§len: Vec<usize>

Number of selected indices per dimension (the output shape).

§

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.

§

View(DType)

The input’s bytes reinterpreted as dtype (MLX’s view): the same shape when the element sizes agree, else the last dimension scaled by their ratio. A view of a row-contiguous input (or of equal element sizes); otherwise a copy, reinterpreted.

§

ToFp8

Floats as E4M3 bytes (uint8), rounding to nearest even and saturating at ±448 (MLX’s to_fp8).

§

FromFp8(DType)

E4M3 bytes (uint8) as dtype floats (MLX’s from_fp8).

§

SortedOffsets

Sorted uint32 indices [..., M] to int32 [..., groups]: where each group’s rows start along the last axis (the lower bound of g; MLX’s gather_mm_offsets), which the grouped gather kernels walk.

Fields

§groups: usize

The number of groups (weight batches).

§

SliceUpdate

(base, update): a copy of base whose region selected as by Slice is replaced by update (of shape len).

Fields

§start: Vec<usize>

First index per dimension.

§step: Vec<isize>

Step per dimension (non-zero, may be negative).

§len: Vec<usize>

Number of selected indices per dimension (the update’s shape).

§

Reduce

Reduction over sorted, unique axes; reduced dimensions become size 1.

Fields

§op: ReduceOp

The reduction.

§axes: Vec<usize>

Reduced axes, sorted and unique.

§

ArgReduce

Index of an extreme value along axis (uint32); the axis becomes size 1.

Fields

§op: ArgReduceOp

Maximum or minimum.

§axis: usize

The reduced axis.

§

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.

Fields

§axis: usize

The indexed axis.

§

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.

Fields

§axis: usize

The indexed axis.

§reduce: ScatterReduce

How updates combine with dst.

§

Sort

The input sorted along axis (ascending, stable, NaN last).

Fields

§axis: usize

The sorted axis.

§

ArgSort

The indices (uint32) that sort the input along axis, as Primitive::Sort.

Fields

§axis: usize

The sorted axis.

§

Scan

A cumulative scan along axis.

Fields

§op: ScanOp

The combination.

§axis: usize

The scanned axis.

§reverse: bool

Scan from the end of the axis.

§inclusive: bool

Whether each output includes its own element (else it holds the elements before it, starting from the combination’s identity).

§

Concatenate

The inputs (one dtype, equal shapes except along axis) joined along axis.

Fields

§axis: usize

The joined 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).

Fields

§alpha: u32

f32::to_bits of alpha.

§beta: u32

f32::to_bits of beta.

§

Quantize

Quantization of the last axis in groups (MLX’s quantize). Outputs the packed codes (uint32, bits per value) and the scales: for affine, scales and biases in the input’s dtype; for the floating-point modes, uint8 scale codes.

Fields

§quant: Quant

The mode, group size and bits.

§

Dequantize

(w, scales[, biases]): the packed values restored in dtype (affine: code · scale + bias in the scales’ dtype, which dtype equals; the floating-point modes have no biases).

Fields

§quant: Quant

The mode, group size and bits.

§dtype: DType

The output dtype.

§

QuantizedMatmul

(x, w, scales[, biases]): x · dequantize(w)ᵀ (transpose) or x · dequantize(w). w is two-dimensional, or has x’s batch dimensions. Biases exist for affine only.

Fields

§quant: Quant

The mode, group size and bits.

§transpose: bool

Whether the dequantized matrix is used transposed (MLX’s default).

§

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).

Fields

§shape: Vec<usize>

The shape drawn per key.

§width: u8

Bytes per element: 1, 2 or 4.

§

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.

Fields

§n_deps: usize

The number of dependencies after x.

§

Collective

A collective over a distributed group (by id; crate::distributed).

Fields

§op: CollectiveOp

The operation and its parameters.

§group: usize

The group’s id.

Implementations§

Source§

impl Primitive

Source

pub fn name(&self) -> &'static str

The operation’s name, as used in error messages.

Source

pub fn fixes_shape(&self) -> bool

Whether the parameters hold sizes taken from the traced shapes, so a replay on other shapes would be wrong (MLX’s primitives without output_shapes): shapeless compile rejects these. Broadcast is listed; shapeless compile re-derives its shape from the operands it was broadcast against. Attention keeps kernel choices made for the traced lengths.

Trait Implementations§

Source§

impl Clone for Primitive

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Primitive

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Primitive

Source§

impl Hash for Primitive

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Primitive

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Primitive

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AutoreleaseSafe for T
where T: ?Sized,

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.