Repository navigation
server: reuse slot samplers across requests to avoid sched_reserve rebuilds - #274
Conversation
|
Measurements + both review items, on Strix Halo (RX 8060S, ROCm 7.2, HIP build, Release). Setup — merge-base Reserve elimination (the point of the PR)
So: zero sampler-caused reserves with the PR, ~15 ms/request saved. The latency medians are within noise at 64 tokens/request (the saving is per-request, not per-token, so it amortizes; it shows up clearly in the reserve counter, and proportionally more for short requests). I'm reporting the reserve counts as the primary evidence rather than dressing up the latency delta. Review item 1 — Review item 2 — Both fixes are in the pushed head; the A/B numbers above are from the fixed build. |
dzannotti
left a comment
There was a problem hiding this comment.
Verdict
We request changes. The speedup is real with backend sampling on, but a short request sequence can leave a slot in a state where every later matching request returns garbage tokens. Master does not have this bug.
Direction
Accept after work. With backend sampling on, the PR removes every per-request sched_reserve and cuts short-request latency by 11 to 30 percent on both backends. With backend sampling off (the default) it saves nothing, so we want the reuse path limited to backend sampling, plus the fix and tests below. Nothing here is specific to this fork, so the set_sampler and release() parts are also good candidates for upstream.
Asks
- Blocking,
tools/server/server-context.cpp:2229-2252: when reuse fails, the old sampler is unregistered and thencommon_sampler_initis called. If init throws (invalid grammar,repeat_penalty:0),slot.smplkeeps the old chain, which is still marked as backend-initialized. The next request with the same params and backend sampling off for that request (for examplen_probs > 0withpost_sampling_probs:false) passes the gate and reuses it on the CPU path.llama_sampler_chain_applythen skips the samplers it thinks the GPU already ran, so no token is selected andcommon_sampler_samplereadscur_p.data[-1]. We reproduced it on ROCm and Vulkan (repro in the details block). The output is token 0 or another random id repeated, and the slot stays broken for later requests with the same params. Fix: callslot.smpl.reset()right after the unregister at line 2242, and try reuse only whenuse_backend_sampling && slot.smpl_backend_sampling. The second change also keeps the default path identical to master. - Should fix,
common/sampling.cpp:548-561:common_params_sampling_equalrepeats the grammar and reasoning-budget presence rules ofcommon_sampler_build_constraints.common_sampler_reusebuildsgrmrandrbudgetanyway, so compute the effective backend flag from those pointers. If an upstream sync changes one copy and not the other, a registered chain can get a grammar, and theGGML_ASSERTincommon_sampler_sampleaborts the server. - Should fix: add tests.
tests/test-backend-sampler.cppalready builds samplers with a model. Cover reuse vs fresh init with a fixed seed (CPU and backend), the seedless case, a table that changes one gate field at a time and expectscommon_sampler_reuseto return false, and the failed-init sequence from item 1. The table also catches a new upstream field that the 50-field gate does not compare. - Should fix, PR text: master does one reserve per request at
-np 1(we counted 50 for 50 requests), and two only when another slot decodes between release and the next request. Please correct "two sched_reserve passes per request", and post the before/after numbers on a public model. - Should fix: the commit has no
Assisted-by:trailer (AGENTS.md), and several comments are 3 to 6 lines where the repo asks for 1 to 2 (common/sampling.cpp:540-544,641-651,common/sampling.h:49-54,tools/server/server-context.cpp:421-423).
Nits: "e.g," typo at common/sampling.cpp:544; drop the "(cherry picked from ...)" line, which points to a commit outside this repo.
Correctness
FAIL on ROCm and Vulkan, from item 1 only.
- Logit parity: f32 final logits and prompt tokens are byte-identical on all four correctness corpora. The long KLD files are byte-identical, and greedy decode matches master.
- State leaks: for 29 request-pair cases we ran request A then B on one slot and compared B with B on a fresh server, with backend sampling off and on. B was byte-identical in every case except item 1. The cases cover seed, temperature, top-k/p, penalties, DRY, mirostat 1/2, XTC, logit_bias, n_probs on/off, grammar, json_schema and reasoning budget. Seedless requests still differ.
- Also identical to master: ctest,
test-llama-context,-np 4with different configs per slot, slot save/restore, embeddings between completions, sleep/wake, clean shutdown, and MTP speculative decoding with a fixed seed. The server pytest suite did not run (not available here).
Performance
50 sequential short requests (8 tokens) per run, 3 runs, base and head alternated. Each cell is the median latency in ms. The spread between runs is under 1 ms.
| backend | model, slots | bs off, master | bs off, PR | bs on, master | bs on, PR | gain |
|---|---|---|---|---|---|---|
| ROCm | Qwen3-0.6B, np 1 | 40.59 | 40.61 | 52.90 | 41.15 | -22.2% |
| ROCm | Qwen3-8B, np 1 | 168.94 | 169.01 | 220.21 | 169.40 | -23.1% |
| ROCm | Qwen3-8B, np 2 | 223.43 | 223.20 | 300.42 | 223.53 | -25.6% |
| Vulkan | Qwen3-0.6B, np 1 | 32.98 | 32.99 | 38.19 | 34.00 | -11.0% |
| Vulkan | Qwen3-8B, np 1 | 163.86 | 163.63 | 180.67 | 163.13 | -9.7% |
| Vulkan | Qwen3-8B, np 2 | 206.98 | 206.72 | 245.29 | 205.64 | -16.2% |
With backend sampling on, the PR brings latency down to the level with it off. llama-bench and the speculative prefill benchmark show no change.
Verification
Strix Halo (gfx1151), ROCm and Vulkan (RADV). We tested the PR merged into master 093c552 (a clean merge, 27 commits behind) against master.
Not tested
The server pytest suite, a hybrid-memory model, speculative depth 64000, and ASan.
Thanks for splitting this out and for folding in the earlier review points.
Review made with AI on behalf of the strix-llama.cpp maintainers.
Full verification (for reproduction and agents)
Environment
- Linux 7.0.0, gcc 15.2.0, cmake 4.2.3, Ninja
- ROCm 10.1 (hipconfig 7.16.26385), AMD clang 24.0.0git d6f6cb69
- Mesa 26.0.8 RADV (Radeon 8060S, RADV STRIX_HALO)
- GPU: gfx1151 (Strix Halo)
Compared
- master tip: 093c552
- PR head: 684a689
- merged (local, not pushed): a189d05d39807c9d7e53f2fc0e337f1c469cb589
- merge base: 0c1021e, 27 commits behind master (none touch the four PR files). The merge was clean, and the merged diff equals the PR diff.
Build
cmake -G Ninja -S <tree> -B <tree>/build-hip -DCMAKE_BUILD_TYPE=Release -DGGML_HIP=ON -DGGML_VULKAN=OFF -DGPU_TARGETS=gfx1151 -DCMAKE_HIP_COMPILER=$(hipconfig -l)/clang -DCMAKE_PREFIX_PATH=$(hipconfig -R)
cmake -G Ninja -S <tree> -B <tree>/build-vk -DCMAKE_BUILD_TYPE=Release -DGGML_VULKAN=ON -DGGML_HIP=OFF
cmake --build <tree>/build-<be> -j 16
Models
| model | source | sha256 |
|---|---|---|
| Qwen3-0.6B Q8_0 | bartowski/Qwen_Qwen3-0.6B-GGUF, Qwen_Qwen3-0.6B-Q8_0.gguf | c159d1518f16bc42533d9a09f034eb598b670341adc2b08b9a9751614aea71eb |
| Qwen3-8B IQ3_XS | bartowski/Qwen_Qwen3-8B-GGUF, Qwen_Qwen3-8B-IQ3_XS.gguf | 15d9f1cfa8be0785df44376669a5b45c3ae75eda916fc553c44923cfb5c3eef2 |
| 27B model with an MTP head, UD-Q4_K_XL | speculative checks only | 3f227079003add2511437e5b1e94812e363385225bf6a9b47b0054a72bc8b01e |
Commands
- Parity:
llama-debug -m <model> -f tests/corpus/correctness-<t>.txt -c 16384 -b 16384 -ub 512 -ngl 99 -fa on -ctk f16 -ctv f16 --save-logits --logits-output-dir <dir>, thencmpof the logits and tokens files. ROCm also withHIP_LAUNCH_BLOCKING=1(prose). - Long KLD:
llama-perplexity -f <four corpora joined> -c 16384 -b 512 -ub 512 -ngl 99 -fa on -ctk f16 -ctv f16 --kl-divergence-base <side>.kld, then head scored against base. - Decode:
llama-completion -f correctness-prose.txt -n 256 --temp 0 --seed 1 -no-cnv --ignore-eos -ngl 99 -fa on -c 16384, base twice. - Unit:
ctest -L main -E 'test-opt|test-llama-archs',ctest -R 'spec-mtp-pcum|draft-vocab',test-llama-context -m <Qwen3-0.6B>. - Functional server:
llama-server -m <Qwen3-0.6B> -ngl 99 -fa on -c 4096 -np 1 -sps 0 --no-cache-prompt [-bs], every request withcache_prompt:false, return_tokens:true. Each case runs its request sequence on one server, then each checked request alone on a fresh server. Compared: content, tokens, completion_probabilities, stop type, tokens predicted (chat: message incl. reasoning_content). - Extra server checks:
-np 4 -c 16384(4 concurrent requests with different configs pinned to slots 0-3, then 4 sequential requests moved to the next slot);--slot-save-path(save, other request, restore, request);--embeddings(completion, /embedding, completion);--sleep-idle-seconds 3. - Speculative:
llama-server -m <27B> --spec-type draft-mtp -c 8192 -np 1 --no-cache-prompt [-bs], two identical requests at temperature 0.7 seed 42 n_predict 128, then one at temperature 0.scripts/bench-spec-prefill.sh <label> <prompt> -m <27B> --spec-type draft-mtp -ngl 99 -fa on -c <depth+2048>, depths 0, 12000, 32000, 2 rounds alternated. - Speed: one server per run,
-c <4096 x np> -np <np> --no-cache-prompt -sps 0 [-bs], 3 warm-up requests, then 50 sequential requests{"prompt":"Tell me a fact about cats.","n_predict":8,"temperature":0.8,"seed":42,"id_slot":0}. For-np 2a streamingignore_eosrequest keeps slot 1 busy during the run. Reserve counts come from a separate-lv 4run (thesched_reserve: reservinglines logged during the 50 requests). - llama-bench:
-m <Qwen3-0.6B> -p 512 -n 128 -d 0,12000 -r 5 -ngl 99 -fa on, 2 rounds per side.
Results
Item 1 repro (one slot, -np 1, Qwen3-0.6B, prompt "The history of the city of Rome begins"):
- A:
{"backend_sampling":true,"n_probs":5,"post_sampling_probs":true,"samplers":["top_k","temperature"],"temperature":0.8,"seed":1,"n_predict":32}returns normal text. - B: A plus
"grammar":"root ::= ("returns 400 "Failed to initialize samplers: failed to parse grammar" (master and PR). B with"repeat_penalty":0instead triggers the same state. - C: A with
"post_sampling_probs":false.
| C | ROCm | Vulkan |
|---|---|---|
| fresh server, master and PR | normal text, tokens 1657 21787 4134 ... | normal text, tokens 1657 21787 4134 ... |
| master after A, B | same as fresh | same as fresh |
| PR after A, B | 32x token 0 ("!!!!...") | 32x token 0, or 32x token 25107 ("(im(im...") |
The PR result is the same for servers with and without -bs, and on ROCm with HIP_LAUNCH_BLOCKING unset and =1. A further request with the C params and a different prompt is broken the same way. A request with a different backend mode rebuilds the sampler and recovers the slot. A variant with the default sampler list plus repeat_penalty:1.5 and a logit_bias ban gives 64x token 0 on the PR, and the same output as a fresh server on master.
Speed (median ms per request, runs r1/r2/r3; reserves during the 50 requests):
| backend | model | np | bs | master | PR | master reserves | PR reserves |
|---|---|---|---|---|---|---|---|
| Vulkan | 0.6B | 1 | off | 33.04/32.96/32.98 | 32.99/32.99/33.49 | 0 | 0 |
| Vulkan | 0.6B | 1 | on | 38.41/38.19/38.11 | 34.00/34.00/34.03 | 50 | 0 |
| Vulkan | 0.6B | 2 | off | 44.60/44.44/44.45 | 44.81/44.52/44.41 | 0 | 0 |
| Vulkan | 0.6B | 2 | on | 56.09/56.43/56.07 | 45.27/44.75/45.00 | 100 | 0 |
| Vulkan | 8B | 1 | off | 163.83/163.86/164.09 | 163.88/163.57/163.63 | 0 | 0 |
| Vulkan | 8B | 1 | on | 180.56/180.67/180.67 | 163.13/163.09/163.39 | 50 | 0 |
| Vulkan | 8B | 2 | off | 206.98/206.89/207.26 | 206.65/206.95/206.72 | 0 | 0 |
| Vulkan | 8B | 2 | on | 245.48/245.25/245.29 | 205.64/205.65/205.53 | 100 | 0 |
| ROCm | 0.6B | 1 | off | 40.59/40.56/40.62 | 40.61/40.60/40.62 | 0 | 0 |
| ROCm | 0.6B | 1 | on | 52.90/53.02/52.88 | 41.09/41.20/41.15 | 50 | 0 |
| ROCm | 0.6B | 2 | off | 56.41/56.75/56.78 | 56.67/56.71/56.79 | 0 | 0 |
| ROCm | 0.6B | 2 | on | 83.13/83.00/83.07 | 58.34/58.33/58.30 | 100 | 0 |
| ROCm | 8B | 1 | off | 168.94/168.93/169.70 | 169.01/168.84/169.05 | 0 | 0 |
| ROCm | 8B | 1 | on | 220.21/220.03/220.34 | 169.40/169.42/169.22 | 50 | 0 |
| ROCm | 8B | 2 | off | 223.16/223.46/223.43 | 222.78/223.20/223.70 | 0 | 0 |
| ROCm | 8B | 2 | on | 300.42/300.83/300.24 | 222.77/223.53/223.60 | 100 | 0 |
Time to first token (server prompt_ms, median) follows the same pattern, for example ROCm 8B np 1 with bs on: 63.5 ms on master, 32.7 ms with the PR.
Speculative prefill benchmark (TTFT ms / tg t/s / acceptance, round 1; round 2 is within 1.1 percent):
| backend | depth | master | PR |
|---|---|---|---|
| ROCm | 0 | 305 / 22.17 / 61% | 305 / 22.21 / 61% |
| ROCm | 12000 | 34173 / 23.59 / 73% | 34200 / 23.59 / 73% |
| ROCm | 32000 | 103570 / 15.10 / 40% | 104666 / 15.01 / 40% |
| Vulkan | 0 | 365 / 25.86 / 64% | 367 / 25.86 / 64% |
| Vulkan | 12000 | 28114 / 26.34 / 73% | 28364 / 26.34 / 73% |
| Vulkan | 32000 | 84501 / 18.78 / 50% | 84773 / 18.78 / 50% |
llama-bench Qwen3-0.6B (t/s, round 1, master / PR): ROCm pp512 14281 / 14422, tg128 229.59 / 229.94, tg128 at d12000 92.69 / 92.80. Vulkan pp512 16178 / 15990, tg128 280.63 / 279.15, tg128 at d12000 103.00 / 102.73. Every delta is inside the stddev.
Correctness
| check | method | result |
|---|---|---|
| Build, both backends | cmake above | pass, master and PR |
| ctest label main | `ctest -L main -E 'test-opt | test-llama-archs'` |
| Spec unit tests | `ctest -R 'spec-mtp-pcum | draft-vocab'` |
| test-llama-context | -m <Qwen3-0.6B> |
12 pass, 3 skip, master and PR |
| f32 logit parity, 4 corpora | llama-debug + cmp | byte-identical, both backends, ROCm also with HIP_LAUNCH_BLOCKING=1 |
| KLD/PPL long | llama-perplexity, -c 16384 | .kld files byte-identical; PPL ROCm 8.6242, Vulkan 8.6336, both sides |
| Multi-step greedy decode | llama-completion, A/A on master | identical |
| Request-pair state checks (29 cases x bs off/on) | B after A vs B fresh | identical, except item 1 |
| Failed init then reuse (item 1) | 3-request sequence | FAIL on the PR, both backends |
| -np 4, slot save/restore, embeddings, sleep/wake, shutdown | server runs | identical to master; every server exited with code 0 |
| Speculative, fixed seed | MTP draft, two identical requests | identical, master and PR, bs off and on |
| Speculative temp 0, master vs PR | same draft settings | identical tokens and draft counts |
| Server pytest suite | - | not run, not available here |
| ASan | - | not run |
Findings in full
- Item 1 above. Root cause:
slot.smpl_backend_samplingrecords whether the chain is registered, not whether it was backend-initialized. After the unregister and a failed init, the flag says false while the chain keepsis_init. The gate at line 2229 then matches a request with backend sampling off. - Item 2 above.
- With backend sampling off,
set_sampler(nullptr)already returns early on master, so no reserve happens. The PR still sends every such request throughcommon_sampler_reuseinstead ofcommon_sampler_init. Timing is unchanged (within 0.5 ms), so this path adds risk with no gain. - Idle slots stay registered after release. The sampling graph then keeps nodes for those slots. They are not computed, and we saw no cost in the numbers above.
- The chain perf counters (
t_sample_us,n_sample) are not reset on reuse. The server does not print them. - Upstream ggml-org#32009 (reuse the backend scheduler when its params are compatible) is not yet in this fork. It removes part of the cost this PR saves, so the gain may shrink after the next upstream sync. The
release()andload_taskhunks sit in code that upstream changes often, so expect merge conflicts during syncs.
Claims
- Two
sched_reservepasses per request with backend sampling on: not reproduced at-np 1(one per request). Reproduced at-np 2with another slot busy (two per request). - Reserves drop to the startup ones only: reproduced. The PR does 0 reserves during 50 requests.
- About 15 ms saved per request: reproduced, and larger on ROCm (12 to 77 ms per request, depending on model and slots).
- n_probs in the reuse gate: reproduced (greedy then n_probs 5 gives non-empty top_probs).
- Fixed-seed determinism with MTP drafting: reproduced.
Not tested
- The server pytest suite (not available in our environment).
- A hybrid-memory model: the PR does not touch memory code, and the server path is the same.
- Speculative depth 64000: the benchmark script cannot pass a prompt that large on the command line.
- ASan.
684a689 to
59e2f97
Compare
|
All five asks addressed in 59e2f97. 1 (blocking) — 2 — 3 — 4 — Claim corrected in the body: one reserve per request at 5 — Nits: Not tested here: Vulkan, server pytest suite, ASan. |
dzannotti
left a comment
There was a problem hiding this comment.
This review replaces our review of 684a689. It covers head 59e2f97.
Verdict
We request changes, but only small ones. The garbage-token bug is fixed, and the speedup holds on both backends. Correctness matches master in every check we ran. Before merge, the two new scripts must leave the PR, and the new test must cover what the PR text says it covers.
Direction
Accept after work. With backend sampling on, the PR removes the per-request sched_reserve and cuts short-request latency by 9 to 23 percent. With backend sampling off (the default), it changes nothing. The asks below make the change easier to keep correct through upstream syncs.
Asks
- Blocking: remove
scripts/04-measure-274-public.shandscripts/repro-item1-274.sh. They are one-off A/B scripts for this PR, with hard-coded paths to a local directory layout, and they write outside the repo. Put the commands in the PR description instead. If you want a regression test for the failed-init sequence, add it totools/server/tests/unit/test_completion.pynext totest_n_probs_post_backend_sampling: the A, B (expect 400), C sequence, then compare C with C on a fresh server. - Should fix,
tests/test-backend-sampler.cpp:2071-2187: the test does not check what the PR text says. Thebackend_sampling = truecase never registers the chain on a context and never decodes, so it runs the same CPU code as thefalsecase. The chain is reused right after init, so leftover state from an earlier request is never tested.penalty_repeatis set, butsamplershas noCOMMON_SAMPLER_TYPE_PENALTIES, so no penalty sampler runs. Fix: sample 16 steps, reuse, then compare 16 more steps with a fresh chain, with the penalties sampler in the list. Add one real backend case withtest_context(as intest_set_sampler): register, decode, reuse, register the same pointer again, decode, and compare with a fresh chain. That case also tests the new no-op inllama_context::set_sampler. - Should fix,
common/sampling.cpp:545-598: if an upstream sync adds a field thatcommon_sampler_initreads, the gate still compiles and passes every test, and reuse then serves the old chain. The 18-row table cannot catch that. Add a compile-time tripwire in the test, for examplestatic_assert(sizeof(common_params_sampling) == N, "update common_params_sampling_equal")behind an x86_64/libstdc++ guard. - Should fix,
common/sampling.cpp:606-619andtools/server/server-context.cpp:369,2227-2277: compare the fields first and build the grammar and reasoning-budget samplers only on a match. Today every request with a grammar orjson_schemabuilds its grammar twice, because the stored chain is always a backend chain and the gate always misses. Also consider droppingsmpl_backend_sampling:llama_set_sampler(ctx, id, nullptr)already does nothing when the seq is not registered, and the flag is set to true even whenllama_set_samplerreturns false. - Should fix, comments:
common/sampling.cpp:540-544is still 5 lines (the repo asks for 1 to 2).common/sampling.h:49-50says "on false ... may normalize params", but only the success path changesparams.
Nits: move the same-pointer check in src/llama-context.cpp:1693-1700 above the debug log line; the seed mix ^ 0x9e3779b9u is now in two places (common/sampling.cpp:494 and 638), so a shared helper keeps reuse and fresh init from drifting apart; the PR description still names a private machine.
Correctness
PASS on ROCm and Vulkan for every check that ran.
- Earlier item 1: the A, B, C sequence now gives the same tokens as a fresh server, with and without
-bs, with an invalid grammar and withrepeat_penalty:0. - State leaks: 243 request checks per backend (a matrix of 29 sampling configurations, each sent after another configuration on one slot and compared with a fresh server, with backend sampling off and on, plus grammar and
json_schemabetween reused requests, per-request backend on/off flips,n_probs, seedless requests, concurrent decode with config changes,n_cmpl, sleep/wake and slot save/restore). Master and the PR give the same results. A hybrid-memory model and MTP with a fixed seed also match. - f32 final logits and prompt tokens are byte-identical on the four corpora (ROCm also with
HIP_LAUNCH_BLOCKING=1). The long KLD files are byte-identical and greedy decode matches. - ctest,
test-llama-context, the spec unit tests andtest-backend-samplergive the same results as master. The newtest_sampler_reusepasses. The server pytest suite did not run (not available here).
Performance
50 sequential short requests (8 tokens), 3 runs, master and PR alternated. Each cell is the median latency in ms. The spread between runs is under 1 ms.
| backend | model, slots | bs off, master | bs off, PR | bs on, master | bs on, PR | gain |
|---|---|---|---|---|---|---|
| ROCm | Qwen3-0.6B, np 1 | 40.02 | 40.01 | 52.02 | 40.52 | -22.1% |
| ROCm | Qwen3-8B, np 1 | 168.03 | 168.12 | 217.93 | 167.93 | -22.9% |
| ROCm | Qwen3-8B, np 2 | 168.78 | 168.84 | 219.56 | 168.99 | -23.0% |
| Vulkan | Qwen3-0.6B, np 1 | 32.56 | 32.65 | 37.70 | 33.45 | -11.3% |
| Vulkan | Qwen3-8B, np 1 | 162.90 | 162.69 | 179.23 | 162.10 | -9.6% |
| Vulkan | Qwen3-8B, np 2 | 164.87 | 164.74 | 181.92 | 164.58 | -9.5% |
Reserves during the 50 requests with backend sampling on: 50 on master, 1 with the PR. Idle slots now keep their chain registered. With -np 4 and three idle registered slots, decode on the busy slot is at most 0.5 percent slower than master, also with MTP drafting. llama-bench (PP2048 at depths 0 to 64000) and MTP speculative decoding at depths 0 to 64000 show no change: acceptance counts are identical and every delta is inside the run spread.
Verification
Strix Halo (gfx1151), ROCm and Vulkan (RADV). We tested the PR merged into master 1dd4c92 (a clean merge, 45 commits behind) against master.
Not tested
The server pytest suite (not available here), ASan, the repo scripts/server-test-*.py, and interleaved multi-slot traffic for the reserve count.
Thanks for working through the earlier review so quickly.
Review made with AI on behalf of the strix-llama.cpp maintainers.
Full verification (for reproduction and agents)
Environment
- Linux 7.0.0, gcc 15.2.0, cmake 4.2.3, Ninja
- ROCm 10.1 (hipconfig 7.16.26385), AMD clang 24.0.0git d6f6cb69
- Mesa 26.0.8 RADV (Radeon 8060S, RADV STRIX_HALO), Vulkan loader 1.4.341
- GPU: gfx1151 (Strix Halo)
Compared
- master tip: 1dd4c92
- PR head: 59e2f97 (previous review: 684a689)
- merged (local, not pushed): 18be6d0936fb4448f4056b836594aa966de4f02e
- merge base: 0c1021e, 45 commits behind master. None of the 45 commits touch the PR files. The merge was clean, and the merged diff equals the PR diff.
Build
cmake -G Ninja -S <tree> -B <tree>/build-hip -DCMAKE_BUILD_TYPE=Release -DGGML_HIP=ON -DGGML_VULKAN=OFF -DGPU_TARGETS=gfx1151 -DCMAKE_HIP_COMPILER=$(hipconfig -l)/clang -DCMAKE_PREFIX_PATH=$(hipconfig -R)
cmake -G Ninja -S <tree> -B <tree>/build-vk -DCMAKE_BUILD_TYPE=Release -DGGML_VULKAN=ON -DGGML_HIP=OFF
cmake --build <tree>/build-<be> -j
Models
| model | source | sha256 |
|---|---|---|
| Qwen3-0.6B Q8_0 | bartowski/Qwen_Qwen3-0.6B-GGUF, Qwen_Qwen3-0.6B-Q8_0.gguf | c159d1518f16bc42533d9a09f034eb598b670341adc2b08b9a9751614aea71eb |
| Qwen3-8B IQ3_XS | bartowski/Qwen_Qwen3-8B-GGUF, Qwen_Qwen3-8B-IQ3_XS.gguf | 15d9f1cfa8be0785df44376669a5b45c3ae75eda916fc553c44923cfb5c3eef2 |
| granite-4.0-h-1b Q4_K_M (hybrid memory) | ibm-granite/granite-4.0-h-1b-GGUF | da3d737121a96f3c9a316685212376257a7f167b74380855666dd488d6af3bcb |
| 27B model with an MTP head, UD-Q4_K_XL | speculative checks only | 3f227079003add2511437e5b1e94812e363385225bf6a9b47b0054a72bc8b01e |
Commands
- Functional server:
llama-server -m <model> -ngl 99 -fa on -c 4096 -np 1 [-bs] --host 127.0.0.1, every request withcache_prompt:false, return_tokens:true. Each case runs its request sequence on one server, then each checked request alone on a fresh server with the same flags. Compared byte for byte: tokens, content, completion_probabilities (chat: the message, including reasoning_content). - Base request R:
{"prompt":"The history of the city of Rome begins","n_predict":32,"temperature":0.8,"seed":1,"samplers":["top_k","top_p","temperature"],"top_k":40,"top_p":0.95,"id_slot":0}. - Earlier item 1: R plus
"backend_sampling":true,"n_probs":5,"post_sampling_probs":true, then the same with"grammar":"root ::= ("(400), then the first request with"post_sampling_probs":false. Also with"repeat_penalty":0as the failing request, and on a server with-bsand no per-request field. - Other sequences: R, invalid grammar, R,
repeat_penalty:0, R (-bs); R,backend_sampling:false, R,n_probs:5, R with-lv 4and a count ofsched_reserve: reserving; R, grammar, R,json_schema, R; R, chat withtools, R; R, chat withreasoning_budget_tokens:16, R; R, R with seed 2, R; R with n_probs 5 twice; seedless R five times;-np 2with slot 0 streaming 512 tokens while slot 1 loops R, backend off, invalid grammar and seed 7;--sleep-idle-seconds 3;--slot-save-pathsave and restore;n_cmpl:2twice; 27B with-bs --spec-type draft-mtp --spec-draft-n-max 16. - State matrix: 28 completion configurations and 1 chat configuration (seed, temp 1.5, temp 0, top_k, top_p, min_p, combined, repeat/frequency/presence penalties with
penaltiesin samplers, DRY with and without breakers, mirostat 1 and 2, XTC, logit_bias, n_probs with and without post-sampling probs, grammar, json_schema, typical_p, top_n_sigma, dynatemp, min_keep, ignore_eos, adaptive_p, a backend on/off flip, chat with a reasoning budget). Each configuration runs after R and, in a second chain in reverse order, after a different configuration. Backend sampling off and on. - Unit:
LLAMACPP_TEST_MODELFILE=<Qwen3-0.6B> ctest -R test-backend-sampler -V,ctest -L main -E 'test-opt|test-llama-archs',ctest -R 'spec-mtp-pcum|draft-vocab',test-llama-context -m <Qwen3-0.6B>,test-mtp-draft-vocab -m <27B>. - Parity (Qwen3-8B):
llama-debug -m <model> -f tests/corpus/correctness-<t>.txt -c 16384 -b 16384 -ub 512 -ngl 99 -fa on -ctk f16 -ctv f16 --save-logits --logits-output-dir <dir>, thencmpof every output file. ROCm also withHIP_LAUNCH_BLOCKING=1. - Long KLD:
llama-perplexity -f <four corpora joined> -c 16384 -b 512 -ub 512 -ngl 99 -fa on -ctk f16 -ctv f16 --kl-divergence-base <side>.kld, then head scored against base. - Decode:
llama-completion -m <model> -f <corpus> -n 256 --temp 0 --seed 1 -no-cnv --ignore-eos -ngl 99 -fa on -c 16384, base twice first. - Short-request speed: one server per run,
-c <4096 x np> -np <np> -sps 0 --no-cache-prompt [-bs], 3 warm-up requests, then 50 sequential requests{"prompt":"Tell me a fact about cats.","n_predict":8,"temperature":0.8,"seed":42,"id_slot":0}. Latency is the serverprompt_ms + predicted_ms. Reserve counts come from a separate-v -lv 4run. - Idle slots:
-c 8192 -np 4 -bs [--spec-type draft-mtp --spec-draft-n-max 16]. Slots 0-3 get one backend-sampled request each, then 12 requests (10 for the 27B) of R withn_predict:256, ignore_eos:true, id_slot:0, readingpredicted_per_second. Control: slots 1-3 re-warmed withbackend_sampling:false. 2 rounds per side, alternated. - llama-bench (Qwen3-8B):
-p 2048 -d 0,12000,32000,64000 -b 2048 -ub 512 -n 0 -r 5 -ngl 99 -fa on -ctk f16 -ctv f16 --load-mode none -o jsonl, 2 rounds, order swapped in round 2. - Speculative: the measurement of
scripts/bench-spec-prefill.sh(1 warm-up, 3 timed requests, temperature 0, n_predict 128), with the request body sent throughcurl --data-binary @filebecause the script's-dargument is too long above about 32k tokens. Server:-m <27B> --spec-type draft-mtp --spec-draft-n-max 16 -bs -ngl 99 -fa on -c <40000|70000> -np 1.
Results
Short requests (median ms of runs r1/r2/r3; reserves include 1 at startup):
| backend | model | np | bs | master | PR |
|---|---|---|---|---|---|
| ROCm | 0.6B | 1 | off | 40.02/40.00/40.03 | 40.03/40.01/39.98 |
| ROCm | 0.6B | 1 | on | 52.08/52.02/51.82 | 40.50/40.52/40.52 |
| ROCm | 0.6B | 2 | off | 40.40/40.47/40.39 | 40.42/40.40/40.42 |
| ROCm | 0.6B | 2 | on | 53.24/53.09/53.20 | 40.99/41.06/41.03 |
| ROCm | 8B | 1 | off | 167.37/168.03/168.35 | 167.81/168.12/168.23 |
| ROCm | 8B | 1 | on | 217.28/218.07/217.93 | 168.06/167.89/167.93 |
| ROCm | 8B | 2 | off | 168.95/168.68/168.78 | 168.84/168.84/168.99 |
| ROCm | 8B | 2 | on | 219.56/219.89/219.41 | 168.79/168.99/169.47 |
| Vulkan | 0.6B | 1 | off | 32.56/32.54/33.03 | 32.66/32.56/32.65 |
| Vulkan | 0.6B | 1 | on | 37.70/38.07/37.59 | 33.47/33.36/33.45 |
| Vulkan | 0.6B | 2 | off | 33.81/34.00/33.49 | 34.22/34.02/33.50 |
| Vulkan | 0.6B | 2 | on | 39.16/39.12/39.22 | 34.90/34.55/34.57 |
| Vulkan | 8B | 1 | off | 163.10/162.79/162.90 | 162.99/162.69/162.55 |
| Vulkan | 8B | 1 | on | 179.07/179.23/179.28 | 161.98/162.10/162.25 |
| Vulkan | 8B | 2 | off | 164.78/164.92/164.87 | 164.87/164.74/164.67 |
| Vulkan | 8B | 2 | on | 182.15/181.92/181.78 | 164.64/164.28/164.58 |
A second request shape (samplers:["top_k","top_p","min_p","temperature"]) gives the same picture: ROCm -22.1% (0.6B) and -15.9% to -16.5% (8B), Vulkan -9.0% to -11.1%, bs off within 0.6%. Reserves during 50 requests: master 1 (bs off) and 51 (bs on), PR 1 and 2, for every model, slot count and backend.
Idle slots, decode t/s on slot 0 with three idle backend-sampled slots (median, sd):
| backend | model | master | PR | delta | control master / PR |
|---|---|---|---|---|---|
| ROCm | 0.6B | 213.34 (0.64) | 212.43 (0.41) | -0.42% | 213.53 / 215.66 |
| ROCm | 8B | 52.37 (0.14) | 52.28 (0.08) | -0.16% | 52.37 / 52.89 |
| ROCm | 27B + MTP | 11.13 (0.01) | 11.09 (0.01) | -0.35% | 11.13 / 11.13 |
| Vulkan | 0.6B | 257.75 (0.27) | 257.10 (0.38) | -0.25% | 257.70 / 257.88 |
| Vulkan | 8B | 55.35 (0.02) | 55.35 (0.02) | +0.00% | 55.34 / 55.40 |
| Vulkan | 27B + MTP | 7.80 (0.01) | 7.88 (0.01) | +1.12% | 7.79 / 7.80 |
Tokens are identical across requests and between master and the PR in every cell. MTP acceptance is the same on both sides (ROCm 1640/14150, Vulkan 1580/15330; draft length 16 was picked as the worst case for idle chains, not as a good setting).
llama-bench Qwen3-8B, PP2048, t/s (stddev), round 1 / round 2:
| backend | depth | master | PR |
|---|---|---|---|
| ROCm | 0 | 1310.43 (66.69) / 1212.82 (4.07) | 1213.42 (5.97) / 1286.99 (60.14) |
| ROCm | 12000 | 723.35 (7.81) / 715.15 (7.97) | 716.56 (8.21) / 716.68 (7.88) |
| ROCm | 32000 | 408.53 (2.73) / 406.60 (2.90) | 407.48 (2.96) / 406.95 (2.68) |
| ROCm | 64000 | 244.97 (1.16) / 244.63 (1.17) | 244.85 (1.15) / 244.79 (1.23) |
| Vulkan | 0 | 1383.54 (77.88) / 1282.94 (3.22) | 1283.74 (3.49) / 1351.56 (73.84) |
| Vulkan | 12000 | 852.29 (5.09) / 845.94 (4.34) | 846.44 (4.93) / 846.95 (4.57) |
| Vulkan | 32000 | 533.19 (4.98) / 532.20 (5.52) | 532.44 (4.99) / 530.50 (4.49) |
| Vulkan | 64000 | 340.17 (2.03) / 340.02 (2.87) | 340.45 (2.17) / 340.30 (2.71) |
At depth 0 the side that runs first in a round has the high-variance run in both orders. compare-llama-bench.py stopped with an AssertionError on this file, so the raw numbers are given.
Speculative decoding, 27B with MTP (TTFT ms / tg t/s / accepted of drafted; the output text is identical between master and the PR at every depth):
| backend | prompt tokens | master | PR |
|---|---|---|---|
| ROCm | 14 | 368 / 9.23 / 216 of 2457 | 332 / 9.28 / 216 of 2457 |
| ROCm | 9610 | 26636 / 12.83 / 273 of 1557 | 26658 / 12.82 / 273 of 1557 |
| ROCm | 30828 | 94845 / 10.00 / 258 of 1818 | 94863 / 9.97 / 258 of 1818 |
| ROCm | 63159 | 225782 / 10.32 / 279 of 1533 | 225706 / 10.32 / 279 of 1533 |
| Vulkan | 14 | 415 / 7.55 / 225 of 2271 | 389 / 7.53 / 225 of 2271 |
| Vulkan | 9610 | 22358 / 10.47 / 276 of 1509 | 22351 / 10.48 / 276 of 1509 |
| Vulkan | 30828 | 78674 / 8.17 / 261 of 1737 | 78621 / 8.16 / 261 of 1737 |
| Vulkan | 63159 | 182107 / 7.12 / 267 of 1668 | 182433 / 7.12 / 267 of 1668 |
Correctness
| check | method | result |
|---|---|---|
| Build, both backends | cmake above | pass, master and PR |
| Earlier item 1 | 3-request sequence, 3 variants | same as a fresh server on master and PR, both backends |
| Request-sequence checks | 243 per backend (Qwen3-0.6B) | master and PR identical; the only failures are 4 requests with repeat_last_n:-1, which both sides reject with 400 (invalid input in our matrix) |
| Hybrid-memory model | 217 checks per backend (granite-4.0-h-1b) | master and PR identical, same 4 rejected requests |
| MTP fixed seed | 6 checks per backend (27B) | identical |
| Reserve count with backend on/off flips | -lv 4, 5 requests |
6 on master, 6 on the PR |
| ctest label main | `ctest -L main -E 'test-opt | test-llama-archs'` |
| test-backend-sampler | ctest with a model file | pass; test_sampler_reuse runs on the PR |
| Spec unit tests | `ctest -R 'spec-mtp-pcum | draft-vocab', test-mtp-draft-vocab` |
| test-llama-context | -m <Qwen3-0.6B> |
12 pass, master and PR |
| f32 logit parity, 4 corpora | llama-debug + cmp | byte-identical, both backends, ROCm also with HIP_LAUNCH_BLOCKING=1 |
| KLD/PPL long | llama-perplexity, -c 16384 | .kld files byte-identical; PPL ROCm 4.9433, Vulkan 4.9378, both sides |
| Multi-step greedy decode | llama-completion, A/A on master | identical |
| Spec decoding, temp 0 | same draft settings, depths 0 to 64000 | identical output and acceptance |
| Server pytest suite | - | not run, not available here |
| ASan | - | not run |
Findings in full
- The two scripts (ask 1).
- The test does not cover backend registration, state after use, or penalties (ask 2). It runs only with a model file; without one, the binary exits with success, so default CI skips it.
- The gate compares every field that
common_sampler_initandcommon_sampler_build_constraintsread today. Not compared, and not read by init:ignore_eos,timing_per_token,user_sampling_config,preserved_tokens,logit_bias_eog,reasoning_budget_intro_message. The test table covers 18 of the 50 compared fields (ask 3). - Idle slots keep their chain registered after release. The chains of idle slots are built into the graph but not computed. The measured cost is within 0.5 percent on decode, also with a draft length of 16.
- After a failed init,
slot.smplis null until the next request. Every reader ofslot.smplneeds an active task, so this is safe. - Pre-existing on master, not from this PR: on ROCm with backend sampling and the explicit chain
["top_k","temperature"], a fixed seed does not give the same tokens run to run. Addingtop_pormin_p, or the default chain, makes it repeatable. Vulkan is repeatable. Our base request usestop_pfor this reason.
Claims
- Reserves drop from 50 to 0 during 50 requests with backend sampling on: reproduced (50 to 1; the 1 is the first request), ROCm and Vulkan.
- Latency -12.6 percent on Qwen3-0.6B, back to the level with backend sampling off: reproduced; ROCm -22 percent, Vulkan -11 percent.
- Backend sampling off unchanged: reproduced, within 0.6 percent.
- Item 1 sequence equals a fresh server: reproduced, both backends.
test_sampler_reusepasses: reproduced; see ask 2 for what it covers.- Two reserves per request only with another slot decoding in between: not tested (needs interleaved traffic).
Not tested
The server pytest suite (not available here), ASan, the repo scripts/server-test-*.py, and interleaved multi-slot traffic for the reserve count. We did not run the two scripts in the PR.
59e2f97 to
b953642
Compare
|
All five asks addressed, head
Nits: same-pointer check moved above the debug log; seed mix is a shared helper
An AI agent (Hermes) made these changes. |
b953642 to
6f234ba
Compare
|
Both should-fixes and all nits addressed, head
Nits: Re-verified on ROCm after these edits: Thanks for the mutation testing — E3a passing the old case was a real gap. An AI agent (Hermes) made these changes. |
6f234ba to
eff4a84
Compare
dzannotti
left a comment
There was a problem hiding this comment.
This review replaces our review of b953642. It covers head 6f234ba, rebased by us onto current master.
Verdict
Both should-fix items from our last review are done, and the test changes look right. But the change breaks n_probs on backend-sampling servers with more than one slot. GitHub CI found it first (test_n_probs and test_logprobs in the backend-sampling server run), and we reproduced it. We ask for one small fix before merge.
Direction
Accept after work. We still want the change: with backend sampling on, it removes the per-request sched_reserve. The fix below is small and keeps that gain.
Asks
- Blocking,
tools/server/server-context.cpp(the removedllama_set_sampler(ctx_tgt, id, nullptr)inserver_slot::reset()): idle slots now keep their backend chain registered, and so do the chains that context creation registers for every seq when backend sampling is on. A request that needs pre-sampling probabilities (n_probs > 0withoutpost_sampling_probs) runs on the CPU path, but another seq still has a registered chain. Thenget_sampled_logits_count()returns 0 for its row,get_token_probabilities()(tools/server/server-common.cpp:1846-1851) returns an empty list, andcompletion_probabilities[].top_logprobsis empty. On master this happens only while two slots are active at the same time in mixed modes. With the PR it happens on the first request to any-bsserver with-np 2or more (the default-npis auto, 4). Suggested fix: inget_token_probabilities(), use the raw logits withn_vocaband identity ids whenllama_get_sampled_logits_ith(ctx, idx)is null (the row was not backend-sampled), ascommon_sampler'sset_logitsalready does. It is about 4 lines in one server file, keeps the reuse, and also fixes the mixed-mode case on master. The CI backend-sampling run (LLAMA_ARG_BACKEND_SAMPLING=1 ./tests.sh) covers it.
Optional, not needed for merge: the comment at server-context.cpp:2300 ("a non-backend chain never matches here") is not true for a chain that was downgraded to the CPU because of a grammar. The behavior is correct; only the comment is wrong.
Correctness
FAIL on ROCm, Vulkan and CPU, for ask 1 only.
n_probsand chatlogprobs, backend sampling on, 2 slots: master returns 10 top probs per token, the PR returns 0. Same on CPU, ROCm and Vulkan, with the CI test model and with Qwen3-0.6B. The sampled tokens match master; only the probabilities are missing. With 1 slot, or without backend sampling, the PR matches master.- Master has the same bug in a narrower case: while one slot generates with backend sampling, an
n_probsrequest on another slot also gets 0 on master. The fix in ask 1 should cover both (we did not test it). - Everything else matches master: f32 final logits and prompt tokens on the four corpora (byte-identical, ROCm also with
HIP_LAUNCH_BLOCKING=1, and Vulkan), greedy decode with and without backend sampling,ctest -L main,test-backend-sampler(the new reuse test passes),test-llama-context, the failed-init sequence, two slots with traffic, and the LRU purge of an idle slot from #176.
Performance
50 sequential short requests (8 tokens), Qwen3-0.6B, 1 slot, 3 runs alternated, median latency in ms. Reserves with backend sampling on: master 51, PR 2.
| backend | bs off, master | bs off, PR | bs on, master | bs on, PR | gain |
|---|---|---|---|---|---|
| ROCm | 40.86 | 40.90 | 44.46 | 41.17 | -7.4% |
| Vulkan | 35.37 | 35.46 | 40.76 | 35.41 | -13.1% |
Master with backend sampling on is faster on ROCm than at our last review (44.46 ms now, 51.56 ms then), so the ROCm gain is smaller.
Verification
Strix Halo (gfx1151), ROCm and Vulkan (RADV), plus a CPU-only build to match CI. The PR was 1201 commits behind master and conflicted. We rebased it onto master f2230a8 and tested that against master. The two conflicts were mechanical: master added a check that EOG tokens cannot be banned with a grammar inside the code that the PR moved, and master excluded TTS tasks from backend sampling where the PR recomputes the flag. We kept both, see the details block. We pushed the rebase to your branch. Before you push again, run git fetch && git reset --hard origin/pr/sampler-reuse.
Not tested
The full server pytest suite, scripts/server-test-*.py, ASan, the llama-bench depth sweep and KLD (no compute code changed), and TTS (no TTS model).
Thanks for addressing every point from the last round so quickly.
Review made with AI on behalf of the strix-llama.cpp maintainers.
Full verification (for reproduction and agents)
Environment
- Linux 7.0.0, gcc 15.2.0, cmake 4.2.3, Ninja
- ROCm 10.1 (hipconfig 7.16.26385), AMD clang 24.0.0git d6f6cb69
- Mesa 26.0.8 RADV (Radeon 8060S, RADV STRIX_HALO)
Compared
- master tip: f2230a8
- PR head: 6f234ba (previous review: b953642; the delta is tests and text only)
- rebased (pushed to the PR branch): eff4a84
- merge base: 0c1021e, 1201 commits behind master
Rebase
Conflicts in two files. Nothing else changed: the range-diff differs from the PR commit only in these two hunks.
common/sampling.cpp: master added, incommon_sampler_init, a check that throws "EOG tokens cannot be banned when a grammar is set". The PR moved that part ofcommon_sampler_initintocommon_sampler_build_constraints. We put the check into the PR'scommon_sampler_init, right aftervocabis read, next to the other parameter checks.common_sampler_reusematches only whengrammarandlogit_biasequal the params that already passed this check, so the reuse path does not need it.tools/server/server-context.cpp(load_task, initialize samplers): master addeduse_backend_sampling &= task.type != SERVER_TASK_TYPE_TTS;. We kept it before the reuse attempt, and added&& task.type != SERVER_TASK_TYPE_TTSto the PR's recompute ofuse_backend_samplingafter init, so a TTS task never reuses and never registers a backend chain, as on master.
The upstream sync did not change common_params_sampling (the size tripwire still holds) or llama_context::set_sampler.
Build
cmake -G Ninja -S <tree> -B <tree>/build-hip -DCMAKE_BUILD_TYPE=Release -DGGML_HIP=ON -DGGML_VULKAN=OFF -DGPU_TARGETS=gfx1151 -DCMAKE_HIP_COMPILER=$(hipconfig -l)/clang -DCMAKE_PREFIX_PATH=$(hipconfig -R)
cmake -G Ninja -S <tree> -B <tree>/build-vk -DCMAKE_BUILD_TYPE=Release -DGGML_VULKAN=ON -DGGML_HIP=OFF
cmake -G Ninja -S <tree> -B <tree>/build-cpu -DCMAKE_BUILD_TYPE=Release -DGGML_VULKAN=OFF -DGGML_HIP=OFF
All six builds (master and rebased, three backends) pass.
Models
| model | source | sha256 |
|---|---|---|
| stories260K (CI test model) | ggml-org/test-model-stories260K, stories260K-f32.gguf | 270cba1bd5109f42d03350f60406024560464db173c0e387d91f0426d3bd256d |
| Qwen3-0.6B Q8_0 | bartowski/Qwen_Qwen3-0.6B-GGUF, Qwen_Qwen3-0.6B-Q8_0.gguf | c159d1518f16bc42533d9a09f034eb598b670341adc2b08b9a9751614aea71eb |
| Qwen2.5-1.5B-Instruct Q4_K_M | Qwen/Qwen2.5-1.5B-Instruct-GGUF, qwen2.5-1.5b-instruct-q4_k_m.gguf | 6a1a2eb6d15622bf3c96857206351ba97e1af16c30d7a74ee38970e434e9407e |
Commands
- CI reproduction:
llama-server -m <stories260K> --temp 0.8 --seed 42 --batch-size 32 --no-slots --ctx-size 512 --parallel 2 --n-predict 64 --no-jinja -bs [-ngl 99], and the same withLLAMA_ARG_BACKEND_SAMPLING=1instead of-bs, and without backend sampling. Requests:/completion{"prompt":"I believe the meaning of life is","n_probs":10,"temperature":0.0,"n_predict":5};/v1/chat/completionsas intest_logprobs(max_tokens5,logprobstrue,top_logprobs10,temperature0). Counttop_logprobsper token. - Qwen3-0.6B,
-c 4096 -ngl 99,n_probs10, 16 tokens, temperature 0: (a)-np 2 -bs, fresh server,n_probson slot 1; (b) a plain request on slot 0, thenn_probson slot 1; (c)-np 1 -bs, plain thenn_probs; (d) all of these without-bs; (e)n_probstwice on slot 0 with-np 2 -bs. Sequence on-np 2 -bs:n_probson s1, s0, plain on s1,n_probson s0, s1, s0. Concurrent: slot 0 generates 400 tokens with backend sampling while slot 1 runsn_probs10. - Unit:
ctest --test-dir <build> --output-on-failure -L main -E 'test-opt|test-llama-archs';test-backend-samplerwithLLAMACPP_TEST_MODELFILE=<Qwen3-0.6B>;test-llama-context -m <Qwen3-0.6B>. - Reserves: 50 sequential
{"prompt":"Tell me a fact about cats.","n_predict":8,"temperature":0.8,"seed":42,"id_slot":0,"cache_prompt":false}with-np 1 -v -lv 4, countingsched_reserve: reserving. - Latency: the same request,
-c 4096 -np 1 -sps 0 --no-cache-prompt [-bs], 3 warm-up then 50 requests, 3 runs per cell, master and PR alternated, median of serverprompt_ms + predicted_ms. - Failed-init: the sequence of
test_sampler_reuse_after_failed_initby hand on-np 2 -bs, forgrammar: "root ::= ("andrepeat_penalty: 0, compared with a fresh server. - Two slots: slot 1 streams 128 tokens (seed 7) while slot 0 repeats a seeded request 15 times; master twice, PR once.
- LRU purge (#176):
-np 2 -bs --slots --cache-ram 100 --kv-unified, requests that purge the idle slot to the prompt cache, then reuse it. - Parity: each corpus cut at a line boundary to at most 7900 tokens;
llama-debug -m <Qwen2.5-1.5B> -f <corpus> -c 8192 -b 8192 -ub 512 -ngl 99 -fa on -ctk f16 -ctv f16 --save-logits --logits-output-dir <dir>,cmpof every file. Greedy:llama-completion -n 256 --temp 0 --seed 1 -no-cnv --ignore-eos -ngl 99 -fa on -c 8192 [-bs], master twice, then the PR.
Results
Top probs per token, the same on CPU, ROCm and Vulkan:
| case | master | PR |
|---|---|---|
stories260K /completion, -bs or env |
10 | 0 |
stories260K chat logprobs, -bs or env |
10 | 0 |
| stories260K, no backend sampling | 10 | 10 |
| Qwen3 (a) fresh, slot 1 | 10 | 0 |
| Qwen3 (b) plain on s0, then s1 | 10 | 0 |
Qwen3 (c) -np 1 |
10 | 10 |
| Qwen3 (e) slot 0 twice | 10, 10 | 0, 0 |
Qwen3 (d) without -bs |
10 | 10 |
| sequence s1, s0, (plain s1), s0, s1, s0 | 10, 10, -, 10, 10, 10 | 0, 10, -, 0, 10, 10 |
| concurrent, other slot generating with bs | 0 | 0 |
The request fails exactly while the other slot still has a registered chain. Sampled tokens equal master in every case.
Reserves during 50 requests (including 1 at startup):
| bs off | bs on | |
|---|---|---|
| ROCm master / PR | 1 / 1 | 51 / 2 |
| Vulkan master / PR | 1 / 1 | 51 / 2 |
Correctness
| check | method | result |
|---|---|---|
| Build, 3 backends | cmake above | pass, master and PR |
n_probs / logprobs with backend sampling |
CI reproduction above | FAIL: 0 top probs on the PR, 10 on master |
| f32 logit parity, 4 corpora | llama-debug + cmp | 48/48 files byte-identical, ROCm (HLB unset and =1), Vulkan |
| Greedy decode, bs off/on | llama-completion, A/A on master | identical |
| ctest label main | as above | 35/35 on both sides, ROCm and Vulkan |
| test-backend-sampler | with Qwen3-0.6B | pass; the new reuse test passes |
| test-llama-context | Qwen3-0.6B | 13 pass, 5 skip, 0 fail, both sides |
| Failed-init sequence | by hand | 400 for the bad request, next request equals a fresh server, no abort |
| Two slots with traffic | seeded | identical to master |
| LRU purge with bs | --cache-ram 100 --kv-unified |
identical to master, no abort; n_probs in the same run 5 on master, 0 on the PR (ask 1) |
| KLD/PPL, llama-bench depth sweep | - | not run: no compute code changed, logits byte-identical |
Server pytest suite, server-test-*.py, ASan |
- | not run |
| TTS | - | not run, no TTS model |
Findings in full
- Ask 1. With backend sampling on, context creation registers one chain per seq. On master,
server_slot::reset()unregisters the slot's chain at slot init and on every release, so an idle slot has nothing registered. The PR removes that call, so idle slots keep their chains. When any seq has a registered chain, the context allocates the sampled-logits buffers, andget_sampled_logits_count()returns 0 for a row whose seq has no chain.get_token_probabilities()then returns an empty list.common_sampler'sset_logitsis not affected, because it falls back to the raw logits whenllama_get_sampled_logits_ith()is null; the suggested fix uses the same rule.n_probson speculative rows (populate_token_probsfrom the draft verify path) goes through the same function. - Optional: the comment at
server-context.cpp:2300, see Asks.
Claims
- Removes the per-request
sched_reservewith backend sampling on: reproduced (51 -> 2 reserves in 50 requests, ROCm and Vulkan). - Lower short-request latency with backend sampling on: reproduced (ROCm -7.4 percent, Vulkan -13.1 percent).
- No change with backend sampling off: reproduced for tokens, latency and reserves; not for
n_probswith backend sampling on (ask 1).
Not tested
The full server pytest suite (not available here; GitHub CI runs it), scripts/server-test-*.py, ASan, KLD/PPL, the llama-bench depth sweep, speculative decoding with a large model, and TTS.
eff4a84 to
f27d8c0
Compare
…builds - llama_context::set_sampler: re-registering the same sampler pointer is now a no-op (graph inputs hold the pointer; backend_init asserts !is_init) - common_sampler_reuse(): in-place re-init when the effective sampling config is unchanged (params equality gate + grammar/rbudget rebuild + reset), keeping the chain pointer stable for graph reuse; the plain fields are compared before any constraint is built - server load_task: try reuse only when the server runs with backend sampling and the slot has a chain; the effective-flag comparison inside reuse keeps non-backend chains out of the backend path; unregister and reset the old chain before rebuilding so a failed init cannot leave a backend-init'd chain on the CPU path; slot release no longer unregisters (master reserves once per request at -np 1) - server get_token_probabilities: a row not sampled by the backend reports the raw logits over the full vocab; fixes empty top_logprobs for n_probs requests on backend-sampling servers with -np 2 and more, and the same mixed-mode bug on master - tests: test_sampler_reuse covers parity with a fresh chain after 16 steps of use, a backend test_context case, the gate fields, and the failed-init sequence, with a sizeof tripwire for the gate; the A/B/C failed-init sequence is a server test in test_completion.py A/B (Qwen3-0.6B Q8_0, ROCm, 50 requests/run): with backend sampling on, reserves drop from 50 to 0 during the run and median latency from 55.3 to 48.3 ms (server-side timings). With backend sampling off, unchanged vs master. Assisted-by: Hermes Agent
f27d8c0 to
87fe47c
Compare
|
Ask 1 (blocking) is fixed in Fix - Verification (Vulkan build, Windows desktop,
Nit: the comment at Not tested here: ROCm/HIP (no AMD box in this session) and the pytest server suite (this Windows build has no libcurl, so the server cannot download the test models). The CI backend-sampling run covers both; I will follow up if it reports anything. |
dzannotti
left a comment
There was a problem hiding this comment.
This review replaces our review of 6f234ba (rebased as eff4a84). It covers head 87fe47c.
Verdict
Approved. The fix for our blocking ask is correct: n_probs and chat logprobs now return 10 top probs per token with backend sampling and 2 or more slots, on CPU, ROCm and Vulkan. The fix also covers the case that was broken on master. Nothing else changed, and the speed gain holds.
Direction
Accept. The change removes the per-request sched_reserve when backend sampling is on, and with this fix it no longer breaks token probabilities.
Asks
None.
Correctness
PASS on CPU, ROCm and Vulkan.
- The request sequences that failed in our last review now give 10 top probs per token in every case, and the probabilities are byte-identical to master and to a run without backend sampling. Your previous head (f27d8c0) still gives 0 in the same sequences, so the test is sensitive.
- The master-only case (one slot generates with backend sampling while another slot runs
n_probs) gives 0 top probs on master and 10 on this PR, on all three backends. - Server pytest with
LLAMA_ARG_BACKEND_SAMPLING=1(test_n_probs*,test_logprobs*): all pass on CPU and Vulkan. On ROCm,test_n_probs_post_backend_samplingfails on master and on the PR in the same way (different sampled token). The cause is that the ROCm backend has no TOP_K for backend sampling, not this PR. The fulltest_completion.pyandtest_chat_completion.pyon CPU give the same result on master and on the PR, with and without backend sampling. test-backend-samplerpasses on CPU, ROCm and Vulkan, including the new reuse test.ctest -L main: 35/35 on ROCm and Vulkan.- GitHub CI Server workflow, including "Tests (Backend sampling)": pass on this head.
Performance
50 sequential short requests, Qwen3-0.6B, 1 slot, median latency in ms, 2 rounds alternated. Reserves with backend sampling on: master 51, PR 2.
| backend | bs off, master | bs off, PR | bs on, master | bs on, PR | gain |
|---|---|---|---|---|---|
| ROCm | 41.27 / 41.04 | 41.11 / 41.10 | 44.74 / 44.51 | 41.43 / 41.43 | -7.2% |
| Vulkan | 35.20 / 35.13 | 35.21 / 35.12 | 39.74 / 39.91 | 35.06 / 35.06 | -12.0% |
Verification
Strix Halo (gfx1151), ROCm, Vulkan (RADV) and a CPU-only build. The PR is on current master 2c3dc19 (0 commits behind), so we tested the head as it is against master. No rebase was needed.
Not tested
scripts/server-test-*.py, ASan, and TTS (no TTS model). The rest of the earlier verification (logit parity, greedy decode, failed-init sequence) carries over: the delta since that round is the 4 lines in get_token_probabilities() and one comment.
Thanks for the quick and exact fix.
Review made with AI on behalf of the strix-llama.cpp maintainers.
Full verification (for reproduction and agents)
Environment
- Linux 7.0.0, gcc 15.2.0, cmake 4.2.3, Ninja
- ROCm 10.1 (hipconfig 7.16.26385), AMD clang 24.0.0git d6f6cb69
- Mesa 26.0.8 RADV (Radeon 8060S, RADV STRIX_HALO)
Compared
- master tip: 2c3dc19
- PR head: 87fe47c (merge base = master tip, 0 commits behind)
- previous head (our rebase): f27d8c0, used as the negative control
Range-diff f27d8c0 -> 87fe47c
Only two changes: get_token_probabilities() in tools/server/server-common.cpp now uses llama_vocab_n_tokens() as the count when llama_get_sampled_logits_ith() is null (the row was not backend-sampled), so it reads the raw logits with identity ids. Before, it used llama_get_sampled_logits_count_ith(), which is 0 for such a row when any other seq has a backend chain. The comment at server-context.cpp:2300 was also corrected. Commit message updated. Without backend sampling the count is n_vocab both before and after, so that path does not change.
Build
cmake -G Ninja -S <tree> -B <tree>/build-hip -DCMAKE_BUILD_TYPE=Release -DGGML_HIP=ON -DGGML_VULKAN=OFF -DGPU_TARGETS=gfx1151 -DCMAKE_HIP_COMPILER=$(hipconfig -l)/clang -DCMAKE_PREFIX_PATH=$(hipconfig -R)
cmake -G Ninja -S <tree> -B <tree>/build-vk -DCMAKE_BUILD_TYPE=Release -DGGML_VULKAN=ON -DGGML_HIP=OFF
cmake -G Ninja -S <tree> -B <tree>/build-cpu -DCMAKE_BUILD_TYPE=Release -DGGML_VULKAN=OFF -DGGML_HIP=OFF
Models
| model | source | sha256 |
|---|---|---|
| stories260K (CI test model) | ggml-org/test-model-stories260K, stories260K-f32.gguf | 270cba1bd5109f42d03350f60406024560464db173c0e387d91f0426d3bd256d |
| Qwen3-0.6B Q8_0 | bartowski/Qwen_Qwen3-0.6B-GGUF, Qwen_Qwen3-0.6B-Q8_0.gguf | c159d1518f16bc42533d9a09f034eb598b670341adc2b08b9a9751614aea71eb |
Commands
- CI reproduction:
llama-server -m <stories260K> --temp 0.8 --seed 42 --batch-size 32 --no-slots --ctx-size 512 --n-predict 64 --no-jinja [-ngl 99]with--parallel 2 -bs,--parallel 2+LLAMA_ARG_BACKEND_SAMPLING=1,--parallel 4 -bs,--parallel 1 -bs, and--parallel 2without backend sampling. Per fresh server:/completion{"prompt":"I believe the meaning of life is","n_probs":10,"temperature":0.0,"n_predict":5,"cache_prompt":false}, then/v1/chat/completionsas intest_logprobs(max_tokens5,top_logprobs10, temperature 0), then the/completionrequest again, then withpost_sampling_probs. Count of top probs per token;completion_probabilitiesJSON compared withcmp. - Qwen3-0.6B,
-c 4096 -np 2 [-bs] [-ngl 99],n_probs10, 16 tokens, temperature 0,id_slotset: sequencen_probson s1, s0, plain request on s1,n_probson s0, s1, s0. Concurrent: slot 0 generates 400 tokens (ignore_eos, temperature 0.8) while slot 1 runsn_probs10. - pytest (in
tools/server/tests,LLAMA_ARG_BACKEND_SAMPLING=1):pytest unit/test_completion.py unit/test_chat_completion.py -k "n_probs or logprobs"on CPU, ROCm and Vulkan, master and PR binaries; the full two files on CPU with and without backend sampling. - Unit:
test-backend-samplerwithLLAMACPP_TEST_MODELFILE=<Qwen3-0.6B>;ctest --test-dir <build> --output-on-failure -L main -E 'test-opt|test-llama-archs'. - Reserves: 50 sequential
{"prompt":"Tell me a fact about cats.","n_predict":8,"temperature":0.8,"seed":42,"id_slot":0,"cache_prompt":false},-c 4096 -np 1 -v -lv 4 -ngl 99 [-bs], countingsched_reserve: reserving. - Latency: the same request,
-c 4096 -np 1 -sps 0 --no-cache-prompt -ngl 99 [-bs], 3 warm-up then 50 requests, median ofprompt_ms + predicted_ms, 2 rounds, master and PR alternated.
Results
Top probs per token (min over the 5 or 16 tokens), the same on CPU, ROCm and Vulkan unless noted:
| case | master | previous head f27d8c0 (CPU) | PR 87fe47c |
|---|---|---|---|
stories260K /completion, -bs -np 2 |
10 | 0 | 10 |
stories260K /completion, env, -np 2 |
10 | 0 | 10 |
stories260K /completion + chat + again, -bs -np 4 |
10, 10, 10 | 0, 0, 0 | 10, 10, 10 |
stories260K, -bs -np 1 |
10 | 10 | 10 |
| stories260K, no backend sampling | 10 | 10 | 10 |
Qwen3 sequence s1, s0, (plain s1), s0, s1, s0, -bs |
10, 10, -, 10, 10, 10 | 0, 10, -, 0, 10, 10 | 10, 10, -, 10, 10, 10 |
Qwen3 concurrent, other slot generating with -bs |
0 | 0 | 10 |
| Qwen3 concurrent, no backend sampling | 10 | 10 | 10 |
completion_probabilities JSON: PR with backend sampling == master with backend sampling == PR without backend sampling, byte-identical, for every stories260K case and the Qwen3 sequence, on all three backends. With post_sampling_probs and temperature 0, every side returns 1 top prob (the greedy token).
pytest:
| run | master | PR |
|---|---|---|
CPU, bs, -k "n_probs or logprobs" |
6 passed | 6 passed |
| Vulkan, bs, same | 6 passed | 6 passed |
| ROCm, bs, same | 5 passed, 1 failed | 5 passed, 1 failed |
| CPU, bs, full two files | 99 passed, 1 failed, 1 skipped | 99 passed, 1 failed, 1 skipped |
| CPU, no bs, full two files | 99 passed, 1 failed, 1 skipped | 99 passed, 1 failed, 1 skipped |
The ROCm failure is test_n_probs_post_backend_sampling (sampled id 368 vs 421 between backend and CPU sampling), on master too; test-backend-sampler skips the TOP_K tests on ROCm for the same reason. The CPU failure is test_completion_stream_with_openai_library_stops: its preset model was not in the local offline cache, so the server did not start, on both sides.
Unit tests: test-backend-sampler exit 0 on CPU (19 PASSED), Vulkan (19 PASSED) and ROCm (13 PASSED, 6 skipped: no backend TOP_K on HIP); sampler reuse test PASSED on all three. ctest -L main: 35/35 on ROCm and Vulkan.
Reserves in 50 requests (including 1 at startup):
| bs off | bs on | |
|---|---|---|
| ROCm master / PR | 1 / 1 | 51 / 2 |
| Vulkan master / PR | 1 / 1 | 51 / 2 |
Latency, median ms per round: see the table in Performance (round 1 / round 2).
Correctness
| check | method | result |
|---|---|---|
| Build, 3 backends | cmake above | pass, master and PR |
n_probs / logprobs with backend sampling, -np >= 2 |
CI reproduction and Qwen3 sequence | PASS: 10 on the PR, 0 on f27d8c0 |
| Master mixed-mode case | concurrent test | PASS: 0 on master, 10 on the PR |
| Probability values | cmp of the JSON |
identical to master and to bs off |
| Server pytest, bs | as above | same as master on all three backends |
| test-backend-sampler | Qwen3-0.6B | pass, 3 backends |
| ctest label main | as above | 35/35, ROCm and Vulkan |
| GitHub CI Server, "Tests (Backend sampling)" | Actions | pass |
| Logit parity, greedy decode, failed-init sequence | earlier round | carried over: the delta does not touch them |
server-test-*.py, ASan, TTS |
- | not run |
Claims
- Fixes empty
top_logprobsforn_probswith backend sampling and 2 or more slots: reproduced on CPU, ROCm and Vulkan. - Also fixes the mixed-mode case on master: reproduced.
- No change without backend sampling: reproduced (probabilities identical, latency and reserves equal).
- Removes the per-request
sched_reservewith backend sampling (51 -> 2) and lowers short-request latency: reproduced.
Not tested
scripts/server-test-*.py, ASan and TTS.
Split from #273 per review (sampler piece only; DeepSeek V4.1 will come in via an upstream sync, Q1_0 recipe split to its own PR #275, now closed).
What
llama_context::set_sampler: re-registering the same sampler pointer is a no-op (graph inputs hold the pointer; backend_init asserts !is_init). The same-pointer check runs before the debug log.common_sampler_reuse(): in-place re-init when the effective sampling config is unchanged, keeping the chain pointer stable for graph reuse. Compares the plain fields first and only builds the grammar / reasoning-budget samplers on a match, so a grammar or json_schema request on a backend-sampling server no longer builds its grammar twice.load_task: try sampler reuse when the server runs with backend sampling and the slot has a chain;common_sampler_reuseitself refuses when the stored chain's effective backend flag differs from the request's, so a non-backend chain can never be reused on the backend path and the default (backend sampling off) path is identical to master. On rebuild the old chain is unregistered (a no-op when it was never registered) andslot.smplis reset, so a failed init cannot leave a backend-initialized chain on the CPU path. No per-slot registration flag is needed. Slot release no longer unregisters.Review fixes
From the first #274 review:
slot.smpl.reset()right after the unregister (item 1 — the garbage-token repro no longer fires); the effective backend flag is computed from thegrmr/rbudgetpointerscommon_sampler_reusebuilds (item 2);test_sampler_reuseadded (item 3); reserve-count claim corrected;Assisted-by: Hermes Agenttrailer, comments shortened.From the second #274 review:
test_sampler_reuse_after_failed_initintools/server/tests/unit/test_completion.py, next totest_n_probs_post_backend_samplingtest_sampler_reusereworked: the penalties sampler is in the chain, the chain is used for 16 steps before reuse, and 16 steps after reuse are compared with a fresh chain (state after use is covered); a real backend case usestest_context: register, decode, accept, reuse, re-register the same pointer (the newllama_context::set_samplerno-op), decode, compared with a fresh chain registered under a new pointer at the reuse point in an identically fed contextstatic_assert(sizeof(common_params_sampling) == 704)behind an__x86_64__/__GLIBCXX__guard — most new upstream fields read bycommon_sampler_initchange the struct size and break the build instead of silently passing the gate (a small field can fit in trailing padding; strings, vectors and 8-byte fields trip it)smpl_backend_samplingdropped (registration state is implied by the effective-flag comparison; unregistering an unregistered sampler is a no-op)^ 0x9e3779b9umoved to a shared helper used by init and reuseNumbers (public model:
Qwen_Qwen3-0.6B-Q8_0.gguf, bartowski, sha256c159d151…aea71eb)Strix Halo (gfx1151), ROCm HIP Release, merge-base
0c1021evs head,-np 1, 50 sequential requests (8 tokens) x 3 runs per cell. Median ms per request = server-sideprompt_ms + predicted_ms. Reserve counts from a separate-lv 4run (includes startup lines).With backend sampling on, reserves during the 50 requests drop from 50 to 0 and median latency by 12.6% (55.3 -> 48.3 ms), back to the bs-off level. With backend sampling off the PR is unchanged vs master — reuse is not attempted.
Correcting an earlier claim: master does one
sched_reserveper request at-np 1(50 for 50 requests), not two; two passes only when another slot decodes between release and the next request (reproduced at-np 2).Measurement commands
Per cell (bs off/on x master/PR), then medians of the per-request
prompt_ms + predicted_ms:Reserve counts: same server with
-v -lv 4, thengrep -c "sched_reserve: reserving"over the log after 50 requests.Item-1 repro (A -> B -> C): A =
{"prompt":"The history of the city of Rome begins","n_predict":32,"temperature":0.8,"seed":1,"samplers":["top_k","top_p","temperature"],"top_k":40,"top_p":0.95,"id_slot":0,"backend_sampling":true,"n_probs":5,"post_sampling_probs":true}, B = A +"grammar":"root ::= ("(400), C = A withpost_sampling_probs:false. Before the fix C returned token 0 repeated; after the fix C is byte-identical to C on a fresh server. Also covered withrepeat_penalty:0as B. The server testtest_sampler_reuse_after_failed_initis a related, distinct sequence (backend sampling on, no n_probs): after B,slot.smplis null, so C does a fresh init; without the unregister + reset inload_task, C re-inits a backend-initialized chain and the server aborts inllama_sampler_chain_backend_init()called twice. Run:pytest tools/server/tests/unit/test_completion.py -k sampler_reuse_after_failed_initwithLLAMA_SERVER_BIN_PATHset.Verification
Tested on Strix Halo (gfx1151), ROCm. Item 1 repro (A -> B -> C): 32x token 0 before the fix, normal text after, byte-identical to the fresh-server reference.
test_sampler_reusepasses (CPU parity, backend parity, thetest_contextbackend case, the gate table, the failed-init sequence). Not tested here: Vulkan, ASan.An AI agent (Hermes) wrote this PR and the fixes; the measurements above were run on the hardware described.