Skip to content

server: --pipeline-groups, run the slots over several contexts of one model - #187

Open
danielhanchen wants to merge 289 commits into
base/upstream-af50f2f24from
feature/pipeline-groups
Open

danielhanchen wants to merge 289 commits into
base/upstream-af50f2f24from
feature/pipeline-groups

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Sep 5, 2026 •

Copy link
Copy Markdown
Member

Adds --pipeline-groups N to llama-server: the slots are run over N independent
llama_context objects created from the same model, each with its own batch, its own sampling
and its own decode thread. The model weights, the task queue, the results queue and the HTTP
layer are shared. With N = 1 nothing changes.

The point is a layer split across two machines. With one context the split is a two-stage
pipeline fed one batch at a time, so each stage is idle while the other computes. With two
groups there are two batches in flight.

Result

Two DGX Sparks (GB10, 111.9 Gb/s per rail), Qwen3.8-27B-UD-Q4_K_XL, layer split over one
ggml-rpc-server on the peer, -c 16384 --parallel 32 --cache-ram 0 -fa on -ngl 99 -t 6,
32 concurrent clients, 64 requests, npp 128 / ntg 256, best of two repeats, all cells inside one
clock state (2386 to 2402 MHz local, 2433 to 2463 MHz peer, no thermal cap markers).

device order groups tok/s (run a / run b) TPOT ms TTFT median s GPU busy local GPU busy peer
CUDA0,RPC0 1 94.9 / 92.9 310 / 311 7.4 / 7.6 44 / 47 pc 43 / 41 pc
CUDA0,RPC0 2 75.5 / 76.3 395 / 398 6.3 / 5.8 43 / 42 pc 44 / 47 pc
CUDA0,RPC0 2, sampling pool off 73.9 / 75.3 408 / 404 6.5 / 5.8 44 / 43 pc 45 / 46 pc
RPC0,CUDA0 1 99.7 / 98.1 295 / 301 7.5 / 6.6 46 / 43 pc 47 / 46 pc
RPC0,CUDA0 2 130.4 / 131.3 223 / 220 5.7 / 6.1 76 / 76 pc 79 / 76 pc
RPC0,CUDA0 1, --backend-sampling 94.8 / 94.9 309 / 311 7.5 / 7.4 42 / 42 pc 44 / 43 pc
RPC0,CUDA0 2, --backend-sampling 120.4 / 123.7 233 / 230 8.0 / 6.4 71 / 74 pc 73 / 75 pc

1.32x over one context on the same split, at 76 to 79 percent GPU busy per node against 43 to 47
percent. Two things had to be true at once:

  1. The device order matters more than the feature. With -sm layer the devices are filled in
    the order they are listed, so the last device holds the output layer. List the RPC device
    first and the local one last (--device RPC0,CUDA0): the logits are then produced locally,
    which removes a n_vocab * n_rows * 4 byte return from every decode step (31.8 MB per step at
    32 rows on this 248320-token vocabulary) and lets the sampler read them out of local memory.
  2. Sampling had to come off the critical path. See below.

Slot count sweep, RPC0,CUDA0, --cache-ram 0, -c sized so every slot keeps 512 tokens:

slots groups 1 groups 2 ratio
32 99.7 tok/s 130.4 tok/s 1.31x
64 117.2 157.2 1.34x
128 124.2 170.1 1.37x
256 124.1 (1 request in error) 130.5 (126 of 512 in error) not comparable, see limits

What the cost was, and how it was found

LLAMA_SERVER_PIPE_PROF=1 (added here) prints, per group and every five seconds, how long an
iteration spends waiting for the engine, in pre_decode, in llama_decode, in
llama_synchronize and in post_decode, and splits post_decode per token into sampling,
detokenization, stop-string handling and the result queue. On the pair, per group iteration:

groups 1, 32 rows groups 2, 16 rows per group
wait for the engine lock 0.00 ms 0.00 ms
pre_decode 0.01 ms 0.01 ms
llama_decode 145 ms 200 to 262 ms
llama_synchronize 148 ms 95 to 126 ms
re-take the engine lock 0.00 ms 0.00 ms
post_decode 21 ms 54 to 76 ms
whole iteration 316 ms 401 ms
per token: common_sampler_sample 0.60 ms 3.6 to 4.6 ms
per token: common_token_to_piece 0.002 ms 0.002 ms
per token: process_token (stop strings, streaming) 0.065 ms 0.18 ms
per token: queue_results.send 0.061 ms 0.18 ms

Sampling one row builds a candidate array over a 248320-token vocabulary, about 4 MB of memory
traffic. With one context that pass runs in the gap where both GPUs are idle and gets the full
memory bandwidth of the node. With two groups it runs against the other group's GPU work on the
same LPDDR5X and costs six to seven times more per row, while sitting on each group's critical
path between the synchronize and the next submit. Every other candidate was measured and
falsified: the results queue and the task queue are three orders of magnitude too small, and the
engine lock was never contended (0.00 ms in every window; forcing the old single shared host lock
back on with an A/B switch gave 74.7 against 73.5 to 75.3 tok/s).

Changes

  • --pipeline-groups N, parsed in tools/server because it only means anything for the server.
    Slots are partitioned contiguously; each context gets n_ctx / N and n_seq_max = P / N, so
    the per-slot context and the total KV over all groups are what the user asked for. Slot
    selection still runs over all slots, so prompt-cache similarity and the slot save / restore
    endpoints behave exactly as before. N > 1 is refused together with speculative decoding,
    multimodal, control vectors and --sleep-idle.
  • Each group samples its rows over a small worker pool. The rows are independent (own sampler,
    own row of the logits), so the tokens are the ones the serial pass would have produced. The
    thread budget is divided by the number of groups, so a pipeline-groups run is not simply given
    more CPU than the single-context run. LLAMA_SERVER_SAMPLE_THREADS=1 turns it off.
  • The engine lock is per group, so the host path of one group does not exclude the other's. This
    is worth nothing on its own on this hardware and is reported as such, but it is what the
    feature is supposed to guarantee.
  • Task processing pauses only the group that owns the slot the task touches, instead of every
    group. This is what fixed TTFT (29 s to 7 s at 32 concurrent).
  • get_available_slot() called prompt_save / prompt_load, which read and write the slot's
    sequence KV, before the guard waited for the owning group's decode to finish. With
    --cache-ram 0 the cache is null so it never fired; with the cache on it is a live race
    against a running decode and it matches the 29 s TTFT and the abort seen while developing this.
    The cache update now happens after the wait.
  • server_metrics is no longer written from several group threads at once, and each group counts
    its own slots instead of every slot of the server (which double counted n_busy_slots).
  • ggml-rpc: a whole message is now atomic on the wire. The socket is cached per endpoint and
    shared by every backend of that endpoint, and a message was three unlocked writes, so two
    contexts interleaved their command streams and --pipeline-groups 2 aborted with "Remote RPC
    server crashed or returned malformed response" within seconds. Responses are handed out in
    request order by a ticket, so a waiter does not hold the send lock. last_graph_uid moved to
    the connection and is checked under that lock, which closes a hazard where GRAPH_RECOMPUTE
    could re-run the other context's graph.

Correctness

Per-group speculative decoding (the section below), on top of the proofs already listed here:

  • Single GPU, five prompts, 48 tokens, temperature 0: greedy output byte identical between the PR
    base, --pipeline-groups 1 and --pipeline-groups 2 on this branch, md5
    e154ffeace8e6d57298e1963f16529b5 for all three.
  • Qwen3.5-4B-MTP on one GPU, --parallel 8: groups 1 and groups 2 give the same sequential greedy
    md5 with MTP (4dae418e8227bddb0fc0e93ab11a9e67) and the same without it
    (43e68a9c6ffafce623dd8089192cdfc3); all eight slots of both groups serve requests and
    /metrics reports drafted and accepted tokens in both MTP arms.
  • tools/server/tests/unit/test_speculative.py (--model-draft sidecar, context shift,
    context-not-exceeded, parallel requests): 6 passed with one group, 6 passed with two.
    test_basic.py, test_completion.py, test_ctx_shift.py, test_slot_save.py: 59 passed,
    1 skipped, 1 failed in both arms, the failure being a preset whose model is not in the offline
    cache. Slot save / restore and context shift therefore also run over two groups.
  • Greedy output byte-identical at N = 1, N = 2 and N = 4 over a CPU-only two-RPC split,
    five prompts, md5 177dc61e0703eba3bdaf7bf1131f0458, same as the unmodified binary.
  • tools/server/tests run serially against this build and against the unmodified branch head on
    the same machine: 325 passed, 233 failed, 6 skipped, 9 errors on both, with byte-identical
    failure name sets. The failures are pre-existing in that environment (the cached test models do
    not match), not introduced here.
  • --pipeline-groups 2 with the sampling pool on and off gives the same test results.

