Repository navigation
Description
System Information (please complete the following information):
- OS & Version: Windows 11, with both ICU and NLS
- ML.NET Version: Microsoft.ML.Tokenizers 3.0.0, and main has the same code
- .NET Version: .NET 10.0
Describe the bug
With RemoveNonSpacingMarks = true, BertNormalizer decomposes the text (NFD) and drops the non-spacing marks. NFD splits each Hangul syllable into jamo, which aren't marks, so they stay. But Normalize ends with an NFC, which puts the jamo back together into syllables.
Hugging Face doesn't recompose, so its uncased vocabularies have Korean as jamo. The vocab.txt of bert-base-multilingual-uncased has 1,459 tokens with jamo and none with Hangul syllables, so every Korean word becomes [UNK].
To Reproduce
- Create a console app:
dotnet new console dotnet add package Microsoft.ML.Tokenizers --version 3.0.0 - Download
vocab.txtof bert-base-multilingual-uncased into the project folder. Its tokenizer lowercases and strips accents, which in ML.NET isRemoveNonSpacingMarks = true(lowercasing is on by default). - Replace
Program.cswith:using Microsoft.ML.Tokenizers; var vocab = File.ReadAllLines("vocab.txt"); var tokenizer = BertTokenizer.Create("vocab.txt", new BertOptions { RemoveNonSpacingMarks = true }); var ids = tokenizer.EncodeToIds("서울은 한국의 수도입니다."); Console.WriteLine(string.Join(" ", ids.Select(id => vocab[id]))); Console.WriteLine(string.Join(" ", ids));
dotnet runprints:[CLS] [UNK] [UNK] [UNK] . [SEP] 101 100 100 100 119 102
Expected behavior
The same tokens as Hugging Face. With its tokenizers package (pip install tokenizers):
from tokenizers import Tokenizer
tokenizer = Tokenizer.from_pretrained("google-bert/bert-base-multilingual-uncased")
encoding = tokenizer.encode("서울은 한국의 수도입니다.")
print(" ".join(encoding.tokens))
print(" ".join(map(str, encoding.ids)))[CLS] 서울 ##은 한국 ##의 수도 ##입 ##니다 . [SEP]
101 47403 11375 47529 10576 66548 62776 82566 119 102
The Korean tokens look like syllables, but they're jamo: 서울 is U+1109 U+1165 U+110B U+116E U+11AF.
Screenshots, Code, Sample Projects
The NFC is at the end of BertNormalizer.Normalize:
| string result = index == 0 ? string.Empty : new string(buffer, 0, index).Normalize(NormalizationForm.FormC); |
Hugging Face's BertNormalizer decomposes and strips the marks, but doesn't recompose: bert.rs.
If I skip that NFC, with a custom Normalizer that otherwise does the same for this text, I get exactly Hugging Face's IDs, so removing it should fix this.
Additional context
- It's not only Korean. With
RemoveNonSpacingMarks = true, 28 other characters are recomposed the same way, for example the Bengali vowel sign U+09CB. With cased models, the NFC changes 1,118 characters that Hugging Face leaves as they are, mostly CJK compatibility ideographs (U+F900 becomes U+8C48), and it also composes decomposed accents. Removing the NFC makes those match Hugging Face too. - Without that NFC, the default
BertTokenizerwouldn't callstring.Normalizeat all, so it would behave the same in globalization-invariant mode (BertTokenizer:RemoveNonSpacingMarks = truefails to strip accents from precomposed characters under invariant globalization #7728). It's also the NFC that throws on U+FFFE whenRemoveNonSpacingMarksis false (BertTokenizer throws ArgumentException on lone surrogates and U+FFFE #7950).
Activity