nanogpt

GitHub

提供极简 GPT 实现,用于学习 Transformer 架构。支持从训练、采样到微调的完整流程,适用于理解模型原理及实验。

01-model-architecture/nanogpt/SKILL.md Orchestra-Research/AI-Research-SKILLs

Trigger Scenarios

需要学习或理解 GPT/Transformer 内部机制 希望在 CPU 或小规模 GPU 上快速复现 GPT-2 或进行教学演示

Install

npx skills add Orchestra-Research/AI-Research-SKILLs --skill nanogpt -g -y
More Options

Non-standard path

npx skills add https://github.com/Orchestra-Research/AI-Research-SKILLs/tree/main/01-model-architecture/nanogpt -g -y

Use without installing

npx skills use Orchestra-Research/AI-Research-SKILLs@nanogpt

指定 Agent (Claude Code)

npx skills add Orchestra-Research/AI-Research-SKILLs --skill nanogpt -a claude-code -g -y

安装 repo 全部 skill

npx skills add Orchestra-Research/AI-Research-SKILLs --all -g -y

预览 repo 内 skill

npx skills add Orchestra-Research/AI-Research-SKILLs --list

SKILL.md

Frontmatter
{
    "name": "nanogpt",
    "tags": [
        "Model Architecture",
        "NanoGPT",
        "GPT-2",
        "Educational",
        "Andrej Karpathy",
        "Transformer",
        "Minimalist",
        "From Scratch",
        "Training"
    ],
    "author": "Orchestra Research",
    "license": "MIT",
    "version": "1.0.0",
    "description": "Educational GPT implementation in ~300 lines. Reproduces GPT-2 (124M) on OpenWebText. Clean, hackable code for learning transformers. By Andrej Karpathy. Perfect for understanding GPT architecture from scratch. Train on Shakespeare (CPU) or OpenWebText (multi-GPU).",
    "dependencies": [
        "torch",
        "transformers",
        "datasets",
        "tiktoken",
        "wandb"
    ]
}

nanoGPT - Minimalist GPT Training

Quick start

nanoGPT is a simplified GPT implementation designed for learning and experimentation.

Installation:

pip install torch numpy transformers datasets tiktoken wandb tqdm

Train on Shakespeare (CPU-friendly):

# Prepare data
python data/shakespeare_char/prepare.py

# Train (5 minutes on CPU)
python train.py config/train_shakespeare_char.py

# Generate text
python sample.py --out_dir=out-shakespeare-char

Output:

ROMEO:
What say'st thou? Shall I speak, and be a man?

JULIET:
I am afeard, and yet I'll speak; for thou art
One that hath been a man, and yet I know not
What thou art.

Common workflows

Workflow 1: Character-level Shakespeare

Complete training pipeline:

# Step 1: Prepare data (creates train.bin, val.bin)
python data/shakespeare_char/prepare.py

# Step 2: Train small model
python train.py config/train_shakespeare_char.py

# Step 3: Generate text
python sample.py --out_dir=out-shakespeare-char

Config (config/train_shakespeare_char.py):

# Model config
n_layer = 6          # 6 transformer layers
n_head = 6           # 6 attention heads
n_embd = 384         # 384-dim embeddings
block_size = 256     # 256 char context

# Training config
batch_size = 64
learning_rate = 1e-3
max_iters = 5000
eval_interval = 500

# Hardware
device = 'cpu'  # Or 'cuda'
compile = False # Set True for PyTorch 2.0

Training time: ~5 minutes (CPU), ~1 minute (GPU)

Workflow 2: Reproduce GPT-2 (124M)

Multi-GPU training on OpenWebText:

# Step 1: Prepare OpenWebText (takes ~1 hour)
python data/openwebtext/prepare.py

# Step 2: Train GPT-2 124M with DDP (8 GPUs)
torchrun --standalone --nproc_per_node=8 \
  train.py config/train_gpt2.py

# Step 3: Sample from trained model
python sample.py --out_dir=out

Config (config/train_gpt2.py):

# GPT-2 (124M) architecture
n_layer = 12
n_head = 12
n_embd = 768
block_size = 1024
dropout = 0.0

# Training
batch_size = 12
gradient_accumulation_steps = 5 * 8  # Total batch ~0.5M tokens
learning_rate = 6e-4
max_iters = 600000
lr_decay_iters = 600000

# System
compile = True  # PyTorch 2.0

Training time: ~4 days (8× A100)

Workflow 3: Fine-tune pretrained GPT-2

Start from OpenAI checkpoint:

# In train.py or config
init_from = 'gpt2'  # Options: gpt2, gpt2-medium, gpt2-large, gpt2-xl

# Model loads OpenAI weights automatically
python train.py config/finetune_shakespeare.py

Example config (config/finetune_shakespeare.py):

# Start from GPT-2
init_from = 'gpt2'

# Dataset
dataset = 'shakespeare_char'
batch_size = 1
block_size = 1024

# Fine-tuning
learning_rate = 3e-5  # Lower LR for fine-tuning
max_iters = 2000
warmup_iters = 100

# Regularization
weight_decay = 1e-1

Workflow 4: Custom dataset

Train on your own text:

# data/custom/prepare.py
import numpy as np

# Load your data
with open('my_data.txt', 'r') as f:
    text = f.read()

# Create character mappings
chars = sorted(list(set(text)))
stoi = {ch: i for i, ch in enumerate(chars)}
itos = {i: ch for i, ch in enumerate(chars)}

# Tokenize
data = np.array([stoi[ch] for ch in text], dtype=np.uint16)

