split.bend checks
raw source on the hub · import bend-bpe-tokenizer@0.1.0.0/split.bend as Split
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.
1 import
import Base
Types
type CC source · line 14 · raw
Data
CC@c:U32 -> @k:U32 -> CC
Definitions
def cls source · line 17 · raw
@x:CC -> U32
def cp source · line 22 · raw
@x:CC -> U32
def is_word source · line 27 · raw
@+fam:U32 -> @+k:U32 -> Bool
def is_other source · line 30 · raw
@+fam:U32 -> @+k:U32 -> Bool
def is_num source · line 33 · raw
@+k:U32 -> Bool
def is_ws source · line 36 · raw
@+k:U32 -> Bool
def is_nl source · line 39 · raw
@+k:U32 -> Bool
def in_run source · line 43 · raw
@+p:U32 -> @+fam:U32 -> @+k:U32 -> Bool
which class a run is made of: 0 words, 1 numbers, 2 punctuation and symbols, 3 whitespace, 4 \r \n
def nat_if source · line 49 · raw
@b:Bool -> @more:(@_:Unit -> Nat) -> Nat
def run source · line 57 · raw
@xs:List<&2, CC> -> @+p:U32 -> @+fam:U32 -> Nat
how many leading elements of xs are in run p
def run_cap source · line 65 · raw
@cap:Nat -> @xs:List<&2, CC> -> @+p:U32 -> @+fam:U32 -> Nat
the same, at most cap
def head_in source · line 74 · raw
@xs:List<&2, CC> -> @+p:U32 -> @+fam:U32 -> Bool
def head_cp source · line 81 · raw
@xs:List<&2, CC> -> U32
def second_cp source · line 88 · raw
@xs:List<&2, CC> -> U32
def fold source · line 96 · raw
@+fam:U32 -> @+c:U32 -> U32
---- contractions: 's 't 're 've 'm 'll 'd, ASCII case-insensitive (and the long s U+017F as s) but in GPT-2 ----
def contraction2 source · line 101 · raw
@+a:U32 -> @+b:U32 -> Nat
def contraction source · line 106 · raw
@+fam:U32 -> @+a:U32 -> @+b:U32 -> Nat
the length of the contraction after an apostrophe, 0 for none
def last_nl source · line 112 · raw
@w:Nat -> @xs:List<&2, CC> -> @+i:Nat -> @+best:Nat -> Nat
---- 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 ws_tail source · line 122 · raw
@+w:Nat -> @at_end:Bool -> Nat
a run of whitespace: x and the w after it; at_end when nothing follows the run
def ws_more3 source · line 129 · raw
@nl:Bool -> @+n:Nat -> @+w:Nat -> @+t:List<&2, CC> -> Nat
def ws_more2 source · line 136 · raw
@+fam:U32 -> @+x:CC -> @+w:Nat -> @+t:List<&2, CC> -> Nat
def ws_more source · line 139 · raw
@+fam:U32 -> @+x:CC -> @+t:List<&2, CC> -> Nat
def alt source · line 143 · raw
@c:Bool -> @yes:(@_:Unit -> Nat) -> @no:(@_:Unit -> Nat) -> Nat
---- the rules, alternative by alternative: how many elements after x the piece takes ----
def other_more2 source · line 151 · raw
@+n:Nat -> @+fam:U32 -> @t:List<&2, CC> -> Nat
punctuation and symbols, then any \r \n right after them
def other_more source · line 154 · raw
@+fam:U32 -> @+t:List<&2, CC> -> Nat
def gpt2_try source · line 158 · raw
@+p:U32 -> @+x:CC -> @+t:List<&2, CC> -> @next:(@_:Unit -> Nat) -> Nat
GPT-2: ?\p{L}+, ?\p{N}+, ?[^\s\p{L}\p{N}]+, then whitespace
def gpt2_more source · line 161 · raw
@+x:CC -> @+t:List<&2, CC> -> Nat
def num_cap source · line 165 · raw
@+fam:U32 -> Nat
the numbers a piece may hold after its first: Qwen 0, Llama 3 two more
def rules_more source · line 170 · raw
@+fam:U32 -> @+x:CC -> @+t:List<&2, CC> -> Nat
the other families: a letter run with one optional leading non-letter, numbers, punctuation (with one optional leading space), then whitespace
def more2 source · line 179 · raw
@+n:Nat -> @+fam:U32 -> @+x:CC -> @+t:List<&2, CC> -> Nat
how many elements after x its piece takes
def more source · line 183 · raw
@+fam:U32 -> @+x:CC -> @+t:List<&2, CC> -> Nat
def split_go source · line 188 · raw
@fuel:Nat -> @+fam:U32 -> @xs:List<&2, CC> -> List<&2, List<&2, CC>>
---- the split ----
fuel bounds the pieces; once it runs out the rest is one piece (never, from split)
def split source · line 197 · raw
@+fam:U32 -> @+xs:List<&2, CC> -> List<&2, List<&2, CC>>