Repository navigation
npu: the t2s launch path -- a container can stream the 2-bit stream (2.125 b/w) - #826
Conversation
PR Reviewer Guide 🔍(Review updated until commit 4826b08)Here are some key observations to aid the review process:
|
…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.
…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.
77da5f4 to
d17cd0c
Compare
|
Persistent review updated to latest commit d17cd0c |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
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.
|
Persistent review updated to latest commit 592b17a |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
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.
|
Persistent review updated to latest commit 658722a |
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.
658722a to
4826b08
Compare
|
Persistent review updated to latest commit 4826b08 |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
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.
|
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 (
Two things that changed since this PR was written, both upstream of it:
No merge requested from this comment; it exists so the review has the device truth next to the code. |
… 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.
… 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.
… 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.
… 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.
…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]>
#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.t2splus the unchanged.ref(e_ref int16[N] + mode byte, since the compaction changes neither the magnitudes nor e_ref) -- inq4nx_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, andq4nx_t2s.his that difference:I8Ctxgrows at2sflag next topb16iand 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 acceptskernels.tag: "_t2s"and requirescontainer.layout: "t2s"+container.pb16i: trueto agree with it;tools/npu_plan.pywrites 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 figurestools/q4nx_t2s.pyproduced on the realblk_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_m16kernels);_t2son a row-major container, or on one that does not claimpb16i, is refused by name.onebit_npu_forwardbuilds clean against main21c72b8;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.elf255,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 asfull_i8_GU_K5120_N17408_t2s_m16.elfand 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.