Skip to content

FA4 phase 4: tcgen05 attention backward (dQ, dK/dV) + model integration (#35) - #40

Merged
feitreim merged 3 commits into
mainfrom
feat/issue-35-fa4-bwd-phase4
Jul 23, 2026
Merged

feitreim merged 3 commits into
mainfrom
feat/issue-35-fa4-bwd-phase4

Conversation

@feitreim

Copy link
Copy Markdown
Owner

Phase 4 of #35: the attention backward rebuilt around Blackwell's tensor cores, on the proven phase 1–3 forward idioms. Two synchronous tcgen05 gradient kernels join flash.ptx and take over the model's tile-aligned backward; the fp32 tiled kernels stay as the non-aligned fallback and the parity oracle.

What landed

Two kernels in gpu/flash-attn/src/tcgen05.rs (sync form, sharing the forward's swizzle-aware bf16 fragment writes, transposed-B O-MMA shape, and fp32 TMEM accumulation):

  • flash_backward_q_tcgen05 (query-parallel, grid (T/128,H,B)): Q/dY resident, streams causal key tiles. Recomputes S = Q·Kᵀ and dP = dY·Vᵀ into fp32 TMEM, forms the true dS = P·(dP − D)·scale in registers, accumulates dQ += dS·K in a TMEM segment.
  • flash_backward_kv_tcgen05 (key-parallel): K/V resident, streams causal query tiles. Recomputes the transposed Sᵀ = K·Qᵀ and dPᵀ = V·dYᵀ, accumulates dV += Pᵀ·dY and dK += dSᵀ·Q.

Probabilities are recomputed base-2 straight from the saved LSE (P = exp2(s − lse·log₂e), no running-max machinery). Operand scaling folds into the bf16 conversions: scale (0.125) into dS (K staged unscaled); ln2 into dSᵀ, so the pre-scaled Q lands scale on dK (ln2·scale·log₂e = scale); dV needs none. Gradient writes are disjoint by query/key tile — no atomics. The three-kernel split keeps backward_dot fp32 in lib.rs.

Host (host.rs): Tcgen05Flash::backward_q/backward_kv, their launch configs, and dynamic-SMEM opt-ins. logsumexp (natural log) and dot (Σ dy·y) are read as plain read-only device slices; dY is a new fourth staging panel + TMA map.

Model integration (gpu/model): dY staging buffer + TMA map added to FlashAttentionScratch; scratch + flash_bf16 threaded into flash_attention_backward_into and all backward callers. Tile-aligned shapes re-stage Q/K/V/dY (scratch is shared across layers, so forward-time staging is stale by backward), run backward_dot over the forward y, then the two tcgen05 kernels. Non-aligned shapes keep the fp32 tiled path.

Gate results

Parity — flash bin CPU reference (staged-bf16, exact exp2/f64, mirrors each kernel's exact ops), tolerance 5e-3:

tcgen05 backward parity against staged-bf16 CPU reference [2,128,3,64]
  dq max abs 6.936e-4   dk max abs 6.380e-4   dv max abs 3.777e-3
[1,256,2,64]   dq 5.895e-4   dk 5.223e-4   dv 1.902e-3
[1,1024,4,64]  dq 4.589e-4   dk 5.870e-4   dv 2.895e-3

Parity — main.rs vs the ops materialized-probability oracle (model data flow: tcgen05 forward y/LSE → fp32 backward_dot → gradient kernels), tolerance 1e-2:

tcgen05 backward parity against ops oracle [1,128,2,64]   dq 1.031e-3  dk 1.653e-3  dv 3.502e-3
[2,256,3,64]   dq 1.299e-3  dk 2.114e-3  dv 4.951e-3
[1,1024,4,64]  dq 1.971e-3  dk 1.345e-3  dv 5.476e-3
✓ tcgen05 backward parity passed on tile-aligned shapes

Model gates (./run.sh model, exit 0): the aligned tcgen05 MoE model parity gate (T=128, which routes attention through the tcgen05 backward) matches CPU; every overfit still converges to ~0; no re-tune needed.

✓ full GPU Dense forward/backward and AdamW (bf16 lm-head) match CPU
✓ aligned tcgen05 substituted MoE model matches CPU with drops and underfull experts
✓ aligned tcgen05 MoE overfits with scheduled aux loss (3.044517 -> 0.000007)

Kernel bench (flash bin, B=32 T=1024 H=24, same container): combined dQ + dK/dV backward 3.94 ms, 73.5 TFLOP/s (vs the forward's persistent 0.84 ms / 137.7 TFLOP/s).

Same-container profile (BASELINE_REF=main ./run.sh model profile):

row baseline (main) candidate speedup
backward.attention.flash_q 21.49 ms (14.36%) 1.84 ms (1.67%) 11.7×
backward.attention.flash_kv 22.56 ms (15.07%) 2.11 ms (1.92%) 10.7×
backward.attention.stage_bf16 — 0.51 ms (new) —
backward.attention (dot+stage+q+kv) 44.46 ms 4.87 ms 9.1×
full step 149.65 ms 110.07 ms −26.4%

Sanitizers: compute-sanitizer is unavailable on the B200 — memcheck, racecheck, and synccheck all abort with Error: Device not supported (sm_100 is unsupported by the sanitizer in this container), so the backward is gated by the three-tier oracle chain + the end-to-end model parity instead.

Deviations from the plan

  • Sync-only (pipelined backward deferred). The plan's commit 2 pipelines the kernels only after commit 1 is green. It is; but the sync backward already takes backward attention from 30.0% of the step to 4.4% (flash_q + flash_kv = 3.6%), so per SPEC's profile-ordered rule it is no longer a tail and the pipelined/warp-specialized backward is not profile-justified. The perf gate — the actual bar for shipping sync-only — passes decisively (−26.4% full step). Recorded as a follow-up.
  • Backward TFLOP/s lives in the flash bin's bench, mirroring exactly where the forward's TFLOP/s lives (src/bin/flash.rs::bench), rather than src/bin/bench.rs (which benches only the fp32 oracle kernels). No modal_app.py changes were needed.

Follow-ups (all recorded, none blocking)

  • Pipelined / warp-specialized backward (K/V and Q/dY TMA rings, single MMA-issue warp, mbarrier handoffs; the S/dP double-buffer constraint the plan flagged).
  • Fused single-pass backward and 2-CTA MMA — explicitly deferred per the issue.

Refs #35. Do not merge.

feitreim added 3 commits July 22, 2026 19:31
…gates (#35)

Phase 4 of #35: two synchronous tcgen05 backward kernels in the pure-PTX
flash.ptx artifact, sharing the forward's swizzle-aware bf16 fragment writes,
transposed-B gradient MMA shape, and fp32 TMEM accumulation.

- flash_backward_q_tcgen05 (query-parallel): recompute S=Q.Kt and dP=dY.Vt per
  key tile, form dS=P*(dP-D)*scale, accumulate dQ += dS.K.
- flash_backward_kv_tcgen05 (key-parallel): recompute St=K.Qt and dPt=V.dYt per
  query tile, accumulate dV += Pt.dY and dK += dSt.Q (ln2 folded so the
  pre-scaled Q lands scale on dK).
- Probabilities recomputed base-2 from saved LSE, no running max; disjoint
  gradient tiles, no atomics.
- Host: Tcgen05Flash::backward_q/backward_kv + configs + SMEM opt-ins.
- Gates: flash bin CPU-reference parity + backward TFLOP/s; main.rs parity vs
  the ops materialized-probability oracle at bf16 tolerances.
)

Phase 4 model integration: add the dY staging buffer + TMA map to
FlashAttentionScratch; thread scratch + flash_bf16 into
flash_attention_backward_into and its callers. Tile-aligned shapes re-stage
Q/K/V/dY (scratch is shared across layers, so forward-time staging is stale by
backward), run the existing fp32 backward_dot over the forward y, then the two
tcgen05 gradient kernels. Non-aligned shapes keep the fp32 tiled path.
Records the design, parity/bench/model gates, and the same-container profile
(full step 149.65 -> 110.07 ms, -26.4%; backward attention 44.46 -> 4.87 ms,
9.1x), plus the sync-only decision and the B200 sanitizer gap.
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