Skip to content

npu forward: a 2-D weight is always decoded as a 5120-byte q4_1 tile, so a bf16 container (pack --dtype bf16) loads garbage instead of refusing #875

Closed

Description

@bong-water-water-bong

The forward decodes every 2-D weight as a 5120-byte q4_1 tile, so a bf16 container loads garbage instead of refusing

Found while chasing #796's d3 parity gap. Measured, with one binary and one prompt, on two containers of the same
weights
:

container engine's dequantized layers.0.linear_attn.qkv_proj.weight row 0 vs the pack's own row 0 mean|·| engine / pack
d3-lite-conv (q4_1, what the format expects) cos +0.99663 (row 0, i.e. the right row) 0.011284 / 0.011118
d3-lite-bf16 (pack --dtype bf16, #846) cos +0.12465 at its best row out of all 6144, median |cos| 0.021 0.091705 / 0.011118 (8.2×)

And a search of the bf16 container's 1024-value row against every 2-D tensor in the pack tops out at |cos| 0.157,
i.e. it is in no tensor at all.

Why

dequant_any() (npu/forward/src/q4nx_forward.cpp:159-168) picks a decoder purely from bytes per tile:

const int cb = (int)(nbytes / n);                       // n = (rows/32) * (cols/256)
if (cb == Q4NX_TILE_BYTES || cb == Q4NX_CHUNK_Q4K || cb == Q4NX_CHUNK_Q8)
    return q4nx_dequant_tensor_chunked(...);
return q4nx_dequant_tensor(...);                        // <-- also q4_1 tiles only

and q4nx_dequant_tensor() (npu/forward/src/q4nx_dequant.c:126-152) reads 5120-byte q4_1 tiles
unconditionally — there is no bf16 branch. It only checks nbytes >= q4nx_tensor_bytes(rows, cols), so a bf16 tensor
does not fail, it is silently mis-decoded: layer 0's in_proj_qkv (6144×1024) needs 3,932,160 B as tiles and the
bf16 container holds 12,582,912 B (exactly the 3.2× that 2 B/value vs 0.625 B/value implies), so the first third of
a bf16 buffer is read as q4_1 tiles.

1-D tensors are unaffected — read_bf16() reads those directly, which is why the same run shows
layers.0.input_layernorm.weight matching the pack to the bit (cos 1.000000, rel 0.000000) and
XN == rmsnorm(X, in_norm_[0]) holding exactly, while every fine-grained weight is garbage.

Impact

Fix shape

Either refuse, or teach the dequant a plain-bf16 branch — the size check is already there to be made explicit:

  1. in dequant_any(): if ((size_t)rows * (size_t)cols * 2 == nbytes) return q4nx_dequant_plain_bf16(data, rows, cols, out);
    (a ~10-line sibling of q4nx_dequant_plain_i8, which exists for exactly this kind of "plain, no tile header"
    tensor);
  2. and in q4nx_dequant_tensor() bis: keep the tile path, but make the size test exact (need != nbytes → error)
    so a future container family that nobody taught the decoder cannot be read as tiles again;
  3. a ctest row with a synthetic 64×512 bf16 tensor whose values are known, decoded through dequant_any and compared
    exactly, plus a row asserting that a bf16-sized tensor no longer takes the tile path;
  4. tools/safetensors_to_q4nx.py pack --dtype bf16 either stays and works, or refuses with a message naming this
    issue.

Found by measuring wqkvlin_/xn/in_norm_[0] at the layer-0 call site on the real pack; harness in
~/lb/d3parity/ (find_w.py, norm_cmp.py, w_run.sh, w_run_q41.sh). Tickets: one per run, host-GEMM, amdxdna
errors 0.

Activity

  1. added
    bug
    npu
    on Nov 3, 2026
  2. bong-water-water-bong commented on Nov 3, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Verified at tonight's pin (23d6972d9) — the mechanism is exactly as described, and the fix's risk is lower than the issue
    implies. bug + npu applied.

    The three lines that matter, confirmed

    npu/forward/src/q4nx_forward.cpp:160-168   dequant_any(): cb = nbytes / n, and only 5120/4736/8704 take the chunked path;
                                               everything else falls through to q4nx_dequant_tensor()
    npu/forward/src/q4nx_dequant.c:130-131     const size_t need = q4nx_tensor_bytes(rows, cols);
                                               if (need == 0 || nbytes < need) return -1;      <- a "<", not "!="
    npu/forward/src/q4nx_dequant.c:141-146     tile = data + (tr*ntc + tc)*Q4NX_TILE_BYTES;  q4nx_dequant_tile(...)  <- 5120 B, always

    So an oversized buffer is not refused, and there is no bf16 branch anywhere (git grep plain_bf16 npu/ → 0). The precedent
    the issue cites is real: q4nx_dequant_plain_i8 exists at npu/forward/src/q4nx_dequant.c:163 with its declaration at
    npu/forward/include/q4nx_dequant.h:106, used for Gemma4's plain-int8 tables — a bf16 sibling fits that shape exactly.

    Two additions to the fix shape

    1. Item 2 (make the size test exact) is safe at both call sites — I checked rather than assumed, because a != breaks any
      caller that passes a padded slice:
      q4nx_forward.cpp:167   dequant_any passes the tensor's real data_size
      q4nx_forward.cpp:2345  the per-layer-embedding path passes nbytes = band = q4nx_tensor_bytes(Q4NX_TILE_ROWS, cols)
                             (computed two lines above at :2343), i.e. already exact
      
      Those are the only two callers (git grep 'q4nx_dequant_tensor(' npu/ tools/). So need != nbytes → -1 is safe today and it
      is the part that closes the class: a container family nobody taught the decoder cannot be read as tiles again.
    2. Refuse and decode, in that order of value: the refusal alone makes #846's --dtype bf16 a loud failure instead of a
      silently wrong model, which is worth having even if the bf16 branch takes longer. If pack --dtype bf16 is meant to stay,
      then item 1 (the plain-bf16 branch) is the one that makes it work, and the ctest in item 3 should pin both directions —
      bf16 decodes exactly and a bf16-sized tensor no longer takes the tile path.

    The blast radius is bigger than #796, and it is worth writing into the issue

    Every number measured through a bf16 container is void, and there are more of them than the parity table: the d3 hidden-vector
    and per-layer rows measured on 11-02 are in the same category, and the layer-0 divergence they showed is now explained by
    this bug, not by a GDN defect
    — which also means #847's mask fix and #849's pairing fix must be re-scored on a q4_1
    container
    before anyone concludes they closed the parity gap. That ordering (refuse/bf16 decode → re-measure on q4_1 → then
    judge #849/#847) is the single most valuable line in this issue.

    Owner pi-npufeat (the #796 cluster, which this voids), bucket A. Host-side only: NPU_INFER_HOST_GEMM=1, no device,
    no ticket — the reproducer is tools/safetensors_to_q4nx.py pack --dtype bf16 on a small tensor plus the layer-0 comparison in
    ~/lb/d3parity/.

  3. bong-water-water-bong commented on Nov 3, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Closing note (issue keeper): closed by #882 → 0314f19a — and the test it shipped is the one the row asked for, run here

    #882's title carries (closes #875); GitHub closed this at 11:29:01Z. Verified device-free at 2fc5c4fd:

    gcc-16 -O1 -I npu/forward/include tests/npu_dequant_bf16_test.c npu/forward/src/q4nx_dequant.c npu/forward/src/q4nx_pack.c -lm
    ok   a rows*cols*2-byte tensor decodes       0 of 32768 values differ from the bf16 source
    ok   a tile-plus-2-byte buffer is refused
    ok   a short buffer is refused
    ok   the tile path yields the packed codes (1.0 / 3.0), so bf16 did not hijack it
    all cases ok            (10 cases)
    

    So both halves of the acceptance are covered by an executable check: decoded exactly when the buffer is bf16-sized, refused when it is off by two bytes (the nbytes < need → exact-size change at q4nx_dequant.c:131), and the q4_1 tile path still decodes tiles. The 3×5 case pins the no-alignment claim.

    The ordering item stands and is now the live one: every measurement taken through a pack --dtype bf16 container is void, and #847's 06:35Z re-run (engine-causal vs reference-causal worst |Δp| 0.6367) is the first set of numbers measured after this fix — so #796's parity rung is now scored on numbers that are not the mis-decode, and the remaining gap is a real forward difference, not a decoder artefact.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugnpu

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions