Lesson 2 of 14
What one token costs
Lesson 1 found that every token makes the GPU read about 15 GB. Follow one token up through Llama 3.1 8B, layer by layer, to see where those bytes go, the 1 GB it skips, and the one part that keeps growing until it outweighs the model itself.
One token, bottom to top
The 3D view could not load. The three stages are described below.
01 · Word to vector
One row in, every row out
A model works on tokens (a token is a piece of a word, about three-quarters of one on average), and Llama 3.1 8B knows 128,256 of them. Its first step is a lookup: each token has its own row of 4,096 numbers in the embedding table. That row, a vector, is what the rest of the model works on. Copying it reads 8.19 KB.
The last step looks the same and costs the opposite. The output layer also has one row per token, 1.05 GB in all, but to score every possible next token it has to read every row. The highest scores win: the model picks one token, and that token goes back to the bottom as the next input. One token, one trip through everything.
Two tables of the same size
Tokens so far:
02 · Inside a layer
Attention looks back, feed-forward reads the most
Between the two tables sit 32 layers, each with its own weights, the learned numbers, at 2 bytes each. Attention compares the vector with notes left by earlier tokens and adds what it finds. The feed-forward block then multiplies the vector out to 14,336 numbers and back.
A matrix multiply uses each weight once, for one multiplication and one addition, so a token reads every weight once. The arithmetic is quick; fetching the numbers from the GPU’s memory is what takes time (lesson 1). Nothing read for one layer helps the next, because every layer has weights of its own.
One layer, matrix by matrix
03 · Where the bytes go
Mostly feed-forward, and a part that grows
Add it up and one token reads 15.0 GB of weights: 75% feed-forward, 18% attention, 7% the output layer. The other 1.05 GB of the model is the embedding table, which gives up one row and is otherwise never touched. So the biggest savings come from the feed-forward block: smaller numbers there, or more users sharing each read.
The notes are not weights. Each earlier token leaves 131 KB of them, and every new token reads them all. In a short chat they are a sliver; near Llama 3.1’s longest conversations, 131,071 tokens, they reach 17.2 GB and outweigh the model. Lesson 4 is about them.
What one token reads as the chat grows
Llama 3.1 8B, 16-bit numbers. Plays by itself; move the slider to take over.
- Attention weights
- Feed-forward weights
- Output layer
- Notes of earlier tokens
04 · Playground
Weigh another model
Pick a model and a conversation length. Try Mistral 7B, whose notes stop growing after 4,096 tokens, and Qwen2.5 0.5B, where the output layer is a large share of a small model.
What one token reads, for any model
16-bit numbers, one conversation, NVIDIA H100
- Attention weights
- Feed-forward weights
- Output layer
- Notes of earlier tokens
05 · Glossary
The vocabulary, in one place
- Token
- The unit a model reads and writes: a piece of a word, about three-quarters of one on average. Llama 3.1 8B knows 128,256 of them.
- Vector
- A list of numbers standing for one token inside the model: 4,096 of them in Llama 3.1 8B.
- Weights
- The model's learned numbers, 16.1 GB of them in 16 bits. Every multiply uses one.
- Embedding table
- One row of numbers per token. A token copies its own row to become a vector, and nothing else in the table is read.
- Layer
- One stage of the model, with its own weights. Every token passes through all 32 in order.
- Attention
- The first part of a layer: it compares the token with the notes of earlier tokens and takes in what matters.
- Feed-forward block
- The second part of a layer: three big matrix multiplies that widen the vector to 14,336 numbers and back.
- Output layer
- The last step: one row per token, all read, to give every possible next token a score.
- Notes (KV cache)
- What each earlier token leaves in every layer for later ones to look at: 131 KB per token here. Lesson 4 is about them.
You can now
- Trace one token through a Llama-style model, from the embedding lookup through every layer to lm_head, and say what each step reads from memory and why the 1 GB embedding table is barely read while the same-sized lm_head is read in full.
- Compute the parameters, FLOPs and bytes of every weight matrix from a model's config, and explain why one decode token costs about 2 FLOPs and, in BF16, 2 bytes per matmul weight, with the MLP as the largest share.
- Decide at what context length attention stops being negligible, using n* = N / (2 x layers x query width), and explain why batching cannot help it while it does help the matmuls.
Open the workbook: the formulas, the exact numbers, graded exercises and the sources behind this page.