Skip to content

q4nx: cross-check our packer against the official converter (#587) - #658

Merged
bong-water-water-bong merged 3 commits into
mainfrom
q4nx-converter-validate
Oct 30, 2026
Merged

bong-water-water-bong merged 3 commits into
mainfrom
q4nx-converter-validate

Conversation

@bong-water-water-bong

@bong-water-water-bong bong-water-water-bong commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

What this is

ROCm's FLM_Q4NX_Converter — the official gguf → Q4NX converter, convert.py + per-arch JSON
configs, Apache-2.0, pushed 2026-10-22 — is now public, and scripts/repack_gguf.py already drives
it for the 1bit serve --device npu repack path. That turns "does our packer agree with the official
format" 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_0
projections, Q8_0 token_embd/output, F32 norms) and the containers byte-compared tile by tile and
decoded back against the GGUF.

What agrees

  • 311/311 tensor names, dtypes (I8/BF16) and shapes identical.
  • The same tile geometry: 32 × 256, 5120 B = 512 scale + 512 min + 4096 codes.
  • Headers differ only by this engine's 104-byte __metadata__ block (34,688 vs 34,584 B), and the
    official header size is exactly the shipped ~/.config/flm/models/Qwen3-0.6B-NPU2 container's —
    the official converter reproduces the format FastFlowLM ships.

What does not

  • Payload: 114 of 311 tensors byte-identical (113 F32 norms + the bf16 embedding), 197 differ.
  • Q4_0 zero-point: ours keeps the codes unsigned with the zero-point in the min plane as -8d;
    theirs keeps min 0 with signed nibbles (q-8, nibble = q xor 8). Scale planes of all
    53,760 Q4_0 tiles are byte-identical; 0/72,752 min tiles and 0/72,752 code tiles are.
  • Intra-tile code order: ours is [row-half(2)][group(8)][col-in-group(32)][row-pair(8)], the
    16-row split npu/q4nx.cpp walks; theirs is plain column major [col(256)][row-pair(16)].
  • Both are exact, and mutually unreadable. Against the GGUF as ground truth: 196/196 exact
    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.cpp and tools/q4nx_to_gguf.py ("its tile order is unknown") are load-bearing, not
    cautious.
  • Q8_0 lm_head is lossy in both, and ours is ~13× closer to the source: max 1.65e-2 / rms
    2.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 --tokenizer and
    reported success on a directory 1bit forward-serve then refuses. Fixed here.
  • config.json: ours writes it; the official output carries none.
  • Coverage: theirs has 17 arch configs including 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): .bf16 prefill twins beside the Q8_0
ssm_alpha/ssm_beta copies, ssm_out_proj Q4_K, (q g) -> (g q) with q=2 for ssm_a and
ssm_dt.bias, norms/conv BF16, row_block 32 / col_block 256. One correction to that note:
parallel_size 32 is the qwen3.5_* family only — every other arch config is 16, and
keep_block_in_2D is true only for qwen3.5_* and gpt-oss (which also uses 128-column blocks).

What changes

  • tools/q4nx_diff.py (new, our code): compare A B says which plane of which tensor differs;
    decode --gguf M --q4nx DIR --layout ours|flm proves either file exact under its own decoder. Every
    number above comes from it, so it can be re-run when either converter moves.
  • tools/gguf_to_q4nx.py: the tokenizer.json fix — fall back to the GGUF's own directory, and
    warn 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 packer
    targets) and the five qwen3.5_* hybrid-GDN files (the family Qwen3.8-27B is in). The other eleven
    are pinned to upstream (ROCm/FLM_Q4NX_Converter at d1d5232) in configs/q4nx/README.md instead of
    being vendored, with the Apache-2.0 attribution block in NOTICE. Nothing in the build reads them,
    and tools/gguf_to_q4nx.py does not start consuming them: its tile order is what npu/q4nx.cpp
    decodes.
  • 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 path
at all (no per-tensor dtype table, no ssm reorder, no .bf16 twins), so a Qwen3.8-27B Q4NX built by
it 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

build passes (3m37s) after the copyright notice landed on configs/q4nx/README.md — that check is
what failed the first push, and the JSON is exempt as data. pr_agent fails for an environment
reason, not this change: it wants coder-llm on :8090, which is deliberately off on this box (the
owner's setting), and the job says so itself ("the local model (coder-llm on :8090) does not answer").

bong-water-water-bong added 2 commits October 30, 2026 09:52
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 bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. NOTICE says "FLM_Q4NX_Converter configuration files (17 JSON files under configs/q4nx/)" but the tree carries 6 (configs/q4nx/: qwen3.json + the five qwen3.5_* + README) — a leftover from the commit that kept only the configs we cite. The attribution itself (project, licence, commit d1d5232, "no code is copied") is correct; only the count is stale, and NOTICE is the wrong file to leave approximate.
  2. The missing-tokenizer.json path 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.

@bong-water-water-bong
bong-water-water-bong merged commit 7b85302 into main Oct 30, 2026
7 of 8 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the q4nx-converter-validate branch October 30, 2026 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant