Repository navigation
perf(qwen4exp): fewer target launches in split decode - #937
Merged
Merged
Conversation
In the split the target GPU and the expert GPU take turns: each layer the target runs attention and the router, hands the activations over, and waits while the expert device runs the routed experts. Each device idles for about a third of decode. LUCE_QWEN4EXP_LATE_EXPERTS=all|<a>-<b> lets the routed sum of a layer join one layer late: the target runs the next layer's attention while the expert device runs the experts, and the routed sum is added to the residual before the next layer's FFN and router. Only that next attention misses it. The graph order (attention queued first, then a copy on the expert device) makes the scheduler overlap the two. The last layer is never late, and prompt chunks skip the pipelined prefill while it is on. This changes the model's arithmetic, so it is off by default. LUCE_QWEN4EXP_ROUTING_STATS_OUT=<csv> records every prompt chunk's top-k picks per layer into a routing-stats CSV for --expert-placement. Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Q8_0 MMVQ, short rows over many rows (the hyper-connection up projection, K = 320 into 10240 rows): one wave walks all eight virtual waves of a row pair, four row pairs per block. Same arithmetic as the eight-wave kernel, 1.5-3x faster at 1-8 columns. LUCE_CUDA_MMVQ_Q8_RDNA4_SMALL_K=0 keeps the old shape. - The RDNA4 Q8_0 gate/up pair also serves plain SwiGLU (the shared expert): SwiGLU-DS4 with an infinite limit. LUCE_CUDA_RDNA4_SWIGLU_PAIR=0 keeps the separate launches. - SCALE then SILU (the hyper-connection low-rank gate) in one launch that also writes the q8_1 form the next quantized matmul reads. LUCE_CUDA_FUSE_SCALE_SILU=0 keeps the two kernels. - LUCE_CUDA_MMVQ_MOE_ALL_WIDTHS=1 (opt-in): inside the batch-invariant scope the one-wave MoE kernel serves every width, one token included, instead of the tokenwise launch that reads every hot expert once per verify token. Decode and verify keep the same arithmetic. - bench_ds41_q8_mmvq: BENCH_MAX_COLS and the Qwen3.8-Flash-Next shapes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… one-launch late join - Decode and verify graphs compute each logits row's argmax on the device. Greedy requests with no logit processing read those ids instead of the rows (a few bytes instead of 600 KB per row over the R9700's PCIe link) and fetch the last emitted row only when generation ends. Sampled requests read the rows as before. - The sampler scans a greedy row in place instead of copying the whole vocabulary first. - A single-token decode runs inside the batch-invariant scope, so with LUCE_CUDA_MMVQ_MOE_ALL_WIDTHS its hot-expert products match a verify row's: text with --draft equals text without it. - The late-experts join (LUCE_QWEN4EXP_LATE_EXPERTS) uses the fused hyper-connection combine-norm op instead of six elementwise kernels. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…PU split Each fusion writes the bits the unfused nodes write; LUCE_CUDA_QN_FUSE=0 turns them all off. - Router weights: GET_ROWS of the probabilities by the picks, SUM_ROWS, CLAMP and DIV in one launch. - Shared expert gate: SIGMOID and MUL folded into the MoE combine kernel. - q8_1 producers: HC_COMBINE_NORM, the HC mix reduce and the gated unary write the q8_1 rows the next MMVQ matmul would quantize (under the q8 memo key), so its quantize launch goes. - Combine pair: a late-expert join (an HC_COMBINE_NORM whose normalized stream is unread) folds into the FFN combine that reads its residual, one launch for two. Target kernels per layer 45.4 -> ~36 in a decode profile; decode +1.5-2.5% on the late-expert split (the expert device's experts set the pace there). Co-Authored-By: Claude Opus 5.5 <[email protected]>
… Q as a view - The late-expert join no longer adds a scale node on the expert device to close its split: the scheduler already ends the target's split before the join, so the routed sum crosses as it is. One graph launch fewer on the expert device's critical path per layer (+2% decode). LUCE_QWEN4EXP_LATE_PIN=1 restores the node. - One query row reaches flash attention as the permuted view, not a copy (the decode kernels read Q through its strides; same values). LUCE_QWEN4EXP_Q_CONT=1 keeps the copy. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The real MUL_MAT_ID dispatch (gate/up with SwiGLU, then down) at split decode and verify widths, with `masked` routes per token left to the other device, consecutive-token expert sharing and a footprint knob (layers x experts). Prints time per layer, bandwidth over the distinct experts' bytes and an output hash to compare env settings. On the Strix Halo expert device: 166 GB/s at 2 tokens x 5 live routes against 225 GB/s for a dense matvec of the same weight type, whatever the footprint (1.5 to 35 GB). Co-Authored-By: Claude Opus 5.5 <[email protected]>
The split decode speedups in this PR now need no environment variable: - The exact fusions and RDNA4 kernel shapes are always on. Their kill switches (LUCE_CUDA_QN_FUSE, LUCE_CUDA_FUSE_SCALE_SILU, LUCE_CUDA_RDNA4_SWIGLU_PAIR, LUCE_CUDA_MMVQ_Q8_RDNA4_SMALL_K) are gone; test_qn_split_fusions checks each against its unfused graph instead. - The one-wave MoE kernel at every width is set by the caller that needs it: ggml_backend_cuda_set_mmvq_moe_all_widths, which the qwen4exp split forwards set with the batch-invariant scope (Qwen4ExpMatmulScope). It stays off for DeepSeek4, whose verify must match a decode that runs outside that scope. Replaces LUCE_CUDA_MMVQ_MOE_ALL_WIDTHS. - Late experts change the model's arithmetic, so they leave this PR with the late join, its HC_COMBINE_NORM pair fusion and the routing-stats dump (LUCE_QWEN4EXP_LATE_EXPERTS, LUCE_QWEN4EXP_LATE_PIN, LUCE_QWEN4EXP_ROUTING_STATS_OUT); they come back as their own PR with a quality gate. LUCE_QWEN4EXP_Q_CONT goes too: one query row always reaches flash attention as the permuted view. - bench_qn_moe moves to the expert-device kernel follow-up. Also fixes the device argmax that greedy decode now reads: ties went to whichever lane kept its own candidate, not the lowest index, so a tie could pick another token than the host scan. Ties now go to the lowest index, as in the CPU backend and std::max_element. test_qn_split_fusions: every fusion and RDNA4 shape against its unfused graph on every GPU, bit for bit, with fusion counters (ggml_backend_cuda_get_qn_fusion_count) so a pattern that stops matching fails the test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
davide221
marked this pull request as ready for review
November 4, 2026 18:58
Contributor
There was a problem hiding this comment.
All reported issues were addressed across 23 files
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
davide221
commented
Nov 4, 2026
davide221
left a comment
Contributor
Author
There was a problem hiding this comment.
Code review
No issues found. Checked for bugs and logic errors in the changed code.
Reviewed 6cb5384c · 1 candidate issue checked, kept at ≥70% confidence · add the code-review label to review again
- ggml-cpu's argmax breaks ties to the lowest index, as the GPU argmax and the host greedy sampler do; test_qn_split_fusions checks the CPU backend too. - test_qn_split_fusions runs in the ROCm and NVIDIA GPU jobs. - bench_ds41_q8_mmvq measures widths 1-8 by default. - Says why decode and verify graphs build the rows' argmax for every caller: building it only for greedy callers rebuilt the retained graph mid-request and the text with a draft no longer matched the text without one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The router renorm matches block_reduce's xor butterfly bit for bit, so its lane count and top-k limit are WARP_SIZE, not a literal 32. Co-Authored-By: Claude Opus 5.5 <[email protected]>
#933 landed the RDNA4 one-wave MoE kernel at every width, the short-row Q8_0 shape, the SwiGLU gate/up pair, the host greedy scan and device argmax picks. This branch keeps main's versions of those and drops its own: the all-widths switch, its matmul scope and their test cases. Co-Authored-By: Claude Opus 5.5 <[email protected]>
One case per fusion, each run on every GPU, so ctest lists and reruns them one by one. CI selects them by the test_qn_split_fusions. prefix. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A producer registered q8_1 rows whenever a quantized matmul read its output next. MMVQ keys the memo on the row length it reads, so rows reshaped longer (the GDN gated output read as [D*H, T]) never matched, and no matmul wider than MMVQ's column limit reads the memo at all: the writes were wasted and each entry held its buffer for the whole prompt chunk. Both now get no entry. The route-norm matcher also declines a gathered-weights reshape marked as an output, and the shared-gate matcher declines an operand the combine cannot read as float4 rows instead of asserting. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Nov 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fewer kernel launches on the R9700 (the target) of the Qwen3.8-Flash-Next two-GPU split, on top of #933. Every fusion computes the same bits as the ops it replaces, so this PR's text equals main's byte for byte. No new environment variables or flags.
What changes
In the qwen4exp graph profile unless noted (
ggml-cuda.cu,moe-fused-combine.cu,hc-cn.cu,hc-mix.cu,unary.cu):block_reduce'sWARP_SIZE-lane butterfly so the bits match.qwen4exp_graph.cpp).Measured
Qwen3.8-Flash-Next UD-Q4_K_XL, MTP draft, greedy, R9700 + Strix Halo. Same box and session, main (34b2087) and this PR alternating, two server starts each with two runs per prompt; decode tok/s, mean (min-max):
--expert-placement: mainThe text is identical to main's in all 24 replies (12 per launch).
Tests
test_qn_split_fusions(new, in the ROCm and NVIDIA GPU jobs): each fusion built as the qwen4exp graph builds it, computed as one graph and in segments a fusion cannot cross (node by node, or a q8_1 producer apart from its matmul): equal bits, and the fusion counters (ggml_backend_cuda_get_qn_fusion_count) show each fusion ran in the one-graph run only. The argmax, on every GPU and on the CPU backend, against a host scan of rows full of ties.Follow-ups (separate PRs)
--draftdiffers from--verify-width 1on this split (6 of 6 replies here, on main and on this PR alike): a single-token decode and a verify column no longer take the same kernels somewhere on the target. Worth restoring, as the verify contract inqwen4exp_graph.hdescribes.--expert-placementfrom a serving run.🤖 Generated with Claude Code