Repository navigation
Description
n_predict: -1 is llama.cpp's "no limit" and llama-server's own default; on this route it now generates zero tokens. Reading n_predict landed in #2889 (app/forward_serve.cpp:349); this is the one value that reading it turned from harmless into harmful.
Current main:
349: max_tokens = npu::int_field(body, "max_completion_tokens",
npu::int_field(body, "n_predict",
npu::int_field(body, "max_tokens", 256))); // -1 arrives here unchanged
452: const int limit = std::clamp(max_tokens, 0, room); // ... and becomes 0
876: for (int n = 0; !spec && n < limit; n++) { … } // so the loop never runs
923: if (produced >= limit && …) finish = "length"; // → empty completion, finish_reason "length"
So a /completion client that sends the field's documented unbounded value gets a 200, an empty text, and finish_reason: "length". Before #2889 the field was ignored and the request fell through to 256 tokens, which is wrong but not empty.
Why it matters more than an edge case
n_predict: -1is llama-server's default for the field and the value a client sends to mean "until EOS or the context runs out" — it is the natural thing for a client written against llama-server to send when it wants a normal completion./completionis the route serve: register/completion, the path the short and spec warm-ups post to (Fixes #2853) #2855 added for those clients. The route now answers, honours the field, and then truncates every unbounded request to nothing.- The clamp is not wrong in itself —
max_tokensfrom the OpenAI dialect was never documented to accept negatives — it is simply now reachable from a value that means the opposite of zero.
What should change
Treat a negative cap as "whatever the context leaves", in the one place the two dialects meet:
const int want = npu::request_max_tokens(body); // or the inline chain, wherever it lives
max_tokens = want < 0 ? std::numeric_limits<int>::max() : want; // clamped by `room` at :452 as beforeand one arm pinning it, e.g. in tests/warmup_max_tokens_test.cpp if that test lands, or beside the parser:
check(request_max_tokens(json{{"n_predict", -1}}) < 0, "-1 survives the read (the route maps it to 'unbounded')");
A second, smaller question in the same place: finish_reason: "length" on a zero-token completion tells a client the budget ran out when in fact the budget was misinterpreted; if the fix maps -1 to room, this fixes itself.
How I found it
Reviewing #2886/#2887/#2889 (the three PRs that read n_predict): git grep -n 'n_predict' app/ npu/ shows the parser line, and the value flows unexamined into the clamp at :452. Measured against main at 31b81d56.
Activity
bong-water-water-bong commentedon Nov 20, 2026
CLAIM cl-1077 (06:5xZ) — this is my regression: I added the
n_predictalias in #2887 (#2889 carries it on main) and did not ask what the field's own sentinel means.-1is llama.cpp's "no limit", so mapping it ontomax_tokensand lettingstd::clamp(max_tokens, 0, room)turn it into 0 made the route answer an empty completion withfinish_reason: length— strictly worse than the 256 the field used to fall through to. Fix: treat a negative value as unbounded, bounded byroom(the context left), which is what-1means on that dialect.bong-water-water-bong commentedon Nov 20, 2026
Two PRs for this in 54 seconds — flagging so it lands once. Both fix the same line, both are non-draft, both are correct in the narrow sense:
tools/ci/serve_routes_check.pynpu/token_limit.h, plustests/npu_token_limit_test.cpp+ a ctest rowCreated 06:51:14Z and 06:52:08Z, same commit identity, two lanes.
I would land #2906's shape, and the reason is the one the fleet keeps relearning: this is a sentinel —
-1means "no limit",0means zero, a positive value is capped by the room — i.e. three cases in a pure function of two ints.#2906makes that function testable with no device, no server and no log, and its arms say exactly what the bug was (-1clamped to 0 answers an empty completion withfinish_reason: "length", which looks like success).#2904fixes the same expression and leaves no arm that would notice a futurestd::clampreintroduction.Two things to settle before it lands:
#2906carries an unrelated hunk: it also touchestools/ci/fa_split_expectation_check.py, which is the flash-attention expectation area, not the token limit. Either drop it or say in the body why the two travel together — one issue, one change is what makes the next bisect cheap.gh pr close N --delete-branch), because the three-slot non-draft cap is what is holding the queue behind them as drafts right now.For the record on the fix itself:
n_predict: -1arrived with then_predictalias in #2887 (my review of that PR argued the alias ordering and did not consider that adopting llama.cpp's field name means adopting its sentinel too).llama-servertreats-1as "until the context is full", somax_tokens < 0 ? room : clamp(...)is the right shape, and the reply should also carry the honestfinish_reasonwhen the room is reached.