nanogpt

Train and fine-tune GPT-2 models with a minimalist PyTorch implementation.

1|Updated Mar 5, 2026
One-click install
npx skills add https://github.com/Clay-HHK/claude-skills --skill nanogpt-clay-hhk
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: nanogpt
Source: https://github.com/Clay-HHK/claude-skills/tree/main/nanogpt
Command: npx skills add https://github.com/Clay-HHK/claude-skills --skill nanogpt-clay-hhk

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires torch, transformers, datasets, tiktoken, wandb, and includes references (resource) components.

What problem does it solve? Learning how GPT models work internally is difficult when production frameworks hide details behind heavy abstractions. This Skill provides a ~300-line GPT implementation that reproduces GPT-2 (124M) on OpenWebText, letting you understand, modify, and train transformers from scratch. ## Core Features & Use Cases - Character-Level Training: Train a small GPT on Shakespeare in about 5 minutes on CPU, then generate sample text. - GPT-2 Reproduction: Reproduce GPT-2 (124M) on OpenWebText using multi-GPU DDP training with torchrun. - Fine-Tuning Pretrained Models: Load OpenAI GPT-2 checkpoints (gpt2, gpt2-medium, gpt2-large, gpt2-xl) and fine-tune on custom datasets. - Use Case: A student learning transformers can prepare the Shakespeare dataset, train a 6-layer character-level model on CPU, and generate Shakespeare-style text within minutes, then scale up to GPT-2 reproduction on GPUs. ## Quick Start Train a small GPT model on the Shakespeare dataset using nanoGPT and generate sample text from the trained checkpoint.

Frequently Asked Questions about nanogpt

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I train a GPT model on Shakespeare with nanoGPT?▼

Run python data/shakespeare_char/prepare.py to create train.bin and val.bin, then run python train.py config/train_shakespeare_char.py. Training takes about 5 minutes on CPU or 1 minute on GPU, and you can generate text with python sample.py.

How to reproduce GPT-2 124M training on OpenWebText?▼

Prepare the data with python data/openwebtext/prepare.py, which takes 1-2 hours and produces about 9 billion tokens. Then launch multi-GPU training with torchrun --standalone --nproc_per_node=8 train.py config/train_gpt2.py, taking roughly 4 days on 8 A100 GPUs.

nanoGPT vs HuggingFace Transformers vs LitGPT for training?▼

nanoGPT is best for learning and experimentation since the entire model and training loop fit in ~300 lines each with no abstractions. Use HuggingFace Transformers for production with many model types, LitGPT for more architectures, or Megatron-LM for large-scale distributed training.

Can nanoGPT fine-tune pretrained OpenAI GPT-2 weights?▼

Yes, set init_from to gpt2, gpt2-medium, gpt2-large, or gpt2-xl in your config to load OpenAI weights automatically via the transformers library. Use a lower learning rate like 3e-5 and fewer iterations for fine-tuning on custom datasets.

Why does nanoGPT run out of CUDA memory during training?▼

CUDA OOM happens when batch size or context length exceeds GPU memory. Reduce batch_size to 1, lower block_size from 1024 to 512, and increase gradient_accumulation_steps to maintain the effective batch size.

Does nanoGPT support CPU-only training?▼

Yes, character-level Shakespeare training runs on CPU in about 5 minutes with the default small config of 6 layers and 384 embedding dimensions. Set device to cpu and compile to False in the config for CPU training.