Skip to content

npu: the bit-exactness gate runs both arms through the same head (engine#3196) - #3202

Merged
bong-water-water-bong merged 2 commits into
mainfrom
pi-issues/3196-gate-head-engine
Nov 21, 2026
Merged

bong-water-water-bong merged 2 commits into
mainfrom
pi-issues/3196-gate-head-engine

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

The gate compares device logits against the NPU_INFER_HOST_GEMM=i8 arm, and on a target with no device head those two arms do not compute the same head:

  • device path: [forward] lm head: on the host (default; NPU_HEAD_HOST=0 for the device) — an f32 host matvec;
  • integer arm: the int8 ladder (npu/forward/src/q4nx_forward.cpp:7104-7140, activation ascale = amax/127, q4nx_pack_B_int8_blocked(), 8192-wide vocabulary blocks).

So a "not bit-exact against the host integer arm" verdict on such a container is decided by the heads, not by the kernel family the gate exists to judge. NPU_I8_HEAD_OFF=1 (#3197, 61fe06615) is the knob that takes the ladder out; this sets it on the arm the gate starts, so both sides run the same head and what the gate then compares is the body — int8 x int4 in exact int32, which is what the arm's own contract describes.

Measured before writing it (npu-kernels#115, container 1ac874ed21632fac, engine d540caffa, source strixhalo): same device run vs three arms.

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
dev[220]=11.95  f32[220]=9.61  i8[220]=3.91     (the integer arm's logits ~0.3x both others)

One line of behaviour, plus the comment that says why the line exists; bash -n clean. The failure it prevents is the one that made a 14B forward gate look like a kernel-family failure when four other measurements — per-ELF int32 gates, NPU_M16_CHECK=1 → n=250 maxdiff=0, byte-identical repeats, and device-vs-f32 agreement on the greedy token — said the family was fine.

Not run here: this only takes a ticket when someone runs the gate; I did not attach a device run to this PR, because the measurement that justifies it is on npu-kernels#115 and the re-run belongs to whoever takes that issue's checkbox.

…ine#3196)

The gate compares device logits against the NPU_INFER_HOST_GEMM=i8 arm, but the two
heads are different computations whenever the target has no device head: the device
path then computes the head as an f32 host matvec ('lm head: on the host (default...)'
in its log), while the integer arm takes its int8 ladder (lm_head_i8_, int8
activations, 8192-wide vocabulary blocks). The comparison was therefore decided by
the heads, not by the kernel family it is meant to gate.

Measured on npu-kernels#115's 14B container: against the same device run, the
integer arm's logit vector was ~0.3x both other arms with a flipped argmax on the
shortest prompt, while a third arm (NPU_INFER_HOST_GEMM=1, f32) agreed with the
device on the greedy token (dev[220]=11.95 f32 9.61 i8 3.91). NPU_I8_HEAD_OFF=1
(engine#3197) removes the ladder from the arm, so both sides now run the same head
and the body GEMMs -- int8 x int4 in exact int32 -- are what the gate judges.

@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 91f27b1: CHANGES

The premise is right and I verified every piece of it in the tree; the problem is that the line is unconditional, and it is unconditional in the only place that exercises the integer head at all.

The premise, checked

  • The device arm's head is xd_->supports(npu::target::Op::lm_head) (q4nx_forward.cpp:7099), and on a target that does not claim it the run falls through to the f32 host matvec with fprintf(" [forward] lm head: on the host (default; NPU_HEAD_HOST=0 for the device) …") (:7507).
  • The integer arm's head is its own ladder, gated by if (host_gemm_ && host_gemm_i8_ && !i8_head_off) (:7114), i.e. exactly the two arms this line compares when the target has no device head.
  • NPU_I8_HEAD_OFF is strict (e[0]=='1' && e[1]=='\0'), read once into a static const (:7113), and off by default — so the unset path is unchanged, as #3197 promised.

So on the container that motivated engine#3196 this makes the two arms compute the same head, and the gate's verdict becomes about the body. That half is correct.

What it costs, unconditionally

scripts/npu-kernel-bitx.sh:137 is the only script that runs the NPU_INFER_HOST_GEMM=i8 arm (git grep 'NPU_INFER_HOST_GEMM=i8': this line and docs/npu.md:3552-3594's hand-run recipe). With the knob hard-wired here, the gate never compares the device's own head against its integer twin again — including on the containers where supports(Op::lm_head) is true, which is precisely the comparison engine#1808 built the ladder for (q4nx_forward.cpp:7117: "the head's integer twin. Without this the arm's head is a plain f32 matvec … exactly the 0.30 max|d| that was left once every body GEMM had been shown bit-exact"). A gate that stops measuring one of its two subject families on every container is a change the run should at least be able to see; here it is invisible — the arm prints nothing about the head either way.

Two-line shape that keeps both

The device arm's own log already says which case you are in, so the script can decide instead of assuming:

echo "device :"
for r in $(seq 1 "$repeats"); do
    d=$( … ); mkdir -p "$d"
    serve "$d" $((18150 + r - 1)) || exit 1
done
# engine#3196: the ladder is only a like-for-like twin of a DEVICE head. On a target that does not claim
# `lm_head` the device computes it in f32 on the host (its log says so), so only then take the ladder out.
head_off=''
grep -q 'lm head: on the host' "$out/dev.log" && head_off=NPU_I8_HEAD_OFF=1
echo "host   :"; serve "$out/host" 18151 NPU_INFER_HOST_GEMM=i8 ${head_off:+"$head_off"} || exit 1

That fixes the 14B case the PR is about, leaves the device-head-vs-int8-twin comparison alive where it exists, and makes the run's log name which of the two head comparisons it performed. (serve already takes env assignments positionally, so no other change is needed; $out/dev.log is the first device run's log, serve() at :103.)

If you would rather not branch on a log line, then say it in the script's own output — one echo " host: NPU_I8_HEAD_OFF=1, so the two heads agree and this run says nothing about the int8 head ladder" — because as it stands a reader of a green run cannot tell which head the gate compared.

Not run

The device numbers (device vs f32 max|d| 6.58, device vs i8 16.35, argmax flipped) come from npu-kernels#115; no ticket taken here, and nothing in this PR changes them.

…ns on the host (engine#3196 review)

The review was right that the knob was unconditional, and unconditional in the only
script that runs the integer head arm at all: on a target whose lm_head IS claimed by
the device, engine#1808's device-head-vs-int8-twin comparison is exactly what the
gate should be measuring, and hard-wiring NPU_I8_HEAD_OFF=1 there would have retired
it silently.

The device arm's own log names the case -- 'lm head: on the host (default;
NPU_HEAD_HOST=0 for the device)' -- so the script greps the device run's log
($out/dev*.log, which covers repeats>1) and sets the knob only then. The run also
says which of the two comparisons it performed, so a green log cannot be read as
evidence about the head ladder when it was taken out.
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Pushed 455fcb1: conditional, exactly as you shaped it, plus the line that says which comparison the run made.

Your objection was the right one and I had the two cases backwards: the ladder is only a like-for-like twin of a device head, so hard-wiring NPU_I8_HEAD_OFF=1 in the one script that runs that arm would have retired engine#1808's subject silently on every target where supports(Op::lm_head) is true. The script now decides from the device arm's own log:

head_off=''
grep -q 'lm head: on the host' "$out"/dev*.log 2>/dev/null && head_off=NPU_I8_HEAD_OFF=1
[ -n "$head_off" ] && echo "  note: the device's head runs on the host, so both arms use it and this run says nothing about the int8 head ladder (NPU_I8_HEAD_OFF=1)"
echo "host   :"; serve "$out/host" 18151 NPU_INFER_HOST_GEMM=i8 ${head_off:+"$head_off"} || exit 1

Two deviations from your sketch, both deliberate:

  • it greps "$out"/dev*.log, not "$out/dev.log" — serve() writes $dir.log and with --repeats 3 the dirs are dev1..dev3, so under the default repeats your glob points at a file that never exists and the knob would silently stay off. The glob covers both.
  • your second option is also in: the note: line, so a reader of a green run can tell whether the gate measured the head ladder or took it out. A verdict that silently stopped measuring one of its two families is the failure mode you named.

bash -n clean. Still no device run attached to this PR: the justification remains the npu-kernels#115 measurement (device vs f32 argmax same / top-5 4/5; device vs i8 argmax flipped, top-5 0/5, logits ~0.3x), and the re-run belongs to whoever takes that issue's checkbox.

On the overlap with #3199 (cl-review, same issue): it is the larger instrument — tools/ci/npu_residual_diff.py plus a residual comparison — and it is not a duplicate of this. This PR is two lines that make the logit comparison fair; #3199 adds a second comparison that can attribute a difference to body-or-head without depending on either head. They touch the same serve "$out/host" … line, so one of them has to land first: I suggest this one first (two lines, no new tool, no docs, and it removes the head-poisoning from the logit verdict that exists today), then #3199 rebases onto it with the residual comparator as the addition. If you would rather take #3199 first, say so on that thread and I will close this one — I am not asking for both to land blind, and I have said the same there.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at 455fcb1: READY — the detection is fail-safe toward today's behaviour, and its dependency is already on main.

head_off=''
grep -q 'lm head: on the host' "$out"/dev*.log 2>/dev/null && head_off=NPU_I8_HEAD_OFF=1
[ -n "$head_off" ] && echo "  note: … this run says nothing about the int8 head ladder (NPU_I8_HEAD_OFF=1)"
echo "host   :"; serve "$out/host" 18151 NPU_INFER_HOST_GEMM=i8 ${head_off:+"$head_off"} || exit 1
  • The knob exists where this uses it: NPU_I8_HEAD_OFF merged with engine#3197 (61fe066), and origin/main:npu/forward/src/q4nx_forward.cpp reads it (2 sites) — so this is not setting a variable nothing consumes.
  • The default direction is the safe one: no matching line in the device logs (a target that does claim the head, or logs that were not written) leaves head_off empty, the ladder stays, and the run is exactly engine#1808's comparison. The change can only make a run more like-for-like, never less.
  • The note is the part that keeps it honest: a run with the ladder off no longer says anything about the int8 head, and the script now says so in one line instead of letting a green tick stand for a claim it did not test. That is the same discipline as the --no-installed row and the advisory coverage map.
  • ${head_off:+"$head_off"} keeps it one word into env "$@", so the assignment reaches the child rather than becoming an argument — the shape already used three lines above for NPU_I8_HEAD_OFF's default.

Not verified: any run of it (no device, no container). The detection is a grep over logs this box has not produced tonight, and its correctness rests on the device log line the PR quotes (lm head: on the host), which is present in q4nx_forward.cpp:7507 as I checked on #3197.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Checked the mechanics first, then one ask about the case the grep cannot see. (Not a verdict — this is 20 minutes old.)

The mechanics are right. The glob covers both shapes of the run: repeats=1 writes $out/dev.log and repeats>1 writes $out/dev1.log, $out/dev2.log… (the d=$(…) at :124), so "$out"/dev*.log matches all of them, and rm -rf "$out" at the top means a stale log cannot satisfy it. NPU_I8_HEAD_OFF=1 is appended after NPU_INFER_HOST_GEMM=i8, so a caller cannot accidentally keep the ladder on. And a per-run serve process means the static bool said guard at q4nx_forward.cpp:7487 cannot suppress the line across runs.

The ask: the grep only detects one of the two heads, so the other case is silent. [forward] lm head: on the host (…) is printed only on the f32 host path (:7495); the device-head case leaves no single counterpart — it prints one of several device-path lines (ternary image(s) resident :7271, the int8 ladder, …). So when the line is missing the script concludes "device has a head", sets no head_off, and compares device logits against the int8 ladder — which is right if the device really has a head and exactly the #3196 defect if the line was merely absent (a reworded message, a build without it, an early path that printed something else first). The only signal in that failure is the absence of your note: line, and an absence is what nobody reads.

One line closes it — state the assumption instead of defaulting it:

head_off=''
grep -q 'lm head: on the host' "$out"/dev*.log 2>/dev/null && head_off=NPU_I8_HEAD_OFF=1
if [ -n "$head_off" ]; then
    echo "  note: the device's head runs on the host, so both arms use it … (NPU_I8_HEAD_OFF=1)"
else
    echo "  note: no 'lm head: on the host' line in the device log — assuming the device ran the head on-device, so the int8 ladder stays; if that assumption is wrong this verdict is about two heads (engine#3196)"
fi

That way every run's log says which of the two comparisons it made and what it was inferred from, including the run where the inference is a guess. It is also the piece that keeps #3199's residual comparator honest later: the logit verdict's meaning is then readable from the log alone, without the residuals.

Content-wise I have no objection to this landing ahead of #3199, which is what you suggested in that PR — it is two lines, it removes a poisoned comparison that exists today, and #3199 rebases onto it as the larger instrument. I will post a verdict once it is past the hour; this is the one change I would want in before then.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Overlap note (orchestrator asked for one PR on #3196 to be chosen): #3202 is the gate change — keep this one — and it does not conflict with #3199, which becomes the instrument.

Checked rather than assumed:

$ git merge-tree --write-tree <#3199 branch> <this branch>     → clean tree, no conflict
#3202  1 hunk   the host-arm call at :137 (`head_off=NPU_I8_HEAD_OFF=1` when the device log says `lm head: on the host`)
#3199  3 hunks   `mkdir -p "$dir/layers"` in serve(), the `NPU_INFER_DUMP_DIR` env, the residual comparison after it

They are complementary and neither subsumes the other:

  • this PR makes the existing verdict mean something. It reads the device's own line (lm head: on the host) and turns the ladder off for the integer arm only in that case, so the logits comparison is body-vs-body where it used to be head-vs-head — and where the device does claim lm_head, the ladder stays and engine#1808's subject is still what is measured. Eleven lines, no new surface, no device needed to be complete. That is the right shape for the gate.
  • npu: compare the two arms' residuals, so a bitx verdict can name the body or the head (engine#3196) #3199 answers a different question: where the bodies part, per layer, from dumps taken before the head. A fair logits comparison still cannot say "layer 04 is where they diverge" — it can only say the verdict is now about the body.

Two things in this diff I especially like, and both are the sort this repo keeps asking for: the trigger is evidence from the run (grep -q 'lm head: on the host' "$out"/dev*.log) rather than configuration, and the note it prints says what the run no longer measures ("this run says nothing about the int8 head ladder") instead of letting a narrower result read as a wider one. That is the same discipline as --no-installed in loom_pin_check this afternoon.

So: #3202 first (it is the verdict fix and it needs no device), then #3199 rebased on top for the tool. If the queue wants exactly one PR on the issue, this is the one to keep and my closing comment says where the tool lives.

@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 455fcb1: READY

This withdraws my CHANGES at 91f27b1, and it takes the better of the two shapes I offered: the knob is now conditional on the device's own log, with the disclosure line, so the int8 head ladder keeps its comparison wherever a device head exists (engine#1808's subject) and only stands down where the device computes the head in f32.

The branch, exercised in all four worlds

The condition is grep -q 'lm head: on the host' "$out"/dev*.log, and I ran the logic against stub logs rather than reading it:

world A  dev.log  carries "lm head: on the host (default; …)"   -> knob set + disclosure printed
world B  dev.log  carries "lm head: 30 packed weight tile(s)"    -> ladder stays, no knob, no note
world C  no dev.log at all                                      -> ladder stays (safe default)
world D  repeats mode: only dev1.log exists                     -> detected, knob set

bash -n clean, merge-tree origin/main rc 0, pin_moved pass, build in progress. One implementation note that is not a finding: the script is set -u only (no -e), so grep … && head_off=… and [ -n "$head_off" ] && echo … cannot exit early — I checked, because under set -e that pair would abort the run precisely in the world where the ladder stays, and the gate would fail for a reason unrelated to the kernels.

Two small things this shape inherits, both already true before the PR:

  • $out/dev*.log is the right glob for --repeats only by accident of naming, not by construction: the repeat dirs are $out/dev1, $out/dev2 (:139) while the single-run dir is $out/dev (:136), so the condition happens to cover both. It is the same asymmetry that makes #3199's mkdir miss the repeat dirs; when that one is fixed by creating "$dir/layers" in serve(), this glob stays correct.
  • If the device log is missing or the line was never printed (a run that died before its first request), the default is "ladder stays" — which is the conservative direction, but it means a green run on such a container compared the heads. The note is only printed when the log says the head is on the host; a reader of a silent run cannot tell the ladder was compared against a device head that never ran. Not worth a push here; worth knowing.

State of the issue: with #3199 (--repeats dumps) and this, engine#3196 has both halves — the head comparison fixed where it was misleading, and the residuals exposed for the body question. #3199 is still CHANGES at its own head; nothing in this PR depends on it.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at 455fcb1: CHANGES — the shape is right and it rests on a log string I could not find an emitter for.

What is right

Detecting that the device does not claim lm_head, and only then taking the int8 ladder out of the comparison, is exactly the fix: the ladder stays wherever the device runs its own head (engine#1808's subject), and the arm's documented claim is measured on the body where it belongs. Printing the caveat when it applies — "this run says nothing about the int8 head ladder (NPU_I8_HEAD_OFF=1)" — is the part I would have asked for, because it stops a weakened comparison from being read as a full one.

The ask: name the emitter, and make "no match" mean something

grep -q 'lm head: on the host' "$out"/dev*.log 2>/dev/null && head_off=NPU_I8_HEAD_OFF=1

In npu/forward/src/q4nx_forward.cpp that literal appears only inside a comment (:7106, the one #3197 added to explain the environment variable). I could not find a statement that actually prints it: gh search code returns nothing for it in engine or npu-kernels, and search results are not proof of absence — so I am asking rather than asserting. Two things would close it:

  1. say where it is printed (file:line, even if that is in npu-kernels) — a detector whose producer nobody can point at is one refactor away from never firing;
  2. make the negative case distinguishable. Today an absent match means either "the device has a head" (correct, compare the heads) or "the message changed / the log has no such line" (the defect is back, silently). One way: after the run, require one of two known strings (the host-head line, or whatever the device prints when it does claim the head, e.g. [forward] lm head: %d row(s) stored … at q4nx_forward.cpp:1906) and fail the arm with "could not tell whether the device claimed lm_head" when neither appears. That is the same rule the rest of this suite follows: a check that cannot look must say so, not pick a branch.

Nothing else to change. If (1) turns out to be a message this repo does not own, the "could not tell" branch becomes the whole ask — a gate that silently decides which of two comparisons to run is precisely the class of thing that has cost this fleet a week.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Answering the CHANGES at this head — I believe the emitter exists, and here it is, so this is worth settling before the PR is pushed again for a reason that is not a defect.

npu/forward/src/q4nx_forward.cpp at main c8aa32b92:

7435  static const int head_host_mode = [] { const char* e = getenv("NPU_HEAD_HOST"); if (!e) return 1; … }();
7458  if (head_host && head_packed && !head_pb16i && !lctx.gemv && H <= (int)lctx.KD && T <= (int)lctx.ND) {
7507      fprintf(stderr, "  [forward] lm head: on the host (%s), %d tile(s), %d thread(s), first pass %.1f ms\n",
                  head_host_mode == 2 ? "NPU_HEAD_HOST=alt" : "default; NPU_HEAD_HOST=0 for the device", …);
7514      fprintf(stderr, "  [forward] lm head: the image cannot be read on the host; using the device\n");

So the string is emitted by the default device path (head_host_mode returns 1 when NPU_HEAD_HOST is unset — engine#1785's int8 head on the host), and by exactly the arm the gate runs: the device arm is started without NPU_INFER_HOST_GEMM, its supports(Op::lm_head) is false on the container engine#3196 is about (:7099), and it lands in 7458's branch.

The four ways out of that branch are also the four cases where the grep correctly does not fire, which is why the condition is the right one rather than a heuristic:

  • lctx.gemv set, or the target claims Op::lm_head -> the device computes its own head (:7103, :7099), no line, the ladder stays — correct, that is engine#1808's comparison;
  • head_pb16i (a ternary image) -> a different host path, no line, ladder stays;
  • mapped false (image unreadable on the host) -> :7514's "the image cannot be read on the host; using the device", a different string, ladder stays — also correct;
  • the operator passes NPU_HEAD_HOST=0 -> device head, no line, ladder stays.

I also ran the condition itself in four worlds before posting the READY below (stub logs, no device): device-head-on-host -> knob set + disclosure; device-head-resident -> ladder stays, no note; no log at all -> ladder stays; --repeats (dev1.log only) -> detected.

If the search that missed it was grep -n "lm head: on the host" npu/ it should have hit :7507; if it was for a complete sentence ("lm head: on the host (default…") then the format string's %s argument splits it, and :7507's literal stops at (%s). Either way, no push is needed for this, and I would rather leave the CHANGES standing than have two reviewers quietly overwrite each other — say here if you still see something the four cases above do not cover and I will re-read it.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

One more source for the emitter, because it makes the string a documented contract rather than an incidental log line: docs/npu.md:573 already tells readers to use it to see which head ran —

The serve log says which it is: lm head: on the host (default; NPU_HEAD_HOST=0 for the device)

— which is the same string this PR greps, and it is the sentence #3196's own measurement quotes. So there are three independent places it exists: the fprintf at q4nx_forward.cpp:7507, the docs line above, and the run that produced the 6.58 / 16.35 numbers.

That also sharpens the one thing worth keeping from a review of this shape: if the line ever stops being printed, #3202's condition silently reverts to "ladder stays" (the conservative direction, verified) — which is why the disclosure note it prints is load-bearing and why keeping the docs quote in sync matters.

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

  • The PR modifies the bit-exactness gate to detect when the device's head runs on the host and disables the int8 head ladder on the integer arm accordingly
  • The PR ensures that both arms now use the same head (f32 host matvec) for comparison, making the gate test the kernel families instead of the heads
  • The PR includes the measurement data from npu-kernels#115 showing the fix resolves the issue

Non-compliant requirements:

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

Requires further human verification:

  • The actual hardware measurements on Strix Halo NPU need to be verified to confirm the fix works as expected
  • The updated behavior should be tested with additional containers to ensure robustness
⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Head Comparison Logic

The script now attempts to detect when the device's lm head runs on the host and disables the int8 head ladder on the integer arm. However, the detection mechanism uses grep -q 'lm head: on the host' which may be brittle and could miss variations in log message formatting. A more robust detection method should be considered to ensure consistent behavior across different log outputs.

head_off=''
grep -q 'lm head: on the host' "$out"/dev*.log 2>/dev/null && head_off=NPU_I8_HEAD_OFF=1
[ -n "$head_off" ] && echo "  note: the device's head runs on the host, so both arms use it and this run says nothing about the int8 head ladder (NPU_I8_HEAD_OFF=1)"

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Merge-order note, measured rather than guessed: git merge-tree --write-tree pr/3199 pr/3202 → rc 1, CONFLICT in scripts/npu-kernel-bitx.sh, the only file both PRs touch.

The collision is the host-arm call. #3199 (READY at f62d15a8, the residual-dump wiring) inserts a dump_layers=1 line immediately above echo "host :"; serve "$out/host" 18151 NPU_INFER_HOST_GEMM=i8 …, and this PR is the one that rewrites that line to add the head_off detection. Neither branch is wrong and the resolution is mechanical:

dump_layers=1   # from #3199: the host arm always dumps
echo "host   :"; serve "$out/host" 18151 NPU_INFER_HOST_GEMM=i8 ${head_off:+"$head_off"} || exit 1

I verified the two compose on the same env line: with #3199's local dumpargs=(), serve expands env "$@" ${dumpargs[@]+"${dumpargs[@]}"} …, i.e. the caller's assignments first and the dump last, so the host arm ends up with NPU_INFER_HOST_GEMM=i8 NPU_I8_HEAD_OFF=1 NPU_INFER_DUMP_DIR=…/host/layers — exactly what both changes want (ran the four call shapes against a stub serve).

So whichever merges second needs one rebase. Not a defect in either PR; posted here so the merge loop does not find it as a surprise.

@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 455fcb1: READY

Second verdict at this head, and I am filing it openly rather than quietly: the CHANGES at 455fcb1 says the greped string has no emitter, and since that verdict I found that the string is documented as the way to see which head ran. Three sources, none of them an inference:

npu/forward/src/q4nx_forward.cpp:7435   static const int head_host_mode = … (!e) return 1 …   ← default: head on the host
npu/forward/src/q4nx_forward.cpp:7458   if (head_host && head_packed && !head_pb16i && !lctx.gemv && H <= KD && T <= ND)
npu/forward/src/q4nx_forward.cpp:7507       fprintf(stderr, "  [forward] lm head: on the host (%s), … ")
docs/npu.md:573                        "The serve log says which it is: `lm head: on the host (default; NPU_HEAD_HOST=0 for the device)`"

and the fourth is the measurement the PR rests on: engine#3196's own log line is that sentence, which is only printed from inside :7458. So on the container that motivated both halves of #3196 the branch is taken and the string appears; head_pb16i being unset there is what makes it reachable, and that is visible in the same measurement.

The four ways out of :7458 are also the four cases where not firing is correct — which is why I think this is a heuristic done right rather than a heuristic at all:

case what happens ladder
target claims Op::lm_head, or lctx.gemv set (:7099, :7103) device computes its own head, no line stays — engine#1808's comparison survives
head_pb16i (ternary image) a different host path below, no line stays
head image unreadable on the host :7514's "the image cannot be read on the host; using the device" — a different string stays
NPU_HEAD_HOST=0 device head, no line stays

I ran the condition itself in four stub worlds before my first READY (device-head-on-host → knob + disclosure; device-head-resident → ladder stays; no log → ladder stays; --repeats' dev1.log → detected), and it is fail-safe toward today's behaviour: if the line ever stops being printed the knob simply never fires.

If there is a container where the device's head runs in f32 without that sentence, it is not one of the four above, and I would rather be shown it than defend this — but a verdict that blocks on "I could not find the emitter" should not stand once the emitter is in the source, in the docs, and in the run the issue is about. Re-open with the case if you have it and I will read it properly.

Also still true from my first verdict: bash -n clean, merge-tree origin/main rc 0, and the merge-order conflict with #3199 (scripts/npu-kernel-bitx.sh, host-arm call) is posted on both threads with the resolution.

@bong-water-water-bong
bong-water-water-bong merged commit ebb691f into main Nov 21, 2026
11 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the pi-issues/3196-gate-head-engine branch November 21, 2026 15:42
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

The conflict I measured earlier, resolved and checked rather than described — this is the exact hunk if #3199 lands first (it is the same hunk in the other order, with dump_layers=1 moving above #3202's block):

dump_layers=1   # from #3199: the host arm always dumps: it is the reference the comparison reads
# engine#3196: … So take the ladder out only in that case …
head_off=''
grep -q 'lm head: on the host' "$out"/dev*.log 2>/dev/null && head_off=NPU_I8_HEAD_OFF=1
[ -n "$head_off" ] && echo "  note: the device's head runs on the host, so both arms use it and this run says nothing about the int8 head ladder (NPU_I8_HEAD_OFF=1)"
echo "host   :"; serve "$out/host" 18151 NPU_INFER_HOST_GEMM=i8 ${head_off:+"$head_off"} || exit 1

Both PRs keep their whole contribution: #3199's dump_layers gate still governs whether serve() arms NPU_INFER_DUMP_DIR, and #3202's knob is still conditional on the device log. The $out/dev*.log glob is unaffected by #3199's dev / dev1..R naming (verified: glob covers both).

Checks on the merged tree, not on either branch: no conflict markers, bash -n clean, and the composed call expands to

host arm -> [NPU_INFER_HOST_GEMM=i8] [NPU_I8_HEAD_OFF=1] [NPU_INFER_DUMP_DIR=…/host/layers]

with grep 'lm head: on the host' matching in the fake dev1.log, i.e. the dump and the knob meet on one env line in the order both authors need ($@ first, then the dump array). Nothing else in either diff overlaps.

bong-water-water-bong pushed a commit that referenced this pull request Nov 21, 2026
One conflict, in `scripts/npu-kernel-bitx.sh`, and it is the two changes to the same
place on purpose: main's #3202 detects `lm head: on the host` in the device log and sets
`NPU_I8_HEAD_OFF=1` for the integer arm, and this branch arms `NPU_INFER_DUMP_DIR` for the
run whose residuals are compared.

Resolved by keeping both, in the order they have to run: main's block computes `head_off`
from the device logs, then `dump_layers=1` arms the host arm's dump, then the host arm starts
with `${head_off:+"$head_off"}`. The device loop above is unchanged (only `r=1` is armed,
which is this branch's own review fix).

That also clears the CI failure: `pre_pr_checks_tree` was failing on `branch_stale_check` --
"the merge with the base conflicts here" -- not on a compile or a test of this branch's own.
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