The Transformer Architecture — The Big Picture
Introduction
Embeddings: Meaning as Vectors ended with a row of numbers for every token, the same row in every sentence. The transformer turns those rows into a prediction, and it is the architecture inside the GPT models; GPT-2's paper says "We use a Transformer (Vaswani et al., 2017) based architecture for our LMs."
Here we keep to the shape of the machine, and we open a real one. Whisper base.en is OpenAI's English speech recognizer, and its paper says "We are releasing models and inference code"; its decoder is a stack of 6 transformer blocks, and every number below comes from running it from those released weights.
The one-sentence version: a transformer passes every token's vector through a stack of blocks; each block takes vectors in and gives vectors of the same size out, mixing in context from the other tokens; the last block's vector at the last position becomes the scores for the next token.
You'll learn:
- the end-to-end flow, from text to the next token's chances;
- what lives inside one block;
- how the guess forms as the vectors rise, measured block by block;
- what depth does and does not buy;
- why this design replaced recurrent networks.
The Big Idea: A Refinement Stack
A token's row from the table is the same wherever the token stands: bank beside river and bank beside account start from one row. The transformer's job is to add context. Each block lets every token take in information from the tokens before it, and gives back a vector of the same size for each one; stacked, the blocks turn the rows into vectors that carry their sentence.
Measured on Whisper's decoder, writing down two spoken sentences, "She sat by the river bank and watched the water." and "She opened a bank account today.": the two bank vectors start at a cosine of 0.961 (one row, at two places), stay together through the first blocks, and after the last block stand at 0.220. Ethayarajh found the same in other models: a word's vectors in different contexts stay alike in the lower layers, and "this self-similarity is much lower in upper layers".
The End-to-End Flow
The whole journey of a prompt:
- Tokenize: the text becomes ids (Tokenization: How Text Becomes Numbers (BPE)).
- Embed and place: each id picks its row of the table, and a vector for its position is added (Embeddings: Meaning as Vectors; positions have their own lesson, Multi-Head Attention & Positional Encoding).
- The blocks: the stack does its work, on every token at once.
- Scores: a final norm, then each vector is compared with every row of the output table, one score for each token of the vocabulary, and a softmax turns the last position's scores into chances (Language Models 101: Predicting the Next Token).
On Whisper base.en, writing down the spoken sentence "The capital of France is Paris.": the text " The capital of France is" is GPT-2's tokens 383, 3139, 286, 4881 and 318, after 2 start marks of Whisper's own. 7 vectors of 512 numbers go in, 6 blocks give 7 vectors of 512 out, and the output table gives 7 rows of 51,864 scores, the last of which puts Paris at a chance of 0.9996. Give the same text with silent audio and Paris gets 0.0001: the decoder writes what it hears, and from the text alone it does not supply the fact.

Everything new happens in step 3: one block first, then the climb up the stack.
Inside a Transformer Block
Every block has the same two parts, each with the same glue.
- Self-attention, the mixing step. Each token's vector takes in information from the tokens before it, weighted by how relevant each one is. In a language model it only looks back: the 2017 paper masks the decoder's attention "to prevent positions from attending to subsequent positions", which is what predicting the next token needs. Self-Attention, Intuitively opens this part.
- The feed-forward network, one token at a time. Each token's vector then goes through a small network on its own, the same one at every position: "two linear transformations with a ReLU activation in between" in the 2017 paper. Whisper's widens 512 numbers to 2,048 and back.
The glue: each part's output is added back onto its input (a residual connection), so a block refines its input instead of replacing it, and a layer norm rescales the numbers. The 2017 paper put the norm after the add, LayerNorm(x + Sublayer(x)); GPT-2 moved it: "Layer normalization (Ba et al., 2016) was moved to the input of each sub-block", and Whisper's model file sets the same norm-first order. Whisper's decoder blocks also have a third part, attention to the audio, which a text-only model does not have.
Why a Stack? Depth and Scale
One block adds a little context; models stack many. GPT-2's four sizes have 12, 24, 36 and 48 blocks (its Table 2), the largest GPT-3 has 96 (its Table 2.1), and Whisper base has 6.
What the blocks do can be watched. Read each block's vectors the way the last block's are read, through the final norm and the output table, and every position shows its guess for the next word at every depth. Belrose and colleagues call this the logit lens and use it to see "how model predictions are refined layer by layer", with a warning that it "yielded useful insights but is often brittle". On Whisper's decoder, writing down the river sentence, the guesses get 0, 1, 0, 0, 3, 7 and 11 of its 11 next words right at the input and after blocks 1 to 6, one count a depth. The input reads back as itself, blocks 2 and 3 read as noise, and the right words arrive in the top blocks. For "The capital of France is", the chance of Paris stays below 0.0001 up to block 4, is 0.0032 after block 5 and 0.9996 after block 6.
Probes of other models find an order in what the layers hold: Tenney and colleagues found BERT's layers taking the steps of a classic language pipeline "in the expected sequence: POS tagging, parsing, NER, semantic roles, then coreference".
Depth is not the whole story of capability. Kaplan and colleagues, measuring how language models improve with size, found that "The loss scales as a power-law with model size, dataset size, and the amount of compute used for training", and that "Other architectural details such as network width or depth have minimal effects within a wide range." More blocks are one way to add size; size, data and compute are what pay.
The Shape of a Forward Pass
In code, the whole forward pass is a loop over the blocks. This one runs with numpy and small random weights, so its guesses mean nothing; it shows the loop and the shapes. Save it as forward.py and run python3 forward.py:
# A decoder's forward pass with small random weights: the loop and the shapes.
import numpy as np
rng = np.random.default_rng(0)
n, d, vocab, blocks = 7, 64, 1000, 6 # 7 tokens, 64 numbers each, 6 blocks
def norm(x):
return (x - x.mean(-1, keepdims=True)) / (x.std(-1, keepdims=True) + 1e-5)
def attention(x, w): # each token looks back, never ahead
q, k, v = x @ w[0], x @ w[1], x @ w[2]
s = q @ k.T / np.sqrt(d)
s[np.triu_indices(n, 1)] = -np.inf
a = np.exp(s - s.max(-1, keepdims=True))
return (a / a.sum(-1, keepdims=True)) @ v
def feed_forward(x, w): # each token on its own
return np.maximum(0, x @ w[3]) @ w[4]
table = rng.normal(0, 0.02, (vocab, d)) # one row a token
places = rng.normal(0, 0.02, (n, d)) # one row a place
shapes = [(d, d), (d, d), (d, d), (d, 4 * d), (4 * d, d)]
weights = [[rng.normal(0, 0.02, s) for s in shapes] for _ in range(blocks)]
x = table[rng.integers(0, vocab, n)] + places
print("in:", x.shape)
for i, w in enumerate(weights, 1):
x = x + attention(norm(x), w) # norm, attend, add back
x = x + feed_forward(norm(x), w) # norm, transform, add back
print(f"after block {i}:", x.shape)
scores = norm(x) @ table.T # read through the same table
p = np.exp(scores[-1] - scores[-1].max())
p = p / p.sum()
print("scores:", scores.shape, "| next-token chances sum to", round(float(p.sum()), 6))Run in zsh on Python 3.14.7 with numpy 2.4.4 and with numpy 2.4.3, it printed the same both times:
in: (7, 64)
after block 1: (7, 64)
after block 2: (7, 64)
after block 3: (7, 64)
after block 4: (7, 64)
after block 5: (7, 64)
after block 6: (7, 64)
scores: (7, 1000) | next-token chances sum to 1.0Seven tokens of 64 numbers go in, and every block gives back seven of 64: the same shape in and out. The last line turns the last position's scores into chances that add up to 1. Trained weights are what make those chances mean something, and Hands-On: Build a Tiny GPT (Transformers Demystified in Code) trains a small one. Whisper's blocks differ from these in their sizes (512 numbers, 8 heads), in a GELU where this code has a ReLU, and in their attention to the audio.
Why It Was Revolutionary
Before 2017 the dominant sequence models were recurrent or convolutional; the Transformer paper opens: "The dominant sequence transduction models are based on complex recurrent or convolutional neural networks". A recurrent network reads one token after another, carrying a summary forward, and the paper names the cost: "This inherently sequential nature precludes parallelization within training examples".
The transformer changed two things:
- Every token at once. Training takes all the positions of a text together. The paper reports that the Transformer "allows for significantly more parallelization", and that it reached a new state of the art in translation after twelve hours of training on eight GPUs.
- A direct path. Through attention, any token reaches any earlier token in one step; the paper's Table 1 gives the longest path between two positions as constant for self-attention and growing with the length for a recurrent layer.
Why Transformers Won (vs RNNs/LSTMs) takes the comparison further.
See It: Trace a Token
Here is the measured stack. Pick a sentence, then climb Whisper's decoder a block at a time: the grid lights each word whose next word that depth already gets right, the depth's guesses are spelled out with their chances, and under the grid the two banks' cosine falls as context mixes in.
Find the noisy middle first, then the block where the two banks part, and reach the top with both sentences.

Check Yourself
Six questions, each answered by the runs above.
Check the transformer
1 / 6Multiple choice
Multiple choice
Multiple choice
Multiple choice
Multiple choice
Multiple choice
Mental-Model Corrections
- A transformer is more than attention. A block is attention and a feed-forward network, each with its residual add and its norm, and the blocks are stacked.
- It computes; whether it understands is another question. Every step above is arithmetic on learned numbers.
- Depth is not memory across calls. A model's answer depends only on the tokens sent with the request; what a chat remembers is the history it sends again (Anatomy of an LLM API Call (Messages, Roles, Params)).
- Same shape in, same shape out. Each block returns one vector of the same size for every token; only the last step turns them into scores.
- More layers are not the main lever. Size, data and compute are; width and depth matter little within a wide range.
Key Takeaways
- A transformer is a stack of blocks that refine every token's vector by adding context; the last block's vector at the last position becomes the next token's scores.
- The flow: tokenize, embed and place, the blocks, a norm and the output table, a softmax.
- A block is self-attention (look back and mix) and a feed-forward network (one token at a time), each with a residual add and a layer norm.
- The guess forms in the top blocks: on Whisper's decoder, Paris goes from below 0.0001 to 0.9996 in the last two.
- It replaced recurrent networks by taking every token at once, with a direct path between any two.
Next: Self-Attention, Intuitively, the part of the block where tokens take in each other.