← Course home

Lesson 05 of 07

What PyTorch is doing for you

llm.c trains GPT-2 in C and CUDA, so you can see the work that remains when the Python framework is gone.

Agent brief (llms.md)

nanoGPT still sits on PyTorch. Hao's next question is what PyTorch is doing for you. llm.c answers that by training GPT-2 in plain C and CUDA. The README's aim is pretraining, in particular reproducing the GPT-2 and GPT-3 miniseries, with a parallel PyTorch reference in train_gpt2.py.

You do not need to become a CUDA programmer in this lesson. You need to see that the work is still there when the Python framework is gone: memory layout, kernels for each layer, a tokenizer, a data loader, an optimizer, and checks against a known PyTorch run.

One C file as the reference

The README points to a simple CPU float32 reference in about 1,000 lines of clean code, in one file, train_gpt2.c. The current file is 1,182 lines in total, or about 895 lines if you skip blanks and comments. The "about 1,000 lines" figure is the repo's own wording, and it is close.

The fast path is CUDA, in train_gpt2.cu and related files. There is also a frozen, simpler fp32 CUDA path for people who want to learn CUDA without mixed precision or multi-node training. Tests such as test_gpt2 load a debug state written by the PyTorch script, run a forward pass, compare logits and loss, then run a few Adam steps and check that the losses still match.

flowchart LR
  PT["train_gpt2.py in PyTorch"] --> Debug["Debug state: batch, activations, gradients"]
      
flowchart LR
  PT["train_gpt2.py in PyTorch"] --> Debug["Debug state"]
  Debug --> C["train_gpt2.c on CPU"]
  C --> Match["Logits, loss, and a few Adam steps match"]
      
flowchart LR
  PT["train_gpt2.py in PyTorch"] --> Debug["Debug state"]
  Debug --> C["train_gpt2.c on CPU"]
  Debug --> CU["train_gpt2.cu on GPU"]
  C --> Match["Numbers match the PyTorch reference"]
  CU --> Match
      

What you can run

If you have no GPU, you can still build the CPU program and follow the README's Shakespeare fine-tune demo. That path downloads a starter pack of .bin files, or you can rebuild them with the Python scripts. The README shows this finishing 40 steps on an M3 Max, which is a demo and a reference, not a serious pretraining run.

git clone https://github.com/karpathy/llm.c.git
cd llm.c
chmod u+x ./dev/download_starter_pack.sh
./dev/download_starter_pack.sh
make train_gpt2
OMP_NUM_THREADS=8 ./train_gpt2

If you do have an NVIDIA GPU, the README's "quick start (1 GPU, fp32 only)" and the CUDA test commands are the next step. Multi-GPU and multi-node training are documented too, with MPI and NCCL. Those are optional for this course.

A good way to read the C is to start with one layer. The repo includes a short tutorial for LayerNorm in doc/layernorm/layernorm.md. Once you have seen one layer in C, the rest of train_gpt2.c is the same idea repeated for attention, the MLP, embeddings, and the loss.

The point of this stop is not to throw PyTorch away in your daily work. The point is to know what it is hiding: kernels, memory, precision, and the glue that makes loss.backward() and optimizer.step() fast. When those things go wrong, you will have a clearer picture of where to look.

Key concepts

  • llm.c trains GPT-2 in plain C and CUDA, with a PyTorch file as the reference implementation.
  • The CPU reference is one file, train_gpt2.c, described in the README as about 1,000 lines. The current file is 1,182 lines in total.
  • Tests check the C and CUDA paths against logits, loss, and optimizer steps from PyTorch.
  • The CPU path is a valid way to read and run a short demo without a GPU.
  • PyTorch is still doing real work for you: kernels, memory, precision, and distribution.

Takeaways

  • Read train_gpt2.c far enough that you can name the pieces a trainer still needs when Python is gone.
  • Use the LayerNorm tutorial if you want one layer spelled out before you read the full file.
  • Run the CPU demo if you like. Treat CUDA and multi-GPU as optional follow-on work.