Speculative decoding per group

--pipeline-groups N now combines with speculative decoding (--spec-type draft-mtp with the MTP
head inside the GGUF, and --model-draft with a sidecar draft model). Each group owns its own
speculative state:

  • one draft or MTP llama_context, created by common_speculative_init_from_params against the
    group's target context, so ctx_other and the next-token embedding hooks point at that context;
  • one common_speculative, sized for the group's P/N sequences and addressed by the slot's
    sequence id inside the group (slot.seq_id, which is slot.id with one group);
  • the draft batches go through the group's draft context on the group's decode thread, like
    llama_decode already does; the two task-queue yields around the drafter are only taken on the
    single-group main-thread path.

Slot save / restore, context checkpoints (the drafter state is stored and restored with the
checkpoint) and the prompt cache behave as before, per slot. With N = 1 the code path is the same
as before, spelled through groups[0]. With --model-draft the sidecar model is loaded once per
group. validate_pipeline_groups no longer refuses drafters; --mmproj, --control-vector and
--sleep-idle-seconds are still refused with N > 1.

Two DGX Sparks, Qwen3.8-27B-UD-Q4_K_XL, layer split over one ggml-rpc-server on the peer
(--rpc peer:50055 --device RPC0,CUDA0 -sm layer), -c 16384 --parallel 32 --cache-ram 0 -fa on -ngl 99 -t 6, real-text prompts, npp 128 / ntg 256, 2 requests per client. MTP is
--spec-type draft-mtp --spec-draft-n-max 3 off the head inside the GGUF. Two passes with the arm
order reversed, because the SM clock decays through a window; the cells below all ran with both
GPUs at 2390 to 2400 MHz and no cap marker (the three cells that were capped or straddled a cap
stage are listed after the table, and each of them has a clean repeat).

groups MTP 8 users tok/s TPOT ms 32 users tok/s TPOT ms accepted/drafted 8 / 32
1 no 55.3 136 96.9 311 -
1 yes 95.5 / 93.6 74 / 75 112.9 / 114.2 233 / 232 0.80 / 0.64
2 no 54.0 139 115.7 219 -
2 yes 77.1 84 133.8 200 0.72 / 0.68

At 32 users the combination is the best cell measured: 1.38x over one context without speculation,
1.17x over one context with MTP and 1.16x over two groups without it. At 8 users it is not: one
context with MTP is faster (95.5 against 77.1), because two groups halve the rows per group and the
draft head is at its most valuable when the batch is small and the step is memory bound. The
crossover is between 8 and 32 rows, which is the same crossover the --pipeline-groups result
itself has.

Cells with a clock caveat, each superseded by the clean repeat in the table: one group without MTP
in the first pass ran at 1828 MHz (53.6 / 95.5), two groups without MTP in the reversed pass at
1690 MHz (51.5 / 108.7), two groups with MTP in the reversed pass straddled a cap stage
(92.0 / 128.3). Every 32-user cell in every arm, including the two without speculation, had one
request of 64 return no tokens; it is present in the arms this PR does not touch, so it is not a
property of per-group speculation.

