File size: 3,642 Bytes
8246b15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Minimal re-implementation of the Falcon-H1 tokenizer.json pipeline (byte-level BPE).
export function makeTokenizer(tj) {
  const vocab = new Map(Object.entries(tj.model.vocab));
  const ranks = new Map(); tj.model.merges.forEach((m, i) => ranks.set(Array.isArray(m) ? m.join(' ') : m, i));
  const added = tj.added_tokens.slice().sort((a, b) => b.content.length - a.content.length);
  const addedMap = new Map(added.map(a => [a.content, a.id]));
  const idToTok = new Map(); for (const [t, i] of vocab) idToTok.set(i, t); for (const a of added) idToTok.set(a.id, a.content);
  const splitRe = new RegExp(tj.pre_tokenizer.pretokenizers[1].pattern.Regex, 'gu');
  const punct = /^(?:[!-\/:-@\[-`{-~]|\p{P})$/u;
  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++); }
  const b2u = new Map(bs.map((b, i) => [b, String.fromCodePoint(cs[i])])), u2b = new Map(bs.map((b, i) => [String.fromCodePoint(cs[i]), b]));
  const enc = new TextEncoder(), dec = new TextDecoder(), cache = new Map();
  function bpe(piece) {
    if (cache.has(piece)) return cache.get(piece);
    let w = Array.from(piece);
    while (w.length > 1) {
      let best = -1, bi = -1;
      for (let i = 0; i < w.length - 1; i++) { const r = ranks.get(w[i] + ' ' + w[i + 1]); if (r !== undefined && (best < 0 || r < best)) { best = r; bi = i; } }
      if (bi < 0) break; w.splice(bi, 2, w[bi] + w[bi + 1]);
    }
    const ids = w.map(t => vocab.get(t)); cache.set(piece, ids); return ids;
  }
  function punctSplit(s) {
    const out = []; let cur = '', mode = null;
    for (const ch of s) { const p = punct.test(ch); if (mode === null || p === mode) cur += ch; else { out.push(cur); cur = ch; } mode = p; }
    if (cur) out.push(cur); return out;
  }
  function isolate(s) {
    const out = []; let last = 0; splitRe.lastIndex = 0; let m;
    while ((m = splitRe.exec(s)) !== null) { if (m[0].length === 0) { splitRe.lastIndex++; continue; } if (m.index > last) out.push(s.slice(last, m.index)); out.push(m[0]); last = m.index + m[0].length; }
    if (last < s.length) out.push(s.slice(last)); return out;
  }
  function encodeChunk(s) {
    const ids = [];
    for (const a of punctSplit(s)) for (const b of isolate(a)) {
      const mapped = Array.from(enc.encode(b)).map(x => b2u.get(x)).join('');
      for (const part of mapped.split(/([0-9])/).filter(x => x)) ids.push(...bpe(part));
    }
    return ids;
  }
  function encode(text) {
    const ids = []; let i = 0, buf = '';
    while (i < text.length) {
      let hit = null; for (const a of added) if (text.startsWith(a.content, i)) { hit = a; break; }
      if (hit) { if (buf) { ids.push(...encodeChunk(buf)); buf = ''; } ids.push(hit.id); i += hit.content.length; } else { buf += text[i++]; }
    }
    if (buf) ids.push(...encodeChunk(buf)); return ids;
  }
  function decode(ids, skipSpecial = true) {
    const sp = new Set(added.filter(a => a.special).map(a => a.id)); const bytes = []; let s = '';
    const flush = () => { if (bytes.length) { s += dec.decode(new Uint8Array(bytes)); bytes.length = 0; } };
    for (const id of ids) { if (sp.has(id)) { flush(); if (!skipSpecial) s += idToTok.get(id); continue; } for (const ch of idToTok.get(id) ?? '') { const b = u2b.get(ch); if (b !== undefined) bytes.push(b); else { flush(); s += ch; } } }
    flush(); return s;
  }
  return { encode, decode, eos: 11, imEnd: addedMap.get('<|im_end|>'), imStart: addedMap.get('<|im_start|>') };
}