Spaces:
Running

File size: 4,006 Bytes
3c5aec5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Byte-level BPE tokenizer: a JavaScript port of tokenizer/tokenizer.py (encode and decode)
// plus the chat template of tokenizer/chat.py. Loaded by the in-browser engine (engine.js).
// tests/test_web_tokenizer.py checks that it gives exactly the same ids as the Python one.
"use strict";

// tokenizer.py's GPT-4 split pattern, written for JavaScript's regex engine:
//  - JavaScript has no possessive quantifiers (?+ ++). Here they never change a match: after
//    "[^\r\n\p{L}\p{N}]?" comes "\p{L}+", which can't start at a non-letter anyway, and after
//    "[^\s\p{L}\p{N}]+" comes "[\r\n]*", which can't fail. So plain ? and + give the same chunks.
//  - "(?i:...)" becomes the flag i on the whole pattern: the only other letters in the pattern
//    are inside \p{...}, which case-insensitivity doesn't change.
const PYTHON_PATTERN = String.raw`'(?i:[sdmt]|ll|ve|re)|[^\r\n\p{L}\p{N}]?+\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]++[\r\n]*|\s*[\r\n]|\s+(?!\S)|\s+`;
const JS_PATTERN = /'(?:[sdmt]|ll|ve|re)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]|\s+(?!\S)|\s+/giu;

class Tokenizer {
  constructor(data) {
    if (data.pattern !== PYTHON_PATTERN) throw new Error("tokenizer uses a split pattern this port doesn't know");
    this.ranks = new Map();                 // pair (a, b) -> the id it merges into
    this.vocab = [];                        // id -> bytes
    for (let i = 0; i < 256; i++) this.vocab.push(Uint8Array.of(i));
    data.merges.forEach(([a, b], i) => {
      this.ranks.set(a * 65536 + b, 256 + i);
      const bytes = new Uint8Array(this.vocab[a].length + this.vocab[b].length);
      bytes.set(this.vocab[a]);
      bytes.set(this.vocab[b], this.vocab[a].length);
      this.vocab.push(bytes);
    });
    this.special = {};
    data.special_tokens.forEach((s, i) => {
      this.special[s] = 256 + data.merges.length + i;
      this.vocab.push(new TextEncoder().encode(s));
    });
    this.specialIds = new Set(Object.values(this.special));
    this.eot = this.special["<|endoftext|>"];
    this.cache = new Map();
  }

  static async load(url) {
    const res = await fetch(url);
    if (!res.ok) throw new Error(`couldn't load the tokenizer (HTTP ${res.status})`);
    return new Tokenizer(await res.json());
  }

  // One regex chunk -> ids, replaying merges in the order they were learned (lowest id first),
  // exactly like Tokenizer._encode_chunk.
  encodeChunk(chunk) {
    const cached = this.cache.get(chunk);
    if (cached) return cached;
    let ids = Array.from(new TextEncoder().encode(chunk));
    while (ids.length >= 2) {
      let best = Infinity, bestKey = -1;
      for (let i = 0; i < ids.length - 1; i++) {
        const rank = this.ranks.get(ids[i] * 65536 + ids[i + 1]);
        if (rank !== undefined && rank < best) { best = rank; bestKey = ids[i] * 65536 + ids[i + 1]; }
      }
      if (bestKey < 0) break;                 // no learned merge applies any more
      const a = Math.floor(bestKey / 65536), b = bestKey % 65536, out = [];
      for (let i = 0; i < ids.length; i++) {
        if (i < ids.length - 1 && ids[i] === a && ids[i + 1] === b) { out.push(best); i++; }
        else out.push(ids[i]);
      }
      ids = out;
    }
    if (this.cache.size < 50000) this.cache.set(chunk, ids);
    return ids;
  }

  // Special-token strings typed by a user stay plain text (as in encode_ordinary)
  encodeOrdinary(text) {
    const ids = [];
    for (const m of text.matchAll(JS_PATTERN)) ids.push(...this.encodeChunk(m[0]));
    return ids;
  }

  // tokenizer/chat.py prompt_ids: <|endoftext|> <|user|> instruction <|assistant|>
  promptIds(instruction) {
    return [this.eot, this.special["<|user|>"], ...this.encodeOrdinary(instruction.trim()), this.special["<|assistant|>"]];
  }

  // Generator.encode_prompt(chat=False): a story continues after <|endoftext|>
  completionIds(text) {
    return [this.eot, ...this.encodeOrdinary(text)];
  }
}

if (typeof self !== "undefined") self.Tokenizer = Tokenizer;