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

Merged
DA
24d ago
+140-12

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 tokensmainbranchchange (means)
9752.89, 2.592.38, 2.34-14%
4,4588.15, 8.107.03, 7.07-13%
16,84428.85, 28.8624.70, 24.61-15%
60,948179.1, 180.989.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 tokensmainbranchchange
68,727226.4101.7-55%
181,696683.5293.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 memory60K main60K branch16K main16K branch
1540 MiB (below the headroom: no row fits)251.7252.770.067.6
1540 MiB, planner forced to 1 row (two bands do not fit)251.7252.770.070.4
2000 MiB (the first pass fits 3840 rows)243.6232.969.963.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 + thischange
9752.632.33-11%
4,4588.166.99-14%
16,84428.8724.46-15%
60,948180.687.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