nanoGPT: Minimal GPT Training in PyTorch
nanoGPT is Andrej Karpathy's minimal, from-scratch PyTorch codebase for training and finetuning GPT-2-scale language models, and its own README now labels it old and deprecated, pointing to a newer project called nanochat instead. Reach for nanoGPT if you want to read and modify a complete GPT training loop in a few hundred lines; skip it if you need an actively maintained project, since nanochat is what the author now recommends.
What is nanoGPT for language model development?
nanoGPT is a compact PyTorch repository for training and finetuning medium-sized GPT models, built as a rewrite of Karpathy's earlier minGPT project that trades some of minGPT's teaching clarity for raw training speed. The entire model lives in a roughly 300-line model.py and the training loop in a roughly 300-line train.py, and both can optionally load pretrained GPT-2 weights released by OpenAI.
Core features of nanoGPT
- ✓train.py is a boilerplate training loop at roughly 300 lines, and model.py defines the GPT architecture at about the same length, per the README.
- ✓Reproduces GPT-2 (124M) on the OpenWebText dataset, reaching a loss of about 2.85 after roughly 4 days of training on one 8xA100 40GB GPU node, per the README's own numbers.
- ✓Loads pretrained OpenAI GPT-2 checkpoints directly, including the four public sizes 'gpt2', 'gpt2-medium', 'gpt2-large', and 'gpt2-xl', so finetuning doesn't require training from a blank model.
- ✓Ships ready-made config files for different scenarios, from a CPU-only character-level Shakespeare run to a full multi-node GPT-2 reproduction via torchrun.
- ✓Uses PyTorch 2.0's torch.compile by default, which the README says cut iteration time from about 250ms to 135ms per step on its reference setup.
- ✓bench.py isolates the core training-loop math for benchmarking and profiling without the surrounding training complexity.
- ✓sample.py generates text from either a self-trained checkpoint or an OpenAI-released GPT-2 model, including gpt2-xl, from the command line.
Setting up nanoGPT for local use
Install the dependencies with a single pip command: `pip install torch numpy transformers datasets tiktoken wandb tqdm`. Per the README, transformers is only needed to load GPT-2 checkpoints from Hugging Face, datasets is only needed if you want to download and preprocess OpenWebText, wandb is optional logging, and tqdm just drives the progress bars. There's no separate setup.py step described in the README beyond that pip install line; you clone the repo and start running scripts like train.py directly.
Training a character-level GPT example
The fastest path in, per the README, is training a compact character-level GPT using text from Shakespeare's plays. Run `python data/shakespeare_char/prepare.py` to download a 1MB text file and turn it into train.bin and val.bin. With a GPU, `python train.py config/train_shakespeare_char.py` trains a 6-layer, 6-head Transformer with a 256-character context and 384 feature channels; on one A100 the README reports this finishes in about 3 minutes with a best validation loss of 1.4697. Sample from the result with `python sample.py --out_dir=out-shakespeare-char`. On a MacBook or other CPU-only machine, the README gives a scaled-down command (`--device=cpu --compile=False --block_size=64 --batch_size=12 --n_layer=4 --n_head=4 --n_embd=128 --max_iters=2000`) that also finishes in about 3 minutes but lands at a loss of 1.88; Apple Silicon users can add `--device=mps` for a reported 2-3x speedup. To reproduce GPT-2 (124M) on OpenWebText instead, run `python data/openwebtext/prepare.py` to tokenize the dataset, then launch `torchrun --standalone --nproc_per_node=8 train.py config/train_gpt2.py` on an 8xA100 40GB node; the README says this takes about 4 days and reaches a loss near 2.85. Finetuning a pretrained checkpoint reuses the same train.py entry point with a smaller learning rate, for example `python train.py config/finetune_shakespeare.py`, which the README says can finish in just a few minutes on a single GPU because the tiny Shakespeare dataset tokenizes in seconds rather than requiring the OpenWebText download.
Strengths
- ✓The entire model and training loop are short enough to read in one sitting: model.py and train.py are each roughly 300 lines, per the README.
- ✓Loads official OpenAI GPT-2 checkpoints directly for finetuning, up to the 1.3B-parameter gpt2-xl size, so you're not limited to training from scratch.
- ✓Documented, working configs cover a wide range of hardware, from a 3-minute CPU/MacBook demo to a full 8xA100 multi-node GPT-2 reproduction.
- ✓MIT license, so there are no restrictions on reuse in your own training code.
- ✓torch.compile is on by default and the README reports it cuts iteration time roughly in half on its reference benchmark, a real speedup rather than a claimed one.
Important considerations and project status
- △The README's biggest limitation is the project itself: as of the Nov 2025 update, nanoGPT is called 'very old and deprecated,' left up 'for posterity' rather than actively developed, with nanochat named as the intended replacement.
- △torch.compile, which the default config relies on for speed, is described in the README as 'fairly new and experimental,' and the README separately notes it isn't yet available on every platform, Windows included; the workaround is adding --compile=False, which the README says slows training down.
- △Reproducing GPT-2 (124M) from scratch needs an 8xA100 40GB node per the README, which is not hardware most individual developers have sitting around.
- △The README's own todo list, covering FSDP support, zero-shot perplexity evals, better finetuning hyperparameters, and rotary/alibi embeddings, was never marked complete, and with the project now deprecated there's no indication any of it still will be.
- △Multi-node training is documented as functional but the README itself warns it will 'most likely crawl' without Infiniband, so network setup matters more than the docs let on at a glance.
Recommended alternatives to nanoGPT
Common questions about nanoGPT
nanoGPT is not actively maintained anymore. The README states that as of the Nov 2025 update the project is 'very old and deprecated,' kept online 'for posterity' while development moved to a newer project called nanochat.
The nanoGPT README points readers to nanochat, calling it a 'new and improved cousin' and saying it's very likely what someone looking at nanoGPT actually meant to find.
nanoGPT's train.py can reproduce GPT-2 (124M) on OpenWebText. The README reports a resulting loss around 2.85, which lines up with an official GPT-2 checkpoint finetuned on the same dataset because of a documented domain gap between OpenWebText and the original WebText.
Hardware needs scale with the goal. nanoGPT's character-level Shakespeare demo trains in about 3 minutes on a single A100 GPU, or runs at reduced scale on CPU or Apple Silicon, while reproducing GPT-2 (124M) on OpenWebText needs at least an 8xA100 40GB node per the README.
nanoGPT is released under the MIT license, per its GitHub repository listing.
Training GPT-2 (124M) to reproduce the paper's results takes about 4 days on an 8xA100 40GB node using PyTorch Distributed Data Parallel, according to the nanoGPT README.
The problem it solves
Most repos aiming to teach GPT internals sacrifice speed for readability, and most repos built for real training speed bury the model behind abstraction layers you have to peel back to see what's happening. nanoGPT's README frames its own reason for existing around that gap: it's a rewrite of Karpathy's earlier minGPT project that trades some of minGPT's teaching clarity for raw training speed, keeping the model and training loop each around 300 lines while still being able to reproduce a full GPT-2 training run.
Best use cases
- •Learning how a GPT training loop actually works end to end, since model.py and train.py are each small enough per the README to read start to finish in one sitting.
- •Training a small character-level language model on your own text corpus using the Shakespeare workflow as a template, on a GPU, a CPU, or an Apple Silicon Mac.
- •Finetuning an existing OpenAI GPT-2 checkpoint (up to the 1.3B gpt2-xl size, per the README) on a narrow dataset with just a few minutes of GPU time.
- •Studying nanoGPT's design as historical context before moving to Karpathy's newer nanochat project, since the README itself frames nanoGPT as superseded.
Who should try it — and who should skip
Try nanoGPT if you want a self-contained codebase to read, modify, or fork for your own GPT training experiments, especially if you value being able to trace every line rather than importing a black-box library. Skip it if you're starting a new project today: the README itself says nanoGPT is deprecated and to look at nanochat instead, and skip it too if you don't have GPU access, since the character-level demo is the only path that comfortably runs on a CPU or Apple Silicon Mac.
Still deciding about nanoGPT?
One click hands the question to an AI along with this page — see what it says about nanoGPT.
