Repository navigation
Certify progressive strict-grammar sampling for top-k and top-p - #266
Merged
Merged
Conversation
leehack
marked this pull request as ready for review
December 2, 2026 01:43
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
Add the optional strict-grammar constructor
llama_dart_sampler_init_grammar_top_k_pfor CPU top-k, top-p and min-pchains. It probes 256 candidates, reuses those grammar decisions in the
1,024-candidate stage, and compacts only a certified ordered prefix. The
legacy constructor keeps its ABI and behavior. The runtime export gate
increases from 122 to 123.
Part of llamadart #2444.
Correctness
The two-sided top-p bound includes grammar-legal probed candidates tied at
the cutoff. It checks the same rounded cumulative cutoff for the full and
compact inputs. Min-p uses the upstream float threshold and requires
min_keepcandidates at or above it. Ambiguity, unsorted min-p alone with top-kdisabled, and incompatible sorted prefixes fall back to the full scan.
The legacy sampler fails the first probe assertion. It probes 64 tokens
and then scans all 2,048. The test expects one 256-token probe. The new
tests compare ordered shortlist, selected token and accepted token with
the full-mask reference for 3,500 randomized arrays,
two 10,000-seed distribution cases, and deterministic boundary fixtures
for the top-p rounding allowance, tail cap (
K - count), cutoff-tie trimming(
<=), andpreceding_upper < top_p. They also exercise real grammarescapes, split UTF-8, EOG, reset, clone, allocation failure and teardown.
Verification
3c7b11a318e8111364bc322c1788aeb50a3642camodifies onlytests/grammar_top_k_p_test.cpp(zero productionsrc/changes fromadec2b244786da121803c270ff5629fd5cdb0d13).3c7b11a318e8111364bc322c1788aeb50a3642ca:-j 6run hit a transientllamadart_threadpool_untrackworker-exit timing failure); focused CPU Debug CTest passed 4/4.unittestdiscovery passed 530 tests with 1 skip (OK (skipped=1)).__cxa_atexitstatic destructor binding check onbuild/grammar-contract/libllamadart.dylib.test_top_p_certificate_boundaries) were each proven to fail under their corresponding audit mutation (addition_error = 0, tail capK - count - 1, cutoff-tie trim<, andpreceding_upper <= top_p), bringing verified mutation controls to 17 total alongside the 13 controls verified atadec2b244786da121803c270ff5629fd5cdb0d13(and the independent rejected-proposal control).adec2b244786da121803c270ff5629fd5cdb0d13(src/is unchanged).macOS CPU benchmark
Apple M4 Max, Gemma 4 E2B IT Q4_K_S, CPU only, context 8,192, four generation
and batch threads, and fresh prompt evaluation each turn. Stochastic
settings were temperature 1.0, top-k 64, top-p 0.95, penalty 1.0 and seed 7
(greedy used seed 1). Each mode ran a 32-token warmup and three 128-token-limit
interleaved pairs in A1, B1, B2, A2, A3, B3 order. Arm A uses strict grammar
on the full candidate array. Arm B uses the progressive constructor.
Each mode used one fixed seed (seed 7 for plain and stochastic modes; seed 1 for
greedy), so the 12 measured A/B pairs repeat 4 distinct seeded trajectories (2
stochastic: 36 and 44 tokens; 1 greedy JSON: 24 tokens; 1 plain: 128 tokens)
rather than 12 independent samples. Across those runs, all pairs matched output
and sampled token hashes and all nine JSON pairs validated. In both stochastic
modes, 100% of sampled steps were certified at stage 1 (
0stage-2 and0fallback steps), so stage-2 and fallback overhead on Gemma 4 was not measured.
Candidate probe counts are totals across the three measured B runs.
adec2b244786da121803c270ff5629fd5cdb0d13(current head3c7b11a318e8111364bc322c1788aeb50a3642caadds only tests, with no productionsrc/changes).171527522610996f5f0acf12334e185156ca8f44.96ee1ee1d562be52dbbc4c12694ed67de8c6ae09451d420fed7d067e552c25cb.16e69c90d18cc0fa78e56e51bd53107c14e05a935811fcea5cc95d73ad9e77e5.Risk
High risk for native sampler behavior, ABI and exit teardown. Native CI is
selected. Initial independent adversarial audit at
adec2b244786da121803c270ff5629fd5cdb0d13was clean (no blocking findings); delta audit at
3c7b11a318e8111364bc322c1788aeb50a3642cais pending.
Changelog