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_launch

def reduce_launch[num_reductions: Int, rank: Int, dtype: DType, InputFnType: def[dtype: DType, width: Int](Coord[**?]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable, OutputFnType: def[dtype: DType, width: SIMDLength](Coord[**?], StaticTuple[SIMD[dtype, width], num_reductions]) -> None & RegisterPassable & ImplicitlyCopyable, ReduceFnType: def[ty: DType, width: SIMDLength, reduction_idx: Int](SIMD[ty, width], SIMD[ty, width]) -> SIMD[ty, width] & RegisterPassable & ImplicitlyCopyable, //, *, reduce_dim: Int](shape: IndexList[rank], init: StaticTuple[Scalar[dtype], num_reductions], input_fn: InputFnType, output_fn: OutputFnType, reduce_fn: ReduceFnType, ctx: DeviceContext)

Selects and launches the appropriate GPU reduction kernel based on the tensor shape, axis, and device saturation level.

Three-tier dispatch:

  1. Thread-saturated (many rows, non-contiguous axis): one row per thread via saturated_reduce_kernel.
  2. Block-saturated (enough rows to fill SMs at one block per row): reduce_kernel or small_reduce_kernel.
  3. Under-saturated (too few rows to fill the device): multiple blocks per row via twophase_reduce_kernel with a two-phase atomic finish.

Parameters:

  • ​num_reductions (Int): The number of fused reductions to perform.
  • ​rank (Int): The tensor rank.
  • ​dtype (DType): The data type of the elements.
  • ​InputFnType (def[dtype: DType, width: Int](Coord[**?]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable): The type of input_fn (inferred).
  • ​OutputFnType (def[dtype: DType, width: SIMDLength](Coord[**?], StaticTuple[SIMD[dtype, width], num_reductions]) -> None & RegisterPassable & ImplicitlyCopyable): The type of output_fn (inferred).
  • ​ReduceFnType (def[ty: DType, width: SIMDLength, reduction_idx: Int](SIMD[ty, width], SIMD[ty, width]) -> SIMD[ty, width] & RegisterPassable & ImplicitlyCopyable): The type of reduce_fn (inferred).
  • ​reduce_dim (Int): The axis along which to reduce.

Args:

  • ​shape (IndexList[rank]): The shape of the input tensor.
  • ​init (StaticTuple[Scalar[dtype], num_reductions]): The identity values for each reduction.
  • ​input_fn (InputFnType): The lambda to load input elements.
  • ​output_fn (OutputFnType): The lambda to store output elements.
  • ​reduce_fn (ReduceFnType): The binary reduction function.
  • ​ctx (DeviceContext): The device context for GPU execution.

Raises:

If the GPU kernel launch fails.

Was this page helpful?