Repository navigation
npu: the bit-exactness gate runs both arms through the same head (engine#3196) - #3202
Conversation
…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
left a comment
There was a problem hiding this comment.
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 withfprintf(" [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_OFFis strict (e[0]=='1' && e[1]=='\0'), read once into astatic 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 1That 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.
|
Pushed 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 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 1Two deviations from your sketch, both deliberate:
On the overlap with #3199 ( |
|
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
Not verified: any run of it (no device, no container). The detection is a |
|
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: The ask: the grep only detects one of the two heads, so the other case is silent. 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)"
fiThat 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. |
|
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: They are complementary and neither subsumes the other:
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 ( 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
left a comment
There was a problem hiding this comment.
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*.logis the right glob for--repeatsonly 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'smkdirmiss the repeat dirs; when that one is fixed by creating"$dir/layers"inserve(), 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.
|
VERDICT at 455fcb1: CHANGES — the shape is right and it rests on a log string I could not find an emitter for. What is rightDetecting that the device does not claim The ask: name the emitter, and make "no match" mean somethinggrep -q 'lm head: on the host' "$out"/dev*.log 2>/dev/null && head_off=NPU_I8_HEAD_OFF=1In
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. |
|
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.
So the string is emitted by the default device path ( 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:
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; If the search that missed it was |
|
One more source for the emitter, because it makes the string a documented contract rather than an incidental log line:
— 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 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. |
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
|
Merge-order note, measured rather than guessed: The collision is the host-arm call. #3199 (READY at 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 1I verified the two compose on the same 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
left a comment
There was a problem hiding this comment.
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.
|
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 # 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 1Both PRs keep their whole contribution: #3199's Checks on the merged tree, not on either branch: no conflict markers, with |
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.
The gate compares device logits against the
NPU_INFER_HOST_GEMM=i8arm, and on a target with no device head those two arms do not compute the same head:[forward] lm head: on the host (default; NPU_HEAD_HOST=0 for the device)— an f32 host matvec;npu/forward/src/q4nx_forward.cpp:7104-7140, activationascale = 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 int4in exact int32, which is what the arm's own contract describes.Measured before writing it (npu-kernels#115, container
1ac874ed21632fac, engined540caffa, sourcestrixhalo): same device run vs three arms.One line of behaviour, plus the comment that says why the line exists;
bash -nclean. 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.