# The pre-tokenizer: splits text into the pieces the split regex of a byte-level BPE family matches, as Hugging Face's # `tokenizers` does (leftmost alternative first, greedy, with the backtracking each pattern implies). The text comes # classified (tok.bend looks each code point up in tables.bend), so this file needs no table: # class 0 other, 1 letter (\p{L}), 2 mark (\p{M}), 3 number (\p{N}), 4 whitespace other than \r \n, 5 \r or \n. # Families: # 0 Qwen2/Qwen3: (?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+ # 1 Qwen3.5+: the same with [\p{L}\p{M}]+ for words, and marks excluded from the punctuation class # 2 Llama 3: Qwen2's with \p{N}{1,3} # 3 GPT-2: 's|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+ # Each piece is a code point and the n after it, n from the rules below; `split` cuts the text with take and drop, # so (LAWS.bend) the pieces always concatenate back to the text, whatever the rules decide. import Base type CC is Data: CC{c: U32, k: U32} def cls(x: CC) -> U32: match x: case CC{c, k}: k def cp(x: CC) -> U32: match x: case CC{c, k}: c def is_word(+fam: U32, +k: U32) -> Bool: Bool.or(U32.is_eq(k, 1), Bool.and(U32.is_eq(fam, 1), U32.is_eq(k, 2))) def is_other(+fam: U32, +k: U32) -> Bool: Bool.or(U32.is_eq(k, 0), Bool.and(U32.is_eq(k, 2), U32.is_ne(fam, 1))) def is_num(+k: U32) -> Bool: U32.is_eq(k, 3) def is_ws(+k: U32) -> Bool: Bool.or(U32.is_eq(k, 4), U32.is_eq(k, 5)) def is_nl(+k: U32) -> Bool: U32.is_eq(k, 5) # which class a run is made of: 0 words, 1 numbers, 2 punctuation and symbols, 3 whitespace, 4 \r \n def in_run(+p: U32, +fam: U32, +k: U32) -> Bool: Bool.pick(Bool, U32.is_eq(p, 0), is_word(fam, k), Bool.pick(Bool, U32.is_eq(p, 1), is_num(k), Bool.pick(Bool, U32.is_eq(p, 2), is_other(fam, k), Bool.pick(Bool, U32.is_eq(p, 3), is_ws(k), is_nl(k))))) def nat_if(b: Bool, more: Unit -> Nat) -> Nat: match b: case True{}: more(Unit{}) case False{}: 0n # how many leading elements of xs are in run p def run(xs: List<&2, CC>, +p: U32, +fam: U32) -> Nat: match xs: case []: 0n case x <> t: nat_if(in_run(p, fam, cls(x)), u => 1n+run(t, p, fam)) # the same, at most cap def run_cap(cap: Nat, xs: List<&2, CC>, +p: U32, +fam: U32) -> Nat: match cap xs: case 0n _: 0n case _ Nil{}: 0n case 1n+c x <> t: nat_if(in_run(p, fam, cls(x)), u => 1n+run_cap(c, t, p, fam)) def head_in(xs: List<&2, CC>, +p: U32, +fam: U32) -> Bool: match xs: case []: False{} case x <> t: in_run(p, fam, cls(x)) def head_cp(xs: List<&2, CC>) -> U32: match xs: case []: 0 case x <> t: cp(x) def second_cp(xs: List<&2, CC>) -> U32: match xs: case []: 0 case x <> t: head_cp(t) # ---- contractions: 's 't 're 've 'm 'll 'd, ASCII case-insensitive (and the long s U+017F as s) but in GPT-2 ---- def fold(+fam: U32, +c: U32) -> U32: Bool.pick(U32, U32.is_eq(fam, 3), c, Bool.pick(U32, Bool.and(U32.is_ge(c, 65), U32.is_le(c, 90)), (c + 32 : U32), Bool.pick(U32, U32.is_eq(c, 383), 115, c))) def contraction2(+a: U32, +b: U32) -> Nat: Bool.pick(Nat, Bool.or(Bool.and(U32.is_eq(a, 114), U32.is_eq(b, 101)), Bool.or(Bool.and(U32.is_eq(a, 118), U32.is_eq(b, 101)), Bool.and(U32.is_eq(a, 108), U32.is_eq(b, 108)))), 2n, 0n) # the length of the contraction after an apostrophe, 0 for none def contraction(+fam: U32, +a: U32, +b: U32) -> Nat: Bool.pick(Nat, Bool.or(Bool.or(U32.is_eq(a, 115), U32.is_eq(a, 116)), Bool.or(U32.is_eq(a, 109), U32.is_eq(a, 100))), 1n, contraction2(a, b)) # ---- whitespace: \s*[\r\n]+ (not GPT-2), then \s+(?!\S), then \s+ ---- # the index, among the w elements of xs, just past the last \r or \n (0 for none) def last_nl(w: Nat, xs: List<&2, CC>, +i: Nat, +best: Nat) -> Nat: match w xs: case 0n _: best case _ Nil{}: best case 1n+v x <> t: last_nl(v, t, 1n+i, Bool.pick(Nat, is_nl(cls(x)), 1n+i, best)) # a run of whitespace: x and the w after it; at_end when nothing follows the run def ws_tail(+w: Nat, at_end: Bool) -> Nat: match at_end: case True{}: w case False{}: Nat.sub(w, 1n) def ws_more3(nl: Bool, +n: Nat, +w: Nat, +t: List<&2, CC>) -> Nat: match nl: case True{}: Nat.sub(n, 1n) case False{}: ws_tail(w, Nat.is_eq(w, List.length(&2, CC, t))) def ws_more2(+fam: U32, +x: CC, +w: Nat, +t: List<&2, CC>) -> Nat: ws_more3(Bool.and(U32.is_ne(fam, 3), Nat.is_gt(last_nl(1n+w, x <> t, 0n, 0n), 0n)), last_nl(1n+w, x <> t, 0n, 0n), w, t) def ws_more(+fam: U32, +x: CC, +t: List<&2, CC>) -> Nat: ws_more2(fam, x, run(t, 3, fam), t) # ---- the rules, alternative by alternative: how many elements after x the piece takes ---- def alt(c: Bool, yes: Unit -> Nat, no: Unit -> Nat) -> Nat: match c: case True{}: yes(Unit{}) case False{}: no(Unit{}) # punctuation and symbols, then any \r \n right after them def other_more2(+n: Nat, +fam: U32, t: List<&2, CC>) -> Nat: Nat.add(n, run(List.drop(&2, CC, t, n), 4, fam)) def other_more(+fam: U32, +t: List<&2, CC>) -> Nat: other_more2(run(t, 2, fam), fam, t) # GPT-2: ` ?\p{L}+`, ` ?\p{N}+`, ` ?[^\s\p{L}\p{N}]+`, then whitespace def gpt2_try(+p: U32, +x: CC, +t: List<&2, CC>, next: Unit -> Nat) -> Nat: alt(Bool.or(Bool.and(U32.is_eq(cp(x), 32), head_in(t, p, 3)), in_run(p, 3, cls(x))), u => run(t, p, 3), next) def gpt2_more(+x: CC, +t: List<&2, CC>) -> Nat: gpt2_try(0, x, t, u => gpt2_try(1, x, t, u => gpt2_try(2, x, t, u => ws_more(3, x, t)))) # the numbers a piece may hold after its first: Qwen 0, Llama 3 two more def num_cap(+fam: U32) -> Nat: Bool.pick(Nat, U32.is_eq(fam, 2), 2n, 0n) # the other families: a letter run with one optional leading non-letter, numbers, punctuation (with one optional # leading space), then whitespace def rules_more(+fam: U32, +x: CC, +t: List<&2, CC>) -> Nat: alt(Bool.and(Bool.not(Bool.or(is_nl(cls(x)), Bool.or(U32.is_eq(cls(x), 1), is_num(cls(x))))), head_in(t, 0, fam)), u => run(t, 0, fam), u => alt(is_word(fam, cls(x)), u => run(t, 0, fam), u => alt(is_num(cls(x)), u => run_cap(num_cap(fam), t, 1, fam), u => alt(Bool.and(U32.is_eq(cp(x), 32), head_in(t, 2, fam)), u => other_more(fam, t), u => alt(is_other(fam, cls(x)), u => other_more(fam, t), u => ws_more(fam, x, t)))))) # how many elements after x its piece takes def more2(+n: Nat, +fam: U32, +x: CC, +t: List<&2, CC>) -> Nat: alt(Bool.and(U32.is_eq(cp(x), 39), Nat.is_gt(n, 0n)), u => n, u => alt(U32.is_eq(fam, 3), u => gpt2_more(x, t), u => rules_more(fam, x, t))) def more(+fam: U32, +x: CC, +t: List<&2, CC>) -> Nat: more2(contraction(fam, fold(fam, head_cp(t)), fold(fam, second_cp(t))), fam, x, t) # ---- the split ---- # fuel bounds the pieces; once it runs out the rest is one piece (never, from `split`) def split_go(fuel: Nat, +fam: U32, xs: List<&2, CC>) -> List<&2, List<&2, CC>>: match fuel xs: case _ Nil{}: [] case 0n x <> t: [x <> t] case 1n+f +x <> +t: (x <> List.take(&2, CC, t, more(fam, x, t))) <> split_go(f, fam, List.drop(&2, CC, t, more(fam, x, t))) def split(+fam: U32, +xs: List<&2, CC>) -> List<&2, List<&2, CC>>: split_go(List.length(&2, CC, xs), fam, xs)