Repository navigation
fattn-vec: split the turbo K dot at D>128 to stop the VGPR spill (#294) — needs AMD validation - #295
Conversation
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.
|
Revised gate pushed as @seanrasch @apollo-mg @jasstrong — thank you, all three of you were right and the first version was wrong in both directions. What changedThe 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);
Against seanrasch's SM 86 table, row by row:
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 reasoningI wrote in the first PR that the LUT branch "is gated on What I can and cannot showCUDA sm_121, 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
The follow-up question that decides whether this is enoughThe ~40 hsk=128 spillers per architecture: what is their ncols and K-type composition? It matters because if a meaningful number are In that case the real fix is teaching the LUT paths to stride and reduce, so they work at any 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. |
|
Validated hsk=128, ncols≥2 (the shapes the D-gate left behind): all 15
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 Not this gate's job — flagging so they aren't read as regressions:
Happy to run a turbo2 decode benchmark on the launched shapes if you want Sean's runtime number confirmed on RDNA. |
|
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:
All 15 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:
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 CI here stays red on @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. |
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.cuhforces the KQ dot onto a single lane for turbo K: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 = 1the per-lane Q working set scales linearly with D:float2[2][64]= 1 KiBfloat2[2][128]= 2 KiBfloat2[2][16]= 256 B2 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:
nthreads_KQ_q/128/cpy_nbsplit, so its Q_reg is a fraction of the sizeThe change
Above D=128, use the same split the other unquantized K types already use. Turbo K is already in
K_is_unquantizedand 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:
template <int D, int nthreads>and stridek_KQ_0 += nthreads*cpy_ne, indexingQ_v[k_KQ_0/nthreads + k_KQ_1], which is exactly the packed layoutQ_regprovideswarp_reduce_sum<nthreads_KQ>(sum)is already called unconditionally and is a no-op at 1ncols == 1, and every spilling shape in your tables isncols = 2, so none of them touch itWhat I verified, and what I did not
Verified on CUDA sm_121 (GB10),
-DLLAMA_FATAL_WARNINGS=ON:Now the caveat that matters. All 1504 of those cases run at
hsk=128. My change only takes effect atD > 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:
<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.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.If it helps: I closed #279 earlier today, which added exactly the
hsk=256turbo 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.