Repository navigation
npu: the live MTP loop fed the drafter a truncated stream (its cursor, not the commit) - #820
Conversation
…ainst a truncated stream
The live speculative loop measured 0.62-0.70 accept-len against the offline protocol's 2.19 on the same
prompts, with identical text. The commit was never the problem; the drafter's own stream was. Three
bookkeeping faults in the drafter's cursor, all silent:
* prime() appended a row and left the cursor at 0, so the 8 primed prompt rows had no position;
* draft() opened the chain with cache.truncate(cursor) -- truncate(0) on the first iteration, deleting the
whole primed stream before the first chain;
* commit_rows() appended the accepted rows after the chain's provisional rows instead of over them, at rope
positions have+nd..., so from iteration 2 the cache was a mixture of rejected drafts and double-numbered
positions.
Plus one engine-side half: commit_verify_rows() left all verify rows in last_hidden_rows(), so the loop's own
size check failed and it skipped the drafter advance without a word -- the accepted prefix is the only thing a
drafter may advance over, and now that is what it gets.
The cursor is now a named object (DraftStream) the loop can check instead of a loose int, the drafter lives in
npu/mtp/mtp_head.{h,cpp} behind a next_row() seam (the serve route needs the class, and the cursor has to be
testable without the 1.6 GiB head), and Stats carries per-depth acceptance so "live equals offline" can be
reported the way the offline protocol reports it.
Host check (tests/mtp_draft_test.cpp section [8], no weights, no device): a toy head that knows the truth
exactly when its cache is the base model's stream.
primed (fixed): rows 28 == prompt+emitted, 0 mismatches, 20/20 drafts accepted, passes/token 0.417
legacy_prime (the old path, kept in the test so the bug stays stated): 24 mismatches, 0/96 accepted,
passes/token 2.000, still text-identical
Real-model numbers need the device run (TMTPLOOP-STATE, step 3).
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 at bfea12e (tprs-ready applied)
Built the target and ran it on this box — ONEBIT_NPU=ON -DONEBIT_NPU_RUNTIME=own -DONEBIT_NPU_RT_DIR=…, gcc-16,
Release, under thermal-run.sh, 0 warnings in the whole build, mtp_draft_test compiles clean at this head.
$ ./build/mtp_draft_test -> all checks passed rc 0
$ ctest -R mtp_draft -> 1/1 tests passed rc 0
And the red/green table in the body reproduces digit for digit (the numbers are the test's own INFO lines):
INFO stream primed k=4: rows=28, mismatches=0, accepted 20/20, passes/token 0.417
INFO stream legacy k=4: rows=24, mismatches=24, accepted 0/96, passes/token 2.000
so the cursor is what moves a live draft from 0/96 to 20/20 here, and the legacy path is reported out of step
rather than silently bad. All eight stream: assertions pass, including the drafter's committed rows == prompt +
emitted (28 = 4-token prompt + 24 emitted) and per-depth counters add up to the totals. The pre-existing checks
(k=8 perfect 0.250, half head 12/24, garbage 2.000, tau gate, rollback) are unchanged and green.
Read, and correct as far as I can see:
prime()truncates tokeep(), then checksat(pos)and advances only on a match — the primed-prompt-then-
truncate(0)bug cannot recur.draft()cuts back to the committed prefix before opening a chain, and chains fromstream_.keep(), so the chain
never lands after the previous chain's provisional rows.commit_rows()cuts back tokeep()and appends the accepted rows from there — accepted rows replace the chain.mtp_head.cpp'sq4nx_forward.cpphalf (+7):stride = last_hidden_rows_.size() / verify_rows_isHon this path
(last_hidden_rows_ = norm_allisM*HwithM == verify_rows_), guarded byverify_rows_ > 0, and an empty
result is legal (nothing accepted ⇒ no drafter advance) while a partial batch now fails loudly — which is the
silence that hid this for a day.- the owner-of-that-file cross-check is real, not cited:
TBONSAI-SPEED-STATE.md:5842-5860reviews the same
commit_verify_rowstruncation and reaches the same stride conclusion.
One nit, not a blocker: a pos the cursor disagrees with is counted (mismatches_) and drafting continues from the
lagging cursor. That is the right choice for a measurement tool, but the serve route this class is being moved for
should assert stream_mismatches() == 0 rather than inherit the tolerance — a one-line if at the call site when
#820's serve half lands.
Not verified, as the body says: the real-model per-depth acceptance (--offline vs --live, 8 prompts × 64 tokens).
That needs the array alone and the body already records why it was not held (aie2_legacy_tdr_detect at 09:48:51 while
another lane's gate unit had the GPU token — two processes on the array). Mine to note only because #820 gates the
owner's serve switch: the host half of that switch is now proven and the device half is still owed a clean window.
Apologies for the order — this was the first PR in my queue and it is the last verdict I could post: my first two build
attempts were killed by the box's thermal guard (88 °C with another lane's llama-bench on eight cores, then 93 °C
ABORT), and the third needed cmake --build because this box's cmake defaults to Unix Makefiles, not Ninja.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at bfea12e (restated for the waiter: same head, verdict and tprs-ready already on it)
Gate state, checked now: state=OPEN, isDraft=false, headRefOid=bfea12ec02efa010c0e9b4be85ddc7d9427323c6
— unchanged since my review at 14:29:40Z, mergeStateStatus=CLEAN, label tprs-ready present, and the review's
commit_id is that same head.
Evidence was built and run here, not quoted: cmake --build build --target mtp_draft_test -j 2 (gcc-16, Release,
ONEBIT_NPU=ON, 0 warnings) → ./build/mtp_draft_test all checks passed, ctest -R mtp_draft 1/1, and the body's
red/green table reproduced digit for digit (primed rows=28, mismatches=0, accepted 20/20, passes/token 0.417 vs
legacy 24, 24, 0/96, 2.000).
Nothing has moved on this branch since that build, so the verdict applies as posted.
The PR's own fix is "take the FIRST `accepted` rows of either documented shape", and nothing tested that. Measured,
by injecting into npu/mtp/draft.h at this head:
* `head->commit_rows(accepted, rows, (int)got)` (hand over the whole batch, not the accepted prefix) -> 111 checks
PASS, `all checks passed`;
* `head->commit_rows(accepted, rows.end() - take*h, take)` (the batch's LAST `take` rows) -> same, green.
Both are exactly the #820/#1138 defect class -- a drafter that advances over rows the base rejected -- and both were
invisible for two structural reasons: [13] runs the whole-batch shape with a PERFECT drafter, so accepted == nd and
every slice of the rows looks identical; and [14]'s `RandomHead` has no rows to inspect, so it can only see text,
positions and base state.
[15] closes it with partial acceptance on the whole-batch shape and a drafter that records what it is asked to
commit: `StreamDrafter` gains an optional flub rate (a wrong draft now and then, so the chain is rejected at depth
2-3) and a `commit_rows` override that records the hidden row it is handed before delegating. The assertion is
shape-agnostic -- consecutive commits must be consecutive stream positions -- so it catches a wrong count AND a wrong
slice, and it does not depend on where in the stream the run started:
PASS prefix rows: the run completes with partial acceptance on the whole-batch shape
PASS prefix rows: text identical to plain decode
PASS prefix rows: acceptance really is partial (otherwise this case proves nothing)
PASS prefix rows: the drafter's stream advanced over the committed rows only
PASS prefix rows: every row the drafter committed is the next stream position (no gap, no reject)
PASS prefix rows: no chain opened off the cursor
INFO prefix rows: stream 28 rows, committed 24, accepted 11/56
Red on both injections, at this head, with the same test file:
INJ1 (whole batch): FAIL every row ... is the next stream position (committed 46 for a 28-row stream)
INJ2 (last rows): FAIL every row ... is the next stream position (committed 24, wrong positions)
Correct code: 113 checks pass, `ctest -R mtp_draft` 1/1, copyright clean. Test-only, no device.
… instead of failing the run BaseModel::last_hidden_rows() documents M rows (the whole verify batch); Q4nxNpuForward truncates them to the accepted prefix in commit_verify_rows (#820). run() demanded the second shape exactly, so a base that keeps its batch buffer -- the shape the header's own wording describes, and what a new target writes first -- failed the whole run at the first partial accept, with an error naming a *partial* buffer it had not received. Row m is the hidden after the token at start_pos+m in both shapes, so the loop now takes the first `accepted` of either and fails loudly on any other count.
The PR's own fix is "take the FIRST `accepted` rows of either documented shape", and nothing tested that. Measured,
by injecting into npu/mtp/draft.h at this head:
* `head->commit_rows(accepted, rows, (int)got)` (hand over the whole batch, not the accepted prefix) -> 111 checks
PASS, `all checks passed`;
* `head->commit_rows(accepted, rows.end() - take*h, take)` (the batch's LAST `take` rows) -> same, green.
Both are exactly the #820/#1138 defect class -- a drafter that advances over rows the base rejected -- and both were
invisible for two structural reasons: [13] runs the whole-batch shape with a PERFECT drafter, so accepted == nd and
every slice of the rows looks identical; and [14]'s `RandomHead` has no rows to inspect, so it can only see text,
positions and base state.
[15] closes it with partial acceptance on the whole-batch shape and a drafter that records what it is asked to
commit: `StreamDrafter` gains an optional flub rate (a wrong draft now and then, so the chain is rejected at depth
2-3) and a `commit_rows` override that records the hidden row it is handed before delegating. The assertion is
shape-agnostic -- consecutive commits must be consecutive stream positions -- so it catches a wrong count AND a wrong
slice, and it does not depend on where in the stream the run started:
PASS prefix rows: the run completes with partial acceptance on the whole-batch shape
PASS prefix rows: text identical to plain decode
PASS prefix rows: acceptance really is partial (otherwise this case proves nothing)
PASS prefix rows: the drafter's stream advanced over the committed rows only
PASS prefix rows: every row the drafter committed is the next stream position (no gap, no reject)
PASS prefix rows: no chain opened off the cursor
INFO prefix rows: stream 28 rows, committed 24, accepted 11/56
Red on both injections, at this head, with the same test file:
INJ1 (whole batch): FAIL every row ... is the next stream position (committed 46 for a 28-row stream)
INJ2 (last rows): FAIL every row ... is the next stream position (committed 24, wrong positions)
Correct code: 113 checks pass, `ctest -R mtp_draft` 1/1, copyright clean. Test-only, no device.
… instead of failing the run (#1138) * npu: the draft loop advances over either documented hidden-row shape, instead of failing the run BaseModel::last_hidden_rows() documents M rows (the whole verify batch); Q4nxNpuForward truncates them to the accepted prefix in commit_verify_rows (#820). run() demanded the second shape exactly, so a base that keeps its batch buffer -- the shape the header's own wording describes, and what a new target writes first -- failed the whole run at the first partial accept, with an error naming a *partial* buffer it had not received. Row m is the hidden after the token at start_pos+m in both shapes, so the loop now takes the first `accepted` of either and fails loudly on any other count. * tests: the draft loop's invariants over randomised inputs, which is how the row-shape defect was found [1]-[13] are hand-picked shapes; the defects that survive them are the ones nobody chose. [14] draws 600 deterministic cases over k, tau, budget, prompt, batch width, head accuracy and BOTH last_hidden_rows() shapes, and asserts text identity with plain decode, exactly prompt+emitted positions consumed, no chain wider than max_batch_rows, and no verify rows left uncommitted. On main's draft.h it reports 147 of 600 violated (all the whole-batch-row cases, via the run aborting); with the previous commit, 0 of 600. Fixed seed, <0.1 s, so it costs CI nothing. * tests: the sweep's toy base carries a recurrent state, so a wrong commit prefix cannot pass unnoticed ToyBase modelled the position bookkeeping only: its logits come from the position, so a commit_verify() that kept the WRONG rows left a state no test could see -- while on a real GDN model that is exactly the deferred-commit contract (#805 P6) and exactly what a slip costs. An opt-in recurrence (off by default: [1]-[13] pin shapes that assume the position-only double) computes each batch row's state without applying it and applies only the accepted prefix. [14] now also asserts the loop leaves the same state plain decode does. Failure mode proven by injection: committing the batch's LAST row instead of the accepted prefix reports 306 of 600 violated; correctly, 0 of 600. * tests: the suite can now see a drafter advanced over the wrong rows The PR's own fix is "take the FIRST `accepted` rows of either documented shape", and nothing tested that. Measured, by injecting into npu/mtp/draft.h at this head: * `head->commit_rows(accepted, rows, (int)got)` (hand over the whole batch, not the accepted prefix) -> 111 checks PASS, `all checks passed`; * `head->commit_rows(accepted, rows.end() - take*h, take)` (the batch's LAST `take` rows) -> same, green. Both are exactly the #820/#1138 defect class -- a drafter that advances over rows the base rejected -- and both were invisible for two structural reasons: [13] runs the whole-batch shape with a PERFECT drafter, so accepted == nd and every slice of the rows looks identical; and [14]'s `RandomHead` has no rows to inspect, so it can only see text, positions and base state. [15] closes it with partial acceptance on the whole-batch shape and a drafter that records what it is asked to commit: `StreamDrafter` gains an optional flub rate (a wrong draft now and then, so the chain is rejected at depth 2-3) and a `commit_rows` override that records the hidden row it is handed before delegating. The assertion is shape-agnostic -- consecutive commits must be consecutive stream positions -- so it catches a wrong count AND a wrong slice, and it does not depend on where in the stream the run started: PASS prefix rows: the run completes with partial acceptance on the whole-batch shape PASS prefix rows: text identical to plain decode PASS prefix rows: acceptance really is partial (otherwise this case proves nothing) PASS prefix rows: the drafter's stream advanced over the committed rows only PASS prefix rows: every row the drafter committed is the next stream position (no gap, no reject) PASS prefix rows: no chain opened off the cursor INFO prefix rows: stream 28 rows, committed 24, accepted 11/56 Red on both injections, at this head, with the same test file: INJ1 (whole batch): FAIL every row ... is the next stream position (committed 46 for a 28-row stream) INJ2 (last rows): FAIL every row ... is the next stream position (committed 24, wrong positions) Correct code: 113 checks pass, `ctest -R mtp_draft` 1/1, copyright clean. Test-only, no device. --------- Co-authored-by: bong-water-water-bong <[email protected]>
The live MTP draft loop was drafting against a truncated stream
The drafter keeps its own KV stream (the MTP layer attends over its own states — that is why priming it
position by position took offline acceptance from 3 % to 69 %). Its quality is therefore one number: how many
committed rows its cache holds, and that the row about to be appended belongs at that position.
MtpDrafterhad that number as a loose
int pos_, and it was wrong in three places, all intools/mtp_acceptance.cpp:prime()draft()cache_.truncate(pos_)truncate(0): the whole primed prompt is deleted before the first chaincommit_rows()pos_without dropping the chain's provisional rowshave+nd…, not over the chain athave…So every live draft was made against a stream of length 1–5 instead of 9–70: a drafter that sees only the
current chain, which is exactly the measured 69 %→14 % / 2.19→0.62 gap. The emitted text was never wrong (the
base path is untouched) — a bad draft only costs passes.
The fix
npu/mtp/draft.h:DraftStream— the cursor as a named, checkable object (at(pos),keep(),commit(pos, keep_rows)) with the two silent mistakes it exists to catch written down. Plus per-depth statsdrafted_at[]/accepted_at[]/hit_at(d), and the loop now fails loudly if a base returns a partialbatch hidden instead of silently skipping the drafter advance (that silence hid this for a day).
npu/mtp/mtp_head.{h,cpp}:MtpDraftermoved out of the measurement tool into the library — the serve routeneeds the same class, and the cursor has to be testable without the 1.6 GiB head.
prime()advances thecursor;
draft()cuts back to the committed prefix and counts a chain opened at the wrong position;commit_rows()cuts back to the committed prefix before appending the accepted rows.npu/forward/src/q4nx_forward.cpp(the one engine-side touch, 7 additive lines):commit_verify_rows()truncateslast_hidden_rows_to the accepted prefix. Without it the draft loop's ownsize check failed on every partial accept and the drafter advance was skipped silently. It touches verify state
only, so plain decode cannot move. Reviewed and confirmed correct by the owner of that file
(
TBONSAI-SPEED-STATE.md, 2026-11-02 09:40).tools/mtp_acceptance.cpp: thinMtpDrafterwiring,--liveprints committed rows + cursor mismatches perprompt and a per-depth acceptance table, so "live vs offline per depth" is one run; stdout line-buffered so
a killed run still says how far it got.
Red / green (host, no weights, no device)
Toy head that only knows the truth when its cache is the base model's stream; same loop, same options, k=4,
tau=0, 24 tokens over a 4-token prompt:
prime()advances)legacy_prime(old path, kept in the test)Text identical to plain decode in both.
ctest -R mtp_draft→ 1/1 passed (the pre-existing checks + 7 newones) in a Release build with
ONEBIT_NPU=ON, gcc-16.Device confirmation: NOT yet claimed
The real-model per-depth comparison (8 prompts × 64 tokens, k=4,
--offlinevs--live) is the remainingdeliverable number and is not in this PR yet. Measured this session, as the in-session baseline:
--offline, prompts 0–1, 64 tokens, k=4: draft 1–4 = 58.5 / 47.9 / 36.2 / 15.4 %, 2.58 expected tokens/pass(the 8-prompt aggregate bar from the 2026-11-02 06:15 window is 69.4 / 58.2 / 49.6 / 41.4, 3.19). The
--liverun on the same prompts was killed by the 3-minute ticket limit on an array TDR (aie2_legacy_tdr_detect,09:48:51, Context IDs 6 and 8) that coincides exactly with another lane's NPU gate unit running on the GPU
token (09:48:13–09:49:41): two processes on the array at once. Nothing here blames that run for this patch — it
blames a claim-discipline collision — and no live acceptance number is claimed until the array can be held
alone. The window rule this lane runs under: one
npu-holdticket at a time, ≤ 3 min, sealed M=16 pb16i ELFs,amdxdnacounter checked after every ticket,:18097never touched.