Skip to content

server: reuse slot samplers across requests to avoid sched_reserve rebuilds - #274

Merged
dzannotti merged 1 commit into
halo-box:masterfrom
baraxnaxgaming-commits:pr/sampler-reuse
Nov 22, 2026
Merged

dzannotti merged 1 commit into
halo-box:masterfrom
baraxnaxgaming-commits:pr/sampler-reuse

Conversation

@baraxnaxgaming-commits

@baraxnaxgaming-commits baraxnaxgaming-commits commented Nov 10, 2026 •

Copy link
Copy Markdown

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.
  • server load_task: try sampler reuse when the server runs with backend sampling and the slot has a chain; common_sampler_reuse itself 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) and slot.smpl is 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 the grmr / rbudget pointers common_sampler_reuse builds (item 2); test_sampler_reuse added (item 3); reserve-count claim corrected; Assisted-by: Hermes Agent trailer, comments shortened.

From the second #274 review:

  • the two one-off scripts are removed from the PR; their commands are inlined below, and the failed-init sequence is now a server test: test_sampler_reuse_after_failed_init in tools/server/tests/unit/test_completion.py, next to test_n_probs_post_backend_sampling
  • test_sampler_reuse reworked: 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 uses test_context: register, decode, accept, reuse, re-register the same pointer (the new llama_context::set_sampler no-op), decode, compared with a fresh chain registered under a new pointer at the reuse point in an identically fed context
  • compile-time tripwire in the test: static_assert(sizeof(common_params_sampling) == 704) behind an __x86_64__/__GLIBCXX__ guard — most new upstream fields read by common_sampler_init change 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_sampling dropped (registration state is implied by the effective-flag comparison; unregistering an unregistered sampler is a no-op)
  • seed mix ^ 0x9e3779b9u moved to a shared helper used by init and reuse

Numbers (public model: Qwen_Qwen3-0.6B-Q8_0.gguf, bartowski, sha256 c159d151…aea71eb)

Strix Halo (gfx1151), ROCm HIP Release, merge-base 0c1021e vs head, -np 1, 50 sequential requests (8 tokens) x 3 runs per cell. Median ms per request = server-side prompt_ms + predicted_ms. Reserve counts from a separate -lv 4 run (includes startup lines).

bs master medians PR medians master reserves PR reserves
off 47.4 / 47.6 / 52.8 47.3 / 47.5 / 52.9 1 1
on 55.2 / 55.3 / 61.1 48.3 / 48.3 / 54.0 51 2

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_reserve per 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:

REQ='{"prompt":"Tell me a fact about cats.","n_predict":8,"temperature":0.8,"seed":42,"id_slot":0,"cache_prompt":false,"return_tokens":true}'
llama-server -m $MODEL -ngl 99 -fa on -c 4096 -np 1 -sps 0 --no-cache-prompt [-bs] --host 127.0.0.1 --port 30274 &
# 3 warm-up requests, then 3 runs x 50 sequential requests:
for _ in $(seq 50); do
  curl -s -H "Content-Type: application/json" -d "$REQ" http://127.0.0.1:30274/completion |
    python3 -c "import json,sys; t=json.load(sys.stdin).get('timings', {}); print(t.get('prompt_ms', 0.0) + t.get('predicted_ms', 0.0))"
done

