Repository navigation
tests: the toy base's last_hidden() follows a batch, as the real forward's does - #1132
Conversation
…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
left a comment
There was a problem hiding this comment.
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.
Root cause
The
mtp_drafttest double (ToyBase,tests/mtp_draft_test.cpp) updatedhidden_instep()only. The real forwardalso updates it on a batched pass:
step_batch_all()ends with(
npu/forward/src/q4nx_forward.cpp:4029and: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]:The adjacent trap, asserted rather than smoothed over
commit_verify_rows()truncateslast_hidden_rows_to the accepted prefix but deliberately leaveslast_hidden_onthe full batch's last row (
npu/forward/src/q4nx_forward.cpp:4100). After a partial accept the two thereforedisagree by construction, and
last_hidden()names a row that was rolled back —last_hidden_rows()is the one thatfollows the commit. A drafter written against the wrong one would commit a rejected row, so
[12]asserts both halves:last_hidden()islast_hidden_rows().back();last_hidden_rows()is truncated to the accepted prefix andlast_hidden()still holds thefull 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 --checkclean; built and run atmain=55cfaf89.