[PR #624] ds4 GGUF: Q5_K GEMVs of >= N activations on int8 tensor cores (DS4_Q5K_IMMA, opt-in, sm_80+), bit for bit the dp4a kernel #632

Open
opened 2026-10-29 21:27:40 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/mw00/project-maya/pull/624
Author: @SeriousRising
Created: 10/28/2026
Status: 🔄 Open

Base: main ← Head: ds4-q5k-imma-gemv


📝 Commits (1)

  • 1872501 ds4 GGUF: Q5_K GEMVs of >= N activations on int8 tensor cores (DS4_Q5K_IMMA, opt-in, sm_80+), bit for bit the dp4a kernel

📊 Changes

4 files changed (+312 additions, -0 deletions)

View changed files

📝 docs/SETTINGS.md (+14 -0)
📝 src/ds4/ds4_iq.cu (+177 -0)
📝 src/ds4/ds4_iq.cuh (+3 -0)
📝 tests/ds4_gguf_test.cu (+118 -0)

📄 Description

What. On GGUF packs like DeepSeek-V4.1-Flash GSQ-RCO, the dense matrices that run once per verify-window row are Q5_K: wq_b, wo_a, wo_b, the shared expert, Engram wkv and the head. They go through gg::mv -> iq::mv_nc -> mv_kernel<kQ5_K, NC> (dp4a). That kernel is not arithmetic-bound: at one column it already runs at 2.5-3.5x the weight-read floor, and each extra column costs ~27 µs on wq_b and ~390 µs on the head, because every warp re-reads the activations and redoes the half sums and the F32 epilogue per row. A verify window of T rows pays that T times.

mv_q5k_imma_kernel<NG> (new, after mv_kernel in src/ds4/ds4_iq.cu): a block of 4 warps over 16 rows x 8 activations (NG = 2 for 9-16):

  • one mma.m16n8k32.s8 a 32-value sub-block gives the exact int32 dots;
  • a second one against a ones matrix gives the activations' two half sums (the Q5_K min term);
  • each thread loads a whole super-block's bytes before the math.

mv_nc dispatches to it for Q5_K when every job has >= N activations, 16-byte-aligned rows and 8-byte-aligned activations, on sm_80+ and not HIP (a STRATA_USE_HIP build keeps the dp4a kernel; NVIDIA's path is unchanged with the setting off).

Setting: MAYA_DS4_Q5K_IMMA=N (the engine reads STRATA_DS4_Q5K_IMMA, then DS4_Q5K_IMMA), off by default: unset or 0 = today's kernel. A start line says when it is on. A section and row in docs/SETTINGS.md. At one activation the tensor-core kernel is slower, so it is a threshold, not a replacement; 2 is the value measured below.

Output: unchanged (bit-identical). The integer dots are exact in any order (k is permuted the same way in A and B). Each output's F32 work is mv_kernel's, operation for operation: sub_dot<kQ5_K>'s __fmul_rn(d, (float)(sc * dot)) - (m * sl + m * sh) per (row, column, sub-block); fmaf(xd, v, acc) into lane kb % 32's running sum in kb order; warp_sum's xor tree over the 32 sums; alpha * v + bias. Warp q keeps the running sums of the super-blocks with sb % 4 == q in registers, and the tree reads all 32 from shared memory. ds4_gguf_test --imma (also in the full suite) compares it with the dp4a kernel at every activation count 1-16 on three shapes, memcmp-equal: Q5_K 136 x 768 (rows past a tile, < 32 sub-blocks), 61 x 1280 (several lane rounds, a partial tile), 40 x 5120 with bias and alpha 0.5.

Measured. 2x NVIDIA CMP 170HX 64 GB (GA100, sm_80, CUDA 12.8; card 0 PCIe Gen2 x16, card 1 Gen2 x4: a mining card, platform-specific), 16-vCPU Xeon E5-2697 v4 VM. DeepSeek-V4.1-Flash-1017 GSQ-RCO IQ2_XXS, layer split CUDA0 [0,20) / CUDA1 [20,40) + head, --tp 1 --gpus 0,1 --dspark on --cpu-threads 14 --suffix-draft 0.

The maya-ds4 CLI on this branch, 2000 fixed prompt ids + 256 greedy tokens, one binary, one run each (a quick check):

unset DS4_Q5K_IMMA=2
token ids, DSpark counts = v1.1.35 = v1.1.35 (bit-identical)
verifying, ms over the 80 rounds 5024.1 4429.1 (-11.8 %)
drafting, ms a round 6.25 5.08 (the drafter's head rows are several-column Q5_K GEMVs too)
decode tok/s 41.11 46.16 (+12 %)

Per call, ds4_gguf_test --bench-cols, CUDA0, µs, dp4a / tensor cores. On this branch: wq_b 32768 x 1280 at 1 / 2 / 4 / 8 columns 77.3 / 90.6, 113.8 / 120.9, 183.7 / 122.3, 325.9 / 114.5; wo_b 5120 x 8192 at 2 / 8: 79.7 / 75.5, 265.2 / 77.0; head 129280 x 5120 at 2 / 8: 1158.6 / 760.4, 3511.9 / 783.7. So at 2 columns wq_b is still slower on the tensor cores and the other shapes are faster. The full sweep, on our v1.1.30-based fork (back to back on one GPU; absolute values drift between runs, the ratios hold):

Q5_K shape (n x k) 1 2 3 4 6 8 16
wq_b 32768 x 1280 60.2 / 72.8 89.0 / 94.8 116.0 / 95.5 143.2 / 96.1 203.0 / 99.5 258.6 / 100.6 486.0 / 191.5
wo_b 5120 x 8192 53.7 / 71.4 82.0 / 77.1 109.6 / 77.6 136.9 / 78.3 205.6 / 78.3 270.2 / 80.1 513.0 / 114.2
1024 x 5120 12.6 / 15.7 16.8 / 17.4 20.7 / 18.2 24.5 / 18.3 33.7 / 18.7 41.8 / 19.0 74.0 / 24.8
head 129280 x 5120 878 / 702 1168 / 850 1901 / 884 2640 / 919 3436 / 953 6591 / 1568

Through the server on that fork (5 prompts x 512 tokens, temperature 0, thinking off; ms a verify window from the engine's timer; its other patches the same in every arm): off 41.96 ms, 46.32 tok/s; =3 39.24 ms (-6.5 %); =2 38.77 ms (-7.6 %), 50.14 tok/s; the same text hashes. --dspark none (every step one column): 24.46 vs 24.40 ms a step, unchanged.

Hardware classes. sm_80 (2x CMP 170HX): measured above. Other sm_80+ (A100, RTX 30 / 40, Hopper): not measured; the one-column loss and the break-even count depend on the GPU's dp4a : int8-mma ratio, so it stays off until measured on e.g. a consumer sm_86 / sm_89 card. HIP and < sm_80: not compiled in.

Limits. At 1 column the tensor-core kernel loses (fixed cost ~70 µs vs 60 on wq_b). At 8 columns it is still ~5x the weight-read floor (wq_b 100 µs vs ~17), so more rows a block or a pipelined loop could go further. Q5_K only; a Q8_0 twin would follow the same plan. Tested on random data at three shapes plus end-to-end token ids, not tensor by tensor on the live pack.

Checks (as .github/workflows/tests.yml): tools/check_hip_compat.py OK; server tests 463 OK (12 skipped); installer and tools tests 584 OK (31 skipped) - the same counts as v1.1.35.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/mw00/project-maya/pull/624 **Author:** [@SeriousRising](https://github.com/SeriousRising) **Created:** 10/28/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `ds4-q5k-imma-gemv` --- ### 📝 Commits (1) - [`1872501`](https://github.com/mw00/project-maya/commit/1872501868fee0a1c40b13de76fa7b4380ab2abf) ds4 GGUF: Q5_K GEMVs of >= N activations on int8 tensor cores (DS4_Q5K_IMMA, opt-in, sm_80+), bit for bit the dp4a kernel ### 📊 Changes **4 files changed** (+312 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `docs/SETTINGS.md` (+14 -0) 📝 `src/ds4/ds4_iq.cu` (+177 -0) 📝 `src/ds4/ds4_iq.cuh` (+3 -0) 📝 `tests/ds4_gguf_test.cu` (+118 -0) </details> ### 📄 Description **What.** On GGUF packs like DeepSeek-V4.1-Flash GSQ-RCO, the dense matrices that run once per verify-window row are **Q5_K**: wq_b, wo_a, wo_b, the shared expert, Engram wkv and the head. They go through `gg::mv` -> `iq::mv_nc` -> `mv_kernel<kQ5_K, NC>` (dp4a). That kernel is not arithmetic-bound: at one column it already runs at 2.5-3.5x the weight-read floor, and each extra column costs ~27 µs on wq_b and ~390 µs on the head, because every warp re-reads the activations and redoes the half sums and the F32 epilogue per row. A verify window of T rows pays that T times. `mv_q5k_imma_kernel<NG>` (new, after `mv_kernel` in `src/ds4/ds4_iq.cu`): a block of 4 warps over 16 rows x 8 activations (NG = 2 for 9-16): - one `mma.m16n8k32.s8` a 32-value sub-block gives the exact int32 dots; - a second one against a ones matrix gives the activations' two half sums (the Q5_K min term); - each thread loads a whole super-block's bytes before the math. `mv_nc` dispatches to it for Q5_K when every job has >= N activations, 16-byte-aligned rows and 8-byte-aligned activations, on sm_80+ and not HIP (a `STRATA_USE_HIP` build keeps the dp4a kernel; NVIDIA's path is unchanged with the setting off). **Setting:** `MAYA_DS4_Q5K_IMMA=N` (the engine reads `STRATA_DS4_Q5K_IMMA`, then `DS4_Q5K_IMMA`), **off by default**: unset or 0 = today's kernel. A start line says when it is on. A section and row in `docs/SETTINGS.md`. At one activation the tensor-core kernel is slower, so it is a threshold, not a replacement; `2` is the value measured below. **Output: unchanged (bit-identical).** The integer dots are exact in any order (k is permuted the same way in A and B). Each output's F32 work is `mv_kernel`'s, operation for operation: `sub_dot<kQ5_K>`'s `__fmul_rn(d, (float)(sc * dot)) - (m * sl + m * sh)` per (row, column, sub-block); `fmaf(xd, v, acc)` into lane kb % 32's running sum in kb order; `warp_sum`'s xor tree over the 32 sums; `alpha * v + bias`. Warp q keeps the running sums of the super-blocks with sb % 4 == q in registers, and the tree reads all 32 from shared memory. `ds4_gguf_test --imma` (also in the full suite) compares it with the dp4a kernel at every activation count 1-16 on three shapes, memcmp-equal: Q5_K 136 x 768 (rows past a tile, < 32 sub-blocks), 61 x 1280 (several lane rounds, a partial tile), 40 x 5120 with bias and alpha 0.5. **Measured.** 2x NVIDIA CMP 170HX 64 GB (GA100, sm_80, CUDA 12.8; card 0 PCIe Gen2 x16, card 1 Gen2 x4: a mining card, platform-specific), 16-vCPU Xeon E5-2697 v4 VM. DeepSeek-V4.1-Flash-1017 GSQ-RCO IQ2_XXS, layer split CUDA0 [0,20) / CUDA1 [20,40) + head, `--tp 1 --gpus 0,1 --dspark on --cpu-threads 14 --suffix-draft 0`. The `maya-ds4` CLI on this branch, 2000 fixed prompt ids + 256 greedy tokens, one binary, one run each (a quick check): | | unset | `DS4_Q5K_IMMA=2` | |---|---|---| | token ids, DSpark counts | = v1.1.35 | = v1.1.35 (bit-identical) | | verifying, ms over the 80 rounds | 5024.1 | **4429.1 (-11.8 %)** | | drafting, ms a round | 6.25 | 5.08 (the drafter's head rows are several-column Q5_K GEMVs too) | | decode tok/s | 41.11 | 46.16 (+12 %) | Per call, `ds4_gguf_test --bench-cols`, CUDA0, µs, dp4a / tensor cores. On this branch: wq_b 32768 x 1280 at 1 / 2 / 4 / 8 columns 77.3 / 90.6, 113.8 / 120.9, 183.7 / 122.3, 325.9 / 114.5; wo_b 5120 x 8192 at 2 / 8: 79.7 / 75.5, 265.2 / 77.0; head 129280 x 5120 at 2 / 8: 1158.6 / 760.4, 3511.9 / 783.7. So at 2 columns wq_b is still slower on the tensor cores and the other shapes are faster. The full sweep, on our v1.1.30-based fork (back to back on one GPU; absolute values drift between runs, the ratios hold): | Q5_K shape (n x k) | 1 | 2 | 3 | 4 | 6 | 8 | 16 | |---|---|---|---|---|---|---|---| | wq_b 32768 x 1280 | 60.2 / 72.8 | 89.0 / 94.8 | 116.0 / 95.5 | 143.2 / 96.1 | 203.0 / 99.5 | 258.6 / 100.6 | 486.0 / 191.5 | | wo_b 5120 x 8192 | 53.7 / 71.4 | 82.0 / 77.1 | 109.6 / 77.6 | 136.9 / 78.3 | 205.6 / 78.3 | 270.2 / 80.1 | 513.0 / 114.2 | | 1024 x 5120 | 12.6 / 15.7 | 16.8 / 17.4 | 20.7 / 18.2 | 24.5 / 18.3 | 33.7 / 18.7 | 41.8 / 19.0 | 74.0 / 24.8 | | head 129280 x 5120 | 878 / 702 | 1168 / 850 | | 1901 / 884 | 2640 / 919 | 3436 / 953 | 6591 / 1568 | Through the server on that fork (5 prompts x 512 tokens, temperature 0, thinking off; ms a verify window from the engine's timer; its other patches the same in every arm): off 41.96 ms, 46.32 tok/s; `=3` 39.24 ms (-6.5 %); `=2` **38.77 ms (-7.6 %)**, 50.14 tok/s; the same text hashes. `--dspark none` (every step one column): 24.46 vs 24.40 ms a step, unchanged. **Hardware classes.** sm_80 (2x CMP 170HX): measured above. Other sm_80+ (A100, RTX 30 / 40, Hopper): not measured; the one-column loss and the break-even count depend on the GPU's dp4a : int8-mma ratio, so it stays off until measured on e.g. a consumer sm_86 / sm_89 card. HIP and < sm_80: not compiled in. **Limits.** At 1 column the tensor-core kernel loses (fixed cost ~70 µs vs 60 on wq_b). At 8 columns it is still ~5x the weight-read floor (wq_b 100 µs vs ~17), so more rows a block or a pipelined loop could go further. Q5_K only; a Q8_0 twin would follow the same plan. Tested on random data at three shapes plus end-to-end token ids, not tensor by tensor on the live pack. **Checks** (as `.github/workflows/tests.yml`): `tools/check_hip_compat.py` OK; server tests 463 OK (12 skipped); installer and tools tests 584 OK (31 skipped) - the same counts as v1.1.35. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
mw00/project-maya#632
No description provided.