Reserve counts: same server with -v -lv 4, then grep -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 with post_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 with repeat_penalty:0 as B. The server test test_sampler_reuse_after_failed_init is a related, distinct sequence (backend sampling on, no n_probs): after B, slot.smpl is null, so C does a fresh init; without the unregister + reset in load_task, C re-inits a backend-initialized chain and the server aborts in llama_sampler_chain_backend_init() called twice. Run: pytest tools/server/tests/unit/test_completion.py -k sampler_reuse_after_failed_init with LLAMA_SERVER_BIN_PATH set.

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_reuse passes (CPU parity, backend parity, the test_context backend 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.

@baraxnaxgaming-commits

baraxnaxgaming-commits commented Nov 10, 2026 •

Copy link
Copy Markdown
Author

Measurements + both review items, on Strix Halo (RX 8060S, ROCm 7.2, HIP build, Release).

Setup — merge-base 0c1021e1f6 vs this PR head 684a689726, server with backend sampling on (-bs), a local 35B-class Q8_0 model (not published), -c 65536 --parallel 1 -ngl 999, 13 identical back-to-back /completion requests (temp 0, seed 42, 64 tokens).

Reserve elimination (the point of the PR)

merge-base this PR
sched_reserve passes over the run 15 (one per request, 14–16 ms each) 3 (init + first registration + pp-shape only)
median request latency 1.440 s 1.451 s

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 — n_probs in the reuse check — agreed, fixed in common_params_sampling_equal (a.n_probs == b.n_probs, with a comment on why it isn't runtime-only). Regression test: request 1 with n_probs=0, temp=0 (greedy chain), request 2 with n_probs=5, post_sampling_probs=true on the same slot -> reuse gate refuses, chain rebuilds, response carries 8 prob groups with non-empty top_probs. Before the fix the second request returned empty probs.

Review item 2 — gsmpl->rng reseed on reuse — agreed, fixed: common_sampler_reuse now reseeds gsmpl->rng the same way common_sampler_init does. Regression test: a local 27B MTP-capable Q8_0 model (not published) with --spec-type draft-mtp (self-draft), temp 0.7, seed 42, two identical back-to-back requests -> outputs byte-identical with drafting enabled.

Both fixes are in the pushed head; the A/B numbers above are from the fixed build.

@dzannotti dzannotti added in-review We are reviewing. verifying Building and running the PR to verify its claims. and removed in-review We are reviewing. labels Nov 12, 2026

@dzannotti dzannotti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. Blocking, tools/server/server-context.cpp:2229-2252: when reuse fails, the old sampler is unregistered and then common_sampler_init is called. If init throws (invalid grammar, repeat_penalty:0), slot.smpl keeps 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 example n_probs > 0 with post_sampling_probs:false) passes the gate and reuses it on the CPU path. llama_sampler_chain_apply then skips the samplers it thinks the GPU already ran, so no token is selected and common_sampler_sample reads cur_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: call slot.smpl.reset() right after the unregister at line 2242, and try reuse only when use_backend_sampling && slot.smpl_backend_sampling. The second change also keeps the default path identical to master.
  2. Should fix, common/sampling.cpp:548-561: common_params_sampling_equal repeats the grammar and reasoning-budget presence rules of common_sampler_build_constraints. common_sampler_reuse builds grmr and rbudget anyway, 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 the GGML_ASSERT in common_sampler_sample aborts the server.
  3. Should fix: add tests. tests/test-backend-sampler.cpp already 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 expects common_sampler_reuse to 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.
  4. 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.
  5. 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 4 with 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>, then cmp of the logits and tokens files. ROCm also with HIP_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 with cache_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 2 a streaming ignore_eos request keeps slot 1 busy during the run. Reserve counts come from a separate -lv 4 run (the sched_reserve: reserving lines 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"):

  1. 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.
  2. B: A plus "grammar":"root ::= (" returns 400 "Failed to initialize samplers: failed to parse grammar" (master and PR). B with "repeat_penalty":0 instead triggers the same state.
  3. 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

  1. Item 1 above. Root cause: slot.smpl_backend_sampling records 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 keeps is_init. The gate at line 2229 then matches a request with backend sampling off.
  2. Item 2 above.
  3. With backend sampling off, set_sampler(nullptr) already returns early on master, so no reserve happens. The PR still sends every such request through common_sampler_reuse instead of common_sampler_init. Timing is unchanged (within 0.5 ms), so this path adds risk with no gain.
  4. 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.
  5. The chain perf counters (t_sample_us, n_sample) are not reset on reuse. The server does not print them.
  6. 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() and load_task hunks sit in code that upstream changes often, so expect merge conflicts during syncs.

Claims

  • Two sched_reserve passes per request with backend sampling on: not reproduced at -np 1 (one per request). Reproduced at -np 2 with 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.

@dzannotti dzannotti added risk:medium Risk: medium waiting-user We asked the author for something. and removed verifying Building and running the PR to verify its claims. labels Nov 12, 2026
@baraxnaxgaming-commits

Copy link
Copy Markdown
Author

All five asks addressed in 59e2f97.

1 (blocking) — slot.smpl.reset() now runs right after the unregister, and reuse is attempted only when use_backend_sampling && slot.smpl_backend_sampling. The default path is byte-for-byte the master path (reuse is not attempted with backend sampling off; the bs-off timing cells confirm: 47.4 vs 47.3 ms medians, 1 reserve per run on both sides). Your A/B/C repro (scripts/repro-item1-274.sh, ROCm, Qwen3-0.6B Q8_0): C now returns 1657 21787 4134 2503 323 432 374 1083 … — identical to the fresh-server reference in your table, 25 unique tokens of 32.

2 — common_sampler_reuse builds grmr / rbudget first and passes params.backend_sampling && !grmr && !rbudget into common_params_sampling_equal as a parameter; the re-derived presence rules are deleted, so the copies can no longer drift. On a gate miss the freshly built constraints are freed before returning false.

3 — test_sampler_reuse in tests/test-backend-sampler.cpp: reuse-vs-fresh token parity with a fixed seed (16 steps, chains fed identical logits, penalties exercised via accept history), for backend_sampling off and on; the seedless case (gate allows reuse, per-instance rngs differ by design); a table mutating one gate field at a time across 18 fields (incl. n_probs, n_prev, grammar, reasoning budget, backend_sampling) each expecting false; and the failed-init sequence (throwing reuse leaves the chain intact and reusable). Passes on the ROCm build.

4 — Claim corrected in the body: one reserve per request at -np 1 (we measured 51 for 50 requests on master, 2 on the PR), two only with another slot decoding between release and the next request. Before/after on the public model (bartowski Qwen_Qwen3-0.6B-Q8_0, sha256 c159d151…, same file as your table) is in the body: bs on, 50x8-token requests x 3 runs, medians 55.2/55.3/61.1 -> 48.3/48.3/54.0 ms (server-side prompt_ms + predicted_ms), bs off unchanged vs master.

5 — Assisted-by: Hermes Agent trailer added; the 3-6 line comments at the cited spots are trimmed to 1-2 lines.

Nits: e.g, fixed; the (cherry picked from …) line dropped.

Not tested here: Vulkan, server pytest suite, ASan.

@dzannotti dzannotti self-assigned this Nov 12, 2026
@dzannotti dzannotti added in-review We are reviewing. and removed waiting-user We asked the author for something. labels Nov 12, 2026

@dzannotti dzannotti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. Blocking: remove scripts/04-measure-274-public.sh and scripts/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 to tools/server/tests/unit/test_completion.py next to test_n_probs_post_backend_sampling: the A, B (expect 400), C sequence, then compare C with C on a fresh server.
  2. Should fix, tests/test-backend-sampler.cpp:2071-2187: the test does not check what the PR text says. The backend_sampling = true case never registers the chain on a context and never decodes, so it runs the same CPU code as the false case. The chain is reused right after init, so leftover state from an earlier request is never tested. penalty_repeat is set, but samplers has no COMMON_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 with test_context (as in test_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 in llama_context::set_sampler.
  3. Should fix, common/sampling.cpp:545-598: if an upstream sync adds a field that common_sampler_init reads, 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 example static_assert(sizeof(common_params_sampling) == N, "update common_params_sampling_equal") behind an x86_64/libstdc++ guard.
  4. Should fix, common/sampling.cpp:606-619 and tools/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 or json_schema builds its grammar twice, because the stored chain is always a backend chain and the gate always misses. Also consider dropping smpl_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 when llama_set_sampler returns false.
  5. Should fix, comments: common/sampling.cpp:540-544 is still 5 lines (the repo asks for 1 to 2). common/sampling.h:49-50 says "on false ... may normalize params", but only the success path changes params.

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 with repeat_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_schema between 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 and test-backend-sampler give the same results as master. The new test_sampler_reuse passes. 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 with cache_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":0 as the failing request, and on a server with -bs and 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 4 and a count of sched_reserve: reserving; R, grammar, R, json_schema, R; R, chat with tools, R; R, chat with reasoning_budget_tokens:16, R; R, R with seed 2, R; R with n_probs 5 twice; seedless R five times; -np 2 with slot 0 streaming 512 tokens while slot 1 loops R, backend off, invalid grammar and seed 7; --sleep-idle-seconds 3; --slot-save-path save and restore; n_cmpl:2 twice; 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 penalties in 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>, then cmp of every output file. ROCm also with HIP_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 server prompt_ms + predicted_ms. Reserve counts come from a separate -v -lv 4 run.
  • 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 with n_predict:256, ignore_eos:true, id_slot:0, reading predicted_per_second. Control: slots 1-3 re-warmed with backend_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 through curl --data-binary @file because the script's -d argument 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

  1. The two scripts (ask 1).
  2. 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.
  3. The gate compares every field that common_sampler_init and common_sampler_build_constraints read 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).
  4. 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.
  5. After a failed init, slot.smpl is null until the next request. Every reader of slot.smpl needs an active task, so this is safe.
  6. 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. Adding top_p or min_p, or the default chain, makes it repeatable. Vulkan is repeatable. Our base request uses top_p for 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_reuse passes: 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.

@dzannotti dzannotti added verified The claims were reproduced. waiting-user We asked the author for something. and removed in-review We are reviewing. labels Nov 13, 2026
@baraxnaxgaming-commits

Copy link
Copy Markdown
Author

All five asks addressed, head b953642511 (amended, one commit).

  1. Scripts removed (blocking). git rm'd both; the measurement and repro commands are inlined in the PR description. The failed-init sequence is now test_sampler_reuse_after_failed_init in tools/server/tests/unit/test_completion.py, next to test_n_probs_post_backend_sampling: A, B (expect 400, both grammar: "root ::= (" and repeat_penalty: 0), C, then C compared with C on a fresh server. Ran on ROCm: 2 passed (new test + neighbor) with LLAMA_SERVER_BIN_PATH at the PR build.

  2. Test reworked. The chain now includes COMMON_SAMPLER_TYPE_PENALTIES; the parity case samples 16 steps, reuses, and compares the next 16 steps with a fresh chain (same rows, same seed) — reuse must clear the state accumulated by use. Runs for backend_sampling off and on. Added the real backend case with test_context: register, decode, reuse, llama_set_sampler with the same pointer (the new no-op), decode — compared token-for-token with a fresh chain taking the same path in a fresh context. One note: with the field-compare-first change (4), reuse with a changed grammar no longer builds and throws — it misses on the plain fields, so the C++ failed-init block asserts the gate miss and chain survival; the throwing-init path is covered end-to-end by the pytest test, which is where the original bug lived.

  3. Tripwire added: static_assert(sizeof(common_params_sampling) == 704, "update common_params_sampling_equal") behind __x86_64__ && __GLIBCXX__ in the test.

  4. Fields compared first. common_sampler_reuse now runs the plain-field equality, and only builds grmr / rbudget on a match, then checks the effective backend flag — a grammar request on a backend-sampling server builds its grammar once (on the rebuild path, where it is needed). smpl_backend_sampling is dropped: the stored chain's effective flag is compared inside reuse, so a non-backend chain can never match a backend request, and unregistering an unregistered sampler is a no-op. Verified on the server: A/B/C clean, 50 requests with -bs -> 2 reserves, grammar request 200 with grammar-constrained output, back-to-plain request clean.

  5. Comments: the 5-line block is now 2 lines; sampling.h says "on false, gsmpl and params are untouched" (checked: common_sampler_build_constraints is read-only on params).

Nits: same-pointer check moved above the debug log; seed mix is a shared helper common_sampler_rng_seed() used by init and reuse; the PR description no longer names a private machine (says "Strix Halo (gfx1151)").

test-backend-sampler full suite passes on ROCm (exit 0) with the new cases.

An AI agent (Hermes) made these changes.

@dzannotti dzannotti added in-review We are reviewing. and removed waiting-user We asked the author for something. verified The claims were reproduced. labels Nov 13, 2026
@dzannotti dzannotti added the verifying Building and running the PR to verify its claims. label Nov 13, 2026
@dzannotti dzannotti added needs-human Review done; a maintainer must approve. verified The claims were reproduced. and removed verifying Building and running the PR to verify its claims. labels Nov 15, 2026
@baraxnaxgaming-commits

Copy link
Copy Markdown
Author

Both should-fixes and all nits addressed, head 6f234ba93f (amended, one commit).

  1. test_context case fixed. The reference run now creates a fresh chain at the reuse point and registers it under its new pointer with llama_set_sampler; both runs call common_sampler_accept(s, t1, true) before the reuse point so the penalties hold state. Verified against your E3a mutation (reuse returns true after the flag check, no reset, no reseed): with the CPU parity loop's comparison removed, the reworked case fails — GGML_ASSERT(t2 == t2_ref && "backend reuse diverged from a fresh chain") at the test_context case. The case now catches reuse-reset removal on the backend path.

  2. Text. The pytest docstring describes the invariant: the failed request must not leave the slot's chain registered, so the next request re-inits it, aborting in llama_sampler_chain_backend_init() called twice. No more "garbage", no "hits the reuse path" (C does a fresh init after B resets slot.smpl), no "PR server: reuse slot samplers across requests to avoid sched_reserve rebuilds #274" in the docstring; the local variable reused is renamed after_fail. The PR description now separates the item-1 repro (the original A/B/C with n_probs + post_sampling_probs, CPU path) from the pytest test (backend path, fresh init, guards the unregister + reset) and says "most new fields" for the tripwire, noting small fields can fit in padding.

Nits: "cache_prompt": False added to base in the new pytest; the 3-line test_context comment is now 2 lines; test_sampler_reuse ends with printf("sampler reuse test PASSED\n") and the tripwire moved above struct backend_test_case; the set_sampler comment in server-context.cpp is above the upstream TODO line; the 2026-11-10 comment edited to drop the two local model file names.

Re-verified on ROCm after these edits: test-backend-sampler full suite exit 0 (13 PASSED lines), pytest unit/test_completion.py -k "sampler_reuse_after_failed_init or n_probs_post_backend" 2 passed with cache_prompt: False in place.

Thanks for the mutation testing — E3a passing the old case was a real gap.

An AI agent (Hermes) made these changes.

@dzannotti dzannotti added in-review We are reviewing. verifying Building and running the PR to verify its claims. and removed needs-human Review done; a maintainer must approve. labels Nov 18, 2026

@dzannotti dzannotti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. Blocking, tools/server/server-context.cpp (the removed llama_set_sampler(ctx_tgt, id, nullptr) in server_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 > 0 without post_sampling_probs) runs on the CPU path, but another seq still has a registered chain. Then get_sampled_logits_count() returns 0 for its row, get_token_probabilities() (tools/server/server-common.cpp:1846-1851) returns an empty list, and completion_probabilities[].top_logprobs is 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 -bs server with -np 2 or more (the default -np is auto, 4). Suggested fix: in get_token_probabilities(), use the raw logits with n_vocab and identity ids when llama_get_sampled_logits_ith(ctx, idx) is null (the row was not backend-sampled), as common_sampler's set_logits already 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_probs and chat logprobs, 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_probs request 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.

  1. common/sampling.cpp: master added, in common_sampler_init, a check that throws "EOG tokens cannot be banned when a grammar is set". The PR moved that part of common_sampler_init into common_sampler_build_constraints. We put the check into the PR's common_sampler_init, right after vocab is read, next to the other parameter checks. common_sampler_reuse matches only when grammar and logit_bias equal the params that already passed this check, so the reuse path does not need it.
  2. tools/server/server-context.cpp (load_task, initialize samplers): master added use_backend_sampling &= task.type != SERVER_TASK_TYPE_TTS;. We kept it before the reuse attempt, and added && task.type != SERVER_TASK_TYPE_TTS to the PR's recompute of use_backend_sampling after 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 with LLAMA_ARG_BACKEND_SAMPLING=1 instead 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/completions as in test_logprobs (max_tokens 5, logprobs true, top_logprobs 10, temperature 0). Count top_logprobs per token.
  • Qwen3-0.6B, -c 4096 -ngl 99, n_probs 10, 16 tokens, temperature 0: (a) -np 2 -bs, fresh server, n_probs on slot 1; (b) a plain request on slot 0, then n_probs on slot 1; (c) -np 1 -bs, plain then n_probs; (d) all of these without -bs; (e) n_probs twice on slot 0 with -np 2 -bs. Sequence on -np 2 -bs: n_probs on s1, s0, plain on s1, n_probs on s0, s1, s0. Concurrent: slot 0 generates 400 tokens with backend sampling while slot 1 runs n_probs 10.
  • Unit: ctest --test-dir <build> --output-on-failure -L main -E 'test-opt|test-llama-archs'; test-backend-sampler with LLAMACPP_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, counting sched_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 server prompt_ms + predicted_ms.
  • Failed-init: the sequence of test_sampler_reuse_after_failed_init by hand on -np 2 -bs, for grammar: "root ::= (" and repeat_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>, cmp of 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

  1. 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, and get_sampled_logits_count() returns 0 for a row whose seq has no chain. get_token_probabilities() then returns an empty list. common_sampler's set_logits is not affected, because it falls back to the raw logits when llama_get_sampled_logits_ith() is null; the suggested fix uses the same rule. n_probs on speculative rows (populate_token_probs from the draft verify path) goes through the same function.
  2. Optional: the comment at server-context.cpp:2300, see Asks.

Claims

  • Removes the per-request sched_reserve with 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_probs with 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.

@dzannotti dzannotti added waiting-user We asked the author for something. and removed in-review We are reviewing. verifying Building and running the PR to verify its claims. verified The claims were reproduced. labels Nov 18, 2026
@dzannotti

Copy link
Copy Markdown
Collaborator

Rebased onto current master (2c3dc19), no conflicts; HIP and Vulkan build and ctest pass. New head f27d8c0.
Before you push again: git fetch && git reset --hard origin/pr/sampler-reuse
Still waiting on the asks in the last review.

…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
@baraxnaxgaming-commits

Copy link
Copy Markdown
Author

Ask 1 (blocking) is fixed in 87fe47c98 (amended onto your rebase f27d8c0; local branch reset to origin/pr/sampler-reuse first, as instructed).

Fix - get_token_probabilities (tools/server/server-common.cpp): when llama_get_sampled_logits_ith() is null the row was not backend-sampled, so llama_get_sampled_logits_count_ith() returns 0 and the function returned an empty list. It now uses the raw logits with n_vocab and identity ids in that case, the same pattern as common_sampler::set_logits. 4 lines, one file.

Verification (Vulkan build, Windows desktop, tinyllamas/stories260K.gguf, -c 1024 -bs --temp 0, request: n_probs=10, n_predict=5, seed=42):

  • Pre-fix head, -np 2: top_logprobs empty on all 5 tokens (the reported bug, reproduced). Same binary, -np 1: 10 per token (control).
  • Fixed head, -np 2: 10 per token, and the full completion_probabilities JSON is byte-identical to the -np 1 reference.
  • Mixed mode, -np 2: an n_probs request while the other slot decodes -> 10 per token. Covers the master-side case you described.
  • logprobs=5 on -bs -np 2: 5 per token. Grammar + n_probs on -bs -np 2 (CPU-downgraded chain): 10 per token.
  • bs off, -np 2: 10 per token, unchanged - with no backend sampling data get_sampled_logits_count() already returned n_vocab, so the null branch gives the same value as before.
  • test_sampler_reuse standalone: PASSED on this build.

Nit: the comment at server-context.cpp:2300 now says a grammar-downgraded chain is non-backend, not that a non-backend chain "never matches".

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 dzannotti added verifying Building and running the PR to verify its claims. and removed waiting-user We asked the author for something. labels Nov 22, 2026

@dzannotti dzannotti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_sampling fails 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 full test_completion.py and test_chat_completion.py on CPU give the same result on master and on the PR, with and without backend sampling.
  • test-backend-sampler passes 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 2 without 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/completions as in test_logprobs (max_tokens 5, top_logprobs 10, temperature 0), then the /completion request again, then with post_sampling_probs. Count of top probs per token; completion_probabilities JSON compared with cmp.
  • Qwen3-0.6B, -c 4096 -np 2 [-bs] [-ngl 99], n_probs 10, 16 tokens, temperature 0, id_slot set: sequence n_probs on s1, s0, plain request on s1, n_probs on s0, s1, s0. Concurrent: slot 0 generates 400 tokens (ignore_eos, temperature 0.8) while slot 1 runs n_probs 10.
  • 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-sampler with LLAMACPP_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], counting sched_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 of prompt_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_logprobs for n_probs with 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_reserve with backend sampling (51 -> 2) and lowers short-request latency: reproduced.

Not tested

scripts/server-test-*.py, ASan and TTS.

@dzannotti dzannotti added ready-to-merge Review done, low risk, approved. verified The claims were reproduced. and removed verifying Building and running the PR to verify its claims. labels Nov 22, 2026
@dzannotti
dzannotti merged commit f702288 into halo-box:master Nov 22, 2026
18 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge Review done, low risk, approved. risk:medium Risk: medium server testing verified The claims were reproduced.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants