Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Fix Array out of bounds exception in the XLM Roberta tokenizer #106655

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/106655.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 106655
summary: Fix Array out of bounds exception in the XLM Roberta tokenizer
area: Machine Learning
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ static Config fromBase64EncodedResource(String resourcePath) throws IOException
private final int[] offsets;
// The entire normalized bytes representations delimited by NULL
private final byte[] normalizedStrUtf8Bytes;
// Continually reused to copy a single char into utf8 bytes
private final byte[] reusableCharByteBuffer = new byte[4];
// reusable char buffer for decoding utf8 bytes to determine char offset corrections
private final char[] reusableCharDecodeBuffer = new char[8];
private final char[] reusableCharDecodeBuffer = new char[64];
private Reader transformedInput;

public PrecompiledCharMapNormalizer(int[] offsets, String normalizedStr, Reader in) {
Expand Down Expand Up @@ -172,7 +170,6 @@ Reader normalize(CharSequence str) {
ByteBuffer byteBuffer = StandardCharsets.UTF_8.encode(CharBuffer.wrap(str));
byte[] strBytes = new byte[byteBuffer.limit()];
byteBuffer.get(strBytes);
int[] strCp = str.codePoints().toArray();
BreakIterator b = BreakIterator.getCharacterInstance(Locale.ROOT);
b.setText(str);
// We iterate the whole string, so b.first() is always `0`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public void testEmoji() throws IOException {
assertNormalization("😀", parsed, "😀");
}

public void testCharThatNormalizesToLongText() throws IOException {
PrecompiledCharMapNormalizer.Config parsed = loadTestCharMap();
assertNormalization("ﷺ", parsed, "صلى الله عليه وسلم");
}

private void assertNormalization(String input, PrecompiledCharMapNormalizer.Config config, String expected) throws IOException {
PrecompiledCharMapNormalizer normalizer = new PrecompiledCharMapNormalizer(
config.offsets(),
Expand Down