Repository navigation
npu-pack: an imported container keeps the pack's fp32 scale/zero and stays servable, under layout q4nx-2 (closes #1185) - #1242
Conversation
…er layout q4nx-2 (closes #1185)
Real OGA packs on strixhalo (device-free), this headBuilt
So the four packs the issue's table measured now pass through the real importer with the container on Still not done here: a device run of |
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 newQ4_1Fchunk is the same 32×256 tile with the pack's fp32scale[256]andzero[256](6,144 B: fp32 parameters + the same 4-bit code regions at +2048).1bit npu-pack importwrites it and recordslayout: "q4nx-2"in the manifest andconfig.json; absent orq4nx-1means the bf16 form every existing (GGUF-derived) container holds, and an unknown layout throws rather than guessing. The importer's line is nowparameters: 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 throughas_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_implcalls it right beforemodel_load, and the fast lane's packers (npu/pack.cppdevice_tiles()) do the same repack in memory, so both device builders accept an imported dir.npu/forward/src/model.ckeeps its q4nx-2 refusal as a safety net for a raw container handed straight to it.Structural checker:
tools/check_q4nx_dir.pyaccepts and counts theq4_1fchunk (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).fconstgeometry + 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.q_proj/down_proj/lm_headtiles are byte-identical to the old bf16 container'stile_q4_1output — 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.npu_pack_import_test tests/goldenall pass; NPU ctest subset 14/14 (also builtonebit_npu_forwardon anONEBIT_NPUconfigure with the stub runtime);copyright --checkclean.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 strixhalo1bit serve -m <imported dir> --device npuare 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
$ONEBIT_Q4NX_CACHE, else~/.cache/1bit/q4nx), so the first serve of an imported dir pays one copy.as_q4_1()in memory rather than adding a second repacker;model.c's refusal is now defensive, not the operator's path.