datarekha

BERT, GPT, T5 — picking the right architecture

Three transformer families with different pretraining objectives and different jobs. How to choose between encoder-only, decoder-only, and encoder-decoder today.

8 min read Advanced NLP & Transformers Lesson 28 of 44

What you'll learn

  • The difference between encoder-only, decoder-only, and encoder-decoder
  • Which family is best for which task
  • Why decoder-only models took over since 2023

Before you start

Three transformer families exist, each built around a different pretraining objective. The objective shapes everything — what tasks the model is good at, how you use it, and how big it tends to be. Knowing which family fits your problem saves you both money and embarrassment.

The cheat sheet

FamilyArchitecturePretraining taskAttentionBest for
BERTEncoder-onlyMasked LM (predict masked tokens)BidirectionalClassification, embeddings, retrieval
GPTDecoder-onlyNext-token predictionCausal (left-to-right)Generation, chat, instruction-following
T5Encoder-decoderSpan corruption (denoising)Encoder bidirectional, decoder causalTranslation, summarization, structured seq2seq
BERTencoder-only, bidirectionalEncoder stackattention maskevery token sees every tokenMLM: predict [MASK]GPTdecoder-only, causalDecoder stackattention masktoken i sees tokens 0..iNTP: predict next tokenT5encoder-decoderEncoderDecoderencoder mask + decoder maskenc (bi)dec (causal)bi-enc, causal-dec, cross-attnspan corruption: fill the blanksfilled cell = token i attends to token j; empty = masked
Three architectures, three attention masks. The mask shape is the deepest difference between the families.

BERT — the encoder for understanding

BERT (2018) processes the whole input bidirectionally — every token sees every other token, in both directions. Pretraining task: “I’ll mask 15% of the tokens; predict them.” To do this well, the model has to deeply understand context in both directions.

"The [MASK] sat on the mat."

"The cat sat on the mat."

BERT is not generative — bidirectional attention means it can see the whole sequence at once, so it can’t autoregressively produce output token-by-token. BERT-style models excel at understanding tasks where you need a fixed-size representation of a piece of text:

  • Sentence classification (sentiment, spam, intent)
  • Token classification (named entity recognition)
  • Sentence-pair tasks (similarity, entailment)
  • Embeddings for retrieval — this is where they shine today

Embeddings is the killer app for encoder-only models. Every RAG system uses an encoder to turn documents and queries into vectors. Models like BAAI/bge, intfloat/e5, nomic-ai/nomic-embed, and mixedbread-ai/mxbai-embed-large are BERT-family descendants optimized for retrieval — and they’re the workhorse of modern LLM applications.

GPT — the decoder for generation

GPT-style models use causal attention: token i can only see tokens 0..i. The pretraining task is “predict the next token, given all the previous ones.”

"The cat sat on the" -> "mat"

Causal attention is what makes generation natural — each new token is predicted from the previous ones, then appended, then the process repeats. Decoder-only models are the basis of:

  • Chat assistants (ChatGPT, Claude, Gemini)
  • Code generation (Copilot, Cursor)
  • Agentic systems
  • Anything that produces variable-length output

Examples today: GPT-4o, Claude Sonnet/Opus, Llama 3.1/3.2, Mistral Large, Qwen 2.5, DeepSeek V3. Every major frontier LLM today is decoder-only. This is the dominant paradigm, and it’s not close.

T5 — encoder-decoder for seq2seq

T5 (2019) has both an encoder (bidirectional) and a decoder (causal). Pretraining: corrupt spans in the input, train the decoder to produce them.

Input:  "The <X> sat on <Y>."
Output: "<X> cat <Y> the mat"

T5 shines on seq2seq tasks: translation, summarization, structured text-to-X. T5 family today: original T5, FLAN-T5, CodeT5+, mT5. They’re not extinct, but mostly displaced by big decoder-only models that handle the same tasks via prompting.

When to pick which

Use an encoder-only model when:

  • You need embeddings (retrieval, clustering, classification at scale)
  • You’re doing classification on short text and want a cheap, fast model
  • Your task has a fixed output shape, not free-form text

