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

Support for split UTF-8 sequences #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
38 changes: 35 additions & 3 deletions Llama3.java
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,12 @@ class Tokenizer {
private final Vocabulary vocabulary;
private final Map<Pair<Integer, Integer>, Integer> merges;
private final Map<String, Integer> specialTokens;
/** buffer to store incomplete UTF-8 sequence */
private final byte[] bufUtf8 = new byte[4];
/** index in UTF-8 buffer */
private int bufUtf8Index = 0;
/** number of expected bytes in UTF-8 buffer */
private int bufUtf8Size = 0;

public String regexPattern() {
if (compiledPattern == null) {
Expand Down Expand Up @@ -1324,11 +1330,37 @@ public List<Integer> encodeAsList(String text) {
public String decode(List<Integer> tokens) {
String decoded = decodeImpl(tokens);
int[] decodedBytesAsInts = decoded.codePoints().map(BYTE_DECODER::get).toArray();
byte[] rawBytes = new byte[decodedBytesAsInts.length];
byte[] rawBytes = new byte[decodedBytesAsInts.length + 3];
int indexRawByte = 0;
for (int i = 0; i < decoded.length(); i++) {
rawBytes[i] = (byte) decodedBytesAsInts[i];
byte b = (byte) decodedBytesAsInts[i];
if ((b & 0b11100000) == 0b11000000 && bufUtf8Index == 0) {
bufUtf8Size = 2; // Start of UTF-8 two bytes sequence.
bufUtf8[bufUtf8Index++] = b;
continue;
}
if ((b & 0b11110000) == 0b11100000 && bufUtf8Index == 0) {
bufUtf8Size = 3; // Start of UTF-8 three bytes sequence.
bufUtf8[bufUtf8Index++] = b;
continue;
}
if ((b & 0b11111000) == 0b11110000 && bufUtf8Index == 0) {
bufUtf8Size = 4; // Start of UTF-8 four bytes sequence.
bufUtf8[bufUtf8Index++] = b;
continue;
}
if (bufUtf8Index > 0) {
bufUtf8[bufUtf8Index++] = b;
if (bufUtf8Index == bufUtf8Size) {
System.arraycopy(bufUtf8, 0, rawBytes, indexRawByte, bufUtf8Size);
indexRawByte += bufUtf8Size;
bufUtf8Index = 0;
}
continue;
}
rawBytes[indexRawByte++] = b;
}
return new String(rawBytes, StandardCharsets.UTF_8);
return new String(rawBytes, 0, indexRawByte, StandardCharsets.UTF_8);
}
}

Expand Down