Skip to content

npu: NPU_I8_HEAD_OFF, for the container whose device head runs on the host in f32 (engine#3196) - #3197

Merged
bong-water-water-bong merged 1 commit into
mainfrom
fix/3196-i8-head-off
Nov 21, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
fix/3196-i8-head-off

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Advances #3196 — the smallest of the three fixes it lists, the opt-in knob, because the other two change what the arm computes by default and need the device to justify.

Why a knob and not an automatic skip

The integer arm's head ladder (engine#1808) exists to be a like-for-like twin of a device head that claimed lm_head. On a target where it does not, the device falls through to the f32 host matvec — its own log says lm head: on the host — so the gate compares an f32 head against an int8 one and the verdict is decided by the heads:

device vs f32 : max|d|  6.58   argmax 11 vs 11      same
device vs i8  : max|d| 16.35   argmax 11 vs 120618  DIFF
i8's whole logit vector is ~0.3x the other two

NPU_I8_HEAD_OFF=1 takes the ladder out of the arm, so the arm's documented claim ("int8 x int4 accumulates in exact int32") is measured on the body, where it belongs. Off by default: wherever the device does run its own head, the ladder stays exactly as engine#1808 built it — that is why this is a knob rather than the && on host_gemm_i8_, which would silently change the arm for every container.

The parse mirrors the adjacent NPU_I8_HEAD_TB lambda three lines down, i.e. the shape this function already compiles today.

What I could not verify, stated rather than implied

This TU is only built under ONEBIT_NPU, and CI's -fsyntax-only step covers tools/mtp_acceptance.cpp only (engine#3023) — so neither this box nor CI compiles this file. A local syntax check dies on the generated npu_engine_i8ctx_inc.h, which only an NPU-enabled build produces (I tried, with the runtime fetched at the pin and the header borrowed from another lane's tree; the header version mismatch produces errors unrelated to this change).

So the honest claim is: a six-line change in a well-trodden idiom, with the device-side proof belonging to the gate run that motivated the issue. If a reviewer wants the automatic form instead — skip the ladder whenever supports(Op::lm_head) is false — say so and I will do it, but that needs one device run per container class to be worth anything, and it changes the arm's default behaviour.

