Skip to content
Open
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 docs/tts/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ All keys nest under `tts.`. Defaults shown; CLI alias listed where one exists.
| `tts.codec-model` | - | - | NanoCodec decoder GGUF (required) |
| `tts.tokenizer-model-dir` | - | - | extracted Magpie `.nemo` dir (required) |
| `tts.tokenizer.sentence-limit.<lang>` | - | per language (`en` 45 ... `ja` 40) | sentence-chunking threshold in words (characters for `zh`/`ja`); subkeys `en`, `es`, `fr`, `vi`, `it`, `de`, `zh`, `hi`, `ja`, `ar`, `ko`, `pt` |
| `tts.tokenizer.latin-as-english` | - | `false` | Korean text: tokenize inline Latin-letter runs (e.g. `API`, `Wi-Fi`) with the English IPA tokenizer instead of ByT5 bytes; C API field `nemo_speech_tts_model_config.latin_as_english` |
| `tts.tn-model-dir` | - | - | enables Sparrowhawk TN with this grammar dir; requires `NEMO_SPEECH_WITH_NORM=ON` |
| `tts.language-code` | - | `en-US` | default text language code |
| `tts.voice-name` | - | - | default voice name or speaker index |
Expand Down
2 changes: 2 additions & 0 deletions include/nemo_speech/tts.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ typedef struct nemo_speech_tts_model_config {
const char* tokenizer_model_dir;
// Optional written-to-spoken TN grammar root. Empty keeps text unchanged.
const char* text_normalizer_model_dir;
// Korean text: encode inline Latin-letter runs with the English IPA tokenizer (default false).
bool latin_as_english;
} nemo_speech_tts_model_config;

