Skip to content

bench: add --ngram flag, default n-gram speculative decoding off - #51

Merged
ekryski merged 2 commits into
alphafrom
ek/benchmark-ngram-flag
Apr 16, 2026
Merged

ekryski merged 2 commits into
alphafrom
ek/benchmark-ngram-flag

Conversation

@ekryski

@ekryski ekryski commented Apr 16, 2026 •

Copy link
Copy Markdown
Owner

Summary

alpha defaults GenerateParameters.ngramSize to 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 defaulted ngramSize = 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 SIZE flag, default 0. Validated as a non-negative integer, exported as MLX_BENCH_NGRAM. Logs [INFO] N-gram: size=N when non-zero.
  • BenchEnv.ngramSize: Int — new reader in Tests/Benchmarks/InferenceBenchmark.swift, defaults 0. Deliberately diverges from the library default (3).
  • Two GenerateParameters construction sites in the harness (runGenerationBenchmark + batched path) now pass ngramSize: BenchEnv.ngramSize and maxNgramDraftTokens: BenchEnv.ngramSize. wikitext2 and the KLD forced-decode path don't sample, so they're unchanged.
  • benchmarks/README.md — --ngram SIZE row in the CLI table, MLX_BENCH_NGRAM row 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 = 3 default 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 N for 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. --ngram fits 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's ek/tom-eric-moe-tuning run (~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.
  • Parameters table in the output file shows Speculative decoding | ngram (size=N, maxDraft=N) when --ngram N > 0, none when --ngram 0.

ekryski and others added 2 commits April 16, 2026 17:22
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>
@ekryski
ekryski merged commit d6183c5 into alpha Apr 16, 2026
@ekryski
ekryski deleted the ek/benchmark-ngram-flag branch April 16, 2026 23:41
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