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
1 change: 1 addition & 0 deletions .github/workflows/validate_wrapper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions docs/v090_qualification.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions src/llama_dart_tts_sampler_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include "llama.h"
#include "llama_dart_wrapper.h"

#include <cmath>
#include <cstdint>
#include <vector>

// 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<llama_logit_bias> bias;
bias.reserve(static_cast<size_t>(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<int32_t>(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;
}
26 changes: 10 additions & 16 deletions src/llama_dart_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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");
Expand Down
83 changes: 83 additions & 0 deletions tests/tts_sampler_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <cassert>
#include <cstdint>
#include <vector>

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<llama_token> control_tokens() {
std::vector<llama_token> 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<llama_token> &suppress) {
llama_sampler *sampler = llama_dart_tts_sampler_chain_init(
request, seed, kVocab, suppress.empty() ? nullptr : suppress.data(),
static_cast<int32_t>(suppress.size()));
assert(sampler != nullptr);
std::vector<llama_token_data> 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<size_t>(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<llama_token> 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;
}
Loading