Skip to content

Commit

Permalink
Merge pull request latitudegames#12 from andrew-healey/master
Browse files Browse the repository at this point in the history
Fix bug in BPE cache
  • Loading branch information
nickwalton authored Jan 13, 2023
2 parents 0adbb50 + e81024a commit 8d31919
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 4 additions & 4 deletions Encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ const byte_decoder = {}
Object.keys(byte_encoder).map(x => { byte_decoder[byte_encoder[x]] = x })

const bpe_ranks = dictZip(bpe_merges, range(0, bpe_merges.length))
const cache = {}
const cache = new Map;

function bpe(token) {
if (token in cache) {
return cache[token]
if (cache.has(token)) {
return cache.get(token)
}``

let word = token.split('')
Expand Down Expand Up @@ -147,7 +147,7 @@ function bpe(token) {
}

word = word.join(' ')
cache[token] = word
cache.set(token, word)

return word
}
Expand Down
9 changes: 8 additions & 1 deletion Encoder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ test('emojis', () => {
const str = "hello 👋 world 🌍";
expect(encode(str)).toEqual([31373, 50169, 233, 995, 12520, 234, 235])
expect(decode(encode(str))).toEqual(str)
});
});

test('properties of Object',()=>{
const str = "toString constructor hasOwnProperty valueOf";

expect(encode(str)).toEqual([1462, 10100, 23772, 468, 23858, 21746, 1988, 5189]);
expect(decode(encode(str))).toEqual(str);
})

0 comments on commit 8d31919

Please sign in to comment.