perf(ggml-cuda): RMS norms and SWIGLU write their MMQ reader's q8_1 rows
Summary
Split out of #964 so that PR stays small. Independent of it: this branch is on main and neither needs the other.
An RMS norm (ADD + RMS_NORM + MUL, RMS_NORM + MUL) or a SWIGLU whose output is read next by an MMQ matmul also writes that reader's q8_1 rows, so the reader skips its quantize launch. The rows go into a Q8 memo entry (#948) created at the producer's node. Always on where the Q8 memo is; bit-exact.
- Row writer (
norm.cu). It isquantize_mmq_q8_1's body, statement for statement, so the bytes match the reader quantizing the F32 output itself. - Which reader (
glue-q8-memo.h, pure).glue_q8_first_readerfinds the first MMQ reader of the producer's output inside the memo window; the entry is keyed, sized and planned as the reader's own entry would be. - Dispatch (
ggml-cuda.cu,mmq.cu).ggml_cuda_mul_mat_takes_mmqtells whether a matmul takes MMQ;ggml_cuda_glue_q8_memo_producecreates the entry. Where the memo serves nothing (a VMM pool, concurrent streams) the producers stay inert and the readers quantize. ggml_backend_cuda_set_glue_q8_producers_override(tests only) turns it off;ggml_backend_cuda_get_glue_q8_producer_countcounts producer writes.
Evidence
Throughput, measured inside the #964 stack on the R9700 (Qwen3.8-27B UD-IQ4_XS, DFlash2 Q8_0 drafter, C4, ABBA×2, steps/s paired by request, runtime switches since removed):
| change | steps/s at C4 |
|---|---|
| RMS-norm q8_1 rows | +0.64% (±0.51) |
| SWIGLU q8_1 rows | +0.34% (±0.36) |
Not measured on main alone. The SWIGLU gain is within noise.
Tests (R9700, all pass on this head):
test_glue_norm_q8_gpu: 1008 kernel cases byte-identical toquantize_mmq_q8_1(D4, DS4, D2S6, 1..64 rows, padded and unpadded). One graph with four norm producers and a SWIGLU is bit-identical with producers off, on and on without the memo (60 outputs across 3 arms). At 16 columns, memo hits go from 4 to 9 with 5 producer writes.test_glue_fusions_gpu(#948's) expects one more memo hit per producer write and stays bit-identical across its 5 arms.test_glue_fusions:glue_q8_first_readercases.test_mmq_tile_twins: pass.
Merge Danger
Door: two-way. No format, API or file change; reverts cleanly; bit-exact against the readers quantizing.
Blast Radius: wherever the #948 Q8 memo is active (CUDA/HIP legacy pool, one stream) and a norm or SWIGLU feeds an MMQ, on any model. With a VMM pool they stay inert, as the memo does.
Known limits (from the #964 review, not addressed here):
- When a dense gate/up pair is fused into the paired path, its norm still writes q8_1 rows that nothing reads. This costs time on those graphs, never bits; the cost is unmeasured.
- The 1024-thread producer kernels carry no
__launch_bounds__. - Every tested row length is a multiple of 32, so a partial 32-value group is untested.
test_glue_norm_q8_gpu.cpprepeats much oftest_glue_fusions_gpu.cpp's setup; folding them and sharing the row writer withquantize_mmq_q8_1would save roughly 600 lines.
🤖 Generated with Claude Code