Skip to content
FossShelf
Home / Categories / For AI Engineers

Nanochat

Minimal LLM training toolkit for GPT-scale models under $100.

Replaces —

60.61K starsMITreviewed 3 Nov 2026last commit 7 Sept 2026
Screenshot of Nanochat

Who it's for

ML researchers reproducing GPT-scale training

Nanochat's speedrun script packages the full recipe to train a GPT-2-class model in one shell script. Researchers run runs/speedrun.sh on an 8-GPU node to reproduce the baseline, then modify individual stages (data pipeline, optimizer, architecture) to study their effect on the CORE metric and validation bits-per-byte. The leaderboard tracks which changes improve time-to-GPT-2.

You need a model larger than GPT-2 scale (depth 26 equivalent). Nanochat targets micro models accessible for under $1,000; larger model training requires elastic multi-node distributed training, which nanochat does not support.

Engineers fine-tuning a base model for specific tasks

After pretraining, nanochat's SFT and RL scripts (chat_sft.py, chat_rl.py) let you fine-tune on custom datasets to add task-specific abilities. The codebase includes a Python code execution tool (execution.py) as an example of an added ability. Evaluation tasks covering MMLU, GSM8K, ARC, and HumanEval let you measure progress.

You want to adapt an existing large foundation model (LLaMA, Mistral, Gemma, etc.) using LoRA or PEFT. Nanochat trains from scratch; for fine-tuning pretrained models, tools like HuggingFace trl or Unsloth are a better fit.

Scaling-laws researchers running depth sweeps

The runs/scaling_laws.sh and runs/miniseries.sh scripts generate a series of compute-optimal models at different depths. Setting depths from 6 to 26 produces models at GPT-1 through GPT-2 scale with automatically tuned hyperparameters. Wandb integration tracks validation bits-per-byte (val_bpb), CORE score, VRAM utilization, and model FLOPS utilization across the sweep.

You need multi-node scaling data beyond a single 8-GPU server. Nanochat runs on a single node only; scaling-laws studies requiring hundreds of GPUs or petaFLOP budgets need a different training framework.

The problem

Training a language model from scratch has historically required either a large research lab budget or deep distributed-training expertise. The original GPT-2 training run cost approximately $43,000 in 2019, and reproduction efforts faced a wall of framework complexity, with giant configuration objects, opaque model factories, and dozens of hyperparameters that interact in non-obvious ways. For researchers and engineers who want to study how each training stage affects model behavior, existing tools present a difficult tradeoff. Large, heavily abstracted frameworks hide the training loop behind configuration APIs. Low-level PyTorch requires writing the entire pipeline from scratch. Neither supports fast iteration on individual stages.

How it works

Single --depth complexity dial

Training a compute-optimal model requires setting only --depth (the number of transformer layers). Nanochat automatically derives transformer width, number of attention heads, learning rate schedule, training horizon, and weight decay from that single integer. GPT-2-class models land around depth 24-26; researchers can sweep depths from 12 upward to produce the full miniseries of compute-optimal models at various sizes.

Full pipeline from tokenization to RL inference

The codebase covers every major LLM stage in single, readable Python files. BPE tokenizer training lives in tok_train.py, base model pretraining in base_train.py, supervised finetuning in chat_sft.py, reinforcement learning in chat_rl.py, and CLI inference in chat_cli.py. Each stage is independently runnable without touching the others.

Explicit mixed-precision control via COMPUTE_DTYPE

Precision is managed through a global COMPUTE_DTYPE environment variable rather than torch.amp.autocast. On H100/A100 hardware the default is bfloat16 compute with fp32 weights stored for optimizer precision. Embeddings store in COMPUTE_DTYPE to reduce memory. Override per-run with the NANOCHAT_DTYPE environment variable without touching model code.

GPT-2 speedrun leaderboard

Nanochat maintains a public leaderboard for the wall-clock time needed to train a model to GPT-2 CORE score (0.2565) on an 8xH100 node. The current record is 1.65 hours, down from the initial 3.04-hour baseline in January 2026. The runs/speedrun.sh script always reflects the reference training recipe, giving contributors a reproducible baseline.

Distributed training with automatic single-GPU fallback

Multi-GPU runs use torchrun with all-reduce for gradient synchronization across 8xH100 or 8xA100 nodes. Omitting torchrun switches the code automatically to gradient accumulation on a single GPU, producing approximately identical results at 8 times the wall-clock time. No code changes are required to switch between the two modes.

