Repository navigation
Description
System Information (please complete the following information):
- OS & Version: Windows, with both ICU and NLS. Nothing throws in globalization-invariant mode.
- ML.NET Version: Microsoft.ML.Tokenizers from main at 755131b, which includes Treat tab, line feed and carriage return as whitespace and keep symbols in BertTokenizer #7820. It also throws before Treat tab, line feed and carriage return as whitespace and keep symbols in BertTokenizer #7820.
- .NET Version: .NET 8.0 and .NET 10.0
Describe the bug
BertTokenizer throws ArgumentException ("String contains invalid Unicode code points") when the text contains:
- a lone surrogate and
RemoveNonSpacingMarksis on - U+FFFE, with any options
A lone surrogate is easy to hit, for example when a text is cut into chunks in the middle of an emoji.
Both come from string.Normalize in BertNormalizer: the NFD at the start (only with RemoveNonSpacingMarks) and the NFC at the end reject these characters. #7820 drops lone surrogates in the normalizer loop, but with RemoveNonSpacingMarks the NFD runs before that loop. In globalization-invariant mode string.Normalize returns its input unchanged, so nothing throws there.
To Reproduce
using Microsoft.ML.Tokenizers;
var tokenizer = BertTokenizer.Create("vocab.txt", new BertOptions { RemoveNonSpacingMarks = true });
tokenizer.EncodeToIds("one\uD83Dline"); // ArgumentException
BertTokenizer.Create("vocab.txt").EncodeToIds("one\uFFFEline"); // ArgumentExceptionExpected behavior
No exception. Both characters should be dropped, the same way BertNormalizer already drops U+FFFD. Hugging Face gives [UNK] for U+FFFE, but it's a noncharacter, so dropping it is fine.
Screenshots, Code, Sample Projects
A fix on top of #7938, which changes the same line:
// string.Normalize throws on lone surrogates and U+FFFE. The loop below removes U+FFFD.
original = _isNormalizationSupported
? SentencePieceNormalizationStep.ReplaceLoneSurrogates(original).Replace('\uFFFE', '\uFFFD').Normalize(NormalizationForm.FormD)
: NonSpacingMarkRemover.Remove(original);SentencePieceNormalizationStep.ReplaceLoneSurrogates also needs to become internal, and the check at the top of the loop that skips U+0000 and U+FFFD needs || c == '\uFFFE'.
A regression test for BertTokenizerTests, using AssertTokens and the vocabulary from #7820. With #7938 applied, it fails without the fix and passes with it, with ICU and in invariant mode.
Test
// string.Normalize throws on a lone surrogate, as in a text cut in the middle of an emoji, and on U+FFFE, as in UTF-16 read
// with the wrong byte order.
[Theory]
[InlineData(0xD83D, false)]
[InlineData(0xD83D, true)]
[InlineData(0xDC4D, true)]
[InlineData(0xFFFE, false)]
[InlineData(0xFFFE, true)]
public void TestLoneSurrogatesAndUFFFEAreRemoved(int c, bool removeNonSpacingMarks)
{
AssertTokens($"one{(char)c}line", "[CLS] one ##line [SEP]", new BertOptions { RemoveNonSpacingMarks = removeNonSpacingMarks });
AssertTokens($"great product {(char)c}", "[CLS] great product [SEP]", new BertOptions { RemoveNonSpacingMarks = removeNonSpacingMarks });
}Additional context
I found this while reviewing #7820 and kept it out of that PR, because the fix overlaps with my PR #7938. I'll fix it once #7938 is merged.
Activity
Fix BertTokenizer throwing on lone surrogates and U+FFFE (dotnet#7954)