Skip to content

perf(ds4): faster layer-major prefill, same bits - #988

Merged
davide221 merged 2 commits into
mainfrom
perf/ds4-prefill-arenas-passes
Nov 11, 2026
Merged

davide221 merged 2 commits into
mainfrom
perf/ds4-prefill-arenas-passes

Conversation

@davide221

@davide221 davide221 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

What

Two small changes to the DeepSeek V4 layer-major prefill when it is split across two devices: dense
work and hot experts on the primary GPU, cold experts on a second device. Neither changes what a
kernel computes: (1) changes where owner arenas live, (2) changes which bands run together in one
pass, never which bands run layer-major or how they are cut into pipeline parts. First-token logits
and the whole post-prefill state hash identical to main (below).

Who gets it: the single-user launch of DeepSeek V4.1 on an R9700 beside a 192 GB Ryzen AI Max+ PRO
495 (--profile ds41-r9700-gorgon, which sets LUCE_DS4_LAYER_MAJOR_DEVICE_RESIDUAL=1,
LUCE_DS4_PREFILL_PIPELINE=2 and dense prefill; the persistent owner arenas are on by default).
Several users at once (--paged-attention ... --ds4-prefill sparse) do not run the layer-major
prefill, and the ds41-halo and ds41-gorgon profiles leave the device residual off, so neither
change acts there.

  1. Persistent owner arenas for pipelined parts. The routing, hot-owner and cold-owner arenas
    already persist across calls from 512 rows. Pipelined prefill runs each caller band as
    max(LUCE_DS4_PREFILL_PIPELINE, 2) parts. When the band is capped below 1024 rows (tight primary
    memory), every part falls under the 512-row gate, and each (layer, part) creates and frees three
    arenas. Each free synchronizes its device, and the cold arena's (about 4 ms) sits on the cold
    device's serial chain: the next part's cold owner waits for the previous one. A pipelined part
    (its device outputs carry a deferred cold slot) now keeps the arenas, as parts of 512 rows or
    more already do.

    The persistent cold arena also selects eager execution for the deferred cold owner
    (heterogeneous_prefill_eager_enabled), as for parts of 512 rows or more: the same kernels. HIP
    builds compile graph replay out by default (GGML_HIP_GRAPHS=OFF; the measured build and the
    product's both have it off), so on this box the whole gain comes from the dropped allocations,
    frees and their syncs. On builds with graph replay the 320-row cold owners now run eagerly.

  2. Passes split so the residual stays on the device. A layer-major pass keeps its rows'
    residual on the device only when it fits beside 1.5 GiB of free memory. If it does not fit, the
    residual goes to the host and the pipeline turns off, much slower per row. Later passes find less
    free memory than the first, so on a long prompt the middle 16K-row passes fell back to the host.

    • The prompt loop groups bands into passes exactly as before. A pass now runs its leading bands
      that fit and leaves the rest as the next pass; the replay tail counts only for the part that
      reaches it. A split never crosses a pass boundary of main.
    • At least two bands run together, and a pass whose first two bands do not fit stays whole: its
      residual is on the host either way, and fewer passes cost less. With no row free, or the
      device residual off, nothing changes.
    • A band split off a layer-major pass runs layer-major even when alone, so which bands run
      layer-major, and their pipeline parts, never depend on free memory.
    • Cluster ranks keep whole passes: every rank must plan the same chunks.
    • One fit rule (deepseek4_layer_major_residual_rows) serves the planner and the placement, and
      each pass that falls back to the host residual is now logged (before, only the first placement
      was).

    Trimming the pool before the fit check does not help: about 660 MiB of cached blocks returned to
    the driver raised its free count by about 12 MiB.

Numbers

DeepSeek V4.1 Flash, R9700 (primary) + Ryzen AI Max+ PRO 495 iGPU (cold experts), the qualified
placement, DSpark drafter on, one user, greedy. Chunk 640 (memory bound), two pipeline parts per
band. Time to first token, interleaved main / branch server pairs on the committed head, long
answers (300 tokens compared):

prompt tokens main branch change (means)
975 2.89, 2.59 2.38, 2.34 -14%
4,458 8.15, 8.10 7.03, 7.07 -13%
16,844 28.85, 28.86 24.70, 24.61 -15%
60,948 179.1, 180.9 89.1, 88.3 -51%

Main's 1K time varies between runs (2.53 to 2.89 s in four runs, the branch 2.26 to 2.38 s), so
the 1K figure is the least certain.

Decode is unchanged: the 300-token answers ran at 44 to 51 tokens/s on both, within 2% of each
other at every size.

Engine timing (LUCE_DS4_TIMING=1), main -> branch:

  • 16K: wall 29.0 -> 24.7 s. hc_post, which includes the pipeline join wait for the cold owner,
    8.4 -> 5.3 s.
  • 60K: wall 182.7 -> 88.3 s. Synchronous cold FFN 64.8 -> 0.3 s: every pass now pipelines.

Where the time went (in-process trace, main):

  • 16K: the cold device's graph (16.2 ms per 320-row part, which reads about 3.6 GiB of cold
    experts, close to its memory bandwidth) is the critical chain. Between parts the cold device sat
    idle 8.8 ms on average, about 4 ms of it the per-part arena free. Both GPUs were idle together
    for 28% of the prefill.
  • 60K: main's passes 2 and 3 (16,000 and 16,126 rows) took 63 s each, against 29.6 s for pass 1.
    Their residual was on the host: a 25 MiB round trip each step, and no pipeline. Both GPUs were
    idle together for 46% of the prefill.

