Skip to content

perf(ds4): paged hybrid steps overlap the two GPUs through a shared MoE overlap step - #983

Merged
davide221 merged 11 commits into
mainfrom
perf/ds4-split-overlap
Nov 11, 2026
Merged

davide221 merged 11 commits into
mainfrom
perf/ds4-split-overlap

Conversation

@davide221

@davide221 davide221 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On a Radeon AI PRO R9700 + Ryzen AI Max+ PRO 495, DeepSeek V4.1 Flash splits each MoE layer between two GPUs. The R9700 runs attention, the dense layers, the shared expert and the hot routed experts. The Radeon 8065S iGPU runs the cold routed experts.

In the multi-user (paged) step the two GPUs took turns inside every layer. With this PR they work at the same time, and each extra row needs fewer launches. Outputs are bit-identical.

before after
paged step time 13 to 18% lower (1 to 4 lanes, 8 rows)
server decode, per request +16% alone, +20% 2 at once, +23% 4 at once

Based on main, with #966's split stack and #981's paged lane isolation in. All numbers below compare against main on the same box (measured as main with #966, with #981 merged in; the PRs that landed after it change no file the V4.1 paged step uses).

The overlap rules live in common code (common/moe_overlap.h, header only), written for any MoE layer split between two GPUs of one runtime. This PR uses them for DeepSeek V4.1's paged steps. Moving Qwen3.8-Flash-Next onto them is a follow-up.

Why the GPUs took turns

  1. The join waited at the start of its split. ggml's scheduler copies a split's inputs from other devices before any node of the split runs. In the paged step, the R9700 split that follows the routes held three things: the hot experts, the shared expert and the join. The join reads the iGPU's sum. So the R9700 waited for the iGPU's whole branch before it ran its own experts.
  2. Unplaced per-token copies (two or more rows). With two or more rows, ROCmFP2 experts are computed token by token. The per-token copies of the input, routes and weights were not assigned to an owner, so the scheduler gave each copy the backend of its neighbour in graph order. The hot owner's first copy comes right after the cold owner's last node, so it ran on the iGPU. The R9700's experts again waited for the whole cold branch.

What changed

  • common/moe_overlap.h (new, header only, model independent). The rules for overlapping the two owners of a MoE layer:
    • queue the expert GPU's routed sum as soon as the routes exist (moe_overlap_queue_expert);
    • join the two F32 sums with a plain add (moe_overlap_join);
    • mark each join so the scheduler ends the main GPU's split right before it, and only there (moe_overlap_mark_join, the scheduler's existing per-node late cross-input split).
  • common/moe_hybrid_ffn_eval.
    • New input MoeHybridGraphInputs::overlap_owners. When it is set, build_moe_owner_join queues three things in order: the secondary owner's sum, then the main owner's routed experts and shared expert, then the join. The shared expert's nodes are placed on the main owner.
    • Token-by-token owners (build_batched_routed_graph): a token's input, routes and weights are column views of the batch, so no copy per token. The secondary owner gathers the input in one copy per layer; the main owner reads it in place. Any copy still needed is placed on its owner.
  • deepseek4/deepseek4_fused_verify.inc. Sets overlap_owners for paged steps whose two owners are GPUs of one runtime, without a device join, a cluster layer or a streamed layer, and marks their joins for the scheduler. One user, and every other path, keeps its graph.
  • common/paged_attention_config.h. LUCE_MOE_Q8_HANDOFF joins the switches paged steps leave out (a profile default is dropped, an explicit setting refused). It acts only on graphs built with a schedule graph, which paged steps now are, and paged mode was never qualified with it.

No new environment variables. The settings refused for paged mode stay refused.

Why it is exact

  • Scheduling only. The overlap changes the order in which nodes are queued and where the scheduler ends a split. Every node runs the same kernel on the same bytes.
  • Same bytes into the same kernels. The column views feed the expert kernels exactly the bytes the per-token copies held. Gathering the input in one copy moves the same values.
  • Same join. The join is the same ggml_add of the same two F32 sums.

