Repository navigation
Conversation
…ive decoding) Builds out the n-gram prompt-lookup speculative decoding path that `GenerateParameters.ngramSize` / `maxNgramDraftTokens` were documented for but never actually wired up. Unlike the existing `SpeculativeTokenIterator` which needs a separate draft *model*, this iterator sources draft tokens from the token history itself (prompt + already-accepted generations) via a rolling FNV-1a hash table over the last-`ngramSize`-token suffixes. Wins when the generation has repetitive structure (boilerplate, code, templates, or factual re-quoting of the prompt); degrades to one hash lookup per step on a miss. ## Guarantees - **Greedy-equivalent** under temperature=0: verification accepts a drafted token only when it matches the main model's argmax at the same position. Output stream is identical to `TokenIterator`. - **Metrics exposed**: `ngramProposedCount`, `ngramAcceptedCount`, `ngramAcceptanceRate` on the iterator. - **Disabled by default** — `ngramSize=0` remains the `GenerateParameters` default; the iterator precondition-fails if constructed without both knobs set, pointing callers back at `TokenIterator` for the non-speculative path. ## Implementation - `NGramLookup`: rolling Swift-side hash table. FNV-1a over last `ngramSize` tokens → list of token-positions where that n-gram ends. `extend(with:)` amortises O(k) work per appended token rather than rescanning the whole history. - `NGramSpeculativeTokenIterator`: prefill the main model, then per round (a) look up a draft of up to `maxNgramDraftTokens`; (b) run `[y, draft_1 ... draft_k]` through the main model in ONE forward pass; (c) accept drafts until the first rejection, always emit the main model's bonus/correction token at position `accepted`; (d) trim the KV cache by `numDraft - accepted`; (e) extend the lookup with the committed tokens. ## Scope (v1) - Single ngramSize (spec'd multi-size 2-5 fallback is a follow-up). - Fixed draft length (dynamic-length-per-acceptance-rate is a follow-up). - Greedy only; temperature > 0 path uses the same argmax verification, but for full parity with sampling that differs per position we'd need a resample loop — tracked as follow-up. ## Tests `Tests/MLXLMTests/NGramSpeculativeTests.swift`: 1. **Greedy parity** — spec iterator emits the same token count as `TokenIterator` at temperature=0. Token-equality under tiny random-weight models is unstable (argmax ties flip), so this is the stable assertion at unit-test scale; real-model end-to-end parity is the acceptance test. 2. **Metrics sanity** — `proposed >= 0`, `accepted <= proposed`, rate in `[0, 1]`. 3. **Config contract** — precondition on `ngramSize >= 1` documented. Existing safety suite (`KVCacheTests`, `SpeculativeDecodingTests`, `Gemma3nOffsetTests`, `MaskedScatterTests`, `BatchTokenIteratorTests`) continues to pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Regression Test Results (M5 Max 128GB)Cherry-picked onto alpha (with #56 and #57 already applied). All 5 models tested with Results @ 1k context
Verdict: No regressions. All within noise. NGram speculative iterator is additive — doesn't touch the default decode path, so expected zero impact on these benchmarks. |
Owner
Author
|
Implementation already landed on alpha as commit c1f6015 — Follow-up Phase A/B work (extract into its own file, add |
4 of 5 tasks
div0-space
pushed a commit
to LibraxisAI/mlx-swift-lm
that referenced
this pull request
Nov 29, 2026
mlx-swift 4c6ca4c53169d0ec52a31707e5ec9cc4fb8aab13 (local wip/cluster-next): cluster ekryski#56 + single-Mac ekryski#58, pinning core a1d75679 (cluster ekryski#35 + single-Mac ekryski#37, the 4d913204 revert) and mlx-c 1d0ce055. The merge before this needs GPU.withCommandBufferLimits, which only this stack has. Co-Authored-By: Claude Opus 5.5 <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
Builds out the n-gram prompt-lookup speculative decoding path that `GenerateParameters.ngramSize` / `maxNgramDraftTokens` were documented for but never actually wired up.
Unlike `SpeculativeTokenIterator` which needs a separate draft model, this iterator sources draft tokens from the token history itself (prompt + already-accepted generations) via a rolling FNV-1a hash table over the last-`ngramSize`-token suffixes.
Wins where
Generation has repetitive structure: boilerplate, code, templates, factual re-quoting of the prompt. Degrades to one hash lookup per step on a miss — overhead is negligible.
Guarantees
Scope (v1)
Test plan
Files