Features

  • Single --depth complexity dial
  • Full pipeline from tokenization to RL inference
  • Explicit mixed-precision control via COMPUTE_DTYPE
  • GPT-2 speedrun leaderboard
  • Distributed training with automatic single-GPU fallback

How to install

Install nanochat by cloning the repository and syncing dependencies with uv on a CUDA-capable GPU node.

git clone https://github.com/karpathy/nanochat
cd nanochat
uv sync --extra gpu
source .venv/bin/activate

Recommended for this setup

Strengths

  • GPT-2-class training for under $100: GPT-2-class capability (CORE score above 0.256525) costs approximately $48 on an 8xH100 node, about 2 hours at current GPU rates. On spot instances the cost drops to around $15. The original 2019 OpenAI GPT-2 training run cost approximately $43,000; nanochat achieves equivalent results through modern hardware efficiency, improved data quality (NVIDIA ClimbMix), and optimizer advances including the Muon optimizer.
  • Minimal, auditable codebase: The core library (nanochat/) is twelve files, with no giant configuration objects or model factory abstractions. Every training stage is a single Python script under scripts/. A researcher can read the entire codebase in a session and understand every architectural decision, from the custom AdamW + Muon optimizer in optim.py to the KV-cache inference engine in engine.py.
  • Compute-optimal hyperparameter scheduling: Setting one integer (--depth) produces a model where all other hyperparameters (transformer width, head count, learning rate schedule, training horizon, weight decay) are automatically tuned for compute-optimal results. This means any candidate improvement to the codebase is principled enough to work across all model sizes, not just GPT-2 scale.

Trade-offs

  • Requires a dedicated 8xH100 or 8xA100 node for the full run: The full GPT-2 speedrun requires an 8xH100 or 8xA100 GPU node with 80GB VRAM per GPU. Single-GPU runs work but take roughly 8 times longer (about 12-16 hours). Machines with less than 80GB VRAM per GPU need a reduced --device-batch-size, which can affect training quality. CPU and MPS paths are available but will not produce GPT-2-grade results in reasonable time.
  • No hosted version and no production serving infrastructure: Nanochat covers training and CLI inference only. There is no REST API server, no model-serving layer, and no checkpointing-resume from arbitrary hardware failures. Teams who want to deploy a trained model into a production serving stack will need to export the checkpoint and set up inference separately. The model architecture is a standard GPT transformer, so it can be loaded with custom serving code.
  • Active development with evolving training defaults: With 28 open issues and commits as recent as September 2026, nanochat is under active development. Training scripts and hyperparameter defaults in the reference speedrun have changed multiple times since the initial October 2025 launch. Researchers who build on nanochat should pin to a specific commit to ensure reproducibility.

Tech stack

Languages
Python, Jupyter Notebook, Shell
Frameworks
—
Databases
—
Tooling
—

FAQ

How much does it cost to train a GPT-2-class model with nanochat?

Approximately $48 on an 8xH100 node (about 2 hours at roughly $24/hr for the node), or closer to $15 on spot instances. The original 2019 OpenAI GPT-2 training cost approximately $43,000; the cost reduction comes from both improved hardware efficiency and seven years of algorithmic advances. The runs/speedrun.sh script reflects the current reference recipe.

What GPU hardware does nanochat require?

The full GPT-2 speedrun requires an 8xH100 or 8xA100 node with 80GB VRAM per GPU. Single-GPU runs work with the same code by omitting torchrun; results are approximately identical but take about 8 times longer. GPUs with less than 80GB VRAM need a reduced --device-batch-size. CPU and Apple Silicon (MPS) paths are available for small experiments but will not produce GPT-2-grade results.

Does nanochat support fine-tuning existing models like LLaMA or Mistral?

No. Nanochat trains models from scratch, starting with pretraining on raw text data. It does not provide a path for loading and fine-tuning existing HuggingFace checkpoints. For LoRA or PEFT fine-tuning of existing foundation models, other tools such as the HuggingFace trl library are a better fit.

Can nanochat run on a single GPU?

Yes. Omitting torchrun from the training command switches the code automatically to gradient accumulation on a single GPU. Results are approximately identical to multi-GPU runs, but training takes about 8 times longer. For the GPT-2 speedrun, that means roughly 12-16 hours instead of 1.5-2 hours. Less than 80GB VRAM requires reducing --device-batch-size.

What license does nanochat use and what can I do with it?

MIT license. You can use, modify, distribute, and build commercial products on top of the code without restriction. The only requirement is preserving the license text and copyright notice. Academic use has a BibTeX citation in the README for attribution.

Similar tools

More in AI Engineers