~/bend-docscommunity

tok.bend checks

raw source on the hub · import bend-ml-llama@0.3.0.0/tok.bend as Tok

bend-ml-llama, the tokenizer: SmolLM2's, in Bend. A pre-tokenizer (SmolLM2's Digits split, then the GPT-2 regex, over bytes) + the 48,900 merge rules, applied with the bend-ml-bpe-tokenizer package (proved roundtrip); decoding; and SmolLM2-Instruct's chat template.

import ./tok.bend as K tb : K.Tables <- K.load(dir) # merges_num.txt and perm.txt of a model directory (reference/prep.py) K.encode(tb, "Once upon a time") # the ids of a text K.decode(tb, ids) # the text of ids, special tokens left out K.chat(tb, ["user", "Hi there!"], True{}) # the ids of a conversation in the chat template, the answer asked for

Adapted from bend-ml's demos/gpt2/tok.bend (MIT). What changes for SmolLM2: every \p{N} character is its own piece; the 21 bytes without a token are dropped before the merges; merge k is id 252 + k; 0x1C-0x1F are not whitespace. llama.bend's reference/test_tok.py, test_detok.py and test_chat.py check it against tokenizers and transformers, through demo/tok.bend.

The pre-tokenizer decodes UTF-8 and classifies every code point like the regex does (\s, \p{L}, \p{N}); the classes of code points >= 128 come from unicode.bend. Known limits: code points above U+1FFFF count as letters, and so do CJK ideographs and Hangul (they are letters in Unicode, so this is exact); invalid UTF-8 bytes count as letters; a special token written as text is not recognized.

3 imports
import Base
import bend-ml-bpe-tokenizer@0.1.3.0/main.bend as BPE
import ./unicode.bend as U

Types

type Next source · line 564 · raw

Data

the next character and the bytes after it

type Tables source · line 690 · raw

Data

the merge rules and the byte permutation of a model, built once for encoding and decoding

Definitions

def rb_done source · line 27 · raw

@m:Pair(File, Result<&1, &1, Pair(U32, String), List<&2, U32>>) -> IO(List<&2, U32>)

def read_bytes source · line 34 · raw

@path:String -> IO(List<&2, U32>)

def id_tok2 source · line 40 · raw

@small:Bool -> @+n:Nat -> 0x559a0abb492a951db15be435191b6d0a/main.Tk

def id_tok source · line 49 · raw

@+n:Nat -> 0x559a0abb492a951db15be435191b6d0a/main.Tk

def digit2 source · line 54 · raw

@lo:Bool -> @hi:Bool -> @+code:U32 -> Maybe<&2, Nat>

def digit source · line 61 · raw

@+code:U32 -> Maybe<&2, Nat>

def head_digit source · line 64 · raw

@xs:List<&2, U32> -> Maybe<&2, Nat>

def flush source · line 71 · raw

@have:Bool -> @+cur:Nat -> @acc:List<&2, Nat> -> List<&2, Nat>

def nums.go source · line 78 · raw

@xs:List<&2, U32> -> @d:Maybe<&2, Nat> -> @+cur:Nat -> @have:Bool -> @acc:List<&2, Nat> -> List<&2, Nat>

def nums source · line 87 · raw

@+xs:List<&2, U32> -> List<&2, Nat>

def rules.go source · line 92 · raw

@ns:List<&2, Nat> -> @+k:Nat -> List<&2, 0x559a0abb492a951db15be435191b6d0a/main.Rule>

def ids_go source · line 111 · raw

@ns:List<&2, Nat> -> @+k:Nat -> @ok:Bool -> Bool

---- runtime check of the merges table read from a file ---- rules.go gives the rules the sequential ids 0, 1, 2..., so BPE.wf (distinct ids) holds by construction; what a malformed file can still break is the shape of the table. It must have an even number of ids, and rule k may only refer to ids that exist before it (bytes < 256, or merges 256..256+k-1). The check is linear in the table size. ok: whether every rule before k was well formed. The check of rule k is passed to the next call, which matches it before going on, so the walk stops at the first bad rule (a helper that branched first would have to call ids_go back, and Bend has no mutual recursion).

def ids_ok source · line 126 · raw

@ns:List<&2, Nat> -> @+k:Nat -> Bool

def toks_of_ids source · line 129 · raw

@ns:List<&2, Nat> -> List<&2, 0x559a0abb492a951db15be435191b6d0a/main.Tk>

def nth source · line 139 · raw

@xs:List<&2, Nat> -> @n:Nat -> Nat

def gid source · line 151 · raw

@+perm:List<&2, Nat> -> @+t:0x559a0abb492a951db15be435191b6d0a/main.Tk -> Nat

def gids source · line 158 · raw

@+perm:List<&2, Nat> -> @xs:List<&2, 0x559a0abb492a951db15be435191b6d0a/main.Tk> -> List<&2, Nat>

def sel source · line 172 · raw

@b:Bool -> @x:Nat -> @y:Nat -> Nat

def in_range source · line 179 · raw

@+c:U32 -> @+lo:U32 -> @+hi:U32 -> Bool

def class_ascii source · line 184 · raw

@+c:U32 -> Nat

class of an ASCII byte. Whitespace is the space and 9-13 (\t \n \v \f \r): tokenizers does not count 0x1C-0x1F as \s (Unicode White_Space does not include them), so they are "other", unlike bend-ml's GPT-2 copy

def after_h source · line 188 · raw

@+cp:U32 -> @t:List<&2, U32> -> Bool

class of a code point >= 128: the first range of the sorted table that does not end before it

def inside_h source · line 195 · raw

@+cp:U32 -> @t:List<&2, U32> -> Bool

def cls_go source · line 202 · raw

@+cp:U32 -> @tbl:List<&2, U32> -> @aft:Bool -> @ins:Bool -> Nat

def cls_cp source · line 211 · raw

@+tbl:List<&2, U32> -> @+cp:U32 -> Nat

def class_cp source · line 214 · raw

@+tbl:List<&2, U32> -> @+cp:U32 -> Nat

def clen source · line 218 · raw

@+c:U32 -> Nat

length of the UTF-8 character that starts with byte c (1 for stray or invalid bytes)

def byte_at source · line 221 · raw

@xs:List<&2, U32> -> @i:Nat -> U32

def cont_bits source · line 230 · raw

@+x:U32 -> U32

def cp_of source · line 234 · raw

@+n:Nat -> @+xs:List<&2, U32> -> U32

the code point of the character of n bytes at the start of xs

def tagged source · line 245 · raw

@+b:U32 -> @+cls:Nat -> @+cont:Bool -> U32

def emit source · line 249 · raw

@n:Nat -> @+cls:Nat -> @+cont:Bool -> @xs:List<&2, U32> -> @rest:List<&2, U32> -> List<&2, U32>

tags the first n bytes of xs (the 1st is not a continuation), then continues with rest

def n_of source · line 256 · raw

@+xs:List<&2, U32> -> Nat

def cls_head source · line 259 · raw

@+tbl:List<&2, U32> -> @+xs:List<&2, U32> -> Nat

def tag source · line 263 · raw

@fuel:Nat -> @+tbl:List<&2, U32> -> @+xs:List<&2, U32> -> @+n:Nat -> @+cls:Nat -> List<&2, U32>

fuel >= number of bytes left; n and cls describe the character at the start of xs

def tag_all source · line 270 · raw

@+tbl:List<&2, U32> -> @+xs:List<&2, U32> -> List<&2, U32>

def class_of source · line 273 · raw

@+c:U32 -> Nat

def head_class source · line 276 · raw

@xs:List<&2, U32> -> Nat

def head_u32 source · line 283 · raw

@xs:List<&2, U32> -> U32

def tail_l source · line 290 · raw

@xs:List<&2, U32> -> List<&2, U32>

def contr2 source · line 299 · raw