Known limits

  • Use --cache-ram 0 with a layer split. The RAM prompt cache moves a whole slot state on
    every slot handover, and on a split most of that state lives on the remote node, so it crosses
    the wire. At 32 concurrent clients on the pair, with the cache at its default: one group 75.9
    tok/s and 33 s median TTFT (against 99.7 and 7.5 with --cache-ram 0); two groups 6.9 tok/s
    with 14 of 64 requests timing out, because the handover runs on the single task thread and the
    other group starves while it does. This is a property of the cache on a split, and one group is
    already badly hurt by it, but two groups make it much worse and it is not fixed here.
  • At 256 slots the two-group cell finished 386 of 512 requests, the rest ending with a truncated
    HTTP response; the server log records no error, so the truncation is on the HTTP path at 256
    concurrent streams, and the node crossed 80 C during that cell. That row is reported with its
    error count and is not a clean measurement. 32, 64 and 128 slots are clean.
  • The gain is specific to a layer split. On a single node there is nothing to pipeline.

Update 2026-09-07: the flag is worth a good deal more than the figures above

The best number in this description is 1.38x at 32 users with two groups and MTP. That was measured before three launch settings were understood, and with all three the same mechanism reaches 1.77x: 209.5 tok/s against 118.6 for one context on the same split. Qwen3.8-27B UD-Q4_K_XL, two nodes, --device RPC0,CUDA0 -sm layer, both GPUs pinned at 1690 MHz.

rows per group tok/s vs one context
32 143.3 1.20x
64 182.7 1.54x
128 209.47 mean (197.2 to 215.8) 1.77x mean, 1.81x best
192 204.3 1.72x
256 206.4 1.73x

What the earlier cells were missing:

  • Rows per group. 32 was never swept. The curve is not linear and it peaks at 128, then turns over. This is the whole difference between 1.20x and 1.814x and it is a property of how much work each group has to amortise its half of the step against, not of the flag.
  • --kv-unified. Worth 23 to 30 percent at 32 rows on a two group arm and nothing at all on one context, so it never showed up in a single context comparison.
  • --tensor-split 0.5,0.5. Without it llama.cpp splits by free memory at load time, so the layer boundary moves between runs and two cells are not comparable. Five boundaries were measured and the even one wins.

Two other things worth recording for anyone reading the numbers above. Speculation does not compose with this at high concurrency: --spec-draft-n-max 3 is the worst of the three depths at every row count on a split, and above 64 rows speculation of any depth loses (212.72 tok/s off against 164.06 at n=1 and 132.24 at n=3 at 128 rows). Acceptance is a property of the depth rather than the rows, 0.87 / 0.78 / 0.69 at depths 1 / 2 / 3, so a deeper draft accepts strictly more tokens per step and still loses throughput. And more groups is not better: four groups is 10.3 percent slower than two and three groups 11.0 percent slower, because the bottleneck GPU is already 91.55 percent busy on CUDA event accounting at two groups and what is left is scheduling inside prompt batches, not idle waiting for a neighbour.

Cost, stated honestly: at 128 rows the TTFT of a saturating burst is 18.1 s median and 27.2 s p99, against 4.6 and 5.6 at 32 rows. --parallel should be sized to the expected load. A server built for 128 rows and driven at 32 gives 110.3 tok/s against 143.3, so oversizing loses 23 percent of throughput and 38 percent of median TTFT at the same time.

Correction 2026-09-08: quote the mean, not the best leg

The 1.814x above was the best of three legs. Re-measured with the original harness on the same binaries, two further legs read 1.789x and 1.665x, with server-side token counters agreeing with the client to 0.01 percent and resident memory matching to the megabyte, so the layer boundary was identical. Pooling all five legs gives a two-group arm of 197.2 to 215.8 tok/s, mean 209.47, against a one-context arm of 118.42 to 118.96, mean 118.63.

So the honest headline for this flag is 1.77x, range 1.67 to 1.81 over five legs, on a saturating 128-row burst whose leg-to-leg spread is 9 percent. The mechanism and the magnitude are unchanged; only the statistic was too flattering.

Two things confirmed while settling this. The comment-reduction pass on this branch is inert: stripping comments with gcc -fpreprocessed -dD -E, all five touched files are token-identical between a1dd7c5e8 and d98d90dd8, and the current head measures 1.759x in the same clock state, inside the leg scatter. And generation length is a real axis worth stating: the same leg reads 1.789x at ntg 256 and 1.714x at ntg 128, because the aggregate ratio is diluted by prefill. The decode-only TPOT ratio is 1.947x.

