Skip to content

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

Merged
davide221 merged 2 commits into
Luce-Org:mainfrom
Graffioh:perf/qwen35-glue-fusions
Nov 8, 2026
Merged

davide221 merged 2 commits into
Luce-Org:mainfrom
Graffioh:perf/qwen35-glue-fusions

Conversation

@Graffioh

@Graffioh Graffioh commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

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:

suite main this PR
gsm8k 100.3 102.8 (+2.5%)
math500 104.9 107.5 (+2.5%)
humaneval 80.5 82.5 (+2.5%)
mbpp 93.6 95.9 (+2.6%)
mt-bench 73.7 75.6 (+2.5%)
all 89.81 92.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 (ce11425) against main f3cc3ec, 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 34b2087 and f3cc3ec.
  • Earlier run: an interleaved run of the same change on c5179e4 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:

arm tok/s vs before
before 89.86
built, all three off 89.84 −0.02%
Q8 memo 90.66 +0.89%
strided gates 90.50 +0.72%
gate|up stack 90.82 +1.07%
all three 92.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 span launches
before 65.12 ms 2091
Q8 memo 64.31 ms 1942 (−149)
strided gates 64.57 ms 1966 (−125)
gate|up stack 64.57 ms 2019 (−72)
all three 63.04 ms 1781 (−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

@Graffioh
Graffioh force-pushed the perf/qwen35-glue-fusions branch from 1cb3eb5 to ce11425 Compare November 6, 2026 08:42
@Graffioh
Graffioh marked this pull request as ready for review November 6, 2026 08:43

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 18 files

Heads up: you’re close to your included review allowance. Set a flex budget so reviews don’t pause.

View guided diff | Re-trigger cubic

Comment thread server/deps/llama.cpp/ggml/src/ggml.c Resolved

@davide221 davide221 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review

No issues found. Checked for bugs and logic errors in the changed code.

Reviewed ce11425c · 0 candidate issues checked, kept at ≥70% confidence · add the code-review label to review again

…ided gates, gate|up stack)

Three byte-identical fusions on the Qwen3.5/3.6/3.8 target, always on where
their rules allow them:

- Q8 memo (ggml-cuda MMQ): a matmul whose F32 activation is read again later
  in the same graph by a matmul of the same q8_1 layout keeps its quantized
  copy, and the later calls skip their quantize launch. Entries end at the
  first node that may write those bytes and never outlive a graph compute.
- Strided gates: on the dense single-sequence DeltaNet chain the GDN kernel
  reads beta/alpha, and the gated norm reads z, in place from the stacked
  projections instead of contiguous copies.
- Gate|up stack: the loader places ffn_gate|ffn_up back to back, and the FFN
  runs one GEMM over the stack wherever the backend vouches that every row
  matches the separate GEMMs (MMQ without stream-k, rows a multiple of 128).

There is no runtime switch. The GPU test turns the fusions off through the
test-only ggml_backend_cuda_set_q8_memo_override and
luce::qwen35::glue::set_fusions_for_test.

It also drops GGML_CUDA_DISABLE_FUSION: the graph-node fusions always run,
as they did by default, and tests turn them off with
ggml_backend_cuda_set_fusion_disabled_override.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
main now has Luce-Org#946. server/CMakeLists.txt: both PRs added a unit test to the
same two target lists and the same block; keep both (test_gdn_single_commit,
then test_glue_fusions), as on the stacked branch of Luce-Org#964.
@davide221

Copy link
Copy Markdown
Contributor

Merged main (now with #946) into the branch. The only conflict was server/CMakeLists.txt, where both PRs add a unit test to the same target lists; I kept both. The merged tree matches 47ecdee, your stacked commit of this PR on #946 in #964.

@davide221
davide221 merged commit ff0867a into Luce-Org:main Nov 8, 2026
11 checks passed
Graffioh added a commit to Graffioh/lucebox-hub that referenced this pull request Nov 9, 2026
Two copies per GDN layer in the concurrent verify, 96 launches per C4
round, are gone:
- The tree commit reads the replay log through the view of the GDN result,
  as SINGLE_REPLAY's rollback already does, instead of a compact copy. The
  view has the copy's [width, H, T, seqs] layout, and the GDN result stays
  alive past the compute.
- The mapped verify's z goes into the fused silu*mul as a strided view of
  the attn_gate|qkv stack (Luce-Org#948's strided gates), instead of a contiguous
  copy. Its GDN output has the same contiguous rows as the chain's.

Both read the same values, so outputs are bit-identical.

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.

2 participants