Skip to content

vulkan: complete turbo2/turbo4 dequant path + hoist turbo dequant4 loads - #373

Merged
TheTom merged 2 commits into
TheTom:feature/turboquant-kv-cachefrom
Fenix46:feature/turboquant-vulkan-dequant-turbo24
Sep 23, 2026
Merged

TheTom merged 2 commits into
TheTom:feature/turboquant-kv-cachefrom
Fenix46:feature/turboquant-vulkan-dequant-turbo24

Conversation

@Fenix46

@Fenix46 Fenix46 commented Sep 19, 2026

Copy link
Copy Markdown

Summary

Two changes, kept as separate commits on this branch:

  1. dequant_turbo2_0 / dequant_turbo4_0 shaders and pipeline registrations. turbo3_0 already had a standalone dequant shader, GET_ROWS pipelines, and CPY-to-F32 pipelines; turbo2_0/turbo4_0 only had SET_ROWS and the FA-fused dequant macros (flash_attn_dequant.glsl), which were already correct and untouched by this PR. This adds the missing generic path (dequantize/dequantize4/get_dm in dequant_funcs.glsl, dequant_turboN_0.comp, get_rows_turboN_0[.f32], cpy_turboN_0_f32), mirroring the existing turbo3_0 pattern exactly. Centroids match ggml/src/ggml-turbo-quant.c byte for byte.

  2. Hoist of dequantize4() for turbo2/3/4. dequantize4() called dequantize() twice, each independently re-reading .qs[] (and .signs[] for turbo3) from global memory. Every caller guarantees iqs % 4 == 0, so the 4 elements iqs..iqs+3 always share a single qs byte (turbo2/turbo3, 4 values packed per byte) or a single pair of consecutive qs bytes (turbo4, nibble-packed, 2 values per byte), and for turbo3 a single signs byte (iqs/8 is constant over the range). The hoisted form reads each byte once and derives all 4 indices from it, instead of 2x (turbo2/turbo3) or 4x (turbo4) redundant loads.

Both changes only affect GET_ROWS and CPY-to-F32 (the non-flash-attention dequant path). Flash attention itself uses its own already-hoisted FA_DEQUANT4_TURBOx_0 macros in flash_attn_dequant.glsl, untouched here.

Commits

  • vulkan: add generic dequant path for turbo2_0/turbo4_0
  • vulkan: hoist dequantize4 loads for turbo2/3/4 KV dequant

No generated files, no compiled binaries committed.

Correctness validation

1. CPU codec round-trip gate (test-turbo-quant)

Test 1 (turbo3): e0 = [1, 0, ...]           MSE=0.00000000 Cosine=1.000000
Test 2 (turbo3): sin*10                     MSE=1.37790418 Cosine=0.986112
Test 3 (turbo4): cos*5                      MSE=0.11060880 Cosine=0.995649

Test 4: chunked dequant == whole-row dequant
  turbo2_0  k=128/256/384/512/4096   ok (blck=128, chunk=256)
  turbo3_0  k=128/256/384/512/4096   ok (blck=128, chunk=256)
  turbo4_0  k=128/256/384/512/4096   ok (blck=128, chunk=256)

Run on macOS (Apple M2, MoltenVK) and on Ubuntu 26.04 / RTX 4000 (native Vulkan). Identical output on both.

2. GLSL compilation (glslc)

All new/touched shader permutations compile clean (no errors/warnings), on both MoltenVK and native Vulkan (libshaderc-dev):

  • dequant_turbo2_0.comp, dequant_turbo4_0.comp (new)
  • get_rows_quant.comp with DATA_A_TURBO2_0/DATA_A_TURBO3_0/DATA_A_TURBO4_0, both D_TYPE=float16_t and D_TYPE=float
  • copy_from_quant.comp with the same three types (exercises the hoisted dequantize4())
  • Regression check: DATA_A_Q4_0 still compiles clean through the same paths

3. Numerical equivalence of the hoist

Standalone C harness replicating the un-hoisted and hoisted GLSL logic bit-for-bit, run against 1000 random qs/signs byte patterns per type at every valid iqs (iqs % 4 == 0, full range [0, 128)):

total=96000 fails=0 -> IDENTICAL

96000 comparisons across turbo2/turbo3/turbo4, zero mismatches.

4. test-backend-ops on real Vulkan hardware

Apple M2 (MoltenVK):

SET_ROWS_TURBO3: 21/21 tests passed, Backend Vulkan0: OK
SET_ROWS_TURBO4: 21/21 tests passed, Backend Vulkan0: OK

Standalone GET_ROWS/CPY smoke test (SET_ROWS -> GET_ROWS -> inverse WHT and SET_ROWS -> CPY-to-F32 -> inverse WHT, compared against the un-rotated input), since test-backend-ops does not have a dedicated GET_ROWS/CPY sweep for turbo types:

type cosine similarity GET_ROWS vs CPY
turbo2_0 0.942 identical, max_diff=0.000000
turbo3_0 0.984 identical, max_diff=0.000000
turbo4_0 0.996 identical, max_diff=0.000000

Values consistent with the documented quality bar (turbo4 cosine 0.9956 in the codec test).

Ubuntu 26.04 / Quadro RTX 4000 (native Vulkan, matrix cores: NV_coopmat2):

SET_ROWS_TURBO3: 21/21 tests passed, Backend Vulkan0: OK
SET_ROWS_TURBO4: 21/21 tests passed, Backend Vulkan0: OK

