r/learnmachinelearning • u/ivan_digital • 3h ago
Tutorial A Unicode preprocessing bug in a Kokoro port: nasal vowels vanished before inference
I help maintain speech-swift, which runs speech models in Swift. We recently fixed an input-tokenization bug in its Core ML port of Kokoro, the text-to-speech model.
Swift iterates a string by Character, which is an extended grapheme cluster. Kokoro has separate vocabulary entries for a vowel such as ɔ and its combining nasal tilde, U+0303. In bɔ̃bɔ̃, Swift sees four characters. Looking up each character as a single vocabulary entry dropped both vowel-plus-tilde groups, leaving only the two b tokens.
This small example reproduces the issue and the lookup strategy we use now. It runs with the Swift standard library; no model download is needed:
let vocab: [String: Int] = ["b": 44, "ɔ": 76, "\u{0303}": 17, "ç": 78]
let phonemes = "bɔ\u{0303}bɔ\u{0303}"
func tokenIDs(_ text: String) -> [Int] {
text.flatMap { character -> [Int] in
let symbol = String(character)
if let id = vocab[symbol] { return [id] }
let scalars = symbol.unicodeScalars
let ids = scalars.compactMap { vocab[String($0)] }
return scalars.count > 1 && ids.count == scalars.count ? ids : []
}
}
print(phonemes.compactMap { vocab[String($0)] })
// [44, 44]
print(tokenIDs(phonemes))
// [44, 76, 17, 44, 76, 17]Trying the full symbol first matters too: Swift's string dictionary treats ç and c\u{0327} as canonically equivalent. If that lookup fails, we split the cluster only when every scalar has a vocabulary entry. That preserves the nasal vowel without keeping half of an unsupported symbol.
Our regression tests cover nasal vowels, canonical equivalence and unsupported combining marks. The change is on the library's main branch; the v0.0.30 release predates it. This example checks token IDs, rather than measuring generated speech quality.
Tokenizer source · Regression tests
When validating a model port, how do you check that its preprocessing emits the same token IDs as the reference?