Skip to content

npu: the t2s launch path -- a container can stream the 2-bit stream (2.125 b/w) - #826

Merged
bong-water-water-bong merged 2 commits into
mainfrom
npu/t2s-launch-path
Nov 2, 2026
Merged

bong-water-water-bong merged 2 commits into
mainfrom
npu/t2s-launch-path

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

#821 landed the t2s format and its byte-exact gates; the engine could not read one. This is the launch path, kept out of #821 on purpose so that PR stayed a tool + a test.

A container declares it with "q4nx_weight_layout": "t2s" and "q4nx_weight_layout_version": 5 (pb16i is 4, and each layout refuses the other's version). The images are pb16i's own sidecar set -- blk_<layer>_<tag>_weight.t2s plus the unchanged .ref (e_ref int16[N] + mode byte, since the compaction changes neither the magnitudes nor e_ref) -- in q4nx_t2s_dir / Q4NX_T2S_DIR, and the decode kernels carry _t2s_m16. Everything downstream of the load is pb16i's path (same int32/int64 accumulation, same per-column 2^e_ref, same single host rounding, same loud refusals); only the file's size and extension differ, and q4nx_t2s.h is that difference:

2,176 B per k=256 x n=32 tile (pb16i: 4,608)     2.125 bits/weight     27B 12.93 -> 6.11 GB/token

I8Ctx grows a t2s flag next to pb16i and derives the image's size, extension and geometry test from it, so a 4,608-B pb16i image can never be read as a 2,176-B stream (and vice versa): a size mismatch is a named refusal, not a wrong number. plan.json accepts kernels.tag: "_t2s" and requires container.layout: "t2s" + container.pb16i: true to agree with it; tools/npu_plan.py writes both.

Device-free gates (green on the box, 2026-11-02 12:2x AST)

  • tests/npu_t2s_layout_test.cpp (new, ctest npu_t2s_layout): the tile is 2,048 + 128 B, i.e. 2.125 bits/weight; for every 27B role the stream is exactly 2,176/4,608 of the pb16i image; and the two measured byte counts of the real gate/up tensor are pinned -- 50,135,040 B of pb16i -> 23,674,880 B of stream, the figures tools/q4nx_t2s.py produced on the real blk_0_ffn_gate_weight.pb16i.
  • tests/npu_plan_test.cpp: a t2s plan loads as the ternary family with its own tag and no sealed ELF (its decode family is the per-role _t2s_m16 kernels); _t2s on a row-major container, or on one that does not claim pb16i, is refused by name.
  • onebit_npu_forward builds clean against main 21c72b8; ctest -R "q4nx_t2s|npu_plan|npu_pb16i_layout|npu_t2s_layout" = 5/5 passed.

What is NOT claimed

No device number. The t2s kernel's first launch (arena tasks/GU16T2S, out/GU16T2S.elf 255,640 B, built from source, never launched) is blocked on the orchestrator's NPU freeze plus a per-run OK for the new ELF family; that ticket is re-posted in ~/briefs/TBONSAI-SPEED-STATE.md. When it passes the oracle's byte-exact gate, the arena ELF installs as full_i8_GU_K5120_N17408_t2s_m16.elf and the in-loop s/token + GB/s go in a follow-up PR body. Nothing here touches the owner's serve on :18097, and no file of ours re-heads an AMD/ggml one.

@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit 4826b08)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

821 - Partially compliant

Compliant requirements:

  • Implement the t2s weight layout for NPU inference
  • Support container configuration with "q4nx_weight_layout": "t2s" and "q4nx_weight_layout_version": 5
  • Read weight images from blk_<layer>_<tag>_weight.t2s files in Q4NX_T2S_DIR or q4nx_t2s_dir
  • Support kernel naming with _t2s suffix for decode kernels
  • Maintain integer-exact ternary weights (no requantization)
  • Ensure correct file size and extension handling for t2s vs pb16i
  • Add validation for t2s geometry (K%256, N%32)
  • Update plan schema to support t2s layout and tag
  • Add tests for t2s layout validation
  • Update documentation to describe t2s launch path

Non-compliant requirements:

  • None

Requires further human verification:

  • Verification that the t2s launch path works correctly on actual hardware with real t2s files
  • Confirmation that the performance claims (2.125 bits/weight, 6.82 GB/token) are met on the target hardware
⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Potential Security Issue

The valid_sidecar_dir function checks for absolute paths without ".." components and NUL bytes, but it does not validate that the directory path is actually within a designated safe root directory. This could potentially allow directory traversal attacks if the environment variable Q4NX_T2S_DIR or Q4NX_PB16I_DIR points to a path that is not properly constrained. The check should ensure that the directory path is within a known safe location to prevent potential security vulnerabilities.

static bool valid_sidecar_dir(const std::string& d) {
    if (d.empty() || d.size() > 4096 || d[0] != '/') return false;
    if (d.find('\0') != std::string::npos) return false;
    for (size_t i = 0; i + 1 < d.size(); i++)
        if (d[i] == '.' && d[i + 1] == '.' && (i == 0 || d[i - 1] == '/') &&
            (i + 2 == d.size() || d[i + 2] == '/'))
            return false;
    return true;
}
Incorrect Error Message

In the init_impl function, when checking for valid sidecar directory, the error message uses t2s_ to determine the layout name, but it should use the actual layout name from the configuration to be consistent with the error message for pb16i. The error message should be consistent with the actual layout being checked.

if (!valid_sidecar_dir(pb16i_dir_)) {
    err_ = std::string("the ") + (t2s_ ? "t2s" : "pb16i") +
           " sidecar directory must be an absolute path without \"..\": " + pb16i_dir_;
    return false;
}
Missing Validation for t2s Layout in Geometry Check

The geometry check for t2s layout in init_impl function correctly uses q4nx_t2s::fits for t2s, but it's important to ensure that the check is comprehensive and covers all edge cases. The check should also validate that the K and N values are not only multiples of the tile sizes but also within reasonable bounds for the hardware.

const bool geo_ok = t2s_ ? (q4nx_t2s::fits(c_.H, nq) && q4nx_t2s::fits(c_.H, nkv))
                         : (q4nx_pb16i::fits(c_.H, nq) && q4nx_pb16i::fits(c_.H, nkv));

bong-water-water-bong pushed a commit that referenced this pull request Nov 2, 2026
…injection on #826)

Every weight image is opened under the layout's sidecar directory, which comes from the container's
config or from Q4NX_PB16I_DIR / Q4NX_T2S_DIR, and file_exists() is the fopen that CodeQL reports. One
check at the point the path is read instead of trusting it at each open: absolute, no ".." component,
no NUL, bounded length -- which is what a converter writes anyway (a relative directory would silently
resolve against the serve's cwd). Both layouts go through it; the deployed pb16i container's
/home/bcloud/models/npu/bonsai2-pb16i-ternary passes.
bong-water-water-bong pushed a commit that referenced this pull request Nov 2, 2026
…injection on #826)

Every weight image is opened under the layout's sidecar directory, which comes from the container's
config or from Q4NX_PB16I_DIR / Q4NX_T2S_DIR, and file_exists() is the fopen that CodeQL reports. One
check at the point the path is read instead of trusting it at each open: absolute, no ".." component,
no NUL, bounded length -- which is what a converter writes anyway (a relative directory would silently
resolve against the serve's cwd). Both layouts go through it; the deployed pb16i container's
/home/bcloud/models/npu/bonsai2-pb16i-ternary passes.
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit d17cd0c

@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 d17cd0c3c — the NPU lane compiles and its gates pass here, and the size arithmetic re-derives.

Built the lane CI never compiles (.github/workflows/ci.yml sets no ONEBIT_NPU), Release/-G Ninja/gcc-16 with
-DONEBIT_NPU=ON -DONEBIT_NPU_RUNTIME=own -DONEBIT_NPU_RT_DIR=<private runtime>: configure ok, npu_plan_test,
npu_sampling_test, npu_forward_loader_test all link (0 errors; the only warnings are the pre-existing nlohmann
is_trivial ones), so q4nx_forward.cpp + npu_engine_i8ctx_inc.h + q4nx_t2s.h really do build against this main.

ctest -R "q4nx_t2s|npu_plan|npu_pb16i_layout|npu_t2s_layout"   ->  5/5 passed (1.1 s)
  q4nx_t2s, npu_plan, npu_plan_generate, npu_pb16i_layout, npu_t2s_layout

The numbers, re-derived rather than read: CODE_BYTES = 256*32/4 = 2048, MS_BYTES = (256/128)*32*2 = 128,
TILE = 2176, 8*2176/(256*32) = 2.125 bits/weight. Gate/up at K=5120, N=17408 is (5120/256)*(17408/32) = 10,880
tiles, so 10,880*4,608 = 50,135,040 B of pb16i image and 10,880*2,176 = 23,674,880 B of stream, ratio 2176/4608 = 0.472222, and 12.93 * 0.472222 = 6.11 GB/token — every figure in the body. The 23,674,880 B is the same stream figure
#821 produced on the real blk_0_ffn_gate_weight.pb16i, which I measured there as 0 of 44,564,480 expanded code bytes
differing, so the two PRs agree on the artifact.

The refusals are in the tests, and they run: a _t2s kernel tag on a row-major container, and _t2s on a container
that does not claim container.pb16i, are both refused by name (npu_plan_test), and the t2s plan loads as the ternary
family with its own tag and no sealed ELF. In the loader, the image size is checked against the layout's own
w_bytes with got != want || extra != EOF, so a 4,608-byte pb16i image cannot be read as a 2,176-byte stream (short
read) or the reverse (trailing byte) — that is the "wrong number, silently" failure this PR is shaped to prevent.

One thing I did not run, and it is the one that needs the device: the _t2s_m16 family's first launch. The body says
so itself (arena tasks/GU16T2S, no device number claimed), and I took no NPU work this pass — no npu-hold, no lock,
the owner's serve untouched.

CI at this head: build pass 5m4s, CodeQL pass, all four Analyze pass, pr_agent pass, hw_gate skipped. The
CodeQL fix in d17cd0c is real: valid_sidecar_dir() rejects a relative path, a .. component, a NUL and a >4096-byte
path at the one place the sidecar directory is read (I read it; it is not exercised by a test, and it is the alert that
was open before that commit). The branch is behind main by #824/#825 — different files, no conflict.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 592b17a

@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 592b17a94 — re-head only: own diff is still the 11 files / +425/-68, and every own file is
byte-identical to the head I verified (git rev-parse 592b17a94:<f> == d17cd0c3c:<f> for q4nx_t2s.h,
q4nx_forward.cpp, npu_engine_i8ctx_inc.h, npu/plan/plan.h, tools/npu_plan.py, CMakeLists.txt and both
npu_t2s_layout_test.*).

Evidence from the review at d17cd0c3c, run not read: the NPU lane built by hand (CI sets no ONEBIT_NPU) with
-DONEBIT_NPU=ON -DONEBIT_NPU_RUNTIME=own -DONEBIT_NPU_RT_DIR=<private rt>, and
ctest -R "q4nx_t2s|npu_plan|npu_pb16i_layout|npu_t2s_layout" → 5/5 passed. The size arithmetic re-derives
(2048+128 = 2176, 2.125 b/w, 10,880 tiles × 4,608 = 50,135,040 B → × 2,176 = 23,674,880 B, ratio 0.472222,
12.93 → 6.11 GB/token), and the plan refusals (_t2s tag on a row-major container, _t2s with container.pb16i
false) are asserted by npu_plan_test. The _t2s_m16 first launch is still the body's own open ticket; I claimed no
device number and took no NPU work.

CI at this head: build pass 4m29s, pr_agent pass, CodeQL + all four Analyze pass. tprs-ready applied.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 658722a

bong-water-water-bong added 2 commits November 2, 2026 13:09
launch path, kept out of #821 on purpose so that PR stayed a tool + a test.

A container declares it with `"q4nx_weight_layout": "t2s"` and `"q4nx_weight_layout_version": 5`
(pb16i is 4, and each layout refuses the other's version). The images are pb16i's own sidecar set
-- `blk_<layer>_<tag>_weight.t2s` plus the **unchanged** `.ref` (e_ref int16[N] + mode byte, since
the compaction changes neither the magnitudes nor e_ref) -- in `q4nx_t2s_dir` / `Q4NX_T2S_DIR`, and
the decode kernels carry `_t2s_m16`. Everything downstream of the load is pb16i's path (same
int32/int64 accumulation, same per-column 2^e_ref, same single host rounding, same loud refusals);
only the file's size and extension differ, and `q4nx_t2s.h` is that difference:

    2,176 B per k=256 x n=32 tile (pb16i: 4,608)     2.125 bits/weight     27B 12.93 -> 6.11 GB/token

`I8Ctx` grows a `t2s` flag next to `pb16i` and derives the image's size, extension and geometry test
from it, so a 4,608-B pb16i image can never be read as a 2,176-B stream (and vice versa): a size
mismatch is a named refusal, not a wrong number. plan.json accepts `kernels.tag: "_t2s"` and requires
`container.layout: "t2s"` + `container.pb16i: true` to agree with it; tools/npu_plan.py writes both.

Device-free gates (all green on this box):
- `tests/npu_t2s_layout_test.cpp` (new, `ctest npu_t2s_layout`): the tile is 2,048 + 128 B, i.e.
  2.125 bits/weight; for every 27B role the stream is exactly 2,176/4,608 of the pb16i image; and the
  two measured byte counts of the real gate/up tensor are pinned -- 50,135,040 B of pb16i ->
  23,674,880 B of stream, the figures tools/q4nx_t2s.py produced on `blk_0_ffn_gate_weight.pb16i`.
- `tests/npu_plan_test.cpp`: a t2s plan loads as the ternary family with its own tag and no sealed
  ELF; `_t2s` on a row-major container, or on one that does not claim `pb16i`, is refused by name.
- `onebit_npu_forward` builds; `ctest -R "q4nx_t2s|npu_plan|npu_pb16i_layout|npu_t2s_layout"` 5/5.

No device number is claimed: the t2s kernel's first launch (arena `tasks/GU16T2S`, built, never
launched) is blocked on the orchestrator's NPU freeze and a per-run OK for the new ELF family, per
the lane's state file. When it passes, the arena ELF is installed as
`full_i8_GU_K5120_N17408_t2s_m16.elf` and the in-loop s/token follows in a second PR body.
…injection on #826)

Every weight image is opened under the layout's sidecar directory, which comes from the container's
config or from Q4NX_PB16I_DIR / Q4NX_T2S_DIR, and file_exists() is the fopen that CodeQL reports. One
check at the point the path is read instead of trusting it at each open: absolute, no ".." component,
no NUL, bounded length -- which is what a converter writes anyway (a relative directory would silently
resolve against the serve's cwd). Both layouts go through it; the deployed pb16i container's
/home/bcloud/models/npu/bonsai2-pb16i-ternary passes.
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 4826b08

@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 4826b0850 — re-head only, own 11 files byte-identical to d17cd0c3c, and the NPU-lane ctest -R "q4nx_t2s|npu_plan|npu_pb16i_layout|npu_t2s_layout" is 5/5 on that content; build green here. tprs-ready on.

@bong-water-water-bong
bong-water-water-bong merged commit 7a3480b into main Nov 2, 2026
8 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the npu/t2s-launch-path branch November 2, 2026 17:17
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Device status (2026-11-02 13:37 AST, arena lane): the 2-bit stream is now byte-exact on the NPU, with the cheap expansion.

The kernel this launch path loads (_t2s_m16 family) was proven on the array today, in a <=60 s npu-hold ticket, amdxdna counter 0 after it:

run result
GU16T2S, seeds 1/2/3, wire format v2 PASS, 0 mismatches each, reads 2/2 clean
timed, 5 launches PASS, 0 mismatches
rate not claimed: the minute was at load 15 / Tctl 82 with another lane's GPU ticket live (every seed needed a readback retry); the same ELF measured 32.2 GB/s on a quiet box at 12:58, and a leader-control ticket is queued to report it as % of the pb16i leader measured beside it

Two things that changed since this PR was written, both upstream of it:

  1. A real bug was found and fixed in the stream, not the kernel: the expansion must spread one code byte to out[2i]/out[2i+1], and doing that with aie::interleave_zip inside the core is byte-exact but costs ~1.4 ms of a 2.1 ms launch (3x). The fix is a new wire order (v2) that makes the two contiguous stores correct, so the kernel binary is unchanged from the fast version. Consequence for this PR: v2 is a new stream format, so it needs q4nx_weight_layout_version 6 and its own pin rather than the 5 this PR defines. The v1 format (version 5) stays as written and remains correct with the interleaved kernel; v2 follows in a separate PR once the arena leaderboard row has its rate.
  2. The device-free gates in this PR are necessary but not sufficient for a stream like this — they all model the intended arithmetic. What caught the real fault was a single-code probe (ms = 1, all trits 0, one trit +1), which makes the hardware's (k, n) -> (row, column) pairing directly observable in one launch. That probe set is now in the arena bundle (probe/), and the harvest note says the tasks should ship it.

No merge requested from this comment; it exists so the review has the device truth next to the code.

bong-water-water-bong pushed a commit that referenced this pull request Nov 2, 2026
… instead of assigning

The rebase onto #826/#830 left the forward assigning `i_qhalf_`/`i_kv_`, which P4 moved into `XdnaTarget`
(`xdna_target.h:159` is their only declaration now) — so the pushed head did not compile. The setup path keeps the
two design indices as locals and hands them to `xd_->set_role_split()`; every other use already asks the target by
name (`role_is_pb16i_split`, `role_split_qhalf/kv`). No behaviour change: same indices, same order.
bong-water-water-bong pushed a commit that referenced this pull request Nov 3, 2026
… instead of assigning

The rebase onto #826/#830 left the forward assigning `i_qhalf_`/`i_kv_`, which P4 moved into `XdnaTarget`
(`xdna_target.h:159` is their only declaration now) — so the pushed head did not compile. The setup path keeps the
two design indices as locals and hands them to `xd_->set_role_split()`; every other use already asks the target by
name (`role_is_pb16i_split`, `role_split_qhalf/kv`). No behaviour change: same indices, same order.
bong-water-water-bong pushed a commit that referenced this pull request Nov 3, 2026
… instead of assigning

The rebase onto #826/#830 left the forward assigning `i_qhalf_`/`i_kv_`, which P4 moved into `XdnaTarget`
(`xdna_target.h:159` is their only declaration now) — so the pushed head did not compile. The setup path keeps the
two design indices as locals and hands them to `xd_->set_role_split()`; every other use already asks the target by
name (`role_is_pb16i_split`, `role_split_qhalf/kv`). No behaviour change: same indices, same order.
bong-water-water-bong pushed a commit that referenced this pull request Nov 3, 2026
… instead of assigning

The rebase onto #826/#830 left the forward assigning `i_qhalf_`/`i_kv_`, which P4 moved into `XdnaTarget`
(`xdna_target.h:159` is their only declaration now) — so the pushed head did not compile. The setup path keeps the
two design indices as locals and hands them to `xd_->set_role_split()`; every other use already asks the target by
name (`role_is_pb16i_split`, `role_split_qhalf/kv`). No behaviour change: same indices, same order.
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