forked from 0hq/WebGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.js
208 lines (183 loc) · 5.43 KB
/
tokenizer.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
class Tokenizer {
constructor() {
this.encoder = undefined;
this.decoder = undefined;
this.vocab_size = undefined;
}
async load() {
throw new Error("Not implemented.");
}
getVocabSize() {
return this.vocab_size;
}
encode(str) {
throw new Error("Not implemented.");
}
decode(arr) {
throw new Error("Not implemented.");
}
}
class SimpleTokenizer extends Tokenizer {
constructor() {
super();
}
async load() {
console.log("Loading simple tokenizer...");
this.encoder = await (await fetch("weights/tokenization/simple_tokens.json")).json();
this.decoder = Object.keys(this.encoder).reduce((acc, x) => ({ ...acc, [this.encoder[x]]: x }), {});
this.vocab_size = Object.keys(this.encoder).length;
}
encode(str) {
return str.split("").map((x) => this.encoder[x]);
}
decode(arr) {
return arr.map((x) => this.decoder[x]).join("");
}
}
// ------------------ GPT Tokenizer ------------------
// Credit to https://github.com/latitudegames/GPT-3-Encoder
class GPT2Tokenizer extends Tokenizer {
constructor() {
super();
this.pat = /'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+/gu;
this.textEncoder = new TextEncoder(); // always utf-8 by spec
this.textDecoder = new TextDecoder("utf-8");
}
async load() {
console.log("Loading GPT2 tokenizer...");
const bpe_file = await (await fetch("weights/tokenization/vocab.bpe")).text();
const encoder = await (await fetch("weights/tokenization/gpt_tokens.json")).json();
this.encoder = encoder;
console.log("Building decoder...");
const decoder = {};
Object.keys(encoder).map((x) => {
decoder[encoder[x]] = x;
});
this.decoder = decoder;
const lines = bpe_file.split("\n");
const bpe_merges = lines.slice(1, lines.length - 1).map((x) => {
return x.split(/(\s+)/).filter(function (e) {
return e.trim().length > 0;
});
});
const byte_encoder = bytes_to_unicode();
const byte_decoder = {};
Object.keys(byte_encoder).map((x) => {
byte_decoder[byte_encoder[x]] = x;
});
this.byte_encoder = byte_encoder;
this.byte_decoder = byte_decoder;
this.bpe_ranks = dictZip(bpe_merges, range(0, bpe_merges.length));
this.cache = new Map();
this.vocab_size = Object.keys(encoder).length;
}
encode(text) {
if (!this.byte_encoder) throw new Error("Tokenizer not loaded.");
let bpe_tokens = [];
const matches = Array.from(text.matchAll(this.pat)).map((x) => x[0]);
for (let token of matches) {
const encoded_bytes = this.textEncoder.encode(token);
let bytes = [];
for (let i = 0; i < encoded_bytes.length; i++) {
bytes.push(this.byte_encoder[encoded_bytes[i].toString()]);
}
token = bytes.join("");
const new_tokens = this.bpe(token)
.split(" ")
.map((x) => this.encoder[x]);
bpe_tokens = bpe_tokens.concat(new_tokens);
}
return bpe_tokens;
}
decode(tokens) {
if (!this.byte_decoder) throw new Error("Tokenizer not loaded.");
let text = tokens.map((x) => this.decoder[x]).join("");
text = this.textDecoder.decode(new Uint8Array(text.split("").map((x) => this.byte_decoder[x])));
return text;
}
bpe(token) {
if (this.cache.has(token)) return this.cache.get(token);
let word = token.split("");
let pairs = get_pairs(word);
if (!pairs) return token;
while (true) {
const minPairs = {};
pairs.forEach(pair => {
const rank = this.bpe_ranks[pair];
minPairs[isNaN(rank) ? 10e10 : rank] = pair;
});
const keys = Object.keys(minPairs).map((x) => parseInt(x));
const bigram = minPairs[Math.min(...keys)];
if (!Object.hasOwn(this.bpe_ranks, bigram)) break;
const first = bigram[0];
const second = bigram[1];
let new_word = [];
let i = 0;
while (i < word.length) {
const j = word.indexOf(first, i);
if (j === -1) {
new_word = new_word.concat(word.slice(i));
break;
}
new_word = new_word.concat(word.slice(i, j));
i = j;
if (word[i] === first && i < word.length - 1 && word[i + 1] === second) {
new_word.push(first + second);
i = i + 2;
} else {
new_word.push(word[i]);
i = i + 1;
}
}
word = new_word;
if (word.length === 1) break;
else pairs = get_pairs(word);
}
word = word.join(" ");
this.cache.set(token, word);
return word;
}
}
const range = (x, y) => {
const res = [];
for (let i = x; i < y; i++) { res.push(i) }
return res;
};
const ord = (x) => {
return x.charCodeAt(0);
};
const dictZip = (x, y) => {
const result = {};
x.map((_, i) => {
result[x[i]] = y[i];
});
return result;
};
const bytes_to_unicode = () => {
const bs = range(ord("!"), ord("~") + 1).concat(range(ord("¡"), ord("¬") + 1), range(ord("®"), ord("ÿ") + 1));
let cs = bs.slice();
let n = 0;
for (let b = 0; b < 2 ** 8; b++) {
if (!bs.includes(b)) {
bs.push(b);
cs.push(2 ** 8 + n);
n = n + 1;
}
}
cs = cs.map((x) => String.fromCharCode(x));
const result = {};
bs.map((_, i) => {
result[bs[i]] = cs[i];
});
return result;
};
const get_pairs = (word) => {
const pairs = new Set();
let prev_char = word[0];
for (let i = 1; i < word.length; i++) {
const char = word[i];
pairs.add([prev_char, char]);
prev_char = char;
}
return pairs;
};