typedef struct nemo_speech_tts_runtime_config {
Expand Down
3 changes: 3 additions & 0 deletions src/tts/c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ synthesizer_config_of(const nemo_speech_tts_synthesizer_config* c) {
if (HAS_FIELD(model, nemo_speech_tts_model_config, text_normalizer_model_dir)) {
cfg.text_normalizer_model_dir = str_or_empty(model->text_normalizer_model_dir);
}
if (HAS_FIELD(model, nemo_speech_tts_model_config, latin_as_english)) {
cfg.tokenizer.latin_as_english = model->latin_as_english;
}
}
if (HAS_FIELD(c, nemo_speech_tts_synthesizer_config, default_language_code))
cfg.default_language_code = str_or_empty(c->default_language_code);
Expand Down
3 changes: 3 additions & 0 deletions src/tts/magpietts/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ MagpieTokenizerSentenceLimits::Register(common::ParameterParser& p) {
void
MagpieTokenizerConfig::Register(common::ParameterParser& p) {
p.Register("sentence-limit", sentence_limit);
p.Register(
"latin-as-english", &latin_as_english,
"Korean text: tokenize inline Latin-letter runs with the English IPA tokenizer");
}

namespace {
Expand Down
43 changes: 32 additions & 11 deletions src/tts/tokenizer/tokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,18 +669,8 @@ class MagpieNativeTokenizer::Impl {
"native Magpie tokenizer is not available for language_code '" + language_code +
"'");
}
p.tokenizer_name = selected->name;
p.locale = selected->locale;
p.grapheme_case = selected->grapheme_case;
p.grapheme_prefix = selected->grapheme_prefix;
p.ascii_letter_case = selected->ascii_letter_case;
p.phoneme_dict = selected->phoneme_dict;
p.heteronyms = selected->heteronyms;
p.offset = selected->offset;
apply_entry(p, *selected);
p.eos_id = profile_.eos_id;
p.expected_vocab_size = selected->size;
p.apostrophe = selected->apostrophe;
p.pad_with_space = selected->pad_with_space;
p.sentence_chunking = should_tokenize_by_sentence(text, p.language, config_.sentence_limit);

tokenizer_result native = tokenize_native(p, *selected);
Expand Down Expand Up @@ -747,11 +737,29 @@ class MagpieNativeTokenizer::Impl {
return true;
}

// Copies one profile entry's tokenizer settings into request params.
static void apply_entry(params& p, const tokenizer_entry& entry) {
p.tokenizer_name = entry.name;
p.locale = entry.locale;
p.grapheme_case = entry.grapheme_case;
p.grapheme_prefix = entry.grapheme_prefix;
p.ascii_letter_case = entry.ascii_letter_case;
p.phoneme_dict = entry.phoneme_dict;
p.heteronyms = entry.heteronyms;
p.offset = entry.offset;
p.expected_vocab_size = entry.size;
p.apostrophe = entry.apostrophe;
p.pad_with_space = entry.pad_with_space;
}

tokenizer_result tokenize_native(const params& p, const tokenizer_entry& entry) const {
switch (entry.kind) {
case tokenizer_kind::ipa:
return tokenize_ipa(p);
case tokenizer_kind::byt5:
if (config_.latin_as_english && p.language == "ko") {
return run_byt5_latin_as_english(p, english_ipa_tokenizer());
}
return run_byt5_native(p);
case tokenizer_kind::hindi_chars:
return run_hindi_native(p);
Expand Down Expand Up @@ -821,6 +829,19 @@ class MagpieNativeTokenizer::Impl {
return result;
}

// English IPA tokenizer for latin_as_english (same instance the "en" path uses).
const ipa_tokenizer& english_ipa_tokenizer() const {
const tokenizer_entry& en = profile_.entry_for_language("en");
if (en.kind != tokenizer_kind::ipa) {
throw std::runtime_error("latin_as_english requires an English IPA tokenizer profile");
}
params q;
q.model = model_dir_;
q.language = "en";
apply_entry(q, en);
return ipa_tokenizer_for("en", ipa_config_for_params(q));
}

const ipa_tokenizer& ipa_tokenizer_for(
const std::string& language, const ipa_config& cfg) const {
std::lock_guard<std::mutex> lock(cache_mutex_);
Expand Down
3 changes: 3 additions & 0 deletions src/tts/tokenizer/tokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ struct MagpieTokenizerSentenceLimits {

struct MagpieTokenizerConfig {
MagpieTokenizerSentenceLimits sentence_limit;
// Korean (ByT5) text: encode inline Latin-letter runs with the English IPA tokenizer so the
// model reads them as English words instead of raw bytes it never saw in Korean context.
bool latin_as_english = false;

void Register(common::ParameterParser& parser);
};
Expand Down
61 changes: 61 additions & 0 deletions src/tts/tokenizer/tokenizer_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,67 @@ run_byt5_native(const params& p) {
return result;
}

static bool
is_latin_letter(unsigned char c) {
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}

// End of the Latin run starting at `begin`: letters with embedded apostrophes/hyphens and single
// spaces between letter groups ("Please hold the line", "Wi-Fi", "don't"). Trailing '/- are left
// out.
static size_t
latin_run_end(const std::string& s, size_t begin) {
size_t end = begin;
while (end < s.size()) {
const unsigned char c = (unsigned char)s[end];
if (is_latin_letter(c) || c == '\'' || c == '-') {
++end;
} else if (c == ' ' && end + 1 < s.size() && is_latin_letter((unsigned char)s[end + 1])) {
++end;
} else {
break;
}
}
while (end > begin && (s[end - 1] == '\'' || s[end - 1] == '-')) {
--end;
}
return end;
}

// ByT5 (Korean) text with inline Latin runs: Latin runs go through the English IPA tokenizer,
// everything else stays bytes. The aggregated vocabulary already holds both ranges and the model
// has no language input, so a mixed token stream is well-formed; it reads the Latin as English.
// Chunk layout mirrors run_byt5_native: ... bytes/IPA ..., </s> (offset+1), EOS.
static tokenizer_result
run_byt5_latin_as_english(const params& p, const ipa_tokenizer& english) {
tokenizer_result result;
result.language = p.language;
result.tokenizer_name = p.tokenizer_name;
result.eos_id = p.eos_id;
for (const std::string& sentence : tokenizer_input_units(p, p.text)) {
chunk ch;
ch.text = sentence;
size_t i = 0;
while (i < sentence.size()) {
const unsigned char c = (unsigned char)sentence[i];
if (is_latin_letter(c)) {
const size_t end = latin_run_end(sentence, i);
const std::vector<int> ids = english.encode(sentence.substr(i, end - i));
ch.tokens.insert(ch.tokens.end(), ids.begin(), ids.end());
i = end;
} else {
ch.tokens.push_back(p.offset + (int)c + 3);
++i;
}
}
ch.tokens.push_back(p.offset + 1);
ch.tokens.push_back(result.eos_id);
pad_short_text_chunk_before_eos(ch, result.eos_id, p.offset);
result.chunks.push_back(std::move(ch));
}
return result;
}

static tokenizer_result
run_hindi_native(const params& p) {
const std::vector<std::string> tokens = hindi_tokens();
Expand Down
8 changes: 8 additions & 0 deletions tests/cpp/tts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ if(DEFINED ENV{NEMO_SPEECH_TEST_TOKENIZER_DIR_V2607})
COMMAND test_tokenizer_single_chars $ENV{NEMO_SPEECH_TEST_TOKENIZER_DIR_V2607})
endif()

add_executable(test_tokenizer_latin_as_english test_tokenizer_latin_as_english.cpp)
target_link_libraries(test_tokenizer_latin_as_english PRIVATE nemo_speech_tts_tokenizer)
if(DEFINED ENV{NEMO_SPEECH_TEST_TOKENIZER_DIR_V2607})
add_test(
NAME tokenizer_latin_as_english_v2607
COMMAND test_tokenizer_latin_as_english $ENV{NEMO_SPEECH_TEST_TOKENIZER_DIR_V2607})
endif()

if(NEMO_SPEECH_TTS_WITH_ZH)
add_executable(test_tokenizer_mandarin test_tokenizer_mandarin.cpp)
target_link_libraries(
Expand Down
99 changes: 99 additions & 0 deletions tests/cpp/tts/test_tokenizer_latin_as_english.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// latin_as_english: Korean (ByT5) text keeps its byte tokens, inline Latin runs become the
// English IPA tokens the "en" path would produce, and the chunk still ends with </s> + EOS.
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>

#include "tts/tokenizer/tokenizer.h"

namespace tts = nemo_speech::tts;

namespace {

bool
expect(bool ok, const char* what) {
if (!ok) {
std::fprintf(stderr, "FAIL: %s\n", what);
}
return ok;
}

std::vector<int32_t>
strip_eos(std::vector<int32_t> tokens) {
// "en" chunks end with EOS (and may be padded before it for short text).
while (!tokens.empty() && tokens.back() >= 3357) {
tokens.pop_back();
}
return tokens;
}

} // namespace

int
main(int argc, char** argv) {
if (argc < 2) {
std::fprintf(stderr, "usage: %s <v2607 tokenizer dir>\n", argv[0]);
return 2;
}
const std::string dir = argv[1];
tts::MagpieTokenizerConfig on;
on.latin_as_english = true;
const tts::MagpieNativeTokenizer mixed(dir, on);
const tts::MagpieNativeTokenizer plain(dir);

constexpr int32_t kKoOffset = 2973; // korean_chartokenizer in the v2607 profile
constexpr int32_t kKoEos = kKoOffset + 1;
constexpr int32_t kEos = 3358;
bool ok = true;

// 1) Korean-only text is unchanged by the flag.
const std::string ko = "안녕하십니까, 민원상담센터입니다.";
ok &= expect(
mixed.tokenize(ko, "ko").tokens == plain.tokenize(ko, "ko").tokens,
"korean-only text unchanged");

// 2) Mixed text: head bytes + English IPA + tail bytes + </s> + EOS.
const std::string head = "담당자가 ";
const std::string latin = "Please hold the line";
const std::string tail = " 이라고 안내할 수 있습니다.";
const auto got = mixed.tokenize(head + latin + tail, "ko").tokens;
std::vector<int32_t> want;
for (unsigned char b : head) want.push_back(kKoOffset + b + 3);
const auto en = strip_eos(plain.tokenize(latin, "en").tokens);
ok &= expect(!en.empty(), "english tokens non-empty");
for (int32_t t : en) ok &= expect(t >= 0 && t < 96, "english tokens in english_phoneme range");
want.insert(want.end(), en.begin(), en.end());
for (unsigned char b : tail) want.push_back(kKoOffset + b + 3);
want.push_back(kKoEos);
want.push_back(kEos);
if (!expect(got == want, "mixed token stream")) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
ok = false;
std::fprintf(stderr, "got (%zu):", got.size());
for (int32_t t : got) std::fprintf(stderr, " %d", t);
std::fprintf(stderr, "\nwant (%zu):", want.size());
for (int32_t t : want) std::fprintf(stderr, " %d", t);
std::fprintf(stderr, "\n");
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

// 3) Flag off: the same text is plain bytes (no english_phoneme-range tokens).
const auto off = plain.tokenize(head + latin + tail, "ko").tokens;
bool any_en = false;
for (int32_t t : off) any_en |= (t < 96);
ok &= expect(!any_en, "flag off keeps bytes");

// 4) Hyphen/apostrophe runs stay one English run; French (Latin script) is untouched.
const auto wifi = mixed.tokenize("서류는 Wi-Fi 연결을 확인해 주세요.", "ko").tokens;
int en_count = 0;
for (int32_t t : wifi) en_count += (t < 96);
ok &= expect(en_count >= 3, "Wi-Fi run encoded as English");
const std::string fr = "Bonjour, comment allez-vous?";
ok &= expect(
mixed.tokenize(fr, "fr").tokens == plain.tokenize(fr, "fr").tokens, "french unchanged");

std::printf("%s\n", ok ? "PASS" : "FAIL");
return ok ? 0 : 1;
}