Repository navigation
The epilogue's shared half, as a ring with the fences in it (#15) - #111
Merged
Merged
Conversation
This was referenced Jul 29, 2026
Merged
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.
softmaxends withstore_tile→fence.proxy.async.shared::cta→bar.sync→tma_store→commit→wait::<0>, written out by hand, and itis the only kernel in the repo that has ever run that chain.
StoreRingisthat chain as a library type, generalised in the one direction the epilogue of
a kernel with no room for its output tile actually needs:
DEPTHstagingbuffers instead of one, so band
k + 1'sstmatrixneed not wait out bandk's store.This is the primitive only. Nothing in
examples/is wired to it — that isthe follow-up, after the ablation ladder lands, and "the arithmetic" below is
written so that follow-up is choosing rather than discovering.
What landed
src/epilogue.rs—StoreRing<E, R, C, S, IN_FLIGHT>:IN_FLIGHT + 1swizzled staging tiles behind a cursor, with
acquire/commit/commit_2d/drain. ~130 lines of code; the rest is the fence and waitdiscipline written down.
device-tests— four cases,store ring depth 1 / 2 / 4 / 2 wide, allpassing on a B200 (36 of 36 in the harness).
regcount:gemm_cg2unchanged at 166 regs / 0 spill, headroom 89 to itsstep. Nothing in
examples/was touched.A separate type, not an extension of
SharedTileRingSharedTileRingis addressing and nothing else —Ntiles andindex % N—and its completion mechanism is supplied separately, by a
SemaphoreRingthecaller pairs it with. That pairing works for loads because a load's destination
is shared memory, where an mbarrier can live and count arriving bytes. A store's
destination is global memory, where none can, so a store's completion is a
per-thread group count and there is no semaphore ring to pair with. The
discipline has nowhere to live but the ring itself, which is why the methods
here are CTA-collective and
SharedTileRing's are pure pointer arithmetic.Folding the two together would put
sync_threadsinto a type that today has nothread identity in it at all.
There is a mechanical half to the same answer. The wait's group count is
DEPTH - 1, andtma_store_wait_read::<{DEPTH - 1}>()is not something Rustwill evaluate in generic-argument position without
generic_const_exprs— therestriction
Element::UnpackedandMmaElement::mmaalready dodge, for thesame reason. So the parameter is
IN_FLIGHT, the number the instructiontakes, and
DEPTH = IN_FLIGHT + 1is derived. A type parameterised that waycould not embed a
SharedTileRing<.., { IN_FLIGHT + 1 }>even if the semanticshad wanted it to.
IN_FLIGHT = 0is the single-buffer ring, and issoftmax's shape.The fence and wait discipline
Three mechanisms, none subsuming another. Full derivation is in the module docs.
fence.proxy.async.shared::cta, before the engine reads a buffer.stmatrixwrites through the generic proxy and the TMA engine reads throughthe async one. The fence orders the writes of the thread that executes it,
so every thread that wrote a row fences, and the
bar.syncafter it is whatcarries that ordering to the one thread that issues the store. A
bar.syncalone is not enough: it orders generic-proxy accesses against each other and
says nothing about a proxy it does not name. Both live inside
commit, in thatorder.
cp.async.bulk.wait_group.read, before a buffer is written again. Recyclinga buffer needs only the engine's reads to be done; blocking on global
visibility would serialize the overlap the ring exists for. Groups are per
thread and age in issue order, so with
DEPTHgroups committed, leaving atmost
IN_FLIGHTwith outstanding reads is exactly "the oldest — this buffer's —is read-complete". The wait is taken by the issuing thread and only by it, and
the
bar.syncafter it is what releases the other warps past a wait they didnot take. Both live inside
acquire. BelowDEPTHcommitted bands the wait istrivially satisfied, so the ring needs no fill phase and no special first
iteration.
cp.async.bulk.wait_group, once, at the end. Nothing else makes a bulkstore visible to a following kernel or to the host — not the end of the kernel,
not a
bar.sync, not dropping the handle. That isdrain, and it is the oneobligation the ring cannot discharge incrementally.
Two
bar.syncper band, which at depth 1 is one more thansoftmaxpays byhand (the acquire barrier, which
softmaxdoes not need because it stores once).The arithmetic, so the follow-up is choosing
The budget. Shared per SM on a B200 is 233472 B and the GEMM holds 2 CTAs,
so a CTA gets 116736 B against a 98392 B plan: 18344 B free without
moving residency. Confirmed on device by this branch's
tmem residency censusrun (
[256,256] s3 (gemm), 98392 B, holding 2). Worth stating explicitly: that2 is set by tensor memory —
BLOCK_N = 256is 256 accumulator columns and512 / 256 = 2— not by shared memory, which is why the plan can grow all theway to 116736 B before anything moves.
What a buffer costs, from
StoreRing::BYTESat the shapes a GEMM epiloguewould want (all asserted in the unit tests):
[128, 128]bf16 — a wholeDRAIN_Nband[128, 64]bf16Against 18344 B of headroom:
[128, 64]fits at depth 1 and nowhere else.[128, 128]does not fit even at depth 1. And[128, 32]is not a shape —SharedTile::WIDTH_OKwants a whole 64-column swizzle subtile, so 64 columnsis the narrowest bf16 staging tile that exists, which also means the register
band the epilogue drains has to be re-shaped to
[32, 64]to match it (aregister-count change, and
regcountis what will say).At
[128, 64]a CTA's128 × 256bf16 output is four round trips an item,and at depth 1 those four are fully serialized —
acquireiswait_read::<0>,which is the drain the ring exists to avoid.
Where I disagree with the brief
The 6.2% ceiling and the 18344 B headroom check out.
examples/README.md§7.4 is where both come from, and I reproduced the budget on device. The 6.2% is
an upper bound on an upper bound there (half the
2s - lcfstore column at8192³, with a note that some of that 13.11 µs may be register pressure), and
§7.4's own rows say the epilogue's placement (
lcsf) and its bytes (hot)are each worth nothing — so what is left for
stmatrixis strictly theissue-count claim.
But
STAGES3→2 is not unmeasured, and it is not available. The brief callsit "the available buyer … has never been measured". #87's rung table in
examples/README.md§7 has it, at this exact tile:[256,256] s3(shipped)[256,256] s2Dropping a stage costs 11.8% at 8192³ and 7.3% at 16384³ — and, note the
CTA/SM column, it costs that at unchanged residency. No occupancy step is
being paid there; it is K-pipeline depth, directly. So the trade is worse than
the brief frames it, and for a different reason: spending 32768 B on a depth-2
ring means recovering 11.8% out of a 6.2% ceiling at 8192³. Net negative by
roughly 2×, with no occupancy step anywhere in the story.
What that leaves, and it is the one configuration that costs nothing to try:
[128, 64]at depth 1, 16384 B, inside the existing headroom, no change toSTAGES, no change to residency. Its whole value isstmatrix's issue countagainst four serialized store round trips it did not previously have. That is
the experiment I would run first, and it is a measurement, not an argument.
(If a depth-2 ring is wanted anyway, the byte arithmetic at
STAGES = 2is:plan 65608, headroom 51128, which admits
[128, 64]at depth 3 (49152) or[128, 128]at depth 1 (32768) — but every one of those starts 11.8% down.)Can
softmaxbe expressed in terms of it?Yes, at depth 1, and I have not done it — the brief marks it optional and it
is a separate change. Two things a reviewer should know before anyone does:
softmax's tile is both the TMA load destination and the store stagingbuffer. The ring can serve that —
StoreRing::buffer(0)hands out theSharedTileto load into — butbufferis deliberately documented ascarrying none of the ring's discipline, so this is the caller stepping
outside the type's story rather than the type covering the case.
bar.syncon a bandwidth-bound kernel:acquire's (whichsoftmaxdoes not need, storing once) anddrain's (which it does not have,ending instead). Small, but it is a regression risk on a kernel measured at
4414 GB/s, so it should be measured rather than assumed free.
Testing
device_testson a B200, 36 of 36:Eight bands through each ring, so every buffer of the deepest ring wraps at
least once — depth 1 cannot exercise the reuse hazard, and a ring that never
wraps has nothing for
acquireto wait on. Four warps write each buffer and onethread issues each store, which is the arrangement the collective halves exist
for. The registers are filled from
RegTile::coordinateand never loaded, so noTMA load, no mbarrier and no MMA can be the cause of a mismatch. The wide case
is two stacked subtiles, so its commit issues two boxes into one group — the
case that says a group is a tile and not an instruction.
Bands are told apart by rotating their column identities by an odd stride, so a
buffer recycled too early, or a band stored at another band's rows, decodes to a
wrong column naming the band it came from rather than to a plausible value;
the destination is seeded with
POISON, so a missed store or a missed finalwait is distinguishable from a misplaced element. Stated plainly in the case
docs: this cannot claim a ring built without the waits would fail here — a
dropped
wait_readis a race, and a race that does not happen leaves no trace.The case is built so a violation is a wrong value, not so that it is forced.
Also
build(clippy ×3, host tests,cargo doc --features host, realsm_100adevice builds of both kernel crates) andregcount, both green.