Skip to content

The dW epilogue never reads C: reduction TMA stores for the fp32 accumulate path (#80 remedy 1) - #82

Merged
feitreim merged 2 commits into
mainfrom
80-reduction-store-epilogue
Aug 4, 2026
Merged

feitreim merged 2 commits into
mainfrom
80-reduction-store-epilogue

Conversation

@feitreim

@feitreim feitreim commented Aug 4, 2026

Copy link
Copy Markdown
Owner

Remedy 1 from #80: the fp32 accumulate fold — the epilogue that read C back at +25–29% over its own store at K=6144 — is deleted. The accumulate path now reduce-stores: each warp scatters its [16, 64] fp32 half-band into the staging tile the bf16 drain already planned (byte-identical 4096 B, SHARED_BYTES unchanged) and cp.reduce.async.bulk.tensor.2d...add adds it into C at the copy engine. C is never read by a thread.

Ferro side: feitreim/ferro-kittens#187 (issue #42's sanctioned scoped fallback — add, rank 2 only; upstream admission filed as NVIDIA/cuda-rust#660). gpu/gemm and gpu/model pin that branch rev (969d7f4) until it reaches ferro main; the gpu/model bump is forced by its #[path] include of gemm's source, and the pin delta beyond #187 is only ferro #183 (scatter_tile) + docs. The ferro branch must not be deleted until swap to a main rev.

model_shapes, before (#80 baseline) → after (two runs, separate containers)

shape M×K×N step row mode before run 1 run 2
3072×6144×8192 bwd gate_up dW /expert f32 acc-T 0.666 0.886 0.884
4096×6144×3072 bwd down dW /expert f32 acc-T 0.588 0.777 0.773
3072×24576×9216 bwd qkv dW f32 acc-T 0.881 0.952 0.952
3072×24576×3072 bwd o_proj dW f32 acc-T 0.892 0.942 0.945
6144×3072×8192 gate_up fwd f32 store 0.715 0.799 0.783
6144×3072×4096 bwd down dx f32 store 0.735 0.806 0.828
24576×3072×9216 qkv fwd f32 store 0.771 0.810 0.754
6144×4096×3072 down fwd f32 store 0.781 0.868 0.872
24576×3072×3072 o_proj fwd f32 store 0.756 0.849 0.841
6144×8192×3072 bwd gate_up dx f32 store 0.925 0.961 0.960
24576×9216×3072 bwd qkv dx f32 store 0.929 0.974 0.962
24576×3072×50432 lm_head fwd bf16 store 0.806 0.844 0.825
24576×50432×3072 bwd lm_head dx bf16 store 0.920 0.947 0.894
3072×24576×50432 bwd lm_head dW bf16 acc-T 0.937 0.905 0.921

The decompose sweep is the diagnosis confirmed: the fold premium over store-T collapsed from +25–29% to +0.3…+9.8%, and on the worst row (bwd down dW) the accumulate is now cheaper than its own store (−1.1% / −2.2% across the runs). What remains below 0.90 on those rows is the store/walk baseline at shallow K — #80's remedy 2 (drain–MMA overlap), untouched here.

The f32 store rows improved without being targeted: removing the fold from the shared kernel body dropped gemm_tcgen05_f32_optimized from 120 regs / 512 B local frame to 102 / 0 (the frame was ferro #174's "band plus C beside it"). The new gemm_tcgen05_f32_accumulate is 80 regs / 0 — below even the bf16 drain. Both ratchets down in this PR.

The two bf16 rows are byte-identical kernels (82 regs before and after); their drift (0.937→0.905/0.921 on bf16 acc-T, with cuBLASLt itself −3–5% in the same containers) is cross-container variance, not this diff. bf16 reduce-store was checked before building and skipped: the row's gap is 0.3 ms/step.

Full-step A/B — same container, --baseline-ref da32b09

Base rev note: this branch (and its A/B baseline) is da32b09, the pre-#81 main; #81 (MoE routing fusion) merged during verification and merges cleanly with this diff (no overlapping hunks). The A/B below isolates this diff's delta.

baseline candidate Δ
full step 460.465 ms 443.190 ms −17.27 ms (−3.75%)
backward.experts.gate_up_weight_gemm 26.533 20.774 −5.76
backward.experts.down_weight_gemm 15.620 11.994 −3.63
backward.qkv_proj.weight_gemm 16.841 16.129 −0.71
backward.o_proj.weight_gemm 6.629 6.318 −0.31
backward.lm_head.weight_gemm (bf16, untouched) 4.769 4.775 +0.01
all dW rows combined −10.40
f32 store-mode GEMM rows combined −6.49

No profile row regresses (largest positive delta +0.006 ms). The −10.4 ms on dW rows lands in the predicted −10–15 ms band; the −6.5 ms on store rows is the register/frame windfall on top.

Numerics (SPEC decision #20)

  • The engine's .add on an fp32 tensor map is an fp32 round-to-nearest add — weight-gradient accumulation stays fp32 end to end, nothing rounded on the way through.
  • One CTA owns each output tile, one warp each band, one reduce per element per launch: per element the operation is exactly one old + new, the same shape as the register fold's — deterministic.
  • One caveat worth recording: red-op fp32 adders historically flush subnormal inputs/results to zero where an FADD would not. Weight-gradient magnitudes are nowhere near 1e-38, and the correctness gates (seeded nonzero C, tolerance and bitwise agreement checks) pass, but it is a semantic difference from the register fold in the last bit of the denormal range.
  • model_shapes' from-zero bitwise agreement check (store-T vs one acc-T launch) still passes: the reduce adds exact values to exact zeros.

Gates

  • gemm correctness (Modal B200): green — including the transposed f32 accumulate against a nonzero seeded C (check_tcgen05_bf16_transposed, uniform_vec-seeded initial) and the bf16 fold-into-nonzero check; a zero-seeded accumulate could not tell store-with-add from store, and none of the accumulate gates are zero-seeded.
  • model parity: green (block linears match CPU forward/backward, overfit + Muon overfit pass).
  • Square bench regression check: f32 accumulate 0.813/0.926/0.896 of cuBLASLt at 2048³/8192³/16384³, at or above the store mode's own ratios at depth; no store row regresses.
  • ferro device gate: tma reduce-add store — a seeded destination reduce-stored twice reads back seed + 2·value bit-exactly on a B200, which no overwrite can fake (51/51 cases green).
  • KernelBudget ratchets: bf16 82/0 (unchanged), f32 store 120/512 → 102/0, f32 accumulate pinned at 80/0.

Closes nothing yet: #80 stays open for remedy 2 (drain–MMA overlap), which now owns the residual on every sub-0.90 row.

… through the copy engine (#80 remedy 1)

The fold read C back at +25-29% over its own store at K=6144 and made the
two per-expert dW rows the worst in the table (0.666x/0.588x of cuBLASLt).
It is replaced by ferro-kittens #42's reduction TMA store: the accumulate
kernel's drain scatters each [16, 64] fp32 half-band into the staging tile
the bf16 drain already planned (byte-identical 4096 B, SHARED_BYTES
unchanged) and cp.reduce.async.bulk.tensor.add adds it into C in fp32 -
SPEC decision #20's precision, one engine-side add per element per launch,
one CTA per tile, so the element-wise order is as deterministic as the
register fold's was.

The split pays twice: the new gemm_tcgen05_f32_accumulate is 80 regs with
no local frame, and the store kernel drops 120/512 to 102/0 because it no
longer monomorphizes a C load beside the band - both ratchets down in the
same change. C reaches the kernel as a rank-2 fp32 tensor map (box [16,32])
built and cached host-side per (address, n, m); the f32_accumulate*
adapters keep their signatures, so gpu/model needs only the kittens pin
bump its #[path] include of this source forces (c648c67 -> 969d7f4, the
delta being ferro #183's scatter_tile and the #42 store itself).

Measured (B200, model_shapes vs cuBLASLt 13.0): the four f32 acc-T rows
0.666->0.886, 0.588->0.777, 0.881->0.952, 0.892->0.942, with the fold
premium collapsed to +0.3..+9.8% and bwd down dW's accumulate now cheaper
than its own store (-1.1%); every f32 store row improves too on the
102-reg kernel. Full tables in the PR.
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