IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
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, ctx: Context[params], tile_fn: TileFn)

Drives tile_fn over the reduce axis, CPU-side, with no monoid state β€” pure per-tile iteration for map/emit terminals (see the state-carrying overload below for reduce phases).

Tiers mirror the state-carrying overload; see there for the per-tier walk description.

Parameters:

  • ​params (ContextParams): Comptime dispatch parameters.
  • ​TileFn (def[ws: Int, _r: Int](IndexList[_r]) -> None & ImplicitlyCopyable): The value-closure type of tile_fn.

Args:

  • ​row_coords (Coord): The current row's coords.
  • ​axis_size (Int): Length of the reduce axis.
  • ​ctx (Context[params]): The dispatch bundle (unused on CPU beyond comptime params reads).
  • ​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, ctx: Context[params], mut state: State, tile_fn: TileFn) where (eq TileFn.State, State)

Drives tile_fn over the reduce axis, CPU-side.

Two tiers, picked from params.emit_tile_width:

  • emit_tile_width == 1: walk axis in simd_width chunks, 8-way unrolled (matches legacy _reduce_along_inner_dimension), then SIMD and scalar tails. tile_fn[ws] is called with ws ∈ {simd_width, 1}; the monoid's width-polymorphic reduce[w] collapses each tile.
  • emit_tile_width > 1: scalar axis walk, tile_fn[W] per step. The body holds W parallel accumulators (the monoid M at width W); each step issues one SIMD load on the innermost-non-axis dim, spreading lanes across the accumulators.

tile_fn is a value closure (its copy-captured state β€” input closures, side-loaded tensors β€” rides the value); it takes the caller's monoid state as a mut argument rather than capturing it, since a captured accumulator can't be mutated through a value closure (captures are immutable) but an inout argument can.

Parameters:

  • ​State (ReduceOp): The monoid type being accumulated.
  • ​params (ContextParams): Comptime dispatch parameters.
  • ​TileFn (def[ws: Int, _r: Int](mut State, IndexList[_r]) -> None & ImplicitlyCopyable): The value-closure type of tile_fn.

Args:

  • ​row_coords (Coord): The current row's coords.
  • ​axis_size (Int): Length of the reduce axis.
  • ​ctx (Context[params]): The dispatch bundle (unused on CPU beyond comptime params reads).
  • ​state (State): The caller's monoid accumulator, threaded through by mut reference and mutated in place by tile_fn.
  • ​tile_fn (TileFn): Per-tile callback; closes over input closures and folds each tile into state.