Skip to main content
Primary: Phase 5Used in Phases 5, 6GitHub RepositoryAdvancedFree Learning Material
Andrej Karpathy

nanoGPT

The simplest, fastest, and most readable open-source repository for training and fine-tuning medium-sized GPT language models (~300 lines of model code and ~300 lines of training loop).

Andrej Karpathy's `nanoGPT` strips away framework bloat and implements the complete GPT-2 architecture in a single readable `model.py` file (~300 lines) and a production-capable distributed mixed-precision training loop in `train.py` (~300 lines). It serves as both a Phase 5 architectural blueprint and a Phase 6 pretraining reference implementation.

Resource Tracking

Course status is tracked independently from roadmap topic checkboxes so you can mark external courses without altering your topic mastery percentage.

Suggested Learning Position

Reference Codebase for Phase 5 (`model.py`) and Phase 6 (`train.py`)

Language:English
Access & Link Verification

100% free MIT-licensed open-source GitHub repository.

Status: Verified (2026-11-09)

What You Will Learn from This Resource

  • Read and write a complete GPT decoder architecture (`CausalSelfAttention`, `MLP`, `Block`, `GPT`) in ~300 lines of clean PyTorch.
  • Configure AdamW parameter groups (separating weight-decayed 2D matrices from 1D biases/LayerNorms).
  • Implement mixed-precision training (`torch.amp.autocast`), gradient clipping, and memory-mapped dataset loading.

Recommended Prerequisites

Review these foundations before starting so you get the most out of this material:

  • PyTorch fundamentals and understanding of causal self-attention (Phases 3–5).

Topics & Keywords

nanoGPTAndrej KarpathyGPT-2PyTorchPretrainingGitHub Repository
Curriculum Integration

How This Resource Fits Into Your AI & LLM Roadmap

Open Interactive Roadmap

Phase-Specific Study Recommendations (2)

Phase 5 · Resource 5.4Practice & Reference

nanoGPT

In Phase 5: Transformers From Scratch

Study `model.py` line by line while building Milestone #10 (Mini GPT Language Model) in Modules 5.2–5.5.

Phase 6 · Resource 6.3Practice & Reference

nanoGPT Training Implementation

In Phase 6: Train Your Own LLM

Study `train.py` and `data/openwebtext/prepare.py` during Modules 6.1–6.3 to see how memory-mapped `.bin` token shards, gradient accumulation, BF16 autocast, cosine learning rate warmup, and DDP work in practice.

Directly Associated Roadmap Modules (7)

Module 5.2Phase 5

Transformer Components

Multi-head attention, positional representations (sinusoidal, learned, RoPE), FFN blocks (GELU, SwiGLU), Pre-LN LayerNorm & RMSNorm, residual connections, weight tying, and initialization.

Inspect module checklist →
Module 5.3Phase 5

Transformer Architectures

Comparing Encoder-only (BERT), Decoder-only (GPT/LLaMA), and Encoder-Decoder (T5) architectures, attention masking patterns, and block stacking.

Inspect module checklist →
Module 5.4Phase 5

Implement GPT Architecture

Hands-on PyTorch implementation of a complete GPT-style language model from scratch—from embeddings and attention blocks to loss calculation, training loop, and generation.

Inspect module checklist →
Module 5.5Phase 5

Text Generation

Decoding strategies (greedy, temperature, top-k, top-p nucleus sampling), context window limits, stopping criteria, KV caching, and repetition mitigation.

Inspect module checklist →
Module 6.2Phase 6

Pretraining Fundamentals

Random weight initialization, token-budget batch sizing, gradient accumulation, AdamW decoupled weight decay, warmup-cosine schedules, gradient clipping, and fault-tolerant checkpointing.

Inspect module checklist →
Module 6.3Phase 6

Train Models of Increasing Size

Progressive pretraining experiments across Model A (100K–1M params), Model B (5M–20M params), and Model C (30M–100M params), tailored to your available hardware.

Inspect module checklist →
Module 6.5Phase 6

Model Packaging

Serializing model weights, tokenizer artifacts, configuration JSONs, model cards, and standalone CLI inference scripts for reproducible distribution.

Inspect module checklist →
Continue Exploring
Browse Full Library
Phases 3, 4, 5Recommended StartVideo Series
Andrej Karpathy
IntermediateFree Learning Material

Neural Networks: Zero to Hero

A code-first introduction to neural networks covering automatic differentiation, backpropagation, language models, and GPT implementations.

Used across:Phase 3 (3.1)Phase 4 (4.3)Phase 5 (5.1)
Andrej KarpathyMicrogradMakemoreBackpropagationAutograd
Your Status:
Phases 5, 6Video Lecture
Andrej Karpathy
AdvancedFree Learning Material

Let's Build GPT: From Scratch

Standalone 2-hour hands-on video lecture building and training a generative pretrained Transformer (GPT) from an empty Python script.

Used across:Phase 6 (6.2)
Andrej KarpathyGPTVideo LecturePretrainingPyTorch
Your Status:
Phases 5, 6, 8Recommended StartUniversity Course Materials
Stanford University
AdvancedFree Learning Material

CS336 — Language Modeling from Scratch

Graduate-level Stanford course walking through every aspect of building language models from scratch: Transformer architecture, tokenization, pretraining data pipelines, GPU systems, parallelism, and scaling laws.

Used across:Phase 5 (5.2)Phase 6 (6.1)Phase 8 (8.1)
StanfordCS336Language ModelingPretrainingScaling Laws
Your Status: