Skip to content

evaluate: disable n-gram speculative decoding by default - #52

Merged
ekryski merged 1 commit into
alphafrom
ek/library-ngram-default-off
Apr 16, 2026
Merged

ekryski merged 1 commit into
alphafrom
ek/library-ngram-default-off

Conversation

@ekryski

@ekryski ekryski commented Apr 16, 2026 •

Copy link
Copy Markdown
Owner

Summary

Change GenerateParameters.ngramSize and maxNgramDraftTokens defaults from 3 to 0. Every library consumer who constructs GenerateParameters() 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.swift only, +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 from 0 → 3 on that migration (see #51 for the benchmark-harness companion that adds --ngram SIZE to opt in). The library default shouldn't silently impose this cost.

What doesn't change

  • Parameter names, types, and semantics stay the same.
  • Existing callers that pass ngramSize: N / maxNgramDraftTokens: N explicitly keep their behaviour unchanged.
  • The speculation code path is untouched — this is a defaults-only change.
  • N-gram decoding remains fully available; callers opt in with:
    GenerateParameters(ngramSize: 3, maxNgramDraftTokens: 3)

Coverage

Grepped Libraries/ for any code that relies on the ngramSize = 3 default: 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 MLXLMTests passes.
  • Bench harness Speculative decoding row reads none when no --ngram flag passed, ngram (size=N, maxDraft=N) when --ngram N is passed (covered by bench: add --ngram flag, default n-gram speculative decoding off #51).
  • Library consumer that passes GenerateParameters(ngramSize: 3, maxNgramDraftTokens: 3) explicitly gets the same behaviour as before this change.

Related

  • #51 adds the benchmark-harness --ngram SIZE flag so users can opt into speculation measurements explicitly.
  • Tom's ek/ngram-speculative-decoding branch 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.

`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>
@ekryski
ekryski merged commit 249ef81 into alpha Apr 16, 2026
@ekryski
ekryski deleted the ek/library-ngram-default-off branch April 16, 2026 23:44
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.

1 participant