Evidence (this branch against main, same box, graphs-OFF builds):

  • Slot logits, bit for bit. 142 of 142 logits rows are identical. The probe covers:

    • 1, 2, 3 and 4 lanes, in every lane order;
    • 8 packed rows (4 lanes x 2);
    • both phases.

    In each build, every (phase, prompt, position) point has one bit pattern whatever the lane count, lane order or row packing: 28 points in 142 runs.

  • test_ds4_paged_exact_model (every case passes):

    • PASS: prompts arriving while others decode (prompt@slot: 4@0 0@1 5@2 2@3)
    • PASS: 23 sequences, 64 greedy tokens each, identical alone and beside others
    • and the one- to six-lane slot and order cases, all PASS.
  • Server greedy texts are identical: alone, 2 at once and 4 at once, before and after (code, prose, a 2K-token document, SQL).

Results

Paged step time

Median of 5 runs, ms per step.

rows in the step before after change
1 lane 44.27 38.34 -13.4%
2 lanes 65.55 54.84 -16.3%
3 lanes 82.57 67.84 -17.8%
4 lanes 100.72 82.47 -18.1%
4 lanes x 2 rows = 8 rows 193.43 160.80 -16.9%

Scheduler splits per step (LUCE_DS4_TP_SPLIT_COUNT): before 161 at every width; after 201 at 1 row and 281 at 2 to 8 rows. Turning on the late split for the whole step instead (the scheduler's global flag, with the existing LUCE_DS4_TP_LATE_JOIN_SPLIT=1 on the same build, measured before main's split stack was merged) gives 281 and 321 splits and a step no faster (slower at 1, 2, 3 lanes and 8 rows, within 0.2 ms at 4 lanes), so only the joins are marked.

Server decode, paged

Manage launch flags: --profile ds41-r9700-gorgon --paged-attention --max-concurrency 4 --kv-pool-tokens 131072 --ds4-prefill sparse --max-ctx 131072, no drafter. Decode tok/s per request, median.

before after change
alone 23.57 27.33 +16.0%
2 at once 16.41 19.77 +20.5% (total 28.5 -> 34.3)
4 at once 9.58 11.79 +23.1% (total 25.4 -> 30.1)

The 4-at-once total includes a 2K-token prefill in each round.

Both GPUs busy

Kernel trace (rocprofv3, 3 runs), share of the step with both GPUs running kernels, and with neither. Measured on this branch and its base before main's split stack was merged in (the paged step graph of this PR is the same).

rows in the step before: both busy after: both busy before: neither busy after: neither busy
1 lane 0.3% 11.5% 24.9% 23.5%
4 lanes 0.9% 13.9% 24.8% 19.5%
8 rows 0.9% 12.7% 24.6% 19.2%

Other changes in the same trace:

  • While the iGPU runs a layer's cold experts at 4 lanes, the R9700 is now busy 0.36 ms of that time (was 0.03 ms).
  • Launches per extra row fell from 1005 to 791 on the R9700 and from 500 to 336 on the iGPU.

What it does not change

One user with the drafter. overlap_owners needs a paged step, and the one-user profile also uses the device join. The token-by-token path runs only for two or more rows without grouped products, and the one-user profile runs grouped products. So the one-user graph is the graph on main. Measured, two interleaved passes (before, after, before, after), decode tok/s:

pass 1 before pass 1 after pass 2 before pass 2 after
code 57.85 57.67 57.80 57.68
prose 44.94 44.98 45.21 45.20
2K document 49.58 49.60 50.12 49.61

Every difference is within the spread of the before runs themselves (49.58 and 50.12 for the 2K document). Texts are identical in all four runs.

Tests