Longer prompts, time to first token, text identical:

prompt tokens main branch change
68,727 226.4 101.7 -55%
181,696 683.5 293.5 -57%

At 181,696 tokens the branch ran 22 passes, every one with the residual on the device, and neither
build logged a prefill retry.

Tight primary memory (a second process holds R9700 memory so 1540 or 2000 MiB stay free after the
warm-up), time to first token, main / branch, text identical in every case, no out-of-memory:

free R9700 memory 60K main 60K branch 16K main 16K branch
1540 MiB (below the headroom: no row fits) 251.7 252.7 70.0 67.6
1540 MiB, planner forced to 1 row (two bands do not fit) 251.7 252.7 70.0 70.4
2000 MiB (the first pass fits 3840 rows) 243.6 232.9 69.9 63.0

Same bits

  • First-token logits, the DSpark feature tail and all 55 tensors of the post-prefill continuation
    state (raw, compressed and indexer KV, compressor states, HC state; 200 MB at 60K) hash
    identical to main after every prefill call at 16,833 and 60,937 prompt tokens. At 60K main runs
    passes of 16000 / 16000 / 16126 / 12680 rows and the branch 16000 / 13440+2560 / 10366+5760 /
    12160+520.
  • Bounded replay off, passes forced to leave a lone band (24 of 25 bands fit): identical to main
    at 16K and 60K. The previous version of this change regrouped bands after a split. There it ran,
    inside a layer-major pass, a band that main runs alone as an ordinary chunk, and its first-token
    logits and 50 of 55 state tensors differed from main at 16K. Splitting only inside main's passes
    fixes that case.
  • 300-token greedy answers identical to main at 975, 4,458, 16,844 and 60,948 prompt tokens.
  • Four 60K prompts sent at once to the single-user launch (served one after another): each
    prefill took 87 to 96 s with every pass on the device and the same passes for each request, and
    each answer equals the same prompt sent alone (4 of 4). On the previous version, which splits
    prompts this short the same way, 2 x 4K, 4 x 4K and 2 x 16K at once on a fresh server: 8 of 8
    equal to alone.
  • Unit test test_layer_major_passes_fit_the_device: the fit rule against the old inequality, and
    the pass split (no bound, two-band minimum, tail, equality, a pass whose first two bands do not
    fit).
  • No new environment variables or options.

With the two-GPU overlap PR

