Volume 1 / Part 3 Mechanics — What Happens Inside / Chapter 30
The Whole Transformer, in One Diagram
We've spent the last few articles looking at Attention, one part of the machine. But looking at a single part doesn't tell you how the whole machine fits together. This time we'll sketch the full diagram: how these parts stack up into one machine that produces language. It turns out the difference between GPT and BERT just comes down to which half of this diagram each one uses.
Let's unfold the blueprint
When you buy a new appliance, the manual usually opens with a full diagram: here's the button, here's where the power goes in, here's how it all moves. Looking at that first makes the details easier to follow later.
Transformers work the same way. We've examined Attention as a single part, but a real model is a much bigger machine built by stacking that part many layers deep. This time, let's get the whole picture into your head as one image.
Drawn out, it's surprisingly simple. The input text becomes embeddings, position information gets added, a pair of layers (Attention and FFN) repeats over and over, and at the end out comes a probability for the next word. That's the entire flow behind generating and understanding text.
A Transformer is a single, straight path: embeddings → position info → repeated Attention and FFN blocks → output.
First, turn words into strings of numbers
Machines can't read letters directly. The first step is converting words or word-fragments (tokens) into vectors of numbers. This step is called embedding.
Think of it like assigning every word in a dictionary a home address made of hundreds of numbers. "Cat" and "dog" end up with nearby addresses; "cat" and "airplane" end up far apart. Words with similar meanings land close together in this numerical space.
At this stage, word order still doesn't matter. "The cat chased the dog" and "The dog chased the cat" produce the exact same per-word numbers. That's where the next step comes in.
Position encoding: teaching the model about order
A Transformer has no circuit that reads a sentence word by word in sequence (no recurrence). It processes every word at once, in parallel. Left alone, that means it can't tell "the cat chased the dog" apart from "the dog chased the cat."
That's where position encoding comes in. The model adds, to each word's embedding, a number representing that word's position in the sentence. The original Transformer paper, published in 2017, generated this position information using a combination of sine and cosine waves, then added it directly into the embeddings.
One way to picture it: the model stamps a rhythm onto each word, like a metronome ticking at a different phase for every position. This lets the model tell apart "who chased whom," preserving order even though it reads everything at once.
Position encoding is the reason a Transformer doesn't lose track of word order.
Attention and FFN: the repeating duo
Once position information is folded into the embeddings, the real processing begins. The core of the model is two kinds of layers stacked alternately. One is the Attention layer we covered last time; the other is the FFN (feed-forward network) layer.
Attention decides where in the sentence to look. FFN takes that result and applies the same transformation independently to each word's position. In the math from the original paper, this means expanding the input into a much larger dimension and then compressing it back down, a two-step transformation. If Attention is like a meeting that decides which information to gather, FFN is like each person going back to their desk and organizing what they picked up.
One Attention layer plus one FFN layer counts as a single "layer," and the same structure repeats many times. More layers mean more passes at figuring out relationships between words and reading context. In the original Transformer paper, this pair was stacked six times on the encoder side and six times on the decoder side.
Encoder, decoder, and why GPT only uses one of them
This stack of layers actually wears two different hats: one is called the "encoder," the other the "decoder." The original Transformer was designed for translation. The part that reads the input sentence and condenses its meaning is the encoder; the part that builds the translated sentence one word at a time, based on that meaning, is the decoder.
Each encoder layer is built from two sub-layers: Attention and FFN. Each decoder layer has three sub-layers instead: self-attention over its own output, an additional Attention layer that looks at the encoder's output, and the FFN. The decoder's self-attention also includes a trick called a causal mask, which stops it from peeking at future words it hasn't generated yet. That detail connects directly to our next topic: how a model predicts the next word.
Here's the part worth remembering: the difference between two famous models, GPT and BERT, comes down to which half they use. GPT uses only the decoder part. GPT-1, published in 2018, started out remarkably simple, just 12 stacked decoder blocks. BERT goes the other way, stacking only encoder blocks. GPT reads a sentence left to right, one direction only, while BERT looks at the whole sentence at once, using context from both directions simultaneously.
GPT-style models use only the decoder; BERT uses only the encoder. Same parts, different placement.
What depth changes
How many times a model stacks these layers varies enormously. The original Transformer used 6 layers. BERT-base stacks 12, and BERT-large goes up to 24. In parameter count, that's roughly 110 million for BERT-base and about 336 million for BERT-large.
GPT-series models go further still, by orders of magnitude. GPT-3, published in 2020, stacks 96 decoder layers in its largest configuration (175B, roughly 175 billion parameters). But within that same GPT-3 family, the smallest configuration, at 125M parameters, uses just 12 layers, so layer count swings wildly depending on model size.
The general belief is that deeper stacks mean more stages for reading context, which helps capture more complex relationships. But piling on layers doesn't automatically make a model smarter; it has to be balanced against training data volume and compute budget. Here's something worth trying: if you were building a miniature Transformer from scratch, how many layers would you start with? The number GPT-1 picked, 12, probably wasn't arbitrary.
FAQ
How do GPT and BERT differ in using the Transformer's encoder and decoder?
GPT stacks only decoder blocks and generates text left to right, one direction at a time. BERT stacks only encoder blocks and reads the whole sentence bidirectionally for understanding. Same building blocks, just deployed differently depending on the goal.
Why does a Transformer need position encoding?
A Transformer processes every word in a sentence in parallel, so left alone it would have no idea what order the words came in. Position encoding adds numeric information to each word's embedding so the model knows which word came first, second, and so on.
Does more layers mean a smarter AI?
Generally, deeper stacks are thought to process context through more stages, which helps with complex relationships. But it's not a simple linear relationship. GPT-3 alone ranges from 12 to 96 layers depending on configuration, balanced against training data and compute resources.
Inside this stack of layers, what the model is ultimately doing boils down to one repeated task: guessing the next word. Next time, we'll look at that final push up close.
Primary sources
- Attention Is All You Need (NeurIPS original paper PDF) — Primary source for encoder/decoder layer counts, sub-layer structure, position encoding, and FFN
- A Review of Large Language Models and Autonomous Agents in Chemistry (arXiv 2407.01603) — Explanation of GPT being decoder-only
- Meet GPT, The Decoder-Only Transformer — Confirms GPT-1's decoder layer count (12)
- Medical Concept Normalization in a Low-Resource Setting (arXiv 2409.14579) — Confirms BERT-base/large layer counts and parameter counts
- Multilingual Large Language Models and Curse of Multilinguality (arXiv 2406.10602) — Confirms GPT-3 layer counts and parameter counts
This article organizes general knowledge based on our experience and public sources. Product specs, prices, and regulations reflect the time of writing (October 7, 2026) and may change.