Skip to content

Fold four warps into one number, and the fp32 element under it (#3) - #64

Merged
feitreim merged 2 commits into
mainfrom
block-reduce
Jul 28, 2026
Merged

feitreim merged 2 commits into
mainfrom
block-reduce

Conversation

@feitreim

Copy link
Copy Markdown
Owner

groupnorm_tile was the last error across all four example kernels. It is gone,
and so is the layernorm feature that gated it.

What landed

impl Element for F32 (src/shared.rs). Unpacked = [f32; 1], BYTES = 4,
PER_WORD = 1, pack/unpack the identity on the bits, read/write a plain
load and store. Nothing assumed PER_WORD == 2: the two places that needed a
pair already said so as a bound (Element<Unpacked = [f32; 2]> on ldst's
stmatrix/ldmatrix paths), so an fp32 element fails to typecheck there rather
than packing silently wrong. Deliberately not an MmaElement — tcgen05
reads fp32 operands as tf32, a different mantissa.

sync::block_reduce::<Op, WARPS> and block_reduce_sum. Warp w stages its
value at element w of a SharedVec<F32, WARPS>, the block syncs, and every
thread folds all WARPS slots in index order from Op::IDENTITY. The result is
block-uniform bit for bit, so a kernel uses it as a scalar operand immediately
with no broadcast step. block_reduce_sum is block_reduce::<Add, _> and not a
hand-written twin.

groupnorm_tile leaves its gate. The WANT block and the raw *mut f32 are
gone; the kernel is in the default build and gets a real cargo oxide build --arch sm_100a, which is the only thing that codegens SharedVec<F32, 4> and
therefore the only thing that proves its post-monomorphization box assert admits
16 bytes. The layernorm cargo feature is deleted rather than left empty —
a feature gating nothing only makes gaps print a blank section — and
modal_app.py::gaps loses its second loop iteration.

Device case block reduction, on a real B200. Four warps, four distinct
powers of eight, so the host reads a returned number's base-8 digits as which
slot was folded how many times
: a warp reading only its own slot, a slot read
twice and a slot skipped are three different numbers rather than three ways of
being wrong. Three folds back to back on one scratch — sum, then the doubled
partials, then a Max — dumped by all 128 threads, so block-uniformity is
checked and not sampled. The seeds go in through RegTile::splat and
tile_sum, so what runs is the same composition groupnorm_tile runs.

Three decisions, and one finding

The bound is ReduceOp, not the issue's BinaryOp. The warps' partials
arrive in slot order, which is not an order the caller chose, so a fold over them
has to be associative and commutative to mean anything — the identical argument
that keeps Sub and Div out of row_reduce. The identity also lets one loop
serve every width.

The input must be warp-uniform; it does not fold across lanes first. Its
input is a tile_sum result, already the whole warp's, and folding again would
count all 32 lanes' copies. Under Add the two readings differ by a factor of
32, so this is stated as a safety condition and a per-lane value has to go
through warp_reduce at the call site.

Two barriers, and the second is the reusable one. groupnorm_tile calls the
reduction twice on one scratch with nothing between the calls but arithmetic, so
the question is whether a barrier is owed there. It is not: the collective syncs
after its own read as well as before it, so the variance pass cannot overwrite
a slot the mean pass has not folded yet. The alternative spelling — sync before
the write instead of after the read — costs exactly the same two barriers and
makes the rule a precondition on the caller rather than a postcondition the
function establishes. This one was chosen so back-to-back calls are
self-sufficient by construction; what the caller still owes is a barrier if some
unrelated use of the same memory precedes the first call, and the # Safety
section says so.

Finding: WARPS must be a multiple of four. SharedVec::BOX_OK wants a
whole number of the TMA's 16-byte lines, so four fp32 — 16 bytes exactly, as the
issue predicted — is the narrowest scratch that constructs, and a 1- or 2-warp
block cannot take this reduction at all. That rule is a statement about a TMA
box, and a scratch vector is the one use of SharedVec that never meets the
engine; whether the assert belongs on from_raw or on the four TMA methods is a
live question this PR does not answer. It is documented on block_reduce, pinned
by a test, and left for a follow-up.

On Scope

Not done, per the issue, and building the reduction produced no new evidence
for it
. The collective takes WARPS as a bare const parameter and derives
nothing else from it, so there was no smaller thing to argue for. The two
cautions in the issue both still stand — Scope::rank() would settle #27 by
accident, and Warp::sync() beside Block::sync() makes two very differently
priced instructions look interchangeable — and examples/README.md and
GAPS.md §1.1 now record that #3 shipped the specific collective rather than the
parameterization, so the sweep is unfiled rather than silently dropped.

Verified, not inferred

  • modal run modal_app.py::gaps — exit 0, one section (flash), no errors.
  • modal run modal_app.py::build — green, including cargo oxide build kittens-examples --arch sm_100a with groupnorm_tile in the default set.
  • modal run modal_app.py::device_tests — 23 of 23 cases passed, including
    block reduction 4 partials folded three ways, identical in 128 threads.
  • modal run modal_app.py::examples — gemm 512x256x256 exact, softmax 2x256x128 rows normalized, worst relative error 1.97e-3.
  • modal run modal_app.py::regcount — block_reduce_probe is 21 registers, no
    spill, no stack.
  • Locally, all three crates: cargo fmt --check, cargo clippy --all-targets --locked -D warnings (root), cargo test --locked (81 pass), RUSTDOCFLAGS=-D warnings cargo doc --no-deps --locked, cargo metadata --locked.

Docs corrected while in there

  • examples/README.md: the GEMM's B operand is integers in [-10, 10], not
    [-2, 2] (gemm: make the reference B vary along K, so the exact check covers the K walk #55). Status table and per-kernel gap list rewritten from the gaps
    run above.
  • The #8 entry said GlobalLayout is bf16-only "because Element is
    implemented only for Bf16". That reason has moved one trait along: Element
    has an fp32 impl now and TensorMapElement does not, which is one associated
    constant, left undone because nothing in tree TMAs an fp32 buffer.
  • The #11 entry and GlobalRows' own doc said the fp32-only mover was waiting
    on an fp32 Element. It is not any more — it is a choice with no caller
    asking otherwise, and both now say that.
  • The pipeline::run entry said the cluster work-item map needs Block-scope reductions: the fold four warps need, and the fp32 Element under it #3's Scope. It
    does not; what it needs is a cluster rank, which is unfiled.

Closes #3

feitreim added 2 commits July 28, 2026 10:32
`sync::block_reduce::<Op, WARPS>` stages one warp-uniform value per warp in a
`SharedVec<F32, WARPS>` and folds all of them in every thread, so the result is
block-uniform bit for bit; `block_reduce_sum` is the `Add` wrapper, the way
`scale_rows` is a `row_map_assign` wrapper. Under it, the `impl Element for F32`
first element in the crate that is deliberately not an `MmaElement`.

`groupnorm_tile` leaves its feature gate with the `WANT` block and the raw
`*mut f32`: it is in the default build and gets a real `sm_100a` codegen, which
is what proves `SharedVec<F32, 4>`'s post-monomorphization box assert admits 16
bytes. The `layernorm` cargo feature is deleted rather than kept empty, and
`gaps` loses its second section.

Two decisions the semantics forced. The bound is `ReduceOp`, not the issue's
`BinaryOp`: partials arrive in slot order, so the fold has to be associative and
commutative to mean anything. And the collective syncs *after* its own read as
well as before it, which is what makes `groupnorm_tile`'s two back-to-back calls
on one scratch safe with no barrier at the call site.

Found on the way: `WARPS` must be a multiple of four. `SharedVec::BOX_OK` wants
a whole number of the TMA's 16-byte lines, so a 1- or 2-warp block cannot
construct the scratch at all — a rule about a TMA box, applied to the one use of
the type that never meets the engine.

Device case `block reduction`: four distinct powers of eight, so the host reads
the returned number's base-8 digits as which slot was folded how many times.
Three folds in a row on one scratch, the second over doubled partials so a stale
read is a digit rather than a total, dumped by all 128 threads.
…the default build

The two asserts were forced by from_raw, which reads as "a SharedVec is a
thing the TMA can deliver" -- a stronger claim than either makes. Both are
about a descriptor's box, and block_reduce's scratch is the one use of the
type that never becomes one: a four-warp block's partials are 16 bytes and a
two-warp block's are 8, so only the first could be constructed. They now sit
on the four transfer methods, where they still fire at codegen for every
caller they genuinely bind.

With every gap list empty, the cargo features held nothing back and actively
hurt: a gated kernel is absent from the default set that
`cargo oxide build kittens-examples --arch sm_100a` compiles, which made the
examples carrying the most library surface the ones CI never codegened.
flash_forward and groupnorm_tile join the default build, the features and the
gaps entrypoint are retired.
@feitreim
feitreim merged commit f940a45 into main Jul 28, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Block-scope reductions: the fold four warps need, and the fp32 Element under it

1 participant