Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/validate_wrapper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ jobs:
cmake --build build/wrapper-contract --target
llamadart_owner_api_test llamadart_owner_reentrancy_test
llamadart_file_sha256_test llamadart_model_fd_test llamadart_threadpool_test
llamadart_grammar_top_k_test
llamadart_grammar_top_k_test llamadart_grammar_top_k_p_test
llamadart_f16_dot_accumulation_test
llamadart_speculative_api_test
llamadart_build_identity_test llamadart_upstream_abi_test
Expand Down Expand Up @@ -656,6 +656,7 @@ jobs:
run: >-
cmake --build --preset windows-x64-full --target
llamadart_barrier_test
llamadart_grammar_top_k_p_test
llamadart_owner_api_test llamadart_owner_reentrancy_test
llamadart_file_sha256_test llamadart_model_fd_test
llamadart_build_identity_test llamadart_upstream_abi_test
Expand All @@ -676,7 +677,7 @@ jobs:
$wrapperDir = (Resolve-Path build/wx64).Path
$upstreamDir = (Resolve-Path build/wx64/bin).Path
$env:PATH = "$wrapperDir;$upstreamDir;$env:PATH"
ctest --test-dir build/wx64 -R "owner_api|owner_reentrant|file_sha256|model_fd|barrier|build_identity|upstream_abi|cancel_flag|vocabulary|reasoning-budget|vulkan_device_info|device_memory|fit_api|tts_api|tts_types|asr_stream|tts_stream|image_token_limit" --timeout 120 --output-on-failure
ctest --test-dir build/wx64 -R "owner_api|owner_reentrant|file_sha256|model_fd|barrier|grammar_top_k_p|build_identity|upstream_abi|cancel_flag|vocabulary|reasoning-budget|vulkan_device_info|device_memory|fit_api|tts_api|tts_types|asr_stream|tts_stream|image_token_limit" --timeout 120 --output-on-failure
- name: Validate wrapper exports
shell: pwsh
run: |
Expand Down Expand Up @@ -735,7 +736,7 @@ jobs:
run: >-
cmake --build ${{ matrix.build_dir }} --target
llamadart_owner_api_test llamadart_owner_reentrancy_test llamadart_file_sha256_test llamadart_model_fd_test llamadart_threadpool_test llamadart_exit_teardown_test llamadart_image_token_limit_test llamadart_fit_api_test llamadart_device_memory_test
llamadart_grammar_top_k_test
llamadart_grammar_top_k_test llamadart_grammar_top_k_p_test
llamadart_build_identity_test llamadart_cancel_flag_test
llamadart_f16_dot_accumulation_test
llamadart_upstream_abi_test llamadart_mtmd_compat_test
Expand Down
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,11 @@ if (LLAMADART_BUILD_TESTS)
target_compile_definitions(llamadart_grammar_top_k_test PRIVATE LLAMADART_TESTING=1)
target_include_directories(llamadart_grammar_top_k_test PRIVATE src)
target_link_libraries(llamadart_grammar_top_k_test PRIVATE llamadart_lib)
add_executable(llamadart_grammar_top_k_p_test tests/grammar_top_k_p_test.cpp)
target_compile_features(llamadart_grammar_top_k_p_test PRIVATE cxx_std_17)
target_compile_definitions(llamadart_grammar_top_k_p_test PRIVATE LLAMADART_TESTING=1)
target_include_directories(llamadart_grammar_top_k_p_test PRIVATE src)
target_link_libraries(llamadart_grammar_top_k_p_test PRIVATE llamadart_lib)
set(grammar_top_k_model "${CMAKE_CURRENT_BINARY_DIR}/grammar_top_k_model.gguf")
add_test(NAME llamadart_grammar_top_k_make-model
COMMAND llamadart_exit_teardown_test make-grammar-top-k-model "${grammar_top_k_model}")
Expand All @@ -722,6 +727,10 @@ if (LLAMADART_BUILD_TESTS)
COMMAND llamadart_grammar_top_k_test "${grammar_top_k_model}")
set_tests_properties(llamadart_grammar_top_k_test
PROPERTIES FIXTURES_REQUIRED llamadart_grammar_top_k_model TIMEOUT 30)
add_test(NAME llamadart_grammar_top_k_p_test
COMMAND llamadart_grammar_top_k_p_test "${grammar_top_k_model}")
set_tests_properties(llamadart_grammar_top_k_p_test
PROPERTIES FIXTURES_REQUIRED llamadart_grammar_top_k_model TIMEOUT 60)

