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 struct
Welford
struct Welford[dtype: DType, W: Int = Int(1)]
Welford's online mean/variance monoid (Welford 1962, Chan et al. 1979 for the combine) β the reduction half of layer_norm / group_norm.
Holds {count, mean, M2} β running sample count, mean, and sum of
squared deviations from the mean β lane-wise across a width-W SIMD
field. Variance is M2 / count. Combining two sub-states uses
Chan's formula:
n = na + nb
Ξ΄ = ΞΌb - ΞΌa
ΞΌ = ΞΌa + Ξ΄ * nb / n
M2 = M2_a + M2_b + δ² * na * nb / naccumulate[W] is elementwise: each call folds one new sample
into every lane's Welford (Chan with nb = 1). Because every lane
advances its count in lock-step, 1 / count is a single scalar
reciprocal β sidestepping the per-lane SIMD fdiv that serializes
through the GPU divider β with no per-tile horizontal reduce.
reduce Chan-combines the W lanes once at the end. On the tiled
tier the lanes are independent output columns, so
reduce/join_parallel are skipped and each lane's Welford stands
as that column's statistics.
Triple-field state: count, mean, M2, each a SIMD[dtype, W].
join_parallel cross-thread-combines through reducer.generic β
Welford's combine isn't a hardware primitive.
Parametersβ
- βdtype (
DType): The accumulator dtype (must be floating-point). - βW (
Int): SIMD width of the per-field storage. Defaults to1(scalar state). Cooperative tiers pass the body's SIMD width so each lane accumulates a strided slice of the row; the tiled tier passestile_widthso each lane is an output column.
Fieldsβ
- βcount (
SIMD[dtype, W]): Per-lane running sample count (uniform across lanes during accumulation). Afterreduce, all lanes hold the combined count. - βmean (
SIMD[dtype, W]): Per-lane running mean. Afterreduce, all lanes hold the combined mean (cooperative); on the tiled tier each lane is its column's mean. - βM2 (
SIMD[dtype, W]): Per-lane running sum-of-squared-deviations-from-the-mean. Variance isM2 / count. Afterreduce, all lanes hold the combinedM2(cooperative); on the tiled tier each lane is its column'sM2.
Implemented traitsβ
AnyType,
Copyable,
ImplicitlyCopyable,
ImplicitlyDeletable,
Movable,
ReduceOp,
RegisterPassable,
TrivialRegisterPassable
comptime membersβ
Singleβ
comptime Single = Welford[dtype]
widthβ
comptime width = W
Methodsβ
__init__β
def __init__() -> Self
Identity: every lane at (0, 0, 0).
__getitem__β
def __getitem__(self, j: Int) -> Self.Single
Returns lane j as a width-1 monoid.
Returns:
Self.Single
__setitem__β
def __setitem__(mut self, j: Int, s: Welford[dtype])
Writes width-1 monoid s into lane j.
accumulateβ
def accumulate[val_dtype: DType, w: Int](mut self, val: SIMD[val_dtype, w], idx: SIMD[DType.int64, w] = 0)
Elementwise Welford fold: each lane folds one new sample into its running state (Chan with nb = 1).
For a full tile (w == W) every lane advances its count in
lock-step, so 1 / new_count is a single scalar reciprocal and
the SIMD work is one sub + two fmas β no horizontal reduce, no
per-lane divide. A partial tile (w < W, the scalar tail of an
odd row) masks the count increment to its first w lanes, which
costs a per-lane reciprocal; tiers at W > 1 only hit full tiles
(the row divides the SIMD width), so this path is compiled but
never executed there.
Parameters:
Args:
- βval (
SIMD[val_dtype, w]): The SIMD tile to fold. - βidx (
SIMD[DType.int64, w]): Unused (Welford is index-agnostic).
joinβ
def join(mut self, other: Self)
Sequential combine of two Welford states via Chan's formula, lane-wise.
Args:
- βother (
Self): The state to combine intoself.
join_parallelβ
def join_parallel[R: Reducer](mut self, reducer: R)
Cross-thread combine via reducer.generic (Welford's combine isn't a hardware primitive), then broadcast the result from lane 0 across all lanes so a body reads (mean, M2, count).slice[w] uniformly. Runs after reduce.
Parameters:
- βR (
Reducer): The parallel reducer.
Args:
- βreducer (
R): The reducer instance.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!