LAWS.bend open laws/TODOs
raw source on the hub · import bend-bpe-tokenizer@0.1.0.0/LAWS.bend as LAWS
The laws of bend-bpe-tokenizer. Each is stated over every input where its shape allows (the three partition laws);
the rest pin the decided behaviour at literal inputs. Matching Hugging Face id for id (the tables, NFC, the regexes,
BPE order) is checked by test/compare.py against tokenizers itself, not here.
8 imports
import Base import ./utf8.bend as U import ./split.bend as S import ./bpe.bend as B import ./hmap.bend as H import ./specials.bend as A import ./tok.bend as TK import ./json.bend as J
Laws
law split_partition provedin PROOF.bendsource · line 78 · raw
@+fam:U32 -> @+xs:List<&2, 0x0c46c789483b48a99efa2c996652d510/split.CC> -> {List.concat(&2, 0x0c46c789483b48a99efa2c996652d510/split.CC, 0x0c46c789483b48a99efa2c996652d510/split.split(fam, xs)) == xs : List<&2, 0x0c46c789483b48a99efa2c996652d510/split.CC>}---- splitting never loses or duplicates text ---- Guarantees: for every family and every classified text, the pieces the pre-tokenizer cuts, concatenated in order, are exactly the text. Why it matters: a piece dropped, repeated or reordered would give ids for a different prompt with no error; this holds whatever the split rules decide, so a wrong rule can only cut in the wrong place.
law bpe_conserves provedin PROOF.bendsource · line 87 · raw
@fuel:Nat -> @+ranks:0x0c46c789483b48a99efa2c996652d510/hmap.HM -> @+ps:List<&2, List<&2, U32>> -> {List.concat(&2, U32, 0x0c46c789483b48a99efa2c996652d510/bpe.bpe_go(fuel, ranks, ps)) == List.concat(&2, U32, ps) : List<&2, U32>}---- merging keeps the bytes ---- Guarantees: however many merges run and whatever the ranks are, the tokens BPE leaves, concatenated, are the byte-level characters it started with. Why it matters: a merge that lost or duplicated a byte would encode a different text; ids could still all be valid.
law added_partition provedin PROOF.bendsource · line 97 · raw
@+added:List<&2, 0x0c46c789483b48a99efa2c996652d510/specials.AT> -> @+xs:List<&2, U32> -> {0x0c46c789483b48a99efa2c996652d510/specials.text_of(0x0c46c789483b48a99efa2c996652d510/specials.split_added(added, xs)) == xs : List<&2, U32>}---- added tokens are cut from the text itself ---- Guarantees: for every list of added tokens and every text, the plain segments and the added-token matches cover the text exactly, in order. Why it matters: text around a special token (a chat template's) must reach the tokenizer whole.
law added_longest provedin PROOF.bendsource · line 105 · raw
{0x0c46c789483b48a99efa2c996652d510/specials.split_added([0x0c46c789483b48a99efa2c996652d510/specials.AT{ascii("<a>"), 1}, 0x0c46c789483b48a99efa2c996652d510/specials.AT{ascii("<a>b"), 2}], ascii("x<a>by")) == [0x0c46c789483b48a99efa2c996652d510/specials.SText{ascii("x")}, 0x0c46c789483b48a99efa2c996652d510/specials.SAdded{2, ascii("<a>b")}, 0x0c46c789483b48a99efa2c996652d510/specials.SText{ascii("y")}] : List<&2, 0x0c46c789483b48a99efa2c996652d510/specials.Seg>}---- the longest added token wins ---- Guarantees: with added tokens <a> (id 1) and <a>b (id 2), "x<a>by" splits as x, <a>b (id 2), y. Why it matters: Hugging Face matches the longest added token; matching <a> would leave a stray "b".
law utf8_round provedin PROOF.bendsource · line 115 · raw
{0x0c46c789483b48a99efa2c996652d510/utf8.encode(0x0c46c789483b48a99efa2c996652d510/utf8.decode_ok([104, 195, 169, 226, 130, 172, 240, 159, 152, 128])) == [104, 195, 169, 226, 130, 172, 240, 159, 152, 128] : List<&2, U32>}---- ill-formed UTF-8 is refused, at the right byte ---- Guarantees: well-formed text of 1- to 4-byte characters decodes and encodes back to the same bytes; a stray continuation byte, overlong 2-, 3- and 4-byte forms, a surrogate, a code point above U+10FFFF, the bytes C1 and F5, a sequence cut short and one broken by a non-continuation byte are each refused with the offset of the first byte of the bad sequence. Why it matters: decision 6: a prompt file that is not UTF-8 is an error naming where, not a silently changed prompt.
law utf8_refusals provedin PROOF.bendsource · line 118 · raw
{[0x0c46c789483b48a99efa2c996652d510/utf8.decode([97, 128]), 0x0c46c789483b48a99efa2c996652d510/utf8.decode([97, 192, 175]), 0x0c46c789483b48a99efa2c996652d510/utf8.decode([237, 160, 128]), 0x0c46c789483b48a99efa2c996652d510/utf8.decode([97, 98, 244, 144, 128, 128]), 0x0c46c789483b48a99efa2c996652d510/utf8.decode([226, 130]), 0x0c46c789483b48a99efa2c996652d510/utf8.decode([224, 159, 128]), 0x0c46c789483b48a99efa2c996652d510/utf8.decode([240, 143, 128, 128]), 0x0c46c789483b48a99efa2c996652d510/utf8.decode([193, 129]), 0x0c46c789483b48a99efa2c996652d510/utf8.decode([245, 128, 128, 128]), 0x0c46c789483b48a99efa2c996652d510/utf8.decode([104, 226, 40, 161])] == [Fail{1}, Fail{1}, Fail{0}, Fail{2}, Fail{0}, Fail{0}, Fail{0}, Fail{0}, Fail{0}, Fail{1}] : List<&2, Result<&2, &2, U32, List<&2, U32>>>}
law encode_refuses provedin PROOF.bendsource · line 125 · raw
{err_at(0x0c46c789483b48a99efa2c996652d510/tok.encode_with(0x0c46c789483b48a99efa2c996652d510/tok.load(tiny(qwen_pattern)), [104, 255])) == 1 : U32}Guarantees: encoding "h" followed by the byte FF (never valid in UTF-8) is refused as ill-formed UTF-8 at offset 1. Why it matters: decision 6 through the public entry point: encode reports where the text is bad, not an id list.
law byte_level_inverse provedin PROOF.bendsource · line 131 · raw
{byte_ok(256n) == True{} : Bool}---- the byte-level map is invertible ---- Guarantees: every byte 0 to 255 maps to a byte-level character that maps back to it. Why it matters: decode reads bytes back through this map; two bytes sharing a character would decode wrongly.
law family_known provedin PROOF.bendsource · line 138 · raw
{outcome(0x0c46c789483b48a99efa2c996652d510/tok.load(tiny(qwen_pattern))) == "family 0" : String}---- known families are read, others refused ---- Guarantees: a Qwen-family tokenizer.json is read as family 0; the same file with any other split pattern is refused as not a known family. Why it matters: decision 1: a tokenizer this package cannot reproduce must be refused, not silently mis-tokenized.
law family_unknown provedin PROOF.bendsource · line 141 · raw
{outcome(0x0c46c789483b48a99efa2c996652d510/tok.load(tiny("\\\\p{L}+"))) == "the pre-tokenizer is not a known byte-level family's (Qwen2/3, Qwen3.5+, Llama 3, GPT-2)" : String}
law blocks_agree provedin PROOF.bendsource · line 159 · raw
{[by_blocks(1n, pretty), by_blocks(3n, pretty), by_blocks(7n, pretty), by_blocks(3n, minified), by_blocks(7n, minified)] == [0x0c46c789483b48a99efa2c996652d510/json.parse(pretty), 0x0c46c789483b48a99efa2c996652d510/json.parse(pretty), 0x0c46c789483b48a99efa2c996652d510/json.parse(pretty), 0x0c46c789483b48a99efa2c996652d510/json.parse(minified), 0x0c46c789483b48a99efa2c996652d510/json.parse(minified)] : List<&2, Result<&2, &2, String, 0x0c46c789483b48a99efa2c996652d510/json.J>>}Guarantees: a pretty-printed JSON text (lines longer than a block, and blocks inside one line) and a minified one (no newline at all) parse block-parallel, at block sizes 1, 3 and 7 bytes, to the value the one-piece parse gives. Why it matters: Tok.open lexes a file's blocks in parallel; a block boundary handled wrongly loses or repeats text (it did, once, for a minified file: a GPT-2 tokenizer.json was refused as "not JSON").
law round_trip provedin PROOF.bendsource · line 167 · raw
{round(0x0c46c789483b48a99efa2c996652d510/tok.load(tiny(qwen_pattern)), [104, 101, 204, 129, 60, 120, 62]) == [104, 195, 169, 60, 120, 62] : List<&2, U32>}---- encode and decode, through NFC and an added token ---- Guarantees: with the tiny tokenizer, "h", "e" + U+0301 (é decomposed), "<x>" encodes as h, é (merged), the added token, and decodes to "hé<x>" with é composed. Why it matters: decisions 3, 4 and 7 end to end: NFC before the split, added tokens matched, decode the inverse.
Definitions
def ascii source · line 14 · raw
@s:String -> List<&2, U32>
---- helpers for the literal laws ----
def byte_ok source · line 21 · raw
@n:Nat -> Bool
def outcome source · line 29 · raw
@r:Result<&2, &2, String, 0x0c46c789483b48a99efa2c996652d510/tok.Tok> -> String
a load's outcome: the family it was read as, or why it was refused
def ids_of source · line 36 · raw
@r:Result<&2, &2, 0x0c46c789483b48a99efa2c996652d510/tok.Err, List<&2, U32>> -> List<&2, U32>
def err_at2 source · line 43 · raw
@e:0x0c46c789483b48a99efa2c996652d510/tok.Err -> U32
def err_at source · line 51 · raw
@r:Result<&2, &2, 0x0c46c789483b48a99efa2c996652d510/tok.Err, List<&2, U32>> -> U32
the offset a failed encode names (0 for anything else)
def round source · line 59 · raw
@r:Result<&2, &2, String, 0x0c46c789483b48a99efa2c996652d510/tok.Tok> -> @bs:List<&2, U32> -> List<&2, U32>
encode, then decode
def tiny source · line 67 · raw
@pattern:String -> List<&2, U32>
a tiny Qwen-family tokenizer: four tokens (h, the two byte-level characters of é's UTF-8 bytes C3 A9, and their merge)
def qwen_pattern source · line 70 · raw
String
def by_blocks source · line 146 · raw
@+size:Nat -> @+bs:List<&2, U32> -> Result<&2, &2, String, 0x0c46c789483b48a99efa2c996652d510/json.J>
---- reading a file in parallel blocks parses as reading it whole ---- the text cut into blocks of size bytes, parsed block-parallel
def pretty source · line 149 · raw
List<&2, U32>
def minified source · line 152 · raw
List<&2, U32>