Source

GitHub · nanoGPT: The simplest, fastest repository for training/finetuning medium-sized GPTs.

Open the source

Karpathy presents nanoGPT as a minimal, readable, and hackable repository for training or finetuning medium-sized GPTs, while noting that it is now deprecated in favor of nanochat. He teaches practical workflows for starting with a small character-level model, scaling configurations to available hardware, reproducing GPT-2, finetuning pretrained checkpoints, sampling outputs, and troubleshooting performance or compatibility issues.

- nanoGPT favors a compact, transparent implementation that can be readily modified for new training and finetuning needs. - A character-level Shakespeare model provides a fast introductory workflow: prepare data, train a model, and sample from its best checkpoint. - Training settings should be reduced for CPU use or expanded when faster accelerators and more memory are available. - Reproducing GPT-2 124M requires tokenized OpenWebText, substantial multi-GPU compute, and comparison against baselines that account for dataset domain gaps. - Finetuning follows the normal training workflow but starts from pretrained weights, uses a smaller learning rate, and runs for less time. - PyTorch compilation can improve efficiency, but disabling it is a practical troubleshooting step on unsupported platforms.

Based on Andrej Karpathy's public content. Not affiliated with or endorsed by Andrej Karpathy.

Supports these rules

Quotes from this video

“The code itself is plain and readable: `train.py` is a ~300-line boilerplate training loop and `model.py` a ~300-line GPT model definition, which can optionally load the GPT-2 weights from OpenAI.”

“Because the code is so simple, it is very easy to hack to your needs, train new models from scratch, or finetune pretrained checkpoints (e.g. biggest one currently available as a starting point would be the GPT-2 1.3B”

“Have a look at all of its args, the script tries to be very readable, hackable and transparent.”

“The size of it very much depends on the computational resources of your system:”

“Here, since we are running on CPU instead of GPU we must set both `--device=cpu` and also turn off PyTorch 2.0 compile with `--compile=False`.”

“If you're willing to wait longer, feel free to tune the hyperparameters, increase the size of the network, the context length (`--block_size`), the length of training, etc.”

“If you are not a deep learning professional and you just want to feel the magic and get your feet wet, the fastest way to get started is to train a character-level GPT on the works of Shakespeare.”

“First, we download it as a single (1MB) file and turn it from raw text into one large stream of integers:”

“So once the training finishes we can sample from the best model by pointing the sampling script at this”

“Indeed, taking the GPT-2 (124M) checkpoint and finetuning on OWT directly for a while reaches loss down to ~2.85.”

“This then becomes the more appropriate baseline w.r.t. reproduction.”

“finetuning Finetuning is no different than training, we just make sure to initialize from a pretrained model and train with a smaller learning rate.”

“Basically, we initialize from a GPT2 checkpoint with `init_from` and train as normal, except shorter and with a small learning rate.”

“If you're running into related error messages try to disable this by adding `--compile=False` flag.”

“this by adding `--compile=False` flag. This will slow down the code but at least it will run.”

Topics