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

row_reduce

def row_reduce[InputFnType: def[dtype: DType, width: Int](Coord[**?]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable, ReduceFnType: def[dtype: DType, width: SIMDLength](SIMD[dtype, width], SIMD[dtype, width]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable, //, BLOCK_SIZE: Int, dtype: DType, simd_width: Int, accum_type: DType = get_accum_type[dtype](), *, axis: Int](mut row_coords: Coord[*row_coords.element_types], init: Scalar[dtype], row_size: Int, input_fn: InputFnType, reduce_fn: ReduceFnType) -> Scalar[accum_type]

Reduces a single row along the given axis using block-level cooperative reduction. Delegates to the multi-reduction row_reduce overload with num_reductions=1.

Parameters:

  • ​InputFnType (def[dtype: DType, width: Int](Coord[**?]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable): The type of input_fn (inferred).
  • ​ReduceFnType (def[dtype: DType, width: SIMDLength](SIMD[dtype, width], SIMD[dtype, width]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable): The type of reduce_fn (inferred).
  • ​BLOCK_SIZE (Int): The number of threads per block.
  • ​dtype (DType): The data type of the input elements.
  • ​simd_width (Int): The SIMD vector width.
  • ​accum_type (DType): The accumulator data type (defaults to widened type).
  • ​axis (Int): The axis along which to reduce.

Args:

  • ​row_coords (Coord[*row_coords.element_types]): The ND coordinates identifying the row.
  • ​init (Scalar[dtype]): The identity value for the reduction.
  • ​row_size (Int): The number of elements in the row.
  • ​input_fn (InputFnType): The lambda to load input elements.
  • ​reduce_fn (ReduceFnType): The binary reduction function.

Returns:

Scalar[accum_type]: The reduced scalar result for the row.

def row_reduce[InputFnType: def[dtype: DType, width: Int](Coord[**?]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable, ReduceFnType: def[dtype: DType, width: SIMDLength, reduction_idx: Int](SIMD[dtype, width], SIMD[dtype, width]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable, //, BLOCK_SIZE: Int, num_reductions: Int, dtype: DType, simd_width: Int, accum_type: DType = get_accum_type[dtype](), *, axis: Int](mut row_coords: Coord[*row_coords.element_types], init: StaticTuple[Scalar[dtype], num_reductions], row_size: Int, input_fn: InputFnType, reduce_fn: ReduceFnType) -> StaticTuple[Scalar[accum_type], num_reductions]

Reduces a row along the given axis with multiple fused reductions using cooperative SIMD-width reads across threads, a block_reduce, and scalar tail handling.

Parameters:

  • ​InputFnType (def[dtype: DType, width: Int](Coord[**?]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable): The type of input_fn (inferred).
  • ​ReduceFnType (def[dtype: DType, width: SIMDLength, reduction_idx: Int](SIMD[dtype, width], SIMD[dtype, width]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable): The type of reduce_fn (inferred).
  • ​BLOCK_SIZE (Int): The number of threads per block.
  • ​num_reductions (Int): The number of fused reductions to perform.
  • ​dtype (DType): The data type of the input elements.
  • ​simd_width (Int): The SIMD vector width.
  • ​accum_type (DType): The accumulator data type (defaults to widened type).
  • ​axis (Int): The axis along which to reduce.

Args:

  • ​row_coords (Coord[*row_coords.element_types]): The ND coordinates identifying the row.
  • ​init (StaticTuple[Scalar[dtype], num_reductions]): The identity values for each reduction.
  • ​row_size (Int): The number of elements in the row.
  • ​input_fn (InputFnType): The lambda to load input elements.
  • ​reduce_fn (ReduceFnType): The binary reduction function parameterized by reduction index.

Returns:

StaticTuple[Scalar[accum_type], num_reductions]: The reduced scalar results, one per fused reduction.

Was this page helpful?