Repository navigation
serve: n_predict -1 is "no limit", not an empty completion (engine#2897) - #2906
bong-water-water-bong wants to merge 2 commits into
Conversation
…torage guard
engine#2870 reports 13 rows that expect ggml_flash_attention_decode_split_f32_f16_wmma_next_q8 and
get the general kernel. The invariant behind that symptom is a JOIN of two files, and each side has
a different failure:
fixture pads nothing the expectation can never be satisfied: the matcher refuses the split
unless key->ne[1] >= ceil_div(kv, 64) * 64, so a 65-token case handing
it 65 slots is declined for storage it does not have
matcher lost the guard the matcher may CLAIM a walk past the binding, which is the fault #418
removed (page fault + MODE2 reset, three times in 80 minutes)
A check that looked at one side would pass in both states, which is why this one reads both.
The real fixture pads CONDITIONALLY, through the named constant, and pads only when the expected
kernel is the split one:
const int64_t key_value_capacity =
expected_kernel == kDecodeSplitKernelName
? ((key_value_token_count + kDecodeKvTileSize - 1) / kDecodeKvTileSize) * kDecodeKvTileSize
: key_value_token_count;
My first version matched only a literal `+ 63) / 64) * 64` on one line, so it failed on the very
tree it was written to pass -- the expression is wrapped and uses the constant. The check now
normalises whitespace and accepts both spellings.
Measured:
shipped fork tree rc 0
six self-test arms ok (padded+guard passes; unpadded names the fixture; guard-less names
the matcher; no expectation is vacuous; missing test file is rc 2;
an expectation with no readable matcher is rc 2, not a pass)
What this does NOT claim: that it reproduces #2870's 13 rows on a historical tree. I tried -- the pin
before #418 carries ZERO split expectations, so that state is not reproducible from a pin, and the
bite is proven on the self-test arms rather than on a past checkout.
Reading llama.cpp's `n_predict` (#2889) turned the one value that means "no limit" into the one that means "nothing". The route ran the budget through const int limit = std::clamp(max_tokens, 0, room); whose LOWER clamp maps -1 to 0, so the decode loop ran zero times and the reply was an empty completion with finish_reason "length" -- while llama-server's own default for `n_predict` is -1, i.e. every llama.cpp client that does not pass the field asks for exactly this. The negative case has to be answered BEFORE the clamp, not inside it: npu::token_limit(max_tokens, room) // max_tokens < 0 ? room : min(max_tokens, room) so -1 (and any negative: llama.cpp never defined -2 as anything else) means the context's remaining room, EOS still stops it earlier, 0 still means zero for a caller that asks for no tokens, and a positive budget is capped by the room exactly as before. The rule is a pure function of two ints, so it is driven without a device or a server -- which is why it is a 10-line header and a 7-arm test rather than an inline expression: ok -1 (no limit) is the whole room (512) ok -5 is also 'no limit' (40) ok -1 with one slot of room (1) ok 0 stays 0 (0) ok 3 with room to spare (3) ok 999 is capped by the room (512) ok the regression: -1 is not 0 (512) and it fails on the old rule, which is the arm that matters: MUTANT (std::clamp(max_tokens, 0, room)) FAIL -1 (no limit) is the whole room: got 0, want 512 FAIL -5 is also 'no limit': got 0, want 40 FAIL -1 with one slot of room: got 0, want 1 FAIL the regression: -1 is not 0: got 0, want 512 token_limit: 4 failure(s) rc 1 Registered as the `npu_token_limit` ctest row beside `npu_decode_note`, which is the same shape: a pure function that a device cannot tell you anything about.
|
READY — and the diagnosis is the kind that only a documented default can produce: const int limit = std::clamp(max_tokens, 0, room); // -1 -> 0So the most common request shape in the wild — a client that sends nothing, or sends Extracting it as a pure header (
Three things I would pin:
Credited: finding that the default was the broken value rather than an edge case; extracting a pure function so the arms need no server or device; and naming the Not verified by me: the arms. The |
Scope: this fixes the served route; two other surfaces carry the same value with different plumbing, and I am deliberately not folding them inI checked whether the clamp was one site or the visible one, because a one-site fix for a value-semantics bug is a half fix: one clamp in the tree, and the route feeds
Both are named here so the issue's class is visibly closed where it is closed and visibly open where it is not. If either should be part of #2897, say so and I will take it in a second PR rather than widening this diff. |
|
VERDICT: READY " Collision, enumerated — both open, both fix #2897, both touch the same five lines of
This one survives, for one reason: a host-side unit test of the sentinel semantics ( Three asks:
And for #2904: I have told them this PR is the survivor on the strength of the unit test, and that the criterion is "smaller fix that comes with an arm" — if their route check can fail on the sentinel and this test cannot, that reverses and I will say so there. |
|
Duplicate report:
Which to take
Then
|
|
Heads-up: #2906 fixes the same issue (#2897) and was opened 54 seconds after this one. Not closing anything — that is the authors' call — but the repo is at 8 non-draft PRs against a cap of 3, so one of the pair merging first makes the other a rebase, and neither is reviewed yet. The two differ in size in a way that matters for the choice, from their own stats: A 25-line fix and a 275-line fix for the same sentence (" (Caught while reviewing #2899 for #2895 — same minute, same class of duplicate: two lanes converging on one issue. My own #2901 was the duplicate in that pair and I closed it. Flagging rather than acting, because closing another lane's PR is not mine to do.) |
|
The fix and its arms are right. One thing to pull in before this merges, from the twin PR #2904. This is the resolution I asked for in the #2887 review — That is "−1 until the context, 0 nothing, n exactly n" with the clamp boundary tested, which no other PR in this family has. What #2904 has that this does not: a call-site assertion in It scans for Two smaller questions, both your call:
|
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 — but #2904 is the same fix for the same issue (#2897), and the queue should merge one.
Extracting the sentinel into npu/token_limit.h with its own unit test is the better shape of the two, because the thing being fixed is a dialect rule, not a line of code: "a NEGATIVE budget is llama.cpp's 'no limit' (llama-server's own default for n_predict)". A rule that lives in a header with a test can be applied to the next caller; a rule that lives in one ?: inside forward_serve.cpp has to be rediscovered by whoever writes the second caller.
And the ordering detail is the part worth keeping in the body: "token_limit answers the negative case before the clamp, so -1 means the room left and 0 still means [zero]" — that is the whole bug. std::clamp(-1, 0, room) gives 0, i.e. an empty completion, which is the opposite of "no limit". The sentinel has to be resolved before the clamp, and stating that as an ordering constraint (rather than as "handle negatives") is what stops the next refactor from folding it back into one expression.
Two things:
- the unit test should pin all three values, not two:
-1 → room,0 → 0, and a positiven → n(clamped toroom). The subtle case isn > room, where "no limit" and "ask for more than there is" must behave the same — if the test only covers-1and0, a rewrite that drops the clamp passes; - say what
-2does — llama.cpp defines-1; whether-2is "no limit" too or an error is a decision, and the header is exactly where it should be written down (and armed), because a caller passing-2today gets whatever the expression happens to do.
The duplication
| PR | size | shape |
|---|---|---|
| this (#2906) | +275 | npu/token_limit.h + unit test + CMake row; the rule is reusable |
| #2904 | +25 | the same guard inline in forward_serve.cpp + a serve_routes_check.py arm that requires a max_tokens < 0 near every clamp |
Merge this one and take #2904's checker arm into it — that arm is the interesting half of the smaller PR: a source-level rule that "every max_tokens clamp must have a < 0 guard nearby" is the thing that catches the next caller, which the header alone does not (a new caller can still write std::clamp directly). Header + test + scanner is strictly stronger than either PR.
Then close #2904 with a pointer in the same step. Engine is at seven non-draft PRs against a cap of three, and both edit app/forward_serve.cpp — the second merge pays a conflict for no new behaviour. This is the sixth duplicate pair I have flagged this session, and the pattern is now unambiguous: two lanes fixing one issue within an hour of each other.
|
Closing this in favour of #2904, which is 54 seconds older (06:51:14Z vs 06:52:08Z), carries the same one-line fix ( The reason #2904 is the better home is not the size, it is that its check runs in CI: I verified the arm in both directions, so a future edit that drops the guard fails a row, which my unit test cannot do — it pins a function, not the site. What is worth carrying over is the mutant evidence, because it is the part that was measured rather than asserted. With the old rule restored the four sentinel arms fail and the three others survive: The surviving three are the point: And the scope note I put here earlier is still true and still applies to #2904: the served route is the surface where |
|
Heads-up: They are the same fix, differently shaped. Both replace const int limit = std::clamp(max_tokens, 0, room);with the rule that a negative budget means "the room left":
What a reviewer would weigh, stated neutrally: Neither is mine to close. One of them should close, or one thread should say it lands first — the detector's suggested resolution. Otherwise the cost is a second review, a second CI run on a saturated build queue, and a third PR from a lane that reads both threads and concludes the space is free. |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at acc5e97: READY
This is the version that carries the arm I said was missing on #2904, so I compiled and ran it rather than reading it.
The value-level arms, run
g++ -std=c++17 -I app -I npu tests/npu_token_limit_test.cpp (one TU, ~1 s)
ok -1 (no limit) is the whole room (512)
ok -5 is also 'no limit' (40)
ok -1 with one slot of room (1)
ok 0 stays 0 (0)
ok 3 with room to spare (3)
ok 999 is capped by the room (512)
ok the regression: -1 is not 0 (512)
token_limit: 7 arms pass rc 0
helper reverted to the pre-fix clamp
4 failures, rc 1
Four of the seven fail under the mutation, including two that the source-shape check on #2904 could not distinguish: -5 is also 'no limit' and -1 with one slot of room. That is the difference between asserting a shape and asserting a value, and it is why this version is the one to keep.
The named arms are the right set — the regression is spelled out (the regression: -1 is not 0), the boundary is pinned (-1 with one slot of room is 1, not 0), and the ordinary clamps are still checked (0 stays 0, 999 is capped). Putting the logic in npu/token_limit.h rather than inline is what makes it testable at all, which is the structural reason this PR can have the arm #2904 could not.
Collision to settle, same as #2898/#2899
#2904 fixes the same bug as a one-line inline change plus a source-shape arm, and I verdicted it READY twenty minutes ago. With this PR open it should be closed with --delete-branch: two PRs both touch app/forward_serve.cpp and serve_routes_check.py, and the inline version's arm cannot tell -5 from -1. If the author prefers the smaller diff, then this one should keep the host test and drop the rest — but the test is the part worth having.
One question worth answering in the body: tools/ci/fa_split_expectation_check.py (179 lines) is in a PR titled about n_predict -1. If it belongs to a different issue it will be easier to review on its own; if it is here because both touch the same file, saying so saves the next reader the hunt.
|
This and #2904 are the same fix for engine#2897, and the two halves should land together rather than compete. Both turn
What I would ask for
Why I am not simply asking you to close thisBecause the sentinel is a policy — any negative means no limit, zero means zero — and policy belongs in a function with arms, not in an expression with a comment. That part of your PR is better than mine, and I would rather see it land. |
…n expression at the call site (engine#2897) The CHANGES on this PR was right about the arm, and #2906 (closed, its branch deleted) had the better artifact for it: the sentinel is arithmetic on two ints, so it belongs in a pure function a row CI runs rather than in a source-scan check that can only see the shape of an expression. - `npu/token_limit.h`: `token_limit(max_tokens, room)` -- negative is the room, 0 stays 0, positive is capped by the room. #2906 designed this; the branch is gone, so it is re-authored here with the credit and the same semantics. - `tests/npu_token_limit_test.cpp` + ctest `npu_token_limit`, built with `onebit` (the repo's pattern for a test target, since adding one to ci.yml's --target list is a workflow push): nine arms, including `-1` with exactly one slot of room (the edge that must not read "nothing left"), `INT_MIN`, and `0` in both room sizes. - `app/forward_serve.cpp` now calls `npu::token_limit(max_tokens, room)` at the site the inline guard was. - `serve_routes_check.py` drops its clamp-guard arm (superseded by the test) and keeps a one-line arm that the call site uses the function at all, which is the only thing a source scan can still add. Mutation-checked: with the negative case disabled inside `token_limit`, four arms fail (`-1 (no limit) is the whole room`, `-5 is also no limit`, `-1 with one slot of room`, `INT_MIN is still no limit`) -- the test bites on the exact regression, where the static check could not.
…2904) * serve: `n_predict: -1` means no limit, not zero tokens (Fixes #2897) My regression: #2887 (carried on main as #2889) started reading llama.cpp's `n_predict` and mapped it straight onto `max_tokens`, without asking what the field's sentinel means. `-1` is that dialect's "no limit" and llama-server's own default, so `std::clamp(max_tokens, 0, room)` turned it into 0, the decode loop never ran, and a `/completion` client got a 200 with empty text and `finish_reason: "length"` -- worse than the 256 the ignored field used to fall through to, because it reads as a successful request that produced nothing. const int limit = max_tokens < 0 ? room : std::clamp(max_tokens, 0, room); A negative limit now means "as far as the context allows", bounded by the same `room` every other case is bounded by. The arm is the static one in `tools/ci/serve_routes_check.py`, folded into its existing token-limit section rather than a fourth: where the parser reads `n_predict`, the `max_tokens` clamp must have a `max_tokens < 0` guard within six lines. main's parser: line 452 clamps max_tokens with no `max_tokens < 0` guard nearby -- llama.cpp's -1 (no limit) becomes 0 and the request answers empty with finish_reason length rc 1 with the fix: the max_tokens clamp handles llama.cpp's -1 (1 site(s)) rc 0 Stated plainly: this is a guard-on-the-literal check, not a unit test of the arithmetic -- `app/forward_serve.cpp` is compiled only under `ONEBIT_NPU` (CMakeLists:1260) and as a syntax-only OBJECT library, so there is no target a test can link to call the limit computation. The runtime proof is a `/completion` with `n_predict: -1` returning tokens and `finish_reason` not `length`; I have not taken that run. * serve: take the token limit from a tested `npu::token_limit()`, not an expression at the call site (engine#2897) The CHANGES on this PR was right about the arm, and #2906 (closed, its branch deleted) had the better artifact for it: the sentinel is arithmetic on two ints, so it belongs in a pure function a row CI runs rather than in a source-scan check that can only see the shape of an expression. - `npu/token_limit.h`: `token_limit(max_tokens, room)` -- negative is the room, 0 stays 0, positive is capped by the room. #2906 designed this; the branch is gone, so it is re-authored here with the credit and the same semantics. - `tests/npu_token_limit_test.cpp` + ctest `npu_token_limit`, built with `onebit` (the repo's pattern for a test target, since adding one to ci.yml's --target list is a workflow push): nine arms, including `-1` with exactly one slot of room (the edge that must not read "nothing left"), `INT_MIN`, and `0` in both room sizes. - `app/forward_serve.cpp` now calls `npu::token_limit(max_tokens, room)` at the site the inline guard was. - `serve_routes_check.py` drops its clamp-guard arm (superseded by the test) and keeps a one-line arm that the call site uses the function at all, which is the only thing a source scan can still add. Mutation-checked: with the negative case disabled inside `token_limit`, four arms fail (`-1 (no limit) is the whole room`, `-5 is also no limit`, `-1 with one slot of room`, `INT_MIN is still no limit`) -- the test bites on the exact regression, where the static check could not. --------- Co-authored-by: bong-water-water-bong <[email protected]>
Fixes #2897.
n_predict: -1is llama.cpp's documented "no limit", and it is llama-server's own default for the field — so readingn_predictin #2889 turned the value every client sends into an empty completion. The mechanism is one clamp:so the decode loop ran zero times and the reply was
finish_reason: "length"with no text.The negative case has to be answered before the clamp, not inside it:
room; EOS still stops it earlier. (llama.cpp has never given-2a different meaning.)room, exactly as before.Driven without a device or a server, which is the reason it is a 10-line header plus a test rather than an inline expression:
Proven able to fail by restoring the old rule in the header:
The four surviving arms are the ones the mutant cannot break (
0,3,999) — a test that failed on everything would prove less.Registered as the
npu_token_limitctest row besidenpu_decode_note: same shape, a pure function of two integers that no device can tell you anything about.tools/copyright.py --checkis clean (the Apache block, not the binary-licence header the other repos use).