Merged cleanly onto perf/ds4-split-overlap (#983). One interleaved pair, #983 against #983 with
this branch, 300-token answers identical to main at every size:

prompt tokens #983 #983 + this change
975 2.63 2.33 -11%
4,458 8.16 6.99 -14%
16,844 28.87 24.46 -15%
60,948 180.6 87.7 -51%

Not covered

  • Measured on one box (R9700 + Ryzen AI Max+ PRO 495) with the qualified DS4.1 placement only.
    Placements where parts already reach 512 rows see no change from (1).
  • (2) changes nothing when a full pass already fits, as for prompts up to about 16K here.
  • Persistent arenas stay held after a cancelled prefill until the next long one, as main already
    does for parts of 512 rows or more.
  • Cluster mode keeps whole passes; it is not measured here.
  • The window where only one or two bands fit was reached by forcing the planner in a measurement
    build, not by memory pressure alone.

🤖 Generated with Claude Code

View guided diff

davide221 and others added 2 commits November 11, 2026 07:34
Pipelined layer-major prefill runs each caller band as
max(LUCE_DS4_PREFILL_PIPELINE, 2) parts. With the band capped to 640
rows by primary memory, every part fell under the 512-row gate for the
persistent routing and owner arenas, so each (layer, part) allocated and
freed three arenas. Each free synchronizes its device, and the cold
arena's free (about 4 ms) sat between one part's cold owner and the
next, which drive the cold device in turn. A pipelined part now keeps
the arenas, the path parts of 512 rows and more already take.

The persistent cold arena also selects eager execution for the deferred
cold owner (heterogeneous_prefill_eager_enabled), as for parts of 512
rows and more: the same kernels, so the same bits. HIP builds compile
graph replay out by default (GGML_HIP_GRAPHS=OFF, as measured), so
there the gain comes from the dropped allocations, frees and their
syncs alone.

Same bits: generated text identical to main at 1K, 4K, 16K and 60K
prompts.

Co-Authored-By: Claude <[email protected]>
A layer-major pass keeps its rows' residual on the device only when it
fits beside 1.5 GiB of free memory. Otherwise the residual moves to the
host and the pipeline turns off, much slower per row. Later passes of a
long prompt find less free memory than the first, so their 16K-row
spans fell back to the host.

The prompt loop still groups bands into passes as before. A pass now
runs its leading bands that fit on the device and leaves the rest as the
next pass. The replay tail counts only for the part that reaches it.
At least two bands run together, and a pass whose first two bands do
not fit stays whole: its residual is on the host either way, and fewer
passes cost less. A split never changes which bands run layer-major or
how they are cut into pipeline parts: a lone band split off a
layer-major pass runs layer-major too, so the output does not depend
on free memory. Cluster ranks keep whole passes, since every rank must
plan the same chunks.

One fit rule (deepseek4_layer_major_residual_rows) now serves both the
planner and the placement, with a CPU unit test, and each pass that
falls back to the host residual is logged.

Same bits: first-token logits and every tensor of the post-prefill state
hash identical to main at 16K and 60K prompts.

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

Copy link
Copy Markdown
Contributor Author

CI failed on ab55f7c2

A Qwen3.8 router-fusion bit-parity test failed on the GB10 runner; the PR's DeepSeek4 changes are unrelated.

One GPU test that checks a fused Qwen3.8 router kernel produces bit-identical output failed by a single float on the self-hosted GB10 runner. The pull request only touches DeepSeek4 prefill planning and arena code, which this test never exercises, so the change under test is not at fault. Re-running the job is the first step; if it fails again at the same spot it is a pre-existing kernel issue on this new GPU, not this PR.

How to fix: Re-run the failed GPU job; the failing parity test is unrelated to this PR's DeepSeek4 changes.

  1. In GitHub Actions, choose "Re-run failed jobs" for the GPU tests (self-hosted DGX GB10, sm_121 verifier) job.
  2. If it passes, merge as normal — the failure was a flake in route_norm_reuse on the GB10 runner.
  3. If it fails again at route_norm_reuse T=1024 byte 840, run the same ctest filter on a main-branch build on that runner to confirm it is pre-existing, and open a separate issue for the route-norm fusion at T=1024 on sm_121 rather than blocking this PR.

gpu bit-parity test failure · high confidence · run

Details and evidence

Failed: GPU tests (self-hosted DGX GB10, sm_121 verifier) › Run Qwen3.8 split fusion parity test

Job GPU tests (self-hosted DGX GB10, sm_121 verifier), step Run Qwen3.8 split fusion parity test (ctest -R '^test_qn_split_fusions\.'). One case failed: test_qn_split_fusions.QnSplitFusionsFixture.route_norm_reuse, with CHECK: route_norm_reuse T=1024: output 0 (DIV) differs at byte 840 (server/test/unit/test_qn_split_fusions.cpp:191, the fixture's fail; the case itself is at server/test/unit/test_qn_split_fusions.cpp:335). The test computes the router weight renormalization (GET_ROWS/SUM_ROWS/CLAMP/DIV) node-by-node as a reference and then as one graph where the fused GGML_CUDA_QN_ROUTE_NORM kernel runs (server/deps/llama.cpp/ggml/src/ggml-cuda/ggml-cuda.cu:6407), and demands bit-equal outputs. Inputs are deterministic (std::mt19937 rng{1234}, server/test/unit/test_qn_split_fusions.cpp:54), so the fused kernel produced one different float (byte 840 = element 210, i.e. token 21 of the [10, 1024] output) on the GB10 (sm_121). T=1024 is the largest size for which the fusion is taken (T > 1 && T <= 1024, line 362), and the smaller sizes passed. The PR diff touches only DeepSeek4 prefill code (deepseek4_backend.cpp, deepseek4_graph.cpp, deepseek4_internal.h, test_deepseek4_unit.cpp, ENVIRONMENT.md); nothing in the ggml-cuda Qwen router fusions or this test, so the failure cannot be caused by these commits. I cannot tell from the logs whether this is a flaky race or a deterministic pre-existing sm_121 kernel bug at T=1024; a main-branch run on the same runner would decide that.

Other things to try

  • Reproduce on the GB10 runner or a local GB10: ctest --test-dir server/build --output-on-failure -R '^test_qn_split_fusions\.QnSplitFusionsFixture\.route_norm_reuse$' and note whether byte 840 diverges every run.
  • Run the same test on a main-branch build on the GB10 runner to confirm the failure pre-dates this PR before merging.
  • If it reproduces deterministically, inspect the fused route-norm launch (ggml_cuda_moe_route_weights_norm) at exactly 1024 tokens on sm_121 — the boundary where the fusion is last taken.
Prompt for a coding agent
A GitHub Actions run failed in Luce-Org/lucebox. Please fix it.

- Pull request #988 (branch `perf/ds4-prefill-arenas-passes`), commit ab55f7c2bc3484a0e1c3308f7970ea955f2d5940
- Workflow: CI (https://github.com/Luce-Org/lucebox/actions/runs/42990606118)
- Failed: GPU tests (self-hosted DGX GB10, sm_121 verifier) › Run Qwen3.8 split fusion parity test

Diagnosis:
Job `GPU tests (self-hosted DGX GB10, sm_121 verifier)`, step `Run Qwen3.8 split fusion parity test` (`ctest -R '^test_qn_split_fusions\.'`). One case failed: `test_qn_split_fusions.QnSplitFusionsFixture.route_norm_reuse`, with `CHECK: route_norm_reuse T=1024: output 0 (DIV) differs at byte 840` (server/test/unit/test_qn_split_fusions.cpp:191, the fixture's `fail`; the case itself is at server/test/unit/test_qn_split_fusions.cpp:335). The test computes the router weight renormalization (GET_ROWS/SUM_ROWS/CLAMP/DIV) node-by-node as a reference and then as one graph where the fused `GGML_CUDA_QN_ROUTE_NORM` kernel runs (server/deps/llama.cpp/ggml/src/ggml-cuda/ggml-cuda.cu:6407), and demands bit-equal outputs. Inputs are deterministic (`std::mt19937 rng{1234}`, server/test/unit/test_qn_split_fusions.cpp:54), so the fused kernel produced one different float (byte 840 = element 210, i.e. token 21 of the [10, 1024] output) on the GB10 (sm_121). T=1024 is the largest size for which the fusion is taken (`T > 1 && T <= 1024`, line 362), and the smaller sizes passed. The PR diff touches only DeepSeek4 prefill code (deepseek4_backend.cpp, deepseek4_graph.cpp, deepseek4_internal.h, test_deepseek4_unit.cpp, ENVIRONMENT.md); nothing in the ggml-cuda Qwen router fusions or this test, so the failure cannot be caused by these commits. I cannot tell from the logs whether this is a flaky race or a deterministic pre-existing sm_121 kernel bug at T=1024; a main-branch run on the same runner would decide that.

Proposed fix: Re-run the failed GPU job; the failing parity test is unrelated to this PR's DeepSeek4 changes.
1. In GitHub Actions, choose "Re-run failed jobs" for the `GPU tests (self-hosted DGX GB10, sm_121 verifier)` job.
2. If it passes, merge as normal — the failure was a flake in `route_norm_reuse` on the GB10 runner.
3. If it fails again at `route_norm_reuse T=1024` byte 840, run the same ctest filter on a main-branch build on that runner to confirm it is pre-existing, and open a separate issue for the route-norm fusion at T=1024 on sm_121 rather than blocking this PR.

Check out that commit, confirm the diagnosis, make the fix (adjust it if the diagnosis is wrong), and re-run the failing step locally before pushing.

Reply with /ci-fix and what to change to get a revised diagnosis.

@davide221
davide221 marked this pull request as ready for review November 11, 2026 10:51

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

@davide221
davide221 merged commit 8739f60 into main Nov 11, 2026
27 of 28 checks passed
@davide221
davide221 deleted the perf/ds4-prefill-arenas-passes 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