Repository navigation
vulkan: complete turbo2/turbo4 dequant path + hoist turbo dequant4 loads - #373
Conversation
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).
|
Checked and merging. Thanks for splitting it into two commits, that made the hoist easy to review on its own. What I checked:
Side effect worth knowing: On the pp512 table. Between 915e4a6 and 15895ba the only SPIR-V that changes is Two older Vulkan problems came out of this review, filed separately:
If you're up for a follow-up PR: |
92e0cb5
into
TheTom:feature/turboquant-kv-cache
|
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. |
Summary
Two changes, kept as separate commits on this branch:
dequant_turbo2_0/dequant_turbo4_0shaders and pipeline registrations.turbo3_0already had a standalone dequant shader,GET_ROWSpipelines, andCPY-to-F32 pipelines;turbo2_0/turbo4_0only hadSET_ROWSand 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_dmindequant_funcs.glsl,dequant_turboN_0.comp,get_rows_turboN_0[.f32],cpy_turboN_0_f32), mirroring the existingturbo3_0pattern exactly. Centroids matchggml/src/ggml-turbo-quant.cbyte for byte.Hoist of
dequantize4()for turbo2/3/4.dequantize4()calleddequantize()twice, each independently re-reading.qs[](and.signs[]for turbo3) from global memory. Every caller guaranteesiqs % 4 == 0, so the 4 elementsiqs..iqs+3always share a singleqsbyte (turbo2/turbo3, 4 values packed per byte) or a single pair of consecutiveqsbytes (turbo4, nibble-packed, 2 values per byte), and for turbo3 a singlesignsbyte (iqs/8is 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_ROWSandCPY-to-F32 (the non-flash-attention dequant path). Flash attention itself uses its own already-hoistedFA_DEQUANT4_TURBOx_0macros inflash_attn_dequant.glsl, untouched here.Commits
vulkan: add generic dequant path for turbo2_0/turbo4_0vulkan: hoist dequantize4 loads for turbo2/3/4 KV dequantNo generated files, no compiled binaries committed.
Correctness validation
1. CPU codec round-trip gate (
test-turbo-quant)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.compwithDATA_A_TURBO2_0/DATA_A_TURBO3_0/DATA_A_TURBO4_0, bothD_TYPE=float16_tandD_TYPE=floatcopy_from_quant.compwith the same three types (exercises the hoisteddequantize4())DATA_A_Q4_0still compiles clean through the same paths3. Numerical equivalence of the hoist
Standalone C harness replicating the un-hoisted and hoisted GLSL logic bit-for-bit, run against 1000 random
qs/signsbyte patterns per type at every validiqs(iqs % 4 == 0, full range[0, 128)):96000 comparisons across turbo2/turbo3/turbo4, zero mismatches.
4.
test-backend-opson real Vulkan hardwareApple M2 (MoltenVK):
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-opsdoes not have a dedicated GET_ROWS/CPY sweep for turbo types: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):Full
test-backend-ops -b Vulkan0sweep (no-ofilter, 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-benchbefore/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:
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 twoggml-vulkanrebuilds.