Inference Engineering

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

    Llama 3.1 8B in 16-bit numbers, as the same tower of 32 layers as in lesson 4. The two parts of each layer and the two slabs are to scale in bytes; the chosen row, the notes and the score bars are enlarged, and the scores are made up. It plays by itself; press a stage to replay it, or drag to look around.

    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:

    Both tables are drawn the same size because they are: one row of 4,096 numbers for each token the model knows. The highlighted row is enlarged; to scale it would be far thinner than a pixel.

    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

    Each rectangle is one weight matrix of one layer: width is how many numbers come out, height how many go in, so area is the number of weights. The sweep reads them in order at a constant speed.

    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

    The bar is to scale, from a one-token chat to the longest one Llama 3.1 allows. Only the hatched notes change.

    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

    Weights and notes in 16 bits, for one conversation. The time is the floor set by memory speed on one NVIDIA H100, as in lesson 1.

    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.