Use a decoder-only model when:

  • You want generation, chat, instruction-following
  • You want one model that does many tasks
  • You want to use it via an API or open-weights (almost everything)

Use an encoder-decoder model when:

  • You’re doing translation or summarization at scale and need every drop of performance
  • You’re building on existing T5 checkpoints
  • Otherwise: just use a decoder-only model

Hugging Face naming, decoded

bert-base-uncased            # encoder-only, English, lowercase
google/flan-t5-base          # encoder-decoder, instruction-tuned
meta-llama/Llama-3.1-8B      # decoder-only, base
meta-llama/Llama-3.1-8B-Instruct  # decoder-only, chat-tuned

The convention: org/model-version-size-variant. “Instruct” or “Chat” in the name = RLHF/SFT-tuned for following instructions; without it, you get the raw pretrained model.

In one breath

  • Three families, three pretraining objectives, three attention masks — and the mask is the deepest difference.
  • BERT (encoder-only, bidirectional, masked-LM): every token sees every other, so it understands but can’t generate — the workhorse for embeddings, retrieval, and classification.
  • GPT (decoder-only, causal, next-token): token i sees only 0..i, which makes generation natural — chat, code, agents. Every frontier LLM today is decoder-only, because one simple objective scales and prompting recovers the rest.
  • T5 (encoder-decoder, span-corruption): a bidirectional encoder plus a causal decoder with cross-attention — strongest on explicit seq-to-seq (translation, summarization), but mostly displaced by big decoder-only models. Rule of thumb: embeddings → encoder; generation → decoder; otherwise just use a decoder-only model — and don’t use an LLM where an embedding model belongs.

Quick check

Quick check

0/2
Q1You want to build a semantic search system over 10 million internal documents. Which model family do you start with?
Q2Why are virtually all frontier LLMs (Claude, GPT, Llama, Gemini) decoder-only rather than encoder-decoder?

Sign in to track your progress

Completed lessons, your XP, level, and streak save to your account — it's free and takes a few seconds.

Practice this in an interview

All questions
What is the difference between encoder-only, decoder-only, and encoder-decoder transformer architectures?

Encoder-only models build bidirectional context representations suited for classification and embedding tasks. Decoder-only models generate text autoregressively using causal (masked) self-attention and dominate language modelling. Encoder-decoder models use a full encoder to encode the source and a decoder with cross-attention to generate the target, fitting sequence-to-sequence tasks like translation and summarisation.

What is the difference between encoder models like BERT and decoder models like GPT?

BERT is an encoder-only transformer that reads all tokens bidirectionally and is trained with masked language modelling — ideal for tasks requiring a rich contextual representation of an entire sequence, like classification or NER. GPT is a decoder-only transformer that attends only to previous tokens via a causal mask and is trained with next-token prediction — ideal for text generation. Encoder-decoder models like T5 combine both for tasks that map one sequence to another.

How does GloVe differ from Word2Vec in learning word embeddings?

GloVe (Global Vectors) builds a global co-occurrence matrix over the entire corpus and then factorizes it, directly encoding how often pairs of words co-occur. Word2Vec uses local context windows and a prediction objective, never explicitly seeing the global statistics. GloVe tends to capture linear substructures slightly better while Word2Vec handles rare words better with negative sampling.

How does Word2Vec work, and what is the difference between Skip-gram and CBOW?

Word2Vec trains a shallow neural network to predict context from a target word (Skip-gram) or a target word from its context (CBOW), learning dense vector representations as a by-product. Skip-gram works better for rare words; CBOW is faster and suits large corpora.

Why do dense word embeddings outperform one-hot vectors?

One-hot vectors are high-dimensional, sparse, and treat all words as equidistant — they carry zero semantic information. Dense embeddings place similar words close together in a low-dimensional space, enabling models to generalize from seen words to unseen but related ones.

What is the key difference between Word2Vec embeddings and BERT's contextual embeddings?

Word2Vec assigns a single static vector to each word token regardless of surrounding words, so polysemous words like 'bank' always get the same representation. BERT generates a different vector for each token depending on its full sentential context, allowing it to disambiguate meaning on the fly.

Related lessons

Explore further

Skip to content