if (APPLE OR CMAKE_SYSTEM_NAME STREQUAL "Linux")
add_test(NAME llamadart_owner_model_test
Expand Down Expand Up @@ -1003,7 +1012,7 @@ if (LLAMADART_BUILD_TESTS)
llamadart_build_identity_test llamadart_upstream_abi_test llamadart_cancel_flag_test
llamadart_tts_eval_test llamadart_tts_seed_test llamadart_tts_batch_test llamadart_tts_sampler_test
llamadart_exit_teardown_test llamadart_model_fd_test llamadart_threadpool_test
llamadart_grammar_top_k_test llamadart_file_sha256_test
llamadart_grammar_top_k_test llamadart_grammar_top_k_p_test llamadart_file_sha256_test
llamadart_mtmd_compat_test
llamadart_vulkan_device_info_test llamadart_barrier_test llamadart_image_token_limit_test
llamadart_fit_api_test llamadart_device_memory_test)
Expand Down
48 changes: 45 additions & 3 deletions docs/sampler_selection_guard.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,43 @@ same upstream ordering breaks ties. Top-k's sort can order equal logits
differently at different values of k, and greedy takes the first maximum
in the resulting order.

Bindings can check for the new export and keep the ordinary grammar path on
older runtimes. No existing signatures, structs or API versions change. The
native export gate requires 120 symbols including the new constructor.
Bindings can check for the optional export and keep the ordinary grammar path
on older runtimes.

## Progressive strict grammar before top-k, top-p and min-p

`llama_dart_sampler_init_grammar_top_k_p(grammar, top_k, top_p, min_p,
min_keep)` uses the same ownership, vocabulary lifetime and teardown rules.
Pass the chain's next filter parameters, with `0` for disabled top-k and
min-p, `1` for disabled top-p, and a positive `min_keep`. Pass `1` and `0`
for top-p and min-p if another filter, such as typical sampling, runs
before those filters. A greedy chain that preserves and selects the highest
logit can use `(1, 1, 0, 1)`.

The wrapper checks a unique raw maximum in the greedy case. It then probes
up to 256 and 1,024 high-logit tokens with the strict grammar's read-only
apply. The second probe reuses the first 256 grammar decisions. When the
ordered legal top-k is fixed, it forwards that exact prefix. Otherwise it
can compact a prefix when both bounds on the rounded top-p cumulative sum
give the same cutoff, or when the actual float min-p threshold excludes
the tail and at least `min_keep` legal tokens clear it. The ordinary
samplers still run, and selection and acceptance stay in the guarded
sampling call. A chain that consumes intermediate top-p probabilities
directly needs the full scan because the compact softmax can produce
different probabilities before the next sampler recomputes them.

The top-p bound accounts for float softmax accumulation in both input
orders, omitted tokens tied at the probe cutoff, and rounding of each
normalized prefix weight. For an already sorted array, certification
requires the ordered top-k prefix the upstream sampler would retain.
With top-k disabled, min-p alone keeps the full scan because the unsorted
min-p path preserves original token order. K above 128, top-p logits
outside the bucket sort range, adverse rounding modes, insufficient
legal candidates and ambiguous cutoffs also keep the full scan. An
ambiguous top-p cutoff takes the full scan even with min-p enabled.

The legacy constructor and its behavior are unchanged. The native export
gate now requires 123 symbols, including both constructors.

## Validation

Expand All @@ -98,3 +132,11 @@ and seeded samples with a separate grammar-first chain. It also compares
greedy selection with a one-candidate probe ahead of top-k 64, including a
tie that takes the full mask. A generated vocabulary variant covers JSON
escapes, split UTF-8 and EOG through the real grammar.

`llamadart_grammar_top_k_p_test` checks both probe stages, fallback
boundaries, ordered shortlist and seeded selection against full-mask
chains. It covers analytical chi-squared checks, randomized candidate
arrays, real-grammar escapes, UTF-8 and EOG, and constructor ownership
through clone, reset, allocation failure and free. The exit teardown
`blocked` scenario checks that the constructor cannot take ownership
after teardown.
2 changes: 2 additions & 0 deletions src/llama_dart_grammar_top_k_test_hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ extern "C" LLAMADART_API llama_token llama_dart_test_sample_candidates(

extern "C" LLAMADART_API void
llama_dart_test_set_grammar_top_k_init_hook(void (*hook)());
extern "C" LLAMADART_API void
llama_dart_test_set_grammar_top_k_p_init_hook(void (*hook)());
#endif
Loading
Loading