Repository navigation
perf(mtp): draft from context copies when the reply repeats the context (+30% file edits, +20% The Grill) - #4104
Conversation
|
The greedy numbers reproduce on an M3 Ultra: file edits 130.4 -> 186.6 tok/s with byte-identical output, and chat is flat. My hesitation is about who actually gets it. Copies only run at temperature 0, and I don't think many people run these models that way. Most setups sit somewhere around 0.6 to 1.0, the server default is 1.0, and the recommended settings here are 1.0 / top_p 0.95 / top_k 20. At those settings the same file-edit run was 140.6 vs 141.0 tok/s with zero copy cycles. So in practice the gain would mostly show up in greedy benchmarks, and carrying another drafting path in the MTP cycle for that feels like more code to maintain than it gives back to users. If a version shows a clear gain in the 0.6 to 1.0 range, I'd look at it again. |
|
Posted by Claude (Anthropic's AI assistant) on behalf of @sethforprivacy, as part of our local-inference tuning work on a Mac Studio M5 Ultra. On the 0.6–1.0 question, one data point from a different model: copies can run at T>0 without changing the output distribution. We run the same idea on GLM-5.3-Flash and it gains more at T=0.6 than at T=0. How we keep it exact when sampling. Each copied draft goes in as a one-hot draft distribution: logprob 0 on the copied token, −inf elsewhere. It goes through the MTP cycle's existing Leviathan/Chen verifier as Numbers (M5 Ultra 80c / 256 GB, macOS 27.0, GLM-5.3-Flash oQ4e with a grafted MTP head, MTP fixed depth 2 on both sides, single stream, decode tok/s; measured 2026-09-26 on our 0.7.0rc1-based stack):
The T=0.6 gain is larger because sampling lowers MTP-chain acceptance, while a verbatim copy is still accepted almost every time (87–99 % of lookup drafts per round). The lane has served this way since then; reasoning and qualify gates pass. Caveats: this is GLM, not Qwen. On GLM each extra MTP draft row pulls a mostly new expert set, so a near-certain 7-token draft is worth more there. We have not measured T=1.0 / top_p 0.95 / top_k 20. Higher temperature should lower acceptance on copies too, but they are verbatim spans, so they should hold up better than MTP drafts. So this is a hint that a sampled variant of #4104 could clear the bar, not a measurement of #4104 itself. |
Greedy singleton chain cycles look up the longest earlier match (at least 12 tokens) of the context tail over prompt + generated tokens and, when one exists, verify the 7 tokens that followed it instead of the MTP chain. The target model still verifies every draft, so output is unchanged; the MTP head still folds the committed tokens, so its history stays exact. - 3-gram index: the prompt sorted once with numpy (about 3 ms per 44K tokens), generated tokens in a dict; about 2 us per cycle after that. - The index build is kept out of the depth controller's timing. - Copy cycles are counted apart (copy=accepted/drafted in N) and never update the controller's per-depth acceptance or cost. - Four zero-accept copies in a row pause copies for 8 cycles. - OMLX_MTP_CONTEXT_COPY=0 disables it. Matching rule after TensorFold's SuffixLookupProposer (MIT).
A copy is a deterministic proposer. Sampled verify cycles now take it as a draft distribution with all its mass on the copied token, so the existing Leviathan/Chen acceptance accepts it with probability p(token) under the filtered target and resamples the residual (p without that token) on rejection: the sampled output distribution is unchanged. Greedy cycles keep exact token equality.
|
Following up on the 0.6–1.0 question. Copies now run at temperature > 0 without changing the sampled distribution. Thanks @sethforprivacy for the pointer. Change (new commit
Test: A/B on an M5 Ultra. Qwen3.8-Flash-Next oQ5e, MTP on, main 87460f4 + this PR. Copies were switched off and on with
Measured on an M5 Ultra only; I have no M3 or any other non-M5 Mac. #4105 still carries the earlier copy commit. I'll rebase it onto this once #4052 is settled. |
ba43c58 to
6d2cf49
Compare
jundot
left a comment
There was a problem hiding this comment.
The temp > 0 version works well here. At temp 1.0 on M3 Ultra, file edits got 1.5-2x faster on Qwen, Gemma 4, GLM-5.3 and MiMo. Chat speed didn't change, and greedy output stayed the same. LGTM, merging.
Fixes #4101. Part of roadmap #4018.
What
Greedy singleton Lightning MTP cycles can now draft from the context. After each verify, the proposer looks for the longest earlier match of the context tail (prompt + reply, 3-gram index, up to 64 tokens back, newest on ties). When at least 12 tokens match, the next window verifies the 7 tokens that followed that stretch instead of the MTP chain (7 drafts + the pending token = the 8-row row-exact window). Otherwise the MTP head drafts as before.
copy=accepted/drafted in Nin theMTP[...]line) and never update the controller's per-depth acceptance or cost.OMLX_MTP_CONTEXT_COPY=0disables it. Stochastic sampling, batched MTP and block drafters are unchanged.Matching rule after TensorFold's
SuffixLookupProposer(MIT). Differences: 12-token matches instead of 8 (replaying our chat and long-context replies, copies after 8-11-token matches had 1.3-2.0 of 7 tokens accepted on average, fewer than the MTP chain they replace), and only the 32 newest occurrences are compared.Results
This branch (upstream main d6b2b92 + this commit) on M5 Ultra,
GBP-DE/Qwen3.8-Flash-Next-oQ5e-mtp, MTP on, greedy, prompt cache off, same server build withOMLX_MTP_CONTEXT_COPY1 vs 0:On our integration build (main + #4052 + #4055 + #4062), two alternating rounds with an earlier revision of this commit: file edits 210 -> 275 tok/s, The Grill 216 -> 255, chat -0.4%, ~48K-token context -1.1%, ~96K -1.4%. That revision timed the index build as part of the first cycle, which skewed the depth controller toward shallower drafts on long prompts; with the build kept out of the timing (this commit, plus the 16-row follow-up) the same comparison gave +1.2% at ~48K and +3.1% at ~96K.
Exactness
Tests
test_context_copy_drafts_keep_greedy_output(qwen and qwen4 test models): the proposer is replaced by the true continuation with one token flipped at a position that moves every cycle, so 8-row windows are verified with 0, 1, 3, 6 and 7 accepted drafts; the output equals MTP-off decode.test_mlx_lm_mtp_patch,test_mtp_depth_controller,test_qwen35_moe_verify_window,test_qwen35_gdn_prework,test_row_exact_qmvpass (642).Tested on M5 Ultra only; there is no M3 or other Mac here.