~/bend-docscommunity

bpe.bend checks

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

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).

2 imports
import Base
import ./hmap.bend as H

Types

type BR source · line 38 · raw

Data

Definitions

def byte_char source · line 9 · raw

@+b:U32 -> U32

GPT-2's bytes_to_unicode: printable bytes stand for themselves, the other 68 for 256 + their order among them

def char_byte source · line 14 · raw

@+u:U32 -> U32

the inverse, for the 256 byte-level characters

def key_go source · line 18 · raw

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

def key source · line 26 · raw

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

a token's map key

def pair_key source · line 30 · raw

@a:List<&2, U32> -> @b:List<&2, U32> -> String

a pair's map key: "a b"

def look source · line 35 · raw

@+m:0x0c46c789483b48a99efa2c996652d510/hmap.HM -> @+k:String -> U32

---- the merge loop ---- the best pair so far: its left index and rank + 1 (0: none yet)

def better source · line 41 · raw

@+r:U32 -> @+best:U32 -> Bool

def best_pick source · line 44 · raw

@+r:U32 -> @+i:Nat -> @b:BR -> BR

def best_go source · line 49 · raw

@ps:List<&2, List<&2, U32>> -> @+ranks:0x0c46c789483b48a99efa2c996652d510/hmap.HM -> @+prev:List<&2, U32> -> @+i:Nat -> @+b:BR -> BR

def best source · line 56 · raw

@+ranks:0x0c46c789483b48a99efa2c996652d510/hmap.HM -> @ps:List<&2, List<&2, U32>> -> BR

def merge_head source · line 63 · raw

@a:List<&2, U32> -> @t:List<&2, List<&2, U32>> -> List<&2, List<&2, U32>>

def merge_at source · line 71 · raw

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

the pieces with the pair at i (and i + 1) joined

def bpe_if source · line 80 · raw

@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>>

def bpe_next source · line 87 · raw

@b:BR -> @ps:List<&2, List<&2, U32>> -> @next:(@_:List<&2, List<&2, U32>> -> List<&2, List<&2, U32>>) -> List<&2, List<&2, U32>>

def bpe_go source · line 93 · raw

@fuel:Nat -> @+ranks:0x0c46c789483b48a99efa2c996652d510/hmap.HM -> @+ps:List<&2, List<&2, U32>> -> List<&2, List<&2, U32>>

up to fuel merges, each the best pair left

def chars source · line 101 · raw

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

a byte list as byte-level characters

def singles source · line 108 · raw

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

def merge source · line 116 · raw

@+ranks:0x0c46c789483b48a99efa2c996652d510/hmap.HM -> @+bs:List<&2, U32> -> List<&2, List<&2, U32>>

a piece's bytes, merged

def ids_if source · line 121 · raw

@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>>

---- ids ---- the tokens' ids, or the first token missing from the vocabulary

def ids_add source · line 128 · raw

@+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>>

def ids_go source · line 131 · raw

@+vocab:0x0c46c789483b48a99efa2c996652d510/hmap.HM -> @ts:List<&2, List<&2, U32>> -> @acc:List<&2, U32> -> Result<&2, &2, List<&2, U32>, List<&2, U32>>

def piece_whole source · line 139 · raw

@whole:Bool -> @+ranks:0x0c46c789483b48a99efa2c996652d510/hmap.HM -> @+vocab:0x0c46c789483b48a99efa2c996652d510/hmap.HM -> @+bs:List<&2, U32> -> Result<&2, &2, List<&2, U32>, List<&2, U32>>

a piece's bytes to ids; with ignore_merges (Llama 3), a piece that is itself in the vocabulary is that token

def piece_ids source · line 145 · raw

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