Repository navigation
tools: safetensors -> Q4NX converter + the d3 readout head loader (#653 prerequisite) - #763
Conversation
…loader class (#653) The blocker on #653 was never the download: the d3 packs ship Hugging Face *safetensors* and the engine's only converter, tools/gguf_to_q4nx.py, takes GGUF. This adds the missing path, host-side, and the readout head the /v1/systemone endpoint will sit on. tools/q4nx_pack.py The q4_1 tile packers that both converters write with -- bf16_bits/from_bf16/f16, quantize_q4_1, tiles, untile -- moved out of gguf_to_q4nx.py so the two cannot drift and so a converter with no GGUF input does not need gguf-py at all. gguf_to_q4nx.py imports them; its own test still passes 14/14, which is the proof the extraction is behaviour-preserving. tools/safetensors_to_q4nx.py `pack <hf dir> <out dir>`: reads model.safetensors (or the sharded files via model.safetensors.index.json), writes model.q4nx in the same container as the GGUF converter -- the HF tensor names ARE the Q4NX names, so nothing is translated -- with q4_1 tiles for 2-D projections and bf16 for norms, biases and the embedding, the model's own config.json plus the two Q4NX fields, and the tokenizer files copied. `verify`: every tensor against the source, dequantized (bf16 bound, q4_1 relative RMS). `decision`: readout.safetensors + decision_config.json -> decision.q4nx (one F32 [codes, hidden] matrix, kept exact: the head is the model's decision, not a weight to quantize). npu/decision.h -- the loader tensor class DecisionHead loads a d3 pack's decision metadata and readout and does the arithmetic the endpoint needs: logits() is W*h (one per option code), probabilities_over() is the temperature softmax over the options a question offers, probabilities() is all of them. It refuses what it cannot honour by name: a readout that is not F32, rows that do not match the declared codes, token_ids that do not line up, more than the d3 contract's 255 options, a missing readout. Measured, no device and no model download in the tests: ctest decision_head PASS -- synthetic packs: W*h against hand values, softmax sums to 1, a subset of options softmaxes over itself (hand-computed value), the readout temperature is applied, and each refusal above ctest safetensors_to_q4nx PASS -- synthetic single-shard and sharded HF models pack and verify; the tiles decode back to the projection; the d3 head round-trips exactly; an unsupported dtype, a mismatched code count and a missing readout are refused by name real pack, no download of the backbone: vllm-sr/d3-lite's readout.safetensors (1.04 MB) and decision_config.json convert to decision.q4nx and load as codes=255 hidden=1024 format=d3-code-readout-v1 prompt=d3 temp=1.00 base=Qwen/Qwen3.5-0.8B, with the 255 probabilities summing to 1.000000 (a zero-mean hidden state gives 0.0040 each, i.e. 1/255) Two bugs the tests caught in my own code: `f16` had lost `np.ascontiguousarray` in the extraction, so a non-contiguous view was reinterpreted at the wrong stride (gguf_to_q4nx's exact Q4_0/Q4_1/Q4_K path went from 14/14 to 9/14 until it was restored), and `verify` called `.copy()` on a `bytes` object. Not in this PR: /v1/systemone itself (the next one, on top of this class), the vision question kinds (box/track/assets), and a conversion of a real 5.14 GB d3 *backbone* -- the converter's backbone path is verified on synthetic fixtures and the real readout is verified against the real pack, but downloading 5 GB is a deliberate separate step (and would run under the memory rule).
PR Reviewer Guide 🔍(Review updated until commit 380548c)Here are some key observations to aid the review process:
|
…es instead of reporting Not Run
.github/workflows/ci.yml builds an explicit list of targets and ctest runs whatever binaries exist, so
a test whose target is missing from that list reports "Not Run" and never executes -- the comment in
the workflow says exactly that, and it is what failed this PR's first CI run (decision_head - Not Run).
The right fix is a ci.yml entry, but this lane's token has no workflow scope ('refusing to allow an
OAuth App to create or update workflow .github/workflows/ci.yml'), so the test binary is built as a
dependency of npu_sampling_test, which IS listed. Whoever can edit the workflow should add
decision_test to the list and drop this; until then the row cannot silently stop running.
|
Persistent review updated to latest commit 90f7b6a |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
At 90f7b6af — first review, build green. Seven files, +982 −55: the safetensors→Q4NX converter, npu/decision.h (the "d3 head"), and their tests.
Both tests run, and both pass:
$ g++-16 -std=c++17 -O1 -I npu -I app -I<json-src> tests/decision_test.cpp -lgtest -lgtest_main && ./a.out
decision: ok
$ python3 tests/safetensors_to_q4nx_test.py
safetensors_to_q4nx: ok
So the contract header and the converter are each gated by a test that runs without a device, and both are add_test()-wired — meaning CI enforces them on every PR, unlike the NPU lanes generally. That matters more than usual here: the converter rewrites the packing of weights, and tools/gguf_to_q4nx.py loses 57 lines to tools/q4nx_pack.py, i.e. the packing logic is being centralised so the safetensors and GGUF paths cannot drift apart in how they lay out a container. Centralising it behind one tested module is the right move rather than a second copy.
Not verified: a converted container actually loading and running on the array (that is the lane's window), and the numeric fidelity of the conversion beyond what the unit test covers — for a converter the interesting failure is not "does it run" but "is the round-trip exact", so if there is a safetensors → q4nx → back comparison anywhere in the test it is worth naming in the body; if not, that is the next gate I would want.
tprs-ready applied.
|
Persistent review updated to latest commit 380548c |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
At 380548c7 — 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.
Both gates re-run at this head: decision: ok and safetensors_to_q4nx: ok. Packing stays centralised in tools/q4nx_pack.py, so the safetensors and GGUF paths cannot drift apart in how they lay out a container.
tprs-ready stands.
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 conversion path #653 was blocked on, plus the readout head the
/v1/systemoneendpoint will sit on. Host-side, no device, no model download in the tests.tools/gguf_to_q4nx.py(the only converter) takes GGUF; the d3 packs ship HF safetensors. That was the blocker — never the download.Three pieces
tools/q4nx_pack.py— the q4_1 tile packers (bf16_bits/from_bf16/f16,quantize_q4_1,tiles,untile) extracted fromgguf_to_q4nx.py, so both converters write with the same code and a converter with no GGUF input needs nogguf-py.gguf_to_q4nx.pynow imports them; its own test still passes 14/14, which is the evidence the extraction is behaviour-preserving.tools/safetensors_to_q4nx.py—pack <hf dir> <out dir>: readsmodel.safetensorsor the sharded files viamodel.safetensors.index.json, writesmodel.q4nxin the same container as the GGUF converter. The HF tensor names are the Q4NX names, so nothing is translated; 2-D projections become q4_1 tiles, norms/biases/the embedding stay bf16; the model's ownconfig.jsonis carried over with the two Q4NX fields; tokenizer files are copied.verify: every tensor against the source, dequantized (bf16 bound for bf16, relative RMS for q4_1).decision:readout.safetensors+decision_config.json→decision.q4nx— one F32[codes, hidden]matrix, kept exact, because the head is the model's decision and not a weight to quantize.npu/decision.h—DecisionHead, the loader tensor class:logits()= W·h (one per option code),probabilities_over()= temperature softmax over the options a question offers,probabilities()= all of them. It refuses by name: a readout that is not F32, rows that do not match the declaredcodes,token_idsthat do not line up, more than the d3 contract's 255 options, a missing readout.Measured
ctest decision_headexp(Δ/T)says; each refusal fires with the field namedctest safetensors_to_q4nxvllm-sr/d3-lite, 1.04 MBreadout.safetensors+decision_config.json)codes=255 hidden=1024 format=d3-code-readout-v1 prompt=d3 temp=1.00 base=Qwen/Qwen3.5-0.8B, 255 probabilities summing to 1.000000, a zero-mean hidden state giving 0.0040 each (1/255)ctest npu_sampling(regression)Two bugs the tests caught in my own code
f16droppednp.ascontiguousarray, so a non-contiguous view was reinterpreted at the wrong stride —gguf_to_q4nx's exact Q4_0/Q4_1/Q4_K path went from 14/14 to 9/14 until it was restored. That is why the extraction is guarded by the existing test rather than by inspection.verifycalled.copy()on abytesobject.Not in this PR
/v1/systemoneitself — the next PR, on top ofDecisionHead(endpoint, question kinds, the 32,768-token per-question limit,max_length_exceeded).box/track/assets) and FFmpeg.systemd-run --user -p MemoryMax=,free -gfirst).