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, mut ctx: Context[params], tile_fn: TileFn)

Drives tile_fn over the reduce axis on the target backend, with no monoid state β€” pure per-tile iteration. See the state-carrying overload below for reduce phases.

Parameters:

  • ​params (ContextParams): Comptime dispatch parameters (target + tier).
  • ​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.
  • ​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)

Drives tile_fn over the reduce axis on the target backend.

See the CPU and GPU reduce impls for per-target tier semantics; both call tile_fn[ws, _r] per axis tile, folding into state. tile_fn is a value closure taking state as a mut argument (not a capture β€” a captured accumulator can't be mutated through a value closure).

Parameters:

  • ​State (ReduceOp): The monoid type being accumulated.
  • ​params (ContextParams): Comptime dispatch parameters (target + tier).
  • ​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.
  • ​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.