@+q:Bool -> @+b:U32 -> @+c:U32 -> Nat

def contr_len source · line 302 · raw

@+xs:List<&2, U32> -> Nat

def span source · line 307 · raw

@xs:List<&2, U32> -> @ok:Bool -> @+cls:Nat -> @acc:List<&2, U32> -> Pair(List<&2, U32>, List<&2, U32>)

def span_cls source · line 314 · raw

@+xs:List<&2, U32> -> @+cls:Nat -> Pair(List<&2, U32>, List<&2, U32>)

def prepend source · line 317 · raw

@c:U32 -> @p:Pair(List<&2, U32>, List<&2, U32>) -> Pair(List<&2, U32>, List<&2, U32>)

def lcl source · line 325 · raw

@xs:List<&2, U32> -> @cur:Nat -> Nat

number of bytes of the last character of xs (1 + trailing continuation bytes)

def ws4 source · line 332 · raw

@long:Bool -> @+run:List<&2, U32> -> @+rest:List<&2, U32> -> @+n:Nat -> @+m:Nat -> Pair(List<&2, U32>, List<&2, U32>)

def ws3 source · line 339 · raw

@+run:List<&2, U32> -> @+rest:List<&2, U32> -> @ends:Bool -> @+n:Nat -> Pair(List<&2, U32>, List<&2, U32>)

def ws2 source · line 349 · raw

@p:Pair(List<&2, U32>, List<&2, U32>) -> Pair(List<&2, U32>, List<&2, U32>)

a run that ends the text, or ends right before a digit, is taken whole: SmolLM2 splits every digit off first, so the regex sees the run at the end of its segment and \s+(?!\S) matches all of it

def ws source · line 354 · raw

@xs:List<&2, U32> -> Pair(List<&2, U32>, List<&2, U32>)

def is_cont source · line 358 · raw

@xs:List<&2, U32> -> Bool

a digit (class 1) is one character: the byte and its continuation bytes (tag bit 2048)

def span_cont source · line 365 · raw

@xs:List<&2, U32> -> @ok:Bool -> @acc:List<&2, U32> -> Pair(List<&2, U32>, List<&2, U32>)

def one_char source · line 372 · raw

@xs:List<&2, U32> -> Pair(List<&2, U32>, List<&2, U32>)

def kind_of source · line 383 · raw

@n_contr:Nat -> @+cc:Nat -> @is_sp:Bool -> @+hc:Nat -> Nat

def nt source · line 386 · raw

@kind:Nat -> @+xs:List<&2, U32> -> @+n_contr:Nat -> Pair(List<&2, U32>, List<&2, U32>)

def next_token source · line 399 · raw

@+xs:List<&2, U32> -> Pair(List<&2, U32>, List<&2, U32>)

def split source · line 408 · raw

@fuel:Nat -> @xs:List<&2, U32> -> @pr:Pair(List<&2, U32>, List<&2, U32>) -> List<&2, List<&2, U32>>

def pretok source · line 419 · raw

@+xs:List<&2, U32> -> List<&2, List<&2, U32>>

def nats_of source · line 424 · raw

@xs:List<&2, U32> -> List<&2, Nat>

def kb_sel source · line 433 · raw

@keep:Bool -> @+n:Nat -> @rest:List<&2, Nat> -> List<&2, Nat>

21 bytes have no token in SmolLM2 (perm gives 0 for them): tokenizers drops them from a piece before the merges (no unk token, no byte fallback), so they are dropped here too

def known_bytes source · line 440 · raw

@+perm:List<&2, Nat> -> @ns:List<&2, Nat> -> List<&2, Nat>

def encode_words source · line 447 · raw

@+table:List<&2, 0x559a0abb492a951db15be435191b6d0a/main.Rule> -> @+perm:List<&2, Nat> -> @ws:List<&2, List<&2, U32>> -> List<&2, Nat>

def encode_text source · line 454 · raw

@+table:List<&2, 0x559a0abb492a951db15be435191b6d0a/main.Rule> -> @+perm:List<&2, Nat> -> @+bs:List<&2, U32> -> List<&2, Nat>