Scope, relative to the base branch

This PR targets master, and against that base it changes 7 files, 1197 insertions and 231 deletions.
Stated explicitly because a reviewer diffs against the base, not against the point in a stack a
branch was cut from, and a scope claim measured from the wrong reference is exactly the sentence a
reviewer trusts instead of checking.

oobabooga and others added 30 commits June 10, 2026 07:45
Add AGPLv3 SPDX headers to the Unsloth prebuild workflows
Prebuilds: merge a set of upstream PRs into the GitHub Actions builds via `pr-set.json`
The mix-build pin for ggml-org#24423 lagged behind the PR head, so
the prebuilds predated the streaming visual server. Bump the pin to the head
that carries the visual server (self-tokenizing, on-device entropy-bound
decoder) and its fixes, and make the build actually produce and ship it:

- scripts/unsloth/pr-set.json: pin ggml-org#24423 at 10a2613 (was c84e85a, 8 commits
  behind -- before the visual server existed).
- CUDA (Linux + Windows) and macOS children: a best-effort step builds
  llama-diffusion-gemma-visual-server + llama-diffusion-cli after the required
  server/quantize build. It reconfigures with LLAMA_BUILD_EXAMPLES=ON and builds
  only those targets, and it can never fail the job: a toolchain that cannot
  build them still publishes the normal bundle. ROCm already builds all examples
  and tars the whole bin dir, so it picks them up with no change.
- scripts/unsloth/package_bundle.py: ship the two DiffusionGemma binaries when
  the build produced them (optional, absent in a plain upstream build, so unlike
  the required binaries their absence is not an error). They join the closure
  walk; their only local libs (libllama, libggml, libllama-common) are already
  pulled by llama-server, and they inherit the same $ORIGIN rpath, so the bundle
  stays self-contained and relocatable.

This lets Unsloth Studio serve unsloth/diffusiongemma-26B-A4B-it-GGUF natively
from the prebuilt bundle, with no DG_* env or source build.
The -j 2 pin on the CUDA Linux and Windows builds was a conservative
hedge against nvcc memory spikes. A 10-run sweep on stock GitHub runners
(4 vCPU / 16 GB) over the multi-arch fanout this prebuild compiles shows
peak host RSS near 3 GB and zero OOMs, so 3 parallel TUs leave about
13 GB free. -j 3 cuts build time roughly 23 percent (about 70 down to
54 min per job); -j 4 adds no further speed (the runner is about 2
physical cores plus HT). macOS and ROCm already build at full cores and
are unchanged.

Applies to both the required server/quantize build and the best-effort
DiffusionGemma example build in each CUDA child.
…de vs visualization STATS split

Picks up the honest per-turn stats: separates model decode time from the per-step
visualization overhead and drops the meaningless prompt tok/s, plus VRAM-aware MAXTOK
auto-sizing and a toolong budget report. Rebuilds llama-diffusion-gemma-visual-server.
…greater overload

The windows-latest image rolled from VS 2022 to VS 2026 (MSVC 14.51), whose STL marks
math builtins like isgreater constexpr (implicitly host+device); that collides with
clang's HIP __device__ forward declares and breaks the ggml-cuda .cu compile
(ggml-org#22570). Pin vswhere -latest to the 17.x line so the build uses the
VS 2022 toolset this job already installs, matching the last known-good release.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 50f45e0363

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…at once

Slot selection now only considers the least loaded groups that still have a free
slot, so below full load requests no longer pile into one group. An idle group
wakes as soon as a slot is launched into it instead of on its 5 ms timer, and a
group decoding a long prompt lets a waiting task thread in between chunks.
danielhanchen added a commit that referenced this pull request Oct 18, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: d2402d7ba8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen added a commit that referenced this pull request Oct 21, 2026
# Conflicts:
#	tools/server/server-context.cpp
… control handler

With --pipeline-groups > 1, wait_for() lets the group finish its in-flight decode, which can finish the
request and release the slot; reasoning_end then dereferenced a null slot->task.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 3c19cd51cb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen added a commit that referenced this pull request Oct 22, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 2ad1e9838f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

This branch has not been deployed

No deployments
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.

3 participants