Trace the Transformer Forward Pass

One token is the machine, run once. Running your first LLM ended with a server producing tokens and a promise to go inside; this module keeps it, taking the single step that produces one token and walking through it station by station until nothing about it is mysterious. This lesson is the walk at tensor level, the map of stations; the three that follow go inside each station, the generation-loop lesson rebuilds the loop around them by hand, and the visual lab runs the trip on one screen.

The method is the course's usual one: run it and look. What model.generate() hid in running your first LLM is mostly a keyword argument away from visible, and as everywhere in this module, every number on this page comes from the canonical prompt run through the real model.

Opening the black box

The entire interaction with the model from running your first LLM went through one opaque call:

generate is a convenience wrapper. The machine underneath is the model's forward pass: tensors in, tensors out, once per token. Call the model directly, and ask it to keep its intermediate results, and the black box opens:

Three lines of output, and the whole trace is in them. The rest of the lesson reads them slowly.

From text to tensors

The input line is the tokenizer from model loading and tokenization at work: five words and the begin-of-text marker, six IDs, [128000, 791, 6864, 315, 9822, 374]. The tokenizer is the last non-neural step in the whole pipeline: a lookup table plus merge rules, running on the CPU in microseconds, and its output is the final moment the request is anything but tensors.

Inside the model, each ID does exactly one thing: it selects its row of the embedding matrix, the [128256, 4096] table from the skeleton from the transformer architecture refresher. Six IDs, six rows, stacked: a tensor of shape [1, 6, 4096], one batch, six positions, 4,096 numbers per position. That tensor is the residual stream, and from here to the far end of the model, its shape never changes.

Through the stack

The 33 in the output is the residual stream photographed at every station: hidden_states holds the embedding output plus the stream after each of the 32 blocks, every one of them [1, 6, 4096]. One wrinkle rides along, and MLP, normalization, and residuals measures it: the library hands you that last photograph after the final norm has already been applied to it. Otherwise this is the map from the transformer architecture refresher confirmed by the machine itself: each block read the stream, computed its adjustment, added it back, and handed the same shape onward.

What each block actually does to those numbers is deliberately not this lesson's job. The attention sublayer, where the six positions inform one another under a mask that points strictly backward, is causal attention. The MLP and the normalization wrapped around everything, where most of the parameters live, is MLP, normalization, and residuals. Here they are stations the stream passes through, 32 times.

Inspect this. len(out.hidden_states) is 33 because index 0 is the embedding output and indexes 1 through 32 are the blocks. Print out.hidden_states[0][0, -1, :5] and then the same slice at index 32: the first is the raw embedding row of is, the second is what 32 blocks and the final norm made of it. Same position, same shape, different numbers; the difference IS the model's reading of the prompt.

Out the other end

After the last block and the final norm, the output matrix (the second [128256, 4096] giant) projects each position's vector back over the vocabulary: [1, 6, 128256], one score per vocabulary entry, per position. The field calls these scores logits; the lesson on logits and sampling is where they get read.

Notice what the shape says: the model scored a next token for all six positions, not just the last one. Why it computes all six, why generation uses exactly one of them, and how a distribution and a choice come out of that row, is where the trace goes next after the two block lessons.

Text to IDs to one stream, 32 read-and-add stops, one projection at the end: that is the map. The next lesson steps inside the first station of every block, attention, and the three letters it is built from.