JA EN
LearnHow Transformers Work
·★ MEMBER·PAPER·9 min read

Encoder or Decoder — The Fork in the Road Between BERT and GPT

One masked triangle in the attention table is what separated BERT from GPT. This piece works through bidirectional versus autoregressive with the equations and an interactive figure, then asks why generation won and where encoders are still the first choice.

ModalitytextTasknlp

BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding


Twins from the same blueprint

Within about a year of the Transformer blueprint appearing in 2017, two models came out that used it almost verbatim: BERT and GPT. Same parts, same way of stacking them — and yet completely different uses, and completely different fates.

What separated them was a single design decision: how much of the sentence is each word allowed to see? That one choice, and almost nothing else, turned one into a reading machine and the other into a writing machine.

Think of BERT as a proofreader. The whole manuscript is spread out on the desk, and a smudged character gets reconstructed from what sits on either side of it. What a proofreader does not do is write a page from scratch. GPT is a storyteller, choosing the next word as they speak, so the visible range is always and only "everything before me."

The intuition: what you can see decides what you can be trained on

This is not a matter of taste. Once you fix the visible range, the shape of the training task is fixed for you.

The moment you decide, as GPT does, that a position sees only what precedes it, the training task becomes free. At every position, the next word is the label. Nobody has to annotate anything; text alone generates an unlimited supply of problems. That is autoregressive modelling.

Decide instead, as BERT does, that everything is visible, and you have a problem. Ask the model to predict the next word and it can already see it. There is nothing to learn from an open-book exam. So BERT introduced a deliberately artificial step: hide some of the words, then ask for them back. That is masked language modelling (MLM), and the original paper hides 15% of the input tokens.

Going bidirectional costs you an extra masking step; going autoregressive costs you the right to look backwards from the future. Both sit on top of a trade-off.

The mechanism: one line drawn across the attention table

As covered in Attention from scratch, self-attention builds an N×NN \times N table of scores for "how much does word ii care about word jj", where NN is the number of tokens.

sij=qikjdks_{ij} = \frac{q_i \cdot k_j}{\sqrt{d_k}}
(1)

Here qiq_i is word ii's "question" vector, kjk_j is word jj's "name tag" vector, and dkd_k is their dimension. Put in words, it says: measure how similar ii's question is to jj's name tag, then flatten the scale by the square root of the dimension.

The fork is entirely in how that table is treated.

sij(j>i)s_{ij} \leftarrow -\infty \quad (j > i)
(2)

GPT adds this one line, which says: replace every score that points into the future (j>ij > i) with -\infty. Because e=0e^{-\infty}=0, softmax turns those weights into exactly zero and no future word gets mixed in. Blacking out the upper-right triangle this way is the causal mask. BERT never adds it and uses the full table. The layer structure, the residual connections, the feed-forward blocks are shared, so structurally this is nearly the whole difference.

Where it really bites is the training objective. BERT takes its loss only at the hidden positions.

LMLM=iMlogp(xixM)\mathcal{L}_{\mathrm{MLM}} = -\sum_{i \in M} \log p(x_i \mid x_{\setminus M})
(3)

MM is the set of hidden positions and xMx_{\setminus M} is everything left visible. Put in words: recover each hidden word using all of the remaining context, and score yourself only on the hidden spots.

GPT takes its loss at every position.

LAR=i=1Nlogp(xix<i)\mathcal{L}_{\mathrm{AR}} = -\sum_{i=1}^{N} \log p(x_i \mid x_{<i})
(4)

x<ix_{<i} is every word before ii — which says: predict each word from its left context only, and every single position counts as a question.

Try flipping the "causal mask" switch in the figure below. Switched on is the world GPT sees; switched off is the world BERT sees.

FIG 1Attention weights arc between words around a circle. Toggling "causal mask" makes the same model flip between BERT's view of a sentence and GPT's

Push the same sentence through each model once and they do not collect the same amount of signal. GPT gets error terms from a sentence of length ; BERT hides only 15%, so it gets roughly . Same compute, different density of supervision. That inefficiency — you can only learn from the spots you covered up — is exactly w

What's behind this

§

Members-only from here

371 walkthroughs, 26 textbook chapters, 48 student units and 6 close readings — all included for $4.99/mo, with three new explainers every day. Cancel any time; access runs to the end of the period.

Already a member? Sign in to keep reading

References

  1. BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. arXiv:1810.04805Paper page·PDF
  2. Language Models are Few-Shot Learners. arXiv:2005.14165Paper page·PDF
  3. ELECTRA: Pre-training Text Encoders as Discriminators Rather Than Generators. arXiv:2003.10555Paper page·PDF
  4. Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks. arXiv:1908.10084Paper page·PDF
  5. Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer. arXiv:1910.10683Paper page·PDF
  6. Smarter. "arXiv:2412.13663Paper page·PDF
  7. Better. Better
  8. Faster. Faster
  9. https://arxiv.org/abs/2412.13663". Longer: A Modern Bidirectional Encoder

This article is written from the source paper above. Where they differ, the original is authoritative.

Comments

Sign in to comment