GPT-2 squeezed into 40 lines of pure NumPy
picoGPT exists to show that a working GPT-2 forward pass fits in 40 lines of plain NumPy, with no deep-learning framework and no pretense of speed.

What it does
picoGPT is a minimal implementation of GPT-2 inference written entirely in NumPy. It downloads OpenAI’s pretrained weights and runs text generation through a forward pass that spans roughly 40 lines of code in gpt2_pico.py. There is no training code, no batching, and no sampling strategy beyond greedy decoding—just the bare mathematical operations needed to push tokens through the model.
The interesting bit
The project treats code golf as pedagogy. By refusing PyTorch, GPUs, and even sensible formatting, it exposes the underlying tensor operations that usually hide inside a framework. The accompanying blog post walks through the same logic in 60 lines, making this a deliberate exercise in stripping away abstraction until only the transformer math remains.
Key highlights
- Forward pass in ~40 lines of pure NumPy (
gpt2_pico.py), with a more readable but still minimal version ingpt2.py - Loads official OpenAI GPT-2 weights and hyperparameters directly via
utils.py - Supports all standard GPT-2 model sizes:
124M,355M,774M, and1558M - Uses OpenAI’s original BPE tokenizer (
encoder.py) rather than a reimplementation - Greedy decoding only: no temperature, top-k, top-p, or batch inference
Caveats
- Inference is explicitly described as “megaSLOW” and limited to greedy sampling
gpt2_pico.pysacrifices readability for line count; the README admits it is not readable- No training code or batch inference, so it is strictly a single-token-at-a-time curiosity
Verdict
Grab this if you want to see transformer inference decomposed to raw NumPy operations, or if you are teaching someone what a forward pass actually does under the hood. Skip it if you need speed, training, or anything resembling production inference.
Frequently asked
- What is jaymody/picoGPT?
- picoGPT exists to show that a working GPT-2 forward pass fits in 40 lines of plain NumPy, with no deep-learning framework and no pretense of speed.
- Is picoGPT open source?
- Yes — jaymody/picoGPT is open source, released under the MIT license.
- What language is picoGPT written in?
- jaymody/picoGPT is primarily written in Python.
- How popular is picoGPT?
- jaymody/picoGPT has 3.5k stars on GitHub.
- Where can I find picoGPT?
- jaymody/picoGPT is on GitHub at https://github.com/jaymody/picoGPT.