To train a small LLM from scratch, pick a tiny transformer, from a few million to a few hundred million parameters, feed it a clean dataset such as TinyStories or a sample of FineWeb-Edu, and run a simple training script like Andrej Karpathy's nanoGPT or its successor nanochat. We ran nanoGPT's CPU example on November 19, 2026: a 0.8 million parameter model learned to write Shakespeare style text in under a minute on an 8 core cloud server with no GPU. A GPT-2-class model with general knowledge is a bigger job: nanochat's reference run takes about two hours on a rented server with eight H100 GPUs (about 48 dollars), and its README says the speedrun script takes about 1.5 hours. Training from scratch is a superb way to learn how language models work, but if you want a useful assistant for a task, fine tuning an existing model is almost always the better choice. Once it trains, you can study what it learned with mechanistic interpretability tools.
What is Megatron-LM? Megatron-LM is NVIDIA's open source (Apache 2.0) codebase for pretraining transformer models with billions of parameters across many NVIDIA GPUs, built on the Megatron Core library. Use it when one GPU or one simple script is no longer enough; for a first small model, nanochat is the better start. The Megatron section below covers versions, install and how it compares with DeepSpeed.
Should you train from scratch at all?
| Goal | Best approach |
|---|---|
| Learn how LLMs really work | Train a small model from scratch |
| Research on architectures, optimizers or data | Train small models from scratch and compare |
| A model that follows instructions for your business | Fine tune an open weight model |
| A model for an unusual language or domain with lots of data | Consider continued pretraining of an existing model first |
The reason is cost. Modern small models are trained on enormous amounts of text: Hugging Face's SmolLM3, a 3 billion parameter model, was trained on 11 trillion tokens, according to its GitHub repository. You will not match that at home, and you do not need to in order to learn.
The three sizes that make sense
1. A tiny story model (laptop or single GPU)
The TinyStories paper showed that models with fewer than 10 million parameters, trained on a synthetic dataset of simple short stories, can produce fluent, consistent stories with nearly perfect grammar. The dataset is free on Hugging Face under a CDLA-Sharing license. This is the best first project: quick to train, and you can see the model improve.
2. A GPT-1 or GPT-2 small sized model (one good GPU, or a short cloud rental)
Models from about 100 to 200 million parameters trained on a few billion tokens of filtered web text start to produce plausible general English. FineWeb-Edu publishes ready made random samples of about 10, 100 and 350 billion tokens, so you can download the 10 billion token sample instead of the full 1.3 trillion. If you only need a small model to use rather than to train, compare the ready made options in SmolLM vs Llama first.
3. A GPT-2-grade chat model (an eight GPU node for a few hours)
This is nanochat's target: a model that beats the original GPT-2's score on the DCLM CORE benchmark, then goes through fine tuning so you can chat with it. Its README puts the cost near 48 dollars (15 on a spot instance), against roughly 43,000 dollars to train GPT-2 in 2019.
nanoGPT vs nanochat: which should you use?
nanoGPT was the go to repository for years: a training loop of about 300 lines and a GPT model file of about 300 lines, MIT licensed, whose train.py reproduces GPT-2 (124M) on OpenWebText in about four days on one node with eight A100 40 GB GPUs. Since November 2025 its README calls it "very old and deprecated" and points to its successor, so use nanoGPT to read and tinker, and nanochat for a full modern run. (NanoGPT, the pay as you go website that offers many commercial chat models through one API, is an unrelated product.)
Our nanoGPT test on a CPU
We ran the README's CPU example on November 19, 2026: character level Shakespeare (about 1 million training characters, 65 symbol vocabulary), 4 layers, 4 heads, 128 dimensions, context of 64 characters, batch of 12 and 2,000 iterations, using PyTorch 2.15.0 on an 8 vCPU Linux cloud server with no GPU.
| Result | Value |
|---|---|
| Parameters | 0.80 million |
| Wall time for 2,000 iterations | 35 to 54 seconds (two runs, shared server) |
| Validation loss at step 0 / 1,000 / 2,000 | 4.16 / 2.05 / 1.89 |
| Sample | Play script layout with names such as "KING EDWARD IV:" and mostly invented words |
The README predicts about three minutes and a loss of about 1.88 for this setup, so a modern server CPU is enough to watch a language model learn. It is a toy: the output looks like Shakespeare only from a distance.
nanochat, the successor
nanochat covers the whole pipeline in one small codebase: training a tokenizer, pretraining, fine tuning, evaluation, and a chat interface. Its design has a single main dial, the depth of the transformer, which sets the width, learning rates and training length automatically so the model comes out compute optimal.
Useful details from the nanochat README:
- It runs on a single GPU by dropping
torchrun; results are nearly identical but take about eight times longer. - On GPUs with less than 80 GB, lower the
--device-batch-sizesetting until it fits. - A
runcpu.shscript trains a heavily shrunk model on a CPU or Apple Silicon in a few tens of minutes, though the results are weak. - It is mostly plain PyTorch, so other accelerators may work, with some rough edges.
What is the nanoGPT speedrun?
Two leaderboards race to train GPT-2 class models on eight H100 GPUs. The modded-nanogpt "NanoGPT speedrun" times how fast a model reaches 3.28 validation loss on FineWeb: the llm.c baseline took 45 minutes in May 2024, and record 92 (August 30, 2026) took 0.665 minutes, under 40 seconds. nanochat's own "time to GPT-2" table targets GPT-2's CORE score instead; its best entry is 1.65 hours (March 14, 2026), against 168 hours for the original 2019 model.
For a full comparison of open corpora (FineWeb-Edu, DCLM, Nemotron-CC, Dolma and more), see our guide to the best LLM pretraining datasets. For context on data: the FineWeb dataset card says FineWeb is cleaned and deduplicated English web text from Common Crawl dumps since 2013, released under ODC By. For image and text pairs, the nonprofit LAION publishes open multimodal datasets and tools.
When to use Megatron-LM instead
nanochat is built to teach. When you need to pretrain a model with billions of parameters across many GPUs, the usual open source choice is NVIDIA's Megatron-LM repository. Its README describes two parts: Megatron-LM, a reference with ready training scripts for research and learning distributed training, and Megatron Core, a composable library of GPU optimized building blocks for custom training frameworks.
Facts from the README, GitHub and PyPI, rechecked November 19, 2026:
- Megatron Core supports tensor, pipeline, data, expert and context parallelism (TP, PP, DP, EP and CP), with FP16, BF16, FP8 and FP4 mixed precision.
- The latest
megatron-coreon PyPI is 0.20.0 (October 29, 2026), which needs Python 3.12 or newer and fully supports DeepSeek V4 through its new HybridModel class. Install it withuv pip install megatron-core, or clone the repository for the training scripts. - The README now quotes results from NVIDIA's August 2026 NeMo container, such as DeepSeek-V3 on 256 GB300 GPUs in MXFP8 at 1,636 model TFLOPs per second per GPU. These are NVIDIA's own benchmark numbers.
- The project began with the 2019 Megatron-LM paper, which introduced tensor parallelism that splits each transformer layer across GPUs; a March 2026 report on MoE training with Megatron Core covers mixture of experts.
- AMD maintains a ROCm fork at ROCm/Megatron-LM for Instinct GPUs, still updated in November 2026, and Megatron Bridge converts checkpoints between Hugging Face and Megatron formats.
Megatron-LM is NVIDIA GPU software and assumes a cluster, so it is overkill below a few hundred million parameters. For a mid point, DeepSpeed (v0.20.0, November 12, 2026) adds ZeRO memory sharding to ordinary PyTorch code. To see how weights, gradients and optimizer state add up, our VRAM guide explains the inference side of the same arithmetic. For fine tuning an existing model instead, see how to fine tune an LLM locally, and for how frameworks compare, our best ML frameworks for Python guide.
How much compute do you need?
A widely used rule of thumb from OpenAI's scaling laws paper is that training costs about 6 × parameters × tokens floating point operations. DeepMind's Chinchilla paper found that, for a fixed budget, model size and training tokens should grow together; its 70 billion parameter Chinchilla model was trained on 1.4 trillion tokens, about 20 tokens per parameter.
Worked example: a 124 million parameter model trained on 2.5 billion tokens, roughly 20 tokens per parameter, needs about 6 × 124 million × 2.5 billion, or roughly 1.9 × 10 to the 18th operations. At an assumed effective 50 trillion operations per second (measure your own GPU), that is about 10 to 11 hours. Small models are often trained on far more than 20 tokens per parameter today, because a smaller, longer trained model is cheaper to run afterwards.
Memory is rarely the problem at this scale. Training needs memory for the weights, the gradients, and the optimizer state, which for the common AdamW optimizer is two extra values per parameter, plus activations. A 124 million parameter model fits easily on a 12 to 24 GB GPU with a modest batch size.
Step by step
- Set up. Install PyTorch with GPU support, then clone nanochat and install its dependencies with
uv, as its README describes. Still choosing a framework? See PyTorch vs JAX or TensorFlow vs PyTorch vs Keras. - Pick data. Start with TinyStories for a story model, or a FineWeb-Edu sample from FineWeb for general text. Load either with Hugging Face Datasets.
- Tokenize. Train or reuse a byte pair encoding tokenizer. A smaller vocabulary keeps tiny models efficient.
- Choose a size. For a first run, 6 to 12 layers is plenty. nanochat's author suggests a 12 layer model for quick five minute experiments on an eight GPU node, which means much longer on one GPU.
- Train and watch the loss. Log validation loss with a tool like Weights & Biases. If validation loss stops falling while training loss keeps dropping, you are overfitting or out of data.
- Sample often. Watching gibberish turn into sentences is the fun part.
- Fine tune for chat (optional). nanochat includes supervised fine tuning and a reinforcement learning stage; our GRPO vs PPO guide explains the RL methods used for that step.
Common mistakes
- Too little data for the model size. A big model on a small dataset just memorizes it.
- Judging too early or changing many things at once. Loss curves are noisy; compare runs at the same number of tokens, one change at a time.
- Skipping evaluation. Keep a held out validation set from the start.
Pros and cons of training from scratch
- Pros: deep understanding of every stage; full control over data and architecture; a fully open model you can describe completely, as our open weight vs open source guide discusses, in the spirit of OLMo, which publishes its data and training code.
- Cons: small home trained models are far weaker than released open weight models; serious runs need rented GPUs; data preparation takes real effort.
FAQ
Is nanoGPT still worth using in 2026?
Yes, as a teaching codebase. Its two short files are still the easiest way to read a whole GPT training loop, and its CPU example ran in under a minute in our test. Its author marks it deprecated, though, so for a full pipeline with tokenizer, fine tuning and chat, start from nanochat.
How long does nanoGPT take to train?
It depends on the size. The tiny character level Shakespeare model took 35 to 54 seconds on our 8 vCPU server. Reproducing GPT-2 (124M) with nanoGPT's train.py takes about four days on eight A100 40 GB GPUs, while the modded-nanogpt speedrun record reaches a similar loss in under 40 seconds on eight H100s.
Megatron-LM vs DeepSpeed: which should I use?
DeepSpeed's ZeRO shards optimizer state, gradients and weights across data parallel GPUs and wraps an ordinary PyTorch model, so it is easier to add to existing code. Megatron Core splits the model itself with tensor, pipeline, expert and context parallelism and is tuned for large NVIDIA clusters. For a few GPUs and an existing PyTorch model, try DeepSpeed first; for pretraining tens or hundreds of billions of parameters, Megatron is the usual choice.
Can I train an LLM from scratch on my laptop?
Yes, a very small one. Our 0.8 million parameter nanoGPT run needed no GPU, a model with a few million parameters trained on TinyStories is practical on a recent laptop or Mac, and nanochat includes a CPU and Apple Silicon script. Expect simple stories, not a general assistant.
How much does it cost to train a GPT-2 level model?
nanochat's README estimates about 48 dollars for its reference run, roughly two hours on an eight-H100 node at about 3 dollars per GPU hour, and less on spot instances. Prices vary by provider.
Should I train from scratch or fine tune?
Fine tune if you want a capable model for a task; it is cheaper and far stronger. Train from scratch to learn, to do research, or for complete control over the training data. If you have no GPU, see how to fine tune an LLM without a GPU.



