Skip to content

The item stream has no boundary, and the drain lifts half-rows behind one wait (#80) - #84

Merged
feitreim merged 13 commits into
mainfrom
80-continuous-pipeline
Aug 5, 2026
Merged

feitreim merged 13 commits into
mainfrom
80-continuous-pipeline

Conversation

@feitreim

@feitreim feitreim commented Aug 5, 2026 •

Copy link
Copy Markdown
Owner

#80's residual was never the epilogue, the tile, the pipeline fill or the feed. It was a fixed per-item cost that cuBLASLt does not pay at all, and this PR deletes most of it. Thirteen of fourteen model rows improve and ten of twelve square-bench cells do; the ≥ 0.90 bar is still not met on six rows, and the residual is now accounted for down to two named terms.

The forensics that redirected the work

Two instruments, both preserved, reported in full at #80.

A depth sweep at fixed output geometry — bin/budget.rs, kept in this PR. Hold the tile count, the wave quantization and the grouped walk still, sweep K, and fit per-item time = fixed + per_block · K/64 for both sides. At the qkv geometry on main:

fixed per item per K block
main +6.32 µs 0.6264 µs
cuBLASLt −1.61 µs 0.6846 µs

Our per-K-block cost was already 8.5% below cuBLASLt's. The whole deficit was the intercept — which is why the ratio walked 0.69 → 0.84 → 1.01 → 1.11 as K deepened, and why at K = 12288 main already beat cuBLASLt by 11%.

A per-phase clock64 probe — branch 80-phase-probe, deliberately not in the shipped module — then split that intercept. Per item at K = 3072, in SM ticks:

phase main what it is
PRE 3 090 pipeline::run's mbarrier re-arm and its opening cluster_sync
ACC 9 003 the issuer waiting the previous drain to release the accumulator
MMA 48 066 the multiply — 1001.4 ticks a K block, and 1001.5 at K = 9216
— FILL 160 — the issuer waiting stage 0: the pipeline fill
POST 3 231 the tcgen05 fence and the closing cluster_sync

Three candidates died on those numbers. The fill is 0.25% of an item. The feed steady state is 2182 TFLOP/s aggregate against the B200's 2250 dense bf16, and ahead of cuBLASLt's own depth slope. And PRE + POST + ACC is K-independent — 15 324 ticks at K = 3072 against 14 905 at K = 9216 — which closes the arithmetic exactly: 48·1001 / (48·1001 + 15 100) = 0.761, and the measured 1660 TFLOP/s over the 2182 steady state is 0.761.

What changed

1. The item stream has no boundary. pipeline::run re-arms all eight mbarriers on one thread and takes two cluster_syncs per item, and by construction nothing may cross it — the producer cannot issue item i+1's first TMA until item i's barriers have been invalidated and re-initialized behind two cluster barriers. So the barriers are armed once per launch, the operand ring is indexed by the global K block, and the item stream is two semaphore rings deep: full says an item's MMA chain is complete, empty says its accumulator has been read. Each of the three roles walks the static strided schedule for itself (Walk), which is why — unlike gemm_sol, whose CLC queue hands tiles out — this needs no item mailbox at all. Nothing inside the loop is cluster-wide, and the producer crosses an item boundary with STAGES blocks still in flight.

ITEMS = 2 is provable rather than a margin: the MMA of item i waits empty(i), which a drain arrives at only after full(i − 1), so the issuer is never more than one item ahead of the epilogue and a slot's previous user is two items back.

2. The drain lifts half-rows behind one wait. The probe's second finding was that ACC ≈ DRAIN — 9 902 against 9 977 — so #83's release was deferring nothing. The reason is not store issue: moving store passes behind the release moved the number by 1%. What costs is the wait per .x8 issue. TmemTile::tile_x8 takes (M/16)·(N/64) of them serially, so the four [32, STAGE_N] bands this used to lift were eight exposed TMEM latencies an item. tile_x8_batched puts ISSUE_LIMIT issues in flight behind one wait, and a [16, BLOCK_N] half-row is exactly that and exactly 128 registers — so the band leaves tensor memory in two waits, and the release lands with half the stores owed rather than a quarter. The bf16 drain takes the same treatment at its [32, STAGE_N] band (two issues, one wait) and the reduction drain at three half-bands; a fourth measured 177 registers, which is the 2 → 1 CTA cliff.

Budgets ratcheted to measured, zero spill throughout: bf16 137 → 133, f32 store 118 → 168, accumulate 127 → 145. The f32 store count is the ceiling twelve warps an SM leave a thread, and it is the held half-row rather than a leak: 168 × 192 × 2 = 64 512 still fits an SM's 65 536, and the f32 rows rose — a residency step down to 1 CTA/SM shows up as 0.46–0.53, which is how #83 caught the two-band hoist.

Measured

Same-harness depth sweep at the qkv geometry, one container each (bin/budget.rs):

fixed per item per K block ratio K=3072 K=9216 K=12288
main 5.04 µs 0.6511 µs 0.838 1.012 1.109
boundary removed 3.43 µs 0.6458 µs 0.881 1.057 1.057
this PR 2.75 µs 0.6570 µs 0.890 — —

Per-item ticks at K = 3072 fell 63 445 → 57 926, and the item is now ACC + MMA with nothing else in it.

model_shapes, --features cublas, B200

Two independent sessions of this branch (C1 / C2), against #83's two post-merge sessions:

row mode main (A / B) this PR (C1 / C2)
qkv fwd 24576×3072×9216 f32 store 0.855 / 0.831 0.887 / 0.893
o_proj fwd 24576×3072×3072 f32 store 0.863 / 0.860 0.895 / 0.900
gate_up fwd 6144×3072×8192 f32 store 0.813 / 0.827 0.873 / 0.869
down fwd 6144×4096×3072 f32 store 0.887 / 0.883 0.905 / 0.894
bwd qkv dx 24576×9216×3072 f32 store 0.966 / 0.963 0.984 / 0.975
bwd gate_up dx 6144×8192×3072 f32 store 0.965 / 0.955 0.968 / 0.968
bwd down dx 6144×3072×4096 f32 store 0.835 / 0.837 0.887 / 0.871
bwd qkv dW 3072×24576×9216 f32 acc-T 0.942 / 0.948 0.952 / 0.940
bwd o_proj dW 3072×24576×3072 f32 acc-T 0.952 / 0.955 0.948 / 0.947
bwd gate_up dW 3072×6144×8192 f32 acc-T 0.886 / 0.893 0.901 / 0.906
bwd down dW 4096×6144×3072 f32 acc-T 0.790 / 0.796 0.789 / 0.791
lm_head fwd 24576×3072×50432 bf16 store 0.868 / 0.841 0.897 / 0.894
bwd lm_head dx 24576×50432×3072 bf16 store 0.919 / 0.830 0.920 / 0.901
bwd lm_head dW 3072×24576×50432 bf16 acc-T 0.959 / 0.948 0.919 / 0.958

Thirteen of fourteen rows improve on the session mean; bwd down dW is flat and is the one row whose deficit is wave quantization rather than the item stream. Six rows still sit under 0.90, four of them by 1–3 points — the accounting for that is below, and #80 does not close on this.

Square bench

shape bf16 store bf16 acc f32 store f32 acc
4096³ 0.874 → 0.903 0.686 → 0.699 0.846 → 0.885 0.816 → 0.848
8192³ 0.964 → 0.973 0.887 → 0.909 0.865 → 0.954 0.849 → 0.905
16384³ 0.950 → 0.986 0.903 → 0.952 0.909 → 0.872 0.855 → 0.850

The main column is #83's post-merge table, so these are cross-container comparisons and the small cells should be read loosely. The one cell that moves the wrong way is 16384³ f32 store, which is the same cell #83 recorded going the wrong way for the same structural reason: at a 1 GiB C the deferred writeback overlaps the next item's operand streaming and pays an L2 term. The model has no such shape, and every square cell at 8192³ and below improves — f32 store by +8.9 points.

Full-step A/B, same container, both build orders

--baseline-ref runs the baseline first, so the second binary always pays the container's warm-up. Running it both ways cancels that, and the two orders agree on the size of it to within 0.02 ms:

order 1 (main first) order 2 (this PR first) mean
full step 417.932 → 419.714 (+1.78) 392.356 → 400.522 (−8.17) −3.19 ms
all fifteen GEMM rows −1.414 −4.556 −2.99 ms

The implied second-position penalty is 4.97 ms from both orders independently, which is why order 1 alone reads as a regression: it is 1.78 = −3.19 + 4.97. The mean full-step delta (−3.19) and the mean GEMM-row delta (−2.99) agree to 0.2 ms, which is the check that matters — only GEMM rows can move, and they account for the whole step delta.

Per row, mean of the two orders:

kernel Δ
forward.experts.gate_up_gemm −0.795
backward.experts.down_input_gemm −0.624
forward.qkv_proj.gemm −0.486
backward.experts.gate_up_weight_gemm −0.399
backward.experts.down_weight_gemm −0.233
backward.o_proj.input_gemm −0.180
forward.experts.down_gemm −0.175
backward.experts.gate_up_input_gemm −0.104
forward.o_proj.gemm −0.086
backward.qkv_proj.input_gemm, .weight_gemm, o_proj.weight_gemm −0.071, −0.063, −0.049
backward.lm_head.weight_gemm +0.049
forward.lm_head.gemm +0.096
backward.lm_head.input_gemm +0.135

The three lm-head rows are the ones that do not move: they are N = 50432 at 128 waves, where the per-item term this PR deletes was already amortized to nothing.

What is left, and what it would take

ACC is now 8 310 ticks — 14% of a K = 3072 item — and it is the drain, entire: one batched tcgen05.ld of the top half-row, one store pass, one batched load of the bottom half-row. Its floor is set by two facts that are both about the register file. The accumulator is 256 fp32 a lane, so no drain can hold a whole band and release before storing any of it; and at 192 threads and 2 CTAs/SM a thread gets 168 registers, of which 128 are already the half-row. The remaining rows below 0.90 are that term plus, on the two smallest tile counts, wave quantization: gate_up fwd runs 768 tiles at 86.5% last-wave efficiency against cuBLASLt's 94.4%, and bwd down dW runs 192 tiles at 64.9% against 86.4% — on that row the wave term alone is larger than the whole deficit.

Two structural exits, both named rather than tried here:

  • A per-column-half release. Splitting the pair MMA into two M256_N128 chains over column sub-ranges of the same accumulator would let the next item's first chain start after half the drain. It costs no tensor memory — which is what closed the double-buffer routes — but it re-labels which rank stages which 64 columns of B, so the tensor map, the operand walk and the drain's column mapping all move together.
  • Re-pricing the narrow tile. Remedy 3 measured [256, 128] losing on all fourteen rows, but it measured it while paying the item boundary twice as often, and both of the terms above are things a narrower tile improves. The dispatch machinery is preserved on 80-narrow-tile; what it was compared against no longer exists.

Gates

  1. --kernel gemm parity: green, three runs. Budgets pinned at the measured 133 / 168 / 145, zero spill on all three.
  2. --kernel ops: green.
  3. --kernel model: green on rerun. The first run took the known stochastic miss of the MoE overfit assertion at 0.0597 against the 0.05 bound — the same value The drain runs beside the next item's MMA, not in front of it (#80 remedy 2) #83 recorded — with every numerical parity row green above it; the rerun landed 0.000046. That assertion is nondeterministic through the MoE scatter atomics, and this kernel's math is bit-identical to main's.
  4. model_shapes: table above, two sessions.
  5. Square bench: table above.
  6. Full-step A/B: both orders, above.

@feitreim
feitreim merged commit 30ca522 into main Aug 5, 2026
@feitreim
feitreim deleted the 80-continuous-pipeline branch August 5, 2026 04:30
@feitreim

feitreim commented Aug 5, 2026

Copy link
Copy Markdown
Owner Author

Correction: the full-step A/B numbers in the description were the warm-up runs

compare_profile runs four profiles, not two. It builds-and-runs each binary once (cargo oxide run --bin profile, lines 320–321 of modal_app.py) and then runs both prebuilt binaries again behind === baseline <ref> === and === candidate === markers — "profile both back-to-back in one container after each binary's equivalent warmups", as its docstring says. The last two tables are the comparison. I parsed the first two.

Everything I wrote about the full step — the +1.78 ms, the "second-position penalty of 4.97 ms", the both-orders mean of −3.19 ms — came from the warm-up pair and should be struck. The measured pair says something simpler:

order 1, measured runs full step
baseline (main @ fd0334a) 418.9825 ms
candidate (this PR) 411.8670 ms
Δ −7.12 ms (−1.70%)

GEMM rows on the same pair, −2.911 ms combined:

kernel main this PR Δ
forward.experts.gate_up_gemm 22.3357 21.1663 −1.169
backward.experts.down_input_gemm 12.4781 11.8783 −0.600
forward.qkv_proj.gemm 13.5221 12.9690 −0.553
forward.experts.down_gemm 10.5936 10.3146 −0.279
backward.experts.down_weight_gemm 13.2238 12.9719 −0.252
forward.o_proj.gemm 5.6875 5.4374 −0.250
backward.experts.gate_up_weight_gemm 22.3643 22.2036 −0.161
backward.o_proj.input_gemm 4.2524 4.1622 −0.090
backward.qkv_proj.weight_gemm / .input_gemm +0.012 / +0.016
backward.o_proj.weight_gemm +0.019
forward.lm_head.gemm 5.1651 5.1926 +0.028
backward.experts.gate_up_input_gemm 19.6684 19.7664 +0.098
backward.lm_head.weight_gemm 5.1443 5.2508 +0.107
backward.lm_head.input_gemm 5.2951 5.4609 +0.166

The three lm-head rows are still the ones that do not move, for the reason given in the description: at N = 50432 and 128 waves the per-item term this PR deletes was already amortized away.

Note the step gains more than the GEMM rows do (−7.12 against −2.91). I have no measurement that attributes the extra −4.2 ms, and it is larger than I would claim from this diff — the honest reading is that a single measured pair still carries container noise in the untouched rows, and the GEMM-row sum is the defensible figure. The reverse-order container has reported its measured baseline (this branch) at 391.0024 ms and is still running its measured candidate; I will add it when it lands.

The direction is not in question in either order or in either pair of runs, and the other gates in the description are unaffected — they were read correctly. Sorry for the noise.

@feitreim

feitreim commented Aug 5, 2026

Copy link
Copy Markdown
Owner Author

Both build orders, measured pairs — the full-step gate closed

The reverse-order container finished. Reading only compare_profile's measured pair in each (the two runs behind its === baseline === / === candidate === markers, after each binary's warm-up):

baseline candidate full step Δ GEMM rows Δ
order 1 — main baseline 418.9825 411.8670 −7.115 (−1.70%) −2.911
order 2 — this PR baseline 391.0024 400.8666 −9.864 (−2.46%) −4.958
mean −8.49 ms −3.93 ms

Both orders agree in direction and roughly in size, from two independent containers whose absolute levels differ by 7% — which is the cross-container spread the both-orders rule exists to see through.

Per GEMM row, mean of the two measured pairs:

kernel Δ
forward.experts.gate_up_gemm −1.057
backward.experts.down_input_gemm −0.734
forward.qkv_proj.gemm −0.560
backward.experts.gate_up_weight_gemm −0.417
backward.experts.down_weight_gemm −0.405
forward.experts.down_gemm −0.251
forward.o_proj.gemm −0.202
backward.qkv_proj.weight_gemm −0.167
backward.o_proj.input_gemm −0.158
backward.o_proj.weight_gemm −0.094
backward.experts.gate_up_input_gemm −0.037
backward.qkv_proj.input_gemm −0.021
forward.lm_head.gemm −0.001
backward.lm_head.weight_gemm +0.035
backward.lm_head.input_gemm +0.135

The ordering matches model_shapes exactly: the per-expert K = 3072/4096 rows move most, the lm-head rows at N = 50432 and 128 waves do not move at all, because the per-item term this PR deletes was already amortized away there.

One thing I am not claiming. In both containers the step gains about twice what the GEMM rows do (−7.1 against −2.9, −9.9 against −5.0). That reproduces, so it is probably not pure noise, but this diff can only touch GEMM kernels and I have no measurement that attributes the remainder — the plausible story (a kernel that spends less time in barrier spin loops leaves more thermal headroom for its neighbours) is a guess, not a result. −3.93 ms on the GEMM rows is the figure I would defend; the −8.49 ms step figure is what the harness reported and should be read with that caveat.

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