Skip to content

The TMA store loses, and the CTA-wide ring is the better of the two (#123) - #123

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

feitreim merged 1 commit into
mainfrom
tma-store-epilogue

Conversation

@feitreim

@feitreim feitreim commented Jul 29, 2026 •

Copy link
Copy Markdown
Owner

#121 left one lever with a number on it: ld.shared + st.global at 2.0-2.3 us a
tile, 31-36% of the residual epilogue, ~+2.7% of the launch. This builds that change
two ways and measures it in four containers. It does not win anywhere the
instrument can see it, and the warp-scope form loses by 1.0-1.7%.

The full-depth A/B cannot see it, and #122 is why. At 8192^3 this comparison is a
few tenths of a percent between two 0.65 ms launches -- 1/share of 458-709, a
HARDER measurement than whole - no drain and not an easier one. Three containers of
it read +0.2, +0.4 and -0.2 percent. So it was re-taken with #122's substitution
applied to a ratio rather than to a subtraction: hold M and N, shorten K, interleave
the arms round-robin, four whole measurements each. staged84 over each arm, so
above 1.000 is the TMA arm ahead, session 1 / session 2:

shape 1/share s84/tma range s84/tmac range
8192^2 x k1024 61-191 0.9885 / 0.9835 0.9743 - 0.9952 0.9948 / 0.9980 0.9924 - 0.9987
16384^2 x k1024 85-427 0.9882 / 0.9899 0.9853 - 0.9913 0.9977 / 1.0061 0.9971 - 1.0069
8192^3 294-709 0.9978 / 1.0018 0.9969 - 1.0023 1.0014 / 1.0034 0.9984 - 1.0062

s84 tma is behind in four independent cells and not one range reaches 1.000.
s84 tmac is a tie to a small loss, and the two containers disagree in sign at
16384^2 with non-overlapping ranges -- reported rather than averaged away.

Even the doubling probe never promised 2.7%. s84t 2g doubles the TMA hop
exactly as s84 2g doubles ld.shared + st.global. At 8192^3 over four
containers: 2.33/2.41/2.56/2.58 us a tile for the vectorized stores against
1.57/1.90/1.91/1.99 for the engine. 2.7% was that term going to ZERO; the engine
takes it to about three quarters of itself, worth 0.42-0.75 us a tile.

And the realised value is below even that, for a reason the probe cannot see. A
DOUBLED store is an extra one left in flight; a REPLACING store is on the critical
path of the next band. At IN_FLIGHT = 0 the ring's acquire is
cp.async.bulk.wait_group.read at zero groups, so band k+1's stmatrix waits out
the engine's read of band k's buffer, four times an item, where store_shared_rows
retires into the memory pipeline and blocks on nothing. Depth 1 is not a choice: the
envelope leaves 1920 B and a second set of buffers is 16 384.

The block barrier was the obvious suspect and the hypothesis was backwards. The
census says the barriers are real -- bar.sync 2/5 and bar.warp.sync 3/0 against
staged84's 2/1 -- and the arm that pays them wins, by 0.6-1.6% in all four
paired cells. One [128,64] box a band beats four [32,64] boxes by more than eight
bar.sync an item cost.

So StoreRing was usable as built and is the faster of the two. What it could
not express was the layout, so it gained a Scope parameter (Cta, the default and
the shipped behaviour, and Warp): 40 lines, no byte moved, only which barrier
converge is and which thread issuing is. Every argument in the type survived
untouched, because none of them mentions how many threads are in the barrier. The
parameter earned its keep by producing the arm that lost.

One hardware fact fell out. gemm rounds its staging run to 128 bytes and not
1024, so every staging tile starts mid-swizzle-period. store ring warp phased is
the store side of swizzle roundtrip short and it passes: the engine reads the phase
off the absolute address in both directions. That is why no alignment padding was
needed and why both TMA rungs declare staged84's 114 816 B to the byte.

Named and not built: the loss is attributed to depth 1 by mechanism, and a depth-2
warp ring of [16,64] buffers is 2 x 2048 = 4096 B a warp -- exactly what a [32,64]
buffer costs today, so the envelope would not move. It needs a 16-row band out of
TmemTile::tile_x8, whose .x8 shape is 32.

New: Scope/Cta/Warp on StoreRing, Tile::drain_staged_tma,
Tile::drain_staged_tma_cta, Tile::cta_origin, three entry points (s84 tma,
s84 tmac, s84t 2g), a C tensor map on the host side, gemm::tma_ordering and two
bench --case residual tables, two device-test cases. No default, constant or
shipped kernel moved; staged84 still ships. Both TMA rungs are on the correctness
gate and pass the element-exact bf16 == at every size; the doubling probe says
UNCHECKED in its own label. build green four times, regcount (occupancy gate
green, 98/94/96 against staged84's 96, zero spill), device_tests 41 of 41.

feitreim added a commit that referenced this pull request Jul 29, 2026
…123)

#121 left one lever with a number on it: `ld.shared` + `st.global` at 2.0-2.3 us a
tile, 31-36% of the residual epilogue, ~+2.7% of the launch. This builds that change
two ways and measures it in four containers. **It does not win anywhere the
instrument can see it, and the warp-scope form loses by 1.0-1.7%.**

**The full-depth A/B cannot see it, and #122 is why.** At 8192^3 this comparison is a
few tenths of a percent between two 0.65 ms launches -- `1/share` of 458-709, a
HARDER measurement than `whole - no drain` and not an easier one. Three containers of
it read +0.2, +0.4 and -0.2 percent. So it was re-taken with #122's substitution
applied to a ratio rather than to a subtraction: hold M and N, shorten K, interleave
the arms round-robin, four whole measurements each. `staged84` over each arm, so
above 1.000 is the TMA arm ahead, `session 1 / session 2`:

| shape | 1/share | s84/tma | range | s84/tmac | range |
| --- | ---: | ---: | --- | ---: | --- |
| 8192^2 x k1024 | 61-191 | 0.9885 / 0.9835 | 0.9743 - 0.9952 | 0.9948 / 0.9980 | 0.9924 - 0.9987 |
| 16384^2 x k1024 | 85-427 | 0.9882 / 0.9899 | 0.9853 - 0.9913 | 0.9977 / 1.0061 | 0.9971 - 1.0069 |
| 8192^3 | 294-709 | 0.9978 / 1.0018 | 0.9969 - 1.0023 | 1.0014 / 1.0034 | 0.9984 - 1.0062 |

`s84 tma` is behind in four independent cells and not one range reaches 1.000.
`s84 tmac` is a tie to a small loss, and the two containers disagree in sign at
16384^2 with non-overlapping ranges -- reported rather than averaged away.

**Even the doubling probe never promised 2.7%.** `s84t 2g` doubles the TMA hop
exactly as `s84 2g` doubles `ld.shared` + `st.global`. At 8192^3 over four
containers: 2.33/2.41/2.56/2.58 us a tile for the vectorized stores against
1.57/1.90/1.91/1.99 for the engine. 2.7% was that term going to ZERO; the engine
takes it to about three quarters of itself, worth 0.42-0.75 us a tile.

**And the realised value is below even that, for a reason the probe cannot see.** A
DOUBLED store is an extra one left in flight; a REPLACING store is on the critical
path of the next band. At `IN_FLIGHT = 0` the ring's `acquire` is
`cp.async.bulk.wait_group.read` at zero groups, so band k+1's `stmatrix` waits out
the engine's read of band k's buffer, four times an item, where `store_shared_rows`
retires into the memory pipeline and blocks on nothing. Depth 1 is not a choice: the
envelope leaves 1920 B and a second set of buffers is 16 384.

**The block barrier was the obvious suspect and the hypothesis was backwards.** The
census says the barriers are real -- `bar.sync` 2/5 and `bar.warp.sync` 3/0 against
`staged84`'s 2/1 -- and **the arm that pays them wins**, by 0.6-1.6% in all four
paired cells. One [128,64] box a band beats four [32,64] boxes by more than eight
`bar.sync` an item cost.

**So `StoreRing` was usable as built and is the faster of the two.** What it could
not express was the layout, so it gained a `Scope` parameter (`Cta`, the default and
the shipped behaviour, and `Warp`): 40 lines, no byte moved, only which barrier
`converge` is and which thread `issuing` is. Every argument in the type survived
untouched, because none of them mentions how many threads are in the barrier. The
parameter earned its keep by producing the arm that lost.

**One hardware fact fell out.** `gemm` rounds its staging run to 128 bytes and not
1024, so every staging tile starts mid-swizzle-period. `store ring warp phased` is
the store side of `swizzle roundtrip short` and it passes: the engine reads the phase
off the absolute address in both directions. That is why no alignment padding was
needed and why both TMA rungs declare `staged84`'s 114 816 B to the byte.

Named and not built: the loss is attributed to depth 1 by mechanism, and a depth-2
warp ring of [16,64] buffers is 2 x 2048 = 4096 B a warp -- exactly what a [32,64]
buffer costs today, so the envelope would not move. It needs a 16-row band out of
`TmemTile::tile_x8`, whose `.x8` shape is 32.

New: `Scope`/`Cta`/`Warp` on `StoreRing`, `Tile::drain_staged_tma`,
`Tile::drain_staged_tma_cta`, `Tile::cta_origin`, three entry points (`s84 tma`,
`s84 tmac`, `s84t 2g`), a C tensor map on the host side, `gemm::tma_ordering` and two
`bench --case residual` tables, two device-test cases. No default, constant or
shipped kernel moved; `staged84` still ships. Both TMA rungs are on the correctness
gate and pass the element-exact bf16 `==` at every size; the doubling probe says
UNCHECKED in its own label. `build` green four times, `regcount` (occupancy gate
green, 98/94/96 against `staged84`'s 96, zero spill), `device_tests` 41 of 41.
@feitreim
feitreim force-pushed the tma-store-epilogue branch from b709dc9 to 18db47d Compare July 29, 2026 15:58
@feitreim feitreim changed the title The TMA store is a null at 8192 cubed, and the barrier was not the reason (#122) The TMA store loses, and the CTA-wide ring is the better of the two (#123) Jul 29, 2026
…123)

#121 left one lever with a number on it: `ld.shared` + `st.global` at 2.0-2.3 us a
tile, 31-36% of the residual epilogue, ~+2.7% of the launch. This builds that change
two ways and measures it in four containers. **It does not win anywhere the
instrument can see it, and the warp-scope form loses by 1.0-1.7%.**

**The full-depth A/B cannot see it, and #122 is why.** At 8192^3 this comparison is a
few tenths of a percent between two 0.65 ms launches -- `1/share` of 458-709, a
HARDER measurement than `whole - no drain` and not an easier one. Three containers of
it read +0.2, +0.4 and -0.2 percent. So it was re-taken with #122's substitution
applied to a ratio rather than to a subtraction: hold M and N, shorten K, interleave
the arms round-robin, four whole measurements each. `staged84` over each arm, so
above 1.000 is the TMA arm ahead, `session 1 / session 2`:

| shape | 1/share | s84/tma | range | s84/tmac | range |
| --- | ---: | ---: | --- | ---: | --- |
| 8192^2 x k1024 | 61-191 | 0.9885 / 0.9835 | 0.9743 - 0.9952 | 0.9948 / 0.9980 | 0.9924 - 0.9987 |
| 16384^2 x k1024 | 85-427 | 0.9882 / 0.9899 | 0.9853 - 0.9913 | 0.9977 / 1.0061 | 0.9971 - 1.0069 |
| 8192^3 | 294-709 | 0.9978 / 1.0018 | 0.9969 - 1.0023 | 1.0014 / 1.0034 | 0.9984 - 1.0062 |

`s84 tma` is behind in four independent cells and not one range reaches 1.000.
`s84 tmac` is a tie to a small loss, and the two containers disagree in sign at
16384^2 with non-overlapping ranges -- reported rather than averaged away.

**Even the doubling probe never promised 2.7%.** `s84t 2g` doubles the TMA hop
exactly as `s84 2g` doubles `ld.shared` + `st.global`. At 8192^3 over four
containers: 2.33/2.41/2.56/2.58 us a tile for the vectorized stores against
1.57/1.90/1.91/1.99 for the engine. 2.7% was that term going to ZERO; the engine
takes it to about three quarters of itself, worth 0.42-0.75 us a tile.

**And the realised value is below even that, for a reason the probe cannot see.** A
DOUBLED store is an extra one left in flight; a REPLACING store is on the critical
path of the next band. At `IN_FLIGHT = 0` the ring's `acquire` is
`cp.async.bulk.wait_group.read` at zero groups, so band k+1's `stmatrix` waits out
the engine's read of band k's buffer, four times an item, where `store_shared_rows`
retires into the memory pipeline and blocks on nothing. Depth 1 is not a choice: the
envelope leaves 1920 B and a second set of buffers is 16 384.

**The block barrier was the obvious suspect and the hypothesis was backwards.** The
census says the barriers are real -- `bar.sync` 2/5 and `bar.warp.sync` 3/0 against
`staged84`'s 2/1 -- and **the arm that pays them wins**, by 0.6-1.6% in all four
paired cells. One [128,64] box a band beats four [32,64] boxes by more than eight
`bar.sync` an item cost.

**So `StoreRing` was usable as built and is the faster of the two.** What it could
not express was the layout, so it gained a `Scope` parameter (`Cta`, the default and
the shipped behaviour, and `Warp`): 40 lines, no byte moved, only which barrier
`converge` is and which thread `issuing` is. Every argument in the type survived
untouched, because none of them mentions how many threads are in the barrier. The
parameter earned its keep by producing the arm that lost.

**One hardware fact fell out.** `gemm` rounds its staging run to 128 bytes and not
1024, so every staging tile starts mid-swizzle-period. `store ring warp phased` is
the store side of `swizzle roundtrip short` and it passes: the engine reads the phase
off the absolute address in both directions. That is why no alignment padding was
needed and why both TMA rungs declare `staged84`'s 114 816 B to the byte.

Named and not built: the loss is attributed to depth 1 by mechanism, and a depth-2
warp ring of [16,64] buffers is 2 x 2048 = 4096 B a warp -- exactly what a [32,64]
buffer costs today, so the envelope would not move. It needs a 16-row band out of
`TmemTile::tile_x8`, whose `.x8` shape is 32.

New: `Scope`/`Cta`/`Warp` on `StoreRing`, `Tile::drain_staged_tma`,
`Tile::drain_staged_tma_cta`, `Tile::cta_origin`, three entry points (`s84 tma`,
`s84 tmac`, `s84t 2g`), a C tensor map on the host side, `gemm::tma_ordering` and two
`bench --case residual` tables, two device-test cases. No default, constant or
shipped kernel moved; `staged84` still ships. Both TMA rungs are on the correctness
gate and pass the element-exact bf16 `==` at every size; the doubling probe says
UNCHECKED in its own label. `build` green four times, `regcount` (occupancy gate
green, 98/94/96 against `staged84`'s 96, zero spill), `device_tests` 41 of 41.
@feitreim
feitreim force-pushed the tma-store-epilogue branch from 18db47d to da7e6d6 Compare July 29, 2026 16:06
@feitreim
feitreim merged commit 81c90da into main Jul 29, 2026
4 checks passed
feitreim added a commit that referenced this pull request Aug 6, 2026
…the loop (#210)

* The staged drain is one library call, and three kernels stop writing 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

* The band index moves both origins once, outside the walk

* The claim the doc makes about regcount is the one regcount made
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.

1 participant