Skip to content

npu: readback can observe stale data after the ERT completion signal; needs a fence or verify-and-reissue #666

Closed

Description

@bong-water-water-bong

What happens

Reading a kernel's result BO after the ERT completion signal sometimes returns a short tile: a
few percent of the C elements are zeros (never written) although the launch reported completed and
the same program, ELF, inputs and weight BO produce an exact result on the next run. Measured on
strixhalo with the shipping M=16 decode twin of the 27B container (ELF md5 b662765e…, i.e. the
artifact the engine ships and that NPU_M16_CHECK passes 7,295 launches against).

This is the same phenomenon already recorded in-tree, without a fix, in
npu/forward/engine-src/npu_engine_i8ctx_inc.h (RESULTS-coverage-multifamily sections 68/82:
"bC is identical immediately before each launch, and yet the dequantized bA -- the host's reading of
that same bC -- differs … So the kernel's output buffer is the same and what the host gets back from
it is not."
). This issue is that finding with a reproducible harness and a scope: production
decode reads C the same way (wait_kernel() then readback()), one launch per GEMM, so a dropped
write inside a layer is a silent wrong activation that can flip a token.

Evidence (key RESULT lines; identical harness bytes, verified by SHA256SUMS)

RESULT task=GU seed=2 K=5120 N=17408 M=16 verdict=FAIL mismatches=17488 ...
       first_idx=832 got=0 want=15154 worst_absdiff=826718 reads=7 clean_reads=0
RESULT task=GU seed=3 K=5120 N=17408 M=16 verdict=FAIL mismatches=608 ...
       first_idx=9344 got=0 want=-222094 worst_absdiff=628392 reads=7 clean_reads=0

Earlier in the same session: 24,782 / 23,229 / 25,901 / 216,399 / 1,519 mismatching elements, all
got=0, all with the same ELF and inputs.

Timeline on one box, no code change between the runs:

window runs failed
11:40–12:15 15 ~7
12:19–12:20 12 0
12:50 7 2
12:56 4 0
13:01 (new blocked-B kernel) 3 3 — every run needed exactly one relaunch

What it is not

  • Not the harness or the inputs: the frozen harness is checked with sha256sum -c before every
    run, and three consecutive runs on the same seed alternate pass/fail.
  • Not a host-side transfer race: re-reading after 2 ms, 7 times over 12 ms, still short
    (reads=7 clean_reads=0); syncing the activation BO before and after each launch, and sleeping
    10 ms after wait(), changes nothing.
  • Not the kernel's maths: the same ELF is byte-exact in the engine's own gate on 7,295 launches,
    and the failures are always missing writes (got=0), never wrong values.
  • Not load/contention: it happened on an exclusive device (fuser /dev/accel/accel0 empty, no
    other serve) and was absent for hours on the same box.

Workaround currently in use (arena only, not in the engine)

~/wt/npu-arena/runner/runner.cpp: after the launches, read C; PASS requires two consecutive
clean reads
; if the first read is short, the whole launch group is re-issued (up to 3 attempts)
and the timing of a non-clean attempt is discarded, so a score can never come from a bad read.
~/wt/npu-arena/oracle.sh additionally requires every seed run and the timed run to be
byte-exact. This makes the benchmark trustworthy; it does nothing for the engine.

Two candidate fixes

  1. A real fence after the completion signal. The ERT COMPLETED state appears to be signalled
    when the core/control sequence retires, not when the last S2MM of the output buffer has landed in
    DDR. Options: an explicit end-of-sequence barrier (a tiny ERT task that touches the output
    buffer / a dedicated "flush" BD on the C channel) before readback() trusts the data, or a
    driver-level fence (DMA-buf fence / amdxdna syncobj) so wait_kernel() implies the writes are
    visible. Preferred if it can be done without a per-launch cost: this is the only fix that helps
    production, which cannot afford a re-read of the tile.
  2. Verify-and-reissue in readback() (cheap, no runtime change): poison a canary in the result
    BO before each launch (e.g. the last row, or one int32 per C tile) and after the sync check the
    canary changed; if it did not, re-issue the launch once. Costs one sentinel store + one load per
    tile and only pays when the race fires. Second choice because it doubles the launch rate in the
    bad windows and hides a runtime defect behind a retry.

How to reproduce

