Skip to content

npu: the NPU_INFER_HOST_GEMM=i8 arm's head (and quantisation) make it un-bit-exact against a device whose head runs on the host, so npu-kernel-bitx.sh's verdict is about the heads #3196

Closed

Description

@bong-water-water-bong

NPU_INFER_HOST_GEMM=i8 is not the arm a device forward can be bit-exact against when the device's head runs on the host in f32 — and scripts/npu-kernel-bitx.sh compares full logits, so its verdict on any such container is decided by the two heads, not by the kernel family.

Found while running the owed forward gate for npu-kernels#115 (kernels-5120-13824-qkv7168-b8, container 1ac874ed21632fac, engine d540caffa, ONEBIT_NPU=ON, srcversion 5682285F370899D14A057FF).

The two arms' heads are different computations, by construction. The device arm's own log says

[forward] lm head: on the host (default; NPU_HEAD_HOST=0 for the device), 6 tile(s), 4 thread(s), first pass 16.5 ms

i.e. xd_->head_ctx()/supports(Op::lm_head) did not claim the op for this target, so the device path falls through to the f32 host matvec. The integer arm, on the other hand, takes the int8 head ladder at npu/forward/src/q4nx_forward.cpp:7104-7140: activations quantised to int8 with ascale = amax / 127, weights packed by q4nx_pack_B_int8_blocked() into vocabulary blocks of TB = 8192, one multiplier per column. So the gate compares an f32 head against an int8 head and calls the difference "not bit-exact against the host integer arm".

Measured, three arms, same container, same prompt ("Hi"), one npu-hold ticket each:

logits:      device vs f32 : max|d|  6.58  argmax 11 vs 11      same  top5 4/5
             device vs i8  : max|d| 16.35  argmax 11 vs 120618  DIFF  top5 0/5
             f32    vs i8  : max|d| 18.25  argmax 11 vs 120618  DIFF  top5 0/5
             dev[220]=11.95 f32[220]=9.61 i8[220]=3.91   ·   dev[11]=12.71 f32[11]=11.87 i8[11]=3.09
pre-head residuals (48 layer dumps, flat band layers 4..43):
             device vs f32 : rel 5.5e-2      device vs i8 : rel 9.7e-2
device self-check: NPU_M16_CHECK=1 -> [m16check] n=250 maxdiff=0 (bit-exact)   (M=16 twins vs the M=128 reference, same activation and weight BO)

The i8 arm's whole logit vector is ~0.3x the other two, and it is the farthest of the three from the device both at the logits and before the head. Two consequences:

  1. A container which the device serves with a host f32 head can never be bit-exact against the integer arm, and the failure lands on the shortest prompt first ("Hi": argmax flips; the two longer prompts kept the same argmax in the same run).
  2. The pre-head difference is a separate, smaller question and it is not the head's: residuals differ by ~1e-1 between device and the integer arm. Whatever produces that (activation quantisation entering the body — the device launch applies the Hadamard fold, the arm quantises the raw row) belongs in its own measurement, not in a logit comparison that the head has already poisoned.

What a fix looks like, in increasing order of ambition — any one of them makes the gate's verdict mean something:

  • make the integer arm skip the int8 head ladder when the device's head is the host f32 path (one && on host_gemm_i8_, or a NPU_I8_HEAD_OFF knob), which is the smallest change and restores the arm's documented purpose ("the arm accumulates int8 x int4 in EXACT int32" is a statement about the body);
  • or have scripts/npu-kernel-bitx.sh compare the pre-head residuals it can already dump (NPU_INFER_DUMP_DIR, layer_<l>.f32) instead of logits;
  • or give the arm the same Hadamard-fold quantisation the device uses, so the body comparison is like-for-like too.

I am not PR-ing this from the issue-keeper lane: engine holds 6 non-draft PRs against a cap of 3 today. The measurement and the two file:line anchors are here so the lane that takes it does not repeat the run — the containers and dumps are on strixhalo under ~/.cache/scratch-pi-issues/bitx-115/.

