Description
nanoGPT is a GitHub repository designed to be the simplest and fastest way to train and fine-tune medium-sized Generative Pre-trained Transformer (GPT) models. Developed by Andrej Karpathy, it prioritizes clarity and efficiency, making it an excellent resource for both beginners and experienced deep learning practitioners.
The core philosophy behind nanoGPT is to provide a clean, understandable codebase that allows users to quickly grasp the fundamentals of GPT training. The repository includes a concise training script (around 300 lines) and a GPT model definition (also around 300 lines), which can optionally load weights from OpenAI's GPT-2 checkpoints. This simplicity facilitates easy modification and experimentation.
Key capabilities of nanoGPT include the ability to train models from scratch or fine-tune existing pre-trained checkpoints. The repository provides examples for reproducing GPT-2 (124M parameters) on datasets like OpenWebText, demonstrating its effectiveness. It supports training on single GPUs, multi-GPU setups, and even CPU-only environments, with specific configurations for different hardware capabilities.
nanoGPT is targeted at AI researchers, machine learning engineers, and students who want to delve into the practical aspects of training large language models. Its straightforward implementation makes it ideal for educational purposes, allowing users to understand the inner workings of GPTs without being overwhelmed by complex frameworks. The value proposition lies in its accessibility, speed, and the ability to achieve significant results with relatively modest computational resources.
The repository also includes scripts for data preparation, sampling from trained models, and benchmarking. It emphasizes the use of modern PyTorch features for performance optimization, such as `torch.compile()`. While nanoGPT itself is now considered deprecated in favor of newer projects like nanochat, it remains a valuable resource for understanding foundational GPT training techniques.
nanoGPT's Core Features
Minimalist and fast repository for training/finetuning GPTs
Simple and readable codebase for understanding GPT architecture
Reproduces GPT-2 (124M) performance on OpenWebText
Supports training from scratch or fine-tuning pre-trained models
Includes scripts for data preparation, training, and sampling
Optimized for performance with PyTorch 2.0 features
Runnable on single GPUs, multi-GPU nodes, and CPUs
Facilitates experimentation with hyperparameters and model sizes
Can load OpenAI GPT-2 checkpoints
Provides examples for character-level GPT training
Includes benchmarking script for model performance analysis
Getting Started with nanoGPT
Clone: Obtain the nanoGPT repository from GitHub.
Install: Set up the necessary Python dependencies using pip.
Prepare Data: Run data preparation scripts for your chosen dataset (e.g., Shakespeare, OpenWebText).
Configure: Adjust training parameters in configuration files (e.g., batch size, learning rate, model size).
Train: Execute the training script with your configuration.
Sample: Generate text from your trained model using the sampling script.
Finetune: Initialize training from a pre-trained checkpoint with a smaller learning rate.
nanoGPT's Use Cases
- GPT Model Training
- Language Model Research
- Reproducing Research
- Educational Tool
- Text Generation
- Model Fine-tuning








