perf(qwen35): fewer glue launches in the target graph (q8_1 memo, strided gates, gate|up stack)

Merged
GR
1mo ago
+2340-40

Summary

Three byte-identical fusions on the Qwen3.5/3.6/3.8 target graph remove about 300 of the ~2,090 launches in a DFlash verify round. None has a runtime switch: each is always on where its rules allow it.

per verify round (Qwen3.8-27B, R9700, 16-token chain verify)
                      before                         after
q8_1 quantize         one per MMQ call               one per activation; later MMQ readers
                                                     of the same layout reuse it (Q8 memo)
beta / alpha / z      ggml_cont of strided slices    read in place from the stacked
                      of the stacked projections     projections (strided gates)
ffn_gate, ffn_up      two GEMMs                      one GEMM over a zero-copy gate|up
                                                     stack, split by row views
  • Q8 memo (ggml-cuda/mmq.cu, rules in glue-q8-memo.h):
    • A dense MMQ call whose F32 activation is read again later in the same graph, by a matmul of the same q8_1 layout (D4, DS4 or D2S6), keeps its quantized copy. The later calls skip their quantize launch.
    • An entry ends at the first node that may write those bytes, and never outlives a graph compute.
    • Concurrent streams, VMM pools and fused groups keep the per-call quantize.
  • Strided gates (qwen35/glue_fusions.h). On the dense single-sequence DeltaNet chain (single-request prefill and capture verify), the GDN kernel reads beta/alpha in place from the stacked ssm_beta|alpha result (ggml_gated_delta_net_shared_gates). The gated norm reads z in place from the stacked attn_gate|qkv result, through the fused silu*mul kernel.
  • Gate|up stack. At load, the zero-copy stacking also places ffn_gate|ffn_up back to back. The FFN runs one GEMM over the stack wherever ggml_backend_cuda_row_stack_inexact vouches that every row matches the separate GEMMs: MMQ without stream-k, more columns than MMVQ takes, and both members a multiple of 128 rows.

The PR also removes GGML_CUDA_DISABLE_FUSION. ggml-cuda's graph-node fusions always run now, as they did when it was unset. Tests turn them off with the test-only ggml_backend_cuda_set_fusion_disabled_override: the DS4 grouped-MMID test's legacy arm moved to it. The strided-gates rule asks ggml_backend_cuda_fusion_enabled(), so it never hands a strided z to the unfused silu.

server/deps/llama.cpp/ggml/
  src/ggml-cuda/glue-q8-memo.h      + which later matmuls may share a q8_1 entry (pure, unit-tested)
  src/ggml-cuda/glue-q8-memo.cuh    + on/off query (tests) + end-of-graph release
  src/ggml-cuda/mmq.cu              ~ acquire / reuse / release around the quantize
  src/ggml-cuda/ggml-cuda.cu        ~ node index for the memo; row_stack_inexact;
                                      GGML_CUDA_DISABLE_FUSION -> test hook
  src/ggml-cuda/gated_delta_net.cu  ~ strided beta/alpha
  include/ggml.h, src/ggml.c        + ggml_gated_delta_net_shared_gates
server/src/qwen35/
  glue_fusions.h                    + plans (pure, unit-tested), test overrides
  gguf_target_loader.cpp            ~ gate|up zero-copy stack
  qwen35_target_graph.cpp           ~ stacked FFN GEMM; in-place gates and z

Evidence

