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, //](row_coords: Coord[*row_coords.element_types], axis_size: Int, mut ctx: Context[params], tile_fn: T)

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

Args:

  • ​row_coords (Coord[*row_coords.element_types]): The current row's coords; the reduce axis must be a dynamic Int.
  • ​axis_size (Int): Length of the reduce axis.
  • ​ctx (Context[params]): The dispatch bundle.
  • ​tile_fn (T): Per-tile callback; closes over input/output closures.

def reduce[State: ReduceOp, params: ContextParams, //](row_coords: Coord[*row_coords.element_types], axis_size: Int, mut ctx: Context[params], mut state: State, tile_fn: T)

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

Args:

  • ​row_coords (Coord[*row_coords.element_types]): The current row's coords; the reduce axis must be a dynamic Int.
  • ​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 (T): Per-tile callback; closes over input closures and folds each tile into state.

Was this page helpful?