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 struct
BlockReducer
struct BlockReducer[BLOCK_SIZE: Int]
Reduces a scalar across BLOCK_SIZE threads in a block. Broadcasts the result to every thread.
Parametersβ
- βBLOCK_SIZE (
Int): Number of threads in the launching block.
Implemented traitsβ
AnyType,
Copyable,
Deinitable,
ImplicitlyCopyable,
Movable,
Reducer,
RegisterPassable,
TrivialRegisterPassable
Methodsβ
__init__β
def __init__() -> Self
Default-initializes a BlockReducer.
sumβ
def sum[dtype: DType](self, val: Scalar[dtype]) -> Scalar[dtype]
Returns the sum of val across the block.
Parameters:
- βdtype (
DType): The scalar dtype.
Args:
- βval (
Scalar[dtype]): The per-thread value.
Returns:
Scalar[dtype]: The block-wide sum, broadcast to every thread.
maxβ
def max[dtype: DType](self, val: Scalar[dtype]) -> Scalar[dtype]
Returns the maximum of val across the block.
Parameters:
- βdtype (
DType): The scalar dtype.
Args:
- βval (
Scalar[dtype]): The per-thread value.
Returns:
Scalar[dtype]: The block-wide maximum, broadcast to every thread.
minβ
def min[dtype: DType](self, val: Scalar[dtype]) -> Scalar[dtype]
Returns the minimum of val across the block.
Parameters:
- βdtype (
DType): The scalar dtype.
Args:
- βval (
Scalar[dtype]): The per-thread value.
Returns:
Scalar[dtype]: The block-wide minimum, broadcast to every thread.
genericβ
def generic[S: ReduceOp](self, mut state: S)
Block-wide all-reduce over state.join; on return every thread holds the combined value.
Dispatches on state size (a perf choice β either path is
byte-safe since ReduceOp is TrivialRegisterPassable):
- Small (
<= _WARP_SHUFFLE_MAX_WORDSuint32 words): a register-only within-warp warp-shuffle butterfly, then a sparse one-entry-per-warp shmem store + a second shuffle over warp 0 (mirrors legacywelford_block_all_reduce) βBLOCK_SIZE/WARP_SIZEstores + one barrier, ~30x less shmem traffic than the tree. - Large: a
log2(BLOCK_SIZE)-step block-wide shmem tree. Past a few words the per-word shuffle + register pressure makes the tree comparable or better.
Parameters:
- βS (
ReduceOp): The monoid type being combined.
Args:
- βstate (
S): The per-thread state; on return, holds the block-wide combined value on every thread.