Skip to content

The attention data path traverses the same Q/K/V bytes four times #88

Closed

Description

@feitreim

Tracking issue for the passes that traverse the same Q/K/V bytes back to back around flash attention. Baseline: main @ ea8e147, standalone ./run.sh model profile, 401.81 ms full step (398.14 ms in kernels) at the §13.9 shape (L=12, B=12, T=2048, D=3072, H=24, HD=128, E=8, K=2).

Measured state

row ms % step
forward.qkv_proj.split 7.4433 1.85%
forward.q_rope 8.5304 2.12%
forward.k_rope 8.5300 2.12%
forward.attention.stage_bf16 7.9263 1.97%
backward.attention.stage_bf16 10.6321 2.65%
backward.q_rope 8.5950 2.14%
backward.k_rope 8.5831 2.14%
backward.qkv_proj.join 7.0409 1.75%
total 67.28 16.74%

The chain is: the qkv GEMM writes an fp32 [N,3D] panel → split_group3 reads and writes it → RoPE reads and writes Q and K → stage_attention_heads_bf16 reads them and writes packed bf16 → flash reads the bf16. Four traversals of the same bytes where one does. The backward mirrors it: flash backward writes fp32 dQ/dK/dV → two rotation passes → join_group3 → the weight GEMM quantizes.

Diagnosis, in two parts

1. RoPE was not bandwidth-bound at all. Per layer a rotation moves 604 MB and took 0.71 ms — 850 GB/s, roughly an eighth of what the staging pass beside it achieves on comparable traffic. bench.rs's bench_rope comment already named the mechanism (#70): the kernel is one thread per element, and each thread computes a powf, a sin and a cos. Two threads redo the same angle for the two halves of one rotated pair, and each loads both halves, so the pass is 2× the compulsory reads on top of ~6 SFU-class ops per element.

The angles are a function of (position, pair) only — T × HD floats, 1 MiB at the training shape, L2-resident and re-read N·H/T times. Nothing about them needed to be in the inner loop.

2. Everything between the GEMM and flash is pure storage. By §7.1's own question — is anything accumulated or compared in this buffer — the split output, the rotated Q/K, and the fp32 acts.q/k/v triple are all storage between two kernels whose only consumer quantizes them. §7.1 flagged q/k/v as deferred candidates for exactly this reason.

