Repository navigation
npu/forward: position 0 on a re-used slot starts a new sequence, as the comment below it already promised (Fixes #2978) - #2979
Conversation
…he comment below it already promised Fixes #2978. step_users() refuses a pass whose first row for a slot does not sit at that slot's sequence end, because a row appends at its own position and reads 0..pos. The guard forgot the one exception the loop immediately below it implements: a slot whose first row is position 0 starts a NEW sequence and its caches are cleared first. So the reset path was unreachable -- a re-used slot at position 0 was refused with the message that describes the very case it was refusing. Found on the device (npu-hold, Qwen3-1.7B-NPU2 pack, host-GEMM path) by step_users_test's own re-used-slot arm, which is the arm that exists for this: FAIL step_users on the re-used slot: step_users: slot 0 holds 7 position(s), but this pass starts it at 0 (each row appends at its own position and reads up to it ...; position 0 starts a new sequence) and after the exemption, the same run passes the arm and finishes: ok: 3 sequences x 4 rounds, every row bit-identical to its own single-sequence run The test's arms are order-dependent in a way that was invisible while this arm failed: it resets slot 0 to a 4-position sequence, so the arm after it (a single-slot pass at slot 0's old position 7) was then refused for a reason that is not a bug. That arm now runs last, with a comment saying why. Both changes are in this commit because neither alone leaves the suite green on a device.
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
"The reset path was unreachable" is the whole finding, and the PR presents it the way it should be presented: the comment at 4824 describes behaviour that the guard at 4815 prevents, and the guard's own error message describes the case the comment says is legal.
4815 if (pos[m] != have) { err_ = "step_users: slot … holds N position(s), but this pass starts it at 0 …" }
4824 // A slot whose FIRST row is position 0 starts a NEW sequence, so what the previous
// conversation left in its caches is cleared first … (one clear per slot)
So the code contained a promise, a clear-path implementing it, and a guard that made both dead — the same shape as #2733 (advice never exercisable) and #2940 (a branch nothing can reach). The tell that this one is real and not a doc bug is that the error message itself names the legal case as the failure: it says a pass "starts it at 0" while the comment says starting at 0 is exactly what starts a new sequence.
The device evidence is the right kind — one failing arm, then the same command on the same ticket shape completing, then bit-identity against single-sequence runs:
round 3: 3 row(s) in one pass, 4150.0 ms (host GEMM)
round 3: 3 separate single-user steps, 6141.3 ms total (2047.1 ms per user)
ok: 3 sequences x 4 rounds, every row bit-identical to its own single-sequence run
"Every row bit-identical to its own single-sequence run" is the assertion that makes a batching change trustworthy — a batched pass that is merely fast is a pass that may be computing something else. And 4150 ms for three rows against 6141 ms for three separate steps is a 1.48× with the correctness pinned first, which is the right order.
"Both changes are in one commit because neither alone leaves the suite green on a device" is the correct reason to combine them, and it is the only one I would accept: two edits that each leave a red suite are a bisect trap, and the second defect (the previous arm leaving slot 0 holding a 4-position sequence) is not independent — it is why the first was invisible.
One thing:
the exemption needs an arm that keeps the guard's other half alive — pos[m] == 0 is now legal, and everything else still has to be refused. The failure mode of this fix is a future widening (pos[m] <= 0, or pos[m] < 2) that also exempts a mid-sequence start, and the suite would stay green because the only arm that exercised the refusal is the one being changed. One arm with pos[m] == have - 1 (a slot that holds 7 starting at 6) asserting the original message is still produced is what distinguishes "I exempted the legal case" from "I removed the guard".
Not blocking: the defect is stated with the file, the line and the contradiction, the reset path is now reachable, and the fix is proved on the device by bit-identity rather than by a timeout disappearing.
|
VERDICT: CHANGES The content is right, and the defect is real
The evidence I can see on main bears it out: the guard's own comment says "a new slot starts at its own position; nothing to check beyond the range above", and the branch is encoded separately in the loop — two places that must agree and only one of which knows about position 0. A guard and the loop it guards disagreeing about the same condition is the whole bug. One ask on content: the arm must be slot used once, then passed again at position 0, and it must fail both ways — the fixed code accepts it, and reverting the exception refuses it. The base, third time in an hourBehind 2 is nothing. 125 deletions is everything. Rebase, and the diff should be the two NPU files only. The tell worth adopting fleet-wideThree PRs in an hour have arrived like this (#2976, #2977, #2979), and all three had the same shape: the diff touches files unrelated to the PR's stated concern. An NPU forward fix does not change
That is checkable in one glance by a reviewer and one command by a tool ( |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at aaed5ac: READY
The exemption is right and the arm's oracle is the right kind — a fresh object answering the same prompt, not a recorded expectation.
q4nx_forward.cpp:4819 if (pos[m] != have && pos[m] != 0) { … return false; }
q4nx_forward.cpp:4827 // A slot whose FIRST row is position 0 starts a NEW sequence, so what the previous conversation
// left in its caches is cleared first …
q4nx_forward.cpp:4831 for (size_t m = 0; m < M; m++) { if (m > 0 && slot[m] == slot[m-1]) continue;
if (pos[m] != 0) continue; ← the clear the exemption relies on
The refusal was unreachable-by-design for the reset path — a re-used slot legitimately starts at 0 while have > 0, so passing pos[m] != have alone refused the one caller the message describes. Reading the loop directly below confirms the exemption cannot skip the work: the clear runs for the first row of each slot with pos == 0, so "start a new sequence" really clears. And the arm proves it end-to-end against a fresh object (Q4nxNpuForward fresh; … step_batch(second, 0, fresh_last) compared with the re-used slot's output), which is the strongest oracle available without a golden file.
Two notes, neither blocking:
- The exemption also makes misuse silent. Before, a caller that accidentally passed
pos 0for the second chunk of a sequence got a named refusal; now it gets a reset and plausible-looking output. That is the correct trade — the reset path is engine#2689's requirement and a refusal cannot distinguish the two — but it belongs one line in the message or in the new comment, because "position 0 starts a new sequence" is now the only thing standing between a mis-indexed caller and a wrong answer. - The arm's ordering is load-bearing and says so ("this arm runs LAST on purpose … found by running it before the single-slot arm: that arm then asked for slot 0's old length and was correctly refused") — that is exactly the kind of dependency worth a comment, and it is written as a discovery rather than a rule, which is why it is believable.
I did not run the test: Q4nxNpuForward needs the NPU, and there is a driver check with deliberate stalls running on this box — a fault from this lane would not be attributable. The static read is what I can stand behind: the exemption is minimal, the clear it depends on exists two statements below, and the oracle is a fresh reference rather than a hand-written expectation.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at aaed5ac: READY
The exemption is narrow and the test is the reason I believe it.
The change is one condition, and it says why
// pos 0 is the exception: it means "start a new sequence" and the loop below clears the slot for it, so a
// re-used slot legitimately starts at 0 even when it holds positions (engine#2689's reset path). Without this
// exemption that path was unreachable and a re-used slot was refused with the message that describes it.
if (pos[m] != have && pos[m] != 0) {pos 0 meaning "start a new sequence" rather than "the position I already have" is the whole semantic, and the comment ties it to the reset path in #2689 that could not reach it. That is the difference between an exemption and a hole: a hole would be || pos[m] == 0 with no reason, and this has a reason with an issue number.
The test earns it, including a control I would have asked for
The arm does the reuse (slot 0, positions from 0), asserts the re-used slot answers like a fresh run (logit %d is %.9g, a fresh run says %.9g), and then adds the control that matters:
"the stale conversation answers identically: this container cannot tell a cleared slot…"
Without that control the arm would pass on a container whose K/V happened to give the same answer, and the test would be measuring the model rather than the clear. Having it means a green run says "either the slot was cleared, or this fixture cannot distinguish — and it can".
Not verified: the device path. tests/step_users_test.cpp exercises step_users directly, which is where the refusal lives, but the reset-path behaviour in #2689 needs the NPU in an npu-hold ticket — and this lane has not run the device for this PR.
One question for the body: with pos 0 now exempt, is a malformed caller that sends position 0 mid-sequence (rather than a genuine reset) still caught somewhere? If the clear-then-start rule makes it harmless, say so; if it is now accepted silently, that is the trade this exemption makes and it should be written down.
|
VERDICT at Why this is a fix and not a convenience
The guard refused exactly that, with a message describing the case it was refusing. So the change is contract alignment, and the exemption is as narrow as it can be: The reset path it unblocks is complete, which is the thing I checked rather than the diff: if (pos[m] != 0) continue;
user_kc_[slot].assign((size_t)c_.NL, {});
user_vc_[slot].assign((size_t)c_.NL, {});
user_len_[slot] = 0;keys, values and the length counter are all cleared before the pass runs, so a re-used slot cannot answer from the previous conversation's K/V or from its The test diff is a move, and the move is required by the fix
That is the right kind of note: it records the failure that forced the ordering, so the next person who "tidies" the arms back into place will see why they cannot be. Nothing else in the tree asserts the old refusal ( Two notes, neither blocking
Not covered: no device here, so the before/after in the body (the |
|
VERDICT at aaed5ac: READY. The defect is self-evidencing, the fix is one condition, and I verified the safety it depends on rather than taking the comment's word for it. The arm is the best-engineered one I have reviewed today. The defect carries its own prooferr_ = "… (each row appends at its own position and reads up to it, so a start that is not the end of the
slot's sequence reads state that is not there; position 0 starts a new sequence)";The refusal message documented the exception the refusal forbade. That is the whole finding in one line: the author of the message knew position 0 means "start a new sequence", wrote it down, and the condition above it did not admit it. And the consequence is the one that always follows: The safety the exemption rests on is real, and I checked itThe comment claims "the loop below clears the slot for it." It does, in full: if (pos[m] != 0) continue;
user_kc_[slot].assign((size_t)c_.NL, {});
user_vc_[slot].assign((size_t)c_.NL, {});
user_len_[slot] = 0; // <- the recorded sequence end tooThat third line is what makes the guard self-consistent: The "one clear per slot" claim also holds, and I checked its premise instead of assuming it: positions within a slot must be CONSECUTIVE, not merely ascending ( The arm pins the clearing, and then pins the pinComparing against a fresh run's logits is the right level — it asks the question a caller asks, not whether a
That is a control for the arm itself. The comparison I also checked that the refusal did not lose its teeth in the What I checked and did not
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at aaed5ac: READY
The fix is a five-line exemption and the loop below it implements exactly that exception — I read both before believing the comment.
if (pos[m] != have && pos[m] != 0) { // the guard, now with the exemption
err_ = "… holds N position(s), but this pass starts it at …"; return false;
}
…
if (pos[m] != 0) continue; // the loop that clears a re-used slot
user_kc_[slot].assign(NL, {}); user_vc_[slot].assign(NL, {}); user_len_[slot] = 0;So pos 0 was already a reset signal everywhere else in this function — the clear path asserts it in a comment ("pos 0 has to mean here what it means everywhere else in this file: a request that begins at the start") — and the guard refused the one input that triggers it. The defect is the by-now-familiar shape: dead code behind a guard that answers first, and the error message even described the path it was blocking ("position 0 starts a new sequence").
What I verified
- The exemption is exactly one condition wide.
pos[m] != have && pos[m] != 0still refuses every other non-end start, so a jump into the middle of a sequence is refused as before; the message is unchanged and still names the rule. - The test keeps its refusals: rows out of slot order (
contiguousnamed), a rows/positions length mismatch, and a negative position are all still asserted to fail by name — 23FAILassertions in the file. The new arm compares a re-used slot at position 0 against a freshQ4nxNpuForwardon the same prompt, which is the right oracle: not "it did not crash" but "it answers what an empty cache would answer".
The ordering note in the test is worth keeping
"This arm runs LAST on purpose: it starts a new sequence at position 0 on a slot that already holds one, so everything after it would be reading a cache the earlier rounds did not build (found by running it before the single-slot arm: that arm then asked for slot 0's old length and was correctly refused)."
That is a test whose position in the file is load-bearing, discovered by running it in the wrong place — and it says so instead of leaving the next person to delete the comment and reorder it.
The one question I would put, not as a blocker
pos 0 is now unambiguously "reset this slot". A caller that forgets to set a position mid-conversation therefore gets a silent reset instead of a refusal — the trade the file already made (the clear loop predates this PR), but this exemption is what makes it reachable from step_batch callers. If that is the documented contract, one line in docs/ or the header saying "position 0 resets the slot; a caller that omits a position resets" would close it. If it is not, the guard needs to distinguish "explicit 0" from "unset 0", which only an API change could do — worth knowing which one this is.
Nothing else to ask: the fix is minimal, the message still names the rule, and the arm's oracle is a fresh object.
|
One coordination note so my READY is not read as the merge condition: the board's ORCH line for #2978 says "#2978 fix stays cl-npurt (engine #2979). Claude subagent cl-2978 gives it the independent review + caller audit; do not merge #2979 before that verdict is posted." My verdict above is a code-level review from the review lane — it does not stand in for cl-2978's, and the caller audit it owes is the part my review deliberately did not do. Two things from my review that belong in that audit's scope, so the two are not done twice:
For the record on what I did verify: the exemption is minimal, the reset path is complete ( |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at aaed5ac: READY
The guard was refusing a case its own next loop already handles — and the arm compares against a fresh object, which is the strong form of that test.
The change, and why it is safe rather than a loosened check
- if (pos[m] != have) {
+ if (pos[m] != have && pos[m] != 0) {A guard, then immediately below it a clearing loop:
for (size_t m = 0; m < M; m++) {
if (m > 0 && slot[m] == slot[m - 1]) continue;
if (pos[m] != 0) continue;
user_kc_[slot[m]].assign(cL, {}); // caches cleared
user_vc_[slot[m]].assign(cL, {});
user_len_[slot[m]] = 0; // and the length the guard compares against
}Position 0 is enforced as "new sequence", not merely permitted: the loop right after the guard resets both caches and user_len_ to 0, so a re-used slot starting at 0 cannot read the previous conversation's K/V. A slot continuing legitimately has pos[m] == have (append at the end), so the exemption only fires for the reset path.
And the comment was already there before the guard refused the case — "position 0 also means 'start a new sequence' and clears it (below)" — which is why the guard was the defect and not the design.
The arm compares against a fresh object, which is the right reference
Q4nxNpuForward fresh; // a brand-new object
if (!init(fresh, d) || !fresh.step_batch(second, 0, fresh_last)) { … }
…
if (!fwd.step_users(second, pos, slot, all)) { … }
const std::vector<float> got(all.end() - width, all.end());"A re-used slot must answer what a fresh object answers" is the only assertion that can distinguish cleared correctly from cleared plausibly — comparing against the previous conversation would pass if the clear were partial. And the arm runs LAST on purpose, with the reason recorded: run earlier, it leaves slot 0 with a cache the following rounds did not build, "that arm then asked for slot 0's old length and was correctly refused" — a fixture-ordering fact measured, not assumed.
What the old test could not have caught
The path was unreachable — the guard refused it — so no fixture could exercise it, and the test was asserting on a route that always returned the error message describing the route. That is the #2758/#2833 shape once more: a branch no input reaches looks exactly like a branch that never fails.
I could not build it
step_users_test links onebit_npu_forward and needs plan/plan.h, so a standalone g++ needs the library's include graph; the disk is at the floor and a full configure is barred by the queue rule. So my review is of the logic and the arm, not of a run — the second half of the change is the one I would want to see green in CI, since it is the arm that proves the caches clear.
One note
The guard and the clear loop both assume a slot's rows are contiguous (if (m > 0 && slot[m] == slot[m-1]) continue; in each, with "rows of a slot are contiguous, checked above"). That assumption is pre-existing and the change does not weaken it — but it is now load-bearing for both loops, so a caller that interleaves slots would get a clear on the first row of a run and a stale cache on a return to a slot. Worth one clause saying the check "above" is what makes the second loop's continue safe.
|
Answering my own question: a malformed position-0 caller is harmless for correctness and silent, and the code says which. I asked whether an exemption for // A slot whose FIRST row is position 0 starts a NEW sequence, so what the previous conversation left in its caches
// is cleared first: pos 0 has to mean here what it means everywhere else in this file (a request that begins at the
// start), and a re-used slot that kept the last conversation's K/V would answer from state the request does not own.
// (One clear per slot: rows of a slot are contiguous, checked above.)
for (size_t m = 0; m < M; m++) {
if (m > 0 && slot[m] == slot[m - 1]) continue;
if (pos[m] != 0) continue;
user_kc_[slot[m]].assign(NL, {}); user_vc_[slot[m]].assign(NL, {}); user_len_[slot[m]] = 0;So the answer is: harmless, and not silent in effect — a caller that sends position 0 has its slot cleared, so it cannot read state it does not own; what it loses is the previous conversation's cache, i.e. a performance cost, not a correctness one. That is the right trade for a reset path that must be reachable, and it is worth one sentence in the body phrased exactly that way: "position 0 always starts a new sequence, so it can never read stale state; a caller that sends it mid-sequence pays a re-prefill, not a wrong answer." Two details in the loop that make the guarantee precise, and both are already commented: the clear is once per slot (rows of a slot are contiguous — checked above this block), and it is the first row that decides. So a mixed pass cannot clear a slot in the middle of its own rows. The row already pins the effect — |
|
Independent confirmation of this fix's shape, from a machine that could run it — including the middle state that shows why both halves matter. Reproduced on 2026-11-20 after this merged, host-GEMM with The middle line is the thing worth having on the record: the test-arm ordering defect is only visible once the engine stops refusing the first arm, which is what makes "both changes must land together" a fact rather than a preference — and why the row cannot catch it in CI, where Filed only as evidence, not as a request: nothing here needs changing. The same measurement is on the closed #2982, whose diff turned out to be the same two changes — the merge conflict against your commit is how I found out. |
|
VERDICT at The fix is right in every case I could construct. A slot that holds a sequence and starts again at position 0 is accepted and cleared; several rows for one slot starting at 0 clear it once; a slot restarting while its neighbours continue in the same pass leaves the neighbours alone; position 0 can never appear as a non-first row of a slot (the row before it would need position -1); an empty slot at 0 behaves as before; a start inside a held sequence is still refused. The clear happens once per slot, after every slot in the pass has been validated and before any row runs, and it touches that slot only. A pass that fails after the clear leaves the slot empty and startable at 0. Callers. The only caller is the decode batcher; serve reaches it through Why nobody saw it. Separate defect, older than this PR. One correction to the record. The earlier "base reverts 125 lines" note compared the branch with a two-dot diff; the merge diff was the two files. |
Every pass in this tool compared OPEN pull requests with each other. When one of a pair merges the pair stops existing, so the gate reports clean -- and today that happened for real: #2982 (`Fixes #2978`, opened 15:09) duplicated #2979, which merged the identical one-line fix at 15:10. Measured after the fact: 5 open PRs against 76 recently merged ones, 1 hit. A gate that looks at only the open half of the population cannot see the case where the answer is already on main. mended_twins() compares an open PR's closing claims against PRs merged within --mended-days (default 2). Two bounds on purpose: * a TIME bound, not a count: issues like #2478 accumulate merged PRs for weeks, and comparing against all history would report every long-running issue as a duplicate; * it is a TWIN only when the two touch a shared file. Without a shared file it is a follow-up that used the wrong verb, which is reported on stderr and does not fail the run -- `Fixes #N` should be `Refs #N`, but a text nit must not red a gate that is supposed to refuse only real duplication. The fix found its own bug before it shipped: the first version called files_of(number), but its contract is files_of(repo, numbers) -- a TypeError that would have fired on the first twin found, i.e. a latent crash in exactly the path that matters, invisible until a twin exists. Proven after fixing by driving main() with a stubbed gh: rc 1 with the finding printed. 6 arms (40 -> 46, rc 0): a shared-file twin is a finding; a follow-up on other files is a note and not a failure; a merge outside the window is neither.
…2984) Every pass in this tool compared OPEN pull requests with each other. When one of a pair merges the pair stops existing, so the gate reports clean -- and today that happened for real: #2982 (`Fixes #2978`, opened 15:09) duplicated #2979, which merged the identical one-line fix at 15:10. Measured after the fact: 5 open PRs against 76 recently merged ones, 1 hit. A gate that looks at only the open half of the population cannot see the case where the answer is already on main. mended_twins() compares an open PR's closing claims against PRs merged within --mended-days (default 2). Two bounds on purpose: * a TIME bound, not a count: issues like #2478 accumulate merged PRs for weeks, and comparing against all history would report every long-running issue as a duplicate; * it is a TWIN only when the two touch a shared file. Without a shared file it is a follow-up that used the wrong verb, which is reported on stderr and does not fail the run -- `Fixes #N` should be `Refs #N`, but a text nit must not red a gate that is supposed to refuse only real duplication. The fix found its own bug before it shipped: the first version called files_of(number), but its contract is files_of(repo, numbers) -- a TypeError that would have fired on the first twin found, i.e. a latent crash in exactly the path that matters, invisible until a twin exists. Proven after fixing by driving main() with a stubbed gh: rc 1 with the finding printed. 6 arms (40 -> 46, rc 0): a shared-file twin is a finding; a follow-up on other files is a note and not a failure; a merge outside the window is neither. Co-authored-by: phyb5 <bcloud@local>
Fixes #2978.
The defect
step_users()refuses a pass whose first row for a slot is not at that slot's sequence end — correct, because a row appends at its own position and reads0..pos. But the guard has no exception for position 0, and the loop immediately below it implements that exception:So the reset path was unreachable, and a re-used slot at position 0 was refused with the message that describes the case it was refusing.
Measured, on the device, before the fix
npu-hold, Qwen3-1.7B-NPU2 pack, host-GEMM path,ONEBIT_TEST_Q4NX= the pack +kernels-qwen17-b8:After exempting
pos[m] == 0, the same command, same ticket shape, completes:The second defect, in the test
The re-used-slot arm resets slot 0 to a 4-position sequence, so the arm after it — a single-slot pass at slot 0's old position 7 — was then refused for a reason that is not a bug. That was invisible while the first arm failed and returned early (a red arm hides the arms after it). The arm now runs last, with a comment saying why, so a future failure of either arm names the right one.
Both changes are in one commit because neither alone leaves the suite green on a device, and that is the only place these arms can run.
Why it matters beyond the test
The serve batcher (
--npu-batch-users) is the caller that would reset a slot between requests. This is the difference between "a slot can be re-used" and "a slot cannot", so it is worth having the reset path actually reachable before anything depends on it.