Skip to content

Flash HD=128 follow-ups: persistent stream-B regression + M64 operand-K mispairing (M128 waste) #47

Closed

Description

@feitreim

Two follow-ups deferred from the HD=128 flash-attention conversion (#42, PR #46). The conversion landed sync + pipelined forwards and both backward kernels; these two items are known gaps.

1. Persistent (phase-3) forward stream-B regression at HD=128

flash_forward_persistent produces correct output for stream A (query tile 2p) but stream B (query tile 2p+1) outputs exactly 0 under the HD=128 (M128-over-64-row) conversion. Reproduced at [B=2, T=128, H=3, HD=128]: y is correct through row 63 (stream A / tile 0) and reads 0 from row 64 onward (stream B / tile 1). The sync and pipelined forwards pass the same parity, so the S=Q·Kᵀ / softmax / O=P·V math and the M128 drain are correct — the bug is in the persistent kernel's two-stream ping-pong scheduling (TMEM layout S_A=0/S_B=64/O_A=128/O_B=256, per-stream barriers, warp_id - TILE/32 for stream B, or the asymmetric tiles_a < tiles_b MMA-warp loop). exactly 0 output means stream B's out_acc merged all zeros — either the O_B accumulator was never written by the MMA warp, or the merge reads the wrong TMEM columns/lanes.

The persistent kernel is currently excluded from the flash-attn parity gate (bin/flash.rs FORWARDS and main.rs check_tcgen05_shape) and the model runs on the pipelined (phase-2) forward meanwhile.

2. M64 shape mis-pairs operand A/B K-indices — M128 workaround wastes half the MMA

The conversion uses M128_N64 MMAs over 64-row tiles (computing 128 M-rows, of which 64 are phantom garbage never drained, plus a TILE_BYTES PHANTOM_PAD per SMEM plan) because the natural M64_N64 shape is broken for this codebase's multi-chunk K accumulation. This wastes half the tensor-core rows.

Empirical findings (from transpose_b_probe / check_transpose_probe, single M64_N64 MMA, drained to a raw dump):

  • The M64 accumulator lane→row map is fine: 16-row groups pack into the first half of each of the 4 TMEM subpartitions — lane = 2*row (rows 0-15 → lanes 0-15, 16-31 → 32-47, 32-47 → 64-79, 48-63 → 96-111). Both non-transpose and transpose_b give the identical map. (So <<17 reads the right lanes for M64.)
  • Row-encoded input (C[m,n]=m): all 64 rows correct.
  • Column-encoded input (C[m,n]=n): all 64 rows correct.
  • Each operand's K-walk in isolation (A[m,k]=2^(k/16), B=1 and vice versa): all rows correct (sum invariant under permutation).
  • Random A·B (both operands varying across k): rows 0-15 correct, rows ≥16 wrong.

The only interaction that fails is both operands varying across k for accumulator rows ≥16 — i.e. for M-rows ≥16, A[m,k] is multiplied against B[n,σ(k)] for some K-permutation σ ≠ identity. σ is invisible to any test where one operand is uniform in k. This looks like an undocumented tcgen05 M64 operand-layout requirement (the M-rows ≥16 live in subpartitions 1-3 and their K addressing diverges). Resolving it — or documenting the SMEM operand layout that pairs correctly — would let the S/O/grad MMAs drop back to M64_N64 and recover the wasted half.

The probe kernel (transpose_b_probe) is retained in gpu/flash-attn/src/tcgen05.rs and exercises exactly this score_mma path; flip its operands to the encodings above to re-derive σ.

Activity

  1. feitreim commented on Jul 23, 2026

    @feitreim
    OwnerAuthor

    Item 1 (stream-B regression) — fixed in PR, root cause below

    The persistent stream-B failure was not an operand/K or TMEM-overlap bug — it was a TMEM lane-access / warp-placement bug introduced when the conversion shrank TILE from 128 to 64.

    A tcgen05.ld warp can only reach TMEM lanes (warp_id % 4) * 32 .. +32. A 64-row tile's M128 accumulator keeps its real rows in lanes 0..63. After the conversion each stream became 2 warps instead of 4, and the dispatch put stream B on warps 2–3 (warpgroup-0 positions 2–3 → hardware lanes 64..127, the undrained phantom rows). So stream B's softmax drained garbage: the inflated max made every P = exp2(s − m_ref) underflow to ~0, giving y = 0 and a too-large LSE — exactly the observed signature (and it explains why the LSE was wrong too, not just y). Stream A (warps 0–1 → lanes 0..63) was unaffected. The stale launch_bounds(320) vs the 192-thread block was the tell: at HD=64/TILE=128 each stream was a full 4-warp warpgroup (0–3 and 4–7) that naturally drained all 128 lanes.

    Fix: move stream B to warps 4–5 (warpgroup-1 positions 0–1 → lanes 0..63) and put the TMA/MMA warps on 2–3. Pure relabel, block stays 192. Persistent parity is now green on both harnesses and it profiles faster than pipelined (kernel bench [32,1024,24,128] 226 vs 181 TFLOP/s; BASELINE_REF=main model profile forward.attention.flash 3.07 → 2.45 ms, −20%), so the model is switched back to the persistent forward.

    Item 2 (M64 operand-K mispairing) — not cracked; not shipped

    I did not empirically re-derive σ or change the MMA shape — flagging honestly rather than shipping a speculative change (the M128-over-64-row path stays).

    What the item-1 result rules in/out: the M64 quirk is a genuine operand-K addressing issue, separate from the item-1 lane bug. The item-1 symptom depended on the input encoding via underflow; the item-2 failure is encoding-invariant except when both operands vary across k (row-, column-, and single-operand-k encodings all pass) — that can only be an A/B K-index permutation σ ≠ identity for accumulator rows ≥16, i.e. the subpartition-1..3 rows.

    Doc research (PTX ISA §9.7.17.4 matrix descriptors, gau-nernst's tcgen05 writeup, Colfax's Blackwell-TMEM tutorial) confirms the SMEM descriptor encodes a Leading Byte Offset (LBO) and Stride Byte Offset (SBO) over 8×16B core matrices, but none of them give an explicit M64-vs-M128 operand-A core-matrix formulation — the community writeups only work M128.

    Best current hypothesis for whoever picks this up: score_mma/grad_mma build the A descriptor with the M128-tuned smem_descriptor (LEADING_BYTES=16, STRIDE_BYTES=1024) and M128 K-chunk advances (chunk*32, SUBTILE_BYTES for the 2nd HD subtile). For M64 the A operand has 64 rows, so the SBO between 8-row core-matrix blocks and the per-K-chunk leading advance differ; the M128 descriptor then mis-addresses A's K chunks for the rows that land in subpartitions 1–3 (rows ≥16) — matching the observed σ footprint exactly, and invisible whenever one operand is uniform in k (the permuted-k sum is invariant). Cracking it means deriving the M64 A-descriptor LBO/SBO + K-chunk byte advance from transpose_b_probe (flip its operands to random-A·B and dump rows 16..63), then converting the S/O/grad MMAs to M64_N64 and deleting PHANTOM_PAD. Left open.

  2. feitreim commented on Jul 23, 2026

    @feitreim
    OwnerAuthor

    Item 2 (M64 operand mispairing) — root cause found; Design A is a dead end; nothing shipped

    Picked this up on perf/flash-m64-recovery (on top of #48). Converted transpose_b_probe to the real M64_N64 shape with the resurrected dcb922a M64 drain (<<17, 2-warp lane map matching the empirically-verified accumulator layout: rows 0-15→lanes 0-15, 16-31→32-47, 32-47→64-79, 48-63→96-111), then ran a series of structured operand encodings (seeded host-side, drained raw, solved offline). B200, clean build.

    The failure is NOT a K-permutation σ — it is an A-operand M-row broadcast

    Four probe encodings, in order:

    1. One-hot pairing (A[m,k]=k, B[n,k]=δ(k==n+64r), both K-subtiles over two runs → C[m,n] = the A-k the core paired with B-slot n+64r): identity for all 64 rows, both subtiles. So there is no K-permutation, and each accumulator row reads all 128 K-columns correctly paired.

    2. Chunk histogram (A=1, B[n,k]=2^(k/16) so each of the 8 K=16 chunks has a distinct weight): every row sums each chunk exactly once and is N-invariant (C=4080 everywhere). No chunk duplicated/dropped, no N/column mixing.

    3. Dense random A·B: rows 0-15 exact, rows 16-63 wrong (48/64 rows > 5% rel) — reproduces the reported symptom exactly.

    4. Row-encoded M-probe (A[m,k]=m, B=1 → C[m,n]/128 = the A-row actually read for accumulator row m): reveals

      π(m) = 16·(m>>5) + (m&15)

      • rows 0-15 → A-rows 0-15 (correct)
      • rows 16-31 → A-rows 0-15
      • rows 32-47 → A-rows 16-31
      • rows 48-63 → A-rows 16-31

    The M64 MMA reads only 32 distinct A-rows; A-rows 32-63 appear in no output. Each subpartition-pair aliases (accumulator rows m and m+16 read identical A data across all 128 K — confirmed by the one-hot spanning both subtiles, so it is a true broadcast, not a 2-wide K-pack). In datapath-lane terms A-row = 16·(L>>6) + (L&15), i.e. the M64 SS-operand read ignores lane bits [5:4].

    Why every prior probe passed and only dense random failed: row-, column-, and single-operand-K encodings all have A that is either constant in M or invariant under the aliasing, so the broadcast is invisible. Only A that varies in M (dense random) exposes it. The earlier "σ K-permutation for rows ≥16" reading was the visible shadow of this M-broadcast, not a K effect.

    It is not fixable by the SMEM descriptor

    Swept the A-operand stride-byte-offset (SBO) with everything else fixed:

    A_SBO= 512: distinct_rows=20   (more aliasing)
    A_SBO=1024: distinct_rows=32   sp1 reads A[0..15], sp3 reads A[16..23]   (current)
    A_SBO=2048: distinct_rows=32   sp1 reads A[0..15], sp3 reads A[32..39]
    

    Every stride yields exactly 32 distinct rows. SBO shifts which 32 rows are read but the subpartition-pair aliasing (the bit-4 drop) is invariant to it — no stride ever reaches 64. (SBO ≥ 4096 walks off the tile → illegal access, as expected.) Since the aliasing collapses two accumulator rows onto one SMEM location, no SMEM relayout can serve them either. CUTLASS mma_traits_sm100 parametrizes the A-layout uniformly on M with no M64 special case, and the PTX ISA / community writeups only ever work M128 — consistent with this being a fixed property of the M64 SS-operand→datapath mapping, not something LBO/SBO exposes.

    Conclusion: Design A (reclaim the wasted half by fixing the M64 operand addressing) is a dead end. The M128-over-64-row path with PHANTOM_PAD stays.

    Design B assessment (not attempted)

    Recovering the half now requires pairing two 64-row tiles per M128 MMA (M128 works and reads all 128 rows contiguously). Blast radius on the persistent forward: interleave the paired tiles' subtiles in SMEM as [A_sub1, B_sub1, A_sub2, B_sub2] for both Q (score) and P (O=P·V); stream B's results then land in accumulator rows 64-127 = lanes 64-127, which inverts the #48 fix (that fix moved stream B off warps 2-3 precisely to avoid those lanes) and requires reworking which warps run softmax for which stream, plus the correction-vote/barrier structure. Forward-only unless the two backward kernels are also restructured (pair adjacent query/key tiles). Given the lane/warp subtlety that produced the #48 regression, and that the attention kernels are likely not purely MMA-bound (so the upside is a fraction of the forward, none of the backward), the blast radius is large relative to the win.

    Per "ship only what parity proves" / "a documented dead end beats a speculative change," nothing was shipped and there is no PR. All probe scaffolding was reverted; perf/flash-m64-recovery == origin/main. Leaving item 2 open for whoever takes on Design B; the dcb922a M64 drain is not needed there since Design B stays on M128.

  3. feitreim commented on Jul 23, 2026

    @feitreim
    OwnerAuthor

    Design B landed on the backwards (PR #51); the forward pairing is a documented non-win

    Followed the plan on perf/flash-m128-pairing (PR #51). Both backward kernels are paired and green — gradients bit-for-bit the same tolerance as the unpaired kernels, and the kernel bench [32,1024,24,128] backward goes 9.28 ms / 118 TFLOP/s → 5.42 ms / 202 TFLOP/s (1.71×), the phantom-half recovery Design B promised. Partially addresses this item.

    The persistent forward pairing is correct but REGRESSES — leaving it on ping-pong

    I implemented the forward pairing exactly as specced (stack [Q_A;Q_B] into one S-MMA, [P_A;P_B] into one O-MMA, stream B's softmax on warps 2–3 to invert the #48 fix, joint correction vote, per-stream running max/sum). It passes every parity shape (in-crate and cross-oracle, incl. the odd-tail T=192 and the 152-workstream loop) — the design and the warp/lane inversion are sound.

    But it is slower: kernel bench [32,1024,24,128] persistent forward 226 → 170 TFLOP/s (below even the pipelined 181). Root cause: pairing fuses the two query tiles into a single 128-thread warpgroup, which destroys the two-warpgroup ping-pong that made the persistent forward fast in the first place (7e14: while warpgroup A runs softmax, the MMA warp feeds warpgroup B's independent tile — that dual stream is what filled the tensor-core issue slots). With one fused stream the MMA warp only has S(i+1)-double-buffer + O(i-1) to overlap — i.e. it degenerates to the pipelined schedule. The forward is scheduling-bound, not MMA-bound, so halving the MMA count buys nothing and the lost ping-pong costs ~25%.

    So the forward stays on the 7e14 ping-pong (this branch reverts it to origin/main); score_mma_paired/load_q_paired are kept as the shared helpers the backwards use.

    If the forward's wasted half is worth reclaiming later, the only way that keeps the ping-pong is to run two paired warpgroups — warpgroup 0 pairs tiles (4p, 4p+1), warpgroup 1 pairs (4p+2, 4p+3), each draining 128 rows, ping-ponging over the shared K/V — i.e. 4 query tiles/CTA at 320 threads and 2× the S/O TMEM. That halves MMA count and keeps the dual stream, but it's a substantially larger kernel than the in-place pairing this issue scoped, so I left it. The backwards (synchronous, MMA-bound, no ping-pong to lose) were the right Design B target.

    Gate/perf caveat

    ./run.sh flash-attn flash and ./run.sh model are fully green. The BASELINE_REF=main model-shape A/B is incomplete: the baseline profiled (backward.attention.flash_q 66.5 ms, flash_kv 78.7 ms, full step 748.8 ms over 12 blocks) but the candidate side aborted with workspace ... is disabled (Modal spend limit). The 1.71× kernel-bench figure is the direct measured evidence; the model-shape candidate A/B is a follow-up once Modal is back.

  4. feitreim commented on Jul 25, 2026

    @feitreim
    OwnerAuthor

    Landed on main (64e74a4). Both original items were already resolved in-tree (stream-B lane fix in 2537fe5; the M64 σ-permutation hypothesis was disproven — A-operand M-row broadcast, π(m)=16·(m>>5)+(m&15), not descriptor-fixable). What this close adds: the leftover stale .maxntid literals from the TILE=128 era were fixed on both warp-specialized forwards (320→192 persistent, 192→128 pipelined), pinned with const asserts. Measured on B200: persistent forward regained 38 regs/thread, 227.5 → 230.2/231.9 TFLOP/s (~+1.5%); parity byte-identical. The flash gate now prints per-kernel ptxas budgets (host::function_profile) so a stale bound can't hide again, and PTX=1 dumping was un-broken for crates with device atomics (dump_ptx now pins --arch sm_100a). SPEC §7e16. Source docs on PHANTOM_PAD/transpose_b_probe updated to the settled M64 findings.

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