~/bend-docscommunity

tok.bend checks

raw source on the hub · import bend-bpe-tokenizer@0.1.0.0/tok.bend as Tok

bend-bpe-tokenizer: byte-level BPE tokenizers from a Hugging Face tokenizer.json, giving the ids Hugging Face's tokenizers gives (encode(text, add_special_tokens=False)), and the text back from ids. import bend-bpe-tokenizer@<version>/tok.bend as TK

Tok.open(path) reads a tokenizer.json of a known family, or says why it can't: Qwen2/Qwen3, Qwen3.5 and later (words take combining marks), Llama 3, and GPT-2 style (GPT-2, RoBERTa, BART...): byte-level BPE, normalizer NFC or none, added tokens without lstrip, rstrip or single_word. encode(tok, bytes) takes UTF-8 text and answers its ids, or the offset of the first ill-formed UTF-8 sequence: added tokens are found in the raw text (longest first), the rest is normalized (NFC, if the file says so), split by the family's regex, and each piece's bytes merged by BPE. Unicode is as Hugging Face's tokenizers sees it (tables.bend, generated from it): Unicode 16.0 for the split, its NFC's own (older) data for normalization. decoder(tok) and decode(dec, ids) give the UTF-8 bytes of ids (an added token's own text for its id).

10 imports
import Base
import ./tree.bend as T
import ./tables.bend as TB
import ./utf8.bend as U
import ./nfc.bend as N
import ./split.bend as S
import ./bpe.bend as B
import ./specials.bend as A
import ./json.bend as J
import ./hmap.bend as H

Types

type Tok source · line 24 · raw

Data

type Err source · line 28 · raw

Data

Definitions

def str_is2 source · line 33 · raw

@j:0x0c46c789483b48a99efa2c996652d510/json.J -> @+s:String -> Bool

---- loading: what the file must say, and the tables built from it ----

def str_is source · line 48 · raw

@v:Maybe<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> @+s:String -> Bool

def is_blank2 source · line 56 · raw

@j:0x0c46c789483b48a99efa2c996652d510/json.J -> Bool

null, absent, or an empty string

def is_blank source · line 71 · raw

@v:Maybe<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> Bool

def bool_of2 source · line 78 · raw

@j:0x0c46c789483b48a99efa2c996652d510/json.J -> @d:Bool -> Bool

def bool_of source · line 93 · raw

@v:Maybe<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> @d:Bool -> Bool

def fieldm source · line 100 · raw

@m:Maybe<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> @+name:String -> Maybe<&2, 0x0c46c789483b48a99efa2c996652d510/json.J>

def items2 source · line 107 · raw

@j:0x0c46c789483b48a99efa2c996652d510/json.J -> List<&2, 0x0c46c789483b48a99efa2c996652d510/json.J>

def items source · line 122 · raw

@v:Maybe<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> List<&2, 0x0c46c789483b48a99efa2c996652d510/json.J>

def kvs_of2 source · line 129 · raw

@j:0x0c46c789483b48a99efa2c996652d510/json.J -> List<&2, 0x0c46c789483b48a99efa2c996652d510/json.KV>

def kvs_of source · line 144 · raw

@v:Maybe<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> List<&2, 0x0c46c789483b48a99efa2c996652d510/json.KV>

def str_of source · line 151 · raw

@j:0x0c46c789483b48a99efa2c996652d510/json.J -> List<&2, U32>

def num_of2 source · line 166 · raw

@j:0x0c46c789483b48a99efa2c996652d510/json.J -> U32

def num_of source · line 181 · raw

@v:Maybe<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> U32

def regex_family source · line 189 · raw

@+v:Maybe<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> U32

the family of the pre-tokenizer, 4 for none known

def byte_level source · line 195 · raw

@+j:0x0c46c789483b48a99efa2c996652d510/json.J -> @+regex:Bool -> Bool

