Repository navigation
serve: leave the prompt's last token to the decoder, so a hybrid handoff is not thrown away (engine#941) - #967
Conversation
…not thrown away (engine#941) The hybrid route handed the provider the whole prompt and then restored/adopted that state. A hybrid model's recurrent memory keeps its prefix in one cell, so the backend cannot trim it back by the one position its cache path needs ([TAG_PROMPT_LOGITS]), and the handoff was discarded: the decoder re-processed the entire prompt (engine#941: 287 / 847 / 3 286 tokens for prompts of the same length). The provider is now handed the prompt minus its last token, and restore/adopt say "leave_one_token": the decoder evaluates that one token from state that is genuinely in place. The flag is what lets the pinned server record the handoff as an in-place (data-less) context checkpoint instead of resetting n_past to 0 - fork 1bit-MONSTER/llama.cpp#<fork-pr>. One token on the decoder replaces the prompt. Prompts shorter than 2 tokens have nothing to hand over and keep the old fallback; a sliding-window model is refused the checkpoint by the fork side and re-processes as before. Closes #941
|
#967 ready for review at 3ae7a3f (pairs with fork 1bit-MONSTER/llama.cpp#189 — the engine half alone is a no-op on the pinned fork; pointer bump after #189 merges) |
PR Reviewer Guide 🔍(Review updated until commit 93560d5)Here are some key observations to aid the review process:
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: CHANGES — the code half is right, but the documentation half describes behaviour the pinned backend does not have, and the pin is the fork's own tip.
What is right
head(ids.begin(), ids.end() - 1) is guarded by the existing ids.size() < 2 return, every downstream check moves with it (provider prefilled N of head.size(), restore holds N of head.size()), and the provider/handoff arms are consistent with each other. Leaving the last prompt token to the decoder is a legitimate single decode step and does buy something on its own through the ordinary prompt-cache prefix search.
What is not there
The request now carries leave_one_token on both /slots/0?action=restore and /slots/0?action=adopt. Engine's third_party/llama.cpp is at d8bd0ff6052a3cc15b5f83d8d0625aac5b644404, and that is also the fork's current tip, and nothing at that commit reads the field:
$ git grep -c leave_one_token d8bd0ff6 -- . # no match anywhere
$ handle_slots_restore(...) -> request_data.at("filename") // only filename
$ handle_slots_adopt(...) -> body.at("tokens"), body.at("share_id") // only those two
So at the pin the field is silently ignored, and docs/hybrid.md's new sentence — "The request carries leave_one_token so the backend records the handoff as an in-place context checkpoint … which is what lets the prompt-cache search find the prefix instead of re-processing it" — states something the shipped backend does not do. That half is fork #189 ("server: a KV handoff records its cells as an in-place checkpoint … (engine#941)"), which is open and unmerged.
What to change
One of:
- land #967 with fork #189 plus the pin bump, so the doc sentence is true when it publishes; or
- keep #967 as the engine half and word the doc conditionally ("once the pin carries it, the backend records …"), naming #189 and the pin.
Nothing to change in the code. tests/hybrid_route.sh and tests/hybrid_kvshare_route.sh assert the request shape, so they pass either way and do not distinguish the two cases — worth knowing before anyone reads them as evidence that the checkpoint path runs.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
Follow-up on my CHANGES, because the dependency is now concrete and it makes the verdict easier to act on:
Fork #189 is the other half and the key matches exactly. At a9122817b (+44/-0) it reads
server-context.cpp:5416 task.slot_action.leave_one_token = request_data.value("leave_one_token", false); // restore
server-context.cpp:5626 task.slot_action.leave_one_token = body.value("leave_one_token", false); // adopt
server-task.h:177 bool leave_one_token = false;
so it is the same leave_one_token this PR sends, on both endpoints, defaulting to false. Two consequences:
- The engine half is safe to land before the pin moves. An older server ignores the unknown key, which is exactly today's behaviour, and
git grepat the pin (d8bd0ff6) confirms nothing there reads it. So this PR cannot make a shipped configuration worse; thehead-minus-one-token change is the part that takes effect now, through the ordinary prompt-cache prefix search. - The only thing left is the doc sentence.
docs/hybrid.md's new paragraph says "the request carriesleave_one_tokenso the backend records the handoff as an in-place context checkpoint … which is what lets the prompt-cache search find the prefix instead of re-processing it". That becomes true when #189 is in the pin and not before. Word it as the pinned behaviour and name #189 and the pin, and my verdict flips to READY on that push — the code needs nothing.
… fork#189 adds (review on #967) pi-prs: the docs described behaviour the pin does not have. At d8bd0ff60 nothing reads leave_one_token - handle_slots_restore takes only filename, handle_slots_adopt only tokens and share_id - so the field is silently ignored there and the one-token saving comes from the ordinary prompt-cache prefix search. The in-place checkpoint (and reuse across later requests) is fork 1bit-MONSTER/llama.cpp#189, which the pin does not carry. The paragraph now says that in those words. The two route tests are also labelled for what they are: they pin the request shape, not the effect, so nobody reads them as evidence that the checkpoint path runs at the pin.
|
Docs fixed at What changed:
Code untouched, as you said. I am also measuring the arm your follow-up assumes, because my gate ran with both halves and I should not claim its result for the engine half alone: engine |
|
Persistent review updated to latest commit 2e1dc4c |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at 2e1dc4c72. My CHANGES was one thing — the doc described behaviour the pin does not have — and this commit says it in the words that are true.
The new paragraph now names the pin explicitly: "At the current pin (d8bd0ff60) nothing reads the field, it is silently ignored, and the one-token saving above comes from the ordinary prefix search instead - the field is forward-looking, not a claim about today's backend", with the checkpoint half attributed to fork 1bit-MONSTER/llama.cpp#189 ("not yet in the pinned third_party/llama.cpp"). Step 3 no longer claims the backend records a checkpoint; step 4's one-token decode is stated as what it is — one legitimate decode step from state already in place, which is what makes the handoff worth anything on a model whose memory cannot be trimmed.
The second half is the one I would not have thought to ask for and is the right instinct: the two route tests are now labelled as pinning the request shape, not the effect, so nobody reads a green hybrid_kvshare_route.sh as evidence the checkpoint path runs at the pin. That closes the trap I flagged at the end of my last review.
Code half unchanged and still correct: the head-minus-one-token vector is guarded by the existing ids.size() < 2 return, and every downstream size check moves with it. Landing this before the pin moves is safe — an older server ignores the key, and #189 reads it with false as the default on both endpoints (server-context.cpp:5416 restore, :5626 adopt), which I verified at a9122817b.
|
The arm your follow-up assumed is now measured, and it changes the PR body — not the code. Engine
Zero Body updated accordingly: the "the two PRs are a pair / the engine half alone is a no-op" line is gone (it was wrong, and the pin arm is why), the measurement section now carries both arms labelled by server build, and the
|
|
Persistent review updated to latest commit 93560d5 |
Root cause
--device hybridhanded the prefill provider the whole prompt and then restored/adopted that state into thedecoder. A hybrid model's recurrent memory keeps its prefix in a single cell, so the backend cannot trim it back by
the one position its cache path insists on ([TAG_PROMPT_LOGITS]): with nothing legal to evaluate it fell back to
re-processing the prompt (engine#941: 287 / 847 / 3 286 prompt tokens re-evaluated for prompts of those lengths), and
the handoff bought nothing end to end on the 27B while still costing 25-57 ms of restore.
The fix (design A from #941, chosen by the orchestrator over B)
The provider is handed the prompt minus its last token, and both handoff requests carry
"leave_one_token": true. The decoder evaluates exactly that token from state that is genuinely in place — onelegitimate decode step, no rollback, and the only shape that also works for a sliding-window model.
The saving needs no change on the backend side: a handoff that is one token short of the prompt is found by the
ordinary prompt-cache prefix search, so
n_pastlands on it and one token is evaluated. That is measured at the pinbelow, not inferred.
Prompts shorter than 2 tokens have nothing to hand over and keep the old fallback (the guard stays at 2: raising it to
3 stole the 2-token case from the provider and
tests/hybrid_route.shcaught it).Measured (strixhalo, 2026-11-04,
gpu-hold -l zerocopy, ctx 20480,-fa on)~/wt/zc-probes/t1_handoff_941.py: one engine, P → Q (a prompt that diverges after the shared prefix) → P again,plus fresh-server and plain
--device hrxreferences. Instrument: the server'sprompt eval time = X ms / N tokensline and the reply's
timings.prompt_n, never the wall clock.Arm 1 — engine
2e1dc4c7againstllama-serverbuilt at pind8bd0ff60(04:30:57-04:33:51; the runninglibllama-server-impl.socontains 0 occurrences ofleave_one_token, i.e. the pin genuinely ignores the field):prompt_n--device hrx--device hrx--device hrxP / QArm 2 — the same with fork #189 in the server (
a9122817b, 02:12-02:17): 1 / 1 / 1 tokens, 117 / 196 / 226 ms,restore 33 / 57 / 43 ms, answers identical. Indistinguishable from arm 1: on this path the backend-side field
changes nothing measurable.
Both arms:
need to evaluate at least 1 tokenlines 0, gfxhub faults added 0.Before (the recorded filing measurement, same instrument, same model): the whole prompt on every arm —
287 / 847 / 3 286 tokens = 1.5 / 4.5 / 17.3 s of decoder work at the 190 tok/s measured on that route.
Shared route, Qwen3-4B (
?action=share/adopt, with #189):prompt_n1 on all three requests, answers identicalto fresh and to
--device hrx, share 0.6-1.6 ms, restore 0 ms.CPU, real llama-server (
tests/hybrid_e2e.sh, Qwen3-0.6B): hybrid reply identical to--device cpu,prompt_n1 vs 22. Both route tests pin the request shape (the provider gets 29 of 30 tokens; adopt carriesleave_one_token) and are labelled as such — they are not evidence that a backend-side checkpoint runs.Scope, and the one thing this PR does not need
Fork 1bit-MONSTER/llama.cpp#189 adds a backend-side in-place checkpoint behind the same field. It was written as
this PR's other half; arm 1 above is why it is not one: #941 is fixed at the pin without it, and I have no
measurement in which #189 changes an outcome. It stays open as an optional follow-up (reuse of a handoff across later
requests where the prefix search does not apply), not as a dependency. The
third_partypointer does not movewith this PR.
Not re-run: nothing. The before numbers are quoted as the recorded measurement from the issue, and the after is
measured twice, at the pin and with #189.
closes #941