Repository navigation
Speed up strict grammar sampling on large vocabularies - #2459
Merged
Merged
Conversation
Owner
Author
|
Independent deep audit (uninvolved session) at head 2695841: clean, no P1/P2/P3. 196-case grids (8 grammars x 6 sampler chains x 4 prompts) on Gemma 4 E2B and Qwen3 1.7B: 0 token mismatches (shortlist accepted 786/962 times, full-vocab fallback 1,819/2,697). Published v1.1.0-2 and -1 libraries lack the export and produce identical text through the existing path. Timing ratios reproduce (greedy K=1 ~19 -> ~0.97 ms/token; seeded K=64 ~20 -> ~11 ms/token). 14 of 15 mutations fail tests; the survivor (always passing K=64) is output-neutral. Non-blocking nit: website/docs/guides/performance-tuning.md omits speculative decoding from the excluded-request list. |
leehack
marked this pull request as ready for review
November 30, 2026 13:57
Contributor
|
Chat app preview removed for |
This was referenced Nov 30, 2026
leehack
added a commit
that referenced
this pull request
Nov 30, 2026
## Summary - Document the speculative decoding exclusion from the strict grammar fast path added in #2459. - Pin the exact exception types for failed speculative initialization, invalid combinations, and missing native exports covered by #2462. - Fail the Gemma 4 smoke when a draft call ran and proposed zero tokens. The report includes `NoDraftTokensProposed` and preserves the performance counters. ## Changelog - [core] Make the local Gemma 4 MTP smoke fail with a typed JSON report when the drafter runs but proposes zero tokens. ## Production-readiness scope - **User-facing scope:** Performance tuning guidance and the maintainer smoke CLI. - **Supported platforms/paths:** Native llama.cpp Gemma 4 GGUF with an external or embedded drafter. - **Unsupported or intentionally unavailable paths:** An unsupported MTP turn still exits 1 and reports its original exception type and message. - **Out of scope / follow-ups:** None. ## Completeness checklist - [x] Scope implemented. - [x] Existing unsupported-path diagnostics preserved. - [x] Performance guide and smoke matrix documentation updated. - [x] Added regression tests and verified that the original smoke behavior and four exception type mutations fail. - [x] Validation inventory N/A. Public capabilities are unchanged. - [x] The smoke diagnostic contains a fixed message and adds no model data or paths. - [x] No follow-up issues required. ## PR type guidance - **Bugfix:** A completed speculative turn could return 0 after the drafter proposed zero tokens. Check the draft-attempt and proposed-token counters and report the failure through the existing JSON output. ## Test Plan - [x] `dart run tool/prepare_workspace.dart` with Flutter 3.47.1. - [x] Repository format, analyze, platform boundaries, docs build and link check, and full Chrome suite. - [ ] Full local VM suite stopped after 1,338 passes with `No space left on device` (errno 28). Focused VM suites passed independently. - [x] Coverage-lib N/A. Library behavior is unchanged. - [x] Verified that four subtype mutations fail the targeted exception assertions, and that the new zero-proposal test fails before the smoke change. ## Matrix Evidence | Matrix row | Scope covered | Platform / model / backend | Result | Evidence / notes | | --- | --- | --- | --- | --- | | static-format-analyze | Format, analyzer, import boundaries | macOS arm64 | PASS | Flutter 3.47.1 | | root-vm | Exception types, smoke runner, and tooling | macOS arm64 | PASS focused | 833 passed, 3 skipped. Full local suite blocked by disk exhaustion. Four subtype mutations and the before-fix smoke failed as expected. | | root-chrome | Browser-compatible suites | macOS Chrome | PASS | 5,422 passed, 1 skipped | | docs-site | Website build and links | macOS arm64 | PASS | `./tool/docs/build_site.sh` completed its build and link check | | gemma4-mtp-smoke | External drafter and missing-drafter refusal | Gemma 4 E2B Q4_K_S / Metal | PASS | External drafter proposed 8 tokens and accepted 7, exit 0. Missing drafter returned the typed initialization error, exit 1. A one-token turn made zero draft attempts and exited 0. | | high-risk-exact-head-independent-qa | Independent review | PR head and current base | Passed | Independent Opus audit at `b985029daf0e26a6fd47e0e7b44a2d2d59834e5c` vs `main` `aabe6fba9b`: no blocking findings; report in pr-2465/audit.md | ## Review Notes - Independent review status: Passed, no P1/P2/P3. - CI status / head SHA: 7 successful, 19 skipped, 12 pending, 0 failed at `b985029daf0e26a6fd47e0e7b44a2d2d59834e5c`. ## High-risk regression review - **Classification:** high-risk (`regressionPolicy`). - **Implementation task:** Follow-ups from #2459 and #2462. - **Independent blocking QA task:** Passed at `b985029daf0e26a6fd47e0e7b44a2d2d59834e5c`; non-blocking notes only. - **Exact head / base at submission:** `b985029daf0e26a6fd47e0e7b44a2d2d59834e5c` / `aabe6fba9bf1969985f34dee4ccb2c7f9d2149e1`. - **Production-branch deletion, bypass, or miswire proof:** The old smoke exited 0 in the new zero-proposal fixture. Changing the missing API, thinking-budget, bundled loadMtp, and topLogprobs paths to the subtype each failed its exact-type assertion. - **Affected-family real-model/artifact evidence:** Gemma 4 E2B with the external MTP drafter on Metal. - **Explicit unavailable-family or other N/A evidence:** The zero-proposal result is injected in a VM fixture. The available native Gemma 4 target uses external MTP. - **Known PR-caused P1 regressions:** 0 known. - **Unresolved review threads:** 0 at submission.
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
LlamaInferenceExceptionand free the grammar and partial chain on failure.Refs #2444. Native sampler contract
Changelog
Production-readiness scope
User-facing scope Automatically accelerates eligible strict grammar requests without a new setting.
Supported paths Native llama.cpp CPU sampling chain on admitted runtimes exporting the wrapper. Existing model offload and public generation options keep their existing semantics.
Fallback paths The original route covers absent export, small vocabularies, out-of-range K, lazy grammar, thinking-budget composites, DRY, top-n-sigma, Mirostat, speculative decoding and requested logprobs. Existing input validation still applies.
Follow-ups Android XL throughput and Apple companion qualification for the patch decision remain under Grammar-constrained sampling is slow on large-vocabulary models (Gemma 4, 262k tokens) #2444 and Track the 0.23.x Apple llama.cpp companion compatibility exception #2448. This draft includes macOS host evidence.
If a future native release introduces a second shortlist contract, add an optional sampler-specific ABI version export in the owner repository. Preserve detection of the existing contract by its constructor export, use full-vocabulary sampling for unknown versions, and test both exports across the supported loaders.
Completeness checklist
PR type guidance
Backend performance fix. Repeated grammar masking over the full vocabulary dominated native sampling on Gemma 4. The native sampler certifies the immediate top-k shortlist before the existing selector or applies the original full mask.
Test Plan
coverage-libwith 70% thresholdMatrix Evidence
static-format-analyzeroot-vm/coverage-libroot-chromedocs-sitenative-exit-teardownstructured-output-adversarialhigh-risk-exact-head-independent-qaTargeted sampler and barrier suites passed 33 tests. The eligible routing test failed against baseline. Additional mutations changing the forwarded K, admitting a lazy request or leaking the grammar on rejection each failed their relevant test.
Gemma 4 CPU comparison
Four paired timed runs per mode after one warmup per arm, alternating full-first and shortlist-first. Both arms use v1.1.0-3, with the export hidden at lookup for the full scan. The metric is the service's sampling and token-to-piece time divided by actual sampler calls, including EOG. The model has a 262,144-token vocabulary and SHA-256
0a2fac16f388b4839f075dedb681357aec3e73a96bd66b413e462b6853550c99. Seed 7, 1,024 context, prefix reuse off, three-field JSON schema. The committed-head rerun also passed exact token parity and lifecycle checks.Reproduce with
LLAMADART_GEMMA4_GRAMMAR_MODEL=/path/to/gemma-4-E2B-it-Q4_K_S.gguf TMPDIR="$(mktemp -d)" dart test -p vm --run-skipped test/e2e/backends/llama_cpp_grammar_top_k_e2e_test.dart --reporter expanded.Review Notes
2695841c0d3388cb51adcf8cc26b5979c6d58be7: 196-case equivalence grids on Gemma 4 E2B and Qwen3 1.7B with zero token mismatches, published v1.1.0-2/-1 libs fall back correctly, ownership and exit-teardown paths reviewed, 14/15 mutations fail tests.2695841c0d3388cb51adcf8cc26b5979c6d58be7. PR CI intentionally omits the coverage job. ChecksHigh-risk regression review
backendRuntime).2695841c0d3388cb51adcf8cc26b5979c6d58be7/ae3789e4a5ede46ca9839a777c5e7e7bb0dd540e. The head was tested from merge base38d07ea31657f1454e3d866e6620c05fc7e2f2c1. Main advanced by Studio PR Warm Gemma Chat prompt prefixes after explicit Studio loads #2458. A merge simulation against current main had no conflicts.