~/wt/npu-arena/oracle.sh GU tasks/GU/kernel --no-leader     # freezes harness, rebuilds the ELF
# or directly:
~/wt/npu-arena/runner/runner --elf <full_i8_G_K5120_N17408_m16.elf> \
    --task ~/wt/npu-arena/tasks/GU --seed 2 --iters 2

Repeat until it fails (it did within 4 runs in three separate windows today); a failure prints
reads=… clean_reads=… and the first mismatching index with got=0.

Blast radius

Every npu_gemm()/npu_gemm_rows() call in npu/forward/src/q4nx_forward.cpp — i.e. all decode and
prefill GEMMs, plus run_lm_head. A dropped write is not detected anywhere: the host dequantises
whatever is in Cm and continues. Proposed owner decision: fix (1) in the engine before the
release candidate is declared final, or (2) as the minimum.

Activity

  1. added
    bug
    on Oct 31, 2026
  2. bong-water-water-bong commented on Oct 31, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Triage: bug, and it is the most load-bearing open bug on the NPU side because production reads exactly the
    way the harness does. At origin/main 31d52e1:

    npu/forward/src/q4nx_forward.cpp:1755   ref.wait_kernel(r);
    npu/forward/src/q4nx_forward.cpp:1756   ref.readback();        <- decode GEMM
    npu/forward/src/q4nx_forward.cpp:1782   ref.wait_kernel(r);
    npu/forward/src/q4nx_forward.cpp:1783   ref.readback();        <- prefill / rows GEMM
    $ git grep -n 'fence\|syncobj\|dma_buf' origin/main -- npu/     -> 0 hits
    

    So nothing in the engine can currently distinguish "launch retired" from "last S2MM landed". #668 (2b33b75c0)
    landed the arena's verify-and-reissue, but as a benchmark discipline — it does not touch readback(), so the
    engine still trusts the first read.

    The issue is filed with the right two candidate fixes and the right cost structure. What it needs is a decision,
    not more measurement:

    • Option 1, a real fence (end-of-sequence barrier task or a driver fence on the output BO): the only fix
      that helps production without a retry, but it prices every launch, and the 1.037 ms GU kernel is the wrong
      place to add 5 %.
    • Option 2, canary verify-and-reissue in readback(): one sentinel store per launch + one load per tile,
      pays only when the race fires; the cost is a doubled launch rate inside the bad windows, which is exactly
      what NPU_M16_CHECK's 7,295-launch gate would then also be exercising.

    My recommendation to the owner, in this order: option 2 now as the safety net (it is small, it is testable
    on the arena harness already in the tree, and it converts a silent wrong activation into a retry), and
    option 1 investigated for the next kernel-format bump, where a per-launch cost can be amortised into the
    chunk. Shipping a release candidate whose wait_kernel() means "probably" is the one outcome I would not
    accept, because every decode GEMM is on that line.

    Board row and the decision request: ~/briefs/TISSUES-BOARD.md, ORCHESTRATOR item 3 / OWNER item 3.

  3. bong-water-water-bong commented on Oct 31, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Decision (orchestrator, 2026-10-31 12:30 ADT), recorded here so the next reader does not re-litigate it:

    Owner lane: pi-zerocopy. It already owns the copy path on this box (#698 target 2), and a stale-read on
    readback is the same question asked from the other end — what the host may assume about bytes the device wrote.

    Fence first, canary as fallback. The fence is a FROM_DEVICE invalidate on readback: invalidate the host
    cache range covering the result BO before the host reads it, so wait_kernel(); readback(); becomes
    "the device's writes are visible" rather than "the launch retired". That is a coherency operation on a cacheable
    host mapping, not a copy — it is the cheap form, and it is the one that can stay in the production path. The
    canary (poison a sentinel per tile before the launch, verify it changed after the sync, re-issue once if not)
    stays as the fallback only if a short tile survives a correct invalidate; it doubles the launch rate in the bad
    windows, which is why it is second.

    Gate: NPU_M16_CHECK, 7,295 launches. That is the artifact the engine ships (the M=16 decode twin of the 27B
    container, ELF md5 b662765e…) and the launch count at which it currently comes back byte-exact. Acceptance is
    that gate clean with the invalidate in place, no per-launch cost that shows up in NPU_PHASE_PROF/[prof]
    readback (today ~0.1 ms), and the 1.037 ms GU kernel unchanged on the arena harness.

    The two candidate fixes in the body are unchanged; this only fixes the order and the owner. If the invalidate alone
    does not close it, the fallback is the canary and the issue stays open until NPU_M16_CHECK is clean on both.

  4. bong-water-water-bong commented on Oct 31, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Measured on this box (strixhalo, shipping int8 M=16 path, engine build from zerocopy/main + the shipping
    runtime ~/wt/bonsai-speed-build/libonebit_npu_rt.a), following the orchestrator's "fence first" decision. Two
    findings, one of which changes the plan.

    1. The FROM_DEVICE invalidate the fence would add is already in the shipping path, on every readback.

    • All three shipping call sites are wait_kernel(r); readback(); before Cm is touched:
      npu_gemm (npu/forward/src/q4nx_forward.cpp:1937), npu_gemm_rows (:2021), run_lm_head (:3508).
    • I8Ctx::readback() (npu/forward/engine-src/npu_engine_i8ctx_inc.h:405) is
      bC->sync(XCL_BO_SYNC_BO_FROM_DEVICE).
    • That call is a real cache sweep, not a stub. npu_rt::bo::sync(dir,size,off) -> flush_range() =
      _mm_mfence(); clflush on every 64 B of the range; _mm_mfence(); — disassembled from the exact library the
      arena runner links (TOOLCHAIN.lock -> NPU_RT_LIB=~/wt/bonsai-speed-build/libonebit_npu_rt.a):
      4d92 mfence / 4db4 clflush (%rcx) / 4de0 clflush 0x0(%rax) + 0x40(%rax) / 4df0 mfence.
    • The arena runner calls it before each of its six reads (runner.cpp:180, :211) and still reports
      reads=7 clean_reads=0, with the pre-launch zeros at the short indices.

    So a stale host line cannot be what those runs read: after a clflush sweep the line is gone. A short row means
    the writes are not in memory when the host looks (or never landed at all), which no amount of
    re-invalidation can fix. This is evidence for your option 1's second half — "ERT COMPLETED is signalled when the
    control sequence retires, not when the last S2MM has landed" — and against a cache-fix reading of the symptom.

    2. What that invalidate already costs (the 2 % question). Measured on the shipping decode path with
    NPU_PHASE_PROF=1 ([ph] on a 1.33 s/token pb16i decode): "C readback" 9 ms/token of 1,330 ms = 0.68 %,
    i.e. ~24 µs per launch over 368 launches. Nothing to win there, and nothing to add: it is already paid.

    What I landed instead (your option 2, the fallback), on branch zerocopy/main:

    • I8Ctx::poison_canary() writes INT32_MIN into every 16th int32 of C row 0 (and of the last row for a batched
      launch) immediately before the kernel is submitted; verify_canary() runs right after the invalidate inside
      readback(). More than 8 survivors = the row is not there.
    • On a short row the whole launch is re-issued once (issue() lambda re-poison -> re-quantise -> relaunch) in
      all three shipping sites, counted in canary_retries_; a second short read is printed loudly rather than
      dequantised into the layer. The M=128 reference side of NPU_M16_CHECK gets the same treatment, so a short
      reference read cannot be blamed on the M=16 twin and pollute the gate.
    • Cost when it does not fire: one store + one load per 16 elements (~4 KiB each way at N=17408), no extra launch.
      ONEBIT_NPU_CANARY=0 disables it, NPU_CANARY_STATS=1 reports the counters.
    • Why sampled and not whole-row: a dropped tile here is hundreds of elements (this issue's own numbers are
      608-216,399 zeroed elements), so every 16th element catches it at 1/16 the traffic; the threshold of 8 keeps an
      accumulator that happens to equal the sentinel (1 in 2^32 per element) from ever looking like a short tile.

    Pending, needs the device window (requested in ~/NPU-WINDOW-OK; pi-release's c17 gate has the box): the
    canary's measured cost ([ph] h_rb with it on vs off) and the acceptance run the orchestrator asked for —
    NPU_M16_CHECK=1, ~8,800 launches (past the 7,295 this issue quotes), requiring MISMATCH: 0 and reporting how
    many [canary] SHORT retries fired. I will post those numbers here and in TZEROCOPY-STATE.md.

  5. bong-water-water-bong commented on Oct 31, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Second window (bonsai2-27b-NPU-m16, canary now flushing its poison before the submit), gate passed, and the
    numbers the fix was waiting on. PR: #701.

    arm m16 checks mismatched worst int32 [canary] SHORT launches
    canary ON (default) 6,915 0 0 0 7,568 (7,200 decode + 368 prefill)
    canary OFF 6,915 0 0 n/a same

    Sampling output byte-identical across the arms (argmax=40554 top=23.238878 margin=5.498953), so the canary does
    not perturb the arithmetic. Cost: readback rb = 5.4 ms/token with it, 4.1-5.1 ms/token without — +0.36 ms per
    token, mean over 38 tokens ≈ 0.03 % of a token
    , far inside the 2 % budget. Request wall 472 s (on) / 479 s (off),
    i.e. inside run-to-run noise.

    Reset of the earlier evidence, for the record: at 6,915 checks in each arm the engine path produced zero
    short rows — this issue's phenomenon still reproduces in the arena runner and not through the engine's launch path,
    which is exactly why the fix here is detect-and-retry rather than a claim to have removed the dropped write.

    Two things this window taught, both of which were my bugs and are fixed in the PR:

    1. The poison must be flushed to memory before the submit. Leaving the sentinels dirty in the host cache means
      they are evicted after the kernel wrote the same lines and clobber the result with the sentinel — 1,416 false
      short reads on the shipping M=128 path, one per launch group. Anything that leaves a dirty host line over the
      result BO can produce this issue's exact symptom; worth keeping in mind for the runtime side.
    2. NPU_M16_CHECK checks nothing on the shipping container ~/models/npu/bonsai2-27b-NPU: it registers
      decode family: 0 role(s) at M=16, so the run reports checks=0. The 7,295-launch gate in this issue lives on
      ~/models/npu/bonsai2-27b-NPU-m16 (with kernels-m16/), which is what the run above used.

    Limits, stated plainly: row 0 only (a batched launch is not canaried beyond row 0 — poisoning row am-1 fires on
    every prefill launch because the kernel does not write the padded last row of the activation BO); the underlying
    completion-signal/fence question is still open, and this makes it a detected retry instead of a silent wrong
    activation.

  6. bong-water-water-bong commented on Nov 1, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Closing — fix merged and in the v2026.44 draft. Orchestrator 02:05: close it, the fix merged in #701 and shipped
    in v2026.44.

    What this issue is closed on, all measured:

    evidence value
    merge #701 → e7446eef3 (2026-10-31 16:51Z)
    shipped in release v2026.44 (draft) — tag v2026.44 resolves to e7446ee, i.e. exactly the #701 merge
    what the fix does poison_canary() before submit (flushed to memory) + verify_canary() inside readback(); a short row re-issues the whole launch once and is never dequantised. Default on (ONEBIT_NPU_CANARY=0 disables, NPU_CANARY_STATS=1 reports)
    gate NPU_M16_CHECK=1 on bonsai2-27b-NPU-m16: 6,915 checks per arm, 0 mismatched, worst int32 = 0; canary ON and OFF arms produce byte-identical sampling (argmax=40554 top=23.238878 margin=5.498953)
    cost readback rb 5.4 ms/token with it vs 4.1–5.1 without → +0.36 ms/token ≈ 0.03 % of a 1.33 s/token decode, inside the 2 % budget; request wall 472 s vs 479 s (noise)

    Two limits the fix states itself, recorded here so the close is honest rather than generous:
    NPU_M16_CHECK only checks anything on ~/models/npu/bonsai2-27b-NPU-m16 (the shipping container registers no M=16
    roles, so it reports checks=0), and the canary covers row 0 (plus the last row for a batched launch) at every
    16th int32 — a dropped tile is hundreds of elements, so that sampling catches it, but it is detection and re-issue,
    not a proven removal of the dropped write.

    The underlying completion-signal question stays open in the runtime, not here: at 6,915 checks per arm the
    engine's launch path produced zero short rows in both arms, while the arena runner still reproduces them
    (clean_reads=0 with the FROM_DEVICE invalidate already in place). If you want that tracked, it wants its own
    issue against the runtime — a fence cannot fix writes that are not in memory when the host looks, which is what the
    window measured. Not reopening this one for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions