Repository navigation
bench: add --ngram flag, default n-gram speculative decoding off - #51
Merged
Merged
Conversation
alpha defaults `GenerateParameters.ngramSize` to 3 so library clients get trigram speculative decoding out of the box. That composite — speculation accept-rate × draft overhead — adds run-to-run variance that masks the primitive we actually want to measure in benchmarks (pure autoregressive decode tok/s). A 4096-ctx Qwen3.5 0.8B summarization row drifted ~10% vs a prior baseline that defaulted ngramSize to 0; investigation showed the whole delta was speculation accept-rate noise, not a real perf change. This PR makes benchmarks default to no speculation and adds a CLI flag to opt in explicitly when we want to measure the speculative path. Changes 1. `scripts/benchmark.sh` gains `--ngram SIZE`, default 0 (disabled). Value validated as a non-negative integer; exported as `MLX_BENCH_NGRAM=N` unconditionally so the harness always sees an explicit value. Logs `[INFO] N-gram: size=N` when non-zero. 2. `BenchEnv.ngramSize: Int` reads `MLX_BENCH_NGRAM`, defaults 0. Deliberately diverges from the library default (3) — library clients get the typical-use default; benchmarks get a clean primitive and opt in for speculative measurements. 3. Two `GenerateParameters` call sites in `InferenceBenchmark.swift` now pass `ngramSize: BenchEnv.ngramSize` and `maxNgramDraftTokens: BenchEnv.ngramSize` — the main `runGenerationBenchmark` and the batched-decode path. `wikitext2` and the KLD forced-decode path don't sample, so ngramSize has no effect there and we leave them alone. 4. `benchmarks/README.md` documents `--ngram SIZE` in the CLI table and `MLX_BENCH_NGRAM` in the environment-variable table, with a note on why the benchmark default diverges from the library default. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The writer grouped rows by a config key built from
`quantization / kvConfig / scenario`. Two runs that differ only in a
dimension outside that triple — e.g. two runs of
`qwen35-0.8b 4bit turbo4v2 summarization`, one with `--ngram 0` and
one with `--ngram 3` — collapsed into the same config entry. Both
results rows landed in the table, but the Parameters block only
reflected whichever run created the entry first. Reader couldn't
tell which row was which.
Fix: `BenchmarkWriter.append` takes a new optional
`configKeyExtras: [(String, String)] = []` that's appended to the
config key as ` / key=value` pairs. The InferenceBenchmark harness
passes `[("ngram", "N")]` when `BenchEnv.ngramSize > 0`; nothing
otherwise.
Result with `--ngram 0` then `--ngram 3` on the same cell:
4bit / turbo4v2 / summarization
4bit / turbo4v2 / summarization / ngram=3
Separate Parameters blocks with `N-gram size | 0` and `N-gram size | 3`
respectively.
Extensible to other benchmark-time dimensions that aren't part of the
quant/kv/method triple (e.g. reasoning effort) without further schema
changes.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2 of 3 tasks
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
alpha defaults
GenerateParameters.ngramSizeto 3 (library clients get trigram speculative decoding out of the box). For benchmarks this composite — speculation accept-rate × draft overhead — adds run-to-run variance that masks the primitive we want to measure (pure autoregressive decode tok/s). Yesterday's 4096-ctx Qwen3.5 0.8B summarization cell drifted ~10% vs a prior baseline that defaultedngramSize = 0; the whole delta was speculation accept-rate noise, not a real perf change.This PR makes benchmarks default to no speculation and adds a CLI flag so we can opt in explicitly when measuring the speculative path.
Changes
scripts/benchmark.sh— new--ngram SIZEflag, default0. Validated as a non-negative integer, exported asMLX_BENCH_NGRAM. Logs[INFO] N-gram: size=Nwhen non-zero.BenchEnv.ngramSize: Int— new reader inTests/Benchmarks/InferenceBenchmark.swift, defaults0. Deliberately diverges from the library default (3).GenerateParametersconstruction sites in the harness (runGenerationBenchmark+ batched path) now passngramSize: BenchEnv.ngramSizeandmaxNgramDraftTokens: BenchEnv.ngramSize.wikitext2and the KLD forced-decode path don't sample, so they're unchanged.benchmarks/README.md—--ngram SIZErow in the CLI table,MLX_BENCH_NGRAMrow in the env-var table, both noting the deliberate divergence from the library default.One commit, 3 files, +40 lines.
Why benchmark default diverges from library default
The library's
ngramSize = 3default is the right choice for application code — most use cases benefit from trigram speculation when acceptance rate is reasonable. The benchmark harness's job is different: measure a clean primitive. If someone wants to measure the speculative path, they pass--ngram 3(matches library default) or--ngram Nfor other configurations. If they want pure autoregressive, they pass nothing.This mirrors the existing pattern for
--ppl,--kld,--think— all three are opt-in flags that enable additional measurement overhead.--ngramfits the same model.Test plan
./scripts/benchmark.sh --model qwen35-0.8b --kv turbo4v2 --context 4096(no flag) — decode matches flag-off numbers from yesterday'sek/tom-eric-moe-tuningrun (~194 tok/s at this cell), confirming speculation is off../scripts/benchmark.sh --model qwen35-0.8b --kv turbo4v2 --context 4096 --ngram 3— decode matches current alpha behaviour (~174 tok/s observed), confirming speculation is on and the flag threads through.Speculative decoding | ngram (size=N, maxDraft=N)when--ngram N > 0,nonewhen--ngram 0.