Repository navigation
The staged drain is one library call, and three kernels stop writing the loop - #210
Merged
Merged
Conversation
…the loop `src/epilogue.rs` held one type, `StoreRing`, which hands a staging tile to the TMA engine — the route #123 measured *losing* on `gemm`. The route that ships was written out by hand three times: `examples/src/gemm.rs`, `experiments/src/gemm.rs` and `experiments/src/gemm_ws.rs` walked the same band loop, with the same two instruction widths, the same `store_shared_rows`, and the same `warp::sync_mask` imported from `cuda_device` because `Scope::converge` was reachable only through the ring. `epilogue::Drain::<LDTM_X8, STMATRIX_X4>::staged` is that loop. The widths are the type's const parameters rather than the function's because naming any of a generic function's arguments in Rust means naming all of them, and the element, both tile shapes and the swizzle are already in the `SharedTile` argument. The inter-band `Warp::converge` is inside it, and so is the band origin: TMEM rows `WARP_ROWS * band` land in destination rows `row + WARP_ROWS * band`, so the callers pass the CTA's origin and `32 * warp_id` is written once instead of twice. Two library facts the kernels duplicated move with it. `BaseLdtm::WARP_ROWS` is the 32 in every `32 * warp_id` in the repo, and `BaseLdtm::WIDEST_BAND` is `DRAIN_N`'s 128 — a thread holds one fp32 per column of its band, so 256 is past the 255 registers the architecture has, which a `const` block in `reg.rs` now checks rather than two kernel docs asserting it in prose. `Scope::store_once` is the third open-coded epilogue: the proxy fence, one `cp.async.bulk.tensor`, its commit and the visibility wait, which `softmax` and `layernorm` wrote out at three sites. It is `StoreRing` with no ring — the shape for a kernel that stores one whole box per item — and it is a trait method for the same reason the widths are a type: the scope is the one thing a call has to name. `StoreRing` stays. #123's measurement is `gemm`-shaped, at a depth its envelope forced to 1, and the engine is still the right answer for a kernel that stores a whole `[R, C]` box per item. `gemm_sol`'s drain does not migrate: it walks sub-bands narrower than its staging tile through `tile_x8_batched` and converges twice a pass, so putting it on this shape is a codegen change on the kernel with the tightest register count in the repo — a measurement rather than a refactor. Closes #126
This was referenced Aug 6, 2026
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.
src/epilogue.rsopened with "the shared→global half of an epilogue" and heldone type,
StoreRing, which hands the tile to the TMA engine — the route #123measured losing on
gemm. The route that ships was written out by hand threetimes.
The three copies, and what they became
examples/src/gemm.rs::Tile::drainepilogue::Drain::<LDTM_X8, STMATRIX_X4>::staged(..)experiments/src/gemm.rs::Tile::drain_stagedWIDE/X4epilogue::Drain::<WIDE, X4>::staged(..)experiments/src/gemm_ws.rs::Tile::drain_stagedWIDE/X4epilogue::Drain::<WIDE, X4>::staged(..)softmax.rs,layernorm.rs×2fence+tma_store+ commit +wait::<0>Cta::store_once(..)Six open-coded epilogues, six calls.
warp::sync_maskis gone from two of thethree GEMM copies — it was imported from
cuda_deviceonly becauseScope::convergewas reachable from outside the ring nowhere else.Two shapes, and why neither is a free function
Drain<LDTM_X8, STMATRIX_X4>::stagedwalks a warp's band: LDTM intoregisters,
stmatrixinto the staging tile,store_shared_rowsout toC,Warp::convergebetween passes. The widths are the type's const parametersbecause Rust will not let a caller name some of a function's generic arguments —
naming the two widths would drag
E,R,CandSto every call site, whichare exactly the four the
SharedTileargument already carries (the issue'ssecond design question).
Scope::store_onceis a trait method for the samereason: the scope is the one thing a call has to say.
The scope is
Warpand not a parameter, per the issue's first design answer.The CTA arrangement —
store_shared_rows::<.., 128>over a[128, N]tile —is what
global's docs describe and nothing in tree uses; it is a secondfunction when something does.
The band index means the same thing at both ends. TMEM rows
WARP_ROWS * bandland in destination rowsrow + WARP_ROWS * band, so callerspass the CTA's origin and
32 * warp_idis written once instead of twice.examples'origin()andexperiments'cta_origin()are now the samefunction.
The library facts that moved
BaseLdtm::WARP_ROWS— the32in every32 * warp_idin the repo, whichthe library described in
warp_id()'s doc and never named as a value.tmem::warp_lanesreads off it.BaseLdtm::WIDEST_BAND—DRAIN_N's 128, whose derivation both GEMM docsstated independently. A
constblock inreg.rsnow checks it: a threadholds one fp32 per column of its band, so
WIDEST_BANDfits the 255 registersthe architecture has and twice it does not. Both
DRAIN_Ns read off it.StoreRingstays, for the reason the issue gives: #123's measurement isgemm-shaped, at a depth its 1920 B envelope forced to 1, and the engine isstill the right answer for a kernel that stores a whole
[R, C]box per item.What did not migrate
gemm_sol's drain, in both copies. It walks sub-bands narrower than itsstaging tile through
tile_x8_batched::<32, BAND_N, BAND_ISSUES>and convergestwice a pass, so it is a different loop, not this one at other widths. Bending
the library to it would mean a conditional convergence and a nested walk fitted
to one caller; bending it to the library would move the codegen of the kernel
with the tightest register count in the repo. That is a measurement, not a
refactor. Also left alone:
drain_staged_twice,drain_staged_tma,drain_staged_tma_ctaand the register-drain rungs, which are other shapes.Gates
Run by hand — GitHub Actions is out. All on the rebased tree (c251d72).
scripts/fmt --checkcargo testcargo clippy --all-targets -- -D warnings(lib)cargo doc --features hostwith-D warningsscripts/modal-run buildscripts/modal-run regcountscripts/modal-run examplesscripts/modal-run device_testsexamplescovers both binaries:gemmat both check sizes and all threetraversal widths,
gemm_ws' four staged rungs,gemm_sol's four drains,softmax,softmax_x4,layernorm,groupnorm,flash_forward— every GEMMrow
exact.regcount, before and after
Measured as an isolated pair on the 6cd342a base (both runs exit 0), so the
three PRs that landed under this branch cannot be confused for it. Re-running
regcountafter the rebase reproduced every one of these rows and added onlythe new entry points #206/#207 brought.
The shipped set, unmoved:
gemm_cg2_staged_x8x4(examples & experiments)gemm_sol_m256/_n128/m512softmax_rowsgroupnorm_tileflash_forwardThe opcode census is identical for every
gemm_*entry point in both crates,and the
.localdepot census is identical everywhere — no instruction moved andno new LLVM local memory appeared.
Three rows did move, and none of them is a shipped GEMM:
examples/layernorm_rowsexperiments/gemm_ws_stagedexperiments/gemm_ws_staged_x8x4layernorm_rowsis oneptxasgranule, and it lands on 48 — exactly what theexperiments/copy of that same source already read, before and after. Thekernel declares no
launch_boundsand shared memory is its occupancy term, so48 × 128 threads costs it nothing.
The two
gemm_wsstaged rungs are the honest finding: same PTX instruction mix,ptxasallocating over a different schedule and spilling where the hand-writtenloop did not. Both are lab rungs and neither ships;
gemm_ws_staged_x4and_x8did not move, and the shippedgemm_cg2_staged_x8x4— the same librarycall in the other GEMM — is byte-identical. Two attempts to chase it (restoring
the original preamble order, hoisting both band origins out of the walk) each
cost a
regcountrun and moved nothing, which is what says it is allocationdrift rather than a live value the library added. The hoist is kept; it is the
better code either way.
Closes #126