Skip to content

mtp: a stop is a stop -- the accepted-prefix loop ends the run, and a full budget pays no verify batch - #997

Merged
bong-water-water-bong merged 5 commits into
mainfrom
npu/mtp-stop-budget
Nov 4, 2026
Merged

bong-water-water-bong merged 5 commits into
mainfrom
npu/mtp-stop-budget

Conversation

@bong-water-water-bong

@bong-water-water-bong bong-water-water-bong commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Root cause

npu/mtp/draft.h run() documents its contract one line above the loop: "on_token returning false stops the loop". The top-of-loop token honoured it; the accepted-prefix loop did not.

if (!emit(d[(size_t)i])) { r.tokens.pop_back(); break; }     // breaks the INNER for, not the while

So a refusal during the accepted prefix walked back out to the top of the while, which then emitted the base's own argmax for that position — the same token the consumer had just refused, a second time — and carried on until max_tokens. For a streaming serve that is the difference between stopping on EOS (or on a client that went away) in the iteration it lands in and stopping one iteration later with a duplicated token.

The same block also ignored the caller's budget: an iteration that entered with exactly one token left paid t1's step, the drafter's chain and a full M=k+1 verify batch, credited the accepted drafts, and emitted none of them.

Fix (three lines of behaviour, npu/mtp/draft.h)

  • a false from on_token sets stopped; the run breaks out of the while, and the refused token stays emitted and committed, exactly as a refusal on t1 at the top of the loop already did (npu/generate.cpp's emit keeps the stopping token too, so this matches the house convention);
  • the budget is checked straight after t1's step, before any draft is made. The step is still taken, so the base has consumed every token the caller was handed — no position is skipped;
  • the end-of-iteration break also honours stopped.

Measured before / after (host, toy base, no device, no weights)

tests/mtp_draft_test.cpp [11] states both defects; the pre-fix header fails 3 of its checks:

arm before (main f63af5f9) after
consumer refuses the 3rd delivery (k=4, perfect stream drafter) 4 deliveries, token #3 delivered twice, run continued 3 deliveries, tokens 3, run ends
k=4 half head, max_tokens = 7 (odd budget, so the 7th token is a bare t1) batches 4 — one verify batch paid for nothing batches 3, consumed 11 = prompt 4 + 7

Reproducer: ninja mtp_draft_test && ./mtp_draft_test → 3 CHECK(S) FAILED on the pre-fix header, all checks passed after. Every pre-existing check [1]–[10] stays green (k=0 bit-identity, rollback fidelity, drafter stream/cursor, per-depth denominator, the 692-column matvec bit-identity).

Also: ctest -R mtp_draft 1/1, mtp_acceptance builds and links against the same header, tools/copyright.py --check rc 0.

No published number moves

The added break can only trigger on an iteration that would have emitted nothing, and the gate runs (prompt 0, 64 tokens, k=4, tau=0) account for 20 iterations = 40 passes = 20 steps + 20 batches — no such iteration existed in them. Acceptance, per-depth tables and byte-identical text are untouched; the saving is one step + one verify batch per run that stops early or fills its budget exactly on t1.

No device was touched for this PR: it is a host-only test-suite change plus three lines of the loop. No issue exists for this; it came out of this lane's own [11] reproducer.

Parity note: this is what the shipped serve path already does

Both behaviours the draft loop was missing are in app/forward_serve.cpp, on the plain decode route that is serving :18097 today:

  • :387 if (!on_text(out, take_held(emit))) break; — a failed delivery ends the loop, it does not walk back out to a
    later iteration;
  • :393 if (n + 1 >= limit) break; — a budget that is already full does not pay for the decode step whose only
    output would be the next position's logits (the same waste, one step there, a step plus a verify batch here).

So #997 is not a new policy: it makes mtp::run() behave the way the route it is meant to replace already behaves when
the owner flips --draft mtp.

… full budget pays no verify batch

run()'s documented contract is "on_token returning false stops the loop", and the top-of-loop token
honoured it while the accepted-prefix loop did not: `if (!emit(d[i])) { r.tokens.pop_back(); break; }`
broke the INNER loop, so the while walked on and handed the consumer the base's own argmax at that
position again -- one delivery past its stop, which is exactly what a streaming serve cannot have
(EOS, or a client that went away, has to end the iteration it lands in).

The same block also ignored the caller's budget: with one token left, the iteration still paid the
step's head commit, the drafter's chain and a full M=k+1 verify batch, credited the accepted drafts,
and then emitted none of them.

Fixed in npu/mtp/draft.h, three lines of behaviour:
  * a false from on_token sets `stopped`, the run breaks out of the while, and the refused token stays
    emitted and committed exactly as a refusal on t1 does (npu/generate.cpp's emit keeps it too);
  * the budget is checked straight after t1's step, before any draft is made -- the step is still
    taken, so the base has consumed every token the caller was handed (no position is skipped);
  * the end-of-iteration break also honours `stopped`.

tests/mtp_draft_test.cpp [11] states both defects, and the old code fails on them:

                                  before                                       after
  consumer refuses the 3rd token  4 deliveries, the 3rd token delivered twice 3 deliveries, tokens 3
  k=4, max_tokens=7 (odd budget)  batches 4 (one paid for nothing)            batches 3, consumed 11

Reproducer: `ninja mtp_draft_test && ./mtp_draft_test` -- "3 CHECK(S) FAILED" before, "all checks
passed" after (the 60-odd pre-existing checks, [1]-[10], unchanged). `ctest -R mtp_draft` 1/1,
`tools/copyright.py --check` rc 0, `mtp_acceptance` builds and links against the same header.

No published number moves: the added break can only trigger on an iteration that would have emitted
nothing, and the gate runs (prompt 0, 64 tokens, k=4) account for 20 iterations = 20 steps + 20
batches = 40 passes, i.e. no such iteration existed. Acceptance, per-depth tables and text identity
are untouched; the saving is one step + one batch per stopped or exactly-full run.
@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit e3f0f1d)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

3 - Partially compliant

Compliant requirements:

  • Every file carries the full Apache-2.0 notice
  • tools/copyright.py validates the notice with --check and can fix it with --fix
  • CI runs a --check step before the build
  • Generator scripts emit the notice themselves
  • LICENSE and NOTICE files are updated with correct copyright lines
  • CONTRIBUTING.md includes a new rule about the notice

Non-compliant requirements:

  • None

Requires further human verification:

  • None

997 - Partially compliant

Compliant requirements:

  • Fix the accepted-prefix loop in npu/mtp/draft.h to properly handle consumer refusal and budget checks
  • Ensure that a false return from on_token stops the loop correctly
  • Ensure that budget is checked before drafting any drafts to avoid unnecessary computation
  • Ensure that the loop breaks correctly when stopped or when budget is full
  • Test with mtp_draft_test.cpp to verify the fix

Non-compliant requirements:

  • None

Requires further human verification:

  • None
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Loop Control Logic

The fix introduces a stopped flag to properly handle the case where on_token returns false, ensuring that the loop breaks out of the while loop correctly instead of just breaking the inner for loop. This prevents the emission of duplicate tokens when a consumer refuses a token during the accepted prefix loop.

bool stopped = false;
for (int i = 0; i < a; i++) {
    if ((int)r.tokens.size() >= opt.max_tokens) break;
    // A false from on_token stops the RUN (run()'s contract, and what a streaming serve needs to stop on
    // EOS or on a client that went away): breaking only this prefix loop walked back out to the top of the
    // while, which then handed the consumer the base's own token again -- one delivery past its stop.
    // The refused token stays emitted and committed, exactly as a refusal on t1 at the top of the loop does.
    const bool keep = emit(d[(size_t)i]);
    take = i + 1;
    if (!keep) { stopped = true; break; }
}
Budget Check Placement

The budget check is now placed immediately after t1's step, before any draft is made. This prevents unnecessary computation when the budget is already full, ensuring that no verify batch is paid for a chain that cannot emit a token.

if ((int)r.tokens.size() >= opt.max_tokens) break;
Loop Termination Condition

The loop termination condition is updated to check both stopped and the budget, ensuring that the loop exits correctly when either condition is met, aligning with the documented contract that a false from on_token stops the loop.

if (stopped || (int)r.tokens.size() >= opt.max_tokens) break;

@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 — I reproduced the three failures the PR claims, by swapping main's header back in under the same test.

Reviewed at f66969d69 (2 files, +65/-3: npu/mtp/draft.h, tests/mtp_draft_test.cpp).

The only evidence that matters here is whether [11] discriminates, so I built tests/mtp_draft_test.cpp against this
head's npu/mtp/* and then again with npu/mtp/draft.h taken from origin/main, same compiler and same flags:

this head:      all checks passed                                      (rc 0)
main's header:  PASS  stop: the refused token is in the output exactly once…
                FAIL  stop: no delivery after the consumer refused a token (the loop ends here)
                FAIL  stop: the consumer is never handed the same token twice by one stop
                INFO  stop: 4 deliveries (refusal at 3), tokens 3, iterations 1, batches 1
                FAIL  budget: no verify batch is paid for a chain that cannot emit a token
                INFO  budget: emitted 7, steps 8, batches 4, consumed 11 for a prompt of 4
                3 CHECK(S) FAILED

Three failures on the old header and none on the new one — the count in the PR body is right — and the INFO lines show
both defects at once: 4 deliveries instead of 3 (the refused token handed back a second time) and 4 batches
instead of 3
(a verify batch paid for a chain that could not emit). The two checks that pass on the old header are the
right ones to pass (stop: the refused token is in the output exactly once), so the test is not just counting failures:
it pins the convention while allowing the bug.

Reading the fix, the three behaviour changes are each the smaller of the two options: stopped propagates out of the
while rather than re-entering it, the budget check sits after t1's step (so the base still consumed every token the
caller was handed — no skipped position), and the end-of-iteration break honours the same flag. The comment on
run()'s contract is now true of both loops, which is the actual defect: the header documented a contract that half the
loop did not keep.

Nit: the new checks live in [11], but the PR body's before/after block describes them as three failures of "the pre-fix
header" without saying which arm produced the budget INFO row; a reader re-running only [11] sees 4 lines of INFO and
has to guess the mapping. One sentence naming [11] there would close it.

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 0de4264

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 3eb9a9a

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 9146460

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit e3f0f1d

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant