Skip to content

npu: bA's writeback covers the rows the host wrote, not the whole BO (16x less flush traffic per decode launch) - #1004

Merged
bong-water-water-bong merged 7 commits into
mainfrom
1bit/t2-a-narrow
Nov 4, 2026
Merged

bong-water-water-bong merged 7 commits into
mainfrom
1bit/t2-a-narrow

Conversation

@bong-water-water-bong

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

Copy link
Copy Markdown
Collaborator

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 whole MD-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 was
written back when it was last written. On this model's decode path am == 1 of MD == 16, so 15 of every
16 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 the
two 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.cpp memsets all MD rows of Am
before memcpying its own, so every line of bA is dirty on that path; set_activation_rows() therefore
still reports MD for 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 had
read 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/token

Pack bonsai2-27b-t2s2-serve (the route :18097 runs), 3 prompts x 24 decode tokens, NPU_C_SYNC_PROF=1
too so both syncs are in the same run:

arm A flush MB/token A flush ms/token C flush MB/token
NPU_NO_A_NARROW=1 (whole MD-row BO, pre-change) 48.94 0.75-1.41 66.57
default (this PR) 3.06 0.24-0.30 66.57

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-B clflush loop 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 from origin/main (9020f8c6, this change
absent), narrow = this head, full = this head with NPU_NO_A_NARROW=1. All three identical per
prompt at emitted tokens, every top_logprobs float, and the sha256 of the final hidden state — i.e. an
under-flushed bA (a stale read by the array) is ruled out by the output, not by the reasoning.

Compile side: 1bit target clean; the EP client that this touches through set_activation_rows() is not in
CI (ONEBIT_EP_NPU=ON needs the private runtime), so onebit_npu_gemm.cpp.o was built explicitly with
-DONEBIT_NPU=ON -DONEBIT_ORT=ON -DONEBIT_EP_NPU=ON — 0 errors.

Knobs: NPU_NO_A_NARROW=1 restores the whole-BO range (NPU_DBG forces it, since bochk checksums the
full extent), NPU_A_SYNC_PROF=1 prints 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 main as the reference binary), because main also gained #985 in between. Both windows: identity PASS, 0
canary SHORT, 0 amdxdna errors.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

#1004 ready for review at b18aabb. Stacked on #988 (same file, adjacent functions): merge #988 first or the two conflict by one hunk. Gate numbers are in the body — identity PASS across a origin/main reference arm, and 0.15 % of a token, stated as such.

…(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.
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

#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 9020f8c6 (git diff origin/main = the 2 files, +54/−2), rebuilt, and the whole identity window re-run on the new base, because main also gained #985 (prefix-cache snapshot) since the first gate:

arm A flush MB/token A flush ms/token
base = binary built from 9020f8c6 (merged main, whole-BO writeback) 48.94 (by construction) — (no counter in main)
full NPU_NO_A_NARROW=1 on this head 48.94 0.75 / 1.41
narrow (this PR) 3.06 0.24 / 0.30

Identity PASS again: 3 arms x 2 rounds identical per prompt at emitted tokens, every top_logprobs float and the final hidden-state sha256 — with main itself as the reference arm, so the 0.15 % claim is not bought with a behaviour change. New head 0104a3b4.

(The 0.75-1.41 ms spread on the full arm is this box, not the change: the bytes are exact by construction and the paired narrow arm lands at 0.24-0.30 ms in the same window.)

@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit 1d16628)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

988 - Partially compliant

Compliant requirements:

  • The C syncs cover the rows the host reads, not the whole BO
  • 16x less flush traffic per decode launch
  • Outputs bit-identical
  • NPU_NO_C_NARROW=1 restores the whole-BO range for an A/B on the same binary
  • NPU_DBG forces the whole-BO range because bochk/RBCHK checksums the full extent
  • NPU_C_SYNC_PROF=1 prints the two syncs' cumulative calls/bytes/ms per token

Non-compliant requirements:

  • None

Requires further human verification:

  • None

873 - Partially compliant

Compliant requirements:

  • Zero the activation BO's pad rows only when a previous launch wrote them
  • The activation BO's pad-row memset is conditional
  • The switches NPU_NO_A_FLUSH=1 and NPU_NO_C_INVAL=1 are used to gate the syncs
  • Identity PASS: emitted tokens, every top_logprobs float and the final hidden-state sha256 are identical
  • The timing improvement of +2.60 ms/token is measured

Non-compliant requirements:

  • None

Requires further human verification:

  • None

666 - Partially compliant

Compliant requirements:

  • None

Non-compliant requirements:

Requires further human verification:

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

Incorrect a_dirty_rows_ initialization

The a_dirty_rows_ member is initialized to 0, but the code assumes it starts at 0 and is only updated by a_dirty() calls. If prepare_activation() is called before any quantizer writes, the a_dirty_rows_ may not be correctly tracked, leading to incorrect sync ranges. This could cause stale data to be read if the sync range is too narrow.

int a_dirty_rows_ = 0;
Potential for incorrect sync range in set_activation_rows

The set_activation_rows() function sets last_am_ but does not update a_dirty_rows_ to reflect the full range of rows that are dirty. This could lead to incorrect sync ranges if the caller does not explicitly call a_dirty() after setting activation rows, potentially causing cache coherency issues.

void set_activation_rows(int rows) {
    last_am_ = rows > 0 ? rows : 1;
    // ...but its A writeback stays the whole BO: that client memsets all MD rows first (the pad rows have to
    // read as zero) and only then memcpys `rows` of them, so every line of bA is dirty. Narrowing this to
    // `rows` would leave the memset's zeroes in cache to be evicted *after* the kernel read them.
    a_dirty(MD);
}

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 0104a3b

@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

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:

  1. NPU_NO_A_NARROW is read as getenv(...) != nullptr, while every other knob in this file (NPU_NO_A_FLUSH,
    NPU_NO_C_INVAL) parses with atoi() != 0. So NPU_NO_A_NARROW=0 selects the whole-BO arm — the opposite of
    what someone A/B-ing a 0 would expect. Same class as the NPU_FFN_CACHE=on|true note on #637.
  2. 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.

@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 4, 2026
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 318e950

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

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 307f684

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 1d16628

@bong-water-water-bong
bong-water-water-bong merged commit f5d9bca into main Nov 4, 2026
9 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/t2-a-narrow branch November 4, 2026 18:37
bong-water-water-bong pushed a commit that referenced this pull request Nov 4, 2026
…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.
bong-water-water-bong added a commit that referenced this pull request Nov 7, 2026
…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]>
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