Documentation Index
Fetch the complete documentation index at: https://mintlify.com/itsubaki/gpt/llms.txt
Use this file to discover all available pages before exploring further.
BPETokenizer encodes text into integer token IDs using trained Byte Pair Encoding merge rules, and decodes token IDs back to text. The vocabulary contains 256 base byte tokens (one per possible byte value 0–255), plus merged subword tokens produced during BPE training, plus one end token appended at the top of the ID space.
BPETokenizer struct
ID2Bytes is the primary reverse-lookup table used by Decode. It is populated at construction time: byte IDs 0–255 map to single-byte slices, merged token IDs map to the concatenated bytes of their constituent pair, and the end token ID maps to the raw bytes of the end token string.
VocabSize equals len(ID2Bytes), which is 256 + mergeRules.Len() + 1.
NewBPETokenizer()
BPETokenizer from a pre-trained set of merge rules. The tokenizer builds its ID2Bytes map eagerly at construction time so that every subsequent Encode/Decode call operates without further allocation on the vocabulary.
The trained BPE merge rules returned by
TrainBPE or deserialized by Load. Each entry maps a Pair of adjacent token IDs to the new merged token ID assigned during training.Optional end-of-text token string. Defaults to
"<|endoftext|>" when omitted. The token is assigned the ID 256 + mergeRules.Len() and its raw bytes are stored in ID2Bytes.*BPETokenizer
NewBPETokenizerFrom()
NewBPETokenizer. Equivalent to calling Load followed by NewBPETokenizer.
Filesystem path to a gob-encoded
DefaultDict[Pair] file previously written by Save.Optional end-of-text token override. Defaults to
"<|endoftext|>".(*BPETokenizer, error) — returns an error if the file cannot be opened or the gob stream cannot be decoded.
Encode()
- End-token splitting — the input string is split on literal occurrences of the end token using a compiled regexp. Each end token occurrence is kept as its own segment and mapped directly to
EndTokenID(). - Pre-tokenization — each non-end-token segment is broken into pre-tokens by the GPT-2 style regex pattern
`'(?:[sdmt]|ll|ve|re)| ?\pL+| ?\pN+| ?[^\s\pL\pN]+|\s+`. This splits on word boundaries, contractions, punctuation, and whitespace. - BPE encoding — each pre-token is converted to a sequence of byte IDs (0–255) and then iteratively merged. At each iteration the pair that appears earliest in the merge rules (lowest assigned ID, meaning highest training priority) is selected and merged, until no further applicable merge rules remain.
[]int — token IDs in input order. An end token in the input produces exactly one ID (EndTokenID()).
Decode()
ID2Bytes and all byte slices are concatenated, then cast to string.
Because each merged token stores the full concatenated bytes of both its constituent parts (built at construction time), multi-byte UTF-8 sequences are reconstructed correctly even when a character boundary falls inside a merged token.
Slice of token IDs as returned by
Encode. IDs outside the valid vocabulary range (0 to VocabSize-1) produce a nil byte slice entry and are silently skipped.string
EndTokenID()
256 + mergeRules.Len() — one above the highest merged token ID — making it the final entry in the vocabulary.
Returns: int
This ID is used by generation loops to detect when the model has produced the end-of-sequence signal:
Vocabulary layout
The token ID space is partitioned into three contiguous regions:| Range | Size | Content | ||
|---|---|---|---|---|
0 – 255 | 256 | Base byte tokens. ID i maps to the single byte byte(i). Every possible byte value has a fixed ID. | ||
256 – 256+N-1 | N | Merged subword tokens, where N is the number of BPE merge rules. IDs are assigned in training order: the most frequent pair found first receives ID 256, the next receives 257, and so on. | ||
256+N | 1 | End token (`< | endoftext | >` by default). Always the highest ID in the vocabulary. |
VocabSize = 256 + N + 1
The merge priority during
Encode is determined by the assigned ID: a lower merged ID means the pair was merged earlier (more frequent) during training and therefore takes precedence. This mirrors the training order stored in DefaultDict[Pair].Order.