Repository navigation
npu: the /v1/systemone decision route on the d3 contract (#653) - #775
Conversation
The route half of #653, on npu/systemone.h's contract (#765) and npu/decision.h's readout (#763): `POST /v1/systemone` (alias `/v1/decisions`) answers `choice`/`noul`/ `score` questions about a `state`, and `GET /v1/systemone/models` lists the decision model. When `decision_config.json` and `decision.q4nx` sit next to `model.q4nx` the decision routes come up beside the chat ones; a pack without them is a chat model. A decision is one backbone prefill per question, the pooled last hidden state, the pack's readout and a temperature softmax over that question's options -- no sampling, no grammar, no stream. To pool it, Q4nxNpuForward gains last_hidden(): the post-final-norm vector both step() and step_batch() already compute for the LM head. The envelope is copied from the d3 server vLLM Semantic Router already speaks (`vllm-sr/d3-lite`'s d3_server.py at 2fc06364), not invented -- `questions` is an object whose ids are the keys of `answers`, `usage` is {input_tokens, output_tokens: 0}, a malformed question gives 422 {"detail":{"invalid_questions":...}} and nothing runs, and an over-limit question is a per-question `max_length_exceeded` entry with **nothing truncated** while the other questions are still answered. That means 422 on these routes rather than the chat route's 400; app/npu_systemone.h is device-free so the envelope, the refusals and the prompt wrapper are tested with no model. The prompt is the pack's chat template with enable_thinking=False, so the assistant turn opens with an EMPTY think block: those bytes are input, and reusing chatml() would change them. Measured (strixhalo, Qwen3-0.6B with a SYNTHETIC 255x1024 readout -- the route is what is under test, the choice means nothing; host GEMM under npu-hold, 0 amdxdna errors): choice/noul/score answers, 3 questions in one request (order kept, usage 269 tokens), /v1/decisions alias, 422 unknown field / invalid_questions / images, 401 without the bearer key, and max_length_exceeded at 34086 > 32768 with the other question answered (usage 90). ctest npu_sampling, systemone_contract, systemone_route and decision_head pass; copyright rc 0. Not in it, and no row claimed: a real d3-lite backbone. Its HF Qwen3_5Model pack cannot be converted yet -- tools/safetensors_to_q4nx.py assumes 2-D tensors (the GDN conv1d is [6144,1,4]), the pack's names are HF's (language_model.*, linear_attn. conv1d/in_proj_*) not the engine's (model.*, ssm_conv1d/qkv_proj/...), and the GGUF on the Hub is arch `qwen35`, which tools/gguf_to_q4nx.py does not accept. That is the next piece, not something to fake a number for.
PR Reviewer Guide 🔍(Review updated until commit 4a70862)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
Reviewed the new surface as code and verified every envelope claim against the pack's own sources on this box, not
against the PR's description of them.
Ran:
$ g++-16 -std=c++26 -O1 -Iapp -Inpu -isystem …/json-src/include tests/systemone_route_test.cpp && ./systemone_route_test tests/golden/systemone/prompts.json
systemone_route: ok rc 0
$ g++-16 -DONEBIT_NPU_RUNTIME_OWN -std=c++26 -fsyntax-only … npu/forward/src/q4nx_forward.cpp ok
$ g++-16 -DONEBIT_NPU_RUNTIME_OWN -std=c++26 -fsyntax-only … app/forward_serve.cpp ok
$ python3 tools/copyright.py --check rc 0
(CI builds neither NPU lane, so the two hand compiles are the only compile evidence that exists for these files.)
The prompt wrapper is checked, not asserted. The test walks all 9 fixtures in tests/golden/systemone/prompts.json
(choice_two_one_null … score_json_levels) and byte-compares
<|im_start|>system\n{system}<|im_end|>\n<|im_start|>user\n{expected_user_prompt}<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n
against systemone_prompt(), then asserts the wrapper differs from chatml() — so the empty think block really is in
the bytes and the "reuse chatml()" shortcut cannot pass.
The envelope matches the pack's server (/tmp/d3meta/s2.py, whose docstring says it is d3_server.py, plus
d3_runtime.py):
POST /v1/systemonetakesquestions(object) plus optionalstate/images/videos/model; anything outside
that list or of the wrong type is422 {"detail": "<reason>"}— s2.py's own docstring says exactly this;- malformed questions →
422 {"detail": {"invalid_questions": {id: reason}}}and nothing runs (s2.py:164); usageis{"input_tokens": tokens, "output_tokens": 0}(d3_runtime.py:2048) andtokensis the sum of the
questions' prompt lengths (d3_runtime.py:2021-2023), not the state's — the PR's point, confirmed;- an over-limit question stays an entry inside
answerswith
{"type", "error": "max_length_exceeded", "message": "… over the maximum context length …; nothing was truncated"}
(d3_runtime.py:1438-1444), other questions still answered — HTTP 200, no truncation; answersis keyed by question id in request order (for key in prepared.keys, d3_runtime.py:2033);enable_thinking=False(d3_format.py:85, d3_runtime.py:1335/1347) — which is the empty think block above.
The one route this PR touches that everything else uses is preserved byte for byte. /health and /v1/health were
refactored into one health() lambda; with no decision head loaded it emits {"status":"ok"} (and {"status":"loading"}
with 503 before ready) exactly as the deleted inline handlers did, and the d3 fields (model, max_input_tokens,
modalities, authentication) are added only under if (e.head).
Not verified: the device rows (choice 0.685 / noul 0.046 / score 0.0724, input_tokens 97 and 269, the 401 with
DECISION_API_KEY, the 34086 > 32768 refusal) and the synthetic 255×1024 head they were taken with. Those need
forward-serve built with the NPU lane plus a decision pack, under npu-hold; they are the lane's numbers, and the
routes are what I checked.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
Confirmation at 4a708620 after update-branch (the branch now carries #768's squash 7f4e98a as a main merge; the
author commit 8a9afdc is unchanged, so the content is byte-identical to what I reviewed at 8a9afdce). Build green.
The evidence stands: systemone_route_test green over all 9 fixtures with the wrapper byte-compared including the empty
think block, both changed NPU-lane TUs compiled by hand, the envelope verified against the pack's s2.py/d3_runtime.py,
and the shared /health route preserved byte for byte. Device rows remain the lane's. tprs-ready stays.
|
Persistent review updated to latest commit 4a70862 |
What
The route half of #653:
POST /v1/systemone(aliasPOST /v1/decisions) answerschoice/noul/scorequestions about astate, andGET /v1/systemone/modelslists the decision model. Whendecision_config.jsonanddecision.q4nxsit next tomodel.q4nx, the decision routes come up beside thechat ones; a pack without them is a chat model, which is the normal case.
A decision is one backbone prefill per question, the pooled last hidden state, the pack's readout
(
npu/decision.h, #763) and a temperature softmax over that question's options. No sampling, no grammar,no stream. To pool it,
Q4nxNpuForwardgainslast_hidden()— the post-final-norm vector that bothstep()andstep_batch()already compute for the LM head, so nothing new is calculated.It stands on
npu/systemone.handnpu/decision.h, both onmainnow (#7651d42bad, #763dcf4751).The envelope is copied, not guessed
npu/systemone.hreproduces the pack'sd3_format.py; the HTTP envelope had to come from the pack'sserver, so I fetched
vllm-sr/d3-lite'sd3_server.py/d3_runtime.pyat the same revision(
2fc06364…) and copied what it does. Four things the issue text does not say and a guess would have got wrong:questionsis an object (id → question), and those ids are the keys ofanswers, in request order.usageis{input_tokens, output_tokens: 0}whereinput_tokensis the sum of every answered question'sprompt length, not the state's.
422 {"detail": {"invalid_questions": {id: "why"}}}and nothing runs; aquestion that is valid but over the input limit is instead an entry inside
answers(
{"type", "error": "max_length_exceeded", "message"}, HTTP 200) and the other questions are stillanswered, with nothing truncated. So these routes answer 422, not the chat route's 400 — the
interop contract with vLLM Semantic Router wins here, and
docs/npu.mdsays why.enable_thinking=False, which opens the assistant turn with anempty think block. Those bytes are model input: reusing
chatml()would be a different request. Thewrapper is byte-compared against all nine committed fixtures from npu: the d3 question/answer contract, byte-equal to the pack's d3_format.py (#653) #765.
DECISION_API_KEY, when set, gates the decision routes exactly as upstream's server does (401 +WWW-Authenticate: Bearer, constant-time compare,/healthopen and reporting"authentication": true)./healthonly gains the d3 fields when a head is loaded, so a chat-only deployment keeps the body it had.Measured
Device: strixhalo, Qwen3-0.6B with a SYNTHETIC 255×1024 readout head (the d3 container layout, random
weights) — the route is what is under test; the choice means nothing.
NPU_INFER_HOST_GEMM=1undernpu-hold -l npufeat, one run at a time, 0amdxdna.*ERRORafter each.ctest npu_sampling / systemone_contract / systemone_route / decision_headsystemone_routeadds 30 assertionstools/copyright.py --check200{"choice":"fast","confidence":0.685,"probabilities":{"cheap":0.057,"fast":0.943}},input_tokens 97/v1/decisions, noul200{"noul":0.046}200{"score":0.0724,"confidence":0.759,"probabilities":{…},"legend":{…}}200, all three answered in request order,usage.input_tokens 269422 {"detail":"temprature not understood; a request may carry state, questions, model"}422 {"detail":{"invalid_questions":{"bad1":"…needs at least one option","bad2":"…is not served by this build"}}}, nothing ranimagessent422 "images: this build serves the text path only"DECISION_API_KEYset401huge→max_length_exceeded(34086 > 32768, "nothing was truncated"),small→ answered;usage.input_tokens 90200Two things this caught that no ctest would have:
curl's default content type isapplication/x-www-form-urlencoded, and httplib parses a form body withan 8 KB cap, answering
413before any handler runs. My first limit row looked like a route bug and wasa client bug: with
Content-Type: application/jsonthe same 170 KB body works. Documented, because a real d3caller (a 32k-token state is ~100 KB) will hit it if it leaves the header at curl's default.
cannot be built with a big state — the over-limit question has to differ from the others (I used a huge
instructions), otherwise the test measures the state and not the split.Not in this PR, and no number claimed for it
A real d3-lite backbone. I downloaded the real pack (1.7 GB backbone + readout + contract files) and could
not convert it, for three concrete reasons — this is the next piece, not something to fake a row for:
tools/safetensors_to_q4nx.py packdies onlinear_attn.conv1d.weight [6144,1,4]:ValueError: too many values to unpack (expected 2, got 3); the pack has 235 non-2-D tensors incl. a 5-Dvisual.patch_embed.proj.weightlanguage_model.layers.N.linear_attn.{in_proj_qkv,in_proj_a,in_proj_b,conv1d,dt_bias,norm,out_proj}; engine:model.layers.N.linear_attn.{qkv_proj,ssm_alpha_proj,ssm_beta_proj,ssm_conv1d,ssm_dt.bias,ssm_norm,ssm_out_proj}ggml-org/vllm-d3-lite-GGUFisgeneral.architecture = qwen35, andtools/gguf_to_q4nx.pyaccepts onlyqwen3/qwen2Also not here:
images/videos(vision tower + FFmpeg, refused by name), upstream's "state computed once perrequest" optimisation (that is the prefix cache's job, #650), and the pack's
noncausal_full_attentionattention mode — so no accuracy claim against upstream is made.
Files
app/npu_systemone.h(new, 152 lines, device-free),app/forward_serve.cpp(+190: routes, head loading,/health),npu/forward/include/q4nx_forward.h+src/q4nx_forward.cpp(last_hidden()),tests/systemone_route_test.cpp(new),CMakeLists.txt,docs/npu.md("Decisions").