For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).
Mojo struct
Row
struct Row[params: ContextParams, accum: DType, dtype: DType, axis: Int, rank: Int, AxisSize: CoordLike, is_cached: Bool]
The body's handle to one logical row. See the module-level note.
Carries no input_fn field: it's threaded as a fresh value-closure
ARGUMENT to every method that needs it (a "forwarding bridge"),
rather than stored as a typed field. A field whose type is a
trait-bound closure that references a sibling struct parameter
(dtype) hits a Mojo limitation the moment the struct itself is
named generically elsewhere (e.g. as another method's parameter or
return type) β "lacking evidence to prove correctness" even when
the constraint holds by construction. Method-level trait-bound
params (fresh per call, not part of Self's persistent identity)
don't hit this; every other closure here (Contribute, G,
Write, ...) already uses that shape.
Parametersβ
- βparams (
ContextParams): Comptime tier parameters (target, tier, widths). - βaccum (
DType): Accumulator dtype the reduce closures compute in. - βdtype (
DType): The primary (axis-walked) input dtype. - βaxis (
Int): Reduced axis (statically known). - βrank (
Int): Tensor rank. - βAxisSize (
CoordLike): The reduced-axis length'sCoordLiketype βComptimeInt[N]when known at comptime (enables the register-resident_fusepath) or a dynamicScalartype otherwise. One type carries both the comptime-known-ness and (via.value()) the runtime length, so the caller supplies a singleAxisSize-typed value instead of a separate comptime "is it static" signal. - βis_cached (
Bool): Whether this view may cache the row β true for normalizing multi-phase bodies; false for single-pass true reductions where caching buys nothing.
Fieldsβ
- βrow_il (
IndexList[rank]): - βaxis_size (
AxisSize): - βctx (
Context[params]):
Implemented traitsβ
AnyType,
Copyable,
Deinitable,
Movable
Methodsβ
__init__β
def __init__[InputFn: def[width: Int, alignment: Int, coord_rank: Int](IndexList[coord_rank]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable](out self, row_coords: Coord, axis_size: AxisSize, ctx: Context[params], input_fn: InputFn) where (eq InputFn.dtype, dtype)
Builds the view, staging the row strip into registers on cache-eligible tiers.
Parameters:
- βInputFn (
def[width: Int, alignment: Int, coord_rank: Int](IndexList[coord_rank]) -> SIMD[dtype, width]&RegisterPassable&ImplicitlyCopyable): The value-closure type ofinput_fn.
Args:
- βrow_coords (
Coord): The row's coords. - βaxis_size (
AxisSize): Length of the reduce axis, as anAxisSize-typed value (ComptimeInt[N]()when statically known, else a dynamicScalar). - βctx (
Context[params]): The dispatch bundle. - βinput_fn (
InputFn): Loads a tile of the primary input at a coord. Used only here (fuse-path staging); not retained as a field β later non-fuse-path methods take it again as an argument.
reduceβ
def reduce[M: ReduceOp, Contribute: def[w: Int](SIMD[dtype, w], IndexList[rank]) -> SIMD[accum, w] & RegisterPassable & ImplicitlyCopyable, InputFn: def[width: Int, alignment: Int, coord_rank: Int](IndexList[coord_rank]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable](mut self, contribute: Contribute, input_fn: InputFn) -> M where (eq Contribute.dtype, dtype) where (eq Contribute.rank, rank) where (eq Contribute.accum, accum) where (eq InputFn.dtype, dtype)
Runs one reduction phase: folds contribute(tile, idx) into M across the row, cross-thread-joins, and returns the joined state. The body reads M's result fields (e.g. ReduceSum.acc[0], Welford.mean[0], ArgMin.best_idx).
contribute is a value closure (its copy-captured state rides the
value); the tier loop's reduce_tile captures it directly.
input_fn (the primary-input loader) is likewise a fresh value
argument, used only on the non-fuse (streaming) path β see the
module note above Row on why it isn't a stored field.
Parameters:
- βM (
ReduceOp): The monoid for this phase (instantiated at the view's SIMD width, e.g.ReduceSum[accum, params.simd_width]). - βContribute (
def[w: Int](SIMD[dtype, w], IndexList[rank]) -> SIMD[accum, w]&RegisterPassable&ImplicitlyCopyable): The value-closure type ofcontribute. - βInputFn (
def[width: Int, alignment: Int, coord_rank: Int](IndexList[coord_rank]) -> SIMD[dtype, width]&RegisterPassable&ImplicitlyCopyable): The value-closure type ofinput_fn.
Args:
- βcontribute (
Contribute): Per-element math; maps the loaded primary tile + its coord to the accumulator-dtype contribution. - βinput_fn (
InputFn): Loads a tile of the primary input at a coord (non-fuse path only).
Returns:
M: The joined M, broadcast to every participant.
def reduce[T: DType, shared: Bool, //, M: ReduceOp, Contribute: def[w: Int](SIMD[T, w], IndexList[rank]) -> SIMD[accum, w] & RegisterPassable & ImplicitlyCopyable, InputFn: def[width: Int, alignment: Int, coord_rank: Int](IndexList[coord_rank]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable, Compute: def[w: Int](SIMD[dtype, w], IndexList[rank]) -> SIMD[T, w] & RegisterPassable & ImplicitlyCopyable](mut self, over: RowCache[params, T, dtype, axis, rank, (Int((add (mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width), AxisSize.static_value if AxisSize.is_static_value else Int(0), -1)) // Int((mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width))) if is_cached and (xor is_cpu[params.target](), True) and (params != ReduceTier(Int(3))) and (eq (add rank, -1), axis) and (xor (eq (xor (lt AxisSize.static_value if AxisSize.is_static_value else Int(0), 1), True), False), True) and (eq (AxisSize.static_value if AxisSize.is_static_value else Int(0) % params), 0) and (xor (eq (lt (mul (Int((add (mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width), AxisSize.static_value if AxisSize.is_static_value else Int(0), -1)) // Int((mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width))), size_of[SIMD[dtype, SIMDLength(params.simd_width)]]()), 257), False), True) else Int(1), params.simd_width, AxisSize.static_value if AxisSize.is_static_value else Int(0), is_cached and not is_cpu[params.target]().__bool__() and (params != ReduceTier.Splitk) and (axis == (rank - Int(1))) and (AxisSize.static_value if AxisSize.is_static_value else Int(0) > Int(0)) and ((AxisSize.static_value if AxisSize.is_static_value else Int(0) % params) == Int(0)) and (Int((mul (Int((add (mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width), AxisSize.static_value if AxisSize.is_static_value else Int(0), -1)) // Int((mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width))), size_of[SIMD[dtype, SIMDLength(params.simd_width)]]())) <= Int(256)), shared], contribute: Contribute, input_fn: InputFn, compute: Compute) -> M where (eq Contribute.T, T) where (eq Contribute.rank, rank) where (eq Contribute.accum, accum) where (eq InputFn.dtype, dtype) where (eq Compute.dtype, dtype) where (eq Compute.rank, rank) where (eq Compute.T, T)
Cached overload of reduce: folds contribute over a previously cached row instead of the primary input β reads the staged value at each column (registers on the fast path, recompute fallback otherwise). Used for a phase that reduces a computed cross-phase value (e.g. sum of the fused intermediate squared).
Resolved at compile time from plain Mojo function overloading β
whether the call site passes over β not a runtime-checked
Optional, so the cached-vs-plain choice costs nothing at
runtime.
contribute is a value closure (its copy-captured state rides the
value); the tier loop's reduce_tile captures it directly.
input_fn / compute (the same producer passed to the cache
call that built over) are likewise fresh value arguments, used
only on the non-fuse (recompute) path β RowCache doesn't carry
them as fields (see its module note). axis_size / ctx are read
from self rather than threaded as separate arguments β the
owning view already holds both.
Parameters:
- βT (
DType): The staged value's dtype (inferred fromover). - βshared (
Bool): The staged handle's shmem flag (inferred). - βM (
ReduceOp): The monoid for this phase. - βContribute (
def[w: Int](SIMD[T, w], IndexList[rank]) -> SIMD[accum, w]&RegisterPassable&ImplicitlyCopyable): The value-closure type ofcontribute. - βInputFn (
def[width: Int, alignment: Int, coord_rank: Int](IndexList[coord_rank]) -> SIMD[dtype, width]&RegisterPassable&ImplicitlyCopyable): The value-closure type ofinput_fn. - βCompute (
def[w: Int](SIMD[dtype, w], IndexList[rank]) -> SIMD[T, w]&RegisterPassable&ImplicitlyCopyable): The value-closure type ofcompute.
Args:
- βover (
RowCache[params, T, dtype, axis, rank, (Int((add (mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width), AxisSize.static_value if AxisSize.is_static_value else Int(0), -1)) // Int((mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width))) if is_cached and (xor is_cpu[params.target](), True) and (params != ReduceTier(Int(3))) and (eq (add rank, -1), axis) and (xor (eq (xor (lt AxisSize.static_value if AxisSize.is_static_value else Int(0), 1), True), False), True) and (eq (AxisSize.static_value if AxisSize.is_static_value else Int(0) % params), 0) and (xor (eq (lt (mul (Int((add (mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width), AxisSize.static_value if AxisSize.is_static_value else Int(0), -1)) // Int((mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width))), size_of[SIMD[dtype, SIMDLength(params.simd_width)]]()), 257), False), True) else Int(1), params.simd_width, AxisSize.static_value if AxisSize.is_static_value else Int(0), is_cached and not is_cpu[params.target]().__bool__() and (params != ReduceTier.Splitk) and (axis == (rank - Int(1))) and (AxisSize.static_value if AxisSize.is_static_value else Int(0) > Int(0)) and ((AxisSize.static_value if AxisSize.is_static_value else Int(0) % params) == Int(0)) and (Int((mul (Int((add (mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width), AxisSize.static_value if AxisSize.is_static_value else Int(0), -1)) // Int((mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width))), size_of[SIMD[dtype, SIMDLength(params.simd_width)]]())) <= Int(256)), shared]): The cached row to reduce. - βcontribute (
Contribute): Per-element math over the staged tile, as a value. - βinput_fn (
InputFn): Loads a tile of the primary input at a coord (non-fuse path only). - βcompute (
Compute): The producer that builtover(non-fuse path only).
Returns:
M: The joined M, broadcast to every participant.
elementwiseβ
def elementwise[G: def[w: Int](SIMD[dtype, w], IndexList[rank]) -> None & RegisterPassable & ImplicitlyCopyable, InputFn: def[width: Int, alignment: Int, coord_rank: Int](IndexList[coord_rank]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable](mut self, g: G, input_fn: InputFn) where (eq G.dtype, dtype) where (eq G.rank, rank) where (eq InputFn.dtype, dtype)
Per-element terminal (normalizing reductions): runs g(tile, idx) over the row; g computes and stores each output element. Reuses the register cache when present.
g is a value closure (its copy-captured state rides the value);
the tier loop's emit_tile captures it directly. input_fn is
likewise a fresh value argument (non-fuse path only) β see the
module note above Row.
Parameters:
- βG (
def[w: Int](SIMD[dtype, w], IndexList[rank]) -> None&RegisterPassable&ImplicitlyCopyable): The value-closure type ofg. - βInputFn (
def[width: Int, alignment: Int, coord_rank: Int](IndexList[coord_rank]) -> SIMD[dtype, width]&RegisterPassable&ImplicitlyCopyable): The value-closure type ofinput_fn.
Args:
- βg (
G): Per-element write; closes over the joined results and side inputs, and stores via the body's output closure. - βinput_fn (
InputFn): Loads a tile of the primary input at a coord (non-fuse path only).
def elementwise[T: DType, shared: Bool, //, G: def[w: Int](SIMD[T, w], IndexList[rank]) -> None & ImplicitlyCopyable, InputFn: def[width: Int, alignment: Int, coord_rank: Int](IndexList[coord_rank]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable, Compute: def[w: Int](SIMD[dtype, w], IndexList[rank]) -> SIMD[T, w] & RegisterPassable & ImplicitlyCopyable](mut self, over: RowCache[params, T, dtype, axis, rank, (Int((add (mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width), AxisSize.static_value if AxisSize.is_static_value else Int(0), -1)) // Int((mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width))) if is_cached and (xor is_cpu[params.target](), True) and (params != ReduceTier(Int(3))) and (eq (add rank, -1), axis) and (xor (eq (xor (lt AxisSize.static_value if AxisSize.is_static_value else Int(0), 1), True), False), True) and (eq (AxisSize.static_value if AxisSize.is_static_value else Int(0) % params), 0) and (xor (eq (lt (mul (Int((add (mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width), AxisSize.static_value if AxisSize.is_static_value else Int(0), -1)) // Int((mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width))), size_of[SIMD[dtype, SIMDLength(params.simd_width)]]()), 257), False), True) else Int(1), params.simd_width, AxisSize.static_value if AxisSize.is_static_value else Int(0), is_cached and not is_cpu[params.target]().__bool__() and (params != ReduceTier.Splitk) and (axis == (rank - Int(1))) and (AxisSize.static_value if AxisSize.is_static_value else Int(0) > Int(0)) and ((AxisSize.static_value if AxisSize.is_static_value else Int(0) % params) == Int(0)) and (Int((mul (Int((add (mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width), AxisSize.static_value if AxisSize.is_static_value else Int(0), -1)) // Int((mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width))), size_of[SIMD[dtype, SIMDLength(params.simd_width)]]())) <= Int(256)), shared], g: G, input_fn: InputFn, compute: Compute) where (eq G.T, T) where (eq G.rank, rank) where (eq InputFn.dtype, dtype) where (eq Compute.dtype, dtype) where (eq Compute.rank, rank) where (eq Compute.T, T)
Cached overload of elementwise: the per-element terminal g receives the staged value at each column (from over) instead of the primary input β registers on the fast path, recompute fallback otherwise. g may additionally read other columns of over via over.load (e.g. rope's rotate-half partner, staged in shmem).
Resolved at compile time from plain Mojo function overloading β
whether the call site passes over β not a runtime-checked
Optional, so the cached-vs-plain choice costs nothing at
runtime.
g is a value closure (its copy-captured state rides the value);
the tier loop's emit_tile captures it directly. input_fn /
compute (the same producer passed to the cache call that
built over) are likewise fresh value arguments, used only on
the non-fuse (recompute) path β RowCache doesn't carry them as
fields (see its module note). axis_size / ctx are read from
self rather than threaded as separate arguments β the owning
view already holds both.
The bound on G is ImplicitlyCopyable (not RegisterPassable
like the other value overloads) because g may copy-capture
non-register-passable state (e.g. over itself, for a terminal
that also reads a different column via over.load); this overload
runs inside the launched kernel (not across a launch boundary),
so register-passability is not required.
Parameters:
- βT (
DType): The staged value's dtype (inferred fromover). - βshared (
Bool): The staged handle's shmem flag (inferred). - βG (
def[w: Int](SIMD[T, w], IndexList[rank]) -> None&ImplicitlyCopyable): The value-closure type ofg. - βInputFn (
def[width: Int, alignment: Int, coord_rank: Int](IndexList[coord_rank]) -> SIMD[dtype, width]&RegisterPassable&ImplicitlyCopyable): The value-closure type ofinput_fn. - βCompute (
def[w: Int](SIMD[dtype, w], IndexList[rank]) -> SIMD[T, w]&RegisterPassable&ImplicitlyCopyable): The value-closure type ofcompute.
Args:
- βover (
RowCache[params, T, dtype, axis, rank, (Int((add (mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width), AxisSize.static_value if AxisSize.is_static_value else Int(0), -1)) // Int((mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width))) if is_cached and (xor is_cpu[params.target](), True) and (params != ReduceTier(Int(3))) and (eq (add rank, -1), axis) and (xor (eq (xor (lt AxisSize.static_value if AxisSize.is_static_value else Int(0), 1), True), False), True) and (eq (AxisSize.static_value if AxisSize.is_static_value else Int(0) % params), 0) and (xor (eq (lt (mul (Int((add (mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width), AxisSize.static_value if AxisSize.is_static_value else Int(0), -1)) // Int((mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width))), size_of[SIMD[dtype, SIMDLength(params.simd_width)]]()), 257), False), True) else Int(1), params.simd_width, AxisSize.static_value if AxisSize.is_static_value else Int(0), is_cached and not is_cpu[params.target]().__bool__() and (params != ReduceTier.Splitk) and (axis == (rank - Int(1))) and (AxisSize.static_value if AxisSize.is_static_value else Int(0) > Int(0)) and ((AxisSize.static_value if AxisSize.is_static_value else Int(0) % params) == Int(0)) and (Int((mul (Int((add (mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width), AxisSize.static_value if AxisSize.is_static_value else Int(0), -1)) // Int((mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width))), size_of[SIMD[dtype, SIMDLength(params.simd_width)]]())) <= Int(256)), shared]): The cached row to walk. - βg (
G): Per-element write over the staged tile, as a value. - βinput_fn (
InputFn): Loads a tile of the primary input at a coord (non-fuse path only). - βcompute (
Compute): The producer that builtover(non-fuse path only).
emitβ
def emit[Write: def(IndexList[rank]) -> None & RegisterPassable & ImplicitlyCopyable](mut self, write: Write) where (eq Write.rank, rank)
Per-row terminal (true reductions): runs write(out_coord) once on the canonical writer for this row. write closes over the joined results and stores them, and is taken as a value arg (its copy-captured state rides the value) rather than a comptime capturing parameter.
Unlike the per-element terminal (elementwise, plain or cached),
emit needs no comptime-form + value-shim pair: once is an
in-kernel thread predicate (not a launch boundary and not a
per-element loop), so the emit_once wrapper (which pins the
reduced axis and calls write) is itself a value closure passed
straight to once, with no per-element env-hoist risk.
Parameters:
- βWrite (
def(IndexList[rank]) -> None&RegisterPassable&ImplicitlyCopyable): The value-closure type ofwrite.
Args:
- βwrite (
Write): Per-row terminal store, passed as a value. Stores the row's output(s) at the collapsed coordinate (reduced axis pinned to 0).
cacheβ
def cache[T: DType, Compute: def[w: Int](SIMD[dtype, w], IndexList[rank]) -> SIMD[T, w] & RegisterPassable & ImplicitlyCopyable, shared: Bool = False](mut self, compute: Compute) -> RowCache[params, T, dtype, axis, rank, (Int((add (mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width), AxisSize.static_value if AxisSize.is_static_value else Int(0), -1)) // Int((mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width))) if is_cached and (xor is_cpu[params.target](), True) and (params != ReduceTier(Int(3))) and (eq (add rank, -1), axis) and (xor (eq (xor (lt AxisSize.static_value if AxisSize.is_static_value else Int(0), 1), True), False), True) and (eq (AxisSize.static_value if AxisSize.is_static_value else Int(0) % params), 0) and (xor (eq (lt (mul (Int((add (mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width), AxisSize.static_value if AxisSize.is_static_value else Int(0), -1)) // Int((mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width))), size_of[SIMD[dtype, SIMDLength(params.simd_width)]]()), 257), False), True) else Int(1), params.simd_width, AxisSize.static_value if AxisSize.is_static_value else Int(0), is_cached and not is_cpu[params.target]().__bool__() and (params != ReduceTier.Splitk) and (axis == (rank - Int(1))) and (AxisSize.static_value if AxisSize.is_static_value else Int(0) > Int(0)) and ((AxisSize.static_value if AxisSize.is_static_value else Int(0) % params) == Int(0)) and (Int((mul (Int((add (mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width), AxisSize.static_value if AxisSize.is_static_value else Int(0), -1)) // Int((mul _resolve_warp_size() if (params == ReduceTier(Int(1))) else params.BLOCK_SIZE, params.simd_width))), size_of[SIMD[dtype, SIMDLength(params.simd_width)]]())) <= Int(256)), shared] where (eq Compute.dtype, dtype) where (eq Compute.rank, rank) where (eq Compute.T, T)
Caches a per-row value the later phases reuse: runs compute(tile, idx) once over the row and stages the result so the cached overloads of reduce / elementwise (and, with shared=True, load at any column) read it back instead of recomputing.
On a cache-eligible tier this writes each participant's tiles into
registers (and, when shared, publishes the row to a shmem strip
with one row-local sync); otherwise the returned handle recomputes
on access (always correct) β the caller re-supplies input_fn /
compute to the cached reduce / elementwise overloads on that
path, since RowCache doesn't carry them as fields (see its
module note).
compute runs exactly once per element on the fast path but may
re-run on the fallback, so any side effect it performs (e.g. a
residual emit) must be idempotent.
Parameters:
- βT (
DType): The cached value's dtype. - βCompute (
def[w: Int](SIMD[dtype, w], IndexList[rank]) -> SIMD[T, w]&RegisterPassable&ImplicitlyCopyable): The value-closure type ofcompute. - βshared (
Bool): Publish the row to shmem for cross-participantload.
Args:
- βcompute (
Compute): Per-element producer from the primary tile + coord.
Returns: