Skip to content

The epilogue's shared half, as a ring with the fences in it (#15) - #111

Merged
feitreim merged 1 commit into
mainfrom
store-ring
Jul 29, 2026
Merged

feitreim merged 1 commit into
mainfrom
store-ring

Conversation

@feitreim

Copy link
Copy Markdown
Owner

softmax ends with store_tile → fence.proxy.async.shared::cta →
bar.sync → tma_store → commit → wait::<0>, written out by hand, and it
is the only kernel in the repo that has ever run that chain. StoreRing is
that chain as a library type, generalised in the one direction the epilogue of
a kernel with no room for its output tile actually needs: DEPTH staging
buffers instead of one
, so band k + 1's stmatrix need not wait out band
k's store.

This is the primitive only. Nothing in examples/ is wired to it — that is
the 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 + 1
    swizzled staging tiles behind a cursor, with acquire / commit /
    commit_2d / drain. ~130 lines of code; the rest is the fence and wait
    discipline written down.
  • device-tests — four cases, store ring depth 1 / 2 / 4 / 2 wide, all
    passing on a B200 (36 of 36 in the harness).
  • regcount: gemm_cg2 unchanged at 166 regs / 0 spill, headroom 89 to its
    step. Nothing in examples/ was touched.
let mut ring = StoreRing::<Bf16, 128, 64, Swizzle128B, 1>::attach(smem);
for band in 0..bands {
    let staging = ring.acquire();                 // wait_read::<1>, then bar.sync
    store_tile(staging.chunk_writer(), row, 0, lane, drained);
    ring.commit(c_map, row_of(band), plane);      // fence, bar.sync, tma_store, commit
}
ring.drain();                                     // wait::<0>, then bar.sync

A separate type, not an extension of SharedTileRing

SharedTileRing is addressing and nothing else — N tiles and index % N —
and its completion mechanism is supplied separately, by a SemaphoreRing the
caller 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_threads into a type that today has no
thread identity in it at all.

There is a mechanical half to the same answer. The wait's group count is
DEPTH - 1, and tma_store_wait_read::<{DEPTH - 1}>() is not something Rust
will evaluate in generic-argument position without generic_const_exprs — the
restriction Element::Unpacked and MmaElement::mma already dodge, for the
same reason. So the parameter is IN_FLIGHT, the number the instruction
takes, and DEPTH = IN_FLIGHT + 1 is derived. A type parameterised that way
could not embed a SharedTileRing<.., { IN_FLIGHT + 1 }> even if the semantics
had wanted it to.

IN_FLIGHT = 0 is the single-buffer ring, and is softmax'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.
stmatrix writes through the generic proxy and the TMA engine reads through
the async one. The fence orders the writes of the thread that executes it,
so every thread that wrote a row fences, and the bar.sync after it is what
carries that ordering to the one thread that issues the store. A bar.sync
alone 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 that
order.

cp.async.bulk.wait_group.read, before a buffer is written again. Recycling
a 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 DEPTH groups committed, leaving at
most IN_FLIGHT with 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.sync after it is what releases the other warps past a wait they did
not take. Both live inside acquire. Below DEPTH committed bands the wait is
trivially 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 bulk
store visible to a following kernel or to the host — not the end of the kernel,
not a bar.sync, not dropping the handle. That is drain, and it is the one
obligation the ring cannot discharge incrementally.

Two bar.sync per band, which at depth 1 is one more than softmax pays by
hand (the acquire barrier, which softmax does 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 census
run ([256,256] s3 (gemm), 98392 B, holding 2). Worth stating explicitly: that
2 is set by tensor memory — BLOCK_N = 256 is 256 accumulator columns and
512 / 256 = 2 — not by shared memory, which is why the plan can grow all the
way to 116736 B before anything moves.

What a buffer costs, from StoreRing::BYTES at the shapes a GEMM epilogue
would want (all asserted in the unit tests):

staging shape depth 1 depth 2 depth 3
[128, 128] bf16 — a whole DRAIN_N band 32768 65536 98304
[128, 64] bf16 16384 32768 49152

Against 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_OK wants a whole 64-column swizzle subtile, so 64 columns
is 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 (a
register-count change, and regcount is what will say).

At [128, 64] a CTA's 128 × 256 bf16 output is four round trips an item,
and at depth 1 those four are fully serialized — acquire is wait_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 - lcf store column at
8192³, 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 stmatrix is strictly the
issue-count claim.

But STAGES 3→2 is not unmeasured, and it is not available. The brief calls
it "the available buyer … has never been measured". #87's rung table in
examples/README.md §7 has it, at this exact tile:

rung shared B CTA/SM 8192³ 16384³
[256,256] s3 (shipped) 98392 2 0.7546 ms 5.4214 ms
[256,256] s2 65608 2 0.8555 ms 5.8492 ms

Dropping 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 to
STAGES, no change to residency.
Its whole value is stmatrix's issue count
against 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 = 2 is:
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 softmax be 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:

  1. softmax's tile is both the TMA load destination and the store staging
    buffer. The ring can serve that — StoreRing::buffer(0) hands out the
    SharedTile to load into — but buffer is deliberately documented as
    carrying none of the ring's discipline, so this is the caller stepping
    outside the type's story rather than the type covering the case.
  2. It costs two extra bar.sync on a bandwidth-bound kernel: acquire's (which
    softmax does not need, storing once) and drain'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_tests on a B200, 36 of 36:

pass  store ring depth 1        8 bands of [64, 64] through 8192 B of ring, 32768 elements placed exactly
pass  store ring depth 2        8 bands of [64, 64] through 16384 B of ring, 32768 elements placed exactly
pass  store ring depth 4        8 bands of [64, 64] through 32768 B of ring, 32768 elements placed exactly
pass  store ring depth 2 wide   8 bands of [64, 128] through 32768 B of ring, 65536 elements placed exactly

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 acquire to wait on. Four warps write each buffer and one
thread issues each store, which is the arrangement the collective halves exist
for. The registers are filled from RegTile::coordinate and never loaded, so no
TMA 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 final
wait 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_read is 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, real
sm_100a device builds of both kernel crates) and regcount, both green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gpu-ci Run the B200 device-test tier on this PR (costs GPU minutes)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant