Repository navigation
perf(qwen4exp): faster split mode on R9700 + Strix Halo - #933
Conversation
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]>
|
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
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)
🤖 Generated with Claude Code |
…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]>
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]>
There was a problem hiding this comment.
All reported issues were addressed across 29 files
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
davide221
left a comment
There was a problem hiding this comment.
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 && !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.
</violation>
</file>
Issue overview
- 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
| 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); |
There was a problem hiding this comment.
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.
…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]>
#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]>
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.
qwen4exp_loader.cpp).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.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).sample_logits_topdraws 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.Results
R9700 + Strix Halo, Qwen3.8-Flash-Next UD-Q4_K_XL, 131K context, MTP draft,
--expert-placementwith 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.Device token picks, A/B on the same build otherwise, held-out prompts, same seed (decode tok/s):
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:Strix Halo alone and the 100K prompt: results to follow in a comment.
Exactness
bench_ds41_q8_mmvqhashes against the eight-wave launch,test_ds4_fused_ops_cuda).ggml_argmaxon CUDA/HIP now breaks ties toward the lowest id and returns 0 for an all-NaN row, as the host scan does.Tests
On an R9700 + Strix Halo build of this branch (gfx1151 + gfx1201):
test_server_unit: 1,217 passed, 0 failed (addstest_prompt_lookup, including the probe at the draft model's own width,qwen4exp_reserved_rowscases, 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_cudaon 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_cudaon the R9700 and the Strix Halo: parity PASS, 109 cases, 135 grouped (adds Q4_1 and Q5_1).test_ds41_mmid_width_invarianceon the R9700: the new qwen4exp expert shapes are batch-invariant and identical.Other models
LUCE_MMID_GROUPED=1keeps the mask-driven opt-in on every GPU.ggml_argmaxtie rule (lowest id) applies to every model that picks with it on CUDA/HIP; rows without ties pick the same id.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 indocs/ENVIRONMENT.md).🤖 Generated with Claude Code