Skip to content

feat(speculative): add opt-in SuffixDrafter and online E[tok]/ms DraftPolicy for Lightning MTP - #5509

Closed
masterfasa wants to merge 9 commits into
jundot:mainfrom
masterfasa:pr/mtp-suffix-drafter-and-draft-policy
Closed

masterfasa wants to merge 9 commits into
jundot:mainfrom
masterfasa:pr/mtp-suffix-drafter-and-draft-policy

Conversation

@masterfasa

Copy link
Copy Markdown
Contributor

Summary

Adds an opt-in (OMLX_MTP_DRAFT_POLICY=1 or model._omlx_mtp_draft_policy = True, off by default) adaptive speculative controller that combines zero-cost n-gram suffix lookup with the model's draft head (DSpark / Lightning MTP):

  • omlx/speculative/suffix_drafter.py (SuffixDrafter): 4-way trigram + 8-gram ring lookup over prompt + generated history (2.2-5.4 us p50 latency up to 98K tokens, 0 mismatches vs reference across 18,000 proposals).
  • omlx/speculative/draft_policy.py (DraftPolicy): Online E[tok]/ms optimizer choosing per verify step between {None, SuffixLookup, Drafter} across 4 lookup buckets, supporting both empirical EMA (Mode A) and calibrated head confidence state.draft_conf survival products (Mode B), plus fair joint budgeting for multi-stream batches and up to 16-row lookup verify windows.
  • 100% greedy-output-invariant: Switching between lookup windows and neural drafter windows preserves exact greedy outputs (test_lookup_and_drafter_windows_alternate_without_drift).

(Note: includes the standalone predraft committed-row fix from #5508; happy to rebase once that merges.)

Real-Hardware Impact (Apple M5 Pro 64 GB, Inkling-2-Mini-oQ4e-mtp and oQ3e-mtp)

  • On code-edit rewrites: 393/398 (98.7%) suffix-lookup acceptance on oQ4e-mtp (85.4 tok/s) and 389/392 (99.2%) on oQ3e-mtp (98.5-103.7 tok/s).
  • On structured JSON extraction: 70.3 tok/s (oQ4e-mtp) and 89.8-95.9 tok/s (oQ3e-mtp).

Verification

  • pytest tests/test_suffix_drafter.py tests/test_draft_policy.py tests/test_dflash_batched.py tests/test_dflash_model_types.py tests/test_mlx_lm_mtp_patch.py: 530 passed, 0 failed.

Nii Moi Addo and others added 9 commits November 21, 2026 14:25
…itted row

A block drafted before the host reads the acceptance holds every verify row
as context, of which only the accepted prefix commits. The attention masks
honour that count, but a recurrent Markov head (Inkling 2's DSpark) was
handed the last verify row as its newest committed state: after a partial
accept, that is the row of a draft the target rejected. The adopted block
then differed from the block drafted from the committed rows, on every cycle
that follows a rejection.

The head now gets the row at the committed count. Output was never affected
(the target verifies every draft); the drafts were.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
(cherry picked from commit b2173bb)
SuffixDrafter proposes the tokens that followed the longest earlier
occurrence of the current suffix (Basalt's rule: every trigram and every
8-gram keeps its 4 most recent ends, longest match wins, the long key first,
newest on ties, min 3 / max 32). A bulk append is indexed with numpy and
later tokens go into two dicts, so a 98K prompt indexes in 15 ms and a
proposal costs 2-5 us at any history length. A continuation that reaches the
end of the history goes on through its own period.

DraftPolicy picks, per verify step, none / lookup with k / drafter with k by
expected committed tokens per millisecond: lookup acceptance per match-length
bucket, drafter acceptance per position or from stated chances calibrated per
bin, a measured scale x base cost per window size with the drafter's cost
split off, a minimum gain over plain decoding and a margin for lookup over
the drafter. allocate_batch_verify_budget spends the verify rows of several
streams where drafts survive.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
(cherry picked from commit e4cf3ac)
…rify step

With OMLX_MTP_DRAFT_POLICY=1 (or _omlx_mtp_draft_policy on the model; off by
default), each verify window comes from the suffix lookup or the draft head,
whichever DraftPolicy expects more committed tokens per millisecond from. It
replaces the context copy's fixed match thresholds for block drafters
(Inkling 2 DSpark, DFlash) and embedded MTP heads (Qwen, DeepSeek).

A lookup window starts no drafter pass: the rows it commits are queued as
drafter context and read with the next block, and while the lookup expects
to continue, the block that would be drafted ahead of the host's read is not
started. Rows that share a verify forward take the window that pays over all
of them. A Markov head that exposes block_confidence() gets each block's
window from its stated chances, calibrated against what the target accepts.

The target verifies every draft whatever its source, so output is unchanged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
(cherry picked from commit 76a1f5b)
A lookup with no drafter to beat must clear the minimum gain over plain
decoding; a lookup window keeps the policy's width while the drafter's depth
controller sits lower; a confidence block is drafted whole whatever that
controller's depth.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
(cherry picked from commit d274996)
A block drafter's target takes lookup windows of its block size. One that
declares a wider verify (_omlx_mtp_max_verify_rows) now takes them up to that
width: a quoted stretch is worth 16 rows where the drafter's block is 8. A
window wider than the block starts no pre-draft, whose ring has spare slots
for one block only.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
(cherry picked from commit 0c4ff4a)
…nt budget

Two findings of a replay on real token streams.

A drafter worth 9% over plain decoding sat parked most of the time: its gain
estimate dips under the 5% guard on a short unlucky streak, and a parked
drafter is not measured again for 48 rounds. The guard now gates starting to
draft; a drafter already running keeps running while it beats plain at all.

The joint budget that commits the most tokens in total gives every row to the
stream quoting its context, and the stream beside it then commits one token
per long pass: 64% of its plain rate in the tested pair. fair=True maximizes
the product of the streams' rates among the allocations that leave no stream
slower than plain decoding.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
(cherry picked from commit 4308f68)
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
(cherry picked from commit b9e6ba0)
A drafter that sets state.draft_conf itself (Inkling 2's screened DSpark head
keeps one lazy array per position) is read like the block_confidence hook's
array, and can ask for whole blocks with states_chances.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
(cherry picked from commit 44ee175)
…aps its draft in

A trial merge with the screened DSpark head's drafter subclass showed three
assumptions of these tests: the proposal recorder's argument count, the
block_confidence hook (now tested on a bare DFlashDrafter), and exact block
equality after 16-row windows, where another grouping of context rows through
a bf16 projection flipped one near-tie in 37 blocks (drift would not stop at
one). Also covers stated chances kept as one array per position.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
(cherry picked from commit 5564ae6)
@jundot

jundot commented Nov 22, 2026

Copy link
Copy Markdown
Owner

#5528, merged today, turns on for Inkling the context copy drafter that oMLX has had since #4104, and lets a verbatim copy run up to 15 tokens past the DSpark block. Your 16-row lookup window here points the same way, so I want to explain why I extended the existing drafter instead of merging this. Thank you for this PR and for the measurements across the series.

There are three reasons. It is off by default behind an environment variable, and I removed env switches from production code in #4396, so most users would never see the gain. It adds a second suffix lookup engine and a policy layer next to the existing context copy drafter, about 1,900 lines with two new test files, instead of building on it. It also conflicts with main now. The CI failure on it was the test order issue fixed for #5515, not your change.

If the policy layer still helps on top of main with #5528, numbers from the same prompts with and without it would make a strong case. I would then be happy to look at it as an extension of the context copy drafter, turned on by a model setting instead of an env var.

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.

2 participants