Throughput, R9700. Setup:

  • Qwen3.8-27B UD-IQ4_XS target, DFlash2 Q8_0 drafter, --profile qwen38-r9700, greedy, thinking on, max_tokens 2048, one request at a time.
  • 30 prompts, 6 each from gsm8k, math500, humaneval, mbpp and mt-bench.
  • Main (34b20876f) against this branch, interleaved ABBA twice: 8 server runs, each with a warm-up request. These runs predate the rebase onto f3cc3ec9c, whose three commits (#935–#937) change qwen4exp and DeepSeek 4.1 code.
  • Values are tokens/s per request, geometric mean per suite.
suitemainthis PR
gsm8k100.3102.8 (+2.5%)
math500104.9107.5 (+2.5%)
humaneval80.582.5 (+2.5%)
mbpp93.695.9 (+2.6%)
mt-bench73.775.6 (+2.5%)
all89.8192.08 (+2.52%)
  • Noise: the four main runs span 89.79 to 89.82, and each block shows the same gain (+2.53% and +2.51%).
  • Identity: every run produced 26,327 tokens and 30/30 outputs identical to main by token ids. This holds in the shipped config, and also with the AR burst pinned off (main 89.83, this PR 92.08), identical. The build without the runtime switches, run the same way against main, also gave 30/30 identical (89.75 → 91.95).
  • After the rebase: this exact commit (ce11425c8) against main f3cc3ec9c, AR burst pinned off, single sequential runs: 30/30 identical, 26,327 tokens each, 90.23 → 92.46 tok/s (+2.5%). Main itself got about 0.5% faster between 34b20876f and f3cc3ec9c.
  • Earlier run: an interleaved run of the same change on c5179e4b5 measured +2.53%.

Concurrency 4. Setup: paged attention, --max-concurrency 8, 4 clients in a closed loop, 40 prompts × 1024 tokens, ABBA twice.

  • Per-request rate goes from 55.89 to 56.62 (+1.30%). Both blocks are positive (+1.63% and +0.96%), and there are 0 errors.
  • Outputs at C > 1 are not compared byte for byte: the engine is not batch-invariant, so even two runs of main differ on most prompts.

Identity and per-fusion gains (measured while the fusions still had runtime switches). Setup: R9700, Qwen3.8-27B UD-IQ4_XS + DFlash2 Q8_0 drafter, greedy, thinking on, max_tokens 2048, 30 prompts (6 each from gsm8k, math500, humaneval, mbpp, mt-bench), one request at a time, AR burst pinned off for strict identity. Sequential runs, tokens/s geometric mean:

armtok/svs before
before89.86
built, all three off89.84−0.02%
Q8 memo90.66+0.89%
strided gates90.50+0.72%
gate|up stack90.82+1.07%
all three92.12+2.52%

Every arm gave 30/30 outputs identical to "before" by token ids.

Where the time goes. A rocprofv3 trace of 5 requests, steady rounds:

round spanlaunches
before65.12 ms2091
Q8 memo64.31 ms1942 (−149)
strided gates64.57 ms1966 (−125)
gate|up stack64.57 ms2019 (−72)
all three63.04 ms1781 (−310)

At verify width (16 columns), one stacked gate|up GEMM against two separate ones: IQ4_XS 191.8 vs 210.2 µs (−8.8%), IQ3_S 251.5 vs 287.3 µs (−12.5%), Q5_K 233.8 vs 246.6 µs (−5.2%). The output hashes are equal.

Correctness.

  • test_glue_fusions (CPU, 23 cases) covers the memo's sharing window: in-place writes, overlapping nodes, non-matmul readers, views, reshapes and the scan bound. It also covers the strided-gate and gate|up plans on every path.
  • test_glue_fusions_gpu runs 5 arms (all off, each alone, all on) in child processes. All 26 outputs and 10 pair checks are bit-identical across the arms. The memo hits exactly where glue-q8-memo.h allows (4 + 12). The tiny target's verify drops CONT 5 → 2 and MUL_MAT 14 → 12, and tree verify keeps its copies.
  • The 7 DS4 grouped-MMID tests pass, with their legacy arm moved to the fusion override.
  • test_server_unit: 1218 passed, 0 failed.

Merge Danger

Door: two-way, by revert. There is no runtime switch any more, and no file format or API change outside ggml-cuda and qwen35. The new exports, ggml_backend_cuda_set_q8_memo_override, ggml_backend_cuda_set_fusion_disabled_override and luce::qwen35::glue::set_fusions_for_test, are test hooks. GGML_CUDA_DISABLE_FUSION is no longer read: anyone who set it, on any model and device, now runs the fused kernels.

Blast Radius: medium.

  • The Q8 memo lives in ggml-cuda MMQ, so it applies to every model's graphs on CUDA/HIP, not only Qwen. Its rules are generic and byte-identical, but its throughput gain was measured on Qwen3.8 only.
  • The other two only touch the Qwen3.5/3.6/3.8 target. The loader's gate|up stack changes the weight placement (still zero-copy aliases, no extra memory).
  • Paths a fusion does not serve keep the current graph: tree verify, concurrent, compact decode, chunked, SpecLA, stream-k devices, and tensor-parallel loads.

🤖 Generated with Claude Code