Repository navigation
evaluate: disable n-gram speculative decoding by default - #52
Merged
Merged
Conversation
`GenerateParameters` shipped `ngramSize = 3` and
`maxNgramDraftTokens = 3` as defaults, so every library consumer who
constructs `GenerateParameters()` implicitly gets trigram prompt-lookup
speculative decoding. Speculation is a net-win only when the generated
text has enough repetition that the trigram table hits with a
reasonable acceptance rate. For novel prose, summarisation, general
chat, etc. accept rate is often too low for the draft work to pay
off — it becomes pure overhead. Yesterday's bench surfaced this as a
~10% decode regression on Qwen3.5-0.8B summarisation at 4096 ctx vs
an older baseline that defaulted to `ngramSize = 0`.
Until we can validate the speculative path with good per-workload
benchmarks and accept-rate instrumentation (being added separately),
the safer library default is off. Clients who want speculation opt
in explicitly:
GenerateParameters(ngramSize: 3, maxNgramDraftTokens: 3)
The parameters and speculation code path are unchanged — this is a
defaults-only change. Existing callers that pass `ngramSize` /
`maxNgramDraftTokens` explicitly keep their behaviour.
Verified: `xcodebuild test -scheme mlx-swift-lm-Package
-only-testing MLXLMTests` passes.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
Change
GenerateParameters.ngramSizeandmaxNgramDraftTokensdefaults from3to0. Every library consumer who constructsGenerateParameters()with defaults currently gets trigram prompt-lookup speculative decoding implicitly — that's the wrong default until we've validated the speculation path gives net wins on representative workloads.One commit,
Libraries/MLXLMCommon/Evaluate.swiftonly, +12 / -4.Why
Speculative decoding is only a net-win when the draft's acceptance rate × speedup-if-accepted exceeds the overhead of the wasted draft work. For general prose / summarisation / chat output, the trigram table hits infrequently — most draft tokens get rejected, and the extra forward passes become pure overhead. For repetitive output (code, templated answers) it's genuinely useful.
Yesterday's bench surfaced this on
ek/tom-eric-moe-tuning→ alpha: Qwen3.5-0.8B summarisation at 4096 ctx decoded ~10% slower when we flipped alpha on, entirely explained by the default going from0→3on that migration (see #51 for the benchmark-harness companion that adds--ngram SIZEto opt in). The library default shouldn't silently impose this cost.What doesn't change
ngramSize: N/maxNgramDraftTokens: Nexplicitly keep their behaviour unchanged.Coverage
Grepped
Libraries/for any code that relies on thengramSize = 3default: nothing but one doc snippet (Libraries/MLXLMCommon/Documentation.docc/porting.md). No ChatSession or factory code sets it explicitly today. Updated the doc comment to document the new default and the opt-in path.Test plan
xcodebuild test -scheme mlx-swift-lm-Package -only-testing MLXLMTestspasses.Speculative decodingrow readsnonewhen no--ngramflag passed,ngram (size=N, maxDraft=N)when--ngram Nis passed (covered by bench: add --ngram flag, default n-gram speculative decoding off #51).GenerateParameters(ngramSize: 3, maxNgramDraftTokens: 3)explicitly gets the same behaviour as before this change.Related
--ngram SIZEflag so users can opt into speculation measurements explicitly.ek/ngram-speculative-decodingbranch has WIP improvements to the speculation path (adaptive auto-disable, MambaCache snapshot/restore). Once that lands, we can re-evaluate flipping the library default back on with the accept-rate gating it includes.