Skip to content

npu-pack: an imported container keeps the pack's fp32 scale/zero and stays servable, under layout q4nx-2 (closes #1185) - #1242

Merged
bong-water-water-bong merged 3 commits into
mainfrom
1bit/npu-pack-fp32-scales
Nov 6, 2026
Merged

bong-water-water-bong merged 3 commits into
mainfrom
1bit/npu-pack-fp32-scales

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Closes #1185. The imported container keeps the pack's own parameters, and it is still servable — the two halves have to land together, because either alone breaks the OGA import route (engine#671).

The defect

Our Q4NX stores per-32-column scale/zero as bf16; an OGA pack stores them as f32. On import ~half the groups lost their low mantissa bits: 2.7–3.3e-3 relative round-trip on all four packs (the bf16 2^-9 floor), even though the codes were exact. The importer printed the percentage, but nothing said the "scales exact" requirement was only met for codes.

What lands

Container layout q4nx-2 (npu/q4nx.{h,cpp}): the new Q4_1F chunk is the same 32×256 tile with the pack's fp32 scale[256] and zero[256] (6,144 B: fp32 parameters + the same 4-bit code regions at +2048). 1bit npu-pack import writes it and records layout: "q4nx-2" in the manifest and config.json; absent or q4nx-1 means the bf16 form every existing (GGUF-derived) container holds, and an unknown layout throws rather than guessing. The importer's line is now parameters: fp32 scale/zero exact for 100.00% of 32-column groups (container layout q4nx-2, the pack's own values; no bf16 round trip).

The device still streams bf16 q4_1, so the imported container stays servable. npu/q4nx_device.{h,cpp} (new) converts a q4nx-2 directory into the Q4NX cache — every 6,144-B I8 tensor through as_q4_1(), other tensors copied, config + source size/mtime stamp, atomic rename, reused on later loads, logged once; a GGUF-derived container is returned untouched with nothing written. Q4nxNpuForward::init_impl calls it right before model_load, and the fast lane's packers (npu/pack.cpp device_tiles()) do the same repack in memory, so both device builders accept an imported dir. npu/forward/src/model.c keeps its q4nx-2 refusal as a safety net for a raw container handed straight to it.

Structural checker: tools/check_q4nx_dir.py accepts and counts the q4_1f chunk (unknown-size case moved to 8,192), so an imported directory no longer reads as an unknown chunk size.

Evidence (device-free, in the CI target npu_pack_import)

  • Crafted pack with the 0.5B's measured .fconst geometry + MatMulNBits head: 34,048/34,048 per-32-group scale/zero pairs bit-equal to the pack's f32 values, codes unchanged, q4nx_rel = max = 0; the old bf16 form loses 17,916 of them.
  • Imported q4nx-2 container converted to the device form: 22 I8 tensors / 1,052 tiles repacked; the converted q_proj/down_proj/lm_head tiles are byte-identical to the old bf16 container's tile_q4_1 output — which is the npu: load Ryzen AI OGA NPU model packs on Linux through the engine (pack import -> Q4NX -> our kernels) #671 route's proof at the tile level; second load reuses the cache; an already-device-form input is unchanged.
  • npu_forward_loader_test: a raw q4nx-2 container is refused, the converted 5,120-B form loads.
  • Default build: npu_pack_import_test tests/golden all pass; NPU ctest subset 14/14 (also built onebit_npu_forward on an ONEBIT_NPU configure with the stub runtime); copyright --check clean.

Not verified here, and the plan

No OGA pack and no NPU device on this box, so the issue's four-pack table is not re-measured in this PR and no device run happened. The real-pack leg (npu_pack_import_test tests/golden <pack dir>) and a strixhalo 1bit serve -m <imported dir> --device npu are the follow-ups; the pack leg is device-free and is being run on strixhalo against ~/models/oga/ before merge, with the numbers added to this thread.

Reviewer notes

  1. Container bytes grow 20 % for imported packs (6,144 vs 5,120 per tile); GGUF-derived containers are unchanged.
  2. The conversion cache is keyed on source size+mtime and lives in the existing Q4NX cache ($ONEBIT_Q4NX_CACHE, else ~/.cache/1bit/q4nx), so the first serve of an imported dir pays one copy.
  3. The fast-lane path reuses as_q4_1() in memory rather than adding a second repacker; model.c's refusal is now defensive, not the operator's path.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Real OGA packs on strixhalo (device-free), this head

Built npu_pack_import_test from this branch on strixhalo (~/wt/issue-1185-check, cmake + gcc-16, no device/ticket needed) and ran the test's pack leg against the box's six ~/models/oga/* entries:

pack result
Llama-3.2-1B-Instruct_rai_1.8.0_npu_4K pass, including the pack leg
Qwen2.5-0.5B-Instruct_rai_1.8.0_npu_4K pass, including the pack leg
Qwen3-1.7B_rai_1.8.0_npu_4K pass, including the pack leg
Qwen3-4B_rai_1.8.0_npu_4K pass, including the pack leg
Qwen2.5-0.5B-Instruct_rai_1.8.0_hybrid skipped: no dd_metastate_*.fconst — the hybrid/model.bin flavour, not an OGA NPU pack this importer reads
Phi-3.5-mini-instruct_rai_1.8.0_npu_4K FAIL pack leg: the decode reproduces the fp16 embedding (corr 0) — pre-existing: the same test built from origin/main (de0256ae, same cmake/gcc-16) fails identically on this pack. That is the phi3 untied-head class of engine#1189, not this change.

So the four packs the issue's table measured now pass through the real importer with the container on q4nx-2; the fp32-exactness assertion itself is the crafted leg (34,048/34,048 per-32-group pairs bit-equal, where the bf16 form loses 17,916), and the real pack legs exercise the whole import path end to end.

Still not done here: a device run of 1bit serve -m <imported dir> --device npu on strixhalo (needs the NPU and a hold ticket). The init-time conversion is covered by npu_pack_import_test/npu_forward_loader_test, and the converted tiles are byte-identical to the old bf16 container's tile_q4_1 output — but the served route itself has not been executed.

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

Labels

None yet

Projects

None yet

1 participant