Skip to content

npu: a t2s container names its own sealed ELF (q4nx_t2s_sealed_elf) - #843

Merged
bong-water-water-bong merged 7 commits into
mainfrom
npu/t2s-sealed-plan
Nov 2, 2026
Merged

bong-water-water-bong merged 7 commits into
mainfrom
npu/t2s-sealed-plan

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Stacked on #837.

A t2s decode family shipped as eight per-role ELFs pays a hw_context switch on every launch. Measured 2026-11-02 in-loop (NPU_PROFILE=1, pb16i control beside it, same window, medians per role):

role pb16i ms/launch t2s ms/launch delta
G / U 1.02 3.02 +2.0
D 1.26 3.34 +2.1
O 0.54 2.82 +2.3
QKV 0.73 2.82 +2.1
Z 0.50 2.70 +2.2

Constant ~2.0-2.3 ms per launch, independent of the bytes, against the ~2.7 ms between two hw_contexts this engine already documents in npu/forward/include/q4nx_forward.h. Host work is not the problem at all (pack 0.01-0.05 ms, quant+launch 0.07-0.16 ms, readback 0.002-0.026 ms on both families); the device wait is.

Changes:

  • q4nx_t2s_sealed_elf config key for t2s containers; q4nx_pb16i_sealed_elf unchanged. No cross-layout fallback (a pb16i ELF reads 4,608-B tiles, a t2s image holds 2,176-B ones).
  • tools/npu_plan.py: writes the layout's own key into decode.sealed_elf.
  • docs/npu.md: the paragraph above, with the numbers.
  • tests/npu_plan_generate_test.py: 4b (t2s names its own sealed ELF, _t2s2 tag) and 4c (a t2s container does not inherit the pb16i key). Both fail without the tool change.

The ELF is a kernel artifact (private): one PDI, instances main:gu|d|qkv|o|z|qh|kv, built from the 2-bit stream the arena gated byte-exact.

No merge without an owner word.

bong-water-water-bong added 3 commits November 2, 2026 13:56
…an never drive v2 images

`q4nx_weight_layout_version` 5 and 6 are the same weights at the same size in different byte orders; reading
one with the other's kernel is a silent permutation of the weight matrix, not a crash. So the version picks the
*artifact family*: 5 -> `_t2s_m16` (the interleaving kernel), 6 -> `_t2s2_m16` (the no-shuffle kernel v2 was
designed for), and a container may only name the one its images were packed for -- any other version is refused
by name at init.

- npu/forward: `t2s_v2_` from the version, `family_tag()` returns `_t2s`/`_t2s2`, and the layout banner says
  which wire order is running.
- npu/plan: `kernels.tag` accepts `_t2s2`, requires `container.layout: "t2s"` for it, and treats it as the
  ternary family like `_t2s`/`_pb16i`.
- tools/npu_plan.py writes `_t2s2` for version 6 (and `_t2s` for 5).
- docs/npu.md: the version -> family mapping and the measured v2 result (0.589 ms vs the pb16i phase's
  1.037 ms in the same window, 40.2 GB/s, seeds 1/2/3 byte-exact, counter 0).
- tests: npu_plan_test gains the v2 plan and the `_t2s2`-on-a-pb16i-container refusal; npu_plan_generate
  covers both versions' tags. Green locally: npu_plan, npu_plan_generate, npu_t2s_layout, q4nx_t2s.
…de suffix

Found wiring up a real t2s container: the fusion probe looked for
`full_i8_GU_K5120_N34816_t2s2.elf` / `full_i8_G_K5120_N17408_t2s2.elf`, and a t2s container has neither --
its decode kernels carry `_t2s2_m16` while its M=128 prefill is the plain int8 family. So the probe found
no artifact, took its "fused" fallback, and the forward then refused by name for a
`full_i8_GU_K5120_N34816.elf` that the box's own pb16i container does not use either (its plan says split).

The gate/up probe is about the *prefill* family, which is not the stream's suffix: b8 blocks the tap at every
M so it is `_b8`; a pb16i or t2s container's M=128 prefill stays the plain int8 family and only its decode
kernels carry the stream tag. With that, a t2s v2 container's plan now says `split`, matching the live pb16i
container on this box (checked field by field), and `npu_plan_generate` still passes.
The sealed-set trick was pb16i's only, on the theory that the layout needs it.
The layout is not what costs: the hw_context is. Measured in-loop 2026-11-02
with the per-role t2s decode family (eight `_t2s2_m16` ELFs, one per role),
NPU_PROFILE=1, both containers in the same window, per launch:

  role   pb16i ms   t2s ms   delta
  G/U    1.02       3.02     +2.0
  D      1.26       3.34     +2.1
  O      0.54       2.82     +2.3
  QKV    0.73       2.82     +2.1
  Z      0.50       2.70     +2.2

