Skip to content

The drain runs beside the next item's MMA, not in front of it (#80 remedy 2) - #83

Merged
feitreim merged 6 commits into
mainfrom
80-deferred-drain
Aug 4, 2026
Merged

feitreim merged 6 commits into
mainfrom
80-deferred-drain

Conversation

@feitreim

@feitreim feitreim commented Aug 4, 2026

Copy link
Copy Markdown
Owner

#80 remedy 2: the drain no longer runs in front of the next item's MMA — it runs beside it.

What changed

One structural change to pipeline::run's job, in the form kittens::pipeline::Job's doc already sanctions: the epilogue is deferred one item. The band warps open item i by draining item i−1's accumulator, hand the accumulator's columns back through a new cluster barrier (acc_free, living in the staging tiles' alignment padding — the shared plan does not move) the moment their last tcgen05.ld retires, and close the item waiting out its done — so every barrier still completes inside the item that armed it. What runs beside the MMA is the previous drain's store tail plus the whole next TMA fill; what remains serial is the load half of the drain, which a single-buffered accumulator cannot give away.

Supporting changes:

  • Two role warps (THREADS 128 → 192): the producer and the MMA issuer each get a warp. The first cut ran them as two diverged lanes of one warp and every model_shapes row lost 5–20% — divergent spin loops share a warp's issue, and the MMA chain stalled behind the producer's polling. The extraction-point kernel and ferro's gemm_ws both kept these roles on their own warps; now this kernel does too.
  • Drains hoist loads one pass ahead of stores (bf16 and the reduce drain), which is what makes the release early: the overlap a drain buys is exactly the registers it holds past its last load, over the band's 256. The fp32 store drain keeps one band (its two-band form measured 181 registers — past the ~170 the register file grants 12 warps an SM — and paid the 2→1 CTA cliff: f32 rows at 0.46–0.53 of cuBLASLt).
  • Budgets ratcheted to measured: bf16 82 → 137, f32 store 102 → 118, accumulate 80 → 127, all zero spill. The increase is the held bands — it is the mechanism, not a leak — and residency stays tensor-memory-bound at 2 CTAs/SM.

What was tried and lost (all preserved)

  • 1 CTA/SM double-buffered TMEM (gemm_ws): ferro-kittens#188/#189 measured it at the shallow-K money shapes for the first time — its epilogue-free floor loses to the shipped 2-CTA kernel with its exposed epilogue at every K=3072 shape (0.822/0.880/0.884 vs 0.828/0.896/0.909). Route closed in both regimes.
  • Anti-phase stagger at 2 CTAs/SM (branch 80-stagger-probe): +2.8% best at 4 µs, monotonically worse beyond — in-phase is an attractor; a one-time nudge relapses within an item.
  • fp32 store through the copy engine (reverted in-branch, 9b4d12f): Reduce's drain with the add turned off measured a wash to −3% on its own rows — seven of eight engine round-trips sit before the release, so the engine's latency lands on the critical path where the register stores' issue did not. ferro The GEMM entry points declare their block, so the driver stops deriving it (#122) #123's finding survives deferral.

Measured (model_shapes, same-container cuBLASLt, B200)

Two independent sessions of the identical final kernel (run A / run B) against the post-#82 baseline rows, because the big bf16 rows swing several points between containers and one session would over-claim:

row mode post-#82 baseline this PR (A / B)
qkv fwd 24576×3072×9216 f32 store 0.75–0.81 0.855 / 0.831
o_proj fwd 24576×3072×3072 f32 store ~0.85 0.863 / 0.860
gate_up fwd 6144×3072×8192 f32 store ~0.80 0.813 / 0.827
down fwd 6144×4096×3072 f32 store ~0.87 0.887 / 0.883
bwd qkv dx (K=9216) f32 store 0.92–0.97 0.966 / 0.963
bwd gate_up dx (K=8192) f32 store 0.92–0.97 0.965 / 0.955
bwd down dx 6144×3072×4096 f32 store 0.81–0.83 0.835 / 0.837
bwd qkv dW f32 acc-T 0.952 0.942 / 0.948
bwd o_proj dW f32 acc-T 0.944 0.952 / 0.955
bwd gate_up dW f32 acc-T 0.886 0.886 / 0.893
bwd down dW f32 acc-T 0.777 0.790 / 0.796
lm_head fwd 24576×3072×50432 bf16 store 0.83–0.84 0.868 / 0.841
bwd lm_head dx bf16 store 0.920 0.919 / 0.830*
bwd lm_head dW bf16 acc-T 0.937 0.959 / 0.948

* run B's lm_head dx is a session outlier (ours 4.45 → 5.02 ms on a byte-identical binary while cuBLASLt moved 2%); run A matches the baseline.

Square bench, branch vs main @ bad698a, same-day separate containers:

shape bf16 store bf16 acc f32 store f32 acc
4096³ 0.884→0.874 0.637→0.686 0.857→0.846 0.821→0.816
8192³ 0.964→0.964 0.784→0.887 0.866→0.865 0.863→0.849
16384³ 0.990→0.950 0.881→0.903 0.938→0.909 0.845→0.855

The accumulate columns are the deferral working exactly as designed — the fold's long tail is what overlaps best. The 16384³ store cells are a real ~2–3% ms regression (5.06→5.20 bf16, 5.34→5.44 f32, reproduced in two branch containers): a measured tradeoff, not noise. Working hypothesis: at the 1 GiB operand footprint the deferred C writeback overlaps the next item's operand streaming and pays an L2 term (#114 saw the same term at K=32768). The model's own deep-K rows (K=8192/9216) improved (0.92–0.93 → 0.955–0.967), so the training step is on the right side of this trade everywhere.

Residual attribution

  • Wave quantization, not drain: gate_up fwd (86.5% last-wave efficiency at the [256,256] pair tile), bwd down dx (86.5%), gate_up dW (86.5%), down dW (64.9% — 192 tiles over 148 clusters is 1.3 waves). These are remedy 3's rows; no epilogue work moves them.
  • The serial load half of the drain: a single-buffered accumulator cannot release before its last tcgen05.ld, and the register file caps how many loads can run ahead of the stores (the overlap fraction is held-registers / 256). That is the remaining epilogue term on qkv fwd / o_proj fwd / down fwd / lm_head fwd, and the honest ceiling of remedy 2 at this tile.

Gates

  • --kernel gemm parity: green (three runs), budgets pinned at the measured 137 / 118 / 127, zero spill.
  • --kernel ops: green. --kernel model: green (one stochastic miss of the MoE overfit assertion at 0.0597 vs the 0.05 bound with every numerical parity row green, then 0.000099 on rerun — the assertion is nondeterministic through the MoE scatter atomics, not a numerics change; this kernel's math is bit-identical to main's).
  • Square bench: table above; 16384³ store cells are the stated tradeoff.
  • Full-step --baseline-ref bad698a A/B, same container, both build orders: 402.2150 → 399.5803 ms and 404.4007 → 401.6943 ms (−2.6/−2.7 ms, consistent).

(Opened by the session orchestrator from the completing agent's drafted body + on-disk gate logs; the agent's session ended after the A/B completed.)

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