Skip to content

fattn-vec: split the turbo K dot at D>128 to stop the VGPR spill (#294) — needs AMD validation - #295

Merged
TheTom merged 2 commits into
feature/turboquant-kv-cachefrom
fix/turbo-k-vgpr-spill
Aug 17, 2026
Merged

TheTom merged 2 commits into
feature/turboquant-kv-cachefrom
fix/turbo-k-vgpr-spill

Conversation

@TheTom

@TheTom TheTom commented Aug 13, 2026

Copy link
Copy Markdown
Owner

Proposed fix for #294, opened specifically so @jasstrong can test it. I have no AMD hardware and cannot validate the thing this is meant to fix. Details of exactly what is and is not verified are at the bottom, and I would rather you read that part first than trust the diff.

The mechanism

fattn-vec.cuh forces the KQ dot onto a single lane for turbo K:

constexpr int nthreads_KQ = K_is_turbo ? 1 : (K_is_unquantized ? 128 / cpy_nb : nthreads_KQ_q);

with the stated rationale that one lane per dot "eliminates warp_reduce_sum shuffle and halves KQ loop iterations". The cost is in the next declaration:

float2 Q_reg[ncols][(D/2)/nthreads_KQ];

With nthreads_KQ = 1 the per-lane Q working set scales linearly with D:

shape Q_reg per lane
D=128, ncols=2 float2[2][64] = 1 KiB
D=256, ncols=2 float2[2][128] = 2 KiB
D=256, ncols=2, split 8 float2[2][16] = 256 B

2 KiB per lane lines up with the 2.5-2.8 KB/lane scratch you measured, and it explains the two things your data showed that a microarchitectural story would not:

  • why K=Q8_0 spills nothing at the same shape — it takes the nthreads_KQ_q / 128/cpy_nb split, so its Q_reg is a fraction of the size
  • why CDNA, RDNA2 and RDNA3 all do it — it is a register-budget problem, not an architecture one

The change

Above D=128, use the same split the other unquantized K types already use. Turbo K is already in K_is_unquantized and already uses the float Q path, so this is treating it like its neighbours rather than inventing anything.

The kernel already supports both settings, which is the part that makes this a one-liner rather than a rewrite:

  • the turbo KQ dots are template <int D, int nthreads> and stride k_KQ_0 += nthreads*cpy_ne, indexing Q_v[k_KQ_0/nthreads + k_KQ_1], which is exactly the packed layout Q_reg provides
  • warp_reduce_sum<nthreads_KQ>(sum) is already called unconditionally and is a no-op at 1
  • the write-back guard already handles any split
  • the shared-memory LUT branch that does assume a single lane is gated on ncols == 1, and every spilling shape in your tables is ncols = 2, so none of them touch it

What I verified, and what I did not

Verified on CUDA sm_121 (GB10), -DLLAMA_FATAL_WARNINGS=ON:

build                     clean, 0 errors
test-backend-ops -o FLASH_ATTN_EXT   2/2 backends passed
turbo-K FA cases          1504 OK, 0 FAIL

Now the caveat that matters. All 1504 of those cases run at hsk=128. My change only takes effect at D > 128. So that run demonstrates I did not break the existing path, and demonstrates nothing whatsoever about the branch this PR adds. On CUDA the suite generates no turbo FA above hsk=128 at all, so there is no configuration on my hardware that exercises the new code.

What I cannot answer, and what would make this mergeable:

  1. Does the spill actually go away at <256,2,K=TURBO4_0,*> on gfx1100/gfx1030/gfx908? The arithmetic says Q_reg drops 8x, but predicted register pressure and measured register pressure are different things.
  2. What does it cost in throughput? It buys back occupancy and pays a warp_reduce_sum. At 2.5 KB/lane of scratch I would expect a large net win, but that is a guess, and if it turns out slower at D=256 the change is not worth making.
  3. Is the output still correct on AMD at D=256 with turbo K? The dot is written to be split-safe and I have read it carefully, but reading is not running.

If it helps: I closed #279 earlier today, which added exactly the hsk=256 turbo FA coverage that would exercise this. Reviving it would give a reproducible harness case rather than relying on the metrics build. Say the word and I will reopen it.

Entirely reasonable outcomes here include "the spill moves but does not go away", "it is slower", or "the mechanism is right but the split value should be different". Any of those is more useful than this sitting unmeasured, and I would rather you shoot it down with numbers than have me merge it on arithmetic.

Proposed fix for #294. See the PR for what is and is not verified.

nthreads_KQ is forced to 1 for turbo K so each lane computes a whole KQ
product and warp_reduce_sum is skipped. Q_reg is [ncols][(D/2)/nthreads_KQ],
so that choice makes the per-lane Q working set scale linearly with D:

    D=128, ncols=2   float2[2][64]  = 1 KiB per lane
    D=256, ncols=2   float2[2][128] = 2 KiB per lane

which matches the ~2.5-2.8 KB/lane scratch jasstrong measured, and explains
why a non-turbo K at the same shape spills nothing: it gets a split of 8-32,
so its Q_reg is a fraction of the size.

Above D=128 the reduction shuffle is far cheaper than the spill, so use the
same split the other unquantized K types already use. Both settings are
already supported by the kernel: the turbo KQ dots are templated on nthreads
and stride k_KQ_0 += nthreads*cpy_ne while indexing Q_v[k_KQ_0/nthreads + ..],
warp_reduce_sum<nthreads_KQ> is a no-op at 1, and the shared-memory LUT branch
that assumes a single lane is gated on ncols==1, which no spilling shape uses.
Revises the D>128 gate in a70a324, which the data on #294 shows was wrong
in both directions:

  - it missed the ~40 hsk=128 spillers present on every RDNA generation
    (40/41/42/40 on gfx1030/1100/1103/1201), the tested and common shape
  - it over-applied, costing turbo2 decode -12.6% at 8K and -31.1% at 32K on
    SM 86, with byte-identical output

seanrasch found the cause. The shared-memory LUT paths for turbo2/turbo3
compute the full D-length dot per lane with no nthreads_KQ striding and never
call warp_reduce_sum, so they structurally require nthreads_KQ==1; raising it
makes every lane redundantly recompute the whole dot. Those same shapes never
spill, and on Ampere they are what decode actually launches. My original PR
text dismissed this branch as gated on ncols==1 'which no spilling shape
uses' — true on AMD, wrong on Ampere, and the wrong thing to reason past.

So the gate is now the LUT being inactive rather than head size: keep
nthreads_KQ=1 exactly where the LUT runs, split everywhere else. turbo4 is
excluded from the LUT by shmem budget so it splits at every ncols; turbo2 and
turbo3 at ncols=1 are left alone.

n_centroids_lut now derives from the same predicate so they cannot drift.

CUDA sm_121, -DLLAMA_FATAL_WARNINGS=ON: build clean, FLASH_ATTN_EXT 2/2
backends passed, 12885 cases, turbo-K 1504 OK / 0 FAIL. 752 of those are
turbo4-K and take the new split at hsk=128, so the changed branch is
exercised rather than merely compiled.
@TheTom

TheTom commented Aug 17, 2026

Copy link
Copy Markdown
Owner Author

Revised gate pushed as f050a2501, stacked on the original rather than replacing it so the delta is readable against what you already reviewed.

@seanrasch @apollo-mg @jasstrong — thank you, all three of you were right and the first version was wrong in both directions.

What changed

The gate is now the LUT being inactive, not head size:

constexpr bool turbo_lut_active = (ncols == 1) && (D <= 256) &&
    (type_K == GGML_TYPE_TURBO3_0 || type_K == GGML_TYPE_TURBO2_0);
constexpr int nthreads_KQ = K_is_turbo ? (turbo_lut_active ? 1 : 128 / cpy_nb)
                                       : (K_is_unquantized ? 128 / cpy_nb : nthreads_KQ_q);

n_centroids_lut now derives from the same predicate so the two cannot drift apart.

Against seanrasch's SM 86 table, row by row:

shape LUT? gate expected
ncols=1, turbo2/turbo3 K active stays 1 no regression, and they never spilled
ncols=1, turbo4 K never (shmem budget) splits the 264-460 spill should go
ncols≥2, anything inactive splits the ~4.3 KB spill should go

turbo4 being excluded from the LUT by shmem budget is what makes this fall out cleanly rather than needing a special case.

On my original reasoning

I wrote in the first PR that the LUT branch "is gated on ncols==1, which no spilling shape uses." That was true of the AMD tables in front of me and false in general, and seanrasch's point is the one that matters: on Ampere the ncols==1 shapes are precisely the ones that never spill and are what decode launches. I reasoned past a branch I had already noticed, which is the same failure mode your §5 warns about.

What I can and cannot show

CUDA sm_121, -DLLAMA_FATAL_WARNINGS=ON:

build clean
FLASH_ATTN_EXT  2/2 backends passed, 12885 cases
turbo-K         1504 OK, 0 FAIL

Unlike last time, the changed branch is actually exercised: 752 of those 1504 are turbo4-K, which has no LUT at any ncols and therefore takes the new split at hsk=128. Last revision, nothing at hsk=128 changed at all, so the same 1504 proved nothing.

What I still cannot show from here is the thing the PR exists for. No spill metrics, no throughput. @jasstrong, the branch is fix/turbo-k-vgpr-spill at f050a2501 if the House of Radeons™ offer still stands. The two questions I would most like answered:

  1. Does the revised gate zero the spills at both 128 and 256, or only 256?
  2. Does it avoid the 12-31% turbo2 hit, given turbo2 at ncols=1 should now be untouched?

The follow-up question that decides whether this is enough

The ~40 hsk=128 spillers per architecture: what is their ncols and K-type composition?

It matters because if a meaningful number are ncols=1 with turbo2/turbo3 K, they are LUT-active, this gate deliberately leaves them at nthreads_KQ=1, and they will keep spilling. No gate can reach them, because splitting is exactly what regresses them.

In that case the real fix is teaching the LUT paths to stride and reduce, so they work at any nthreads_KQ — a much larger change than a gate, and worth knowing before anyone treats this PR as closing #294 rather than covering part of it.

If instead the hsk=128 spillers are mostly turbo4-K or ncols≥2, this gate should reach all of them and the picture is much simpler.

@jasstrong

Copy link
Copy Markdown

Validated f050a2501 on RDNA2 (gfx1030/V620), RDNA3 (gfx1100/7900 XTX), and the RDNA3 APU (gfx1103/Phoenix2). Per-kernel VGPR spill from -Rpass-analysis=kernel-resource-usage, turbo-K flash_attn_ext_vec only, original D>128 gate (a70a324f) → LUT gate (f050a2501):

hsk=128, ncols≥2 (the shapes the D-gate left behind): all 15 <128,2,turbo-K,*> went from 173–421 VGPR spill → 0 (a few V=TURBO3/Q8 keep a 20–37 tail).

arch kernels spilling worst spill (VGPR)
gfx1030 40 → 3 294 → 23
gfx1100 42 → 10 421 → 37
gfx1103 40 → 2 298 → 25

D=64 ncols≥2 cleared too — the D-gate had left a few there as well.

LUT shapes (ncols==1, D≤256, TURBO2/3): 0→0 spill on all three arches. Keeping them at nthreads_KQ=1 costs nothing in registers, so un-splitting the launched shapes is pure decode-cost win with no spill penalty. Right call in both directions.

Not this gate's job — flagging so they aren't read as regressions:

  • D=256 ncols≥2 turbo-K residual is identical under both gates (≤22/≤62/≤23 VGPR on RDNA2/RDNA3/APU) — a V-side/accumulator spill, not the Q_reg one this gate targets.
  • K=Q4_0 @ D=256 still spills big (428/341/385) — the non-turbo nthreads_KQ_q path, pre-existing.
  • gfx1100 only: <128,1,K=TURBO2,V=TURBO3> carries a 36 VGPR spill under both gates — a small pre-existing residual in the RDNA3 LUT path itself.

Happy to run a turbo2 decode benchmark on the launched shapes if you want Sean's runtime number confirmed on RDNA.

@TheTom

TheTom commented Aug 17, 2026

Copy link
Copy Markdown
Owner Author

Merging on the strength of @jasstrong's three-architecture validation. Thank you for turning that around so fast, and to @seanrasch and @apollo-mg for taking the first version apart — it was wrong in both directions and would have shipped a 12-31% turbo2 decode regression.

The numbers that justify it, from the RDNA sweep:

arch kernels spilling worst spill
gfx1030 40 → 3 294 → 23
gfx1100 42 → 10 421 → 37
gfx1103 40 → 2 298 → 25

All 15 <128,2,turbo-K,*> shapes went from 173-421 VGPR spill to zero, which is exactly the set the original D>128 gate left behind. D=64 ncols≥2 cleared as well. And the LUT shapes measured 0→0 on all three arches, confirming that keeping them at nthreads_KQ=1 costs nothing in registers, so un-splitting the launched shapes is a straight win.

Being clear that this does not close #294. jasstrong flagged three residuals that are not this gate's job, and I do not want them read as regressions later:

  • D=256 ncols≥2 turbo-K, ≤22/62/23 VGPR, identical under both gates — a V-side accumulator spill, a different mechanism
  • K=Q4_0 @ D=256, 428/341/385 — the non-turbo nthreads_KQ_q path, pre-existing
  • gfx1100 only, <128,1,K=TURBO2,V=TURBO3>, 36 VGPR under both gates — a residual inside the RDNA3 LUT path itself

The last one is the interesting one, since it is a LUT-active shape and therefore untouchable by any gate of this kind. If that pattern turns out to be common rather than a single instance, the fix is teaching the LUT paths to stride and reduce so they work at any nthreads_KQ, which is a much larger change.

CI here stays red on ubuntu-22-hip-quality-check, and that is expected on two counts: it was failing at Build with Werror on the nodiscard bug that #293 just fixed, and once it gets past that it will still flag the residuals above.

@jasstrong, yes please on the turbo2 decode benchmark if you still have the time. seanrasch measured the regression on SM 86 and the LUT shapes should now be byte-identical to before, so I expect flat, but expected and measured are different things and RDNA is not Ampere.

@TheTom
TheTom merged commit f6124e9 into feature/turboquant-kv-cache Aug 17, 2026
4 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants