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 function
reduce
def reduce[params: ContextParams, //, TileFn: def[ws: Int, _r: Int](IndexList[_r]) -> None & ImplicitlyCopyable](row_coords: Coord, axis_size: Int, mut ctx: Context[params], tile_fn: TileFn)
Drives the tier-aware iteration over the reduce axis, with no monoid state β pure per-tile iteration for map/emit terminals (see the state-carrying overload below for reduce phases).
tile_fn[ws, _r] is invoked per tile with the scaffolder's SIMD
width and the row's coords. It's a value closure (its copy-captured
state β input/output closures β rides the value).
reduce runs iteration only; the body calls rowwise.pjoin
separately for each state needing a cross-thread combine.
Two-pass algorithms (softmax pass 2, layernorm rewrite) use
reduce with no pjoin β pure map-style iteration.
Tier semantics, picked from ctx:
- Warp tier (cooperative,
emit_tile_width == 1): laneiruns one scalartile_fn[1]on elementi(axis_size <= WARP_SIZE). - Block tier (cooperative,
emit_tile_width == 1): threads grid-stride byBLOCK_SIZE * simd_widthalong the axis; full SIMDtile_fn[simd_width]on aligned chunks, scalartile_fn[1]on the tail. - Tiled tier (
emit_tile_width > 1): the thread ownsemit_tile_widthadjacent rows. Each axis step issues onetile_fn[emit_tile_width]; the body dispatches each lane to its own state. No cross-thread combine βrowwise.pjoinno-ops. - Serial tier (one thread, one row): scalar
tile_fn[1]per axis step. - Split-K tier (
num_rows < sm_count, large row): each block handles a slice of one row;rowwise.pjoincoordinates across blocks.
Parameters:
- βparams (
ContextParams): The comptime dispatch parameters. - βTileFn (
def[ws: Int, _r: Int](IndexList[_r]) -> None&ImplicitlyCopyable): The value-closure type oftile_fn.
Args:
- βrow_coords (
Coord): The current row's coords.ctx.axisis the reduce axis; other dims are pinned. - βaxis_size (
Int): Length of the reduce axis. - βctx (
Context[params]): The dispatch bundle. - βtile_fn (
TileFn): Per-tile callback; closes over input/output closures.
def reduce[State: ReduceOp, params: ContextParams, //, TileFn: def[ws: Int, _r: Int](mut State, IndexList[_r]) -> None & ImplicitlyCopyable](row_coords: Coord, axis_size: Int, mut ctx: Context[params], mut state: State, tile_fn: TileFn) where (eq TileFn.State, State)
State-carrying overload of reduce: same tier-aware iteration as above, but tile_fn takes the caller's monoid state as a mut argument (rather than capturing it β a captured accumulator can't be mutated through a value closure) and folds each tile into it.
Parameters:
- βState (
ReduceOp): The monoid type being accumulated. - βparams (
ContextParams): The comptime dispatch parameters. - βTileFn (
def[ws: Int, _r: Int](mut State, IndexList[_r]) -> None&ImplicitlyCopyable): The value-closure type oftile_fn.
Args:
- βrow_coords (
Coord): The current row's coords.ctx.axisis the reduce axis; other dims are pinned. - βaxis_size (
Int): Length of the reduce axis. - βctx (
Context[params]): The dispatch bundle. - βstate (
State): The caller's monoid accumulator, threaded through bymutreference and mutated in place bytile_fn. - βtile_fn (
TileFn): Per-tile callback; closes over input closures and folds each tile intostate.