def index_of source · line 462 · raw

@perm:List<&2, Nat> -> @+id:Nat -> @+i:Nat -> Nat

def tk_sel source · line 469 · raw

@b:Bool -> @x:0x559a0abb492a951db15be435191b6d0a/main.Tk -> @y:0x559a0abb492a951db15be435191b6d0a/main.Tk -> 0x559a0abb492a951db15be435191b6d0a/main.Tk

def tk_of_gid source · line 476 · raw

@+perm:List<&2, Nat> -> @+id:Nat -> 0x559a0abb492a951db15be435191b6d0a/main.Tk

def tks_sel source · line 479 · raw

@special:Bool -> @+perm:List<&2, Nat> -> @+id:Nat -> @rest:List<&2, 0x559a0abb492a951db15be435191b6d0a/main.Tk> -> List<&2, 0x559a0abb492a951db15be435191b6d0a/main.Tk>

def tks_of source · line 486 · raw

@+perm:List<&2, Nat> -> @ids:List<&2, Nat> -> List<&2, 0x559a0abb492a951db15be435191b6d0a/main.Tk>

def u32s source · line 493 · raw

@xs:List<&2, Nat> -> List<&2, U32>

def head_b source · line 500 · raw

@xs:List<&2, U32> -> U32

def tail1 source · line 507 · raw

@xs:List<&2, U32> -> List<&2, U32>

def selu source · line 514 · raw

@b:Bool -> @x:U32 -> @y:U32 -> U32

def is_cont_b source · line 521 · raw

@+b:U32 -> Bool

def ulen source · line 526 · raw

@+b:U32 -> Nat

the length of a UTF-8 sequence from its first byte; 1 for a byte that cannot start one (a continuation byte, 0xC0, 0xC1, 0xF5-0xFF)

def lo2 source · line 531 · raw

@+b0:U32 -> U32

the range of the second byte after a first byte b0 (Unicode table 3-7: no overlong forms, no surrogates, nothing above U+10FFFF); the bytes after it are always 0x80-0xBF

def hi2 source · line 534 · raw

@+b0:U32 -> U32

def conts source · line 538 · raw

@n:Nat -> @t:List<&2, U32> -> Nat

how many of the (at most n) bytes after the first are valid continuation bytes, in a row

def conts2 source · line 545 · raw

@n:Nat -> @+lo:U32 -> @+hi:U32 -> @t:List<&2, U32> -> Nat

def dec_cp source · line 552 · raw

@+b0:U32 -> @+b1:U32 -> @+b2:U32 -> @+b3:U32 -> @n:Nat -> U32

def nchar source · line 570 · raw

@ok:Bool -> @+b0:U32 -> @+t:List<&2, U32> -> @+n:Nat -> @+k:Nat -> Next

a complete sequence of n bytes is one character; an incomplete one (k < n - 1 continuation bytes) is one U+FFFD for its first 1 + k bytes, as Rust's String::from_utf8_lossy (the decoder of tokenizers) does

def next_char source · line 577 · raw

@bs:List<&2, U32> -> Next

def utf8_chars source · line 585 · raw

@fuel:Nat -> @nx:Next -> List<&2, Char>

fuel: one character per byte at most

def decode_bytes source · line 592 · raw

@+bs:List<&2, U32> -> String

def decode_text source · line 595 · raw

@+table:List<&2, 0x559a0abb492a951db15be435191b6d0a/main.Rule> -> @+perm:List<&2, Nat> -> @ids:List<&2, Nat> -> String

def bytes_of source · line 607 · raw

@cs:List<&2, Char> -> List<&2, U32>

def default_system source · line 615 · raw

List<&2, U32>

"You are a helpful AI assistant named SmolLM, trained by Hugging Face" (ASCII, one byte per character)

def bytes_eq source · line 619 · raw

@xs:List<&2, U32> -> @ys:List<&2, U32> -> @ok:Bool -> Bool

