Skip to content

A stopwatch on every phase of a flash backward tile visit, and what it finds (#94) - #95

Merged
feitreim merged 1 commit into
mainfrom
flash-bwd-probe
Aug 5, 2026
Merged

feitreim merged 1 commit into
mainfrom
flash-bwd-probe

Conversation

@feitreim

@feitreim feitreim commented Aug 5, 2026

Copy link
Copy Markdown
Owner

Phase 1 of #94, and the instrument the rest of that front is argued from. No shipped kernel changes; flash_forward, flash_backward_q and flash_backward_kv are untouched to the byte.

Why an instrument first

The backward attention rows are 57.01 ms of a 355.82 ms step (16.0%) on a clean standalone profile of this base — flash_kv 29.80, flash_q 21.33, flash_dot 5.88 — which is the largest block left after #89/#92/#93. The bench puts the two kernels at 1.094 and 1.566 ms.

Neither roof explains that. Per key-tile visit at [32,1024,24,128], 55 296 visits over 148 SMs at 1 CTA/SM:

ns/visit MMA floor of tensor peak DRAM compulsory of 8 TB/s
backward q 2 925 414 ns 14.1% 1.21 GB / 151 µs 13.8%
backward kv 4 154 552 ns 13.3% 1.61 GB / 201 µs 13.0%

Both kernels sit at ~13–14% of both roofs, so the time is in neither, and the standing candidate list (operand staging, ring depth, recompute-vs-reload, LSE handling, launch overhead) had no way to be told apart. #80's campaign settled the same question twice with a clock64 phase probe; this is that pattern ported to flash.

What it is

  • gpu/flash-attn/src/phase_probe.rs — the two backward kernels copied and instrumented, as a child module of tcgen05 (#[path]), so it sees the shared plans, the tile types and the tile contract without any of them becoming crate surface. This is gemm/src/phase_probe.rs's shape; the difference is that flash's plans are private and gemm's operands were reconstructible from kittens.
  • Two #[kernel] entry points in tcgen05::kernels, flash_backward_q_probe / flash_backward_kv_probe, taking one extra clocks slice.
  • gpu/flash-attn/src/bin/probe.rs — ./run.sh flash-attn probe. Times the shipped kernel and the instrumented one back to back at three shapes and prints the per-visit budget.
  • Two Tcgen05Flash launch methods beside the existing ones.

enforce_kernel_budgets reads flash.kernels(), which is the three shipped kernels, so no budget moves — the probe kernels are unpinned by construction.

The stopwatch costs under 1%

Probe against shipped, same container, six measurements:

shape backward q backward kv
[32,1024,24] 1.085 vs 1.093 (−0.7%) 1.565 vs 1.552 (+0.8%)
[12,2048,24] 1.447 vs 1.458 (−0.7%) 2.059 vs 2.039 (+1.0%)
[32,512,24] 0.348 vs 0.350 (−0.6%) 0.508 vs 0.504 (+0.9%)

Two of the six are faster than the kernel they instrument, which is the size of the noise. These are the shipped kernels' own numbers.

The budget

Ticks are the CTA's own SM clock, divided by that CTA's tile visits. Columns are the leader's role and the warpgroup's role — the same 128 threads at different times.

backward q

shape ISSUE PASS FEED RECYCLE SCORED GRAD STAT SYNC Σ
[32,1024,24] 2 108 1 641 244 63 91 91 11 52 4 301
[12,2048,24] 2 160 1 608 164 76 91 98 5 52 4 254
[32,512,24] 2 025 1 695 428 42 91 79 20 52 4 432

backward kv

shape ISSUE PASS FEED RECYCLE SCORED GRAD STAT SYNC Σ
[32,1024,24] 2 628 2 708 249 63 91 91 397 54 6 281
[12,2048,24] 2 679 2 553 152 77 91 98 396 52 6 098
[32,512,24] 2 545 3 023 563 42 91 79 398 60 6 801

Σ against the shipped kernel's own ns/visit implies an SM clock of 1.47–1.58 GHz at the two real shapes — a plausible loaded B200 clock, and the check that says the columns are the whole visit.

Three things it settles

Every wait is nearly zero. SCORED — the warpgroup waiting the score MMAs of the tile it is about to work on — is 91 ticks, flat, at every shape in both kernels. GRAD 79–98, RECYCLE 42–77, SYNC 52–60, FEED 152–563 and falling as the stream deepens. Against a 4 300–6 800 tick visit, #75's overlap is not merely working, it has ~46× more slack than it uses. That prices operand staging and ring depth at ≤7% combined and says the GEMM campaign's six-stage lesson does not transfer — flash's feed was never short.

The visit is warp 0's serial chain. ISSUE is the leader's MMA issue, not a wait, and it is 42–49% of the visit — added to the register pass rather than overlapped with it, because the leader is one of the four pass warps. Warps 1–3 finish their pass and sit at the block barrier. The instrument reports from warp 0, which arrives last, which is why its own SYNC reads 52 rather than ~2 100 — and the eight columns sum to 4 301 against a 4 390-tick visit, so 99% of the visit is one warp's serial chain.

ISSUE scales with the MMA instruction count (kernel A issues 24 tcgen05.mma per visit, kernel B 32, ratio 1.333; measured ratio 1.247), which is what says it is the tensor core's own cadence. Per instruction ~88 ticks against a ~26-tick full-rate figure: the tensor core while busy delivers 4.4 / 4.8 TFLOP/s per SM, 29–32% of the SM's peak, which is MmaShape::M128_N64's own rate.

Kernel B pays 397 ticks a visit for its statistic where kernel A pays 11. Kernel A hoists load_row_vec to the item head; kernel B's transposed band indexes the statistic by column, so load_col_vec runs per SCORE_CHUNK inside the pass. 6.3% of kernel B.

Gates

  • --kernel flash-attn --bin flash — green. Parity byte-identical at every gated shape (y ≤ 1.469e-3, lse ≤ 9.537e-7, dq ≤ 7.299e-4, dk ≤ 7.437e-4, dv ≤ 2.933e-3), the forced-correction shape still corrects on 100.00% of its 32 eligible visits and every other shape on 0.00%, and ptxas is 128 / 182 / 190, zero spill — the The flash forward rescales O inside its own tensor-memory segment, and the last .local frame in the module goes to zero #77 pins, unmoved, because no shipped kernel changed.
  • --kernel flash-attn — green (the two-oracle gate).
  • No --kernel ops / --kernel model claim needed: gpu/model includes flash-attn/src/lib.rs, and the only additions are new kernels nothing on that path loads. The model gate is run on the change stacked on this one.
  • No full-step A/B: this PR cannot move a row.

Note

The instrument belongs in main on its own merits, the way #85's residency probe did — the next question about these kernels should not have to rebuild it. A stopwatch does not belong in the shipped module, and it is not in one: tcgen05.rs's three kernels carry no counter, no clock64 and no extra argument.

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