Repository navigation
Document the greedy grammar top-k contract - #259
Merged
Merged
Conversation
13 of 14 tasks
leehack
added a commit
to leehack/llamadart
that referenced
this pull request
Dec 1, 2026
## Summary - Eligible greedy strict-grammar requests on large llama.cpp vocabularies certify one candidate at any `topK`. The chain keeps the requested `topK` and the existing sampler order. - Refs #2444. ## Changelog - [core] Greedy strict-grammar requests on large native GGUF vocabularies take the shortlist path at any topK, with unchanged output. ## Production-readiness scope - **User-facing scope** — Shorter grammar sampling time with `temp <= 0` and typical sampling, XTC and dynamic temperature disabled. - **Supported platforms and paths** — Native llama.cpp with the existing grammar wrapper export and at least 131,072 vocabulary tokens. - **Fallbacks** — Older runtimes and ineligible requests retain the existing sampler path. A failed certificate applies grammar to the full vocabulary. Stochastic requests still use the ordinary shortlist for `topK` 1–64. - **Follow-ups** — Keep #2444 open for stochastic sampling improvements in the owner runtime. The greedy-wrapper contract docs and test in [llamadart-native#259](leehack/llamadart-native#259) must land before this PR is marked ready. ## Completeness checklist - [x] Implemented the declared scope. - [x] Documented eligibility and fallback behavior. - [x] Updated the performance guide, package skill and PR changelog. The scope is internal native routing. - [x] Tested the routing regression, sampler ownership, missing export and real-model parity. - [x] Validation coverage registry check passes. The public capability inventory is unchanged. - [x] Reviewed changes for secret-bearing logs, errors and metadata. - [x] Stochastic work remains tracked by #2444. ## PR type guidance - **Bugfix** — The greedy chain needs only the best legal candidate, but it previously requested a full `topK` certificate or skipped the wrapper altogether. Tests verify the wrapper argument and the following native sampler. ## Test Plan On the current head, the 28 grammar routing unit rows, focused formatting, root analysis and the platform-boundary check passed on macOS arm64. The two new rows at `temp: 0.01` and `temp: 0.1` both failed the audit mutation `temp <= 0` → `temp < 0.5` by receiving wrapper K=1 instead of K=40. The checklist below preserves local validation from the original implementation head `29dfcd364f`. Current-head CI and independent review are tracked separately. - [x] `dart run tool/prepare_workspace.dart` - [x] `python3 tool/ci/check_format.py`, `dart analyze` and `dart run tool/testing/check_platform_boundaries.dart` - [x] `dart test -p vm -j 1 --exclude-tags local-only --coverage=coverage` (16,795 passed, 136 skipped) - [ ] Chrome is N/A for the native-only sampler change. - [x] `coverage-lib` local row (93.85% line coverage, 70% required) - [x] 26 grammar routing unit rows, 55 validation coverage and package skill tests, and 2,945 full tooling tests - [x] Local-only Gemma 4 parity, release docs verification, docs-site build, website analysis and 32 website tests ## Matrix Evidence Broad local test and model results below are from `29dfcd364f`. The current-head delta is one test-only commit and a clean merge of `main`. The sampler implementation is byte-identical to the audited version. | Matrix row | Scope covered | Platform / model / backend | Result | Evidence | | --- | --- | --- | --- | --- | | `static-format-analyze` | Format, analysis and platform boundaries | macOS arm64, pinned Flutter 3.47.1 | PASS | No format changes or analyzer issues | | `root-vm` | Native root tests | macOS arm64 | PASS | 16,795 passed, 136 skipped with local-only tests excluded | | `root-chrome` | Browser tests | Chrome | N/A | Native-only sampler routing | | `coverage-lib` | Line coverage | macOS arm64 | PASS | 93.85% (38,288/40,798), exceeds the 70% threshold | | `structured-output-adversarial` | Grammar enforcement and token parity | Apple M4 Max CPU, two threads, Gemma 4 E2B IT Q4_K_S, native v1.1.0-4 | PASS locally | Full-mask and wrapper runs agree for greedy K=0/1/40/65, typical sampling, seeded K=64, escaped JSON and partial UTF-8. Cancellation and reload pass. | | `docs-site` | Built docs, links and release docs metadata | macOS arm64 | PASS | 5,047 pages and zero broken internal links | | `high-risk-exact-head-independent-qa` | Blocking-only review | Independent auditor-deep | PENDING | Draft until audit and owner contract land | Model SHA-256 `0a2fac16f388b4839f075dedb681357aec3e73a96bd66b413e462b6853550c99`. On four warm K=40 comparisons, wrapper sampling took 1.09–1.26 ms/token and the full mask took 20.34–22.50 ms/token. K=1 with the wrapper took 1.07–1.12 ms/token. ## Review Notes - The independent blocking audit was clean at `29dfcd364f`. The current head adds two unit rows and merges `main`. The pinned llama.cpp runtime installs a no-op named `?top-k` for K=0 and `top-k` for positive K. The real unit chain verifies that both occupy the slot immediately after grammar. - Fresh GitHub CI is pending on `0a336e14b1a775420f1e1ea053fbe1e66f0d1762`. ## High-risk regression review - **Classification** — high-risk (`backendRuntime`) - **Implementation task** — #2444 greedy grammar routing - **Independent blocking QA task** — Clean auditor-deep verdict at `29dfcd364f`. Current-head review is pending. - **Exact head / base used for delta validation** — `0a336e14b1a775420f1e1ea053fbe1e66f0d1762` / `e168d24e016deb51aa64a5f933200061ad42da62`. - **Production-branch deletion, bypass, or miswire proof** — The K=0, 40 and 65 greedy rows fail on main and pass on this head. All seven original unit mutations failed their expected rows. The audit's `temp < 0.5` mutation now fails both near-zero positive-temperature rows. - **Affected-family evidence** — Gemma 4 E2B parity on the original audited head, as recorded above. Removing the typical guard did not diverge or abort on this model and prompt. The unit test detected that mutation by recording 1 instead of 40. - **Other evidence** — Input rendering and output parsing are outside the changed native sampler path. - **Known PR-caused P1 regressions** — 0 observed locally. The original audit found no blocking findings. Current-head independent review is pending. - **Unresolved review threads** — 0 at the local evidence update. | Unit mutation | Failing rows | | --- | --- | | Pass the requested K to the wrapper | Greedy K=40, 0 and 65 | | Require K=1–64 for every chain | Greedy K=0 and 65 | | Remove the typical guard | `typicalP: 0.9` at K=40 and K=0 | | Remove the XTC guard | `xtcProbability: 0.5` | | Remove the dynamic-temperature guard | `dynatempRange: 0.5` | | Allow temperature below 1 | `temp: 0.8` | | Allow temperature below 0.5 | `temp: 0.01` and `temp: 0.1` | | Remove the stochastic K range | Stochastic K=0 and K=65 |
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
Document when a greedy CPU chain can use a one-candidate strict-grammar probe while keeping its configured top-k. The wrapper falls back to the full grammar mask on a tie, and the original chain order determines which tied token wins.
Refs leehack/llamadart#2444.
Changelog
Scope and risk
Standard risk. The changes cover documentation, the public header comment, and a native test. The owner CI classifier selects the native validation matrix. No runtime or ABI change requires a native release.
Validation
top_k = 1with full grammar masking ahead of chaintop_k = 64for a unique legal winner, a rejected winner, and tied highest logits.git clang-format --diff. The diff passedgit diff --check.