Full test-backend-ops -b Vulkan0 sweep (no -o filter, every op) run to ~15900 lines with zero failures logged across SET_ROWS, DEQUANTIZE, IM2COL, MUL_MAT, MUL_MAT_ID/FUSION (MoE), ROPE, ROPE_BACK, TOP_K, UPSCALE.

5. llama-bench before/after the hoist, same build/hardware (Qwen3.5-9B Q4_K_M, RTX 4000, -ngl 99, -p 512 -n 128, -dev Vulkan0)

pp512 (tokens/s), baseline (pre-hoist) vs with hoist:

type_k type_v baseline with hoist delta
f16 f16 1319.15 1369.58 +3.8%
f16 turbo3 1285.67 1336.39 +4.0%
f16 turbo4 1208.28 1313.32 +8.7%
turbo3 f16 1135.00 1297.25 +14.3%
turbo3 turbo3 1108.23 1279.26 +15.4%
turbo3 turbo4 1100.41 1274.48 +15.8%
turbo4 f16 1101.08 1273.95 +15.7%
turbo4 turbo3 1105.43 1263.13 +14.3%
turbo4 turbo4 1123.38 1260.92 +12.2%

tg128 shows the same trend at smaller magnitude (~3-8%), as expected since decode is dominated by other factors. Gains scale with how much turbo dequant work the prefill actually does per element, consistent with the hoist's mechanism (fewer redundant global-memory reads).

Both runs used the same binary build process, same GPU, same model, same flags; only the shader source (dequant_funcs.glsl, pre- vs post-hoist) differed between the two ggml-vulkan rebuilds.

turbo3_0 had dequant_funcs.glsl entries, a standalone dequant shader,
GET_ROWS and CPY-to-F32 pipelines; turbo2_0/turbo4_0 only had SET_ROWS
and the FA-fused dequant macros. This adds the missing generic path
(dequantize/dequantize4/get_dm, dequant_turboN_0.comp, get_rows_turboN_0,
cpy_turboN_0_f32) mirroring the turbo3_0 pattern, with centroids matching
ggml-turbo-quant.c byte for byte.

Validated by compiling all new/touched shader permutations with glslc
(no errors/warnings). Full C++ build not run in this environment
(Xcode license not accepted).
dequantize4() called dequantize() twice, each re-reading .qs[] (and
.signs[] for turbo3) independently. With iqs%4==0 (guaranteed by every
caller), elements iqs..iqs+3 share a single qs byte (4 per byte for
turbo2/3, 2 consecutive bytes for turbo4's nibble-packed layout) and,
for turbo3, a single signs byte (iqs/8 constant over the range). One
load of each is enough instead of two (turbo2/3) or four (turbo4).

Verified numerically equivalent to the un-hoisted form (96000 random
qs/signs byte patterns per type, bit-exact match) and compiles clean
with glslc for the two consumers of dequantize4() in this file
(copy_from_quant.comp, get_rows_quant.comp).
@TheTom

TheTom commented Sep 23, 2026

Copy link
Copy Markdown
Owner

Checked and merging. Thanks for splitting it into two commits, that made the hoist easy to review on its own.

What I checked:

  • The hoisted dequantize4() against the old two-call form, over every qs/signs byte value at every valid iqs (about 4.2M cases). 0 mismatches, same element order.
  • All 12 new or touched turbo shader permutations from both commits build with glslc -O and pass spirv-val.
  • The turbo2 and turbo4 tables equal the C tables as float32, and the block layouts match the codec byte for byte (34 and 66 bytes).

Side effect worth knowing: SET_ROWS_TURBO4 ends its graph in a CPY from turbo4 to F32, which the base branch didn't advertise on Vulkan. So that test was skipped as "not supported" until this PR, and your 21/21 on the RTX 4000 is its first real run there.

On the pp512 table. Between 915e4a6 and 15895ba the only SPIR-V that changes is cpy_turbo{2,3,4}_0_f32, and llama-bench never runs it. Turbo KV forces flash attention, and during the bench the only ops that touch the cache are SET_ROWS, FLASH_ATTN_EXT and TURBO_WHT. CPY from turbo to F32 shows up only in the context-shift graph. The f16/f16 row moved +3.8% with no turbo code involved, so that is roughly the run-to-run drift. I wouldn't quote the table for this change. If you want to measure it, -r 10 with interleaved A/B runs and GGML_VK_PERF_LOGGER=1 will show which ops actually run.

Two older Vulkan problems came out of this review, filed separately:

If you're up for a follow-up PR: test-backend-ops has no turbo GET_ROWS or CPY sweep, and the CPU reference aborts on GET_ROWS for turbo types. Adding TURBO2_0/3_0/4_0 to the get_rows_q case group in ggml-cpu/ops.cpp, a test_get_rows plus test_cpy loop over the three types, and a SET_ROWS_TURBO2 copy of the turbo4 test would put the new paths under CI.

@TheTom
TheTom merged commit 92e0cb5 into TheTom:feature/turboquant-kv-cache Sep 23, 2026
11 of 26 checks passed
@Fenix46

Fenix46 commented Sep 23, 2026

Copy link
Copy Markdown
Author

Thanks for the thorough review and for merging!

I'll pick up the two issues you filed:

If #383 is confirmed, I'll re-measure the turbo rows properly (-r 10, interleaved A/B runs, GGML_VK_PERF_LOGGER=1) and share the numbers there.

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