Repository navigation
Description
System Information (please complete the following information):
- OS & Version: Windows 11, with ICU and NLS
- ML.NET Version: Microsoft.ML.Tokenizers 3.0.0, and main at 218d0b8, which includes Fix BertTokenizer throwing on lone surrogates and U+FFFE #7954
- .NET Version: .NET 10.0 and .NET Framework 4.8
Describe the bug
The tokenizers call string.Normalize on the text, and it throws ArgumentException ("String contains invalid Unicode code points") on noncharacters:
- With NLS, on noncharacters like U+FFFF, U+FDD0 and U+1FFFE. .NET Framework always uses NLS, and .NET uses it with
DOTNET_SYSTEM_GLOBALIZATION_USENLS.BertTokenizerthrows with or withoutRemoveNonSpacingMarks, and so do theStripAccentsand Unicode normalization steps ofSentencePieceTokenizer.CreateFromTokenizerJson. - With ICU, on U+FFFE. Fix BertTokenizer throwing on lone surrogates and U+FFFE #7954 fixed this for
BertTokenizer, but the tokenizer.json steps still throw.
To Reproduce
using System.Text;
using Microsoft.ML.Tokenizers;
File.WriteAllLines("vocab.txt", new[] { "[PAD]", "[UNK]", "[CLS]", "[SEP]", "[MASK]", "one", "line", "##line" });
BertTokenizer.Create("vocab.txt").EncodeToIds("one\uFFFFline"); // ArgumentException with NLS
string json = """
{
"normalizer": { "type": "NFKC" },
"pre_tokenizer": { "type": "Metaspace", "replacement": "\u2581", "add_prefix_space": true },
"model": { "type": "Unigram", "unk_id": 0, "vocab": [["<unk>", 0.0], ["\u2581one", -1.0], ["line", -1.0]] }
}
""";
var tokenizer = SentencePieceTokenizer.CreateFromTokenizerJson(new MemoryStream(Encoding.UTF8.GetBytes(json)));
tokenizer.EncodeToIds("one\uFFFFline"); // ArgumentException with NLS
tokenizer.EncodeToIds("one\uFFFEline"); // ArgumentException, also with ICUFor NLS, run it with DOTNET_SYSTEM_GLOBALIZATION_USENLS=1, or on .NET Framework, where the message is "Invalid Unicode code point found at index 3".
Expected behavior
No exception. Noncharacters should work the way U+FFFF does with ICU: on main, BertTokenizer gives [UNK], and the tokenizer.json steps keep it unchanged.
Screenshots, Code, Sample Projects
Both normalizers already handle lone surrogates, and BertNormalizer also handles U+FFFE, but not the other noncharacters:
| string result = index == 0 ? string.Empty : new string(buffer, 0, index).Normalize(NormalizationForm.FormC); |
machinelearning/src/Microsoft.ML.Tokenizers/Normalizer/SentencePieceNormalizationStep.cs
Line 314 in 218d0b8
| public override string Normalize(string text) => text.Length == 0 ? text : ReplaceLoneSurrogates(text).Normalize(_form); |
Noncharacters don't change under normalization and don't combine with the characters around them, so one option is to normalize the text between them and keep them as they are.
Additional context
- I found this while fixing BertTokenizer throws ArgumentException on lone surrogates and U+FFFE #7950 in Fix BertTokenizer throwing on lone surrogates and U+FFFE #7954, which only handles what ICU rejects: U+FFFE and lone surrogates.
- With
RemoveNonSpacingMarks,BertNormalizeralso callsstring.Normalizeat the start (L39). So removing the final NFC, as BertTokenizer recomposes Korean after stripping accents, so it gives[UNK]with uncased multilingual BERT #7957 proposes, would fixBertTokenizerwith the default options, but not withRemoveNonSpacingMarks.
Activity