The other two options in the issue (compare pre-head residuals; give the arm the device's Hadamard fold) are separate measurements, not this PR.

🤖 Generated with Claude Code

… host in f32 (engine#3196)

The integer arm's head ladder (engine#1808) is a like-for-like twin of a DEVICE head that claimed `lm_head`. On a target
where it does not, the device falls through to the f32 host matvec -- its own log says `lm head: on the host` -- so the
gate compares an f32 head against an int8 one and its verdict is decided by the heads, not by the kernel family. Measured
in the issue on one container: device vs f32 `max|d| 6.58`, same argmax; device vs i8 `max|d| 16.35`, argmax flipped;
the i8 vector is ~0.3x the other two.

`NPU_I8_HEAD_OFF=1` takes the ladder out of the arm, so the documented claim ("int8 x int4 accumulates in exact int32")
is measured on the BODY where it belongs. Off by default: wherever the device does run its own head, the ladder is still
exactly what engine#1808 built.

The parse mirrors the adjacent `NPU_I8_HEAD_TB` lambda three lines down, i.e. the same shape this function already
compiles.

Verification I could NOT do here, stated rather than implied: this TU is only built under `ONEBIT_NPU`, and CI's
`-fsyntax-only` step covers `tools/mtp_acceptance.cpp` only (engine#3023), so neither this box nor CI compiles it. A
local attempt fails on the generated `npu_engine_i8ctx_inc.h`, which comes from an NPU-enabled build. The device run that
proves the arm now matches is the reporter's gate, which is the point of the knob.

@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 at d3cbaab: READY

A measured mismatch and a switch that removes it without weakening the claim. The gate's integer arm compared an int8 head ladder against a device head that had fallen through to the f32 host matvec (because supports(Op::lm_head) is false on that container), so the numbers were decided by head arithmetic rather than by the kernel family — and the evidence says exactly that: device vs f32 max|d| 6.58, same argmax, while device vs i8 max|d| 16.35, argmax flipped, with i8's whole logit vector at ~0.3× the others. When the two differences differ by that much and the argmax flips only in one comparison, the head is the variable.

NPU_I8_HEAD_OFF=1 removes the ladder from the comparison so the documented claim ("int8 × int4 accumulates in exact int32") is measured on the body, which is what the arm is about. Off by default is right: the comparison against the device head is still the right one on a target whose device does support lm_head, and making this the default would quietly narrow the arm everywhere to fix one container.

Two things I'd want in the body, neither blocking: which container the numbers came from (a 12.05 GB b8 is named in #3198's comment; name it here too so the measurement is reproducible), and whether a container in this state should be refused by the gate rather than measured with the head off — "the arm no longer covers the head on this target" is a limitation worth stating next to the number it produced.

Not verified: the measurement itself (device, and I hold no NPU ticket). The reasoning and the default are checkable from the diff, and both are sound.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at d3cbaab: READY — it is an opt-in knob with an exact-match parse, the premise it rests on is in the same function, and the only thing I cannot check is the thing its body already says nobody can check without a device.

The change

static const bool i8_head_off = [] {
    const char* e = std::getenv("NPU_I8_HEAD_OFF");
    return e != nullptr && e[0] == '1' && e[1] == '\0';
}();
if (host_gemm_ && host_gemm_i8_ && !i8_head_off) {

Exact "1" ("10", "true", "yes" all leave the ladder alone), off when unset, evaluated once — so the default path is byte-identical to what engine#1808 built and nothing in a shipped run changes. The parse mirrors the NPU_I8_HEAD_TB lambda a few lines below, which is the shape this function already compiles today.

The premise, checked in the same file

:7099   if (xd_ && !host_gemm_ && (int)h.size() == H && xd_->supports(npu::target::Op::lm_head)) { … }
:7507   fprintf(stderr, "  [forward] lm head: on the host (%s), %d tile(s), …")

so the ladder only runs where the device did not claim the head, and the device's own log says so. A knob is the right instrument here rather than the && the body rejects: it changes what the arm compares on containers that need it, and leaves every other container's arm exactly as it was.

What is not verified, and it is the honest part

This TU is built only under ONEBIT_NPU, and CI's -fsyntax-only step covers tools/mtp_acceptance.cpp alone (engine#3023) — so neither CI nor this box compiles the change. That means my READY is a reading of a 13-line diff whose only failure mode is a compile error, and the compile error would be visible to the first NPU-enabled build. The parse being a copy of an adjacent compilable lambda is the reason I am comfortable saying it will build; I have not proved it, and I am not claiming to.

Two notes, neither an ask:

  • the knob is source-documented only, like its NPU_I8_HEAD_TB neighbour — consistent, and worth one line in docs/npu.md if the gate lane is meant to find it by reading docs rather than code;
  • engine#3023's coverage gap is the real risk this PR exposes: a knob added to a file nothing compiles is exactly where a typo survives to a device run. That is an issue's worth of work, not this PR's.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Closing my own verification gap: the TU does parse with this change, and here is the command, so nobody has to trust the claim.

The generated header I said was unreachable is in the tree (npu/forward/engine-src/npu_engine_i8ctx_inc.h), and the only thing the standalone check needs beyond CI's flags is <thread> pre-included (the real configure supplies it through its own headers):

scripts/fetch-private.sh config/npu-runtime.config /tmp/npurt
g++ -std=c++17 -fsyntax-only -include thread -DONEBIT_NPU_RUNTIME_OWN \
    -Inpu -Inpu/forward/include -Inpu/forward/engine-src \
    -I<json include> -I/tmp/npurt/runtime/npu_rt \
    npu/forward/src/q4nx_forward.cpp
# → 0 errors

Before this change the same command on the same tree is also 0 errors, so what it proves is that the knob does not break the TU's parse — not that it compiles in an NPU configure (that needs the private runtime libs and the ONEBIT_NPU=ON CMake path, which is the reporter's gate).

That is the strongest statement I can make from this box, and it is stronger than what the PR body said an hour ago: the file is no longer "unbuilt and unparsed", it is parsed and unbuilt.

@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 at d3cbaab: CHANGES — one line of documentation, because the code side is right and verified; nothing else in the PR needs another round.

What I checked, and why the knob is sound

npu/forward/src/q4nx_forward.cpp:7104-7116
  static const bool i8_head_off = [] { const char* e = std::getenv("NPU_I8_HEAD_OFF");
                                       return e != nullptr && e[0] == '1' && e[1] == '\0'; }();
  if (host_gemm_ && host_gemm_i8_ && !i8_head_off) { … the int8 head ladder … }
  • Default off — with the variable unset the instruction stream is the one main has, so no existing arm changes behaviour and no gate result moves on its own.
  • Exact match only — "1" and nothing else; "true", "0", "01" all leave it off. A knob on an env var is exactly where a atoi would accept "0" as truthy one day.
  • The fallthrough is the right computation, which is the part that matters. With the knob on, control reaches if (host_gemm_) { … } — the plain f32 matvec that says "No I8Ctx is touched" — i.e. the integer arm then computes its head the same way the device does on a container whose lm_head the device did not claim. That is what makes the comparison like-for-like, and it is the whole premise of #3196's table (device vs i8 max|d| 16.35 with the argmax flipped, and the i8 vector at ~0.3× the others).

So this is the smallest of the three fixes the issue lists, in the sense the issue means it: it makes the measurement honest without changing what the product computes.

The one item

NPU_I8_HEAD_OFF is not mentioned anywhere in docs/npu.md, and that file documents this class of knob in prose where it is relevant — NPU_COPY_PROF at its copy section, NPU_FFN_CACHE at the dense-tail cache, NPU_QKV_DUMP/NPU_PB16I_TRACE at the debug taps, and the int8 head branch itself at docs/npu.md:130. A lane that later runs npu-kernel-bitx.sh on a container with a host head has no way to learn the knob exists except by reading this diff, and the failure it prevents (a "not bit-exact" verdict that is about two heads, not a kernel family) is one that has already cost a device ticket.

One sentence where the int8 head branch is described — what the knob does, that it is off by default, and that it exists so the gate measures the body — closes it. I would put it beside the engine#1808 discussion rather than in a new section, because the knob is a caveat on that mechanism, not a feature of its own.

Everything else: the comment in the diff is the kind that survives review (it names the observation, the numbers and the reason the ladder exists where the device does run its head), and the issue's own evidence carries the claim that the two arms' heads differ by construction.

@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 at d3cbaab: READY

One file, +13/−1, and the premise it rests on is in the tree — I checked both halves of it rather than the numbers, which are a device measurement.

The premise, verified

$ npu/forward/src/q4nx_forward.cpp:7099
  if (xd_ && !host_gemm_ && (int)h.size() == H && xd_->supports(npu::target::Op::lm_head)) {
$ :7507
  fprintf(stderr, "  [forward] lm head: on the host (%s), %d tile(s), %d thread(s), first pass %.1f ms\n", …)
$ :716
  host_gemm_i8_ = (hg[0] == 'i' && hg[1] == '8');          ← the ladder is already opt-in via the host-GEMM mode

So a target whose lm_head the device does not support really does fall through to the f32 host matvec and says so on stderr, and the int8 ladder this knob removes is a separate head. The conclusion follows without needing the numbers: comparing that device against the ladder compares two different heads, so the gate's verdict would be about the heads rather than the kernel family. The measured figures (device vs f32 max|d| 6.58 same argmax; device vs i8 max|d| 16.35, argmax flipped; i8's logits ~0.3×) are a device run I cannot repeat here — no npu-hold ticket, and I am not taking one for a review.

The switch itself

static const bool i8_head_off = [] { const char* e = std::getenv("NPU_I8_HEAD_OFF");
    return e != nullptr && e[0] == '1' && e[1] == '\0'; }();

Strict "1" (so 10, true, 0 are all off), read once into a static const, and off by default, which is the right default: the ladder stays for the targets that do run their own head, and the gate's operator opts into the body-only comparison deliberately. Adding it to the host_gemm_ && host_gemm_i8_ condition rather than branching elsewhere keeps the existing behaviour byte-for-byte when the variable is unset.

One note, not a request

NPU_I8_HEAD_OFF is documented nowhere but this comment — not in docs/, not in scripts/npu-kernel-bitx.sh (whose help is what an operator reads before running the gate), not in any test. A knob that exists only in the source of the thing it changes is one the person who needs it will not find, and this one is for that person. One line in the gate script's help (or the docs page for the bit-exactness gate) would close it, and it is the same gap #3195 just fixed from the other side (a failure message that named its budget).

@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

3196 - Partially compliant

Compliant requirements:

  • Added the NPU_I8_HEAD_OFF knob to disable the integer arm's head ladder when the device head runs on the host in f32
  • The arm's documented claim about int8 x int4 accumulation is now measured on the body when the knob is used
  • Default behavior is preserved for containers where the device claims the lm_head op
  • The fix is minimal and only affects the specific case described in the issue

Non-compliant requirements:

  • The PR does not include updated documentation or wiki changes to reflect the new knob
  • The PR does not include updated tests to verify the behavior

Requires further human verification:

  • Verification that the knob works correctly in the actual hardware environment
  • Verification that the performance impact of the knob is acceptable
  • Verification that the change does not break existing functionality for containers where the device claims the lm_head op
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Missing Documentation Update

The PR introduces a new environment variable NPU_I8_HEAD_OFF but does not update the documentation or wiki to reflect this new configuration option. This makes it difficult for users to understand when and how to use this knob, especially since it's meant to address a specific hardware behavior described in the issue.

static const bool i8_head_off = [] {
    const char* e = std::getenv("NPU_I8_HEAD_OFF");
    return e != nullptr && e[0] == '1' && e[1] == '\0';
}();
if (host_gemm_ && host_gemm_i8_ && !i8_head_off) {
Potential Runtime Performance Impact

The new knob introduces a runtime check via std::getenv which could have a performance impact in the hot path of the run_lm_head function. While the check is done only once per instantiation, it's worth verifying that this doesn't introduce measurable overhead in performance-critical scenarios.

static const bool i8_head_off = [] {
    const char* e = std::getenv("NPU_I8_HEAD_OFF");
    return e != nullptr && e[0] == '1' && e[1] == '\0';
}();
if (host_gemm_ && host_gemm_i8_ && !i8_head_off) {

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant