-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer_engine.js
More file actions
353 lines (319 loc) · 12.1 KB
/
Copy pathtokenizer_engine.js
File metadata and controls
353 lines (319 loc) · 12.1 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/**
* LLM Tokenizer — 独立分词引擎
*
* 支持浏览器(全局变量)和 Node.js(require)两种环境。
*
* 实现:GPT-2 风格 Byte-Level BPE + Metaspace BPE
*
* 浏览器用法:
* <script src="tokenizer.js"></script>
* <script>
* const tok = new BPETokenizer(data);
* const result = tok.encode("Hello world!");
* </script>
*
* Node.js 用法:
* const { BPETokenizer } = require('./tokenizer.js');
* const fs = require('fs');
* const data = JSON.parse(fs.readFileSync('./ds-v4/tokenizer.json', 'utf-8'));
* const tok = new BPETokenizer(data);
* console.log(tok.encode("Hello world!"));
*/
// ============ Byte-Level BPE 字节映射表 ============
function buildBytesToUnicode() {
const bs = [];
const cs = [];
// 可打印区间直接映射
for (let i = 33; i <= 126; i++) { bs.push(i); cs.push(i); } // !...~
for (let i = 161; i <= 172; i++) { bs.push(i); cs.push(i); } // ¡...¬
for (let i = 174; i <= 255; i++) { bs.push(i); cs.push(i); } // ®...ÿ
// 不可打印字节映射到 U+0100 往后
let n = 0;
for (let b = 0; b < 256; b++) {
if (!bs.includes(b)) {
bs.push(b);
cs.push(256 + n);
n++;
}
}
const byteToUni = {};
const uniToByte = {};
for (let i = 0; i < 256; i++) {
byteToUni[bs[i]] = String.fromCharCode(cs[i]);
uniToByte[cs[i]] = bs[i];
}
return { byteToUni, uniToByte };
}
const { byteToUni, uniToByte } = buildBytesToUnicode();
// ============ BPE Tokenizer 类 ============
class BPETokenizer {
constructor(data) {
this.vocab = data.model?.vocab || {};
this.merges = data.model?.merges || [];
this.specialTokens = data.added_tokens || [];
this.byteLevel = (data.decoder?.type === 'ByteLevel');
// Metaspace 配置(NLLB-200 等 SentencePiece 风格)
if (data.pre_tokenizer?.type === 'Metaspace') {
this.metaspaceReplacement = data.pre_tokenizer.replacement || '▁';
this.addPrefixSpace = data.pre_tokenizer.add_prefix_space || false;
}
// id → token string
this.idToToken = {};
for (const [token, id] of Object.entries(this.vocab)) {
this.idToToken[id] = token;
}
for (const st of this.specialTokens) {
this.vocab[st.content] = st.id;
this.idToToken[st.id] = st.content;
}
// merge 优先级表: "a b" → rank (越小越优先)
this.mergeRank = {};
for (let i = 0; i < this.merges.length; i++) {
this.mergeRank[this.merges[i]] = i;
}
// 预构建特殊 token 排序列表(最长匹配优先)
this._specialMap = [];
for (const st of this.specialTokens) {
this._specialMap.push({ content: st.content, id: st.id, len: st.content.length });
}
this._specialMap.sort((a, b) => b.len - a.len);
}
// ---- BPE 核心算法(支持字符串或字符数组输入) ----
bpe(symbols) {
if (typeof symbols === 'string') {
symbols = [...symbols];
}
if (symbols.length <= 1) {
const merged = symbols[0];
if (this.vocab[merged] !== undefined) return [merged];
return [];
}
let word = symbols.slice();
while (word.length > 1) {
let minRank = Infinity;
let bestIdx = -1;
for (let i = 0; i < word.length - 1; i++) {
const pair = word[i] + ' ' + word[i + 1];
const rank = this.mergeRank[pair];
if (rank !== undefined && rank < minRank) {
minRank = rank;
bestIdx = i;
}
}
if (bestIdx === -1) break;
word[bestIdx] = word[bestIdx] + word[bestIdx + 1];
word.splice(bestIdx + 1, 1);
}
return word;
}
// ---- Byte-Level: UTF-8 字节 → unicode 字符串 ----
bytesToUnicodeStr(bytes) {
let result = '';
for (const b of bytes) {
result += byteToUni[b];
}
return result;
}
// ---- Byte-Level: 将 unicode token 字符串解码为原始文本 ----
decodeTokenByteLevel(tokenStr) {
const bytes = [];
for (const ch of tokenStr) {
const code = ch.codePointAt(0);
if (uniToByte[code] !== undefined) {
bytes.push(uniToByte[code]);
}
}
if (bytes.length === 0) return tokenStr;
return new TextDecoder('utf-8', { fatal: false }).decode(new Uint8Array(bytes));
}
// ---- 通用: 将 token 字符串解码为可读文本 ----
decodeToken(tokenStr) {
if (this.byteLevel) {
return this.decodeTokenByteLevel(tokenStr);
}
// Metaspace 等非 ByteLevel:▁ → 空格
return tokenStr.replace(/▁/g, ' ');
}
// GPT-2 风格预分词正则(仅 ByteLevel 分词器使用)
static get pat() {
if (!this._pat) {
this._pat = new RegExp(
"'s|'t|'re|'ve|'m|'ll|'d| ?\\p{L}+| ?\\p{N}+| ?[^\\s\\p{L}\\p{N}]+|\\s+(?!\\S)|\\s+",
'gu'
);
}
return this._pat;
}
// 检查并处理特殊 token
_matchSpecial(remaining) {
for (const sp of this._specialMap) {
if (remaining.startsWith(sp.content)) {
return sp;
}
}
return null;
}
// ---- 编码文本片段(不含特殊 token),用于 ByteLevel ----
_encodeSegmentByteLevel(segment, tokens, ids) {
let remaining = segment;
while (remaining.length > 0) {
BPETokenizer.pat.lastIndex = 0;
const match = BPETokenizer.pat.exec(remaining);
if (match) {
const word = match[0];
const wordBytes = new TextEncoder().encode(word);
const wordUnicode = this.bytesToUnicodeStr(wordBytes);
const bpeTokens = this.bpe(wordUnicode);
for (const bt of bpeTokens) {
const id = this.vocab[bt];
tokens.push(bt);
ids.push(id !== undefined ? id : 0);
}
remaining = remaining.substring(word.length);
} else {
remaining = remaining.substring(1);
}
}
}
// ---- Byte-Level BPE 编码 ----
encodeByteLevel(text) {
const tokens = [];
const ids = [];
let remaining = text;
while (remaining.length > 0) {
// 优先匹配开头的特殊 token
const matchedSpecial = this._matchSpecial(remaining);
if (matchedSpecial) {
tokens.push(matchedSpecial.content);
ids.push(matchedSpecial.id);
remaining = remaining.substring(matchedSpecial.len);
continue;
}
// 检查即将到来的文本中是否包含特殊 token(最长匹配优先)
let nextSpecialPos = remaining.length;
let nextSpecial = null;
for (const sp of this._specialMap) {
const pos = remaining.indexOf(sp.content);
if (pos !== -1 && pos < nextSpecialPos) {
nextSpecialPos = pos;
nextSpecial = sp;
}
}
// 如果下一个特殊 token 前面还有文本,先处理前面的文本
if (nextSpecial && nextSpecialPos > 0) {
const segment = remaining.substring(0, nextSpecialPos);
this._encodeSegmentByteLevel(segment, tokens, ids);
remaining = remaining.substring(nextSpecialPos);
continue;
}
// 没有特殊 token,直接编码剩余文本
this._encodeSegmentByteLevel(remaining, tokens, ids);
break;
}
return { tokens, ids };
}
// ---- Metaspace BPE 编码(NLLB-200 等 SentencePiece 风格) ----
encodeMetaspace(text) {
const tokens = [];
const ids = [];
// 1. 空格 → ▁
let processed = text;
if (this.addPrefixSpace) {
processed = '▁' + processed;
}
processed = processed.replace(/ /g, this.metaspaceReplacement || '▁');
// 2. 按 ▁ 边界分片,每片分别做 BPE
let remaining = processed;
while (remaining.length > 0) {
const matchedSpecial = this._matchSpecial(remaining);
if (matchedSpecial) {
tokens.push(matchedSpecial.content);
ids.push(matchedSpecial.id);
remaining = remaining.substring(matchedSpecial.len);
continue;
}
// 检查即将到来的文本中是否包含特殊 token(最长匹配优先)
let nextSpecialPos = remaining.length;
let nextSpecial = null;
for (const sp of this._specialMap) {
const pos = remaining.indexOf(sp.content);
if (pos !== -1 && pos < nextSpecialPos) {
nextSpecialPos = pos;
nextSpecial = sp;
}
}
// 如果下一个特殊 token 前面还有文本,先处理前面的文本
if (nextSpecial && nextSpecialPos > 0) {
let segment = remaining.substring(0, nextSpecialPos);
// 处理 segment 中的 Metaspace 边界
while (segment.length > 0) {
const nextMeta = segment.indexOf('▁', 1);
let piece;
if (nextMeta === -1) {
piece = segment;
segment = '';
} else {
piece = segment.substring(0, nextMeta);
segment = segment.substring(nextMeta);
}
if (piece.length === 0) {
segment = segment.substring(1);
continue;
}
const bpeTokens = this.bpe([...piece]);
for (const bt of bpeTokens) {
const id = this.vocab[bt];
tokens.push(bt);
ids.push(id !== undefined ? id : 0);
}
}
remaining = remaining.substring(nextSpecialPos);
continue;
}
// 找到下一个 ▁ 边界(跳过位置 0 的 ▁)
const nextMeta = remaining.indexOf('▁', 1);
let piece;
if (nextMeta === -1) {
piece = remaining;
remaining = '';
} else {
piece = remaining.substring(0, nextMeta);
remaining = remaining.substring(nextMeta);
}
if (piece.length === 0) {
remaining = remaining.substring(1);
continue;
}
const bpeTokens = this.bpe([...piece]);
for (const bt of bpeTokens) {
const id = this.vocab[bt];
tokens.push(bt);
ids.push(id !== undefined ? id : 0);
}
}
return { tokens, ids };
}
// ---- 统一入口 ----
encode(text) {
if (this.byteLevel) {
return this.encodeByteLevel(text);
}
return this.encodeMetaspace(text);
}
// ---- 解码:token IDs → 文本 ----
decode(ids) {
const tokens = [];
for (const id of ids) {
const token = this.idToToken[id];
if (token !== undefined) {
tokens.push(this.decodeToken(token));
}
}
return tokens.join('');
}
}
// ============ 环境适配:浏览器全局 + Node.js module.exports ============
if (typeof module !== 'undefined' && module.exports) {
module.exports = { BPETokenizer, buildBytesToUnicode, byteToUni, uniToByte };
}
// 浏览器环境:BPETokenizer / byteToUni / uniToByte 已是全局变量(var/const 在顶层 script 中)