Skip to content

perf(qwen4exp): faster split mode on R9700 + Strix Halo - #933

Merged
davide221 merged 12 commits into
mainfrom
perf/qwen4exp-split-speed
Nov 4, 2026
Merged

davide221 merged 12 commits into
mainfrom
perf/qwen4exp-split-speed

Conversation

@davide221

@davide221 davide221 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Faster Qwen3.8-Flash-Next in split mode (dense layers on an R9700, routed experts on a Strix Halo) and on the Strix Halo alone, with fixes for stalls and out-of-memory on long prompts.

  • Expert device memory. ggml gave the Strix Halo's 71.7 GiB expert buffer managed memory, which the kernel swapped out and the GPU faulted back in mid-prompt (a 32K prompt took up to 58 s instead of 17 s). The expert device now gets device memory (qwen4exp_loader.cpp).
  • Block radix top-k on RDNA4. The device-wide hipCUB sort kept four copies of the scores in the pool and ran an R9700 holding hot experts out of memory on 80K+ prompts. The block radix top-k selects the same ids in the same order and is faster at every row count (R9700, 4 rows of 8192 scores: 0.05 vs 0.16 ms; 4096 rows: 2.3 vs 6.7 ms).
  • Anchored split-input copies (ggml_backend_sched_set_anchored_copies). A prompt chunk's cross-device copies live from the split that sends them to their last reader, not for the whole chunk: a 16384-row chunk's R9700 workspace drops from 8.5 GB to 3.5 GB. The hot experts then take the memory the fitted context and the largest chunk leave (11.6 GiB at 131K context), instead of a fixed 8 GiB with the chunk held at 8192 rows.
  • RDNA4 short rows. MUL_MAT_ID with short expert rows (qwen4exp's Q4_K gate/up rows of 10 blocks, Q5_1 down rows of 20) ran at about a quarter of the card's bandwidth on the eight-wave launch; it now runs on the one-wave MoE kernel at every width. Q8_0 rows of at most 24 blocks (the hyper-connection up projection, the shared expert's down projection) use a one-wave block shape, bit-identical. The fused RDNA4 Q8_0 gate/up pair also takes plain SwiGLU (the shared expert).
  • Wider prompt chunk on one GPU. The chunk widens from 4096 toward 8192 rows while one full-context prefix snapshot still fits (Strix Halo at 131K context: 5,376 rows).
  • Prompt-lookup drafts beside adaptive MTP (common/prompt_lookup.h): each round verifies either the MTP drafts or the tokens that followed an earlier copy of the current suffix, whichever is expected to commit more tokens per ms. The rule and its priors follow Strata's draft policy (github.com/Niko1221/Strata, MIT).
  • Greedy sampling without penalties scans the logits row already on the host instead of uploading it to the GPU again.
  • Tokens picked on the device. A verify no longer copies every logits row to the host (5 rows = 5 MB of 248K-vocab logits, 2-3 ms per step over a narrow PCIe link). Greedy requests read a device argmax, one id per row. Sampled requests with a top_k, which is the model card's default (thinking and non-thinking recipes), read each row's top 32 or 64 candidates from a device top-k; sample_logits_top draws from them the token and RNG value the whole row gives, or declines before drawing (a tie at the cut, a NaN, a penalty that could lift an outside token) and the row is read then. Other sampled requests read only the rows they sample, one at a time.
  • Grouped expert matmul for Q5_1 on RDNA3. The Strix Halo read each cold expert's Q5_1 down projection once per (token, expert) pair; the grouped MUL_MAT_ID kernel reads it once per verify batch, about 24% faster on that projection and bit-exact. It is now the HIP default for Q5_1 on RDNA3.5 only (Q4_1/Q5_1 join the grouped kernel as type mask bit 128), within the arch's existing MMVQ width ceiling; every other HIP format, RDNA3.0 and RDNA4 keep today's path.

Results

R9700 + Strix Halo, Qwen3.8-Flash-Next UD-Q4_K_XL, 131K context, MTP draft, --expert-placement with a routing ranking, greedy. Median of 3 runs, same prompts for both builds, measured the same day. Decode tok/s, prompt tok/s in brackets.

Prompt main this PR
short reply (57 tokens) 72.6 79.6
edit, 8K 72.1 (1,446) 90.0 (1,764)
explain, 8K 56.6 (1,447) 63.4 (1,801)
explain, 32K 56.8 (1,695) 59.2 (1,837)

Device token picks, A/B on the same build otherwise, held-out prompts, same seed (decode tok/s):

Prompt rows read on the host picks on the device
short reply, greedy 77.3 81.5
explain 8K, greedy 66.8 70.4
explain 32K, greedy 65.7 69.0
short reply, card sampling (thinking) 78.2 81.0
explain 8K, card sampling (thinking) 59.2 61.5
explain 32K, card sampling (thinking) 56.9 59.0
summarize 8K, card sampling (non-thinking) 53.2 55.0

Grouped Q5_1 experts on the Strix Halo, split mode, back to back with LUCE_MMID_GROUPED=0 (order off, on, on, off; mean of two runs each). Decode tok/s, prompt tok/s in brackets:

Prompt off on
short reply 79.7 82.1
chat 45.5 45.5
explain, 8K 63.4 (1,730) 63.3 (1,734)
explain, 32K 64.6 (1,808) 65.3 (1,811)
summarize, 8K 57.0 (1,602) 59.0 (1,599)

Strix Halo alone and the 100K prompt: results to follow in a comment.

Exactness

  • Within this branch, each change was A/B'd on the same prompts with identical greedy text.
  • Against main, split-mode text diverges after a few hundred tokens (word choices such as "previous helper" for "helper function"): more experts now run on the R9700, and its short expert rows use the MoE kernel, whose sums are ordered differently from the eight-wave launch.
  • The Q8_0 short-row block shape and the plain-SwiGLU fused pair are bit-identical to the kernels they replace (bench_ds41_q8_mmvq hashes against the eight-wave launch, test_ds4_fused_ops_cuda).
  • The greedy host scan picks the same token as the GPU path (lowest id on ties).
  • Device picks: identical text on 24 greedy and 36 sampled held-out prompts (same seed). The top candidates settled every sampled draw (0 row reads in 9,306 draws). ggml_argmax on CUDA/HIP now breaks ties toward the lowest id and returns 0 for an all-NaN row, as the host scan does.
  • Grouped Q5_1: identical text with the kernel on and off (15 of 15 split prompts in each pair, 9 of 9 on the Strix Halo alone).

Tests

On an R9700 + Strix Halo build of this branch (gfx1151 + gfx1201):

  • test_server_unit: 1,217 passed, 0 failed (adds test_prompt_lookup, including the probe at the draft model's own width, qwen4exp_reserved_rows cases, the greedy scan case, top_candidates_draw_like_the_row: the candidate draw equals the whole-row draw with ties, penalties and NaN).
  • test_ds4_fused_ops_cuda on the R9700 and the Strix Halo: 0 failures (adds plain-SwiGLU pairs, and device top candidates for 248,320 / 151,936 / 8,193 columns x k 32 / 64 / 128, ties across the cut included).
  • test_deepseek4_mmid_grouped_cuda on the R9700 and the Strix Halo: parity PASS, 109 cases, 135 grouped (adds Q4_1 and Q5_1).
  • test_ds41_mmid_width_invariance on the R9700: the new qwen4exp expert shapes are batch-invariant and identical.

Other models

  • RDNA4 MUL_MAT_ID with short expert rows takes the MoE kernel for any model whose expert rows are short: faster decode there, with a different sum order than main.
  • The block radix top-k is the RDNA4 default for every model that uses the plain k = 512 top-k (the DS4 indexer); the selection is unchanged.
  • The greedy host scan applies to every model; the picks are unchanged.
  • Grouped MUL_MAT_ID on HIP: only Q5_1 experts on RDNA3.5 change path by default (bit-exact); LUCE_MMID_GROUPED=1 keeps the mask-driven opt-in on every GPU.
  • The plain-SwiGLU fused Q8_0 gate/up pair on RDNA4 serves any model's single-column dense gate/up, not only qwen4exp's shared expert (bit-identical).
  • Sampled top candidates: a power-of-two top_k (32, 64, 128) now gets the next power of two of candidates, so its draws settle on the device too.
  • The ggml_argmax tie rule (lowest id) applies to every model that picks with it on CUDA/HIP; rows without ties pick the same id.
  • top-k routes power-of-two k from 32 to 2048 on rows wider than 4096 to the tiled block radix; today only the sampler's candidates use those shapes (the DS4 router and DSpark heads are unchanged).

Kill switches

LUCE_CUDA_MMVQ_RDNA4_MOE=0, GGML_DS4_TOPK_BLOCK_RADIX=0, LUCE_QWEN4EXP_LOOKUP=0, LUCE_GPU_ARGMAX=0, LUCE_MMID_GROUPED=0 (documented in docs/ENVIRONMENT.md).

🤖 Generated with Claude Code

View guided diff

Split mode (dense layers on an R9700, routed experts on a Strix Halo) and the
Strix Halo alone, Qwen3.8-Flash-Next:

- The expert device's weights go to device memory: ggml gave the Strix Halo's
  71.7 GiB expert buffer managed memory, which the kernel swapped out and the
  GPU faulted back in mid-prompt (32K prompts took up to 58 s).
- RDNA4 uses the block radix top-k: the device-wide sort kept four copies of
  the scores in the pool and ran an R9700 holding hot experts out of memory on
  80K+ prompts. Same ids in the same order, and faster at every row count.
- Anchored split-input copies (ggml_backend_sched_set_anchored_copies): a
  prompt chunk's cross-device copies live from their sending split to their
  last reader, which halves the R9700 workspace of a 16384-row chunk; the hot
  experts take the memory the fitted context and chunk leave.
- RDNA4 short rows: MUL_MAT_ID with short expert rows runs on the one-wave MoE
  kernel at every width, and Q8_0 rows of at most 24 blocks use a one-wave
  block shape (bit-exact). The fused RDNA4 Q8_0 gate/up pair also takes plain
  SwiGLU (qwen4exp's shared expert).
- One GPU: the prompt chunk widens toward 8192 rows while one full-context
  prefix snapshot still fits.
- Prompt-lookup drafts beside adaptive MTP, chosen per round by expected
  tokens per ms (common/prompt_lookup.h; LUCE_QWEN4EXP_LOOKUP=0 turns it off).
- Greedy sampling without penalties scans the host row instead of uploading it
  to the GPU.

Greedy output is unchanged in every A/B.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@davide221

Copy link
Copy Markdown
Contributor Author

Strix Halo alone and the 100K prompt, as promised in the description. Same setup: Qwen3.8-Flash-Next UD-Q4_K_XL, 131K context, MTP draft, greedy, median of 3 runs, same prompts for both builds. Decode tok/s, prompt tok/s in brackets.

Strix Halo alone (no --expert-placement)

Prompt main this PR
short reply 43.8 48.9
edit, 8K 44.5 (1,029) 57.0 (1,188)
explain, 8K 37.8 (1,043) 36.6 (1,217)
explain, 32K 35.2 (1,179) 37.6 (1,279)

The explain 8K decode difference is inside the run-to-run spread (this PR's three runs: 34.0, 35.7, 39.4; the draft acceptance rate moves between 0.50 and 0.66 across runs).

R9700 + Strix Halo, 100K prompt (explain)

main this PR
decode tok/s 55.2 60.0
prompt tok/s 1,606 1,721

🤖 Generated with Claude Code

davide221 and others added 2 commits November 4, 2026 00:58
…needs

Every verify copied all of its logits rows to the host (5 rows = 5 MB on
Qwen3.8-Flash-Next's 248K vocab), which costs 2-3 ms per step when the
target GPU sits on a narrow PCIe link.

- Greedy requests without penalties or grammar take each row's pick from
  a device argmax (ggml_argmax in the verify and T=1 graphs) and read one
  id per row. argmax.cu now breaks ties toward the lowest id and returns
  row 0 for an all-NaN row, as the host scan does.
- Sampled requests with a top_k (the model card's thinking and
  non-thinking recipes) read each row's top 32 or 64 candidates from a
  device top-k. sample_logits_top draws from them the token, and spends
  the RNG value, that the whole row gives, or returns -1 before drawing
  when the candidates cannot settle it (a tie at the cut, a NaN, a
  penalty that could lift an outside token); the row is read only then.
- Other sampled requests read only the verify rows they sample, one at a
  time.
- top-k.cu routes power-of-two k from 32 to 2048 on rows wider than 4096
  to the tiled block radix (the sampler's k = 32 or 64 of 248K); both
  paths are stable selections.

Same seed, same text on 36 held-out prompts (thinking and non-thinking
recipes, 0 to 32K context); the candidates settled every draw. Split
R9700 + Strix Halo decode, sampled: +2 to +5% (code 78.2 -> 81.0,
8K 59.2 -> 61.5, 32K 56.9 -> 59.0 tok/s); greedy: +4 to +5%.
LUCE_GPU_ARGMAX=0 restores the full-row reads.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The grouped kernel reads each routed expert once per verify batch instead of
once per (token, expert) pair. Q4_1/Q5_1 join it (type mask bit 128), and on
HIP it is now on by default for Q5_1 on RDNA3: the Strix Halo, which owns the
cold experts of Qwen3.8-Flash-Next in split mode and whose expert down
projection is Q5_1. That projection gets about 24% faster, bit-exact. Split
decode, back to back with the kill switch: +3% on short replies and 8K
summaries, flat on long explanations; prompt speed unchanged.

Every other HIP format and RDNA4 keep today's path. LUCE_MMID_GROUPED=0 is the
kill switch; =1 restores the mask-driven policy on every GPU.

The grouped parity test covers Q4_1 and Q5_1 (109 cases, 135 grouped).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
davide221 and others added 6 commits November 4, 2026 14:48
tensor_id_copy multiplied the consumer's backend id by the copy count in int
before adding it to the size_t index (CodeQL
cpp/integer-multiplication-cast-to-long). The values are small, so the index
is unchanged; the product is now computed in size_t.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Three backends parsed the switch with their own copies of the same lambda.
gpu_argmax_enabled() in common/sampler.cpp now holds it, and qwen4exp's single
request and concurrent paths pick their verify rows with one rule,
qwen4exp_rows_for (the two copies agreed but were written differently).
No behaviour change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The one-wave block shape for short Q8_0 rows is bit-identical to the
eight-wave launch it replaces, so LUCE_CUDA_MMVQ_Q8_RDNA4_SHORT had nothing to
restore.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review follow-ups to the Q5_1 grouped default:
- The default no longer raises the MUL_MAT_ID width ceiling. A batch the
  grouped path declines (more than 256 routed pairs) fell back to the MoE
  kernel compiled for the RDNA3 ceiling of 8 columns, at up to 16. Raising
  stays with LUCE_MMID_GROUPED=1 and CUDA's default, as on main.
- The default is RDNA3.5 only. RDNA3.0 runs Q5_1 MMVQ with several warps per
  row, so grouped verify rows would no longer match decode in batch-invariant
  mode; RDNA3.5 uses one warp per row, like the grouped kernel.

Qwen3.8-Flash-Next is unaffected: verify widths stay at or below 8.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With top_k 32, 64 or 128 and no penalty, sample_logits_top_n returned exactly
top_k candidates. The draw needs the value below the cut to rule out a tie
there, so it never settled and every row was read back. The count is now the
next power of two above top_k (or twice top_k with a penalty). Output was and
stays exact; the card recipes (top_k 20) are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
decode_check_ is a std::function, which does not convert to bool implicitly;
the previous commit did not build.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@davide221
davide221 marked this pull request as ready for review November 4, 2026 18:46

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

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

View guided diff | Re-trigger cubic

Comment thread server/test/bench/bench_ds4_topk.cpp Resolved
Comment thread server/test/bench/bench_ds4_topk.cpp Resolved
Comment thread server/src/common/prompt_lookup.h Resolved
Comment thread server/src/qwen4exp/qwen4exp_graph.cpp Resolved
Comment thread server/src/qwen4exp/qwen4exp_loader.cpp Resolved
Comment thread server/docs/ENVIRONMENT.md Resolved
Comment thread server/docs/ENVIRONMENT.md Resolved
Comment thread server/docs/ENVIRONMENT.md 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

Found 1 issue.

Copy to agent

Use the copy button in the top-right corner of the block, then paste into your agent.

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.

Repository: Luce-Org/lucebox
Reviewed commit: 535dd4a56c624eca7c4be928b7d5545f8652a810
Verify the findings against the current code, run relevant checks for any fixes, and explain any findings you reject.

<file name="server/src/qwen4exp/qwen4exp_graph.cpp">

<violation number="1" line="2893" end_line="2897">
Lookup rounds grow Qwen4ExpMtpPending without bound; nothing catches the owed draft pairs up

In `qwen4exp_mtp_step`, a normal MTP round trims `pending` before drafting (`qwen4exp_mtp_catch_up(..., keep=1)` at line 2827, guarded by `verify &amp;&amp; !lookup`), so `pending-&gt;tok`/`pending-&gt;h` stay bounded by `cache.mtp_draft + 1` entries. The new lookup branch instead only *appends*: `pending-&gt;h.insert(...)` / `pending-&gt;tok.insert(...)` add `retained` entries (up to 8 rows of `n_embd*n_hc` floats, tens of KB each) and never erase, and `pending-&gt;pos` is only advanced by `qwen4exp_mtp_catch_up`, which a lookup round never runs.

The policy in `common/prompt_lookup.h` is built to keep choosing lookup rounds when quoting repeats — exactly the edit/refactor workload this feature targets — so on such a request the draft layer never runs and `pending` grows linearly with the whole generation: at Qwen3.8-Flash-Next hidden sizes that is on the order of 40 KB per emitted token of host memory (over a GB for a long 32K-token edit), plus a single huge catch-up forward (512-row slices over the entire accumulated backlog) whenever an MTP round finally fires or the request's prefill/next-request drain (`qwen4exp_mtp_catch_up(..., 0)`) runs.

The non-lookup path's bound should apply to lookup rounds too: when `pending-&gt;tok.size()` exceeds `cache.mtp_draft + 1` (or a similar cap) at the start of a lookup round, run `qwen4exp_mtp_catch_up` on the owed pairs before the trunk verify, keeping `pending-&gt;pos` consistent with the retained tail as the MTP path does.
</violation>

</file>
Issue overview
  1. Lookup rounds grow Qwen4ExpMtpPending without bound; nothing catches the owed draft pairs up · likely (server/src/qwen4exp/qwen4exp_graph.cpp:2893-2897)

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

Comment on lines +2893 to +2897
if (lookup) {
// The draft layer did not run: its pending pairs are still owed, then the retained ones (as plain
// decode adds its one pair). The next MTP round catches them all up.
pending->h.insert(pending->h.end(), hidden.begin(), hidden.begin() + (std::ptrdiff_t) ((size_t) retained * hd));
pending->tok.insert(pending->tok.end(), decision.emitted.begin(), decision.emitted.begin() + retained);

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.

Lookup rounds grow Qwen4ExpMtpPending without bound; nothing catches the owed draft pairs up · likely

In qwen4exp_mtp_step, a normal MTP round trims pending before drafting (qwen4exp_mtp_catch_up(..., keep=1) at line 2827, guarded by verify && !lookup), so pending->tok/pending->h stay bounded by cache.mtp_draft + 1 entries. The new lookup branch instead only appends: pending->h.insert(...) / pending->tok.insert(...) add retained entries (up to 8 rows of n_embd*n_hc floats, tens of KB each) and never erase, and pending->pos is only advanced by qwen4exp_mtp_catch_up, which a lookup round never runs.

The policy in common/prompt_lookup.h is built to keep choosing lookup rounds when quoting repeats — exactly the edit/refactor workload this feature targets — so on such a request the draft layer never runs and pending grows linearly with the whole generation: at Qwen3.8-Flash-Next hidden sizes that is on the order of 40 KB per emitted token of host memory (over a GB for a long 32K-token edit), plus a single huge catch-up forward (512-row slices over the entire accumulated backlog) whenever an MTP round finally fires or the request's prefill/next-request drain (qwen4exp_mtp_catch_up(..., 0)) runs.

The non-lookup path's bound should apply to lookup rounds too: when pending->tok.size() exceeds cache.mtp_draft + 1 (or a similar cap) at the start of a lookup round, run qwen4exp_mtp_catch_up on the owed pairs before the trunk verify, keeping pending->pos consistent with the retained tail as the MTP path does.

Checked (82%): Confirmed: the lookup branch in qwen4exp_mtp_step (qwen4exp_graph.cpp:2893-2897) only appends retained hidden/token pairs to pending and never runs qwen4exp_mtp_catch_up, while the non-lookup verify path trims via catch_up at line 2827 and reassigns a bounded tail at 2901-2903. The only drains are end-of-generation/request catch-ups (qwen4exp_backend.cpp:678, 846) and those only fire after the whole decode loop, so a long run of lookup rounds — which the policy in the decode loop (qwen4exp_backend.cpp:793-803) is designed to keep choosing on repetitive/quoting workloads — grows pending->h by retained * n_embd * n_hc floats per round with no bound, exactly the unbounded host-memory growth the reviewer describes. One caveat: the final catch-up at line 846 slices work by chunk, softening (but not eliminating) the deferred-work concern, and the memory is reclaimed at request end rather than leaking permanently.

davide221 and others added 3 commits November 4, 2026 19:57
…s them

The T=1 decode and verify graphs built a full-vocabulary argmax even when the
caller passed no row outputs (the concurrent slot engine, LUCE_GPU_ARGMAX=0)
and then dropped it. They now build it only with out_rows, and a stable decode
graph replays only for a caller that wants the same outputs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
At the draft model's own width an unmeasured lookup round borrows that round's
cost, draft included, so it could never beat it on paper and was never tried.
The probe now covers that width as well; after a few rounds the lookup's own
cost decides.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…der comment

- ENVIRONMENT.md: list LUCE_QWEN4EXP_LOOKUP, LUCE_CUDA_MMVQ_RDNA4_MOE and the
  qwen4exp LUCE_EXPERT_BUDGET_MB read in the inventory; LUCE_MMID_GROUPED=1
  applies to eligible GPUs; GGML_DS4_TOPK_BLOCK_RADIX=0 also covers the tiled
  top-k.
- bench_ds4_topk: reject a malformed row list, fail when no case ran, and label
  the kernel the way the backend reads the variable.
- qwen4exp_loader.cpp: rewrite the expert-device comment as a full sentence.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@davide221
davide221 merged commit 20ab10e into main Nov 4, 2026
27 of 30 checks passed
davide221 added a commit that referenced this pull request Nov 5, 2026
#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 deleted the perf/qwen4exp-split-speed branch November 6, 2026 19:24
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