| |
| |
| |
| |
| |
| |
| |
| |
| (function (root) { |
| "use strict"; |
|
|
| |
| |
| function byteTables() { |
| const bs = []; |
| for (let i = 33; i <= 126; i++) bs.push(i); |
| for (let i = 161; i <= 172; i++) bs.push(i); |
| for (let i = 174; i <= 255; i++) bs.push(i); |
| const cs = bs.slice(); |
| let n = 0; |
| for (let b = 0; b < 256; b++) |
| if (!bs.includes(b)) { bs.push(b); cs.push(256 + n); n++; } |
| const b2u = new Map(), u2b = new Map(); |
| for (let i = 0; i < bs.length; i++) { |
| const ch = String.fromCodePoint(cs[i]); |
| b2u.set(bs[i], ch); u2b.set(ch, bs[i]); |
| } |
| return { b2u, u2b }; |
| } |
| const { b2u, u2b } = byteTables(); |
|
|
| |
| |
| const SPLIT = /'s|'t|'re|'ve|'m|'ll|'d| ?[\p{L}]+| ?[\p{N}]+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+/gu; |
|
|
| function build(json) { |
| const model = json.model || {}; |
| if (String(model.type || "").toUpperCase() !== "BPE") |
| throw new Error(`tokenizer type "${model.type}" is not supported — this build implements byte-level BPE ` + |
| `(GPT-2, Llama-style and Qwen2 repos). A different tokenizer would decode to subtly wrong text.`); |
| const vocab = model.vocab || {}; |
| const ids = []; |
| for (const [tokStr, id] of Object.entries(vocab)) ids[id] = tokStr; |
|
|
| |
| const ranks = new Map(); |
| (model.merges || []).forEach((m, i) => { |
| const pair = Array.isArray(m) ? m : String(m).split(" "); |
| if (pair.length === 2) ranks.set(pair[0] + " |