---
title: "What PyTorch is doing for you"
lesson_id: "05"
---

# What PyTorch is doing for you

- **Lesson ID:** 05
- **Goal:** Show what remains of a GPT-2 trainer when it is written in C and CUDA, and how that code is checked against PyTorch.
- **Human lesson:** [05-what-pytorch-hides.html](05-what-pytorch-hides.html)

## Prerequisites

- Lesson 04, so the training loop is already familiar.

## Inputs, outputs, and artifacts

- **Inputs:** A Python training loop, and the question of what the framework is doing.
- **Outputs:** A reading plan for `train_gpt2.c`, with an optional CPU demo.
- **Artifacts:** Progressive mermaid diagrams of the PyTorch debug state, the C reference, and the CUDA path.

## Agent build steps

1. Point at [llm.c](https://github.com/karpathy/llm.c). State the goal: GPT-2 (and GPT-3 series) training in C and CUDA, with [train_gpt2.py](https://github.com/karpathy/llm.c/blob/master/train_gpt2.py) as the PyTorch reference.
2. Quote the README's "about 1,000 lines" for `train_gpt2.c`, and give the current total of 1,182 lines.
3. Explain the test path: a debug state from PyTorch, then compare logits, loss, and a few Adam steps.
4. Use progressive diagrams to add the CPU file, then the CUDA file.
5. Give the CPU quick start from the README. Mark CUDA and multi-GPU as optional.
6. Point at [doc/layernorm/layernorm.md](https://github.com/karpathy/llm.c/blob/master/doc/layernorm/layernorm.md) as a way into one layer.
7. Close on the idea that PyTorch still does kernels, memory, precision, and distribution for you.

## Constraints

Keep the language simple, friendly, and literal. No quizzes, no em dashes. Brand Edge FDE only. Do not require an NVIDIA GPU to complete the lesson.

## 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.

## Acceptance checks

- The 1,000-line claim is sourced to the README and accompanied by the current line count.
- A GPU is not required to finish the lesson.
- Agent brief link is present.
- [ ] Proceed to [lesson 06 brief](06-what-the-pipeline-costs.llms.md).