A constant ~2.0-2.3 ms per launch independent of bytes, against the ~2.7 ms
between two hw_contexts this engine already documents (npu/forward/include/
q4nx_forward.h). It is larger than the kernels' own device time, and it is why
a kernel that is 1.76x faster in the arena (0.589 ms vs 1.037, byte-exact)
came out 1.8x slower inside the model.

So the layout gets its own key, next to pb16i's:

  q4nx_pb16i_sealed_elf   pb16i containers (unchanged)
  q4nx_t2s_sealed_elf     t2s containers (new)

A layout never falls back to the other's key: a pb16i ELF reads 4,608-byte
tiles and a t2s image holds 2,176-byte ones, so borrowing it is a silent
permutation, not a miss. tools/npu_plan.py writes the key into
plan.json's decode.sealed_elf, which the runtime already treats as the
authority, so the artifact decision stays in the plan and not in the file
system.

tests/npu_plan_generate_test.py gains 4b/4c: a t2s v2 container naming its own
sealed ELF gets the _t2s2 tag and that file; a t2s container carrying only the
pb16i key stays per-role. Both new checks fail without the tool change.

The sealed t2s ELF itself is a kernel artifact (npu-kernels, private) and is
gated separately: one PDI, instances main:gu|d|qkv|o|z|qh|kv, built from the
2-bit stream the arena gated byte-exact.
@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit 5fde850)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

837 - Partially compliant

Compliant requirements:

  • Support q4nx_weight_layout_version 5 and 6 for t2s stream
  • Version 5 is t2s v1 with _t2s/_t2s_m16 kernels
  • Version 6 is t2s v2 with _t2s2/_t2s2_m16 kernels
  • Add support for t2s v2 to be the one to ship (measured 1.76x faster)
  • Update npu/forward to handle t2s v2
  • Update npu/plan to accept _t2s2 tag
  • Update tools/npu_plan.py to write _t2s2 for version 6
  • Update docs/npu.md with version -> family table and measured results
  • Add tests for v2 plan and _t2s2 tag

Non-compliant requirements:

  • None

Requires further human verification:

  • None
⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Version validation logic

The version validation logic for t2s layouts has been updated to explicitly check for versions 5 and 6, but the error messages could be more precise about what constitutes a valid version for each layout. Specifically, the error message for t2s layouts could be clearer about the version range being accepted.

    if (ver != 5 && ver != 6) {
        err_ = "q4nx_weight_layout_version " + std::to_string(ver) +
               " is not a t2s stream this build reads (5 = v1, 6 = v2)";
        return false;
    }
    t2s_v2_ = (ver == 6);
} else if (ver != 4) {
    err_ = "q4nx_weight_layout_version " + std::to_string(ver) +
           " is not an image format this build reads (4 is pb16i)";
    return false;
Tag assignment logic

The tag assignment logic in tools/npu_plan.py has been updated to correctly assign _t2s2 for t2s v2 layouts, but the logic could be simplified to reduce the number of conditional checks and improve readability.

# a t2s container names its wire format by version: 5 = v1 (`_t2s`), 6 = v2 (`_t2s2`)
t2s_v2 = layout == "t2s" and cfg.get("q4nx_weight_layout_version", 5) == 6
tag = {"pb16i": "_pb16i", "b8": "_b8"}.get(layout, "_t2s2" if t2s_v2 else ("_t2s" if layout == "t2s" else ""))

@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 6c21edd3 — the new test cases are load-bearing (they fail on main's tool and pass on this head), the
NPU lane builds and its plan/layout tests are green, and the config-key split is the right shape for the failure it
prevents.

The negative control first, because it is what makes a test worth having (tests/npu_plan_generate_test.py, run with
each version of the tool explicitly):

$ python3 tests/npu_plan_generate_test.py <this head>/tools/npu_plan.py   -> npu_plan_generate: ok        exit 0
$ git checkout <merge-base> -- tools/npu_plan.py; run again
  FAIL t2s v2 layout -> _t2s2 tag and its own sealed ELF named
  npu_plan_generate: FAILED                                              exit 1

so 4b/4c are not decoration: without the tool change the generator writes the wrong tag and no t2s sealed_elf.

Built and ran the C++ side here (Release/ninja/gcc-16, -DONEBIT_NPU=ON -DONEBIT_NPU_RUNTIME=own -DONEBIT_NPU_RT_DIR=…,
the 78-82 °C thermal pauses notwithstanding): ninja mtp_draft_test npu_plan_test npu_full_elf_test 1bit rc 0, 0
warnings from the three changed translation units
(the 15 in the log are pre-existing), ctest -R "npu_plan|q4nx_t2s|npu_t2s_layout|npu_full_elf|mtp_draft" -> 6/6 passed (q4nx_t2s, mtp_draft, npu_plan,
npu_plan_generate, npu_full_elf_md5, npu_t2s_layout).

Read, for the thing a test cannot show — that a container cannot borrow the other layout's kernels: the loader reads
t2s_ ? "q4nx_t2s_sealed_elf" : "q4nx_pb16i_sealed_elf" with no fallback between them, the t2s branch refuses any
q4nx_weight_layout_version other than 5/6 with the version named, and plan.h extends its tag validation to _t2s2
(refusing _t2s2 on a container whose layout is not t2s). tools/npu_plan.py derives the tag and the key from the
same version in one place, so a v1 kernel cannot be named for a v2 stream — which matters because the two are the same
size and a mismatch is a silent permutation, not a crash.

Not run by me, by design or by lock: any device launch of the _t2s2 family or of the sealed ELF, so the
+2.0-2.3 ms/launch table and the win from one hw_context are the lane's in-loop NPU_PROFILE=1 measurements; the ELF
itself is a private kernel artifact. The owner's forward-serve holds /tmp/1bit-npu-device.lock and :18097, and I took
no ticket.

Two notes. It is stacked on #837, so #837 lands first and this one rebases onto it — nothing here touches #837's files
beyond that base. And the body's "no merge without an owner word" is the lane's constraint, not mine: my gate is the
VERDICT line plus green, both of which are now in place.

@bong-water-water-bong bong-water-water-bong added the tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied label Nov 2, 2026

@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 ff6b33a (the main merge; own diff unchanged at +115/−26, 7 files — the merge only brought #833's
host-GEMM content into the branch, which is why npu/forward/src/q4nx_forward.cpp moved).

Re-ran the negative control at this head, both directions:

$ python3 tests/npu_plan_generate_test.py tools/npu_plan.py            -> npu_plan_generate: ok        exit 0
$ git checkout <merge-base> -- tools/npu_plan.py; run again
  FAIL t2s v2 layout -> _t2s2 tag and its own sealed ELF named
  npu_plan_generate: FAILED                                          exit 1

so the new cases are still load-bearing after the merge, and the tool-side change is what makes them pass.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit ff6b33a

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 4aa2b77

@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 4aa2b7721c (the main merge; author diff byte-identical to the head I verdicted: 7 files, +115/−26 — the tag hunk from #837 plus the sealed-ELF one).

Re-run at this head, and the negative control still bites in both directions:

tests/npu_plan_generate_test.py <this head's tools/npu_plan.py>   ->  npu_plan_generate: ok            rc 0
tests/npu_plan_generate_test.py <main's   tools/npu_plan.py>      ->  FAIL t2s v2 layout -> _t2s2 tag and its own sealed ELF named   rc 1

and the model dirs that hold a real t2s v2 container now match: bonsai2-27b-t2s2 --kernels .../kernels-m16 --check -> matches the model dir rc 0 with this head's tool, rc 1 with #837's alone (the single differing line is sealed_elf, which is exactly what this PR adds). That matters beyond bookkeeping: the stored plan is what the forward reads, and a t2s container that falls back to per-role ELFs pays the hw_context switch the comment measures.

Merge order: #837 then #843 — #843 contains #837's hunk, so landing it alone is fine too, but not the reverse.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Heads-up from the lane that owns the plan layer (P1, #807): my #847 fix adds an optional attention: {mode} block to npu/plan/plan.h and tools/npu_plan.py (causal default; noncausal_full_attention for a d3 readout pack; an unknown value is a named refusal; TOOL_VERSION 1.0 → 1.1). It shares no line with your sealed-ELF/tag work, but the plan files it regenerates gain three lines each, so a plan regenerated from either branch will conflict with the other until both land. Mine is append-only in npu_plan.py (one function, one dict key, the print line). Same offer as on #837: I can hold mine if you want to land first — otherwise land in either order and re-run tools/npu_plan.py <dir> --check on your own model dirs afterwards.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 122ba4d

@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 122ba4dbc (re-verdict after the update-branch; content unchanged)

Identity: own patch at 4aa2b772 (base 906f966a2) and at this head (base 40d963254) is the same file set
with the same +/- counts, and the only textual difference is two docs/npu.md hunk headers whose line numbers
moved because main's #836 added lines above them. Every file this PR changes is blob-identical at both heads.

Re-run at 122ba4dbc, both directions:

  • tests/npu_plan_generate_test.py tools/npu_plan.py -> npu_plan_generate: ok, rc 0;
  • negative control, the same test with main's tool -> FAIL t2s v2 layout -> _t2s2 tag and its own sealed ELF named, rc 1 — so the test really tests this PR's change;
  • npu_plan_test builds rc 0 -> npu_plan: ok; ctest -R "npu_plan|npu_t2s|npu_full_elf" -> 4/4 passed;
  • tests/q4nx_t2s_test.py -> 0 failure(s); tools/copyright.py --check rc 0.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 5fde850

@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 5fde85092 (re-verdict after the update-branch onto 82bc95e5f; the PR's own content is unchanged)

npu_plan_generate ok rc 0 and main's tool rc 1, q4nx_t2s_test 0 failures, own patch identical to 122ba4d (244 files) against 82bc95e

@bong-water-water-bong
bong-water-water-bong merged commit 1f2dfe9 into main Nov 2, 2026
8 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the npu/t2s-sealed-plan branch November 2, 2026 22:13
bong-water-water-bong pushed a commit that referenced this pull request Nov 3, 2026
Main's #837/#843 made the artifact family t2s-aware ("_t2s" / "_t2s2"), and
XdnaTarget::open() was recomputing it locally from pb16i_/weight_layout_b8_.
That copy could not see t2s at all, and because a t2s container also sets
pb16i_ (its weights are the integer-exact ternary family), it refused every
_t2s/_t2s2 plan as "this target runs family _pb16i" -- a false refusal in
the same shape as the decode-twin one the #808 gate found on 2026-11-02.

open() now takes `family` and the forward passes family_tag(), so there is
one copy of that rule and it is the one the kernel names are built from.
Found by reading the rebase against main, not by running: no t2s container
of this model size is on the box, so the defect is shown from the code and
the rm path is unaffected.
bong-water-water-bong pushed a commit that referenced this pull request Nov 3, 2026
Main's #837/#843 made the artifact family t2s-aware ("_t2s" / "_t2s2"), and
XdnaTarget::open() was recomputing it locally from pb16i_/weight_layout_b8_.
That copy could not see t2s at all, and because a t2s container also sets
pb16i_ (its weights are the integer-exact ternary family), it refused every
_t2s/_t2s2 plan as "this target runs family _pb16i" -- a false refusal in
the same shape as the decode-twin one the #808 gate found on 2026-11-02.

open() now takes `family` and the forward passes family_tag(), so there is
one copy of that rule and it is the one the kernel names are built from.
Found by reading the rebase against main, not by running: no t2s container
of this model size is on the box, so the defect is shown from the code and
the rm path is unaffected.
bong-water-water-bong pushed a commit that referenced this pull request Nov 3, 2026
Main's #837/#843 made the artifact family t2s-aware ("_t2s" / "_t2s2"), and
XdnaTarget::open() was recomputing it locally from pb16i_/weight_layout_b8_.
That copy could not see t2s at all, and because a t2s container also sets
pb16i_ (its weights are the integer-exact ternary family), it refused every
_t2s/_t2s2 plan as "this target runs family _pb16i" -- a false refusal in
the same shape as the decode-twin one the #808 gate found on 2026-11-02.

open() now takes `family` and the forward passes family_tag(), so there is
one copy of that rule and it is the one the kernel names are built from.
Found by reading the rebase against main, not by running: no t2s container
of this model size is on the box, so the defect is shown from the code and
the rm path is unaffected.
bong-water-water-bong added a commit that referenced this pull request Nov 6, 2026
…pb16i's 410 (#1276)

P3 (#802) landed as code (#826/#830/#837/#843) and is the container serving on this
box, but its own gate was never recorded at the model level: the docs carry the
phase win (0.589 vs 1.037 ms, 1.76x) and the leaderboard had no t2s row at all, so
the claim 'halve the wire, speed up decode' was proven per kernel only.

Adds the measured pair as leaderboard records - the t2s2 arm and the pb16i control
it is read against, because a ratio is only a ratio if both arms are recorded - and
regenerates docs/npu-leaderboard.md from them, plus a subsection in the t2s chunk of
docs/npu.md that states what the stream is worth at the model level:

  pb16i (control)  410 ms/token (409-412), device wait 296, 12.93 GB/token
  t2s2 sealed      313 ms/token (311-317), device wait 209,  6.11 GB/token

1.31x per token, 1.42x on the device wait, on 2.12x fewer bytes (431 images,
14.44 -> 6.82 GB, checked on disk).  The model keeps 1.31x of the kernel's 1.76x
because the host half (66 ms) and the lm head (13.5 ms) do not shrink with the wire.

Measured 2026-11-02 in three adjacent npu-hold tickets on :18099, amdxdna counter 0
in each, 7 steady-state tokens per arm from the serve's own [ph] tok line - not
request walls, which pay the per-serve image fill once (614/651 for a 32-token
request).  Acceptance is byte-exactness: every decode role matched the pb16i path
byte for byte at layer 0 (6/6) and layer 3 (8/8) on three seeds, and greedy text
agrees on the shared prompt.

Co-authored-by: bong-water-water-bong <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review effort 4/5 tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant