Repository navigation
q4nx: cross-check our packer against the official converter (#587) - #658
Conversation
ROCm's FLM_Q4NX_Converter is the official gguf -> Q4NX converter and it is
public and Apache-2.0 now, so "does our packer agree with it" became a
question with an answer instead of an assumption. It was run beside
tools/gguf_to_q4nx.py on the same GGUF (qwen3 Q4_0, 311 tensors) and the
containers compared tile by tile:
* the format agrees: 311/311 names, dtypes and shapes, the same 32x256
tile of 5120 B, and headers that differ only by this engine's
104-byte __metadata__ block -- the official header size is exactly the
shipped FastFlowLM container's;
* the payload does not: our tiles keep the Q4_0 codes unsigned with the
zero-point in the min plane (-8d), theirs keeps min 0 with signed
nibbles, and the code order inside a tile is the 16-row split our
kernel walks against plain column major. Scale planes of all 53,760
Q4_0 tiles are byte-identical, min and code planes of all 72,752 are
not;
* both are exact, and mutually unreadable: 196/196 exact for ours and
197/197 for theirs under its own layout, 0/197 either way when the
layouts are crossed. So the provenance guards in npu/model.cpp and
tools/q4nx_to_gguf.py are load-bearing, not cautious;
* the one lossy tensor in both is a Q8_0 lm_head, and ours is ~13x
closer to the source (rms 2.28e-3 against 2.94e-2) because the
official path runs 8-bit codes through its 4-bit packer.
tools/q4nx_diff.py is that comparison, kept so it can be repeated: compare
two containers and say which plane differs, and decode either layout
against a GGUF to prove each file is exact under its own decoder.
tools/gguf_to_q4nx.py gains the one thing the official converter does
better and we got wrong: when --tokenizer is omitted it now looks for
tokenizer.json beside the GGUF, and warns loudly when it is missing
instead of reporting success on a directory 1bit forward-serve refuses.
configs/q4nx/ carries the official per-architecture configs verbatim
(Apache-2.0, attributed in NOTICE and its own README) as reference data for
the dtype/block-size table and the GDN name map; nothing in the build
reads them, and no 27B config entry is invented, because this packer has
no hybrid-GDN path yet and such an entry would not validate.
The first cut carried all 17 upstream arch configs (3,298 of the 3,631 added lines) for architectures this engine does not serve on the NPU. Keep the six that matter -- qwen3.json, which is what the packer targets, and the five qwen3.5_* hybrid-GDN configs that describe the family Qwen3.8-27B belongs to -- and pin the rest to upstream in the README instead of vendoring them: ROCm/FLM_Q4NX_Converter at d1d5232. Also add the Apache-2.0 notice to configs/q4nx/README.md, which is the file `python3 tools/copyright.py --check` failed the build on (the JSON itself is exempt as data, so the check is otherwise clean).
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY (posted as a comment: GitHub refuses --request-changes/--approve on a PR owned by the same account). One number in NOTICE needs fixing before it merges.
Every claim in the body that can be re-run on this box, re-run. The artifacts are still here (~/lb/587-q4nx/{ours,theirs}, ~/models/nvfp4/Qwen3-0.6B-Q4_0ctl.gguf).
Container comparison (tools/q4nx_diff.py compare ours theirs --summary):
A header 34688 B, data at 34696 | B header 34584 B, data at 34592
__metadata__ A: {"format": "q4nx", "source": "Qwen3-0.6B-Q4_0ctl.gguf", "converter": "tools/gguf_to_q4nx.py"}
__metadata__ B: absent
tensors: A=311 B=311; only A=0 only B=0
dtype diffs: 0 | shape diffs: 0
per-tensor bytes: identical=114 differing=197
plane scale identical tiles: 53760/72752
plane min identical tiles: 0/72752
plane codes identical tiles: 0/72752
so 311/311, 114 identical / 197 differing, the 104-byte metadata block, all 53,760 Q4_0 scale tiles identical and not one min or code byte, exactly as written. And the official header being the shipped container's size checks out against the box: ~/.config/flm/models/Qwen3-0.6B-NPU2/model.q4nx and ~/models/npu/Qwen3-0.6B/model.q4nx both have a 34,584-byte header.
Decoding against the GGUF as ground truth (all four directions):
--q4nx ours --layout ours -> exact=196 inexact=1 skipped=114 worst 1.653e-02 (output.weight Q8_0)
--q4nx theirs --layout flm -> exact=196 inexact=1 skipped=114 worst 3.233e-01 (output.weight Q8_0)
--q4nx ours --layout flm -> exact=0 inexact=197 skipped=114 wor
st 2.777e+00 (blk.27.ffn_up.weight)
--q4nx theirs --layout ours -> exact=0 inexact=197 skipped=114 worst 3.010e+00 (blk.27.ffn_up.weight)
"196/196 exact under its own decoder, 0/197 crossed (worst 2.78 / 3.01)", the Q8_0 lm_head being the one lossy tensor in both (1.65e-2 / rms 2.28e-3 against 3.23e-1 / rms 2.94e-2, i.e. ~13x on rms), is exact. So is the conclusion that matters for the engine: the layouts are mutually unreadable and the provenance guards are load-bearing.
The vendored configs really are verbatim: fetched from ROCm/FLM_Q4NX_Converter at d1d5232d… and hashed against the PR's copies — qwen3.json and the five qwen3.5_* files are byte-identical (6/6).
The tokenizer.json fix works, both branches, run on this head:
$ tools/gguf_to_q4nx.py pack /tmp/t658/wtok/m.gguf /tmp/t658/wtok-out # tokenizer.json beside the gguf, no --tokenizer
wrote /tmp/t658/wtok-out/model.q4nx (683820936 bytes)
-> config.json model.q4nx tokenizer.json tokenizer_config.json
$ tools/gguf_to_q4nx.py pack /tmp/t658/notok/m.gguf /tmp/t658/notok-out # none anywhere
WARNING: no tokenizer.json in /tmp/t658/notok: /tmp/t658/notok-out has model.q4nx and config.json only,
and 1bit forward-serve will refuse it. Pass --tokenizer <dir>.
-> config.json model.q4nx
and the freshly packed directory decodes identically to the archived one (--layout ours: 196 exact, worst 1.653e-02), so the tool is exercised on output from this head, not only on last night's artifacts. tools/copyright.py --check passes.
Two nits, the first one worth fixing in this PR:
NOTICEsays "FLM_Q4NX_Converter configuration files (17 JSON files under configs/q4nx/)" but the tree carries 6 (configs/q4nx/:qwen3.json+ the fiveqwen3.5_*+ README) — a leftover from the commit that kept only the configs we cite. The attribution itself (project, licence, commitd1d5232, "no code is copied") is correct; only the count is stale, andNOTICEis the wrong file to leave approximate.- The missing-
tokenizer.jsonpath still exits 0 — a stderr line is easy to lose in a pipeline that greps only stdout. An exit code (or--strict) would make "unservable directory" a failure rather than a warning; the warning text itself is exactly right.
Not verifiable here, and not claimed by this PR: the .bf16 twins / (q g) -> (g q) / parallel_size 32 details are read out of the upstream configs (which I did hash, and the qwen3.5_* files do carry parallel_size 32 and keep_block_in_2D true), but nothing in this PR packs a GDN model, and the 27B NPU route is not part of it.
What this is
ROCm's FLM_Q4NX_Converter — the official gguf → Q4NX converter,
convert.py+ per-arch JSONconfigs, Apache-2.0, pushed 2026-10-22 — is now public, and
scripts/repack_gguf.pyalready drivesit for the
1bit serve --device npurepack path. That turns "does our packer agree with the officialformat" into a question with an answer. It did not, in a specific and now-measured way.
Both converters were run on the same GGUF (
Qwen3-0.6B-Q4_0ctl.gguf, qwen3, 311 tensors: 196 Q4_0projections, Q8_0
token_embd/output, F32 norms) and the containers byte-compared tile by tile anddecoded back against the GGUF.
What agrees
I8/BF16) and shapes identical.__metadata__block (34,688 vs 34,584 B), and theofficial header size is exactly the shipped
~/.config/flm/models/Qwen3-0.6B-NPU2container's —the official converter reproduces the format FastFlowLM ships.
What does not
-8d;theirs keeps min 0 with signed nibbles (
q-8, nibble =qxor 8). Scale planes of all53,760 Q4_0 tiles are byte-identical; 0/72,752 min tiles and 0/72,752 code tiles are.
[row-half(2)][group(8)][col-in-group(32)][row-pair(8)], the16-row split
npu/q4nx.cppwalks; theirs is plain column major[col(256)][row-pair(16)].for ours under our layout, 196/197 for theirs under the FLM layout, and 0/197 in both
directions when the layouts are crossed (worst 2.78 / 3.01). The provenance guards in
npu/model.cppandtools/q4nx_to_gguf.py("its tile order is unknown") are load-bearing, notcautious.
lm_headis lossy in both, and ours is ~13× closer to the source: max 1.65e-2 / rms2.28e-3, against the official path's max 3.23e-1 / rms 2.94e-2 — it runs 8-bit codes through its
4-bit packer.
tokenizer.json: theirs extracts it from the GGUF; ours only copied it with--tokenizerandreported success on a directory
1bit forward-servethen refuses. Fixed here.config.json: ours writes it; the official output carries none.qwen3.5_*(hybrid GDN); ours is qwen3-only.The GDN dtype table and name map that this repo's earlier note described are confirmed in their
source (
configs/qwen3.5_4b.json,q4nx/models/qwen35.py):.bf16prefill twins beside the Q8_0ssm_alpha/ssm_betacopies,ssm_out_projQ4_K,(q g) -> (g q)withq=2forssm_aandssm_dt.bias, norms/conv BF16,row_block32 /col_block256. One correction to that note:parallel_size32 is theqwen3.5_*family only — every other arch config is 16, andkeep_block_in_2Dis true only forqwen3.5_*and gpt-oss (which also uses 128-column blocks).What changes
tools/q4nx_diff.py(new, our code):compare A Bsays which plane of which tensor differs;decode --gguf M --q4nx DIR --layout ours|flmproves either file exact under its own decoder. Everynumber above comes from it, so it can be re-run when either converter moves.
tools/gguf_to_q4nx.py: thetokenizer.jsonfix — fall back to the GGUF's own directory, andwarn loudly when it is still missing instead of silently producing an unservable directory.
configs/q4nx/: six upstream configs verbatim as reference data —qwen3.json(what the packertargets) and the five
qwen3.5_*hybrid-GDN files (the family Qwen3.8-27B is in). The other elevenare pinned to upstream (ROCm/FLM_Q4NX_Converter at
d1d5232) inconfigs/q4nx/README.mdinstead ofbeing vendored, with the Apache-2.0 attribution block in
NOTICE. Nothing in the build reads them,and
tools/gguf_to_q4nx.pydoes not start consuming them: its tile order is whatnpu/q4nx.cppdecodes.
docs/npu.md: "Cross-check against the official converter", with the table and the numbers.No 27B config entry. Their
qwen3.5_*data is now carried, but this packer has no hybrid-GDN pathat all (no per-tensor dtype table, no ssm reorder, no
.bf16twins), so a Qwen3.8-27B Q4NX built byit would match neither the official container nor our kernel. Carrying the data describes the gap
instead of guessing at it; closing it is its own change.
Device-free: nothing here needs the NPU, and no measurement in it was taken on one.
CI
buildpasses (3m37s) after the copyright notice landed onconfigs/q4nx/README.md— that check iswhat failed the first push, and the JSON is exempt as data.
pr_agentfails for an environmentreason, not this change: it wants
coder-llmon:8090, which is deliberately off on this box (theowner's setting), and the job says so itself ("the local model (coder-llm on :8090) does not answer").