Repository navigation
serve: read n_predict, so the warm-ups generate 0 and 2 tokens as their comments say (engine#2884) - #2886
serve: read n_predict, so the warm-ups generate 0 and 2 tokens as their comments say (engine#2884)#2886bong-water-water-bong wants to merge 1 commit into
Conversation
…ir comments say (engine#2884)
The two /completion warm-ups send llama.cpp's field name and the route read only the OpenAI pair:
serve.cpp warmup_short_body: {"prompt": …, "n_predict": 0, …}
warmup_spec_body: {"n_predict": 2}
forward_serve max_tokens = int_field(body, "max_completion_tokens", int_field(body, "max_tokens", 256))
Nothing in app/ or npu/ read `n_predict`, and options_from_body ignores an unknown key by design, so both
requests ran at max_tokens = 256. That is the whole reason those two requests exist undone: the short one
moves HRX's small-batch matmul build (2.1 s on 4 cores) to load time, and the spec one moves the Q6_K ffn
layout build (76 host copies, ~5.6 GB, 10.4 s) off the first client request that ends on a one-token step.
A 256-token decode is not that step.
`/completion` is llama.cpp's own endpoint, so `n_predict` is the name its clients send; reading it is the
coherent fix (the alternative in the issue, rewriting our bodies to speak OpenAI, leaves every external
client of /completion with the same silent no-op). Precedence is explicit and lives in one place,
npu::request_max_tokens: max_completion_tokens -> n_predict -> max_tokens -> 256, the most specific field
winning, with the type safety of int_field kept so a wrong type is still the route's 400 (engine#1106).
The test is on the bodies, not on a copy of them, because the failure was invisible from outside: a body
whose every key the route ignores passes a path check exactly as a correct one does (#2883's checker covers
paths). So the three builders moved to app/warmup_bodies.h -- unchanged apart from their namespace and the
`inline` they need there -- and tests/warmup_max_tokens_test.cpp reads the cap out of the same object the
server posts, through the same function the route parses it with:
ok the short warm-up asks for 0 tokens
ok the spec warm-up asks for 2
ok the sampling chat warm-up keeps its own 8-token cap
ok n_predict is read / max_tokens is still read / max_completion_tokens is still read
ok when both are present the llama.cpp field wins
ok and the most specific field wins over both
ok no cap field keeps the route's default / an explicit null is still absent
ok a fractional cap truncates, as it did before
ok a non-numeric n_predict throws (the handler turns it into a 400)
Not vacuous: with the old single-field parser restored, 6 of those 12 arms fail, including both that name
the bug ("the short warm-up asks for 0 tokens", "the spec warm-up asks for 2").
Wired as add_dependencies(onebit warmup_max_tokens_test) so the row is built by a target ci.yml builds
(ci_targets_check, engine#1595) -- no workflow edit. pre-pr 13 green, 2 skipped; app/forward_serve.cpp
syntax-checks clean (ONEBIT_NPU_RUNTIME_OWN, public headers only).
PR Reviewer Guide 🔍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: READY
This is the second half of #2854, and it is the half that would have survived the first fix: registering /completion makes the warm-ups reach the server, but n_predict was read nowhere — options_from_body ignores unknown keys by design — so both warm-ups generated up to 256 tokens instead of 0 and 2, i.e. the entire job of those requests left undone while returning 200.
A 200 that does the wrong amount of work is worse than a 404, and that is the sentence this PR is really about: #2854's failure at least left no trace, whereas this one warms the wrong shape — 256 tokens rather than the 0-token and 2-token shapes the docs promise — so the first real request still pays for the per-shape program it was supposed to find pre-built. The two bugs composed: route missing → nothing warmed; route added → the wrong thing warmed, silently.
"Read n_predict in one place" is the right shape for the fix, and the reasoning is the interesting part: /completion is llama.cpp's own endpoint, so n_predict is the name its clients send — accepting it is not a compatibility shim, it is reading the field the route's own API defines. options_from_body ignoring unknown keys is a good default that becomes a liability exactly when the server's documented body and its parser disagree.
Two things:
- precedence must be stated and armed: with both
max_tokensandn_predictpresent, which wins? OpenAI's field or llama.cpp's? A one-line rule plus an arm for the conflict case is what stops this becoming ambiguous when a client sends both — and the warm-up path is exactly where such a pair would appear; - the arm should assert the token count, not the
200: "the short warm-up generated 0 tokens and the spec warm-up 2" is the property; a test that posts the body and checks the status would have passed before this fix, which is precisely how the bug shipped. If the warm-up logs its generated count anywhere, say so — that log line is the only evidence a reader will have that the shape was really warmed.
Not blocking: one field, one place, and the failure mode named as "generates the wrong amount of work while succeeding".
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 16d992d: CHANGES
The fix is right and I ran its test — but reading n_predict makes one documented llama.cpp value catastrophic: n_predict: -1 now yields zero tokens.
The good part, measured (compiled and ran the new test against the tree's own json headers):
$ g++-16 -std=c++20 -I app -I …/json-src/include -o /tmp/wmt tests/warmup_max_tokens_test.cpp && /tmp/wmt
ok the short warm-up asks for 0 tokens: its prompt is the work, and a 256-token decode is the bug (#2884)
ok the spec warm-up asks for 2: the second token is the step that builds the one-token decode layout
ok the sampling chat warm-up keeps its own 8-token cap (the OpenAI field name)
ok n_predict is read ok max_tokens is still read ok max_completion_tokens is still read
ok when both are present the llama.cpp field wins ok the most specific field wins over both
ok no cap field keeps the route's default ok an explicit null is still absent
ok a fractional cap truncates ok a non-numeric n_predict throws (400)
all cases ok
And the design call that makes it verifiable is the one I asked for: the bodies moved to app/warmup_bodies.h and the test reads the same object the server posts, through the same function the route parses it with — a body whose keys nothing reads is byte-for-byte identical to a correct one from outside, which is exactly how this survived #2855 and the path check.
The one reason to hold it
n_predict = -1 is llama.cpp's documented "no limit" for /completion, and llama-server's own default. Here:
$ /tmp/neg # npu::request_max_tokens({{"n_predict", -1}})
n_predict:-1 -> max_tokens=-1
app/forward_serve.cpp:441 const int limit = std::clamp(max_tokens, 0, room); -> 0
So a client of the route this PR is making work, sending the field's unbounded value, gets 0 tokens — an empty completion. Before this PR it at least fell through to the 256 default and produced something; after it, the more faithful reading turns that client's request into nothing. The clamp is not wrong in itself (max_tokens was never documented to accept negatives), it is just now reachable from a value that means the opposite.
Two lines and one arm, either way:
// -1 is llama.cpp's "no limit": cap at the room the context leaves, not at zero.
const int want = request_max_tokens(body);
max_tokens = want < 0 ? std::numeric_limits<int>::max() : want; // or: want < 0 ? room : wantcheck(npu::request_max_tokens(json{ { "n_predict", -1 } }) < 0, "-1 survives the parser read (the route maps it to 'unbounded')");whichever spelling, the property to pin is "a negative cap means unlimited, not zero".
Everything else is right
- Precedence is explicit and argued:
max_completion_tokens→n_predict→max_tokens→ 256, with the llama.cpp name beatingmax_tokensbecause/completion's clients set it — that is a decision, not an accident, and the arms pin both directions. - The warm-up bodies keep their own dialects honestly: the chat body keeps
max_tokens: 8(it posts to/v1/chat/completions), the two/completionbodies keepn_predict— so the test proves each body asks for its intended cap through the route that will read it. int_fieldsemantics preserved: null → absent, fractional → truncates, non-numeric → throws so the handler answers 400 rather than 500 (engine#1106) — and the new arms assert all three rather than trusting them.app/serve.cpploses 49 lines to the header, so the bodies have one definition and the test cannot drift from the server.
|
VERDICT at The finding is right and the arms are the best I have seen on this tool family
The test asserts on the real body builders, and the rest of the arms are exactly the right ones: Two of those are subtle and both are right: an explicit
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 16d992d: READY
I compiled the host test by hand and ran it both ways, because a regression test for a parser is worth exactly as much as its behaviour against the old parser.
The fix and its counter-proof
g++ -std=c++17 -I app tests/warmup_max_tokens_test.cpp (with the PR)
all cases ok rc 0
same binary, helper reverted to the pre-fix parse (n_predict ignored)
FAIL the short warm-up asks for 0 tokens: its prompt is the work,
and a 256-token decode is the bug (#2884)
FAIL the spec warm-up asks for 2: the second token is the step that builds the one-token decode layout
FAIL n_predict is read
FAIL when both are present the llama.cpp field wins: it is the one this endpoint's clients set
FAIL a fractional cap truncates, as it did before rc 1
Five named failures, and the first two are the bug itself rather than a complaint about a field name — that is the difference between a test that locks the convention and a test that locks the consequence.
What the case list settles
Beyond the three bodies (0 / 2 / 8), it pins the precedence explicitly: n_predict beats max_tokens when both are present, max_completion_tokens beats n_predict, {} keeps 256, an explicit null counts as absent, 2.9 truncates, and a non-numeric n_predict throws. The middle two are the ones a future refactor would otherwise "tidy" into a different answer without noticing.
checks at this head: CodeQL pass, hrx_build pass, pr_agent pass, reviewer pass, hw_gate skipping.
Not verified: the full ctest registration (add_dependencies(onebit warmup_max_tokens_test) puts the build order in the tree; I built the single translation unit by hand instead, since disk is under this lane's floor for a tree).
|
Closing this as superseded by #2887 — opened six minutes after it, on the same issue, and the one that should live. Why the other one wins, from comparing the two heads rather than the titles:
What I would carry over, and why it is not in either PR: a compiled arm can prove two things a source check cannot — the precedence when a client sends both They are worth adding to |
Closing this lost one arm that the merged check does not carry, and it is the one that is easiest to re-break
What it does not cover is what this PR's host test uniquely covered: precedence, null, and truncation.
int_field(body, "max_tokens", int_field(body, "n_predict", 256))every field is still read, the check still passes — and a request that sets both Two ways to keep it, whichever the author prefers:
Not a re-open request: the bug is fixed on main and #2887's guard is real. This is about the arm that would catch the next edit to the same expression, which nothing currently watches. |
Claiming engine#2884. The two
/completionwarm-ups send llama.cpp's field name; the route read only the OpenAI pair, so both generated up to 256 tokens instead of 0 and 2 — which is the entire job of those requests left undone.n_predictwas read nowhere, andoptions_from_bodyignores unknown keys by design, so the short warm-up never took HRX's small-batch matmul build (2.1 s on 4 cores) and the spec warm-up never took the Q6_K ffn layout build (76 host copies, ~5.6 GB, 10.4 s) — the first real client request paid it instead.The fix: read
n_predict, in one place/completionis llama.cpp's own endpoint, son_predictis the name its clients send; that is the issue's coherent option (rewriting our bodies to speak OpenAI leaves every external client of/completionwith the same silent no-op). The precedence is explicit, innpu::request_max_tokens:the most specific field winning, and the type safety of
int_fieldkept, so a wrong type is still the route's 400 and not ajson::type_errorescaping as a 500 (engine#1106).The test asserts the body, not a copy of it
The failure was invisible from outside — a body whose every key the route ignores passes a path check exactly as a correct one does. So the three builders moved to
app/warmup_bodies.h(unchanged apart from namespace +inline) andtests/warmup_max_tokens_test.cppreads the cap out of the same object the server posts, through the same function the route parses it with:Not vacuous: with the old single-field parser restored, 6 of those 12 arms fail, including both that name the bug.
Wired as
add_dependencies(onebit warmup_max_tokens_test)so the row is built by a targetci.ymlbuilds (ci_targets_check, engine#1595) — no workflow edit needed.pre-pr-checks13 green / 2 skipped;app/forward_serve.cppsyntax-checks clean underONEBIT_NPU_RUNTIME_OWN.What this does not do
It does not measure the 10.4 s on a device. The change makes the warm-up request mean what its comment says; the measurement needs a server load on a goal model, and I would rather it be a separate device run than a claim in this description.