From d6830531efa1d2c9acd9c3e8e73be13223885490 Mon Sep 17 00:00:00 2001 From: Jhin Lee Date: Sat, 7 Nov 2026 11:22:24 -0500 Subject: [PATCH] Apply the GGUF's suppress tokens in the TTS talker sampler The talker sampler omitted tokenizer.ggml.suppress_tokens, which upstream common_sampler applies. The Qwen3-TTS converter suppresses the codec's control tokens there; a sampled control token reaches code2wav as an out-of-range code and fails the synthesis with no PCM. --- .github/workflows/validate_wrapper.yml | 1 + CMakeLists.txt | 9 ++- README.md | 6 ++ docs/v090_qualification.md | 4 ++ src/llama_dart_tts_sampler_internal.h | 41 +++++++++++++ src/llama_dart_wrapper.cpp | 26 ++++---- tests/tts_sampler_test.cpp | 83 ++++++++++++++++++++++++++ 7 files changed, 153 insertions(+), 17 deletions(-) create mode 100644 src/llama_dart_tts_sampler_internal.h create mode 100644 tests/tts_sampler_test.cpp diff --git a/.github/workflows/validate_wrapper.yml b/.github/workflows/validate_wrapper.yml index 1284a7d9..8cfb91f7 100644 --- a/.github/workflows/validate_wrapper.yml +++ b/.github/workflows/validate_wrapper.yml @@ -530,6 +530,7 @@ jobs: llamadart_tts_types_test llamadart_tts_eval_test llamadart_tts_seed_test + llamadart_tts_sampler_test llamadart_mtmd_compat_test llamadart_image_token_limit_test llamadart_barrier_test diff --git a/CMakeLists.txt b/CMakeLists.txt index fe3b5b1d..d6439765 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -428,6 +428,12 @@ if (LLAMADART_BUILD_TESTS) ) add_test(NAME llamadart_tts_seed_test COMMAND llamadart_tts_seed_test) + add_executable(llamadart_tts_sampler_test tests/tts_sampler_test.cpp) + target_compile_features(llamadart_tts_sampler_test PRIVATE cxx_std_17) + target_include_directories(llamadart_tts_sampler_test PRIVATE src) + target_link_libraries(llamadart_tts_sampler_test PRIVATE llama) + add_test(NAME llamadart_tts_sampler_test COMMAND llamadart_tts_sampler_test) + add_executable(llamadart_image_token_limit_test tests/image_token_limit_test.cpp) target_compile_features(llamadart_image_token_limit_test PRIVATE cxx_std_17) target_include_directories(llamadart_image_token_limit_test PRIVATE @@ -705,7 +711,8 @@ if (LLAMADART_BUILD_TESTS) foreach(test_target llamadart_owner_api_test llamadart_owner_reentrancy_test llamadart_speculative_api_test llamadart_tts_api_test llamadart_tts_types_test llamadart_build_identity_test llamadart_cancel_flag_test - llamadart_tts_eval_test llamadart_tts_seed_test llamadart_exit_teardown_test + llamadart_tts_eval_test llamadart_tts_seed_test llamadart_tts_sampler_test + llamadart_exit_teardown_test llamadart_mtmd_compat_test llamadart_vulkan_device_info_test llamadart_barrier_test llamadart_image_token_limit_test) if (MSVC) diff --git a/README.md b/README.md index 8a736ecc..c3e7b4a6 100644 --- a/README.md +++ b/README.md @@ -517,6 +517,12 @@ runs one code-predictor call under a different seed first. Each task therefore starts the RNG from its own seed, and a repeated request and seed repeat the PCM. `LLAMA_DEFAULT_SEED` draws one random seed per task for both samplers. +The talker sampler gives the GGUF's `tokenizer.ggml.suppress_tokens` a `-inf` +bias, as upstream `common_sampler` does. For Qwen3-TTS these are the codec's +control tokens, which code2wav rejects as out-of-range codes, failing the +synthesis. Earlier wrappers did not apply them, so a fixed seed can give +different PCM where a control token reached the sampled candidates. + The local smoke target is opt-in because it requires large model artifacts: ```bash diff --git a/docs/v090_qualification.md b/docs/v090_qualification.md index a6f29e47..fa497899 100644 --- a/docs/v090_qualification.md +++ b/docs/v090_qualification.md @@ -24,6 +24,10 @@ The wrapper builds against v0.9.0 without source changes, and every - `MTMD_GEN_AUDIO_TYPE_CHATTERBOX` maps to `LLAMA_DART_TTS_MODEL_TYPE_UNKNOWN`, so the TTS entry points report it unsupported. Chatterbox and the new predefined-voice API are not wrapped. +- The TTS talker sampler now applies the GGUF's suppress tokens. A fixed + Qwen3-TTS seed can give different PCM from v0.8.0 wrappers where a codec + control token reached the sampled candidates; with the ggml-org 1.7B Base + Q4_K_M GGUF, 40 seed and sampling combinations gave identical PCM. Evidence, 2026-11-06, Apple M4 Max, macOS arm64, Metal on, Release, `LLAMADART_BUILD_TESTS=ON`, `LLAMADART_BUILD_TTS_SMOKE=ON`: native CTest diff --git a/src/llama_dart_tts_sampler_internal.h b/src/llama_dart_tts_sampler_internal.h new file mode 100644 index 00000000..925179c9 --- /dev/null +++ b/src/llama_dart_tts_sampler_internal.h @@ -0,0 +1,41 @@ +#pragma once + +#include "llama.h" +#include "llama_dart_wrapper.h" + +#include +#include +#include + +// The talker's sampler chain. As in upstream common_sampler, the GGUF's +// tokenizer.ggml.suppress_tokens get a -inf bias before any other stage: the +// Qwen3-TTS converter lists the codec's control tokens there, and code2wav +// rejects a control token as an out-of-range code. +static inline llama_sampler *llama_dart_tts_sampler_chain_init( + const llama_dart_tts_request &request, uint32_t seed, int32_t n_vocab, + const llama_token *suppress, int32_t n_suppress) { + llama_sampler *sampler = + llama_sampler_chain_init(llama_sampler_chain_default_params()); + if (sampler == nullptr) { + return nullptr; + } + if (suppress != nullptr && n_suppress > 0) { + std::vector bias; + bias.reserve(static_cast(n_suppress)); + for (int32_t i = 0; i < n_suppress; ++i) { + bias.push_back({suppress[i], -INFINITY}); + } + llama_sampler_chain_add( + sampler, llama_sampler_init_logit_bias( + n_vocab, static_cast(bias.size()), bias.data())); + } + llama_sampler_chain_add(sampler, llama_sampler_init_top_k(request.top_k)); + llama_sampler_chain_add(sampler, + llama_sampler_init_top_p(request.top_p, 1)); + llama_sampler_chain_add(sampler, + llama_sampler_init_min_p(request.min_p, 1)); + llama_sampler_chain_add( + sampler, llama_sampler_init_temp(request.temperature)); + llama_sampler_chain_add(sampler, llama_sampler_init_dist(seed)); + return sampler; +} diff --git a/src/llama_dart_wrapper.cpp b/src/llama_dart_wrapper.cpp index 2ca1be2f..d6ec441f 100644 --- a/src/llama_dart_wrapper.cpp +++ b/src/llama_dart_wrapper.cpp @@ -8,6 +8,7 @@ #include "llama_dart_speculative_internal.h" #include "llama_dart_tts_eval_internal.h" #include "llama_dart_tts_internal.h" +#include "llama_dart_tts_sampler_internal.h" #include "llama_dart_tts_seed_internal.h" #include "llama_dart_tts_types_internal.h" @@ -115,22 +116,14 @@ static uint32_t llama_dart_tts_capabilities( } static llama_sampler * -llama_dart_tts_sampler_init(const llama_dart_tts_request &request, +llama_dart_tts_sampler_init(const llama_vocab *vocab, + const llama_dart_tts_request &request, uint32_t seed) { - llama_sampler *sampler = - llama_sampler_chain_init(llama_sampler_chain_default_params()); - if (sampler == nullptr) { - return nullptr; - } - llama_sampler_chain_add(sampler, llama_sampler_init_top_k(request.top_k)); - llama_sampler_chain_add(sampler, - llama_sampler_init_top_p(request.top_p, 1)); - llama_sampler_chain_add(sampler, - llama_sampler_init_min_p(request.min_p, 1)); - llama_sampler_chain_add( - sampler, llama_sampler_init_temp(request.temperature)); - llama_sampler_chain_add(sampler, llama_sampler_init_dist(seed)); - return sampler; + int32_t n_suppress = 0; + const llama_token *suppress = + llama_vocab_get_suppress_tokens(vocab, &n_suppress); + return llama_dart_tts_sampler_chain_init( + request, seed, llama_vocab_n_tokens(vocab), suppress, n_suppress); } static bool llama_dart_tts_prime_code_predictor(llama_dart_tts *tts, @@ -2230,7 +2223,8 @@ static enum llama_dart_tts_status llama_dart_tts_start_impl( std::random_device random_seed; const uint32_t seed = llama_dart_tts_resolve_seed(request->seed, [&] { return random_seed(); }); - tts->sampler = llama_dart_tts_sampler_init(*request, seed); + tts->sampler = llama_dart_tts_sampler_init( + llama_model_get_vocab(llama_get_model(tts->llama)), *request, seed); if (tts->sampler == nullptr) { return llama_dart_tts_fail(tts, LLAMA_DART_TTS_STATUS_UPSTREAM_ERROR, "sampler initialization failed"); diff --git a/tests/tts_sampler_test.cpp b/tests/tts_sampler_test.cpp new file mode 100644 index 00000000..d473f97f --- /dev/null +++ b/tests/tts_sampler_test.cpp @@ -0,0 +1,83 @@ +#ifdef NDEBUG +#error "Wrapper contract tests require active assertions in every configuration" +#endif + +#include "llama_dart_tts_sampler_internal.h" + +#include +#include +#include + +namespace { + +// A Qwen3-TTS-like tail: the codec's control tokens follow its codes, and the +// GGUF suppresses every control token except the end-of-speech one. +constexpr int32_t kVocab = 64; +constexpr int32_t kFirstControl = 48; +constexpr llama_token kEndOfSpeech = 50; + +std::vector control_tokens() { + std::vector tokens; + for (llama_token token = kFirstControl; token < kVocab; ++token) { + if (token != kEndOfSpeech) { + tokens.push_back(token); + } + } + return tokens; +} + +bool is_control(llama_token token) { + return token >= kFirstControl && token != kEndOfSpeech; +} + +llama_dart_tts_request request_with_top_k(int32_t top_k) { + llama_dart_tts_request request{}; + request.top_k = top_k; + request.top_p = 0.95f; + request.min_p = 0.0f; + request.temperature = 0.8f; + return request; +} + +// Every control token outranks every code. +llama_token sample(const llama_dart_tts_request &request, uint32_t seed, + const std::vector &suppress) { + llama_sampler *sampler = llama_dart_tts_sampler_chain_init( + request, seed, kVocab, suppress.empty() ? nullptr : suppress.data(), + static_cast(suppress.size())); + assert(sampler != nullptr); + std::vector data; + for (llama_token token = 0; token < kVocab; ++token) { + const float logit = is_control(token) ? 10.0f + token : token * 0.01f; + data.push_back({token, logit, 0.0f}); + } + llama_token_data_array candidates{data.data(), data.size(), -1, false}; + llama_sampler_apply(sampler, &candidates); + assert(candidates.selected >= 0 && + static_cast(candidates.selected) < candidates.size); + const llama_token token = candidates.data[candidates.selected].id; + llama_sampler_free(sampler); + return token; +} + +void test_suppressed_tokens_are_never_sampled() { + const std::vector suppress = control_tokens(); + for (int32_t top_k : {0, 1, 40, kVocab}) { + for (uint32_t seed = 1; seed <= 64; ++seed) { + assert(!is_control(sample(request_with_top_k(top_k), seed, suppress))); + } + } + assert(sample(request_with_top_k(1), 1, suppress) == kEndOfSpeech); +} + +void test_without_suppress_tokens_the_chain_is_unbiased() { + assert(sample(request_with_top_k(1), 1, {}) == kVocab - 1); +} + +} // namespace + +int main() { + test_suppressed_tokens_are_never_sampled(); + test_without_suppress_tokens_the_chain_is_unbiased(); + return 0; +}