Graphs-OFF build (this engine's product build):

  • New test_moe_hybrid_overlap_owners_order_and_placement (in test_server_unit, no GPU): a 2-row layer with token-by-token ROCmFP2 owners and a shared expert, built with overlap_owners:
    • every node reachable from the output has an owner or reads a weight;
    • in graph order, every secondary-owner node comes before every main-owner node, and the join comes last;
    • the input crosses to the secondary owner in one copy, the main owner reads it in place, and no owner copies per token.
  • test_feature_gate: LUCE_MOE_Q8_HANDOFF is refused for paged mode. Passed: 5 Failed: 0, exit 0.
  • test_server_unit: Passed: 1277 Failed: 0 Skipped: 4, exit 0.
  • test_deepseek4_unit and test_ds4_moe_combine_cuda (unchanged files, run as a regression check): exit 0; every combine case PASS (top_k 4 and 6, 1 to 401 tokens).

Graphs-ON build (Flash-Next's product build, same common code): luce_server compiles.

The logits probe above needs test-only engine hooks (forced rows per slot, a logits dump), so it stays out of the tree.

Known limits and follow-ups

  • The weight read in the join wait, for paged steps. Main now reads the main owner's next weights while it waits for the secondary owner's sum (ggml_moe_fused_prefetch, with the Engram rows due in the same wait), but only in the device-join branch, which paged steps do not take. Paged overlap steps would take it with the same two calls in the overlap branch (queue the wait work, then the read). That is a follow-up with its own measurement: on an earlier build that had both, the read was worth 0.6 to 1.2 ms per paged step. The copy tail from the same PR is set by Flash-Next's split scheduler only.
  • Flash-Next on the same rules. Qwen3.8-Flash-Next keeps its own copy of these rules in qwen4exp_graph.cpp (moe_join, the late-split setup in its split scheduler, and the routed sum it queues right after its routes). Moving it onto moe_overlap.h is a follow-up.
  • Grouped cold-expert products. The largest remaining per-row cost is the iGPU's cold experts, about +9 ms per extra row, bound by reading expert weights from memory. Grouped products would read each expert once per step instead of once per token. They are refused in paged mode after an unresolved host hang, so reopening them is a separate decision. The new unit test checks the token-by-token path; grouped products would need their own check.
  • Verified up to 8 rows per step. Exactness is verified up to 8 rows per step, the same limit as the paged lane isolation (fix(ds4): paged V4.1 lanes attend their own rows, whole routes at every width #981).

🤖 Generated with Claude Code

davide221 and others added 9 commits November 10, 2026 18:31
…ry width

On a profile that sets both fused F16 verify K/V flags (ds41-r9700-gorgon)
with --paged-attention, a sequence's output depended on the sequences
beside it.

A gathered lane took the fused F16 K/V branch, which reads the raw K/V
source from row 0. For a paged lane that source is the flat pool of every
slot's ring, so at layer 0 and at compressed layers without compressed
rows yet each lane attended the pool from slot 0's ring onward instead of
its own staged rows. A gathered lane no longer takes that branch.

A paged step with more than one row summed the six routed experts as
4 + 2 while a one-row step sums them in one combine, so a row's bits
depended on how many sequences shared its step. Paged steps keep the six
routes whole at every width.

test_ds4_paged_exact_model (optional full-model test, exits 77 without
the model or two GPUs) decodes six prompts alone and then in seven shared
layouts; every sequence's greedy tokens must equal its alone run. On main
21 of 23 sequences differ; with either hunk alone it still fails; with
both, 23 of 23 are identical.

Paged output on that profile changes with this fix. Steps with several
sequences also get faster (2 at once 76 -> 61 ms, 4 at once 123 -> 98 ms a
step on the tree the fix was written on).

Co-Authored-By: Claude <[email protected]>
…esign

Every paged step keeps the six routed experts in one owner graph, which is
what LUCE_DS4_TP_NATIVE_ROUTE_WIDTH selects, so that switch now changes
nothing under paged serving and leaves the paged refused list. Paged steps
also pass Disabled for dynamic route balance (it applies up to five rows
only, so a sixth row would change its company's arithmetic), and
LUCE_DS4_TP_HEAD4_TAIL2 no longer reaches paged steps.

The earlier commit's message understates fault 1: the F16 branch was taken
at layers 0 and 1 on every row and at ratio-2 layers at position 0, and slot
0 was wrong alone too (its gathered rows are padded to 16, the pool view is
not).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each case asserts the slots it names, the slot 0 cases check the prompt
lengths against the ring, the test skips (77) unless the visible GPUs are
the profile's, and its header states the invariant.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…oE split step

In the paged (multi-user) step the MoE layers are split between the R9700
(attention, dense work, the shared expert, hot experts) and the iGPU (cold
experts) without a device join. The scheduler copies a split's foreign
inputs before any of its nodes run, so the R9700 split that held the hot
experts, the shared expert and the join waited for the whole cold branch:
the two GPUs took turns.

A split step (new common/moe_split_step.h, written for any two-GPU MoE split):
the cold experts are queued right after the routes, then the hot experts and
the shared expert, then the join; the scheduler ends the R9700's split at the
join (late cross-input split), so its own work runs while the iGPU computes.
Scheduling only: every node computes the same bytes; one user (device join)
is not touched.

Co-Authored-By: Claude <[email protected]>
…copy per layer, all nodes placed

A token-by-token owner (ROCmFP2 experts at 2+ rows) copied each token's input,
routes and weights into tensors of their own, three launches per token, owner
and layer; the copies were not placed, so the scheduler gave each the backend
of its graph neighbour. In a split step the main owner's first copy follows
the secondary owner's last node: it ran on the iGPU and the R9700's hot
experts waited for the whole cold branch (no overlap at 2+ rows). A column of
a dense tensor is dense, so the views are read directly; the input is
gathered on the owner once per layer (one cross-device copy instead of one per
token); any copy still needed is placed on the owner. Same bytes into the same
kernels.

Co-Authored-By: Claude <[email protected]>
Paged steps now build their MoE layers with a schedule graph (overlapping
owners), which is what the Q8 handoff waits for: it would hand the secondary
owner Q8_1 rows of the input, a path paged steps were never qualified with.
List it with the other unqualified expert-owner switches, so a profile default
is left out and an explicit setting is refused.

Co-Authored-By: Claude <[email protected]>
…d a split

Review fixes for the overlapping-owner step:
- The overlap is set for paged steps only (the measured case). One user, a
  two-GPU launch without a profile and other engines keep their graphs.
- Each join is marked for the scheduler (ggml_backend_sched_add_late_cross_
  input_split_node) instead of turning on the late cross-input split for the
  whole step, which also split the secondary owner at each of its inputs.
- The join is a plain add of two F32 sums; the cast for an F16 sum is gone
  (it never ran for V4.1 and belongs to the engine that needs it).
- The main owner reads the layer input in place (no gather copy), and its
  shared expert's nodes are placed on it like its routed experts.
- common/moe_split_step.{h,cpp} becomes the header-only common/moe_overlap.h
  (moe_overlap_queue_expert, moe_overlap_join, moe_overlap_mark_join);
  MoeHybridGraphInputs::split_join becomes overlap_owners. The names no longer
  collide with the split-owner device join, split owners or split KV.

Co-Authored-By: Claude <[email protected]>
A 2-row graph with token-by-token ROCmFP2 owners and a shared expert, built
with overlap_owners and a schedule graph, on a no_alloc context: every node
reachable from the output has an owner list or reads a weight; every
secondary-owner node precedes every main-owner node in graph order and the
join comes last; the input crosses to the secondary owner in one copy and the
main owner reads it in place, with no copy per token.

Co-Authored-By: Claude <[email protected]>
@davide221 davide221 changed the title perf(ds4): paged hybrid steps overlap the two GPUs through a shared MoE split step perf(ds4): paged hybrid steps overlap the two GPUs through a shared MoE overlap step 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 marked this pull request as ready for review November 10, 2026 23:04

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

The lane-isolation fix this branch was stacked on is on main as #981, so
the branch now carries only the overlap step on top of main.

Co-Authored-By: Claude <[email protected]>
@davide221
davide221 changed the base branch from fix/ds4-paged-lane-isolation to main November 10, 2026 23:13
@davide221
davide221 merged commit 4de2dc2 into main Nov 11, 2026
10 checks passed
@davide221
davide221 deleted the perf/ds4-split-overlap 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.

1 participant