- Category
- AI Tools
- Rank
- No. 499Tools index
- Pricing
- Open Source
- Type
- TOOL
- Builder
- karpathy
- GitHub
- 10.7k stars
- Date
About
Andrej Karpathy's minimal, clean implementation of Byte Pair Encoding (BPE) — the tokenization algorithm used by GPT and most modern LLMs.
What it does
Minbpe turns UTF-8 text into integer tokens by repeatedly joining common adjacent byte pairs. It can learn those joins from supplied text, reverse tokens back into text, split input into categories before processing, and recognize explicitly permitted special tokens.
Why it's ranked here
Its value comes from transparency and scope. The repository exposes the complete training and encoding process in short Python modules, then tests round trips, persistence, special-token handling, and agreement with the referenced GPT-4 encoding. It favors understanding and experimentation over production throughput.
What's good
The basic and regex-based variants separate the core algorithm from practical text preprocessing. Models can be saved and loaded, while a separate human-readable vocabulary shows how tokens were assembled. Special tokens require explicit permission, reducing the risk that attacker-controlled text gains unintended control meaning.
Tradeoffs
Training repeatedly counts adjacent pairs and rebuilds token sequences, and the project explicitly leaves large-file and large-vocabulary optimization as future work. The GPT-4-compatible variant cannot train, save, or load models. Support for other GPT generations, Llama, and SentencePiece-style tokenization is also unfinished.
How to use it well
Use it to learn byte-pair encoding, teach tokenizer internals, prototype a custom vocabulary, or compare text splitting strategies. Start with the basic variant, then add category-aware splitting and special tokens. Choose a separate implementation for production-scale training, broad pretrained-tokenizer coverage, or SentencePiece compatibility.
Technical notes+
minbpe/basic.py trains by repeatedly selecting the most frequent adjacent pair and assigning IDs after the 256 byte tokens. minbpe/regex.py applies GPT4_SPLIT_PATTERN, trains chunks independently, and gates registered special tokens through allowed_special. minbpe/gpt4.py reads cl100k_base through tiktoken, reconstructs merges with recover_merges, and compensates for the byte permutation; its train, save, and load methods raise NotImplementedError. minbpe/base.py stores the split pattern, special-token table, and merge pairs in a text model, while producing a lossy display-only vocabulary. tests/test_tokenizer.py uses pytest for round trips, tiktoken equality, special tokens, the reference BPE example, and save/load behavior. requirements.txt lists regex and tiktoken.
Observed
- License
- MIT License
- Primary language
- Python
- Interface
- Importable Python library with tokenizer classes; no CLI or network API is shown
- Dependencies
- requirements.txt lists regex and tiktoken
- Persistence
- Trainable tokenizers write a loadable model and a separate human-readable vocabulary
- Testing
- A pytest suite covers round trips, GPT-4 token equality, special tokens, reference output, and persistence
- Tokenizer variants
- Basic byte-level, regex-splitting, and pretrained GPT-4-compatible variants are exported
Read from README.md, requirements.txt, train.py, minbpe/base.py, minbpe/gpt4.py, minbpe/basic.py, minbpe/regex.py, minbpe/__init__.py, tests/test_tokenizer.py, LICENSE, lecture.md, exercise.md, tests/taylorswift.txt.
What it can do
Train a BPE tokenizer on text data
Raw text corpus → Trained BPE tokenizer model
Tokenize text into subword units
Text string and trained BPE model → List of token IDs
Decode token IDs back to text
List of token IDs and BPE model → Original text string
Build vocabulary from byte pairs
Training text corpus → BPE vocabulary with merge rules
Learn merge operations for subword splitting
Text data and vocabulary size parameter → Sequence of merge operations
Convert text to byte-level representation
Unicode text string → Byte sequence
Tags
Tech Stack
Comments (0)
No comments yet
Editorially curated, with community endorsements as a secondary signal. Corrections welcome.
