Skip to content

perf(ggml-cuda): RMS norms and SWIGLU write their MMQ reader's q8_1 rows - #969

Closed
Graffioh wants to merge 1 commit into
Luce-Org:mainfrom
Graffioh:perf/q8-1-producers
Closed

Graffioh wants to merge 1 commit into
Luce-Org:mainfrom
Graffioh:perf/q8-1-producers

Conversation

@Graffioh

@Graffioh Graffioh commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

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 is quantize_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_reader finds 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_mmq tells whether a matmul takes MMQ; ggml_cuda_glue_q8_memo_produce creates 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_count counts 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 to quantize_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 (perf(qwen35): fewer glue launches in the target graph (q8_1 memo, strided gates, gate|up stack) #948's) expects one more memo hit per producer write and stays bit-identical across its 5 arms.
  • test_glue_fusions: glue_q8_first_reader cases.
  • 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.cpp repeats much of test_glue_fusions_gpu.cpp's setup; folding them and sharing the row writer with quantize_mmq_q8_1 would save roughly 600 lines.

🤖 Generated with Claude Code

View guided diff

At verify width, the RMS-norm producers (ADD + RMS_NORM + MUL,
RMS_NORM + MUL) and the SWIGLU of the FFN and GDN output feed MMQ
matmuls. Each of those matmuls quantizes the producer's F32 output with
its own quantize_mmq_q8_1 launch. The Q8 memo shares one quantize per
layout, but the first reader still pays for it: about 97 quantize
launches per C4 round for SWIGLU alone.

Now the producer also writes those q8_1 rows, in the layout of its first
MMQ reader, into a Q8 memo entry created at the producer's node. That
reader and the later readers of the same layout skip their quantize.

- norm.cu:
  - norm_row_q8_1_mmq is quantize_mmq_q8_1's body, statement for
    statement, run on the finished row after a block barrier. The norm
    reductions and F32 outputs are unchanged. D4, DS4 and D2S6 instances.
  - swiglu_q8_mmq_kernel computes op_silu(x) * g as unary_gated_op_kernel
    does, then the same row writer.
- mmq.cu: ggml_cuda_glue_q8_memo_produce registers the entry with the
  key, size and scan plan the reader's luce_glue_q8_acquire would use,
  under the same conditions (memo on, legacy pool, one stream).
- glue-q8-memo.h: glue_q8_first_reader, the first later MUL_MAT that
  reads the output through plain MMQ before anything may rewrite it.
- ggml-cuda.cu: ggml_cuda_mul_mat_takes_mmq mirrors ggml_cuda_mul_mat's
  dispatch. RMS_NORM + MUL keeps its MMVQ q8_1 rows where it writes them.

On by default. ggml_backend_cuda_set_glue_q8_producers_override (tests
only) turns it off, and ggml_backend_cuda_get_glue_q8_producer_count
counts the writes.

Tests:
- test_glue_norm_q8_gpu:
  - kernel arm: bytes vs quantize_mmq_q8_1 for D4, DS4 and D2S6, over 8
    row lengths and 1..64 rows.
  - graph arms: one graph with four norm producers and a SWIGLU,
    bit-identical with producers off, on, and on without the memo, with
    exactly the expected writes and memo hits.
- test_glue_fusions: three glue_q8_first_reader cases.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant