Repository navigation
Conversation
…_COPY_AR Reuses NgramIndex, block_for_ext and the probation/EMA/suspend policy (now CopyGovernor) of the MTP context-copy lane; verifies a prompt block with one multi-row forward, point-mass acceptance with residual resampling when sampling, KV trim on rejection. Default off. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
jvmenen
pushed a commit
to jvmenen/MTPLX
that referenced
this pull request
Oct 8, 2026
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Models without an MTP head, such as Llama 3.1 8B or Qwen3-8B, generate one token per forward pass, also when the answer is mostly text that is already in the prompt: rewriting a file with a small change, extracting passages from a document, quoting. Models with an MTP head already skip through such copied text with context-copy; models without one do not get that speedup at all. On Llama 3.1 8B a full-file code edit runs at 56 tokens per second, even though most of the file is copied unchanged.
How it works, in plain terms
Context-copy looks up where the last few generated tokens also occur in the prompt, and proposes the text that follows there as a block of guesses. The model then checks the whole block in one forward pass instead of one pass per token, and keeps the part that matches what it would have produced. With greedy decoding that is exactly the same text as without context-copy. With sampling, a correction at the first rejected token keeps the output distribution the same. The cache entries of rejected tokens are removed again, so the next step starts from the accepted text. This PR adds that loop to the decode path for models without an MTP head, behind
MTPLX_CONTEXT_COPY_AR=1(off by default). It does not run with penalties, constrained decoding or guards, and hybrid models whose recurrent state cannot be rolled back keep plain decoding. On the full-file edit above, decoding goes from 56 to 275 tokens per second (4.9x); extraction from a report gains 1.3x, and prose without source text is unchanged.What changes
The same lane in
generate_ar, behindMTPLX_CONTEXT_COPY_AR=1(off by default, and still subject toMTPLX_CONTEXT_COPY).mtplx/context_copy_ar.py. One forward over[token, *block], then acceptance and cleanup:max_tokensis exact.mtplx/context_copy.py.NgramIndex, the block drafter and theMTPLX_CONTEXT_COPY_*knobs. The probation and suspend policy that the MTP loops keep inline is extracted asCopyGovernor; the MTP lanes themselves are untouched.generation.py. About 60 lines ingenerate_ar.trim. Hybrid recurrent models fall back to plain AR.[Unreleased].Results
M5 Pro 64 GB,
mlx-community/Llama-3.1-8B-Instruct-4bitloaded withmtp=False, in process. Off and on interleaved, 5 runs per cell; decode tok/s, median:Tests
tests/test_context_copy_ar.py(CPU):max_tokens1 to 16 and stop tokensThe existing context-copy tests are unchanged and pass.
Notes
CopyGovernor. That is left out here to keep the MTP lanes free of risk.🤖 Generated with Claude Code