Repository navigation
FA4 phase 4: tcgen05 attention backward (dQ, dK/dV) + model integration (#35) - #40
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.ptxand 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. RecomputesS = Q·KᵀanddP = dY·Vᵀinto fp32 TMEM, forms the truedS = P·(dP − D)·scalein registers, accumulatesdQ += dS·Kin a TMEM segment.flash_backward_kv_tcgen05(key-parallel): K/V resident, streams causal query tiles. Recomputes the transposedSᵀ = K·QᵀanddPᵀ = V·dYᵀ, accumulatesdV += Pᵀ·dYanddK += 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);ln2into dSᵀ, so the pre-scaled Q landsscaleon dK (ln2·scale·log₂e = scale); dV needs none. Gradient writes are disjoint by query/key tile — no atomics. The three-kernel split keepsbackward_dotfp32 inlib.rs.Host (
host.rs):Tcgen05Flash::backward_q/backward_kv, their launch configs, and dynamic-SMEM opt-ins.logsumexp(natural log) anddot(Σ 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 toFlashAttentionScratch;scratch+flash_bf16threaded intoflash_attention_backward_intoand 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), runbackward_dotover the forwardy, 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:
Parity — main.rs vs the ops materialized-probability oracle (model data flow: tcgen05 forward y/LSE → fp32 backward_dot → gradient kernels), tolerance 1e-2:
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.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):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
src/bin/flash.rs::bench), rather thansrc/bin/bench.rs(which benches only the fp32 oracle kernels). No modal_app.py changes were needed.Follow-ups (all recorded, none blocking)
Refs #35. Do not merge.