Skip to content

perf(split): faster R9700 + iGPU split decode (+36.9% Qwen3.8-Flash-Next), ported to DeepSeek V4.1 and Qwen3.8-27B - #966

Merged
davide221 merged 16 commits into
mainfrom
perf/split-speed-stack
Nov 10, 2026
Merged

davide221 merged 16 commits into
mainfrom
perf/split-speed-stack

Conversation

@davide221

@davide221 davide221 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Faster decode when a model is split across an R9700 (dense layers, attention, hot experts, the drafter) and a gfx1151 iGPU (cold experts; the engine calls it the Strix), and the same ideas ported to DeepSeek V4.1 and Qwen3.8-27B. Every change is exact: greedy output is the same text as main.

Measured on an R9700 + Ryzen AI Max+ PRO 495 box:

Model, setup main this PR text
Qwen3.8-Flash-Next split, 1 slot, 11.8 GB hot experts, MTP width 3 61.02 tok/s 83.51 tok/s (+36.9%) same
DeepSeek V4.1 1017, ds41-r9700-gorgon, decode step (short / 2K / 26K prompt) 59.1 / 61.4 / 59.9 ms 57.7 / 58.5 / 57.8 ms (-2.4 / -4.7 / -3.5%) same
DeepSeek V4.1 1017, one request with the drafter, code / prose / 1,951-token prompt (three and two runs) 52.1-53.0 / 39.9-40.9 / 45.4 tok/s 56.3 / 43.8-44.5 / 48.5 tok/s (+6 to +10%) same
DeepSeek V4.1 1017, 4 slots paged without the drafter, each request with 1 / 2 / 4 active 22.5-23.9 / 12.7-13.3 / 7.6-8.0 tok/s 22.3-23.5 / 13.2-13.7 / 7.7-8.4 tok/s (no change) same alone
Qwen3.8-Flash-Next split, 4 slots, full context (as Manage launches it), one request / 4 at once 51.4 / 67.3 tok/s 60.8 / 71.8 tok/s (+18.3 / +6.6%) same
Qwen3.8-Flash-Next on the iGPU alone, 4 slots (as Manage launches it), one request / 4 at once 38.2 / 52.5 tok/s 42.0 / 54.6 tok/s (+10.0 / +3.9%), measured before the last commit, which costs a lone request 1-2% same
Qwen3.8-27B, 8 slots paged (as Manage launches it), 1 / 2 / 4 / 8 requests at once, two runs each 113-125 / 174-191 / 277-297 / 425-438 tok/s 117-125 / 174-181 / 288-296 / 435-448 tok/s (no change beyond the runs' spread) not compared: this model's greedy text already differs between two runs of main

DeepSeek V4.1 is compared by step time: its drafter's acceptance moves a few percent between runs of the same build while the text stays the same, so tok/s carries that noise.

What changed

Qwen3.8-Flash-Next split engine (server/src/qwen4exp), in the order the changes landed (each measured alone, same text):

Change Split decode
GDN step in fewer launches (conv step, gated norm writing q8_1 rows, rms_norm + scale), Strix Q4_K expert prefetch, route-norm alias fix +9.2%
Router in one launch, its outputs ahead of the expert nodes; verify rollback as one batched copy +1.7%
Batched peer copies, one copy kernel per hop +1.1%
One expert split per MoE layer +2.7%
Scheduler: split events recorded when the next split starts +0.9%
HC mix fused (all 96 verify mixes), split stable inputs async +1.1%
Rollback on a side stream, async draft uploads, Strix idle hold +1.7%
Pinned staging for verify uploads, output reads and the draft id read +1.3%
R9700 reads the next layer's first weights into its Infinity Cache while it waits for the Strix (ggml_moe_fused_prefetch, 56 MB) +4.5%
Strix read-ahead: a Q8_0 copy of the next router guesses the cold experts and reads their gate/up slices into the MALL (ggml_moe_fused_prefetch_rows) +4.7%
Strix Q4_K expert loop at 2 rows per wave with the rounding written out +1.3%
Strix wake hold: one wave sleeps until the R9700's router posts, so the first kernel after the hop runs at speed +0.6%
Hop copy captured at the graph's tail +1.0%
PLE conv fused at every width, verify argmax in two launches +0.3%
RMS_NORM -> MUL -> ROPE fused on the R9700 too +0.1%
Concat as one copy launch +0.3%
LM head: the last 20% of rows on the Strix, both GPUs on the RDNA4 Q8_0 matvec (per-node flag ggml_backend_cuda_mul_mat_rdna4_rows, so every logit keeps the R9700's bits) +0.5%

ggml-cuda (every model): argmax in two launches for rows of 8192+ (same pick rule), contiguous concat of up to 1 MiB as one copy launch, a batched copy kernel, split events recorded when the next split starts, a barrier fix in the soft_max and group_norm reductions (a race), the RDNA4 Q8_0 matvec on RDNA3.5 for flagged products.

DeepSeek V4.1 (server/src/deepseek4/deepseek4_fused_verify.inc, server/src/common/moe_hybrid_ffn_eval.*): in every MoE layer of the split verify the R9700 waits for the expert device's cold-expert sum once its hot and shared experts are done. In that wait it now reads the next layer's first 56 MiB of attention weights (the output head after the last layer) into its 64 MB Infinity Cache, through a new MoeHybridGraphInputs::prefetch_next list. Nothing the graph reads changes. 40 MiB gains less, 72 MiB (past the cache) loses.

Qwen3.8-27B (server/src/qwen35, one GPU): the RMS_NORM -> MUL -> ROPE [-> PERMUTE -> CONT] fusion now runs for every HIP M-RoPE graph, including the Qwen3.5 family's interleaved M-RoPE (attention Q and K: one launch each instead of three or four), with the separate kernels' bits.

This PR no longer changes how the Qwen3.8-27B engine commits accepted tokens after a batched verify round: that rework measured no clear gain on that model, and #964 reworks the same step with its own design, so it is left to that PR (reverted here in one commit; server/src/qwen35 is identical to main). The one piece the later commits here build on, ggml_backend_cuda_enter_stream (the single entry for work queued on a backend's stream from outside its own launches), stays. After the revert and the merge with main: both product builds, the split unit tests (31 of 31, 7 of 7), the exactness smoke on the iGPU alone and split, and 1 to 4 requests at once give the same text (39 of 39 requests) and the same speed as before.

Exactness

Texts compared run by run against main: Qwen3.8-Flash-Next (short prompts, 191 to 70K tokens of context), DeepSeek V4.1 (short, 2K and 26K prompts, x2). Unit tests, all passing on both GPUs with HIP graphs on and off:

  • test_qn_split_fusions (28): every fusion against the segmented graph, bitwise; the LM head row split on both GPUs at 1, 3, 4 and 8 columns (0 differing outputs; the Strix's own matvec differs in 73% of them, which is why the flag exists).
  • test_norm_rope_fusion: 30 chains fused per GPU (standard and interleaved M-RoPE), same hash as the separate kernels, with and without the qwen4exp profile.
  • test_cuda_copy_batch (6), test_ple_conv_fusion, test_ds41_mmid_width_invariance, test_deepseek4_unit, test_qwen4exp_chunk, test_server_unit (1205).

Known limits

  • Five constants are fitted to one R9700 + iGPU pair and documented with their measurements where they are defined: the LM head share on the expert device (20%), the read-ahead depth (8 picks, 16 experts), the wake hold (500 us), the idle hold bound (8 ms) and the 56 MiB weight pre-read sized to the R9700's 64 MB cache (defined once in qwen4exp and once in deepseek4). They change timing only, never an output.
  • Left for a follow-up, to keep this PR from growing: one hold kernel and one clock-rate helper (there are two of each), one Q8 memo key helper, shared device helpers for the hand-copied router top-k, F32 twin and copy-batch loops, one rope_rotate used by the fused and unfused paths, a named helper for the allocator lifetime edges in qwen4exp_graph.cpp, and the MTP staging on PinnedStage.
  • With several requests at once, DeepSeek V4.1's greedy text depends on how many requests are active, on main as on this branch (alone it is the same on both). Not changed here.

Test plan

  • HIP build (gfx1201 + gfx1151), HIP graphs on and off
  • Unit tests above on both GPUs
  • Qwen3.8-Flash-Next split A/B vs main, same text
  • DeepSeek V4.1 split A/B vs main, same text
  • Qwen3.8-Flash-Next split with 4 slots (Manage flags), A/B vs main, same text
  • Qwen3.8-Flash-Next on one gfx1151 alone: smoke_qwen4exp_forward 300 --mtp 64 --mtp-draft 3 passes 3 of 3 runs (MTP logits identical to plain decode, 16/16 rollback cases, 16/16 catchup); the same smoke in split mode passes 2 of 2. Before the last commit it failed every run on one gfx1151, at a different token each time: the one-device router was queued ahead of the experts.
  • The same smoke on a Gorgon Halo alone (2 of 2 runs) and its split (1 of 1), after the last commit
  • Qwen3.8-27B with 8 slots, A/B vs main: no change
  • CI (CUDA and ROCm jobs)

🤖 Generated with Claude Code

View guided diff

davide221 and others added 3 commits November 9, 2026 11:59
Split decode of Qwen3.8-Flash-Next (R9700 runs the dense layers, attention,
hot experts and the MTP drafter; the Strix Halo runs the cold experts) goes
from 61.02 to 83.51 tok/s (+36.9%) on an R9700 + Ryzen AI Max+ PRO 495 box with the
same greedy text, short prompts and 191 to 70K tokens of context.

Split engine:
- GDN step in fewer launches, router in one launch, fused HC mixes, verify
  rollback as one batched copy on a side stream, async draft uploads and
  pinned staging for verify uploads and output reads.
- One expert split per MoE layer, batched peer copies (one copy kernel per
  hop), the hop copy captured at the graph's tail.
- R9700: reads the next layer's first 56 MB of weights into its Infinity
  Cache while it waits for the Strix (ggml_moe_fused_prefetch).
- Strix: a Q8_0 copy of the next router guesses the cold experts and reads
  their gate/up slices into the MALL (ggml_moe_fused_prefetch_rows); Q4_K
  expert loop at 2 rows per wave with the rounding written out; a wake hold
  so its first kernel after the hop runs at speed.
- LM head: the last 20% of rows on the Strix, both GPUs on the RDNA4 Q8_0
  matvec (per-node flag ggml_backend_cuda_mul_mat_rdna4_rows) so every logit
  keeps the R9700's bits.

ggml-cuda (every model): argmax in two launches for rows of 8192+, small
contiguous concat as one copy launch, a batched copy kernel, split events
recorded when the next split starts, a barrier fix in the soft_max and
group_norm reductions, PLE conv fused at every width.

Tests: test_qn_split_fusions (every fusion bitwise against the segmented
graph, LM head row split on both GPUs), test_cuda_copy_batch,
test_ple_conv_fusion, test_ds41_mmid_width_invariance.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…m-rope fusion for Qwen3.5 M-RoPE

DeepSeek V4.1 split verify (R9700 + Ryzen AI Max+ iGPU): in every MoE layer
the R9700 waits for the expert device's cold-expert sum once its hot and
shared experts are done. In that wait it now reads the next layer's first
56 MiB of attention weights (Q down/up, KV, compressor and indexer
projections, output projections; the output head after the last layer)
into its 64 MB Infinity Cache (ggml_moe_fused_prefetch, a new
MoeHybridGraphInputs::prefetch_next list). Nothing the graph reads changes.
A decode step is 2.1% shorter on a short prompt, 3.1% at 2K and 1.7% at 26K
tokens than without the read, same text (ds41-r9700-gorgon, x2).

Qwen3.5 family (Qwen3.8-27B): the RMS_NORM -> MUL -> ROPE [-> PERMUTE ->
CONT] fusion now runs for every HIP M-RoPE graph, including interleaved
M-RoPE (attention Q and K: one launch each instead of three or four), with
the separate kernels' bits. test_norm_rope_fusion covers interleaved M-RoPE
at the 27B shapes, with and without the qwen4exp profile.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With several slots and the full context, the fit moves some attention
layers' KV cache and weights to the expert device
(move_qwen4exp_attention). The next-layer read after such a layer took
those weights too, so the scheduler placed the read on the CPU, which has
no MOE_FUSED kernel, and the first decode step aborted. The read now keeps
only weights of the target's own buffer type.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
davide221 and others added 8 commits November 9, 2026 12:28
The Windows CUDA build compiles ggml-cuda.cu's host code with MSVC, which
has no __atomic_store_n. The hold's flag (pinned host memory the hold
kernel polls) is now raised with a release fence and a volatile store,
the same ordering on every host compiler.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
nvcc on Windows has no __builtin_fmaf in device code. The Q4_K register
dot now calls a small helper: clang's builtin on HIP (the same
instruction as before), __fmaf_rn on CUDA.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- QSA positions: finish the stable-input uploads before the scheduler copies them to the expert device.
- Norm-rope fusion: no overlap of the output with x, gamma or the positions; enabled per device (RDNA3.5 and
  RDNA4, where the tests compare it bit for bit).
- HIP graph tail: a context destroyed with its capture still open ends it first, on any thread (one list of open
  captures for all threads, so a context freed off its opener's thread leaves no stale entry); retired graph
  instances wait for their last launch; a freed backend publishes the peer copies that name it; get_stream flushes
  the work the backend still holds back.
- Wait-read prefetch: every thread's loads stay live (the sink store almost never runs).
- Stacked alpha/beta views on HIP only (CUDA routes the 48- and 96-row products to different kernels).
- Scheduler: a registered late cross input never takes the waits-already shortcut.
- Router test: the LUT gather's row count is computed in 64 bits (CodeQL).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A user input copied to another backend waited for the split backend's last event first, because the blocking copy
is not ordered with that backend's stream. A copy flagged input and output has memory of its own for the whole
graph (allocated first, never freed) and is written before its first use, so when every backend was idle at the
start there is nothing to wait for. Layers placed on the second GPU read the target's positions and masks this way,
which drained that GPU in the middle of every decode step.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- The Engram row upload runs as a node of the graph instead of a blocking read before it.
- The Engram wkv projection is cut into chunks that run in the MoE layers' waits for the expert device
  (MoeHybridGraphInputs::wait_work).
- LUT uploads go through one pinned staging buffer; per-step inputs are pinned.
- A verify-cache miss rebuilds over a slot built for an earlier stretch of the context (its smallest padded
  compressed-row count is below what any later verify of the generation can span) and keeps that slot's scheduler,
  pinned staging, allocator buffers and metadata arena, instead of creating them again in a never-built slot.
  ggml_backend_sched_replan makes the kept scheduler plan the new graph afresh: the new key can give the old
  graph's node and leaf counts with another topology, which the scheduler would otherwise reuse by node index.

Same text; a rebuild costs 10.2 ms instead of 16.8 ms, decode step -1.6% (code) and -4.9% (2K-token prompt) on
ds41-r9700-gorgon.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The batched decode ran GET_ROWS, CONCAT, CONT, SET_ROWS, SSM_CONV, SILU and two RMS_NORM + SCALE per GDN layer
and slot group, plus four gate ops. The GDN conv step kernel now takes slot ids (one state slab per slot), the
HIP backend runs the chain as one launch (GGML_CUDA_QN_GDN_CONV_SLOTS), q and k share one norm, and the recurrence
applies the raw gates as the one-token step does. Unit tests compare both against the unfused graph bit for bit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Tree commit: ggml_backend_cuda_tree_commit_transaction_async runs the preflight on the host's own copies of the
  indices (no readbacks), checks device placement once per set of addresses, and commits every cache, the feature
  rows and each run of same-shape layers in one launch each (129 launches per step become 4; same per-element
  bodies; overlapping destinations keep the per-layer order), stream-ordered with one sync at the end of the round.
- Drafter uploads go through pinned staging; the drafter's tail (top-k) stays on the GPU.

Qwen3.8-27B, Manage's flags (8 slots): +3.3% / +3.5% alone, +2.6% / +4.6% at 4 at once (two interleaved runs).
Texts differ between runs of the same build on this path (main vs main 3 of 4); the host path stays within that.
test_tree_commit_async compares the async commit with the synchronous one byte for byte.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
On one device the MoE router is no longer queued ahead of the experts: with it
first, MTP steps on gfx1151 alone differed from plain decode from run to run
(smoke_qwen4exp_forward --mtp 64 --mtp-draft 3: logits and all 16 rollback
cases). Split mode keeps the early router. The rollback's batched copies only
take the device buffer type on the CUDA backend, and the prefix cache test's
queued backend gets its own guid.

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

Copy link
Copy Markdown
Contributor Author

@cubic

@cubic-dev-ai

cubic-dev-ai Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

@cubic

@davide221 cubic can't start this review because your workspace has reached its monthly review limit. cubic has reviewed 160,512 of the 160,000 allowed lines of code this month. Reviews resume on 24 November 2026 (in 14 days). Enable flex capacity to cover overages automatically and resume reviews now. Learn how flex capacity works.

To help optimise your usage, you can tune cubic to get the most out of your usage limits:

Learn more →

davide221 and others added 3 commits November 10, 2026 13:22
A benchmark-only setter, its global and eleven block shapes only it could
select. Nothing called it.

Co-Authored-By: Claude <[email protected]>
… one stream entry

The peer copy kernel and peer copy batching were forced on for qwen4exp
graphs, so GGML_CUDA_PEER_COPY_KERNEL=0 and GGML_BATCH_PEER_COPIES=0 no
longer turned them off there; and the HIP graph tail ran for any batch of
kernel copies, which reached DeepSeek V4.1 profiles that set both. Unset
now means on for qwen4exp graphs and off elsewhere, "0" means off
everywhere, and the tail runs only on a backend that
ggml_backend_cuda_set_copy_tail turned on: the qwen4exp split scheduler.
DeepSeek V4.1 is back to the copies it had before this series.

Work queued on a backend's stream from outside its own launches goes
through one entry, ggml_cuda_enter_stream (collected peer copies, an open
graph tail, an idle hold, pending side copies, the device), including two
callers that skipped it: the GDN chain commit and the vision rotary.
ggml_backend_cuda_get_stream is a lookup again, as before this series;
ggml_backend_cuda_enter_stream is the call that settles.

Also: the router's wake post is set through
ggml_backend_cuda_div_wake_post and one GGML_MOE_FUSED_WAKE_SLOTS, the MTP
staging falls back to a blocking upload when its pinned buffer is full,
and the idle hold's bound has a name.

test_cuda_copy_batch: the tail stays off without the setter and with
either variable at 0, and enter_stream publishes collected peer copies
where get_stream does not.

Co-Authored-By: Claude <[email protected]>
davide221 and others added 2 commits November 10, 2026 20:08
This reverts 45e5501 (perf(qwen35): DFlash2 rounds with 3 host waits
instead of ~100).

That commit reworked how the Qwen3.8-27B concurrent engine commits the
accepted tokens of a batched verify round: a stream-ordered tree commit
validated on the host's copy of the indices, pinned uploads for the
drafter and the selector's top-k on the GPU. It belongs to the 27B
concurrent-verify work, not to the split decode this branch is about; it
measured no clear gain on that model; and another open pull request
reworks the same commit step with a different design. Dropping it here
leaves that step to that pull request.

server/src/qwen35, the DFlash2 drafter and selector files in
server/src/common, pinned_stage.h and gdn-replay-log.cu are back to main
byte for byte. test_tree_commit_async and the async cases of
test_draft_topk_cuda go with the code they tested.

Kept: ggml_backend_cuda_enter_stream, which that commit added. The later
review fixes rebuilt it on ggml_cuda_enter_stream, the one entry for work
queued on a backend's stream from outside its own launches (batched
copies, the GDN chain commit, the vision rotary), and
test_cuda_copy_batch checks that it publishes collected peer copies.

Co-Authored-By: Claude <[email protected]>
@davide221
davide221 marked this pull request as ready for review November 10, 2026 22:43
@davide221
davide221 merged commit 9cd9aef into main Nov 10, 2026
17 of 18 checks passed
davide221 added a commit that referenced this pull request Nov 10, 2026

@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 e7816f6c · 2 candidate issues checked, kept at ≥70% confidence · add the code-review label to review again

davide221 added a commit that referenced this pull request Nov 10, 2026
Conflicts kept both sides:
- moe_hybrid_ffn_eval.h: MoeHybridGraphInputs keeps overlap_owners and gains
  main's prefetch_next / prefetch_bytes / wait_work.
- deepseek4_fused_verify.inc: a layer sets overlap_owners (paged steps) and
  then main's prefetch weights, prefetch budget and Engram wait work.
Main consumes the prefetch and wait work in the device-join branch only, so
paged overlap steps build as reviewed.

Co-Authored-By: Claude <[email protected]>
@davide221
davide221 deleted the perf/split-speed-stack branch November 12, 2026 14:57
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