IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /max/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. /max/get-started.md).

Mojo trait

Reducer

Parallel scalar reducer over the closed set of associative ops.

Reduces a scalar across all participants in the current parallel scope; every participant receives the reduced value (like MPI_Allreduce).

Concrete impls provide hardware-fast sum / max / min over a closed set of dtypes, plus a generic fallback that combines any ReduceOp via its join (slower; dtype-agnostic).

Implemented traits​

AnyType

Required methods​

sum​

def sum[dtype: DType](self, val: Scalar[dtype]) -> Scalar[dtype]

Returns the sum of val across all participants.

Parameters:

  • ​dtype (DType): The scalar dtype.

Args:

Returns:

Scalar[dtype]: The reduced sum, broadcast to every participant.

max​

def max[dtype: DType](self, val: Scalar[dtype]) -> Scalar[dtype]

Returns the maximum of val across all participants.

Parameters:

  • ​dtype (DType): The scalar dtype.

Args:

Returns:

Scalar[dtype]: The reduced maximum, broadcast to every participant.

min​

def min[dtype: DType](self, val: Scalar[dtype]) -> Scalar[dtype]

Returns the minimum of val across all participants.

Parameters:

  • ​dtype (DType): The scalar dtype.

Args:

Returns:

Scalar[dtype]: The reduced minimum, broadcast to every participant.

generic​

def generic[S: ReduceOp](self, mut state: S)

Combines state across all participants using only join β€” the multi-field path for monoids whose state isn't decomposable into scalar sum/max/min (Welford, ArgMax, ArgMin, …).

Impls pick by state size: small states (a few uint32 words) use a register-only warp-shuffle butterfly, larger states a shmem tree β€” both valid since ReduceOp is TrivialRegisterPassable. Monoids that can decompose (ReduceSum, ReduceMax, OnlineLogSumExp, …) instead call reducer.sum/max/min directly, which compile to one hardware redux.sync per field and are faster.

Parameters:

  • ​S (ReduceOp): The monoid type being combined.

Args:

  • ​state (S): Per-participant state; on return, holds the combined value on every participant.