~/bend-docscommunity

bpe.bend source

bpe.bend on the hub · documented module

# Byte-level BPE: a piece's UTF-8 bytes as GPT-2's byte-level characters, merged pair by pair, lowest merge rank first# (the leftmost pair on a tie), as Hugging Face's `tokenizers` does, then each token's id from the vocabulary.# Tokens are lists of byte-level characters; ranks and the vocabulary are maps keyed by the characters as a String# (a merge as "a b", the file's own form), holding rank + 1 and id + 1, so 0 means absent (hmap.bend).import Baseimport ./hmap.bend as H# GPT-2's bytes_to_unicode: printable bytes stand for themselves, the other 68 for 256 + their order among themdef byte_char(+b: U32) -> U32:  Bool.pick(U32, Bool.or(Bool.and(U32.is_ge(b, 33), U32.is_le(b, 126)), Bool.or(Bool.and(U32.is_ge(b, 161), U32.is_le(b, 172)), U32.is_ge(b, 174))), b,    Bool.pick(U32, U32.is_le(b, 32), (b + 256 : U32), Bool.pick(U32, U32.is_eq(b, 173), 323, (b + 162 : U32))))# the inverse, for the 256 byte-level charactersdef char_byte(+u: U32) -> U32:  Bool.pick(U32, U32.is_lt(u, 256), u,    Bool.pick(U32, U32.is_le(u, 288), (u - 256 : U32), Bool.pick(U32, U32.is_eq(u, 323), 173, (u - 162 : U32))))def key_go(cs: List<&2, U32>) -> String:  match cs:    case []:      SNil{}    case c <> t:      SCon{Chr{c}, key_go(t)}# a token's map keydef key(cs: List<&2, U32>) -> String:  key_go(cs)# a pair's map key: "a b"def pair_key(a: List<&2, U32>, b: List<&2, U32>) -> String:  key_go(List.append(&2, U32, a, 32 <> b))# ---- the merge loop ----# the best pair so far: its left index and rank + 1 (0: none yet)def look(+m: H.HM, +k: String) -> U32:  H.get(m, k)type BR is Data:  BR{at: Nat, rank: U32}def better(+r: U32, +best: U32) -> Bool:  Bool.and(U32.is_ne(r, 0), Bool.or(U32.is_eq(best, 0), U32.is_lt(r, best)))def best_pick(+r: U32, +i: Nat, b: BR) -> BR:  match b:    case BR{at, +rank}:      Bool.pick(BR, better(r, rank), BR{i, r}, BR{at, rank})def best_go(ps: List<&2, List<&2, U32>>, +ranks: H.HM, +prev: List<&2, U32>, +i: Nat, +b: BR) -> BR:  match ps:    case []:      b    case +p <> t:      best_go(t, ranks, p, 1n+i, best_pick(look(ranks, pair_key(prev, p)), i, b))def best(+ranks: H.HM, ps: List<&2, List<&2, U32>>) -> BR:  match ps:    case []:      BR{0n, 0}    case p <> t:      best_go(t, ranks, p, 0n, BR{0n, 0})def merge_head(a: List<&2, U32>, t: List<&2, List<&2, U32>>) -> List<&2, List<&2, U32>>:  match t:    case []:      [a]    case b <> t2:      List.append(&2, U32, a, b) <> t2# the pieces with the pair at i (and i + 1) joineddef merge_at(ps: List<&2, List<&2, U32>>, i: Nat) -> List<&2, List<&2, U32>>:  match ps i:    case Nil{} _:      []    case a <> t 0n:      merge_head(a, t)    case a <> t 1n+j:      a <> merge_at(t, j)def bpe_if(none: Bool, at: Nat, ps: List<&2, List<&2, U32>>, next: List<&2, List<&2, U32>> -> List<&2, List<&2, U32>>) -> List<&2, List<&2, U32>>:  match none:    case True{}:      ps    case False{}:      next(merge_at(ps, at))def bpe_next(b: BR, ps: List<&2, List<&2, U32>>, next: List<&2, List<&2, U32>> -> List<&2, List<&2, U32>>) -> List<&2, List<&2, U32>>:  match b:    case BR{at, rank}:      bpe_if(U32.is_eq(rank, 0), at, ps, next)# up to fuel merges, each the best pair leftdef bpe_go(fuel: Nat, +ranks: H.HM, +ps: List<&2, List<&2, U32>>) -> List<&2, List<&2, U32>>:  match fuel:    case 0n:      ps    case 1n+f:      bpe_next(best(ranks, ps), ps, x => bpe_go(f, ranks, x))# a byte list as byte-level charactersdef chars(bs: List<&2, U32>) -> List<&2, U32>:  match bs:    case []:      []    case b <> t:      byte_char(b) <> chars(t)def singles(bs: List<&2, U32>) -> List<&2, List<&2, U32>>:  match bs:    case []:      []    case b <> t:      [byte_char(b)] <> singles(t)# a piece's bytes, mergeddef merge(+ranks: H.HM, +bs: List<&2, U32>) -> List<&2, List<&2, U32>>:  bpe_go(List.length(&2, U32, bs), ranks, singles(bs))# ---- ids ----# the tokens' ids, or the first token missing from the vocabularydef ids_if(missing: Bool, +id: U32, t: List<&2, U32>, acc: List<&2, U32>, next: List<&2, U32> -> Result<&2, &2, List<&2, U32>, List<&2, U32>>) -> Result<&2, &2, List<&2, U32>, List<&2, U32>>:  match missing:    case True{}:      Fail{t}    case False{}:      next(id <> acc)def ids_add(+v: U32, t: List<&2, U32>, acc: List<&2, U32>, next: List<&2, U32> -> Result<&2, &2, List<&2, U32>, List<&2, U32>>) -> Result<&2, &2, List<&2, U32>, List<&2, U32>>:  ids_if(U32.is_eq(v, 0), (v - 1 : U32), t, acc, next)def ids_go(+vocab: H.HM, ts: List<&2, List<&2, U32>>, acc: List<&2, U32>) -> Result<&2, &2, List<&2, U32>, List<&2, U32>>:  match ts:    case []:      Done{List.reverse(&2, U32, acc)}    case +t <> rest:      ids_add(look(vocab, key(t)), t, acc, a => ids_go(vocab, rest, a))# a piece's bytes to ids; with ignore_merges (Llama 3), a piece that is itself in the vocabulary is that tokendef piece_whole(whole: Bool, +ranks: H.HM, +vocab: H.HM, +bs: List<&2, U32>) -> Result<&2, &2, List<&2, U32>, List<&2, U32>>:  match whole:    case True{}:      ids_go(vocab, [chars(bs)], [])    case False{}:      ids_go(vocab, merge(ranks, bs), [])def piece_ids(+ignore: Bool, +ranks: H.HM, +vocab: H.HM, +bs: List<&2, U32>) -> Result<&2, &2, List<&2, U32>, List<&2, U32>>:  piece_whole(Bool.and(ignore, U32.is_ne(look(vocab, key(chars(bs))), 0)), ranks, vocab, bs)