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

Sync with tiktoken #87

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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 @@ -178,7 +178,7 @@ public static void main(String[] args) throws Exception {
}

var totalSize = calculateTotalFileSize(rootFolder);
if (totalSize != 99_945_723) {
if (totalSize != 99_945_750) {
throw new AssertionError("Total size did not match expected value, actual: " + totalSize);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/java/com/knuddels/jtokkit/Cl100kParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static void split(String input, Predicate<ByteArrayList> fragmentConsumer) {

if ((c0 == '\'') && c1 > 0) {
if (isShortContraction(c1)) {
// 1) `'[sdtm]` - contractions, such as the suffixes of `he's`, `I'd`, `'tis`, `I'm`
// 1) `'[sdmt]` - contractions, such as the suffixes of `he's`, `I'd`, `'tis`, `I'm`
endIndex += 2;
finished = fragmentConsumer.test(addUtf8Bytes(input, startIndex, endIndex, utf8Bytes));
continue;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/main/java/com/knuddels/jtokkit/EncodingFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static Encoding p50kEdit() {
* @return an {@link Encoding} instance for the cl100k_base encoding
*/
static Encoding cl100kBase() {
// "'(?:[sdmt]|ll|ve|re)|[^\r\n\\p{L}\\p{N}]?+\\p{L}+|\\p{N}{1,3}| ?[^\\s\\p{L}\\p{N}]++[\r\n]*|\\s*[\r\n]|\\s+(?!\\S)|\\s+"
// "'(?:[sdmt]|ll|ve|re)|[^\r\n\\p{L}\\p{N}]?+\\p{L}++|\\p{N}{1,3}+| ?[^\\s\\p{L}\\p{N}]++[\r\n]*+|\\s++$|\\s*[\r\n]|\\s+(?!\\S)|\\s"
Map<byte[], Integer> mergeableRanks = loadMergeableRanks("/com/knuddels/jtokkit/cl100k_base.tiktoken");
GptBytePairEncodingParams params = new GptBytePairEncodingParams("cl100k_base", null, mergeableRanks, SPECIAL_TOKENS_CL100K_BASE);
return new Cl100kGptBytePairEncoding(params);
Expand All @@ -122,7 +122,7 @@ private static Encoding from50kParameters(
String fileName,
Map<String, Integer> specialTokens
) {
Pattern regex = compileRegex("'(?:[sdmt]|ll|ve|re)| ?\\p{L}+| ?\\p{N}+| ?[^\\s\\p{L}\\p{N}]+|\\s+(?!\\S)|\\s+", false);
Pattern regex = compileRegex("'(?:[sdmt]|ll|ve|re)| ?\\p{L}++| ?\\p{N}++| ?[^\\s\\p{L}\\p{N}]++|\\s++$|\\s+(?!\\S)|\\s", false);
Map<byte[], Integer> mergeableRanks = loadMergeableRanks(fileName);
GptBytePairEncodingParams params = new GptBytePairEncodingParams(name, regex, mergeableRanks, specialTokens);
return fromParameters(params);
Expand Down
6 changes: 1 addition & 5 deletions lib/src/main/java/com/knuddels/jtokkit/TokenEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,7 @@ int mergeBytesAndGetTokenCount(ByteArrayWrapper piece, int length, IntArrayList
ranks.set(nextIndex, DUMMY_RANK);

length--;
if (length < 3) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

micro-optimization, let's simplify

break; // single tokens were already filtered out, let's skip a minimum calculation
} else {
minRankIndex = getMinRankIndex(ranks);
}
minRankIndex = getMinRankIndex(ranks);
}
assert getMinRankIndex(ranks) < 0;
return length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static int calculateTokensLarge(TokenEncoder tokenEncoder, int maxTokenCount, bo
assert rankMap.containsKey(MAX_RANK);

int tokenCount = match.length();
while (tokenCount > 2 && rankMap.size() > 1) {
while (rankMap.size() > 1) {
for (Iterator<RankNode> it = rankMap.pollFirstEntry().getValue().values().iterator(); it.hasNext(); ) {
RankNode minNode = it.next();
int minRank = minNode.rank;
Expand Down