-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathencoder.js
265 lines (227 loc) · 6.56 KB
/
encoder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
const {
dictZip,
range,
getCharCode,
getCharFromCode,
splitToken,
getPairs,
} = require("./utils");
/** @type {Record<string, number> } */
const bpeRanks = require("./data/bpe_ranks.json");
/** @type {Record<string, number> } */
const encoder = require("./data/encoder.json");
/** @type {Record<number, string>} */
const decoder = dictZip(Object.values(encoder), Object.keys(encoder));
const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();
/**
* @returns {Map<number, string>}
*/
function createByteToUnicodeMap() {
const asciiRange = range(getCharCode("!"), getCharCode("~") + 1);
const latin1Range1 = range(getCharCode("¡"), getCharCode("¬") + 1);
const latin1Range2 = range(getCharCode("®"), getCharCode("ÿ") + 1);
const initialCodePoints = [...asciiRange, ...latin1Range1, ...latin1Range2];
const mappedCodePoints = initialCodePoints.slice();
let newCodePointOffset = 0;
for (let byteValue = 0; byteValue < 2 ** 8; byteValue++) {
if (!initialCodePoints.includes(byteValue)) {
initialCodePoints.push(byteValue);
mappedCodePoints.push(2 ** 8 + newCodePointOffset);
newCodePointOffset += 1;
}
}
const unicodeChars = mappedCodePoints.map(getCharFromCode);
return new Map(initialCodePoints.map((x, i) => [x, unicodeChars[i]]));
}
const pat =
/'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+/gu;
const byteEncoder = createByteToUnicodeMap();
/** @type {Map<string, number>} */
const byteDecoder = new Map(Array.from(byteEncoder, ([k, v]) => [v, k]));
/**
* @param {string} token
* @param {Map<string, string>} cache
* @returns {string}
*/
function bpe(token, cache = new Map()) {
if (cache.has(token)) {
return /** @type {string} */ (cache.get(token));
}
let word = splitToken(token);
let pairs = getPairs(word);
if (!pairs.length) {
return token;
}
while (true) {
/** @type {Map<number, [string, string]>} */
const minPairs = new Map();
pairs.forEach((pair) => {
const rank = bpeRanks[pair.join(",")];
minPairs.set(isNaN(rank) ? 10e10 : rank, pair);
});
const keys = Array.from(minPairs.keys());
const bigram = minPairs.get(Math.min(...keys));
if (!bigram || !(bigram.join(",") in bpeRanks)) {
break;
}
const [first, second] = bigram;
/** @type {string[]} */
let newWord = [];
let i = 0;
while (i < word.length) {
const j = word.indexOf(first, i);
if (j === -1) {
newWord = newWord.concat(word.slice(i));
break;
}
newWord = newWord.concat(word.slice(i, j));
i = j;
if (word[i] === first && i < word.length - 1 && word[i + 1] === second) {
newWord.push(first + second);
i = i + 2;
} else {
newWord.push(word[i]);
i = i + 1;
}
}
word = newWord;
if (word.length === 1) {
break;
} else {
pairs = getPairs(word);
}
}
const result = word.join(" ");
cache.set(token, result);
return result;
}
/**
* @param {string} text
* @param {Map<string, string>} cache
* @returns {Generator<number[], void, undefined>}
*/
function* encodeGenerator(text, cache = new Map()) {
for (let [token] of text.matchAll(pat)) {
token = Array.from(
textEncoder.encode(token),
(x) => byteEncoder.get(x) ?? "",
).join("");
const new_tokens = bpe(token, cache)
.split(" ")
.map((x) => encoder[x]);
yield new_tokens;
}
}
/**
* @param {string} text
* @param {number} tokenLimit
* @param {Map<string, string>} cache
* @returns {false | number} false if token limit is exceeded, otherwise the number of tokens
*/
function isWithinTokenLimit(text, tokenLimit, cache = new Map()) {
const tokenGenerator = encodeGenerator(text, cache);
let count = 0;
for (const tokens of tokenGenerator) {
count += tokens.length;
if (count > tokenLimit) {
return false;
}
}
return count;
}
/**
* @param {string} text
* @param {Map<string, string>} cache
* @returns {number[]}
*/
function encode(text, cache = new Map()) {
return Array.from(encodeGenerator(text, cache)).flat(1);
}
/**
* @param {number} token
* @returns {string | undefined}
*/
function decodeToken(token) {
const decodedToken = decoder[token];
if (typeof decodedToken === "undefined") {
return "";
}
const decodedBytes = splitToken(decodedToken).map(
(x) => /** @type {number} */ (byteDecoder.get(x)),
);
return textDecoder.decode(new Uint8Array(decodedBytes), {
stream: true,
});
}
/**
* @param {string} string
* @returns {boolean}
*/
function endsWithIncompleteUtfPairSurrogate(string) {
if (string.length === 0) return false;
// Check if the last character is a high surrogate
const lastCharCode = string.charCodeAt(string.length - 1);
return lastCharCode >= 55296 && lastCharCode <= 56319;
}
/**
* @param {Iterable<number>} tokens
* @returns {Generator<string, void, undefined>}
*/
function* decodeGenerator(tokens) {
/** @type {string} */
let buffer = "";
for (const token of tokens) {
buffer += decodeToken(token);
if (buffer.length === 0 || endsWithIncompleteUtfPairSurrogate(buffer)) {
// Keep the high surrogate in the buffer and continue with the next token
continue;
} else {
yield buffer;
// reset buffer
buffer = "";
}
}
// Yield any remaining characters in the buffer
if (buffer.length > 0) {
yield buffer;
}
}
/**
* Decode tokens asynchronously and yield the decoded strings, one by one.
* Will not yield for tokens that include a high surrogate, but wait for the next token.
* @param {AsyncIterable<number>} tokens
* @returns {AsyncGenerator<string, void, undefined>}
*/
async function* decodeAsyncGenerator(tokens) {
/** @type {string} */
let buffer = "";
for await (const token of tokens) {
buffer += decodeToken(token);
if (buffer.length === 0 || endsWithIncompleteUtfPairSurrogate(buffer)) {
// Keep the high surrogate in the buffer and continue with the next token
continue;
} else {
yield buffer;
// reset buffer
buffer = "";
}
}
// Yield any remaining characters in the buffer
if (buffer.length > 0) {
yield buffer;
}
}
/**
* @param {number[]} tokens
* @returns {string}
*/
function decode(tokens) {
return [...decodeGenerator(tokens)].join("");
}
module.exports.encode = encode;
module.exports.decode = decode;
module.exports.encodeGenerator = encodeGenerator;
module.exports.decodeGenerator = decodeGenerator;
module.exports.decodeAsyncGenerator = decodeAsyncGenerator;
module.exports.isWithinTokenLimit = isWithinTokenLimit;