Skip to content

npu-pack: import can declare the device weight layout, so a b8 kernel set is one command away - #1217

Merged
bong-water-water-bong merged 5 commits into
mainfrom
1bit/npu-pack-weight-layout
Nov 6, 2026
Merged

bong-water-water-bong merged 5 commits into
mainfrom
1bit/npu-pack-weight-layout

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Root cause

A container served against the b8 (blocked-B) int8 kernel family must declare q4nx_weight_layout: "b8" in its
config.json — that key is what selects the _b8 / _b8_m16 ELF names and makes packB write the blocked image
host-side. Nothing in an OGA pack says that, because the layout is a property of the runtime the user picks, not of the
pack, so until now the only way to say it was to hand-edit the imported container's config.json.

That is the last manual step in the path this lane just validated end to end (Phi-3.5-mini OGA pack → import →
our b8 kernel set → 1bit serve --device npu), so it is worth closing:

1bit npu-pack import <pack> -o <dir> --weight-layout b8

The change

  • Options::weight_layout, written into the container's config.json as q4nx_weight_layout when it is not the
    default. Absent stays absent for the row-major family, so every container the importer has ever written is
    byte-for-byte unchanged without the flag.
  • weight_layout_refusal() accepts only what an import can actually produce a loadable container for: b8 (needs
    nothing else) and rm (no key). t2s and pb16i are refused by name with the reason — both also need
    q4nx_weight_layout_version and a directory of per-layer images, which an import cannot invent, so accepting them
    would write a container that refuses to load.
  • One summary line when the key is written, naming the kernel family the container now needs.

Measured

Device-free, on the Phi-3.5-mini pack (the same pack as #1209), then one NPU ticket:

$ 1bit npu-pack import ~/models/oga/Phi-3.5-mini-instruct_rai_1.8.0_npu_4K -o /tmp/phi-reimport --weight-layout b8
imported 291 tensors, 454392 q4_1 tiles into /tmp/phi-reimport
  container:  read back through q4nx::dequant_tile  rel_err 2.790e-03  max |diff| 8.545e-04
  layout:     declares q4nx_weight_layout "b8" -- this container needs the _b8 kernel family, and packB writes that image host-side
  attention:  6144/6144 .fconst tiles survive decode->encode byte for byte; ...
config.json -> q4nx_weight_layout = 'b8'

The import numbers are identical to the flag-less run (291 tensors, 454,392 tiles, 2.790e-03), so the flag changes
exactly one key.

$ 1bit serve -m /tmp/phi-reimport --device npu          # one npu-hold ticket, held 30 s, rc 0
1bit serve: /tmp/phi-reimport is a model-generic container: serving it through the per-op forward
[forward] weight layout: b8 blocked 8x8 microtiles, tile-n-major (kernels carry the _b8 suffix)
full ELF ok ×8
'The capital of France is' -> ' Paris.'
amdxdna ERROR 0 -> 0

So a pack directory plus one command now produces a container that the NPU route serves, with no hand editing.

Test

tests/npu_pack_import_test.cpp gains a device-free leg (weight_layout_refusal): absent/rm/b8 accepted,
pb16i/t2s/an unknown name refused, the unknown one refused by name. It runs under ctest's existing
npu_pack_import entry, which needs no pack (CI runs it as-is).

Refs #1189 (the pack whose kernel set needed this), not a close: #1209 is the close.

… set is one command away (refs #1189)

A container that will be served against the b8 (blocked-B) int8 kernel family must declare
`q4nx_weight_layout: "b8"` in its config.json; nothing in the pack says that, because the layout is a
property of the runtime the user picks, not of the pack. Until now the only way to say it was to edit
the imported container's config.json by hand.

   1bit npu-pack import <pack> -o <dir> --weight-layout b8

writes the key, and the absence of the key stays the row-major default every earlier container has.
The flag accepts only what an import can actually produce a loadable container for: b8 (nothing else
needed) and rm (no key). t2s and pb16i are refused by name with the reason -- both also need
q4nx_weight_layout_version and a directory of per-layer images, which an import cannot invent, so
accepting them would write a container that refuses to load.

Measured on the Phi-3.5-mini pack (device-free import, then one npu-hold ticket):
  import --weight-layout b8: 291 tensors, 454,392 tiles, rel_err 2.790e-03 -- the same numbers as
  without the flag -- and config.json carries q4nx_weight_layout "b8"
  1bit serve -m <dir> --device npu: 8/8 full ELFs, 'weight layout: b8 blocked 8x8 microtiles',
  'The capital of France is' -> ' Paris.', amdxdna ERROR 0, ticket held 30 s

Test: tests/npu_pack_import_test.cpp gains a device-free leg (weight_layout_refusal: b8/rm/absent
accepted, pb16i/t2s/unknown refused by name), run by ctest's npu_pack_import with no pack present.

@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 c321d662.

Device-free half re-derived end to end with my own build (Release, gcc-16) on the Phi-3.5-mini pack:

$ 1bit npu-pack import ~/models/oga/Phi-3.5-mini-instruct_rai_1.8.0_npu_4K -o /tmp/phi-reimport --weight-layout b8
imported 291 tensors, 454392 q4_1 tiles into /tmp/phi-reimport
  container:  read back through q4nx::dequant_tile  rel_err 2.790e-03  max |diff| 8.545e-04
  layout:     declares q4nx_weight_layout "b8" -- this container needs the _b8 kernel family, ...
config.json -> q4nx_weight_layout = 'b8'
  • The two load-bearing numbers are the body's exactly (291 tensors, 454,392 tiles, rel_err 2.790e-03, max |diff| 8.545e-04).
  • "Changes exactly one key" proven by diff, not by inspection: --weight-layout rm writes no key at all ('q4nx_weight_layout' in config.json -> False) and the pretty-printed configs differ by exactly one line, "q4nx_weight_layout": "b8",. So every container the importer wrote before is byte-identical without the flag.
  • The refusals are by name with the reason, which is the part that stops someone writing a container that will not load:
--weight-layout t2s   -> rc 1: t2s also needs q4nx_weight_layout_version (5 or 6) and a directory of per-layer t2s images; import cannot invent either
--weight-layout pb16i -> rc 1: pb16i also needs version 4 and a directory of per-layer pb16i images; import cannot invent either
--weight-layout bogus -> rc 1: unknown weight layout "bogus" (the containers the forward reads declare "b8" or nothing at all, ...)

Accepting t2s/pb16i because the forward reads them would have been the tempting mistake; refusing them with the missing obligation named is right.

Not re-derived: the 1bit serve -m /tmp/phi-reimport --device npu arm under the ticket (that is the device half, and it is yours). Note the import prints self-check: skipped -- the head is not tied to the embedding, which is a pre-existing property of this pack, not something this flag changes — worth one line in the body so the next reader does not think the flag skipped it.

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit 0d850a7)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

1189 - Partially compliant

Compliant requirements:

  • The PR introduces a mechanism to declare device weight layout, which is essential for correctly handling the b8 kernel set needed for Phi-3.5-mini
  • The PR adds validation to ensure only supported layouts are declared (--weight-layout b8 is allowed, t2s and pb16i are refused)
  • The PR ensures containers are byte-for-byte unchanged for existing behavior (default rm layout)
  • The PR adds proper error messages for unsupported layouts

Non-compliant requirements:

  • The PR does not directly address the core issue of untied head verification (the need for an anchor that covers the head when the head is untied)
  • The PR does not implement the suggested solutions for verifying untied-head packs (allow-unverified-head flag, cross-checking with prefill branch, or anchoring against upstream checkpoint)

Requires further human verification:

  • Whether the new --weight-layout b8 option actually resolves the Phi-3.5-mini import issue in practice
  • Whether the b8 kernel family correctly handles the Phi-3.5-mini model's weight layout on the hardware
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Layout Validation

The weight_layout_refusal function correctly validates that only 'b8' and 'rm' layouts are allowed, but it does not prevent the case where a user might specify a layout that is valid for the container but not for the actual hardware or runtime. This could lead to containers that are syntactically correct but semantically incorrect for the target device.

std::string weight_layout_refusal(const std::string& want) {
    if (want.empty() || want == "rm" || want == "b8") return "";
    if (want == "t2s")
        return "t2s also needs q4nx_weight_layout_version (5 or 6) and a directory of per-layer "
               "t2s images; import cannot invent either";
    if (want == "pb16i")
        return "pb16i also needs version 4 and a directory of per-layer pb16i images; import cannot "
               "invent either";
    return "unknown weight layout \"" + want + "\" (the containers the forward reads declare "
           "\"b8\" or nothing at all, which is the row-major default)";
}
Missing Hardware Verification

While the PR enables declaring the weight layout, it does not include any verification that the specified layout is actually compatible with the hardware or runtime environment. This could lead to containers being created that cannot be loaded or executed on the target device.

// The device weight layout, when the caller declared one (`--weight-layout b8`): the container
// then needs the `_b8` / `_b8_m16` kernel family and packB writes the blocked image host-side.
// The key stays absent for the default layout, exactly as every earlier container has it.
if (!opt.weight_layout.empty() && opt.weight_layout != "rm")
    c["q4nx_weight_layout"] = opt.weight_layout;

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit a717d30

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 33c9f6e

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit d56ed28

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 0d850a7

@bong-water-water-bong
bong-water-water-bong merged commit e6c1627 into main Nov 6, 2026
9 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/npu-pack-weight-layout branch November 6, 2026 04:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant