Inside a Transformer
Follow the question about a five-by-five-metre room all the way from text to a proposed answer. Each stage reuses a calculation from the previous chapters.
Open this chapter with experiments and audio
How does a model read the question?
Consider “How many square meters is a room of 5 meters by 5 meters?” The tokenizer splits the text into pieces and converts each piece into an ID. The embedding table turns each ID into a vector. If the same numeric token occurs twice, both occurrences begin with the same input embedding. Their positions differ, so the model can give them different roles as it processes the question.
The diagram breaks one possible processing route into visible steps. A trained model computes vectors at every token position at once during its initial pass over the prompt. The small numbers and labels in this illustration let you trace the operations. Interpreting a hidden vector in a real checkpoint requires separate measurement.
What happens inside one block?
In a common pre-normalized decoder, a normalization operation rescales each token’s features. Causal self-attention then mixes information from allowed positions. The resulting update is added to the original representation. That addition is the residual connection. It lets information already present travel forward while the layer contributes an update.
h = x + attention(norm(x))
output = h + feed_forward(norm(h))
The feed-forward network transforms features separately at each position, using the same learned weights for all positions. A nonlinear activation allows it to combine the features in new ways. Attention exchanges information between positions. The feed-forward network transforms the information now available within each position. A stack of blocks repeats these operations.
How does word order enter?
The sentences “dogs chase cats” and “cats chase dogs” contain the same three words. To handle order, architectures supply position information. One option adds a position vector to each token embedding. Rotary position embeddings instead rotate components of queries and keys by position-dependent angles, making their dot products sensitive to relative positions. A model uses the position scheme it was trained with.
The original translation Transformer had an encoder and a decoder. An encoder can let each input token read the entire source. Its decoder can attend to those source representations as it generates a translation. Many text generators use only causal decoder blocks and place the prompt and its continuation in a single sequence.
Where does “25 m²” come from?
The output projection converts the last hidden state into a score for each token in the vocabulary. With 10,000 possible tokens, that gives 10,000 scores for the next position. A decoding rule selects the next token. The selected token is appended, and the process repeats until a stop condition. The answer may also begin with “The”, depending on the learned distribution and the decoding rule.
A model can produce a correct short calculation from learned patterns. For an assistant that requires exact arithmetic, application code can instead execute a calculator call using the extracted dimensions. Chapter 27 follows the arguments, calculation and result of that route.
Does one block generate a whole answer?
One block updates hidden representations. The whole stack and output projection supply next-token scores. The generation loop produces a sequence of tokens.
Which steps mix positions and which work locally?
Suppose “cold” occurs at position 2 and “water” at position 3. A causal attention head at position 3 can copy a feature from position 2 into its mixture. The following feed-forward network reads the updated vector at position 3. It can now transform a combination of features that arrived from different words, even though that network processes each position separately with the same weights.
Take a residual coordinate with value 3. If the attention branch contributes −0.5, the sum is 2.5. A feed-forward contribution of +1.2 gives 3.7. These arithmetic examples omit normalization and projection. In the executable block you can inspect their exact placement and every vector. Setting a branch contribution to zero leaves the incoming value available to the next step.
Could an early position use the later word “water” in a causal decoder?
The mask prevents that information path at every layer. An encoder with bidirectional attention permits it. During training a decoder can calculate all positions together and still apply that same causal mask.
Inspect the residual additions in the trained decoder →
Sources · Vaswani et al., 2017