For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /max/get-started.md).
Mojo function
twophase_reduce_kernel
def twophase_reduce_kernel[rank: Int, axis: Int, num_reductions: Int, BLOCK_SIZE: Int, input_fn: def[dtype: DType, width: Int, rank: Int](IndexList[rank]) capturing thin -> SIMD[dtype, width], output_fn: def[dtype: DType, width: SIMDLength, rank: Int](IndexList[rank], StaticTuple[SIMD[dtype, width], num_reductions]) capturing thin -> None, reduce_fn: def[ty: DType, width: SIMDLength, reduction_idx: Int](SIMD[ty, width], SIMD[ty, width]) capturing thin -> SIMD[ty, width], dtype: DType, simd_width: Int, accum_type: DType = get_accum_type[dtype]()](shape: IndexList[rank], init: StaticTuple[Scalar[dtype], num_reductions], partials: Pointer[Scalar[accum_type], MutAnyOrigin, _safe=True], counters: Pointer[Int32, MutAnyOrigin, _safe=True], blocks_per_row: Int)
GPU kernel for reductions when there are too few rows to saturate the device at one block per row. Assigns multiple blocks per row and uses a two-phase approach: each block reduces a chunk via cooperative block-level reduction, then the last block to finish (detected via a per-row atomic counter) reduces all partial results for its row.
Parameters:
- βrank (
Int): The tensor rank. - βaxis (
Int): The axis along which to reduce. - βnum_reductions (
Int): The number of fused reductions to perform. - βBLOCK_SIZE (
Int): The number of threads per block. - βinput_fn (
def[dtype: DType, width: Int, rank: Int](IndexList[rank]) capturing thin -> SIMD[dtype, width]): The lambda to load input elements. - βoutput_fn (
def[dtype: DType, width: SIMDLength, rank: Int](IndexList[rank], StaticTuple[SIMD[dtype, width], num_reductions]) capturing thin -> None): The lambda to store output elements. - βreduce_fn (
def[ty: DType, width: SIMDLength, reduction_idx: Int](SIMD[ty, width], SIMD[ty, width]) capturing thin -> SIMD[ty, width]): The binary reduction function. - βdtype (
DType): The data type of the elements. - βsimd_width (
Int): The SIMD vector width. - βaccum_type (
DType): The accumulator data type.
Args:
- βshape (
IndexList[rank]): The shape of the input tensor. - βinit (
StaticTuple[Scalar[dtype], num_reductions]): The identity values for each reduction. - βpartials (
Pointer[Scalar[accum_type], MutAnyOrigin, _safe=True]): Global memory buffer for per-block partial results. Size: grid_dim.x * num_reductions elements of accum_type. - βcounters (
Pointer[Int32, MutAnyOrigin, _safe=True]): Global memory buffer for per-row atomic completion counters. Size: num_rows elements of int32, zero-initialized. - βblocks_per_row (
Int): The number of blocks assigned to each row.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!