Skip to content

perf(qwen4exp): fewer target launches in split decode - #937

Merged
davide221 merged 12 commits into
mainfrom
perf/split-tensor-parallel
Nov 6, 2026
Merged

davide221 merged 12 commits into
mainfrom
perf/split-tensor-parallel

Conversation

@davide221

@davide221 davide221 commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

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):

  • The router's weight renormalization (GET_ROWS, SUM_ROWS, CLAMP, DIV) in one launch, following block_reduce's WARP_SIZE-lane butterfly so the bits match.
  • The shared expert's sigmoid gate folded into the MoE combine.
  • HC_COMBINE_NORM, the HC mix reduce and a gated unary (any model) write the q8_1 rows the next quantized matmul would make, so its quantize launch goes.
  • SCALE then SILU in one launch (any model), also writing the next matmul's q8_1 rows.
  • One query row reaches flash attention as the permuted view instead of a copy (qwen4exp_graph.cpp).
  • ggml-cpu's argmax picks what the GPU argmax picks since perf(qwen4exp): faster split mode on R9700 + Strix Halo #933: the host scan's choice, the lowest id of the row's maximum.

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):

code prose 2K-token document
one user, 131K context, --expert-placement: main 62.9 (56.1-66.7) 57.3 (56.2-58.0) 51.9 (50.5-53.3)
this PR 66.8 (66.1-68.1) 58.4 (57.4-59.2) 54.3 (52.9-54.9)
four users, default launch: main 43.0 (42.6-43.3) 47.1 (45.4-48.3) 36.7 (36.1-37.2)
this PR 44.5 (43.9-45.0) 48.9 (48.2-49.2) 38.3 (37.8-38.7)

The 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.
  • The ROCm GPU job's steps (same build flags and test list, draft top-K, capture-safe copies, DSpark load, model smoke, affine ROCmFP2 build) on main with this PR, fix(qwen4exp): keep MTP drafting while a thinking budget is open #935 and perf(deepseek41): two-box decode switches in ds41-r9700-gorgon #936 applied together, run by hand on an R9700 and a Strix Halo while the shared runner queue kept cancelling the job: all pass, 98 of 98 ctest cases on each GPU (the skips are the multi-GPU cases).

Follow-ups (separate PRs)

  • Since perf(qwen4exp): faster split mode on R9700 + Strix Halo #933, greedy text with --draft differs from --verify-width 1 on 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 in qwen4exp_graph.h describes.
  • Late experts (a layer's cold experts join one layer late, so the GPUs overlap): they change the arithmetic, so as a flag with a quality gate.
  • Routing stats for --expert-placement from a serving run.

🤖 Generated with Claude Code

davide221 and others added 3 commits November 4, 2026 01:05
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]>
@davide221 davide221 changed the title perf(qwen4exp): overlap the two GPUs in split decode (late experts, draft) perf(qwen4exp): faster two-GPU split decode (overlap, fewer launches, draft) Nov 4, 2026
davide221 and others added 4 commits November 4, 2026 04:43
…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 davide221 changed the title perf(qwen4exp): faster two-GPU split decode (overlap, fewer launches, draft) perf(qwen4exp): faster two-GPU split decode Nov 4, 2026
@davide221
davide221 marked this pull request as ready for review November 4, 2026 18:58

@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 23 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread server/src/qwen4exp/qwen4exp_graph.h Resolved
Comment thread server/CMakeLists.txt Resolved

@davide221 davide221 left a comment

Copy link
Copy Markdown
Contributor Author

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 6cb5384c · 1 candidate issue checked, kept at ≥70% confidence · add the code-review label to review again

davide221 and others added 3 commits November 4, 2026 20:13
- 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]>
@davide221 davide221 changed the title perf(qwen4exp): faster two-GPU split decode perf(qwen4exp): fewer target launches in split decode Nov 5, 2026
davide221 and others added 2 commits November 5, 2026 18:53
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]>
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