diff --git a/docs/tts/configuration.md b/docs/tts/configuration.md index 6d614853..9ce4a4ab 100644 --- a/docs/tts/configuration.md +++ b/docs/tts/configuration.md @@ -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.` | - | 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 | diff --git a/include/nemo_speech/tts.h b/include/nemo_speech/tts.h index 9cb2547f..e0df5900 100644 --- a/include/nemo_speech/tts.h +++ b/include/nemo_speech/tts.h @@ -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 { diff --git a/src/tts/c_api.cpp b/src/tts/c_api.cpp index 6f7e9c37..b86c934e 100644 --- a/src/tts/c_api.cpp +++ b/src/tts/c_api.cpp @@ -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); diff --git a/src/tts/magpietts/config.cpp b/src/tts/magpietts/config.cpp index ed22b6d3..380d7393 100644 --- a/src/tts/magpietts/config.cpp +++ b/src/tts/magpietts/config.cpp @@ -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 { diff --git a/src/tts/tokenizer/tokenizer.cpp b/src/tts/tokenizer/tokenizer.cpp index 88be5c91..f2bc12d2 100644 --- a/src/tts/tokenizer/tokenizer.cpp +++ b/src/tts/tokenizer/tokenizer.cpp @@ -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); @@ -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); @@ -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 lock(cache_mutex_); diff --git a/src/tts/tokenizer/tokenizer.h b/src/tts/tokenizer/tokenizer.h index 9853752f..afbd0236 100644 --- a/src/tts/tokenizer/tokenizer.h +++ b/src/tts/tokenizer/tokenizer.h @@ -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); }; diff --git a/src/tts/tokenizer/tokenizer_impl.cpp b/src/tts/tokenizer/tokenizer_impl.cpp index 13009cc9..7aa3c9e1 100644 --- a/src/tts/tokenizer/tokenizer_impl.cpp +++ b/src/tts/tokenizer/tokenizer_impl.cpp @@ -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 ..., (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 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 tokens = hindi_tokens(); diff --git a/tests/cpp/tts/CMakeLists.txt b/tests/cpp/tts/CMakeLists.txt index 7dfcacdb..e38b2545 100644 --- a/tests/cpp/tts/CMakeLists.txt +++ b/tests/cpp/tts/CMakeLists.txt @@ -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( diff --git a/tests/cpp/tts/test_tokenizer_latin_as_english.cpp b/tests/cpp/tts/test_tokenizer_latin_as_english.cpp new file mode 100644 index 00000000..eb60804e --- /dev/null +++ b/tests/cpp/tts/test_tokenizer_latin_as_english.cpp @@ -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 + EOS. +#include +#include +#include +#include + +#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 +strip_eos(std::vector 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 \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 + + 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 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")) { + 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"); + } + + // 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; +}