# Split train/val
n = len(data)
train_data = data[:int(n*0.9)]
val_data = data[int(n*0.9):]

# Save
train_data.tofile('data/custom/train.bin')
val_data.tofile('data/custom/val.bin')

Train:

python data/custom/prepare.py
python train.py --dataset=custom

When to use vs alternatives

Use nanoGPT when:

  • Learning how GPT works
  • Experimenting with transformer variants
  • Teaching/education purposes
  • Quick prototyping
  • Limited compute (can run on CPU)

Simplicity advantages:

  • ~300 lines: Entire model in model.py
  • ~300 lines: Training loop in train.py
  • Hackable: Easy to modify
  • No abstractions: Pure PyTorch

Use alternatives instead:

  • HuggingFace Transformers: Production use, many models
  • Megatron-LM: Large-scale distributed training
  • LitGPT: More architectures, production-ready
  • PyTorch Lightning: Need high-level framework

Common issues

Issue: CUDA out of memory

Reduce batch size or context length:

batch_size = 1  # Reduce from 12
block_size = 512  # Reduce from 1024
gradient_accumulation_steps = 40  # Increase to maintain effective batch

Issue: Training too slow

Enable compilation (PyTorch 2.0+):

compile = True  # 2× speedup

Use mixed precision:

dtype = 'bfloat16'  # Or 'float16'

Issue: Poor generation quality

Train longer:

max_iters = 10000  # Increase from 5000

Lower temperature:

# In sample.py
temperature = 0.7  # Lower from 1.0
top_k = 200       # Add top-k sampling

Issue: Can't load GPT-2 weights

Install transformers:

pip install transformers

Check model name:

init_from = 'gpt2'  # Valid: gpt2, gpt2-medium, gpt2-large, gpt2-xl

Advanced topics

Model architecture: See references/architecture.md for GPT block structure, multi-head attention, and MLP layers explained simply.

Training loop: See references/training.md for learning rate schedule, gradient accumulation, and distributed data parallel setup.

Data preparation: See references/data.md for tokenization strategies (character-level vs BPE) and binary format details.

Hardware requirements

  • Shakespeare (char-level):

    • CPU: 5 minutes
    • GPU (T4): 1 minute
    • VRAM: <1GB
  • GPT-2 (124M):

    • 1× A100: ~1 week
    • 8× A100: ~4 days
    • VRAM: ~16GB per GPU
  • GPT-2 Medium (350M):

    • 8× A100: ~2 weeks
    • VRAM: ~40GB per GPU

Performance:

  • With compile=True: 2× speedup
  • With dtype=bfloat16: 50% memory reduction

Resources

Version History

  • 773a529 Current 2026-08-20 11:52

Dependencies

  • required torch
  • required transformers
  • required datasets
  • required tiktoken
  • required wandb

Same Skill Collection

01-model-architecture/litgpt/SKILL.md
01-model-architecture/mamba/SKILL.md
01-model-architecture/rwkv/SKILL.md
01-model-architecture/torchtitan/SKILL.md
02-tokenization/huggingface-tokenizers/SKILL.md
02-tokenization/sentencepiece/SKILL.md
03-fine-tuning/axolotl/SKILL.md
03-fine-tuning/llama-factory/SKILL.md
03-fine-tuning/peft/SKILL.md
03-fine-tuning/unsloth/SKILL.md
04-mechanistic-interpretability/nnsight/SKILL.md
04-mechanistic-interpretability/pyvene/SKILL.md
04-mechanistic-interpretability/saelens/SKILL.md
04-mechanistic-interpretability/transformer-lens/SKILL.md
05-data-processing/nemo-curator/SKILL.md
05-data-processing/ray-data/SKILL.md
06-post-training/grpo-rl-training/SKILL.md
06-post-training/miles/SKILL.md
06-post-training/openrlhf/SKILL.md
06-post-training/simpo/SKILL.md
06-post-training/slime/SKILL.md
06-post-training/torchforge/SKILL.md
06-post-training/trl-fine-tuning/SKILL.md
06-post-training/verl/SKILL.md
07-safety-alignment/constitutional-ai/SKILL.md
07-safety-alignment/llamaguard/SKILL.md
07-safety-alignment/nemo-guardrails/SKILL.md
07-safety-alignment/prompt-guard/SKILL.md
08-distributed-training/accelerate/SKILL.md
08-distributed-training/deepspeed/SKILL.md
08-distributed-training/megatron-core/SKILL.md
08-distributed-training/pytorch-fsdp2/SKILL.md
08-distributed-training/pytorch-lightning/SKILL.md
08-distributed-training/ray-train/SKILL.md
09-infrastructure/lambda-labs/SKILL.md
09-infrastructure/modal/SKILL.md
09-infrastructure/skypilot/SKILL.md
10-optimization/awq/SKILL.md
10-optimization/bitsandbytes/SKILL.md
10-optimization/flash-attention/SKILL.md
10-optimization/gguf/SKILL.md
10-optimization/gptq/SKILL.md
10-optimization/hqq/SKILL.md
10-optimization/ml-training-recipes/SKILL.md
11-evaluation/bigcode-evaluation-harness/SKILL.md
11-evaluation/lm-evaluation-harness/SKILL.md
12-inference-serving/llama-cpp/SKILL.md
12-inference-serving/sglang/SKILL.md
12-inference-serving/tensorrt-llm/SKILL.md

Metadata

Files
0
Version
773a529
Hash
8893d8dd
Indexed
2026-08-20 11:52

Home - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-21 07:07
浙ICP备14020137号-1 $Map of visitor$