perf(qwen4exp): faster split mode on R9700 + Strix Halo
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_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. - 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_mmvqhashes 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_argmaxon 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 (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
- 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=1keeps 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_argmaxtie 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