Repository navigation
npu: the MTP draft loop -- draft k, verify as one batch, keep the longest prefix, roll back - #785
Conversation
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
Reviewed and run — this is the strongest shape of the three MTP PRs: the loop is a pure state machine with a closed-form
oracle, and the test proves the property that matters (a wrong draft costs time, never text).
Ran at 1c4b7d7c (host-only, no device):
$ cmake -DONEBIT_NPU_HOST=ON … && ninja mtp_draft_test rc 0
$ ./mtp_draft_test
PASS k=8 head=perfect: tokens identical to plain decode
PASS k=8 head=perfect: final state == prompt+emitted (28), no skipped positions
PASS k=8 head=perfect: draft rows never exceeded max_batch_rows (over=0)
PASS k=8 head=perfect: acceptance 24/24, mean accept len 8.00, passes/token 0.25
PASS k=8 head=garbage: tokens identical to plain decode … acceptance 0/192 … passes/token 2.00
PASS k=0: no batched pass, no rollback, same tokens
INFO k=8 perfect: iterations=3 steps=3 batches=3 emitted=24 accepted=24 passes/token=0.250
INFO k=8 garbage: passes/token=2.000 (plain decode = 1.000), rows verified=192
all checks passed rc 0
So all four claims in the body reproduce exactly, including both headline numbers (0.25 passes/token with a perfect
head, 2.00 with a garbage one) and the invariant that every case emits the same tokens as plain greedy decode. That last
one is the whole safety argument for a speculative loop, and it is asserted for every head and every k rather than once.
What I checked in the code, beyond the test: k is clamped to max_batch_rows rather than widening the batch (the
over=0 row is the assertion, and the clamp is where the loop decides rather than where it launches); truncate() is
allowed to refuse and the loop then stops with an error rather than emitting — which is right for a GDN layer whose
recurrence is not position-indexed, and is the honest gap the body names; and the rollback really is a length bump with
no per-iteration state copy.
On the head: this branch is behind — a two-dot diff against current main shows main's newer files as deletions
(docs/hetero-prefill.md, npu/ort/onebit_npu_gemm.cpp, …), which is the behind-branch artifact and not this PR's
content. Its own change is npu/mtp/draft.h, tests/mtp_draft_test.cpp and the CMakeLists.txt registration (+7), and
tools/mtp_head_ref.py is main's copy, not this PR's. I am not adding tprs-ready because the head will move on
update-branch; the verdict will be re-posted at the final head, and per the orchestrator nothing merges before #786 does.
One note for whoever takes the device step: the body's "the head already runs the M=16 twin" is the reason step_batch()
can return all M rows without a new kernel — worth confirming with the real head kernel before the loop is wired to it,
since the loop's correctness assumption is "row i is the model's choice at position i", not merely "M rows came back".
PR Reviewer Guide 🔍(Review updated until commit 2a14750)Here are some key observations to aid the review process:
|
1c4b7d7 to
21b436e
Compare
|
Persistent review updated to latest commit 21b436e |
…gest prefix, roll back
One weight pass over the 27B container is ~0.44 s of decode and the M=16 twins verify up to 16 rows for
the price of one row's stream, so a draft head is worth its cost exactly when it turns "one pass per
emitted token" into "one pass per accepted+1 tokens". This is the policy half of that, with no weights
and no device state in it:
* DraftHead proposes k tokens from the base model's last hidden state and the token just consumed;
* the base model verifies all k as ONE batch, and every draft is checked against the model's own
choice AT THAT POSITION (draft 0 against the last step's logits, draft i against row i-1);
* the longest agreeing prefix is kept, the rest is truncated away, and the loop continues from the
logits of the row it stopped at -- so a fully accepted iteration also yields the next token free.
Rollback is a truncation, not a snapshot: a 400-token state copy per iteration would cost more than the
drafts save, and every cache here is position-indexed (the caller supplies ). A GDN layer's
recurrence is the exception, so BaseModel::truncate() may refuse -- the loop then stops with a clear
error rather than emitting wrong tokens; giving the engine a recurrent-state rollback is the remaining
device-side work, and it is named in the header.
tests/mtp_draft_test.cpp pins the invariants on a toy base model (closed-form truth, perfect / garbage /
prefix-only heads, k = 0,1,2,4,8): emitted text is identical to plain greedy decode in every case, the
final state matches prompt+emitted, draft rows never exceed the batch width, and the accounting is honest
-- a perfect head at k=8 costs 0.25 passes per token against 1.00 plain, a garbage head costs 2.00.
…t report it 'Not Run' ci.yml's build step lists the test targets explicitly, so a target that is not in that list never gets a binary and its ctest entry fails as 'Not Run' -- which is what the build leg hit. This follows the pattern the other test-only targets in this file already use (decision_test, systemone_test, systemone_route_test): the binary is pulled in as a dependency of npu_sampling_test. Adding mtp_draft_test to ci.yml's list is the cleaner form and needs a workflow-scoped push, which this branch does not have.
21b436e to
2a14750
Compare
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
Confirmation at 21b436e3, rebased onto current main (7ba14cde/5a34f624): the tree is identical to the head I
reviewed — git diff --stat 1c4b7d7c 21b436e3 → empty — and build is green.
So the evidence stands: ninja mtp_draft_test rc 0 and all checks passed, including the numbers in the body —
k=8 perfect → acceptance 24/24, passes/token 0.250; k=8 garbage → passes/token 2.000 against plain decode's 1.000;
tokens identical to plain greedy decode in every head/k case; final state == prompt+emitted with no skipped positions;
over=0 on the max_batch_rows bound; and k=0 bit-identical with no batch and no truncation.
tprs-ready applied. If the build fix you mentioned was meant to land here, note that this head's tree is the one I
tested — so if the CI build fails at some later head, that is a different tree and I will re-verdict it rather than
carry this one.
|
Persistent review updated to latest commit 2a14750 |
Plan item 1b (host side, CPU first): the MTP draft loop. Stacked on #784 (item 2's reference); two new files plus a
ctest registration, no device code, no weights.
What it does.
DraftHeadproposes k tokens from the base model's last hidden state and the token just consumed;the base verifies all k as one batch; every draft is checked against the model's own choice at that position
(draft 0 against the last step's logits, draft i against row i-1); the longest agreeing prefix is kept, the rest is
dropped, and the loop continues from the logits of the row it stopped at — so a fully accepted iteration also yields
the next token for free. That is the mechanism that turns "one weight pass per emitted token" into "one pass per
accepted+1 tokens" on the M=16 twins.
Rollback is a truncation, not a snapshot. A 400-token state copy per iteration (~100 MB) would cost more than the
drafts save, and every cache here is position-indexed because the caller supplies
pos— so "forget everything pastN" is a length bump for attention. A GDN layer's recurrence is the exception (it is not position-indexed), so
BaseModel::truncate()may refuse; the loop then stops with a clear error instead of emitting wrong tokens. Givingthe engine a recurrent-state rollback is the remaining piece, and it is the first thing the device step needs.
The engine change this needs (not in this PR, it touches the hot path):
step_batch()returns only the lastrow's logits. Verification needs every row, i.e. the final norm + lm head over M rows in one launch — possible
without any new kernel because the head already runs the M=16 twin ("the head is ONE row of activations" is a
property of the current call, not of the design).
Checked on the host, 65 assertions, toy base model with a closed-form truth and three draft heads
(perfect / garbage / prefix-only), k ∈ {0,1,2,4,8}:
The two INFO lines are the win/cost condition stated as numbers, which is what tomorrow's real-head measurement has to
beat: with FLM-class acceptance the pass count per token has to land under 1.00 against the current 1.00.
Not merged here. Nothing in it needs an NPU, and nothing in it changes the decode path until a caller opts in.