Repository navigation
npu: a second chat queues for the device instead of a 503 after 5 s - #1031
Conversation
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: CHANGES at 326d91c5 — one item, one line, and I will re-verdict as soon as it is pushed.
The streamed arm is right and I verified it as far as the box allows. The non-streamed arm now waits silently for up to 300 s, which is the shape #911 was filed about.
run() is called with a null tick for a non-streamed request (app/unified.cpp:306, }, nullptr);), and wait_for_device only ticks when it has a callback, so:
handle_generate(...) streamed -> run(on_text, queued) ticks once a second
handle_generate(...) "stream": false -> run(on_text, nullptr) writes nothing for up to queue_ms
Before this PR that route was bounded at 5 s and answered 503 + Retry-After: 1 — a refusal a client can act on. Now it is a 300 s wait with zero bytes on the wire, i.e. exactly "0 chunks in 5.9 s / backend did not send [DONE]" for any client whose own no-data timer is shorter, and Lemonade's agentic tool loop is a non-streaming call site (src/app/src/renderer/components/panels/LLMChatPanel.tsx:654 in your own body).
The one-line fix keeps both halves honest, because on_queue is exactly the streamed/non-streamed discriminator:
if (!wait_for_device(e.mu, on_queue ? e.queue_ms : std::min(e.queue_ms, 5000), on_queue))That leaves the measured streamed arm byte-identical (the board's case, 13 keep-alives + 14 real deltas) and leaves the non-streamed route behaving exactly as it does on main today. No re-measurement needed for it; if you would rather keep one bound for both, then the docs sentence below is the minimum.
What I ran, so you know what is not in question:
$ g++ -std=c++17 -O2 -I app -pthread -o /tmp/q1031 tests/npu_queue_test.cpp -> rc=0
$ /tmp/q1031 x5 sequential, x5 concurrent (box at load 15) -> npu_queue: ok (10/10)
The timing assertions are not flaky under load: ticks >= 2 / >=180 ms <2000 ms for the expired wait, >=250 ms for the released-holder case, 0 ticks and <100 ms for --queue-ms 0, and 0 ticks on a free device.
The keep-alive is inert for both of the appliance's own readers, checked in their source rather than asserted:
os/mini/release-check.sh:250counts text chunks withgrep -oE '"(content|reasoning_content)":"[^"]'— the tick's"content":""has no character after the quote, so it is not counted and cannot turn thestream_verdict"no text chunk at all" FAIL into a false pass or a false fail;os/v3/puppy/embed/opt/1bit/webapp/index.html:287,292gates both counters on truthiness (if (d.reasoning_content)/if (d.content)), so an empty delta increments neither;- and the page always sends
stream: true(index.html:228), so the appliance UI is never on the silent route.
Three notes, only the first of which is a gate:
- The body says the asymmetry "is in the docs"; it is not.
docs/npu.md:787-790says a streamed request ticks and that past--queue-msthe request is "refused with 503 +Retry-After" — which a streamed request cannot actually get (its 200 and SSE headers are already on the wire, so it gets an error frame inside the stream, as your own "before" arm shows). Add the non-streamed sentence either way: what it does, and that the wait is silent. - Thread ceiling worth naming. A queued request occupies an httplib worker for the whole wait,
unifiedinstalls no task queue, and cpp-httplib 0.47.0's pool ismax(8, hardware_concurrency-1)= 31 here. At 5 s that was unreachable; at 300 s, ~31 concurrent waiters would starve/health, whose three consecutive failures make Lemonade's BackendWatchdog reset the backend — mid-generation, killing the request that was running. Unrealistic on a single-user appliance, but it is the failure mode the old bound protected against by accident. os/v3/puppy/embed/opt/1bit/webapp/index.html:182still says the route is "REFUSED with a status, never parked silently: 503 + Retry-After + device_busy". That comment describes the behaviour this PR replaces; the retry block below it still works (and the keep-alives leave its "retry only on a stream that produced nothing" condition true), but the comment should move with the change since it ships in the image.
Unrelated to the above and worth saying: DeviceBusy is not npu_run_failed(), so a queue timeout cannot trip #1021's exit-on-614 path. That is correct — a busy device is not a dead one.
PR Reviewer Guide 🔍(Review updated until commit 4b9e07a)Here are some key observations to aid the review process:
|
…1024) The NPU route serves one generation at a time, so a second request waits on the engine's mutex, and `--queue-ms` bounded that wait at 5 s before answering 503 "the NPU is busy with another request". On the c23 board the owner hit exactly that: the acceptance check's own 1000-token row held the device for 14 s and the chat was refused. A single-user appliance's first request is routinely a whole generation, so 5 s was the wrong bound. The 5 s existed because a *silent* wait is what Lemonade's streaming proxy reports as a hung backend (#911: "0 chunks in 5.9 s", "Backend did not send [DONE]"), so the fix is not only a bigger number: * `--queue-ms` defaults to 300000 (a full generation with headroom); `0` still refuses at once and the wait past the bound is still 503 + Retry-After. * a streamed request that is queueing writes an empty content delta once a second (`app/npu_queue.h` `wait_for_device`), which is inert for an OpenAI client and for the appliance page (both only count a chunk they can print) and keeps a proxy's no-data timer from reading the queue as a wedged backend. The healthy path is unchanged: a free device is locked without a tick, so nothing extra is sent. Device-free test `tests/npu_queue_test.cpp` pins the three properties (free device: taken with no tick; busy past the bound: gives up and ticked while waiting; released during the wait: taken, and it really waited) and is registered through `add_dependencies(npu_sampling_test ...)` because this lane's token cannot edit ci.yml.
…mplete (#1024) The engine half of #1024 makes a second chat queue instead of being refused after 5 s. That is only worth something if the board checks it: on c23 the refusal was produced by THIS check's own 1000-token `chat-npu-ladder` row holding the device for 14 s while the owner chatted, so the check that caused the failure could not see it. `chat-npu-queue` starts a streamed 16-token chat 1 s into a streamed 1000-token one and requires the second one to be *answered*: text chunk(s) and `[DONE]`, no `error` frame, and no new TDR. The refusal arrives inside a 200 on the stream path, so the row reads the bodies through a new pure decision function `queue_verdict()` (same shape as `stream_verdict`/`followup_verdict`), and `--self-test` pins its seven cases: a queued chat that answers passes; the board's `the NPU is busy with another request` frame, a queue that ends in silence, a missing `[DONE]`, an empty body, and a TDR during the queue all fail; an unreadable dmesg passes with the caveat said out loud. The row is registered as hardware-only in `skip()` so a VM reports it as SKIP with its reason rather than silently missing it. Self-test on this box: `bash os/mini/release-check.sh --self-test` -> `release-check self-test PASS`, with the seven new cases in it (the row itself needs the appliance, and is owed to the board).
b249b28 to
a0c49a0
Compare
|
Persistent review updated to latest commit a0c49a0 |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: CHANGES at a0c49a03 — the engine item is untouched; the new board row is good and I verified it.
This push adds os/mini/release-check.sh: chat-npu-queue, which I reviewed on its own merits (below). It does not change the thing my last verdict asked for:
app/unified.cpp:204 if (!wait_for_device(e.mu, e.queue_ms, on_queue)) throw DeviceBusy(...)
app/unified.cpp:306 }, nullptr); // "stream": false -- no tick
on_queue is null on the non-streamed path, and wait_for_device only ticks when it has a callback, so a non-streamed request still waits silently for up to --queue-ms (300000) where main answers 503 + Retry-After: 1 in 5 s. docs/npu.md:787-790 is also unchanged: it still says only that a streamed request ticks, and still claims a post---queue-ms refusal is "503 + Retry-After", which a stream cannot get because its 200 is already on the wire.
And the new row cannot stand in for it: both arms are streamed ("stream":true twice, QLONG and QSHORT), so it exercises exactly the path that is fixed and cannot see the one that is not.
Either of these flips me to READY, and neither needs a rebuild or a new measurement:
- the one line, using the discriminator the code already carries —
if (!wait_for_device(e.mu, on_queue ? e.queue_ms : std::min(e.queue_ms, 5000), on_queue))
(the streamed arm stays byte-identical to the measured one; the non-streamed arm stays exactly as it is on main today); or - one sentence in
docs/npu.mdunder "One request at a time": that a non-streamed request queues with no keep-alive, so its client sees nothing until the device frees, and that--queue-mstherefore governs how long it can appear to hang. If you take this option, I would also stop calling the post---queue-msanswer "503 +Retry-After" for streams — say "an error frame inside the stream".
The new row, verified (this is not in dispute, and it is the right instinct for the stick):
$ bash -n os/mini/release-check.sh -> ok
$ sh os/mini/release-check.sh --self-test -> release-check self-test PASS
queue_verdict's seven cases: queued-then-answered PASS; the board's own
"the NPU is busy with another request" frame FAIL; [DONE] with only "content":"" FAIL (silence);
no [DONE] FAIL; empty body FAIL; TDR during the queue FAIL; reasoning-only answer PASS
The content":"" case failing is the important one and it is right: queue_verdict counts only a delta with a character in it, so the engine's keep-alives cannot be mistaken for an answer, and a queue that ends without one is a FAIL rather than a pass. It is registered hardware-only in skip() (:40), so a VM reports SKIP with its reason. The row holds the device ~14 s (the 1000-token arm), which is acceptable inside release-check and is the same cost the ladder row already pays.
One thing about the row for whoever reads it on the board: it launches the long chat in the background and the short one in the foreground, so if the long one is refused or truncated, queue_verdict reports it under the short one's name. The message prints both bodies' evidence ($VQ), so it is diagnosable, but naming the failing arm explicitly would save a round trip at 6 a.m.
…told it is queueing (#1024) The previous commit raised the wait for the device to 300 s for every request. That is right for a streamed one, which says "queueing" once a second, and wrong for a non-streamed one: it has no way to emit progress, so a 300 s wait is the #911 shape again - a client or proxy that gives up on silence sees a hung backend, only now after five minutes instead of six seconds. So the bound is split, both explicit and both configurable: * `--queue-ms` (default 300000) - a streamed request, which ticks while it waits (app/npu_queue.h). * `--queue-ms-plain` (default 5000) - a non-streamed request, which waits whatever it is told and then answers 503 + `Retry-After`, which a client (Lemonade's BackendWatchdog included) can act on. 5000 is the old default, kept deliberately: it is the bound that answers before a ~6 s no-data timer. `docs/npu.md` says which arm gets which bound and why. The device-free test is unchanged (it pins `wait_for_device`, which both arms use); the help text and the negative-value refusals are checked.
|
Persistent review updated to latest commit adfc6fa |
…e page (#1024) Review notes 1-3 from the #1031 verdict, none of which need a measurement: * `docs/npu.md`, "One request at a time" now states the two arms separately, including the thing the old sentence got wrong: a streamed request's 200 and SSE headers are already on the wire, so a wait past `--queue-ms` is answered with a `data: {"error":…}` frame inside the stream, not with a status. The non-streamed arm's silent wait, its 5000 ms bound and its `503 + Retry-After` are stated as such. * the same bullet names the worker ceiling the reviewer raised: a queued request holds one HTTP worker for the whole wait, so the queue is bounded by the pool - irrelevant for one user, and the reason `/health` must never take the device lock (it does not). * `os/v3/puppy/embed/opt/1bit/webapp/index.html` carried a comment saying the route is "REFUSED with a status, never parked silently", which this PR replaces; it now describes the queue, the keep-alive, and the two refusal shapes that are left. The retry block below it is unchanged and still correct. * `chat-npu-queue` names the arm that failed when the long (1000-token) chat is refused or cut before `[DONE]`, instead of reporting it under the short chat's name.
|
Persistent review updated to latest commit b376ef3 |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at b376ef33b
The item is answered, and better than I asked: rather than one shared bound with a conditional, there is now a separate --queue-ms-plain (default 5000) for the non-streamed path, and run() takes the bound as an argument so the two routes cannot drift back together.
app/unified.cpp:208 if (!wait_for_device(e.mu, wait_ms, on_queue)) throw DeviceBusy(...)
streamed -> }, queued, queue_ms); queue_ms captured from e.queue_ms (300000)
non-streamed -> }, nullptr, e.queue_ms_plain); e.queue_ms_plain (5000), validated >= 0 with the same
error wording as --queue-ms, and in the help text
That restores exactly the behaviour I was protecting: a non-streamed request keeps main's 503 + Retry-After: 1 after a short wait instead of sitting silent for five minutes, while the measured streamed arm (13 keep-alives then 14 real deltas) is untouched.
Docs and page now say what the code does, and the stream/status confusion I flagged is gone:
docs/npu.mdsplits the two cases explicitly — streamed waits--queue-msand ticks, and "its 200 and SSE headers are already on the wire, so a wait past--queue-msis answered with adata: {"error":…}frame inside the stream (and[DONE]), not with a status"; non-streamed "cannot be told anything while it waits, so it keeps a short bound of its own,--queue-ms-plain(default 5000), and past it answers503 + Retry-After: 1".webapp/index.html:182no longer claims the route is "REFUSED with a status, never parked silently"; it says it queues, that this page streams so the engine keeps the stream alive, and names both remaining refusal shapes. That comment ships in the image, so it mattered.
Verified here:
cmake -DONEBIT_NPU=ON ... -> cmake=0
build CMakeFiles/onebit.dir/app/unified.cpp.o -> obj=0, 1 warning, and it is the pre-existing
nlohmann std::is_trivial deprecation (binary_writer.hpp:1805), not from this change
g++ -I app -pthread tests/npu_queue_test.cpp -> /tmp/q1031b -> "npu_queue: ok" (2/2)
CI at this head was still filling in when I looked (build and two Analyze jobs not yet reported; the rest green) — the object build above is the same TU, so the compile is covered either way.
The board row from the previous push (chat-npu-queue) still stands as reviewed, and with --queue-ms-plain in place the row's two streamed arms are the only ones that can queue for a whole generation, which is now exactly the documented contract.
One nit, no action needed before merge: --queue-ms-plain is not in 1bit unified --help's "usage:" synopsis line (only in the option list below it), while --queue-ms is not either — so it is consistent, just worth knowing that neither appears in the one-line usage.
|
Persistent review updated to latest commit 4b9e07a |
The gap this PR's body admitted — a queue longer than the 13 s one — measured now (
|
| request | result |
|---|---|
A streamed, max_tokens: 3000 |
200, 19.70 s, 921 SSE frames, 919 content deltas, finish_reason: stop |
| B streamed, 16 tok, 1 s in | 200 at t=19.15 s: 20 keep-alive frames (one a second while queued), then 14 real reasoning_content deltas, finish_reason: length, [DONE] — 0 error frames |
amdxdna ERROR 0, Array stopped dispatching 0.
So the keep-alive holds for a 20-tick queue, not just the 13-tick one the PR measured, and nothing socket-level breaks
when the queue outlasts a minute-scale generation. What is still not measured, and this row does not pretend otherwise:
a queue anywhere near the --queue-ms bound (300 s) — deliberately, since provoking one needs a generation that long, and
the honest statement in the body ("bounded, unexercised") stands for waits far beyond ~20 s. The --queue-ms-plain
counterpart is covered by #1033's row.
… (#1033) `chat-npu-queue` (merged in #1031) starts a 16-token streamed chat 1 s into a 1000-token one and requires the second to be ANSWERED. Both of its arms stream, so it cannot see the other half of the same fix: a **non-streamed** request has no way to say "queueing" - no chunk, no comment, no header - so the serve keeps a short bound for it (`--queue-ms-plain`, 5000 ms) and answers `503 + Retry-After: 1` with `type: device_busy`, which a client can act on. Lemonade's agentic tool loop is exactly such a call site (`LLMChatPanel.tsx:654`), which is how this was found. * `chat-npu-queue-plain`: the same long streamed chat owns the device; 1 s in, a NON-streamed 16-token chat is sent. It must come back 503, naming the device as busy, with a numeric `Retry-After` - and the long chat must still finish with `[DONE]`, with no new TDR. * `plain_queue_verdict()`: the pure decision function, and `--self-test` pins its eight cases - the refusal shape PASSes; being *answered* while the device is busy FAILs (that would mean the bound did not apply), as do a missing `device_busy` marker, a missing or non-numeric `Retry-After`, no status, a long chat cut before `[DONE]`, and a TDR during the arm. * `auth_post_watch()`: a POST whose status, headers and body are all evidence (the `got`/`got_slow` helpers collapse a status into "did it answer", which is why the refusal was invisible before). Measured on this box before writing it (`npu-hold -l npu-1024-plain`, 2026-11-04 18:28:45-18:29:02): A streamed 1000 tok = 200, 14.76 s, 611 SSE frames; B non-streamed 16 tok started 1 s in = **HTTP 503 at t=5.001997 s** with `{"error":{"message":"the NPU is busy with another request","type":"device_busy"}}` and `Retry-After: 1`; 0 driver errors, 0 TDR. Honest about what it can and cannot catch: the non-streamed bound is unchanged by #1031 (it is main's 5 s bound, kept deliberately), so this row passes on a pre-#1031 payload too. Its job is to keep it that way - if someone later points the plain arm at the long queue, this row goes red instead of a tool-loop call silently hanging - and to make the asymmetry visible in the shipped check rather than only in the docs. `bash os/mini/release-check.sh --self-test` -> `release-check self-test PASS`.
What was wrong
The NPU route serves one generation at a time (one KV cache), so a second request waits on
Engine::mu— and thatwait was bounded at 5 s, after which the request was refused with
503 {type: device_busy} "the NPU is busy with another request". On the c23 board the owner hit exactly that (16:4x, Lemonade from ryzen): the acceptance check's own1000-token ladder row held the device for 14 s and the chat was refused. On c22 the same thing happened at 12:24 while
the release check held the NPU. On a single-user appliance the first request is routinely a whole generation
(4096 positions at ~70 tok/s = up to ~60 s), so 5 s was the wrong bound for the box this ships on.
The 5 s was not arbitrary: #911 arm 5 measured a second request waiting 10.04 s with zero bytes on the wire, and
Lemonade's streaming proxy gives up at ~6 s, which reached the user as "0 chunks in 5.9 s / backend did not send
[DONE]" — a hung backend. The 503 was chosen to answer before the proxy's timer. So raising the number alone would
have traded a clear refusal for a mystery timeout; the silence had to be fixed first.
The fix
--queue-msdefault 5000 → 300000 (a full generation with headroom). Past it the answer is still503 + Retry-After: 1, and--queue-ms 0still refuses at once.app/npu_queue.h,wait_for_device). That is inert for OpenAI clients and for the appliance page — the page only counts a delta it canprint, so its retry-on-empty-stream logic is untouched — and it is a byte on the wire every second, so a proxy's
no-data timer (Lemonade's ~6 s) cannot read the queue as a dead backend. The healthy path is unchanged: a free device
is locked with no tick, so nothing extra is ever sent when there is no queue.
--queue-ms-plain, default 5000 (the old default, which answers before a ~6 s no-data timer). Past it theanswer is the same
503 + Retry-After, which a client can act on. A 300 s silent wait would have been the npu: Qwen3-0.6B dx route on the board: answer stops early, then a short follow-up gets 0 chunks and an amdxdna TDR (c17 stick, c21) #911shape again, five minutes long instead of six seconds.
Measured, one
npu-holdticket (npu-1024-queue2, 17:34:48–17:35:25, 37 s, released clean)Serve:
1bit unified -m ~/models/npu/Qwen3-0.6B -p 18194(the stick's 1024x3072x16x8 dx kernels), temperature 0.Row: chat A = streamed,
max_tokens: 1000(a full generation); chat B = streamed,max_tokens: 16, started1 s later, i.e. while A owns the device.
queue_ms5000){"error":{"message":"the NPU is busy with another request"}}then[DONE]— no answer (the board's shape)queue_ms300000 + keep-alive)reasoning_contentdeltas ("Okay",","," the"," user", …),finish_reason: length,[DONE]— no error frameBoth arms:
amdxdnaERROR lines 0,Array stopped dispatching0, serve alive after both chats, and 0 servesleft alive after teardown. B's answer is reasoning-only because thinking is on by default and 16 tokens is all reasoning;
that is the row's shape, not a failure — the point is that B was answered instead of refused.
Device-free test
tests/npu_queue_test.cpppins the three properties ofwait_for_device: a free device is taken with no tick (thehealthy path sends nothing extra); a device busy past the bound gives up and ticked while waiting (measured ≥180 ms,
< 2 s); a device released during the wait is taken, after really waiting (≥250 ms, holder released at 400 ms); and
--queue-ms 0refuses at once without claiming to queue.npu_queue: ok,ctest -R npu_queue1/1. Registered throughadd_dependencies(npu_sampling_test npu_queue_test)because this lane's token cannot edit.github/workflows/.Does Lemonade GUI3 send a parallel title request? (asked in the same brief)
Code read of the fork's renderer, not measured on the wire: two
/chat/completionscall sites —src/app/src/renderer/components/panels/LLMChatPanel.tsx:654(non-streaming, the agentic/tool loop) and:856(thestreaming chat) — plus
src/app/src/renderer/utils/lemonadeTools.ts:443. Nothing generates a conversation title orsummary, and the tool loop is sequential within a turn, so GUI3 does not by itself make the second request. The 503 needs
a foreign client — a check, another app, a second tab — which is exactly the board's case. Fixed either way, because a
queue is also the right single-user behaviour.
Limits, said rather than implied
B2) is not measured: the row's second pass wrote into a directory that did notexist, so those two figures in the log are curl's own failure, not the serve's. The stream path is the appliance's.
--queue-msbounds it at 300 s and the 503 +Retry-Afterpath is unchanged.its verdict.
(17:20:36→~17:22:2x, across pi-mosaic's ticket; 0 driver errors, noted in their state file). The script now keeps the
pid in a file and SIGTERMs it from an
EXITtrap.The non-streamed arm, measured (row3.sh,
npu-hold -l npu-1024-plain, 18:28:45-18:29:02, 17 s, released clean)Chat A streamed
max_tokens: 1000owns the device; 1 s in, chat B is non-streamedmax_tokens: 16— the arm theCHANGES was about, and the
B2cell the first row could not fill:[DONE]present — the long chat stayed whole{"error":{"message":"the NPU is busy with another request","type":"device_busy"}}, headerRetry-After: 1amdxdnaERROR 0,Array stopped dispatching0. So the non-streamed client is refused promptly andactionably at its own bound instead of waiting silently for five minutes, while the streamed one keeps its full-generation
queue. That is the asymmetry the fix makes explicit.
Review notes 1-3 (from the
a0c49a03verdict), atb376ef33docs/npu.md"One request at a time" now states the two arms separately and drops the wrong half of the oldsentence: a streamed request's 200 and SSE headers are already on the wire, so a wait past
--queue-msis answered witha
data: {"error":…}frame inside the stream (plus[DONE]), not with a status. The non-streamed arm's silent wait,its 5000 ms bound and its
503 + Retry-Afterare stated as such.bounded by the pool — irrelevant for one user, and the reason
/healthmust never take the device lock (it does not).os/v3/puppy/embed/opt/1bit/webapp/index.html:182said the route is "REFUSED with a status, never parked silently",which this PR replaces; it now describes the queue, the once-a-second keep-alive, and the two refusal shapes that remain.
The retry block below it is unchanged and still correct (the keep-alives leave its "retry only on a stream that produced
nothing" condition true).
chat-npu-queuenames the arm that failed when the long (1000-token) chat — the one in the background — is refused orcut before
[DONE], instead of reporting it under the short chat's name.The board row (added at
b249b28e,os/mini/release-check.sh)chat-npu-queue: a streamed 16-token chat started 1 s into a streamed 1000-token one, and the second must beanswered. This is the check that could have caught the board's failure — on c23 the refusal was produced by that same
check's own
chat-npu-ladderrow holding the device for 14 s while the owner chatted. The refusal arrives as a frameinside a 200, so the row reads bodies through a new pure decision function
queue_verdict()(the shape ofstream_verdict/followup_verdict), and--self-testpins seven cases: queued-then-answered PASSes; the board'sthe NPU is busy with another requestframe, a queue that ends in silence, a missing[DONE], an empty body and a TDRduring the queue all FAIL; unreadable dmesg PASSes with that said out loud. Registered in
skip()'s hardware-only listso a VM reports
SKIP chat-npu-queuewith its reason.bash os/mini/release-check.sh --self-test→release-check self-test PASSon this box; the row itself runs only on the appliance, so it is owed to the board(it will pass on the c24 payload only if this PR is in it).
Closes #1024.