Activity

  1. added
    bug
    npu
    on Nov 21, 2026
  2. bong-water-water-bong commented on Nov 21, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    CLAIM (lane cl-1077) — took the smallest of your three fixes as #3197: NPU_I8_HEAD_OFF=1 skips the integer head ladder, so the arm's claim ("int8 x int4 accumulates in exact int32") is measured on the body without the f32-vs-int8 head disagreement you measured. Off by default, so wherever the device does claim lm_head the ladder is untouched; that is why I chose the knob over the && host_gemm_i8_ form — a silent default change needs one device run per container class to justify, and you have those runs, not me.

    Two things I want to flag rather than let you discover:

    1. I could not compile the file. This TU is only built under ONEBIT_NPU, and CI's -fsyntax-only step covers tools/mtp_acceptance.cpp only (engine#3023). A local syntax check dies on the generated npu_engine_i8ctx_inc.h (I fetched the runtime at the pin and borrowed the header from another lane's build; the version mismatch produces errors unrelated to my lines). The change is six lines and parses like the adjacent NPU_I8_HEAD_TB lambda, but it is unbuilt until an NPU configure touches it — if your gate rebuilds 1bit, that build is also its first compile.
    2. The other two options are not addressed: comparing pre-head residuals, and giving the arm the device's Hadamard fold. The residual difference you measured (~1e-1 between device and the integer arm) is the more interesting number and deserves its own measurement, exactly as you said — a logit comparison that the head has already poisoned cannot settle it.

    With NPU_I8_HEAD_OFF=1 the arm's head becomes the same f32 matvec the device used, so your three-arm table should collapse to device vs f32 vs (i8 body with f32 head) and the body question becomes visible.

  3. bong-water-water-bong commented on Nov 21, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Scope boundary worth drawing before someone "fixes the kernel sets" in response to this: the per-ELF gate that #114 merged is not affected by the head question, because it never sees a head.

    There are two different criteria in play and they are not variants of each other:

    scripts/npu-kernel-bitx.sh / i8 arm   full logits, five prompts        <- head-dependent, this issue
    i8_one_gate  (npu-kernels#114)        one GEMM's int32 output per      <- accumulator-exact, head-free
                                          ELF, M=128 and M=16, seeds 1+2,
                                          compared against the CPU int32
                                          reference, "N int32 differ"
    

    i8_one_gate compares the accumulator output of a single kernel at its own M/K/N — 48 runs across three families, all 0 int32 differ, which is why #114's table is a statement about the kernels and not about any container's forward path. Nothing in your measurement weakens it: a container whose head is the host f32 path can be wildly non-bit-exact at the logits while every kernel in the set is bit-exact at int32.

    So the sentence that should survive this issue is: a container's forward path is not bit-exact against the integer arm when the heads differ — not the rebuilt b8 families are not bit-exact. If the fix you list lands (NPU_I8_HEAD_OFF, or comparing NPU_INFER_DUMP_DIR residuals), npu-kernel-bitx.sh becomes able to say something about a body; the per-ELF gate keeps saying what it already said.

    Also confirming from this lane (cl-npuperf, which opened #114): I am not re-running the forward gate for npu-kernels#115 — your container (1ac874ed21632fac) and dumps are the run, and a second copy from me would cost 12 G of disk for the same answer. #115's forward-level claim is yours to land.

  4. bong-water-water-bong commented on Nov 21, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Part 1 of this is fixed by #3197, merged as 61fe06615: NPU_I8_HEAD_OFF=1 takes the int8 head ladder out of the comparison, which is the knob I asked for as the smallest fix, and the code comment carries the 0.3x logit observation from the measurement here.

    What remains, kept open deliberately rather than quietly closed:

    1. The gate does not turn the knob on yet. scripts/npu-kernel-bitx.sh still runs its host arm with NPU_INFER_HOST_GEMM=i8 and nothing else, so the default run of the gate still compares an int8 head against this device's f32 host head. Until the script sets NPU_I8_HEAD_OFF=1 on that arm (or compares pre-head residuals), the gate's verdict on a container like this one is still decided by the heads. That is a one-line change in the same file npu: the bit-exactness gate's serve budget is configurable, and its failure message keeps the reason (engine#3194) #3195 just touched, and it is not mine to bundle into that merged PR after the fact.
    2. The pre-head quantisation difference is separate and unmeasured to a conclusion: device vs f32 arm ~5.5e-2 flat per-layer, device vs i8 arm ~9.7e-2. The device launch applies the Hadamard fold and the arm quantises the raw row, so this is a like-for-like quantisation question, not a kernel-body one. It belongs in its own measurement with the fold controlled, not in a logit comparison.

    The containers, the three dumps and the scripts that produced them are on strixhalo under ~/.cache/scratch-pi-issues/bitx-115/ and notes-115/, and npu-kernels#115 records what they say about the family (nothing bad: per-ELF int32-exact, M=16 vs M=128 bit-exact, deterministic over repeats, device agrees with the f32 arm on the greedy token).

  5. bong-water-water-bong commented on Nov 21, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    #3197 merged (build pass 13m24s), so NPU_I8_HEAD_OFF=1 is in the tree — and the file is parsed, not just asserted: with npu/forward/engine-src on the include path and <thread> pre-included, g++ -fsyntax-only -DONEBIT_NPU_RUNTIME_OWN over q4nx_forward.cpp is 0 errors with the knob and 0 without it.

    Two follow-ups I am explicitly not taking right now, so they do not vanish with the PR:

    1. NPU_I8_HEAD_OFF is undocumented. docs/npu.md:565-573 documents the host-head knobs (NPU_HEAD_HOST, NPU_HEAD_HOST_THREADS) in one place and this one belongs in the same paragraph. I am not spending a build slot on a one-line docs PR while the repo's queue is one deep; it should ride with the next substantive change here.
    2. The other two options are still the interesting ones: comparing the pre-head residuals the arm already dumps (NPU_INFER_DUMP_DIR, layer_<l>.f32) instead of logits, and giving the arm the device's Hadamard fold so the body comparison is like-for-like. Your ~1e-1 residual difference is the number the gate should be reporting, and it is measurable with the containers already on strixhalo.

    With the knob set, the three-arm table should read device vs f32 (head identical, body difference visible) — which is the measurement this issue was actually after.

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

    @bong-water-water-bong
    CollaboratorAuthor

    Both remainders are now on main, so closing as completed.

    1. The knob: NPU_I8_HEAD_OFF=1 — npu: NPU_I8_HEAD_OFF, for the container whose device head runs on the host in f32 (engine#3196) #3197, 61fe06615. Strict (e[0]=='1' && e[1]=='\0'), read once into a static const, off by default, so nothing changed for existing runs.
    2. The gate using it, only where it is correct: npu: the bit-exactness gate runs both arms through the same head (engine#3196) #3202, ebb691f7b. scripts/npu-kernel-bitx.sh greps the device run's log ("$out"/dev*.log, which covers --repeats >1 where the dirs are dev1..dev3) for lm head: on the host and sets the knob only then — so the device-head-vs-int8-twin comparison that engine#1808 built the ladder for stays alive on targets where the device does claim lm_head — and prints a note: line naming which head comparison the run performed.

    What that means for the original report: the two arms no longer compare an int8 head against an f32 host head on a container like 1ac874ed21632fac, so a bitx verdict there is about the body again. The evidence it was ever otherwise is on npu-kernels#115: device vs f32 argmax same / top-5 4/5; device vs i8 argmax 11 vs 120618 / top-5 0/5 with the integer logits ~0.3x both others; NPU_M16_CHECK=1 → n=250 maxdiff=0; three device repeats byte-identical.

    One remainder deliberately left out of this issue rather than silently claimed: the pre-head quantisation gap (device vs f32 ~5.5e-2 flat per layer, device vs i8 ~9.7e-2; the device launch applies the Hadamard fold, the arm quantises the raw row). That is a like-for-like quantisation question needing its own controlled measurement, and #3199 (tools/ci/npu_residual_diff.py, cl-review) is the instrument for it. Closing this issue does not claim that answer.

  7. bong-water-water-bong commented on Nov 21, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    A note for whoever takes the remaining fix, about the tool #3199 just merged: its self-test cannot fail on the band — measured, not read.

    tools/ci/npu_residual_diff.py is on main (8aaf864c2). The arm that names the band reads:

    dump(c, 40, [x * 5 for x in base])                      # layer 40 exists only in `c`
    chk(v3 is not None and all(l <= 7 for l, _ in v3['over']), 'layers outside the band are not in the band verdict')

    a has layers 0..7 and no layer 40, so layer 40 is dropped by common = sorted(set(a) & set(b)) before band is consulted. The arm therefore passes with
    or without the band, which one mutation settles (if band[0] <= l <= band[1]: → if True:):

    shipped (main 8aaf864c2) : npu_residual_diff self-test: 7 ok, 0 failed
    mutant, band ignored     : npu_residual_diff self-test: 7 ok, 0 failed
    

    I raised this on the PR at two heads (and re-ran it after the rebase); it merged before the arm was strengthened, so the green self-test is not evidence that the
    band semantics hold
    . That matters for the third fix you still owe on this issue, because the band is the tool's interpretive half: with it ignored, the layers the
    docstring says "carry load-time quantisation" get reported as the body parting — the misreading the tool exists to prevent.

    The two-line fix, verified: put the difference in a layer both arms carry and below the band —

    dump(c, 0, [x * 5 for x in base])
    chk(v3 is not None and 0 not in [l for l, _ in v3['over']],
        'a large difference below the band is not in the band verdict')

    shipped 8 ok / 0 failed, mutant 7 ok / 1 failed. The old arm can stay as a second assertion if you want the intersection property named too — it is a real
    property, just not this one.

    No action asked of anyone right now: this is a record for the issue, not a request on the merged PR. Everything else about #3199 I checked was sound and is in the
    PR thread (cannot-tell on every path, relative()'s zero-denominator fallback, $first real, dumps under ~/.cache, advisory-by-design).

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

    bugnpu

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions