Skip to content

tests: the toy base's last_hidden() follows a batch, as the real forward's does - #1132

Merged
bong-water-water-bong merged 2 commits into
mainfrom
npu/mtp-toy-hidden
Nov 5, 2026
Merged

bong-water-water-bong merged 2 commits into
mainfrom
npu/mtp-toy-hidden

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Root cause

The mtp_draft test double (ToyBase, tests/mtp_draft_test.cpp) updated hidden_ in step() only. The real forward
also updates it on a batched pass: step_batch_all() ends with

last_hidden_rows_ = norm_all;          // per-row normed hidden, for a drafter that must advance too
...
last_hidden_ = fnorm;  // for last_hidden(): the same vector the head reads, last position only

(npu/forward/src/q4nx_forward.cpp:4029 and :4040).

So after any batch the double reported a hidden from the previous single step — a forward that cannot exist. It is not
a cosmetic mismatch: a test asking "what is the hidden after a batch" would read a stale value and certify the wrong
thing.

Found while pricing a folded draft loop, where there is no plain step at all and the batch's rows are the only hidden
source there is; the fold study is not part of this PR.

Red on main, green after

One line added to the double, plus the contract asserted in a new case [12]:

pre-fix : FAIL  toy contract: after a batch last_hidden() is the batch's last row, as the real forward's is
          1 CHECK(S) FAILED
post-fix: ctest -R mtp_draft -> 1/1 Passed
          ./mtp_draft_test      -> all checks passed   (whole suite, 60-odd pre-existing checks + the 9 new ones)

The adjacent trap, asserted rather than smoothed over

commit_verify_rows() truncates last_hidden_rows_ to the accepted prefix but deliberately leaves last_hidden_ on
the full batch's last row
(npu/forward/src/q4nx_forward.cpp:4100). After a partial accept the two therefore
disagree by construction, and last_hidden() names a row that was rolled back — last_hidden_rows() is the one that
follows the commit. A drafter written against the wrong one would commit a rejected row, so [12] asserts both halves:

  • after a batch, last_hidden() is last_hidden_rows().back();
  • after a partial commit, last_hidden_rows() is truncated to the accepted prefix and last_hidden() still holds the
    full batch's last row.

The double keeps the engine's asymmetry faithfully instead of "fixing" it, because the asymmetry is real.

Scope

Test-only: tests/mtp_draft_test.cpp (+36/−0). No production code, no behaviour change, no device. tools/copyright.py --check clean; built and run at main = 55cfaf89.

…ard's does

The `mtp_draft` test double (ToyBase) updated hidden_ only in step(), so after a batched pass last_hidden()
still held the previous single step's value. The real forward does the opposite: step_batch_all() ends with
`last_hidden_ = fnorm;` (npu/forward/src/q4nx_forward.cpp:4040), i.e. the batch's own last row
(norm_all[M-1]), and last_hidden_rows() carries every row.

So the double modelled a forward that cannot exist. Any test asking "what is the hidden after a batch" read a
stale value and would have certified the wrong thing -- which is how this was found: pricing a folded draft
loop, where the batch's rows are the only hidden source there is.

Red on main, green after (one line added to the double, and the contract asserted):

  pre-fix : FAIL  toy contract: after a batch last_hidden() is the batch's last row, as the real forward's is
            1 CHECK(S) FAILED
  post-fix: ctest -R mtp_draft -> 1/1 Passed; whole suite "all checks passed"

The commit also pins the adjacent asymmetry rather than smoothing it over, because it is the same trap one step
further on: commit_verify_rows() truncates last_hidden_rows() to the accepted prefix but deliberately leaves
last_hidden_ on the FULL batch's last row (q4nx_forward.cpp:4100). After a partial accept the two therefore
disagree by construction, and last_hidden() names a row that was rolled back. New case [12] asserts both facts
-- batch sets it, partial commit does not -- so a drafter cannot be written against the wrong one without a red
test.

Test-only change: no production code, no behaviour, no device.

@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: READY at e334c263 — the two claims it makes about the real forward are both true at main, and the test builds and passes here.

Claim 1, the one that was wrong in the double:

npu/forward/src/q4nx_forward.cpp:4040   last_hidden_ = fnorm;   // for last_hidden(): … last position only

so step_batch_all() does advance the visible hidden to the batch's own last row, and a toy that only updated it in step() modelled a forward that cannot exist. That is the defect.

Claim 2, the asymmetry it deliberately keeps:

:2200   if (verify_rows_ > 0 && !last_hidden_rows_.empty()) {
:2201       const size_t stride = last_hidden_rows_.size() / (size_t)verify_rows_;
:2202       last_hidden_rows_.resize(stride * (size_t)accepted);

commit_verify_rows() truncates last_hidden_rows_ to the accepted prefix and never touches last_hidden_ — so after a partial accept the two disagree by construction and last_hidden() names a row that was rolled back. The PR asserts that rather than smoothing it, with the reason spelled out ("a drafter that advanced from last_hidden() after a commit would commit a rejected row").

That decision is the best thing here. The easy version of this PR is to make the toy agree with itself — have commit_verify_rows() update both — and it would have removed the exact trap the real engine has. Keeping the drift and pinning it in a check is what makes the double worth having: a test double that drifts silently certifies the wrong thing, which is what the original bug already was once.

Built and run at this head:

$ ninja -C build mtp_draft_test   -> BUILD_RC=0
$ ./build/mtp_draft_test          -> all checks passed

No code touched (one test file), so there is nothing else to regression-check.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant