Skip to content

npu converter: --dtype bf16 writes no tiles, so the engine and a reference can be compared on the same weights (#796) - #846

Merged
bong-water-water-bong merged 2 commits into
mainfrom
npu/converter-bf16
Nov 2, 2026
Merged

bong-water-water-bong merged 2 commits into
mainfrom
npu/converter-bf16

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

One new flag and one test row; no behaviour change to the default path.

tools/safetensors_to_q4nx.py pack --dtype bf16 keeps every tensor at its source dtype (bf16, or the F32 a
transform forces) instead of writing q4_1 tiles. The default --dtype auto is untouched.

Why it exists. engine#796's remaining host-side ask is numeric parity against the pack's own d3_runtime.py.
Comparing an engine run of a q4_1 pack against a bf16 reference mixes two causes: the forward, and the weight
precision. With this flag the weights are held fixed, so:

  • engine(bf16 container) vs reference(bf16 pack) isolates the forward;
  • engine(bf16) vs engine(q4_1) isolates what quantization costs on the same requests.

Measured on the real vllm-sr/d3-lite pack this round (host-side convert, then engine runs under one npu-hold
ticket each, host-GEMM, no device):

row result
pack --dtype bf16 8.7 s, 320 tensors, 1.5 GB, 153 visual.* skipped
verify on it 320/320 ok, 0 error (bf16 max err 0) — no loss by construction
ctest safetensors_to_q4nx PASS (new row: no I8 anywhere, forced-F32 stays F32, tileable projection is BF16, verify accepts it)
tools/copyright.py --check rc 0

The parity row itself is posted on #796, including the part that matters: the engine's answers do not match
the reference, and quantization alone is not the reason (bf16-vs-bf16 still differs by |dp| up to 0.84).

Not in it: no --dtype f16, no per-tensor overrides, and nothing here changes what a served pack looks like.

…rence can be compared on the same weights (#796)

`tools/safetensors_to_q4nx.py pack --dtype bf16` keeps every tensor at its source dtype (bf16, or the F32 a
transform forces) instead of writing q4_1 tiles. It is the container for measuring, not for serving: with the
weights held fixed, an engine run and a reference run differ only in the forward, and what q4_1 costs becomes a
separate delta instead of an unexplained one.

Used this round for #796's parity row against the pack's own d3_runtime.py: `verify` reports 0 error on the
1.5 GB container (320/320 tensors), and the two containers of the same pack (bf16 and q4_1) answer the same ten
contract requests. The row itself, and what it found (the gap is not weight precision), is on the issue.

Test: `ctest safetensors_to_q4nx` gains the bf16 container row -- no I8 tile anywhere, the transform-forced F32
tensor stays F32, a tileable projection is BF16, and `verify` accepts it.
@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit 03e1476)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

796 - Partially compliant

Compliant requirements:

  • The PR introduces a --dtype bf16 flag that allows the converter to write no tiles at all, keeping tensors in their source dtype (bf16 or F32 from transforms), which is essential for numeric parity testing.
  • The PR adds tests to verify that --dtype bf16 produces no q4_1 tiles and correctly preserves the expected dtypes.
  • The PR updates documentation to reflect the new --dtype bf16 option and its intended use for measuring forward pass accuracy.

Non-compliant requirements:

  • The PR does not address the specific issue of the converter assuming 2-D tensors for non-2-D tensors like the GDN conv1d (3-D [6144,1,4]).
  • The PR does not address the name mapping issue between HF names and llama.cpp-style names.
  • The PR does not address the three transforms that fail silently (A_log → ssm_a, conv1d transpose, alpha/beta handling).
  • The PR does not provide the 7-design ELF set for the 0.8B-GDN geometry required for device-side testing.

Requires further human verification:

  • Verification that the 2-D tensor assumption is handled correctly for non-2-D tensors (e.g., GDN conv1d).
  • Verification that the name mapping between HF and llama.cpp-style names works correctly.
  • Verification that the three silent-failing transforms are handled correctly.
  • Verification that the 7-design ELF set is correctly implemented and functional on the hardware.
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Incorrect dtype handling for non-2D tensors

The PR introduces a --dtype bf16 option to avoid quantization, but it does not address the core issue of the converter assuming 2-D tensors. The pack has 235 non-2-D tensors, including a 3-D tensor [6144,1,4] for GDN conv1d, which would still be incorrectly handled by the current logic. This could lead to silent failures or incorrect tensor processing.

kind = (forced != "F32" and
        (args.dtype == "bf16" or len(w.shape) == 1 or name in KEEP_BF16 or
         w.shape[0] % ROWS or
         (len(w.shape) == 2 and w.shape[1] % COLS)))
Missing name mapping logic

The PR does not implement the required name mapping between HF names and llama.cpp-style names. The authority for the mapping is already in the tree (configs/q4nx/qwen3.5_0.8b.json), but this mapping is not applied in the converter, which is a critical part of the requirements.

    continue
w = read_tensor(path, header, data_off, name)
got = transcode(name, w, arch)
if got is None:
    skipped += 1
    continue
name, w, forced = got
kind = (forced != "F32" and
        (args.dtype == "bf16" or len(w.shape) == 1 or name in KEEP_BF16 or
         w.shape[0] % ROWS or
         (len(w.shape) == 2 and w.shape[1] % COLS)))
Silent failure in transforms

The PR does not address the three transforms that fail silently if wrong: A_log → ssm_a (F32 and negative), conv1d (transpose, not flatten), and alpha/beta (int8 tiles vs Q8_0). These transforms are essential for correct behavior and must be implemented with known-answer checks.

    continue
w = read_tensor(path, header, data_off, name)
got = transcode(name, w, arch)
if got is None:
    skipped += 1
    continue
name, w, forced = got
kind = (forced != "F32" and
        (args.dtype == "bf16" or len(w.shape) == 1 or name in KEEP_BF16 or
         w.shape[0] % ROWS or
         (len(w.shape) == 2 and w.shape[1] % COLS)))

@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 at 1a42be6f — --dtype bf16 does what the page says, re-derived on a real checkpoint rather than on
the PR's synthetic fixture.

python3 tests/safetensors_to_q4nx_test.py tools/safetensors_to_q4nx.py -> npu_plan_generate: ok-style single line,
rc 0 (the file's check() prints only failures, so rc is the signal). The new cases are the ones that matter: no I8
in the container's dtypes, ssm_a stays F32 under --dtype bf16 (the transform-forced case), a tileable projection
comes out BF16, and verify accepts it.

Then the same on a real model, which the PR does not do and the doc's claim deserves — Qwen2.5-0.5B-Instruct-hf on
this box, 290 tensors, --dtype bf16:

wrote /tmp/tprs846/bf16/model.q4nx (988097928 bytes, 290 tensors)
$ python3 tools/safetensors_to_q4nx.py verify … /tmp/tprs846/bf16
290/290 tensors ok                          # every row "bf16 max err 0 (bound …)"
$ read_q4nx(model.q4nx) -> 290 tensors, dtypes {'BF16': 290}, any I8: False

988,097,928 B for a 0.5B checkpoint is bf16 with no tiles, the tensor set is identical to the --dtype auto container's,
and max err 0 is literally exact — "the weights are held fixed" is true of the bytes, not approximately true.

Two notes, neither a gate. (1) The page says the d3-lite parity row is measured this way; that row needs the pack's own
d3_runtime.py and a NPU_INFER_HOST_GEMM=1 serve, which I did not run — the container side is verified here, the row
is the lane's. (2) pack happily writes a container with no tokenizer and only says so in a WARNING line (my run hit
it: the HF checkpoint has no tokenizer.json), and the page's own text says 1bit forward-serve will refuse such a
container — the mode that exists to be served by a reference is the mode most likely to hit this, so it might be worth
refusing outright or copying tokenizer.json when the source dir has one. Pre-existing behaviour, not introduced here.

No device time, no GPU claim: this is a converter and a container, and nothing in it opens the NPU.

@bong-water-water-bong bong-water-water-bong added the tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied label Nov 2, 2026
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Correction to one string in my review above: the test prints safetensors_to_q4nx: ok (not npu_plan_generate: ok — that is a different test in this repo). Everything else stands, verdict included.

@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 at 03e1476 (the main merge; own files unchanged).

Re-verified rather than assumed: at this head all three of the PR's files — tools/safetensors_to_q4nx.py,
tests/safetensors_to_q4nx_test.py, docs/npu.md — are byte-identical to the blobs I verdicted at 1a42be6
(git rev-parse compared). The cheap gate re-run here anyway: tests/safetensors_to_q4nx_test.py -> safetensors_to_q4nx: ok, rc 0, and tools/copyright.py --check rc 0.

The 19:21Z substance stands: --dtype bf16 writes the same tensor set with no I8 tiles, keeps the transform-forced F32,
and on a real checkpoint (Qwen2.5-0.5B-Instruct-hf) produced 290 tensors all BF16 with verify 290/290 at max err 0 —
frame the weights exactly, which is the point of that mode.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 03e1476

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review effort 3/5 tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant