Topic 05 of 62 · Inside the Transformer block
Encoder and decoder stacks
EasyWhat do the encoder and decoder of the original Transformer each do, and which parts do modern LLMs keep?
1 · Start with the intuition
The original Transformer was built for translation. Picture two towers. The encoder tower reads the whole source sentence at once and produces one context-aware vector per source token. The decoder tower writes the translation one token at a time: at each step it looks at what it has written so far and at the encoder’s vectors, then predicts the next token. Each tower is a stack of identical layers (six in the paper) that have the same structure but their own weights. An encoder layer has two parts: self-attention, then a feed-forward network. A decoder layer has three: masked self-attention, attention over the encoder output, then a feed-forward network. The rest of this stage opens those boxes one at a time.
2 · How the mechanism works
Mechanism step 1 of 4 · no question to answer
Embed the tokens
Each token becomes a vector of width d_model (512 in the base model). Only the bottom layer sees embeddings; every other layer receives the output of the layer below it.
Next, we will look at: Encode the source. Move on when this operation makes sense.
Visualizing the behavior
Two stacks: the encoder reads the source, the decoder writes the output
Illustrative- Encoder layers
- Decoder layers
- Encoder output
Inside one layer of each stack
3 · The governing equation
Every sub-layer: out = LayerNorm(x + Sublayer(x)) Encoder layer = [ SelfAttention, FeedForward ] Decoder layer = [ MaskedSelfAttention, EncoderDecoderAttention, FeedForward ] Base model: N = 6 layers per stack, d_model = 512, h = 8 heads, d_ff = 2048
Notation: x is the list of vectors entering a sub-layer, one per token position. N is the number of layers in each stack, d_model the vector width, h the number of attention heads, and d_ff the hidden width of the feed-forward network.
The split matches the task: translation has a complete input to read and a separate output to write. Later models keep only the part they need. Encoder-only models such as BERT read text and produce representations. Decoder-only models such as the GPT family drop the encoder and the encoder-decoder attention; the prompt and the generated text share one sequence, and a causal mask stops each position from seeing later ones. Most current large language models are decoder-only, so the layer you will meet most often has just two parts: masked self-attention and a feed-forward network.
4 · Put concrete numbers on it
Worked numbers (from stated assumptions)5 · Common misconception to avoid
Saying the encoder compresses the sentence into a single vector. It outputs one vector per source token, and every decoder layer attends over all of them.
6 · Self-check & interview synthesis
Explain it in your own words first
Before revealing the reference answer, try answering the opening question aloud in four beats: what it is → how the math works → what trade-off it makes → a concrete number or failure mode.
Follow-up question interviewers often ask next
MediumHow can a decoder-only model translate without an encoder?
Show follow-up reasoning
The source sentence is placed in the prompt, in the same sequence as the output. When the model generates the translation, ordinary causal self-attention can look back at the source tokens, which does the job that encoder-decoder attention did in the original design.