Repository navigation
Fold four warps into one number, and the fp32 element under it (#3) - #64
Merged
Merged
Conversation
`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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
groupnorm_tilewas the last error across all four example kernels. It is gone,and so is the
layernormfeature that gated it.What landed
impl Element for F32(src/shared.rs).Unpacked = [f32; 1],BYTES = 4,PER_WORD = 1,pack/unpackthe identity on the bits,read/writea plainload and store. Nothing assumed
PER_WORD == 2: the two places that needed apair already said so as a bound (
Element<Unpacked = [f32; 2]>onldst'sstmatrix/ldmatrixpaths), so an fp32 element fails to typecheck there ratherthan packing silently wrong. Deliberately not an
MmaElement— tcgen05reads fp32 operands as tf32, a different mantissa.
sync::block_reduce::<Op, WARPS>andblock_reduce_sum. Warpwstages itsvalue at element
wof aSharedVec<F32, WARPS>, the block syncs, and everythread folds all
WARPSslots in index order fromOp::IDENTITY. The result isblock-uniform bit for bit, so a kernel uses it as a scalar operand immediately
with no broadcast step.
block_reduce_sumisblock_reduce::<Add, _>and not ahand-written twin.
groupnorm_tileleaves its gate. TheWANTblock and the raw*mut f32aregone; the kernel is in the default build and gets a real
cargo oxide build --arch sm_100a, which is the only thing that codegensSharedVec<F32, 4>andtherefore the only thing that proves its post-monomorphization box assert admits
16 bytes. The
layernormcargo feature is deleted rather than left empty —a feature gating nothing only makes
gapsprint a blank section — andmodal_app.py::gapsloses its second loop iteration.Device case
block reduction, on a real B200. Four warps, four distinctpowers 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 ischecked and not sampled. The seeds go in through
RegTile::splatandtile_sum, so what runs is the same compositiongroupnorm_tileruns.Three decisions, and one finding
The bound is
ReduceOp, not the issue'sBinaryOp. The warps' partialsarrive 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
SubandDivout ofrow_reduce. The identity also lets one loopserve every width.
The input must be warp-uniform; it does not fold across lanes first. Its
input is a
tile_sumresult, already the whole warp's, and folding again wouldcount all 32 lanes' copies. Under
Addthe two readings differ by a factor of32, so this is stated as a safety condition and a per-lane value has to go
through
warp_reduceat the call site.Two barriers, and the second is the reusable one.
groupnorm_tilecalls thereduction 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
# Safetysection says so.
Finding:
WARPSmust be a multiple of four.SharedVec::BOX_OKwants awhole 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
SharedVecthat never meets theengine; whether the assert belongs on
from_rawor on the four TMA methods is alive question this PR does not answer. It is documented on
block_reduce, pinnedby a test, and left for a follow-up.
On
ScopeNot done, per the issue, and building the reduction produced no new evidence
for it. The collective takes
WARPSas a bare const parameter and derivesnothing 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 byaccident, and
Warp::sync()besideBlock::sync()makes two very differentlypriced instructions look interchangeable — and
examples/README.mdandGAPS.md§1.1 now record that #3 shipped the specific collective rather than theparameterization, 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, includingcargo oxide build kittens-examples --arch sm_100awithgroupnorm_tilein the default set.modal run modal_app.py::device_tests— 23 of 23 cases passed, includingblock 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_probeis 21 registers, nospill, no stack.
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'sBoperand 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 thegapsrun above.
#8entry saidGlobalLayoutis bf16-only "becauseElementisimplemented only for
Bf16". That reason has moved one trait along:Elementhas an fp32 impl now and
TensorMapElementdoes not, which is one associatedconstant, left undone because nothing in tree TMAs an fp32 buffer.
#11entry andGlobalRows' own doc said the fp32-only mover was waitingon an fp32
Element. It is not any more — it is a choice with no callerasking otherwise, and both now say that.
pipeline::runentry said the cluster work-item map needs Block-scope reductions: the fold four warps need, and the fp32 Element under it #3'sScope. Itdoes not; what it needs is a cluster rank, which is unfiled.
Closes #3