a ByteLevel step that only maps bytes (no prefix space; with use_regex it is GPT-2's split)

def split_ok source · line 199 · raw

@+j:0x0c46c789483b48a99efa2c996652d510/json.J -> Bool

def is_nil_j source · line 203 · raw

@xs:List<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> Bool

def seq_family2 source · line 210 · raw

@t:List<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> @+s1:0x0c46c789483b48a99efa2c996652d510/json.J -> U32

def seq_family source · line 218 · raw

@steps:List<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> U32

def family source · line 225 · raw

@+pre:Maybe<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> U32

def vocab_go source · line 230 · raw

@kvs:List<&2, 0x0c46c789483b48a99efa2c996652d510/json.KV> -> List<&2, 0x0c46c789483b48a99efa2c996652d510/hmap.HE>

the vocabulary: token -> id + 1 the vocabulary's entries, halves built at the same time, depth times over

def vocab_par source · line 237 · raw

@depth:Nat -> @+n:Nat -> @+kvs:List<&2, 0x0c46c789483b48a99efa2c996652d510/json.KV> -> 0x0c46c789483b48a99efa2c996652d510/hmap.ETr

def first_str source · line 246 · raw

@xs:List<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> List<&2, U32>

the merges: "a b" -> rank + 1; a merge is "a b" or, in newer files, ["a", "b"]

def char_bytes source · line 253 · raw

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

def merge_key2 source · line 260 · raw

@xs:List<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> List<&2, U32>

def merge_key source · line 267 · raw

@j:0x0c46c789483b48a99efa2c996652d510/json.J -> List<&2, U32>

def ranks_go source · line 283 · raw

@ms:List<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> @+r:U32 -> List<&2, 0x0c46c789483b48a99efa2c996652d510/hmap.HE>

the merges' entries (rank r first), halves built at the same time, depth times over

def ranks_par source · line 290 · raw

@depth:Nat -> @+n:Nat -> @+r:U32 -> @+ms:List<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> 0x0c46c789483b48a99efa2c996652d510/hmap.ETr

def added_ok source · line 299 · raw

@+a:0x0c46c789483b48a99efa2c996652d510/json.J -> @+nfc:Bool -> Bool

an added token, if it has no option this tokenizer can't honour

def added_all source · line 303 · raw

@as:List<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> @+nfc:Bool -> Bool

def added_go source · line 310 · raw

@as:List<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> List<&2, 0x0c46c789483b48a99efa2c996652d510/specials.AT>

def why_not source · line 317 · raw

@+root:0x0c46c789483b48a99efa2c996652d510/json.J -> @+fam:U32 -> @+nfc_ok:Bool -> Maybe<&2, String>

def build_maps source · line 331 · raw

@+fam:U32 -> @+root:0x0c46c789483b48a99efa2c996652d510/json.J -> @+kvs:List<&2, 0x0c46c789483b48a99efa2c996652d510/json.KV> -> @+ms:List<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> Tok

the two maps, built at the same time

def build source · line 337 · raw

@why:Maybe<&2, String> -> @+root:0x0c46c789483b48a99efa2c996652d510/json.J -> @+fam:U32 -> Result<&2, &2, String, Tok>

def nfc_ok source · line 344 · raw

@+n:Maybe<&2, 0x0c46c789483b48a99efa2c996652d510/json.J> -> Bool

def load2 source · line 347 · raw

@+fam:U32 -> @+root:0x0c46c789483b48a99efa2c996652d510/json.J -> Result<&2, &2, String, Tok>

def load_json source · line 350 · raw

@+root:0x0c46c789483b48a99efa2c996652d510/json.J -> Result<&2, &2, String, Tok>

def load_parsed source · line 353 · raw

@r:Result<&2, &2, String, 0x0c46c789483b48a99efa2c996652d510/json.J> -> Result<&2, &2, String, Tok>

def load source · line 361 · raw

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

a tokenizer from the bytes of its tokenizer.json

def classify source · line 365 · raw

@+ct:0x0c46c789483b48a99efa2c996652d510/tree.RT -> @cs:List<&2, U32> -> List<&2, 0x0c46c789483b48a99efa2c996652d510/split.CC>

---- encoding ----

def piece_cps source · line 372 · raw

@xs:List<&2, 0x0c46c789483b48a99efa2c996652d510/split.CC> -> List<&2, U32>

def err_tok source · line 379 · raw

@r:Result<&2, &2, List<&2, U32>, List<&2, U32>> -> Result<&2, &2, Err, List<&2, U32>>

def join2 source · line 387 · raw

@xs:List<&2, U32> -> @b:Result<&2, &2, Err, List<&2, U32>> -> Result<&2, &2, Err, List<&2, U32>>

results joined, the first failure kept

def join source · line 394 · raw

@a:Result<&2, &2, Err, List<&2, U32>> -> @rest:(@_:Unit -> Result<&2, &2, Err, List<&2, U32>>) -> Result<&2, &2, Err, List<&2, U32>>

def pieces_ids source · line 401 · raw

@+ignore:Bool -> @+ranks:0x0c46c789483b48a99efa2c996652d510/hmap.HM -> @+vocab:0x0c46c789483b48a99efa2c996652d510/hmap.HM -> @ps:List<&2, List<&2, 0x0c46c789483b48a99efa2c996652d510/split.CC>> -> Result<&2, &2, Err, List<&2, U32>>

def tok_fam source · line 408 · raw

@t:Tok -> U32

def maybe_nfc source · line 413 · raw

@on:Bool -> @+nt:0x0c46c789483b48a99efa2c996652d510/nfc.NT -> @cs:List<&2, U32> -> List<&2, U32>

def text_ids source · line 420 · raw

@+fam:U32 -> @+nfc:Bool -> @+ignore:Bool -> @+vocab:0x0c46c789483b48a99efa2c996652d510/hmap.HM -> @+ranks:0x0c46c789483b48a99efa2c996652d510/hmap.HM -> @+ct:0x0c46c789483b48a99efa2c996652d510/tree.RT -> @+nt:0x0c46c789483b48a99efa2c996652d510/nfc.NT -> @cs:List<&2, U32> -> Result<&2, &2, Err, List<&2, U32>>

def seg_ids source · line 423 · raw

@+fam:U32 -> @+nfc:Bool -> @+ignore:Bool -> @+vocab:0x0c46c789483b48a99efa2c996652d510/hmap.HM -> @+ranks:0x0c46c789483b48a99efa2c996652d510/hmap.HM -> @+ct:0x0c46c789483b48a99efa2c996652d510/tree.RT -> @+nt:0x0c46c789483b48a99efa2c996652d510/nfc.NT -> @s:0x0c46c789483b48a99efa2c996652d510/specials.Seg -> Result<&2, &2, Err, List<&2, U32>>

def segs_ids source · line 430 · raw

@+fam:U32 -> @+nfc:Bool -> @+ignore:Bool -> @+vocab:0x0c46c789483b48a99efa2c996652d510/hmap.HM -> @+ranks:0x0c46c789483b48a99efa2c996652d510/hmap.HM -> @+ct:0x0c46c789483b48a99efa2c996652d510/tree.RT -> @+nt:0x0c46c789483b48a99efa2c996652d510/nfc.NT -> @ss:List<&2, 0x0c46c789483b48a99efa2c996652d510/specials.Seg> -> Result<&2, &2, Err, List<&2, U32>>

def encode_tok source · line 437 · raw

@t:Tok -> @cs:List<&2, U32> -> Result<&2, &2, Err, List<&2, U32>>

def encode_cps source · line 442 · raw

@t:Tok -> @r:Result<&2, &2, U32, List<&2, U32>> -> Result<&2, &2, Err, List<&2, U32>>

def encode source · line 450 · raw

@t:Tok -> @bs:List<&2, U32> -> Result<&2, &2, Err, List<&2, U32>>

the ids of UTF-8 text

def str_cps source · line 454 · raw

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

---- decoding ----

def dec_go source · line 461 · raw

@ps:List<&2, Sigma<&2, &2, String, _ => U32>> -> @m:Map<&2, List<&2, U32>> -> Map<&2, List<&2, U32>>

def dec_added source · line 468 · raw

@as:List<&2, 0x0c46c789483b48a99efa2c996652d510/specials.AT> -> @m:Map<&2, List<&2, U32>> -> Map<&2, List<&2, U32>>

def decoder source · line 476 · raw

@t:Tok -> Map<&2, List<&2, U32>>

id -> bytes, for decode

def dec_one source · line 481 · raw

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

def decode source · line 487 · raw

@+d:Map<&2, List<&2, U32>> -> @ids:List<&2, U32> -> List<&2, U32>

the UTF-8 bytes of ids (an unknown id gives nothing)

def encode_with source · line 495 · raw

@r:Result<&2, &2, String, Tok> -> @bs:List<&2, U32> -> Result<&2, &2, Err, List<&2, U32>>

encode with a loaded tokenizer, or a refusal at offset 0 when there is none (for LAWS.bend)

def got_bytes source · line 503 · raw

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

---- reading a file ----

def read_more source · line 511 · raw

@fa:Pair(File, List<&2, List<&2, U32>>) -> @go:(@_:File -> @_:List<&2, List<&2, U32>> -> IO(Pair(File, List<&2, List<&2, U32>>))) -> IO(Pair(File, List<&2, List<&2, U32>>))

blocks of 2^16 bytes from offset at, n of them (last first)

def read_blocks source · line 516 · raw

@n:Nat -> @f:File -> @+at:U32 -> @acc:List<&2, List<&2, U32>> -> IO(Pair(File, List<&2, List<&2, U32>>))

def size_of source · line 525 · raw

@r:Pair(File, Result<&1, &1, Pair(U32, String), U32>) -> Pair(File, U32)

def load_read source · line 532 · raw

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

def opened source · line 539 · raw

@r:Pair(File, U32) -> IO(Result<&2, &2, String, Tok>)

def open_file source · line 546 · raw

@r:Result<&1, &1, Pair(U32, String), File> -> @+path:String -> IO(Result<&2, &2, String, Tok>)

def open source · line 556 · raw

@+path:String -> IO(Result<&2, &2, String, Tok>)

a tokenizer from a tokenizer.json file, read in blocks that are lexed at the same time