The whole picture
Here is the complete path of a decoder-only transformer, the design of GPT, Llama, Qwen, DeepSeek, Kimi, GLM, Gemma and almost every chat model. Every box is something you have already met.
- Token IDsThe tokenizer’s output: one number per token
- Embedding tableEach ID becomes a vector of hidden-size numbers (4,096 in Llama 3.1 8B)
- Transformer blocks, repeated32 in Llama 3.1 8B, 61 in DeepSeek-V3. Each block: attention, then feed-forward
- Final normalisationRescale the last vectors
- Unembedding (lm_head)The last token’s vector becomes one score per vocabulary token
- Softmax and samplingScores become chances; one token is picked and added to the text
The blocks are where the work happens. They all have the same design, but each has its own weights, so different blocks learn different jobs. Researchers find that early blocks tend to handle grammar and local patterns, while middle and later blocks handle meaning, facts and the plan for what to say next.
Inside one block
- Add to the residual streamThe block’s second result is added to every token’s vector
- Feed-forward (SwiGLU)Each token’s vector, on its own, is widened, bent and narrowed: 4,096 → 14,336 → 4,096
- RMSNormRescale each vector to a standard size
- Add to the residual streamThe attention result is added to every token’s vector
- Attention (causal, many heads)Each token gathers information from earlier tokens
- RMSNormRescale each vector to a standard size
x = x + Attention(Norm(x))First, tokens share information with each other.
x = x + FeedForward(Norm(x))Then, each token processes what it gathered, on its own.
Notice that attention is the only place where tokens exchange information. Everything else, the normalisation and the feed-forward block, works on each token separately. Notice too that each block only adds to the vectors. That is the residual stream from the backprop lesson, and it is why a stack of 61 or more blocks can still be trained.
Does each part mix information between tokens, or work on each token alone?
-
Attention
Answer: Mixes tokens
-
The feed-forward block
Answer: Each token alone
-
RMSNorm
Answer: Each token alone
-
The embedding lookup
Answer: Each token alone
-
Adding to the residual stream
Answer: Each token alone
-
The causal mask deciding who can see whom
Answer: Mixes tokens
How does it know the word order?
Here is a hidden problem. Attention compares queries and keys by dot products, and nothing in that sum depends on where a token is. Without help, “dog bites man” and “man bites dog” would look identical to it. A transformer needs to be told the positions.
The 2017 transformer added a fixed pattern of waves to each embedding, a different pattern for each position. Nearly every model today instead uses rotary position embeddings (RoPE): before the dot product, each query and key is rotated by an angle proportional to its position. When two rotated vectors are compared, only the difference between their angles survives, so attention sees how far apart two tokens are, which is usually what matters.
Quick check
Three families
| How attention looks | Good at | Examples | |
|---|---|---|---|
| Encoder-only | Every token sees the whole text, both ways | Understanding: classifying, searching, embeddings | BERT (2018), most embedding models |
| Encoder-decoder | An encoder reads the input; a decoder writes the output, attending to it | Turning one text into another: translation, speech to text | The 2017 transformer, T5, Whisper |
| Decoder-only | Each token sees only earlier tokens | Writing, chat, code, reasoning, agents | GPT, Llama, Qwen, DeepSeek, Kimi, GLM, Gemma |
Decoder-only won for chat models because it is the simplest and scales best: one stack, one job (predict the next token), and every text on the internet is training data. But the families still borrow from each other. DeepSeek’s September 2026 model V4.1-Flash describes itself as a “causal encoder-decoder”, with 20 encoder layers and 20 decoder layers inside a model that still writes one token at a time.
Read a real config.json
Every open model on Hugging Face has a config.json. Here are the important lines of Llama 3.1 8B’s, exactly as published. You can now read all of them.
{
"architectures": ["LlamaForCausalLM"],
"vocab_size": 128256,
"hidden_size": 4096,
"num_hidden_layers": 32,
"num_attention_heads": 32,
"num_key_value_heads": 8,
"head_dim": 128,
"intermediate_size": 14336,
"hidden_act": "silu",
"rms_norm_eps": 1e-05,
"rope_theta": 500000.0,
"max_position_embeddings": 131072,
"tie_word_embeddings": false,
"torch_dtype": "bfloat16"
}
| Setting | Meaning |
|---|---|
| LlamaForCausalLM | A decoder-only model that predicts the next token (“causal” means it only looks back) |
| vocab_ | 128,256 tokens, so the embedding table and lm_head each have 128,256 rows |
| hidden_ | Every token is a vector of 4,096 numbers in the residual stream |
| num_ | 32 transformer blocks |
| num_ | 32 query heads per block… |
| num_ | …sharing 8 key-value heads: grouped-query attention |
| head_ | Each head works with vectors of 128 numbers (32 × 128 = 4,096) |
| intermediate_ | The feed-forward block widens to 14,336 |
| hidden_ | SiLU, the bend inside SwiGLU |
| rms_ | A tiny number added inside RMSNorm so it never divides by zero |
| rope_ | Sets how fast RoPE’s rotations turn; a large value helps with long texts |
| max_ | Trained for texts up to 131,072 tokens (128K) |
| tie_ | false: separate matrices for the embedding table and lm_head |
| torch_ | The weights are stored in BF16 |
Why it won
- Parallel training. With the causal mask, a whole text is processed at once, so GPUs are kept busy. RNNs could not do this.
- It keeps improving with scale. Make it bigger and train it on more data, and the loss falls smoothly and predictably. Part 5 shows this “scaling law”.
- It works on anything that can be cut into tokens. Text, code, pieces of images, slices of audio, robot actions. Part 8 shows the same blocks seeing, listening and moving.
- It is simple. One block design, repeated. Every improvement since 2017, from RoPE to GQA to mixture of experts, is a change to one box of this diagram.
Remember
- A decoder-only transformer: embedding table → N identical blocks → final norm → lm_head → softmax.
- Each block: x = x + Attention(Norm(x)), then x = x + FeedForward(Norm(x)). Only attention mixes tokens.
- Attention ignores order by itself; RoPE rotates queries and keys so that attention sees distances.
- Three families: encoder-only (BERT), encoder-decoder (T5, Whisper), decoder-only (all chat models).
- A model’s
config.jsonlists its shape: vocabulary, hidden size, layers, heads, key-value heads, feed-forward size, RoPE and context length.
Words to know
- Transformer
- The neural network design built from attention and feed-forward blocks, introduced in 2017.
- Transformer block
- One repeated unit: normalisation, attention, add; normalisation, feed-forward, add.
- Decoder-only
- A transformer that only looks back and predicts the next token. Every chat model.
- Pre-norm
- Putting normalisation before each part of the block, which makes deep models stable.
- Positional encoding
- Any way of telling a transformer where each token is.
- RoPE
- Rotary position embedding: rotating queries and keys by their position, so attention sees relative distance.
- Causal language model
- A model that predicts each token from the ones before it.
- config.json
- The file that describes a model’s shape: its sizes, layers, heads and settings.
Go to the source
This lesson is built on these papers and documents. Paper links open arxiv.org, a separate site.
-
Attention Is All You Need
Read the whole paper now. Figure 1 is the encoder-decoder version of the diagram in this lesson.
-
RoFormer: Enhanced Transformer with Rotary Position Embedding
The English paper on RoPE. Section 3.2 has the rotation in two dimensions.
-
Transformer升级之路:2、博采众长的旋转式位置编码 (Su Jianlin, 科学空间)
The original Chinese post where RoPE was worked out. Worth reading with a translator: the derivation is beautifully clear.
-
Formal Algorithms for Transformers
Every part of the transformer written as short, exact pseudocode. A perfect reference.
-
On Layer Normalization in the Transformer Architecture
Why pre-norm trains more stably than the original post-norm design.
-
nanoGPT (A. Karpathy)
A complete GPT in about 300 lines of PyTorch for the model. Read model.py and you will recognise every block.
Check yourself
5 questions. Tap the answer you think is right.
0 of 5 answered
Lesson complete
Answer every question above to complete this lesson.
Your progress is saved on this device only.
Next lessonLong context: reading a whole book at once