Repository navigation
npu: the d3 question/answer contract, byte-equal to the pack's d3_format.py (#653) - #765
Conversation
PR Reviewer Guide 🔍(Review updated until commit 7ea2519)Here are some key observations to aid the review process:
|
d2f9226 to
68f2c9f
Compare
|
Persistent review updated to latest commit 68f2c9f |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
At 68f2c9f3 — first review, build green. npu/systemone.h (+257), tests/systemone_test.cpp (+185), and a golden set tests/golden/systemone/prompts.json (+187, keyed source/note/cases).
Ran the test:
$ g++-16 -std=c++17 -O1 -I npu -I app -I<json-src> tests/systemone_test.cpp -lgtest -lgtest_main && ./a.out
systemone: ok (30 checks, exit 0)
A contract PR for the d3 head, with goldens committed beside it — which is the pattern that makes a contract testable later: the prompts are data, the expectations are in the JSON, and the test drives the header against them without a device. 30 checks over a golden set is enough coverage to make a future change to the contract fail loudly rather than drift.
It is add_test()-wired, so CI runs it on every PR — for an NPU-adjacent component that is worth more than the usual green tick, since the NPU lanes themselves are not built by CI.
Not verified: the behaviour against a real d3 head on the array (the contract is exercised, the runtime is not), and the goldens' provenance — where those expected outputs came from. If they were recorded from a working run, saying so in the golden file's note would make them a regression baseline rather than an assertion of intent; if they were hand-written from the spec, that is fine too, but the difference decides what a later failure means.
tprs-ready applied.
|
Persistent review updated to latest commit fbb82fe |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
At fbb82fe6 — re-confirmed after the waiter moved the head: build green, and I re-ran the gate at this content rather than assuming the rebase was inert.
tests/systemone_test.cpp → systemone: ok, 30 checks against the committed golden set (source/note/cases), add_test()-wired so CI enforces the contract on every PR.
tprs-ready stands.
…_format.py (#653) The /v1/systemone endpoint is only honest if its prompt is the one the model was trained on: the d3 pack's d3_format.py says the contract expects exactly those strings, and changing a byte changes the input. This is that contract, as a device-free header, checked against the pack's own file rather than against my reading of it. npu/systemone.h question_of() a request question -> keys, option texts, and the rules: choice 1..255 options (a "key: description" line, or the key alone when the description is null), noul always false/true with the pack's fallback texts, score an ordered list of 2..10 distinct levels rendered as a choice over them (what d3_runtime.py does) user_prompt() the "State:/Question:/Options:/Reply with only the code..." turn, byte-exact -- including Python's ", " and ": " json spacing and the request's key order answer_of() choice -> {choice, probabilities, confidence}; noul -> {noul}; score -> {score = sum(level * p), probabilities, confidence, legend} candidate_codes() A..Z then AA.. up to 255 tests/golden/systemone/prompts.json Nine fixtures whose expected prompts were produced by the PACK'S d3_format.user_prompt() at revision 2fc06364715b967f1860aea9cf38778875588b17, committed with that provenance in the file. They cover a null description, a JSON-object description, a JSON-array description, unicode, explicit instructions, noul with and without criteria, a state that is a bare string, a null state, a score over plain levels and a score over JSON levels. ctest systemone_contract PASS -- all nine prompts byte-identical to upstream, plus: the option rules (1..255, score 2..10 distinct non-empty, noul's two keys and fallback texts, 255 refused at 256), the answer shapes, ties keeping the first option (as upstream's strict > does), confidence 0 for a flat distribution and 1 for a decided one, score's expected value, and the refusals (wrong count, negative, all-zero) ctest decision_head PASS (unchanged) ctest safetensors_to_q4nx PASS (unchanged) Two things this exposed in my own work: the first test draft parsed the fixtures through nlohmann::json, which sorts keys, so every JSON-description case failed -- the fix is ordered_json, and the header now says why the state must stay ordered (upstream keeps the request's order, and a sorted object is a different prompt). The other is that score questions are NOT in d3_format.py at all: they are rendered as a choice by d3_runtime.py, so the header documents that lineage rather than implying d3_format covers them. Not here: the HTTP route, the backbone forward + pooling and the 32,768-token per-question limit -- those are the endpoint half, on top of this contract and npu/decision.h's readout.
|
pi-prs: re-verdict at head #763 merged as Verified at this head: |
fbb82fe to
1f1d4bf
Compare
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
At 1f1d4bfb — build green after the rebase, and I re-ran the contract test at this content rather than assuming the rebase was inert: tests/systemone_test.cpp → systemone: ok (30 checks against the committed golden set).
The d3 question/answer contract remains what I reviewed: prompts as data in tests/golden/systemone/prompts.json (source/note/cases), the header driven against them without a device, and add_test()-wired so CI enforces the contract on every PR — worth more than the usual tick for an NPU-adjacent component, since the NPU lanes are not built by CI.
tprs-ready stands.
|
Persistent review updated to latest commit 449ebb1 |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
Confirmation at 449ebb1d: Merge branch 'main' into 1bit/npu-systemone-contract, author commit unchanged
(1f1d4bf, the one I ran the suite on), own diff still 4 files (+640), main an ancestor.
Re-ran the suite at the tip (it compiles against the newer tree now):
$ g++-16 -std=c++26 -O1 -Inpu -isystem <build>/_deps/json-src/include tests/systemone_test.cpp && ./systemone_test
systemone: ok
tprs-ready stays. No GPU and no NPU work — the test is device-free and reads a committed golden set.
|
Persistent review updated to latest commit 7ea2519 |
|
One contract divergence found while writing the endpoint on top of this branch, worth fixing here before merge, because the endpoint will otherwise ship it.
Upstream return Question(kind, question, rendered,
[str(i) for i in range(len(levels))], # keys -> "0", "1", "2"
list(criteria)) # descriptions (legend)and "probabilities": dict(zip(question.keys, shares)), # {"0": p0, "1": p1, "2": p2}
"legend": {key: _canonical(text) for key, text in zip(question.keys, question.descriptions)}
Two-line fix: give Not changed in this branch by me right now (it is your PR, not mine to rewrite mid-review), and the endpoint PR is |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
Confirmation at 7ea2519c: Merge branch 'main' into 1bit/npu-systemone-contract, author commit unchanged
(1f1d4bf), own diff still 4 files (+640), main an ancestor. Re-ran the suite at the tip rather than trusting the
rebase, since the tree under it moved (main now carries #761 and #764):
$ g++-16 -std=c++26 -O1 -Inpu -isystem <build>/_deps/json-src/include tests/systemone_test.cpp && ./systemone_test
systemone: ok (rc 0)
tprs-ready stays.
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.
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. Co-authored-by: bong-water-water-bong <[email protected]>
What
The d3 question/answer contract as a device-free header, checked against the pack's own
d3_format.pybyte for byte. This is the layer/v1/systemonewill stand on: how a request'sstateandquestionsbecome the prompt the model reads, and how per-option probabilities become the answer object.The pack says the model expects exactly those strings — "changing any byte changes its input" — so "close enough" is not a thing here.
npu/systemone.hquestion_of()— a request question into keys, option texts, and the rules:choice1–255 options (a"key: description"line, or the key alone when the description is null),noulalwaysfalse/truewith the pack's fallback texts ("No / false","Yes / true"),scorean ordered list of 2–10 distinct non-empty levels rendered as a choice over them.user_prompt()— theState:/Question:/Options:/Reply with only the code of the best option.turn: byte-exact, including Python's", "and": "JSON spacing and the request's key order.answer_of()—choice→{choice, probabilities, confidence};noul→{noul};score→{score = Σ level·p, probabilities, confidence, legend}.confidenceis upstream's1 − H/log n.candidate_codes()— A..Z then AA.. up to 255.How it is checked
tests/golden/systemone/prompts.jsonholds nine fixtures whose expected prompts were produced by the pack's ownd3_format.user_prompt()at revision2fc06364…, committed with that provenance in the file. They cover a null description, a JSON-object description, a JSON-array description, unicode, explicitinstructions,noulwith and without criteria, a bare-string state, a null state, a score over plain levels and a score over JSON levels.ctest systemone_contract: PASS — all nine prompts byte-identical to upstream, plus the option rules (1–255, score 2–10 distinct non-empty, 255 refused at 256,noul's keys and fallbacks), the three answer shapes, ties keeping the first option (as upstream's strict>does), confidence 0 for a flat distribution and 1 for a decided one, score's expected value, and the refusals (wrong probability count, negative, all-zero).Two things this exposed
nlohmann::json, which sorts keys, so every JSON-description case failed. The fix isordered_json— and the header now says why the state must stay ordered: upstream keeps the request's order, and a sorted object is a different prompt, i.e. different model input. An endpoint that ignores this would be silently wrong.scoreis not ind3_format.pyat all. It is rendered as a choice byd3_runtime.py, which is where the expected-value answer,confidenceandlegendcome from. The header documents that lineage rather than implyingd3_formatcovers all three kinds.Not in this PR
max_length_exceeded— the endpoint half, on top of this contract andnpu/decision.h's readout (tools: safetensors -> Q4NX converter + the d3 readout head loader (#653 prerequisite) #763).box/track/assets) — refused by name here, so a caller gets a 400 instead of a wrong answer.