Skip to content

Certify progressive strict-grammar sampling for top-k and top-p - #266

Merged
leehack merged 2 commits into
mainfrom
lane-2444-grammar-top-k-p
Dec 2, 2026
Merged

leehack merged 2 commits into
mainfrom
lane-2444-grammar-top-k-p

Conversation

@leehack

@leehack leehack commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Summary

Add the optional strict-grammar constructor
llama_dart_sampler_init_grammar_top_k_p for CPU top-k, top-p and min-p
chains. 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_keep candidates at or above it. Ambiguity, unsorted min-p alone with top-k
disabled, 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
(<=), and preceding_upper < top_p. They also exercise real grammar
escapes, split UTF-8, EOG, reset, clone, allocation failure and teardown.

Verification

  • Test-only hardening head 3c7b11a318e8111364bc322c1788aeb50a3642ca modifies only tests/grammar_top_k_p_test.cpp (zero production src/ changes from adec2b244786da121803c270ff5629fd5cdb0d13).
  • At 3c7b11a318e8111364bc322c1788aeb50a3642ca:
    • macOS arm64 Release wrapper CTest passed 118/118 (after an initial -j 6 run hit a transient llamadart_threadpool_untrack worker-exit timing failure); focused CPU Debug CTest passed 4/4.
    • Python unittest discovery passed 530 tests with 1 skip (OK (skipped=1)).
    • The export gate validated 123 required symbols and passed the __cxa_atexit static destructor binding check on build/grammar-contract/libllamadart.dylib.
    • Four added deterministic boundary fixtures (test_top_p_certificate_boundaries) were each proven to fail under their corresponding audit mutation (addition_error = 0, tail cap K - count - 1, cutoff-tie trim <, and preceding_upper <= top_p), bringing verified mutation controls to 17 total alongside the 13 controls verified at adec2b244786da121803c270ff5629fd5cdb0d13 (and the independent rejected-proposal control).
  • The release-style macOS arm64 build was validated at 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.

Mode Full mask, ms/token Progressive, ms/token Exact pairs Single / stage 1 / stage 2 / full
plain 0.082 0.082 3/3 0 / 0 / 0 / 0
greedy-json 19.726 0.810 3/3 57 / 12 / 0 / 3
stochastic-three-field 20.048 0.307 3/3 0 / 108 / 0 / 0
stochastic-restricted 15.075 0.292 3/3 0 / 132 / 0 / 0

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 (0 stage-2 and 0
fallback steps), so stage-2 and fallback overhead on Gemma 4 was not measured.
Candidate probe counts are totals across the three measured B runs.

  • Benchmarked native head adec2b244786da121803c270ff5629fd5cdb0d13 (current head 3c7b11a318e8111364bc322c1788aeb50a3642ca adds only tests, with no production src/ changes).
  • Pinned llama.cpp 171527522610996f5f0acf12334e185156ca8f44.
  • Model SHA-256 96ee1ee1d562be52dbbc4c12694ed67de8c6ae09451d420fed7d067e552c25cb.
  • Benchmarked dylib SHA-256 16e69c90d18cc0fa78e56e51bd53107c14e05a935811fcea5cc95d73ad9e77e5.

Risk

High risk for native sampler behavior, ABI and exit teardown. Native CI is
selected. Initial independent adversarial audit at adec2b244786da121803c270ff5629fd5cdb0d13
was clean (no blocking findings); delta audit at 3c7b11a318e8111364bc322c1788aeb50a3642ca
is pending.

Changelog

  • Add progressive strict-grammar sampling for CPU top-k, top-p and min-p while preserving seeded token selection.

@leehack
leehack marked this pull request as ready for review December 2, 2026 01:43
@leehack
leehack merged commit f914d88 into main Dec 2, 2026
30 checks passed
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