Repository navigation
Description
Found by the 2026-07-29 library audit (GAPS.md §7).
src/epilogue.rs opens with "The shared→global half of an epilogue". It has
one type, StoreRing, and that type hands the tile to the TMA engine. #123
then measured the engine against the vectorized-store route on gemm and the
engine lost — so the library's only epilogue abstraction is the route that lost,
and the route that ships is written out by hand in both GEMMs.
The duplication, verbatim
examples/src/gemm.rs:1371-1403 (Tile::drain_staged) and
examples/src/gemm_ws.rs:780-813 (Tile::drain_staged) are the same function:
while column < BLOCK_N as u32 {
let band: StagedBand = if WIDE { acc.tile_x8(32 * warp_id, column) }
else { acc.tile(32 * warp_id, column) };
if X4 { store_tile_x4(chunks, 0, 0, lane, band); }
else { store_tile(chunks, 0, 0, lane, band); }
store_shared_rows::<Bf16, 32, STAGE_N, Swizzle128B, 32>(
c, row_base, column_base + column, lane, stage);
warp::sync_mask(u32::MAX);
column += STAGE_N as u32;
}SHIPPED_EPILOGUE is Epilogue::StagedWideX4 (gemm.rs:172), which is this
loop at WIDE = X4 = true. gemm.rs carries four further variants of the same
skeleton (drain_staged_twice 1454-1505, drain_staged_tma 1592-1618,
drain_staged_tma_cta 1652-1676, drain_storing_twice 1721-1734) and the
register-drain rung beside them (drain 1260-1277, gemm_ws.rs 714-729).
Every instruction in it is already a library call. What is not in the library is
the loop, and the one thing in it that is not a call: warp::sync_mask —
the write-after-read the loop owes itself between bands, imported from
cuda_device because epilogue::Scope::converge is not reachable from outside
the ring (Scope is public, but the ring is the only thing that calls it).
The three supporting facts the kernels also duplicate
STAGE_N = 64(gemm.rs:429,gemm_ws.rs:458) — the staging tile's width.DRAIN_N = 128(gemm.rs:412,gemm_ws.rs:447), with the same derivation in
both docs: 256 fp32 a thread is past the 255-register architectural limit, so
a band wider than 128 cannot be drained in one pass. That is a library fact
aboutBaseLdtmand the register file, stated twice inexamples/.32 * warp_idas the band's row origin, appearing in every kernel in the repo
(flash_forward.rsinlines it five times). The32isBaseLdtm's
rows-per-warp, which the library never names.
The shape of a fix
A drain that takes the accumulator, a staging tile, a destination cursor and an
origin, and walks the band:
kittens::epilogue::drain_staged::<E, M, N, STAGE_N, S>(
accumulator, row, staging, dest, row_base, column_base, lane)with the .x8/.x4 widths as const parameters (they are already const
parameters at both call sites, WIDE and X4), and the inter-band
Scope::converge inside it.
Two things a design has to decide, both of which the call sites answer today:
- The staging tile is per-warp, so the convergence is
Warpand the
destination rectangle is this warp's 32 rows. A CTA-scope variant is the
store_shared_rows::<.., 128>arrangementglobal.rs's docs describe and
nothing in tree uses. store_shared_rows' five generic parameters are four redundant ones —
E,R,CandSall come off theSharedTileargument
(gemm.rs:1387). Whatever wraps it should not make a caller restate them.
What this does not settle
Whether StoreRing should be deleted. It should not: #123's measurement is
gemm-shaped (one item's four bands, depth 1 forced by a 1920 B envelope), and
the engine is still the right answer for a kernel that stores a whole [R, C]
box per item — softmax and layernorm both do exactly that, by hand, with
tma_store + tma_store_commit + tma_store_wait::<0>() and an identical
preceding fence (softmax.rs:351-367, layernorm.rs:300-307 and 372-379).
That three-call sequence is a third open-coded epilogue and is what
StoreRing::drain would be if the ring covered the no-ring case.
Estimate
~120 lines for the staged drain, ~40 for a single-shot TMA store that covers the
softmax/layernorm shape. The correctness bar already exists: both GEMMs
check C element-exact with == at every benchmark size, so the migration is
checkable without new infrastructure.
Related: #123 (the measurement that made the shipped route the hand-written
one), #124 (scope — the convergence inside the loop is a Scope::converge),
#125 (the shared plan the staging tiles are carved out of).
Activity
feitreim commentedon Jul 29, 2026
Checked the "verbatim" heading against a real diff of the two ranges: the loop is the same program — same band selection on `WIDE`/`X4`, same `store_tile{,_x4}` feeding the same `store_shared_rows`, same warp-scope write-after-read, same `STAGE_N` stride — but the two functions are not identical end to end.
gemm_ws's takes astage: u32and resolves both the accumulator (it has two accumulator stages) and the tile origin inline, wheregemm's takes aStageTileand callsorigin().That does not weaken the finding — the loop is the part that would move into the library, and the preamble is exactly the part that stays with each kernel because it is where the two designs actually differ. Recording it so the extraction is scoped to the loop and nobody later reports the diff as a surprise. GAPS.md §7.4 says it this way as of #133.
Name the lane, the warp and the proxy fence (#127) (#135)
Neither half of the drain has two widths, so neither is a `bool` (#217)