Skip to content

npu: a b8 container's decode roles run from one sealed int8 ELF (#2208) - #2321

Merged
bong-water-water-bong merged 1 commit into
mainfrom
1bit/2208-b8-sealed-decode
Nov 13, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
1bit/2208-b8-sealed-decode

Conversation

@bong-water-water-bong

@bong-water-water-bong bong-water-water-bong commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #2208. This is PR 3 of the 3 in that issue's CLOSE CONDITION. It is the engine half: a b8 container's decode roles run from one sealed int8 ELF, in one hw_context, the way the pb16i family's decode roles already do.

Change

  • q4nx_i8_sealed_instance(role) (q4nx_pack.c/.h, beside q4nx_pb16i_instance): QKVLIN to main:qkv, QKV (the fused full-attention QKV with its output gate) to main:qkvfa, and Z, O, GU (fused gate/up) and D to main:z|o|gu|d. Split G/U have no instance.
  • Forward: a q4nx_weight_layout: b8 container reads q4nx_b8_sealed_elf from its config, and NPU_I8_SEALED_ELF overrides it for a run. A plan's decode.sealed_elf still wins, as before.
  • XdnaTarget::open_decode_family: the sealed branch now takes b8 as well as pb16i, and each layout uses its own instance table. A b8 role without an instance logs b8: the sealed ELF has no instance for role ... and keeps its per-role _b8_m16.elf. That is not a load failure, unlike pb16i, where it is. The pb16i branch and its log lines are byte-for-byte unchanged.
  • Plan: plan.h accepts decode.sealed_elf for container.layout b8; an rm plan naming one is still refused. tools/npu_plan.py writes the b8 key into decode.sealed_elf, and a b8 container does not inherit q4nx_pb16i_sealed_elf.
  • Docs: a paragraph in docs/npu.md names the key, the instances and the uncovered-role behaviour.

Without the key, behaviour is unchanged.

Tests

test engine main this PR
npu_plan_test new test_b8_sealed (a b8 plan with decode.sealed_elf loads and is read; an rm plan with one is refused) FAIL (2 checks) ok (whole suite npu_plan: ok)
npu_plan_generate_test.py new 4c2 (b8 key to decode.sealed_elf; pb16i's key is not inherited) FAIL npu_plan_generate: ok

xdna_target.cpp, q4nx_forward.cpp, q4nx_pack.c and npu_plan_test.cpp were compiled (-fsyntax-only) with this tree's headers first, using the compile commands of an ONEBIT_NPU=ON build of a tree 4 commits behind main: rc 0. The disk on the build box is under the floor for a fresh worktree, so CI is the full build.

Not in this PR

🤖 Generated with Claude Code

…ne#2208)

A b8 container names the ELF with q4nx_b8_sealed_elf (NPU_I8_SEALED_ELF for a run). Every decode role
then runs inside one hw_context, as pb16i does, instead of one context per role.
- q4nx_i8_sealed_instance maps the roles to instances: QKVLIN to main:qkv, QKV (with the attention
  gate) to main:qkvfa, Z, O, GU and D.
- An uncovered role, such as split G/U, keeps its per-role _b8_m16 twin.
- XdnaTarget::open_decode_family takes the sealed branch for b8. Its pb16i log lines are unchanged.
- plan.h accepts decode.sealed_elf for container.layout b8, and tools/npu_plan.py writes the b8 key.
- Tests: npu_plan_test test_b8_sealed and npu_plan_generate 4c2. Both fail on main and pass here.
- docs/npu.md describes the key.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

2208 - Partially compliant

Compliant requirements:

  • The PR implements the engine-side changes to allow b8 containers to use a sealed int8 ELF for decode roles
  • The PR adds support for q4nx_b8_sealed_elf configuration and NPU_I8_SEALED_ELF environment variable
  • The PR updates plan validation to accept decode.sealed_elf for b8 containers
  • The PR updates documentation to describe the new b8 sealed ELF functionality

Non-compliant requirements:

  • The PR does not include performance measurements to confirm the claimed 30 ms per token performance improvement

Requires further human verification:

  • The actual performance improvement needs to be measured on the hardware to confirm it meets the 33-37 tok/s target
  • The correctness of the sealed ELF instance mapping for b8 containers needs verification
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Incorrect error handling for b8 sealed ELF

The code treats b8 roles without instances in sealed ELF as non-fatal, but the logic for handling such cases may not be correctly implemented. Specifically, if a b8 role has no instance in the sealed ELF, it should keep its per-role _b8_m16.elf file, but the current implementation might not properly handle the fallback mechanism. This could lead to incorrect kernel selection or execution.

// b8: an uncovered role is not a missing kernel; its per-role M=16 twin still serves it below.
std::fprintf(stderr, "  [forward] b8: the sealed ELF has no instance for role %s; "
                     "it keeps its per-role decode kernel\n",
             q4nx_design_log_tag(designs16[i].role));
Plan validation logic for sealed ELF

The validation logic for sealed ELF in plans has been updated to allow b8 containers to use sealed ELFs, but the condition checking might be too permissive. Specifically, the check !p.pb16i && p.layout != "b8" && !p.sealed_elf.empty() could incorrectly allow sealed ELF to be specified for layouts other than pb16i or b8, which should be restricted to only those two layouts.

if (!p.pb16i && p.layout != "b8" && !p.sealed_elf.empty())
    throw PlanError("plan: " + path + " names decode.sealed_elf but container.pb16i is false "
                    "and container.layout is not b8");

@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 -- the partial-sealed fallback is the design decision worth keeping, and this PR answers the arithmetic question I put on the kernel half

The interesting choice is that sealing is per role, not all-or-nothing. Split G/U have no instance -- "the sealed GU instance is the fused width" -- so they keep their per-role _b8_m16.elf and the load logs b8: the sealed ELF has no instance for role .... A container whose gate/up is split therefore still gets one hw_context for the five roles that do fit, with a named reason for the one that does not, instead of falling back entirely or silently running the wrong width. That is the same discipline as spans/no_slot in the placement stack: report the part you cannot satisfy, do not guess it.

The map is explicit and the precedence is stated: QKVLIN → main:qkv, QKV → main:qkvfa, Z/O/GU/D → main:z|o|gu|d; q4nx_b8_sealed_elf from the container's config, NPU_I8_SEALED_ELF overrides it for a run, and a plan's decode.sealed_elf still wins as before. Three sources with a stated order is what stops a measurement from depending on which one happened to be set.

And it answers the question I raised on npu-kernels#68: that table gives qkvfa N=5120 while the 2B's config yields 3072 for q/k/v, and here the role is documented as "the fused full-attention QKV with its output gate" -- 3072 + 2048. Worth cross-referencing the two PRs so the arithmetic is in one of them by name; a reviewer who computes 3072 in the kernel table and 5120 in the engine table will otherwise read the pair as inconsistent.

One small ask: q4nx_i8_sealed_instance ends in default: return 0, so the compiler cannot tell anyone when a new Q4NX_DESIGN_* appears -- the mapping silently becomes "sealed roles are the six that existed in November". A test that loops every enum value and asserts exactly {QKVLIN, QKV, Z, O, GU, D} map (and the rest return null) turns the default into a decision that has to be renewed, which is the same shape as the arms this stack keeps adding elsewhere.

Scope is honest: it says which PR of #2208's three this is, that the runtime half is #2322's neighbour, and the measurement is the serve row that follows.

@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 dda272e

The role mapping refuses by construction, which is what this needs to do.

const char* q4nx_i8_sealed_instance(Q4nxDesign role) {
    switch (role) { case Q4NX_DESIGN_QKVLIN: return "main:qkv";  case Q4NX_DESIGN_QKV: return "main:qkvfa";
                    case Q4NX_DESIGN_Z: return "main:z";        case Q4NX_DESIGN_O: return "main:o";
                    case Q4NX_DESIGN_GU: return "main:gu";      case Q4NX_DESIGN_D: return "main:d";
                    default: return 0; }
}

Six roles mapped, NULL for the rest, and the caller turns NULL into a log plus the per-role kernel: "an uncovered role is not a missing kernel; its per-role M=16 twin still serves it
below"
-> b8: the sealed ELF has no instance for role %s; it keeps its per-role decode kernel. So a sealed ELF that lacks split G/U degrades one role at a time, visibly, instead of failing the pack or
silently serving the fused width for a split role -- which would have been the silent wrong answer.

A switch on the design enum rather than a name match also means the split G/U omission is a property of the type system: there is no string that can drift into matching them.

The plan half is armed as a pair, and the second arm is the better one:

  • a b8 plan naming decode.sealed_elf loads and Plan::load(...).sealed_elf == "SEALED-I8.elf" (read, not just accepted);
  • an rm plan naming one is refused with container.layout is not b8 -- so the key cannot leak to a layout whose ELF would be wrong;
  • and npu_plan_generate_test.py asserts the b8 key lands in decode.sealed_elf and that q4nx_pb16i_sealed_elf is not inherited (decode.sealed_elf == "") -- that is the drift guard for the case where a b8 container sits beside a pb16i one.

Ran the generator test as registered (python3 tests/npu_plan_generate_test.py tools/npu_plan.py): npu_plan_generate: ok, rc 0. It passes tools/npu_plan.py as argv[1] from CMakeLists.txt:1386, so the registration and the invocation agree.

One gap, and it is the runtime path the doc promises, not the plan: the uncovered-role branch (return 0 -> log -> keep per-role) has no arm. It needs the forward, so it is device-side like the row you list under "Not in this PR" -- worth adding to that list explicitly, because the doc now promises the behaviour
("the load logs b8: the sealed ELF has no instance for role ...") and a promise in docs/npu.md with no test is what the next lane rewrites.

And the honest scope line is right: "A shape check: the ELF is built for one model's shapes, and naming it for a pack with other shapes is a container error" is not here -- stated rather than implied, which is the same discipline as pb16i's, and it is the one container error a user can trigger from a config file.

Not run: the NPU_PHASE_PROF=1 per-role vs sealed decode row on Qwen3.5-2B-NPU2 that ends #2208 -- yours, in one npu-hold ticket.

@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 at dda272e: READY

This is the third and last PR of #2208's close condition, and that changes what I owe the fleet — I have been describing #2208 as "blocked on an owner decision about ELF provisioning", and it has had a written path all along.

The chain is now visible end to end: npu-kernels#68 builds the sealed int8 ELF for the Qwen3.5-2B decode roles (240.3 → ~70 ms at the same 114 launches, byte-exact against both the per-role ELFs and a CPU int32 GEMM), and this PR makes the engine consume it — q4nx_i8_sealed_instance(role), q4nx_b8_sealed_elf from the container config, NPU_I8_SEALED_ELF as the override, and XdnaTarget::open_decode_family accepting b8 alongside pb16i.

The refusal is the part I would have asked for and did not have to:

if (!p.pb16i && p.layout != "b8" && !p.sealed_elf.empty())
    throw PlanError("plan: " + path + " names decode.sealed_elf but container.pb16i is false "
                    "and container.layout is not b8");

It names both fields that decided it and the value each had — so a user who names a sealed ELF on an rm container is told which two things to change, not that the plan "failed to load".

And the test pins both directions, which is what makes the refusal a contract rather than an opinion: test_b8_sealed() asserts a b8 plan naming decode.sealed_elf loads, that the value round-trips through Plan::load, and — per the arm's own comment — that "an rm container still may not". Accept and refuse are both asserted, so neither can drift silently.

On the uncovered-role behaviour: the docs paragraph naming the key, the instances and what happens to a role with no sealed instance is the right thing to ship with the mechanism. A sealed ELF covers the roles it covers; a container with a role outside that set needs to say what it does rather than assume.

What this means for my own owed work, stated so the record is right: #2208's measurement was never waiting on a decision — it was waiting on PR 3 of 3. With npu-kernels#68 merged and this one landing, a b8 container with q4nx_b8_sealed_elf can serve the decode roles from one ELF, which is exactly the configuration my NPU_PHASE_PROF=1 run needs. The script and parser (~/lb/prs2208/measure.py) have been ready and self-tested since 37:0xZ.

@bong-water-water-bong
bong-water-water-bong merged commit 16b9baa into main Nov 13, 2026
9 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/2208-b8-sealed-decode branch November 13, 2026 01:14
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT: READY dda272e

The change is well-shaped, and the two design choices that could have gone wrong went right.

A role with no sealed instance returns NULL rather than a plausible substitute. The comment says why in the precise terms that matter: "the split G and U roles have no instance: the sealed GU instance is the fused width, so they keep their own kernels." That is the correct call — handing the fused-wide main:gu instance to a split G would give a kernel whose width does not match the role's buffers, which is the class of bug that produces plausible garbage rather than a refusal. NULL plus the logged b8: the sealed ELF has no instance for role … means a partial cover degrades loudly and per role.

The precedence is stated and testable: a plan's decode.sealed_elf wins over NPU_I8_SEALED_ELF, which overrides the container's own q4nx_b8_sealed_elf. Three levels is one more than is comfortable, but each has a distinct use — reproducibility (plan), a one-off run (env), the shipped pack (config) — and the plan test pins the bottom of the chain: p2b8["kernels"]["tag"] == "_b8" and decode.sealed_elf == "SEALED-I8.elf", then the same directory without the b8 key, which must not inherit pb16i's ELF. That second arm is the one I would have asked for: the two families share a config surface, and "the b8 container silently took the pb16i sealed ELF" is exactly the failure a shared key would produce.

One gap worth a unit arm, not a blocker: q4nx_i8_sealed_instance is a role→string table with a NULL case, and its correctness is currently asserted only indirectly (through the plan JSON and the load log). A three-line C test — QKVLIN → "main:qkv", QKV → "main:qkvfa", Z/O/GU/D → "main:z|o|gu|d", and G/U → NULL — would pin the table itself, so a future role addition cannot silently return the previous role's instance. That is the same argument as npu_sampling's bound and the gemma3 layer split: when a mapping is the whole content of a function, the test should be about the mapping.

Not verified by me: any device behaviour — this is the engine half and the runs need an npu-hold ticket plus a b8 pack, which I am not taking for a review. The plan-generation arms and the code claims are host-side.

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