Repository navigation
npu: bA's writeback covers the rows the host wrote, not the whole BO (16x less flush traffic per decode launch) - #1004
Conversation
…(target 2, audit row 17) The array reads activation rows [0, MD) on every launch, so sync_a_to_device() flushed the whole MD-row BO before each submit. Only the lines the host has touched since the last flush can be dirty -- every other row was written back when it was last written -- so the range is the dirty span, not the BO. On the decode path that span is one row (am is always 1), i.e. 16x less traffic per launch on the M=16 context. prepare_activation() already knows exactly which rows the host wrote (that is #873's row tracker): the quantiser writes [0, am), and when it zeroes the pad rows it zeroes [am, a_written_rows_). a_dirty() takes the higher of those, and the flush clears it. The EP client that writes Am itself reports through set_activation_rows() but keeps the whole-BO range: it memsets all MD rows before memcpying its own, so every line of bA is dirty there and a `rows`-sized flush would leave the memset's zeroes in cache to be evicted after the kernel read them. Measured on bonsai2-27b-t2s2-serve (NPU_A_SYNC_PROF=1, I8Ctx's own counters, 30 decode tokens, 431 A syncs/token): 48.94 -> 3.06 MB/token flushed, 0.81 -> 0.32 ms/token of clflush. Identity PASS across three arms x 2 rounds (origin/main binary, narrow, NPU_NO_A_NARROW=1 whole-BO) at token, logprob and final hidden-state sha256 level.
b18aabb to
0104a3b
Compare
|
#988 merged while this was open (3ea5d09), so this branch has been rebased onto main and re-gated — it is now a standalone single commit, not stacked. Rebased onto
Identity PASS again: 3 arms x 2 rounds identical per prompt at emitted tokens, every (The 0.75-1.41 ms spread on the |
PR Reviewer Guide 🔍(Review updated until commit 1d16628)Here are some key observations to aid the review process:
|
|
Persistent review updated to latest commit 0104a3b |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
Read every host write to bA and confirmed each one registers its dirty span, checked the sync() argument order
against the declaration, and compiled the changed TU with the NPU path on. The device identity gate is the lane's.
Argument order (the one thing that would have made this a silent no-op): sync(int direction, size_t size, size_t offset)
— npu/npu_rt_api.h:87. So bA->sync(XCL_BO_SYNC_BO_TO_DEVICE, a_host_bytes(), 0) flushes a_host_bytes() bytes at
offset 0, and the call is the same shape as the merged #988 C narrowing two functions below (bC->sync(FROM_DEVICE, c_host_bytes(), 0)), which is already in production.
Every writer to Am is covered — an unregistered write is the failure mode this change creates, so I enumerated them:
| writer | registers |
|---|---|
quantize_async / quantize_async_rows -> q4nx_quant_i8::row(...) |
prepare_activation() runs before the loop and does a_dirty(am) |
quantize_bf16 (gemv, am == 1) |
a_dirty(1) |
whole-BO zero on ak != KD / first launch |
a_dirty(MD) |
pad-row zeroing, a_written_rows_ > am |
a_dirty(a_written_rows_) |
EP client memcpy into Am (onnx/ort/onebit_npu_gemm.cpp) |
set_activation_rows() -> a_dirty(MD), deliberately the whole BO because that client memsets all MD rows first — the #666 direction |
And NPU_NO_A_FLUSH returns before a_dirty_rows_ = 0, so a skipped flush keeps the span for the next real one.
a_host_bytes() clamps to bA->size() and never goes below one row, so the first call cannot flush nothing.
Compile: npu/forward/src/q4nx_forward.cpp with the NPU flags (-DONEBIT_NPU_RUNTIME_OWN, private runtime include
paths) -> rc 0, and the only diagnostic in the whole TU is the pre-existing nlohmann is_trivial deprecation from
_deps/json-src. The EP TU (set_activation_rows's other caller) is the lane's explicit -DONEBIT_ORT=ON -DONEBIT_EP_NPU=ON build; CI cannot compile it, which is why that is called out in the body.
The numbers are the shape the code predicts: 431 A syncs/token in both arms, 48.94 -> 3.06 MB/token = 15.99x,
and MD == 16 for the decode design (plan.json: decode.M = 16) against one dirty row = exactly the 16:1 the
a_dirty() tracker gives an am == 1 launch. Same call count in both arms, so the delta is only the range.
Two notes, neither a gate:
NPU_NO_A_NARROWis read asgetenv(...) != nullptr, while every other knob in this file (NPU_NO_A_FLUSH,
NPU_NO_C_INVAL) parses withatoi() != 0. SoNPU_NO_A_NARROW=0selects the whole-BO arm — the opposite of
what someone A/B-ing a0would expect. Same class as theNPU_FFN_CACHE=on|truenote on #637.- I did not re-run the identity gate on the device: the owner's serve owns the NPU and the serve pack is an 18 GiB
load, so the safety evidence here is your 3 arms x 2 rounds plus the enumeration above, not my own run. If the
window is still cheap, the one arm I would add is a prefill (am > 1) pair — every measured arm is decode
(am == 1), and prefill is where the narrowed span is wider than one row.
|
Persistent review updated to latest commit 318e950 |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at d537560b0 — merge-only update, own content byte-identical.
Checked with a hash of the PR's own diff at both heads rather than by eyeballing the file list: git diff <merge-base> <head> | sha256sum is the same at 0104a3b4a and at d537560b0, so nothing of this PR's moved. The branch is MERGEABLE/BLOCKED; the whole delta is main advancing (#1007, #1002, and the console-TUI/prefix-cache work that landed with them).
Evidence from the review at 0104a3b4a stands: the narrowed bA writeback — every host writer registers its dirty span, sync() arg order verified at npu/npu_rt_api.h:87, TU compiles rc 0.
CI at this head: red/pending: build, pr_agent, Analyze (c-cpp), Analyze (rust) — pr_agent failures on engine PRs are superseded-run cancellations ("Canceling since a higher priority waiting request exists"), not findings.
|
Persistent review updated to latest commit 307f684 |
|
Persistent review updated to latest commit 1d16628 |
…1021's serve watchdog land under the split main@434db3d2 had landed #1004 inside npu/forward (bA's narrow writeback), so the gate rows on d39c448 were void and were re-run on 212096e; while they ran, main moved again and #1022 ("the deferred-commit replay ran the GDN recurrence single-threaded and strided") edits both npu/forward/src/q4nx_forward.cpp and its header — the same file the split sits in — so the re-gate is repeated on this tip rather than handed over against a base that no longer exists. One conflict, and it is the expected one. P4 moved the phase accounting out of q4nx_forward.h into npu/forward/include/q4nx_lh.h (the forward and a target accumulate into one array), and main had since added a slot to that same enum (LH_HEAD_ROWS, run_lm_head_rows(), used at q4nx_forward.cpp:2327 and :3209). Resolved in P4's direction — the header keeps `#include "q4nx_lh.h"` and the inline namespace stays deleted — with main's new row ported into q4nx_lh.h at its position (after LH_PACK_UPLOAD, before LH_N) plus its name in kn[]; a port, not a take-theirs, because taking either side whole loses one of the two claims. The rest merged without conflicts and was read rather than trusted: #1022's gdn_apply_rows() loop exchange is model maths (q4nx_forward.cpp:2212) and P4 does not touch it; #1021's classifier is app/serve_watchdog.h + app/unified.cpp, outside the split; CMakeLists.txt keeps P4's two hunks plus main's new test target.
…and the audit stops quoting the pre-narrowing ranges (#1307) The page has been telling agents (and the orchestrator, tonight) that target 2 and target 3 are "not done". Both were done by shipped PRs, and the audit rows still quoted the traffic from before those PRs: - target 2 (#908, #873, #988, #1004): A quantised straight into the mapping on an AVX2 path, pad memset gone, C invalidate 1 065.09 -> 66.57 MB/token, A writeback 1 908.62 -> 321.59 MB/run, each gated byte-identical at token/logprob/head level. Both syncs are required — the kill switches emit garbage. - target 3: the fused path's node boundary moves 0 bytes (1 983 trace lines, never a `copy bytes=`); the readback leg is 0.044 ms of a 4.32 ms call. The largest pure copy left is the graph's own Concat, handed over. Rows 1-4 of the audit now carry the narrowed ranges with the PR that measured each, and the historical numbers are labelled historical instead of looking current. And the M=16 pad claim is corrected the same way: at measurement time the pack had 0 kernel roles at M=16, the twins ship now (7 and 18 ELFs, and the fleet's serve launches with --kernels kernels-m16), so what is left there is tile selection in the NPU runtime, not a copy. Docs only. Co-authored-by: bong-water-water-bong <[email protected]>
Target 2 (zero-copy on the NPU serving path), audit row 17: the last whole-BO sync. Based on
9020f8c6,i.e. on top of the merged #988, whose narrowed C syncs are the neighbouring half of the same story.
Root cause
sync_a_to_device()flushed the wholeMD-row activation BO before every submit ("the array reads rows[0, MD)"). Only cache lines the host has written since the last flush can be dirty; every other row waswritten back when it was last written. On this model's decode path
am == 1ofMD == 16, so 15 of every16 rows flushed per launch were already clean in memory.
prepare_activation()(#873, merged) already tracks exactly which rows the host writes: the quantiser writes[0, am), and when it zeroes pad rows it zeroes[am, a_written_rows_).a_dirty()keeps the higher of thetwo and the flush clears it, so the range is derived from the same tracker the padding logic uses rather than
from an assumption about what the caller did.
One client needs the whole range and keeps it:
npu/ort/onebit_npu_gemm.cppmemsets allMDrows ofAmbefore memcpying its own, so every line of
bAis dirty on that path;set_activation_rows()thereforestill reports
MDfor the writeback (while reporting its real row count for the narrowed C invalidate from#988). A
rows-sized flush there would leave the memset's zeroes in cache to be evicted after the kernel hadread them — the #666 failure mode, so it is called out in the code.
Measured —
I8Ctx's own counters (NPU_A_SYNC_PROF=1), one shipping pack, 431 A syncs/tokenPack
bonsai2-27b-t2s2-serve(the route:18097runs), 3 prompts x 24 decode tokens,NPU_C_SYNC_PROF=1too so both syncs are in the same run:
NPU_NO_A_NARROW=1(wholeMD-row BO, pre-change)16x less traffic per decode launch on the M=16 context, 0.44-1.1 ms/token measured, and it is the same
instrument arithmetic as the C narrowing:
bo::sync()is a per-64-Bclflushloop over exactly its range,so the bytes are exact and the ms is the paired wall of the same calls (431 calls in both arms).
Said plainly: this is 0.15 % of a ~300 ms token, 17x smaller than #988's 8.5 ms/token. Its value is that
it removes the last whole-BO sync from the decode path (after #988 the two C syncs are rows-bounded), not
the milliseconds. If review decides the completeness is not worth the diff, dropping it loses nothing
measured — but then row 17 stays open in the audit.
Identity gate: PASS
Three arms x 2 rounds,
t2_cmp.py: base = a binary built fromorigin/main(9020f8c6, this changeabsent), narrow = this head, full = this head with
NPU_NO_A_NARROW=1. All three identical perprompt at emitted tokens, every
top_logprobsfloat, and the sha256 of the final hidden state — i.e. anunder-flushed
bA(a stale read by the array) is ruled out by the output, not by the reasoning.Compile side:
1bittarget clean; the EP client that this touches throughset_activation_rows()is not inCI (
ONEBIT_EP_NPU=ONneeds the private runtime), soonebit_npu_gemm.cpp.owas built explicitly with-DONEBIT_NPU=ON -DONEBIT_ORT=ON -DONEBIT_EP_NPU=ON— 0 errors.Knobs:
NPU_NO_A_NARROW=1restores the whole-BO range (NPU_DBGforces it, sincebochkchecksums thefull extent),
NPU_A_SYNC_PROF=1prints the counter.Gate provenance
Two windows, both under
npu-hold -l zerocopy, on the shipped pack: the first on the pre-merge base (f63af5f9,3 arms x 2 rounds), the second after #988 merged and this branch was rebased onto
9020f8c6(3 arms x 2 rounds again,with merged
mainas the reference binary), because main also gained #985 in between. Both windows: identity PASS, 0canary SHORT, 0
amdxdnaerrors.