Plan

  1. RoPE's angles come from a table, and the backward rotation folds into the join (RoPE reads its angles from a table, and the backward rotation folds into the join (#88) #89). Table built host-side with nn::Rope's own expression, so the CPU reference and every device consumer share one set of angles. Rotation kernels become one thread per pair. join_group3_rope substitutes for rope_backward twice + join_group3; gated bit-exactly against that composition.
  2. Q/K/V reach flash as one rotate-and-stage pass, and stay staged for the backward (Q/K/V reach flash as one rotate-and-stage pass, and stay staged for the backward (#88) #90). stage_qkv_heads_bf16 reads the fp32 [N,3D] panel and writes the three head-major bf16 panels — split, rotate, scale, quantize and relayout in one pass. The panels become the per-block saved activation (§7.1 decision change), so the backward re-stages only dY.

Both are exact substitutions: every intermediate dropped was fp32 storage between two kernels, so the fused kernels reproduce the composition bit for bit, and that is what the gates assert rather than a tolerance.

Not attempted, and why

The qkv GEMM epilogue. The owner's framing allowed either the epilogue or the flash operand load path. The load path was taken: the epilogue is a Blackwell pipeline stage (TMEM drain → smem swizzle → TMA store) that #80's whole campaign put at the dominant cost, its tile mapping is [256,256] MN-major rather than head-major, and the rotation crosses column pairs inside a tile. The staging pass already reads exactly these bytes and already writes exactly the layout flash wants, so the fusion costs one kernel there and nothing anywhere else. Revisit only if the staging row becomes the residual.

Activity

  1. feitreim commented on Aug 5, 2026

    @feitreim
    OwnerAuthor

    Both phases shipped: #89 (table + fused backward join) and #90 (fused rotate-and-stage, staged panels as the saved activation, stacked on #89).

    Cumulative, same container, reverse order

    BASELINE_REF=<stack> ./run.sh model profile with main mounted as the candidate, i.e. the delta below is what going back to main costs, negated. Running it this way round is the order check on the two forward-order pairs in #89 and #90.

    row #89+#90 main ea8e147 Δ
    forward.qkv_proj.split — 7.2550 −7.2550
    forward.q_rope — 7.9196 −7.9196
    forward.k_rope — 7.8935 −7.8935
    forward.attention.stage_bf16 — 7.4532 −7.4532
    forward.attention.stage_qkv 5.0422 — +5.0422
    backward.attention.stage_bf16 2.5288 10.0695 −7.5407
    backward.q_rope — 8.1012 −8.1012
    backward.k_rope — 8.0300 −8.0300
    backward.qkv_proj.join 4.6840 6.9613 −2.2773
    the eight rows above 12.2550 63.6833 −51.43 (−80.7%)
    full step 776.1234 837.9876 −61.8642 (−7.38%)

    Both orders agree: the two forward-order pairs sum to −55.2 ms, this reverse-order pair reads −61.9 ms, same rows and same sign. The ~10 ms the step moves beyond the eight rows is nine GEMM rows each drifting 0.4–2.4 ms in the same direction in both runs — consistent enough to be real (the freed 5.4 GiB is the only candidate) but not something either PR claims.

    End to end

    --kernel model --bin train --shard /data/wiki-train-00000.tok --steps 100, same shard:

    main #89+#90
    tokens/s 62345.2 73109.9 (+17.3%)
    MFU 27.68% 32.46% (+4.78 pp)
    step 394.2 ms 336.2 ms (−14.7%)
    step=100 loss 8.119326 8.000919
    workspace VRAM 134.8 GiB 129.4 GiB

    What is left of it

    12.26 ms across three rows, against 63.68:

    • forward.attention.stage_qkv 5.04 — one pass, reading [N,3D] fp32 and writing [N,3D/2] words. This is the compulsory traffic; it is not obviously improvable without moving it into the qkv GEMM epilogue, which is the option this issue deliberately declined.
    • backward.attention.stage_bf16 2.53 — dY, one operand, same shape. Same story.
    • backward.qkv_proj.join 4.68 — reads dQ/dK/dV fp32 and writes the fp32 [N,3D] panel that backward.qkv_proj.weight_gemm then quantizes twice (Every step GEMM gets a cuBLASLt denominator at its exact shape and mode, and the fold, not the walk, is what lags #79's note). This is the remaining fusion here: join_group3_rope could write packed bf16 directly and delete both quantizes, on the same §7.1 argument that carried q/k/v — scratch.qkv on the backward side is written by one kernel and read only by consumers that quantize it. Worth its own issue; it also shrinks the backward.qkv_proj.weight_gemm row rather than the join.
  2. feitreim commented on Aug 5, 2026

    @feitreim
    OwnerAuthor

    Correction and the cleaner numbers. Both compare_profile containers above were throttled (absolute step ~810 ms where a standalone profile of the same baseline reads 401.8), and the ~10 ms of same-direction GEMM drift I attributed there to the freed VRAM is an artifact of that, not a real effect. Standalone profiles of both trees — one container each, no contention, ./run.sh model profile — say so:

    row main ea8e147 #89+#90 Δ
    forward.qkv_proj.split 7.4433 — −7.4433
    forward.q_rope 8.5304 — −8.5304
    forward.k_rope 8.5300 — −8.5300
    forward.attention.stage_bf16 7.9263 — −7.9263
    forward.attention.stage_qkv — 5.3946 +5.3946
    backward.attention.stage_bf16 10.6321 2.7417 −7.8904
    backward.q_rope 8.5950 — −8.5950
    backward.k_rope 8.5831 — −8.5831
    backward.qkv_proj.join 7.0409 5.0046 −2.0363
    the eight rows 67.2811 13.1409 −54.14 (−80.5%)
    full step 401.8066 348.7404 −53.07 (−13.2%)

    Rows sum to −54.14 against a −53.07 step — a 1.1 ms cross-check, where the throttled pairs disagreed by 6–10. Every neighbouring row is flat: forward.qkv_proj.gemm 12.19 → 12.24, forward.attention.flash 14.61 → 14.34, backward.attention.flash_kv 26.44 → 27.46, backward.qkv_proj.weight_gemm 16.97 → 17.17. Nothing outside the data path moved, which is the claim these PRs should be read as making.

    These are two separate containers, so they are corroboration and not the §10.1 gate; the gate is the three same-container pairs (both orders), which agree in sign and rows, and the same-shard training smoke (62345 → 73110 tok/s, 27.68% → 32.46% MFU).

    The residual is 13.14 ms in three rows, and #91 is the one of them that is still a fusion rather than compulsory traffic.

  3. added a commit that references this issue on Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions