Skip to content

The staged drain is one library call, and three kernels stop writing the loop - #210

Merged
feitreim merged 3 commits into
mainfrom
library/staged-drain
Aug 6, 2026
Merged

feitreim merged 3 commits into
mainfrom
library/staged-drain

Conversation

@feitreim

@feitreim feitreim commented Aug 6, 2026

Copy link
Copy Markdown
Owner

src/epilogue.rs opened with "the shared→global half of an epilogue" and held
one type, StoreRing, which hands the tile to the TMA engine — the route #123
measured losing on gemm. The route that ships was written out by hand three
times.

The three copies, and what they became

before after
examples/src/gemm.rs::Tile::drain the loop, widths baked in epilogue::Drain::<LDTM_X8, STMATRIX_X4>::staged(..)
experiments/src/gemm.rs::Tile::drain_staged the loop, WIDE/X4 epilogue::Drain::<WIDE, X4>::staged(..)
experiments/src/gemm_ws.rs::Tile::drain_staged the loop, WIDE/X4 epilogue::Drain::<WIDE, X4>::staged(..)
softmax.rs, layernorm.rs ×2 fence + tma_store + commit + wait::<0> Cta::store_once(..)

Six open-coded epilogues, six calls. warp::sync_mask is gone from two of the
three GEMM copies — it was imported from cuda_device only because
Scope::converge was reachable from outside the ring nowhere else.

Two shapes, and why neither is a free function

Drain<LDTM_X8, STMATRIX_X4>::staged walks a warp's band: LDTM into
registers, stmatrix into the staging tile, store_shared_rows out to C,
Warp::converge between passes. The widths are the type's const parameters
because Rust will not let a caller name some of a function's generic arguments —
naming the two widths would drag E, R, C and S to every call site, which
are exactly the four the SharedTile argument already carries (the issue's
second design question). Scope::store_once is a trait method for the same
reason: the scope is the one thing a call has to say.

The scope is Warp and 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 second
function when something does.

The band index means the same thing at both ends. TMEM rows
WARP_ROWS * band land in destination rows row + WARP_ROWS * band, so callers
pass the CTA's origin and 32 * warp_id is written once instead of twice.
examples' origin() and experiments' cta_origin() are now the same
function.

The library facts that moved

  • BaseLdtm::WARP_ROWS — the 32 in every 32 * warp_id in the repo, which
    the library described in warp_id()'s doc and never named as a value.
    tmem::warp_lanes reads off it.
  • BaseLdtm::WIDEST_BAND — DRAIN_N's 128, whose derivation both GEMM docs
    stated independently. A const block in reg.rs now checks it: a thread
    holds one fp32 per column of its band, so WIDEST_BAND fits the 255 registers
    the architecture has and twice it does not. Both DRAIN_Ns read off it.

StoreRing stays, for the reason the issue gives: #123's measurement is
gemm-shaped, at a depth its 1920 B envelope forced to 1, and the engine is
still 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 its
staging tile through tile_x8_batched::<32, BAND_N, BAND_ISSUES> and converges
twice 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_cta and the register-drain rungs, which are other shapes.

Gates

Run by hand — GitHub Actions is out. All on the rebased tree (c251d72).

gate result
scripts/fmt --check clean
cargo test 86 passed
cargo clippy --all-targets -- -D warnings (lib) clean
cargo doc --features host with -D warnings clean
scripts/modal-run build 0 — ap-FJQKuszpfV5hBdkXq2eK7Q
scripts/modal-run regcount 0 — ap-k8ORjLHitw2bVxAFhU2jnU
scripts/modal-run examples 0, every case exact — ap-VYRc6tCShdHjGC2RPQ7l6R
scripts/modal-run device_tests 0, 57 of 57 — ap-rY245Omlkg0CRuOwsItOEc

examples covers both binaries: gemm at both check sizes and all three
traversal widths, gemm_ws' four staged rungs, gemm_sol's four drains,
softmax, softmax_x4, layernorm, groupnorm, flash_forward — every GEMM
row 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
regcount after the rebase reproduced every one of these rows and added only
the new entry points #206/#207 brought.

The shipped set, unmoved:

kernel regs spill st spill ld stack
gemm_cg2_staged_x8x4 (examples & experiments) 80 → 80 12 → 12 16 → 16 8 → 8
gemm_sol_m256 / _n128 / m512 82/80/83 → same 0 0 0
softmax_rows 32 → 32 0 0 0
groupnorm_tile 48 → 48 0 0 0
flash_forward 168 → 168 0 0 1568 → 1568

The opcode census is identical for every gemm_* entry point in both crates,
and the .local depot census is identical everywhere — no instruction moved and
no new LLVM local memory appeared.

Three rows did move, and none of them is a shipped GEMM:

kernel before after
examples/layernorm_rows 40, no spills 48, no spills
experiments/gemm_ws_staged 110, no spills 96, 32 st / 36 ld, 24 B
experiments/gemm_ws_staged_x8x4 80, no spills 80, 8 st / 8 ld, 8 B

layernorm_rows is one ptxas granule, and it lands on 48 — exactly what the
experiments/ copy of that same source already read, before and after
. The
kernel declares no launch_bounds and shared memory is its occupancy term, so
48 × 128 threads costs it nothing.

The two gemm_ws staged rungs are the honest finding: same PTX instruction mix,
ptxas allocating over a different schedule and spilling where the hand-written
loop did not. Both are lab rungs and neither ships; gemm_ws_staged_x4 and
_x8 did not move, and the shipped gemm_cg2_staged_x8x4 — the same library
call 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 regcount run and moved nothing, which is what says it is allocation
drift rather than a live value the library added. The hoist is kept; it is the
better code either way.

Closes #126

…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
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.

The epilogue that ships is open-coded in both GEMMs; epilogue.rs holds the route that lost

1 participant