Availit World How AI really works, in your language

Lesson 16 of 4614 minFacts checked October 20, 2026

The transformer: putting it all together

You now know every part: embeddings, attention, feed-forward blocks, residual connections, normalisation and softmax. Stack them in the right order and you have the design behind nearly every AI model of the last decade. At the end you will read a real model’s config.json and understand every line.

In this lesson you will

  • Trace a decoder-only transformer from token IDs in to next-token chances out
  • Explain what one transformer block does, and in what order
  • Explain how a transformer knows word order, using rotary position embeddings (RoPE)
  • Tell apart the three transformer families, and read a real config.json
On this page
  1. The whole picture
  2. Inside one block
  3. How does it know the word order?
  4. Three families
  5. Read a real config.json
  6. Why it won
  7. Remember
  8. Check yourself

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.

From text to the next token
  1. Token IDsThe tokenizer’s output: one number per token
  2. Embedding tableEach ID becomes a vector of hidden-size numbers (4,096 in Llama 3.1 8B)
  3. Transformer blocks, repeated32 in Llama 3.1 8B, 61 in DeepSeek-V3. Each block: attention, then feed-forward
  4. Final normalisationRescale the last vectors
  5. Unembedding (lm_head)The last token’s vector becomes one score per vocabulary token
  6. 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

One transformer block (read from the bottom up)
  1. Add to the residual streamThe block’s second result is added to every token’s vector
  2. Feed-forward (SwiGLU)Each token’s vector, on its own, is widened, bent and narrowed: 4,096 → 14,336 → 4,096
  3. RMSNormRescale each vector to a standard size
  4. Add to the residual streamThe attention result is added to every token’s vector
  5. Attention (causal, many heads)Each token gathers information from earlier tokens
  6. RMSNormRescale each vector to a standard size
The whole block in two lines
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?

0 of 6 sorted
  • 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

With RoPE, what does attention between two tokens end up depending on?

Show the answer

Answer: Only their distance from each other Each vector is rotated by its own position, and the dot product of two rotated vectors depends only on the difference of the angles: their distance.

Three families

The transformer family tree
How attention looksGood atExamples
Encoder-onlyEvery token sees the whole text, both waysUnderstanding: classifying, searching, embeddingsBERT (2018), most embedding models
Encoder-decoderAn encoder reads the input; a decoder writes the output, attending to itTurning one text into another: translation, speech to textThe 2017 transformer, T5, Whisper
Decoder-onlyEach token sees only earlier tokensWriting, chat, code, reasoning, agentsGPT, 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.

Llama 3.1 8B: config.json (selected lines)json
{
  "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"
}
What each line means
SettingMeaning
LlamaForCausalLMA decoder-only model that predicts the next token (“causal” means it only looks back)
vocab_size128,256 tokens, so the embedding table and lm_head each have 128,256 rows
hidden_sizeEvery token is a vector of 4,096 numbers in the residual stream
num_hidden_layers32 transformer blocks
num_attention_heads32 query heads per block…
num_key_value_heads…sharing 8 key-value heads: grouped-query attention
head_dimEach head works with vectors of 128 numbers (32 × 128 = 4,096)
intermediate_sizeThe feed-forward block widens to 14,336
hidden_actSiLU, the bend inside SwiGLU
rms_norm_epsA tiny number added inside RMSNorm so it never divides by zero
rope_thetaSets how fast RoPE’s rotations turn; a large value helps with long texts
max_position_embeddingsTrained for texts up to 131,072 tokens (128K)
tie_word_embeddingsfalse: separate matrices for the embedding table and lm_head
torch_dtypeThe 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.json lists 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.

Check yourself

5 questions. Tap the answer you think is right.

0 of 5 answered

In a transformer block, which part lets tokens exchange information?

Show the answer

Answer: Attention Attention is the only step where one token’s vector is influenced by other tokens. The rest works on each token alone.

What does “x = x + Attention(Norm(x))” mean?

Show the answer

Answer: The attention result is added to the vector: a residual connection Each part adds its result to the residual stream instead of replacing it, which keeps very deep stacks trainable.

Why does a transformer need position information?

Show the answer

Answer: Attention’s dot products ignore where tokens are Without positions, attention would treat a text like a bag of tokens, and “dog bites man” would look the same as “man bites dog”.

Which family are chat models like DeepSeek and Qwen?

Show the answer

Answer: Decoder-only They predict the next token from the earlier ones, with a causal mask: decoder-only transformers.

A config.json says num_attention_heads 24 and num_key_value_heads 4. What does that tell you?

Show the answer

Answer: The model uses grouped-query attention: 6 query heads share each key-value head Fewer key-value heads than query heads means GQA. These are Qwen3.8-27B’s numbers: 24 ÷ 4 = 6 query heads per key-value head.

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
Previous lesson All lessons

Choose your language