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

[tokenizer] Return if exceed max token length #2957

Merged
merged 1 commit into from
Jan 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class Encoding {
private long[] specialTokenMask;
private CharSpan[] charTokenSpans;
private Encoding[] overflowing;
private boolean exceedMaxLength;

protected Encoding(
long[] ids,
Expand All @@ -36,6 +37,7 @@ protected Encoding(
long[] attentionMask,
long[] specialTokenMask,
CharSpan[] charTokenSpans,
boolean exceedMaxLength,
Encoding[] overflowing) {
this.ids = ids;
this.typeIds = typeIds;
Expand All @@ -44,6 +46,7 @@ protected Encoding(
this.attentionMask = attentionMask;
this.specialTokenMask = specialTokenMask;
this.charTokenSpans = charTokenSpans;
this.exceedMaxLength = exceedMaxLength;
this.overflowing = overflowing;
}

Expand Down Expand Up @@ -127,6 +130,15 @@ public CharSpan[] getCharTokenSpans() {
return charTokenSpans;
}

/**
* Returns if tokens exceed max length.
*
* @return {@code true} if tokens exceed max length
*/
public boolean exceedMaxLength() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be even better if we can get how many tokens exceeded.

return exceedMaxLength;
}

/**
* Returns an array of overflowing encodings.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,10 @@ private Encoding toEncoding(long encoding, boolean withOverflowingTokens) {
long[] specialTokenMask = TokenizersLibrary.LIB.getSpecialTokenMask(encoding);
CharSpan[] charSpans = TokenizersLibrary.LIB.getTokenCharSpans(encoding);

long[] overflowingHandles = TokenizersLibrary.LIB.getOverflowing(encoding);
boolean exceedMaxLength = overflowingHandles.length > 0;
Encoding[] overflowing;
if (withOverflowingTokens) {
long[] overflowingHandles = TokenizersLibrary.LIB.getOverflowing(encoding);

overflowing = new Encoding[overflowingHandles.length];
for (int i = 0; i < overflowingHandles.length; ++i) {
overflowing[i] = toEncoding(overflowingHandles[i], true);
Expand All @@ -551,6 +551,7 @@ private Encoding toEncoding(long encoding, boolean withOverflowingTokens) {
attentionMask,
specialTokenMask,
charSpans,
exceedMaxLength,
overflowing);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,9 @@ public void testTruncationStride() throws IOException {
.build()) {
String text = "Hello there my friend I am happy to see you";
String textPair = "How are you my friend";
Encoding[] overflowing = tokenizer.encode(text, textPair).getOverflowing();
Encoding encoding = tokenizer.encode(text, textPair);
Assert.assertTrue(encoding.exceedMaxLength());
Encoding[] overflowing = encoding.getOverflowing();

int expectedNumberOfOverflowEncodings = 7;
Assert.assertEquals(overflowing.length, expectedNumberOfOverflowEncodings);
Expand Down
Loading