ok: whether the bytes before were equal (the walk stops at the first difference)

def chat_turn source · line 631 · raw

@+table:List<&2, 0x559a0abb492a951db15be435191b6d0a/main.Rule> -> @+perm:List<&2, Nat> -> @role:List<&2, U32> -> @content:List<&2, U32> -> List<&2, Nat>

<|im_start|>role\ncontent<|im_end|>\n

def chat_gen source · line 634 · raw

@+table:List<&2, 0x559a0abb492a951db15be435191b6d0a/main.Rule> -> @+perm:List<&2, Nat> -> @gen:Bool -> List<&2, Nat>

def chat_turns source · line 642 · raw

@+table:List<&2, 0x559a0abb492a951db15be435191b6d0a/main.Rule> -> @+perm:List<&2, Nat> -> @ms:List<&2, List<&2, U32>> -> @+gen:Bool -> List<&2, Nat>

the messages as role, content, role, content, ...; gen: end with <|im_start|>assistant\n

def chat_sys source · line 649 · raw

@sys:Bool -> @+table:List<&2, 0x559a0abb492a951db15be435191b6d0a/main.Rule> -> @+perm:List<&2, Nat> -> @+ms:List<&2, List<&2, U32>> -> @+gen:Bool -> List<&2, Nat>

def first_is_system source · line 656 · raw

@ms:List<&2, List<&2, U32>> -> Bool

def chat_ids source · line 665 · raw

@+table:List<&2, 0x559a0abb492a951db15be435191b6d0a/main.Rule> -> @+perm:List<&2, Nat> -> @+ms:List<&2, List<&2, U32>> -> @+gen:Bool -> List<&2, Nat>

the ids of a conversation in SmolLM2-Instruct's template (an empty one: no default system message, as in the template, whose loop never runs)

def selb source · line 670 · raw

@b:Bool -> @x:List<&2, U32> -> @y:List<&2, U32> -> List<&2, U32>

def enc1 source · line 677 · raw

@+cp:U32 -> List<&2, U32>

def utf8_of source · line 680 · raw

@cs:List<&2, Char> -> List<&2, U32>

def tables_of source · line 693 · raw

@ok:Bool -> @+ns:List<&2, Nat> -> @pb:List<&2, U32> -> Maybe<&2, Tables>

def read_tables source · line 702 · raw

@merges:String -> @perm:String -> IO(Maybe<&2, Tables>)

the tables in the two files reference/prep.py writes for a model (merges_num.txt and perm.txt), or None for a malformed merges table (ids_ok). The merges file is parsed once, for the check and for the rules.

def load.check source · line 709 · raw

@dir:String -> @mt:Maybe<&2, Tables> -> IO(Tables)

def load source · line 717 · raw

@+dir:String -> IO(Tables)

the tables of the model in dir; a malformed merges table stops the program

def encode source · line 723 · raw

@tb:Tables -> @text:String -> List<&2, Nat>

the ids of a text

def decode source · line 729 · raw

@tb:Tables -> @ids:List<&2, Nat> -> String

the text of ids: special tokens left out, bytes that are not valid UTF-8 as U+FFFD

def utf8s source · line 734 · raw

@ms:List<&2, String> -> List<&2, List<&2, U32>>

def chat source · line 743 · raw

@tb:Tables -> @ms:List<&2, String> -> @+gen:Bool -> List<&2, Nat>

the ids of a conversation in SmolLM2-Instruct's chat template; ms: role, content, role, content, ...; gen: end with <|im_start|>assistant\n, which asks for the answer

def chat_next source · line 752 · raw

@tb:Tables -> @msg:String -> List<&2, Nat>

the ids that continue a conversation after an answer: <|im_end|> and a newline close the assistant's turn, msg is the user's next turn, and <|im_start|>assistant\n asks for the next answer. After the conversation's ids and the answer's (without its <|im_end|>, when it has one), they give chat's ids for the conversation one turn longer, as long as the answer's text encodes back to its ids