Pre-trained encoders such as BERT give excellent representations, but they cannot generate text: they predict masked tokens from context on both sides, not the next token. Many tasks, such as summarization, translation, and question answering with free-form answers, need an output that is itself a piece of text. Encoder–decoder models pre-train the full sequence-to-sequence Transformer, so the decoder already knows how to write before fine-tuning starts.
This reading covers the two best-known examples: BART (Lewis et al., 2020) and T5 (Raffel et al., 2020).
1Three families of pre-training
| Family | Pre-training objective | Examples |
|---|---|---|
| Encoder-only | predict masked tokens from both sides | BERT, RoBERTa |
| Encoder–decoder | the encoder reads a corrupted text; the decoder generates the original (or the missing parts) left to right | BART, T5 |
| Decoder-only | predict the next token | GPT |
An encoder–decoder model combines the strengths of the other two: a bidirectional encoder builds a representation of the input using context on both sides, and an autoregressive decoder generates the output one token at a time, attending to the encoder through cross-attention.
Denoising autoencoders
How do we pre-train such a model without labels? Take a clean text, add noise to it (hide, delete, or shuffle parts), feed the corrupted text to the encoder, and train the decoder to generate the original text. This is a denoising autoencoder. To undo the noise, the model must understand the input (the encoder's job) and write fluent text (the decoder's job), which is exactly what downstream generation tasks need.
2BART
BART (Bidirectional and Auto-Regressive Transformers) is a standard Transformer encoder–decoder pre-trained as a denoising autoencoder. The paper compared five ways of adding noise:
- Token masking: replace random tokens with
<mask>(as in BERT). - Token deletion: delete random tokens. The model must also figure out where tokens are missing.
- Text infilling: replace random spans with a single
<mask>. Span lengths are drawn from a Poisson distribution with mean 3, and a span can have length 0. The model must predict how many tokens are missing, not just which ones. - Sentence permutation: shuffle the order of the sentences.
- Document rotation: pick a random token and rotate the document so that it starts there. The model must find the true start.
Which noise works best?
In BART's comparison, text infilling was the most consistently strong objective. Sentence permutation and document rotation performed poorly on their own: shuffling or rotating does not hide any content, so the model can copy most of the input. The final BART model combines text infilling (masking about 30% of tokens) with sentence permutation, and was pre-trained on the same 160GB of text as RoBERTa.
facebook/bart-large model (no fine-tuning) with beam search. Each input has one <mask>; BART generates the complete sentence and decides for itself how many words the mask should become.Fine-tuning BART
- Sentence classification: feed the same input to both the encoder and the decoder, and put a classifier on the decoder's final token. That token has attended to the entire input, so it plays the role of BERT's
[CLS]. - Token classification (e.g., extractive QA): feed the full document to both, and use the decoder's top hidden state for each token.
- Sequence generation (summarization, abstractive QA): the input goes to the encoder and the decoder generates the output, exactly like pre-training.
- Translation: BART was pre-trained on English only, so the paper replaces BART's input embedding layer with a new, small, randomly initialized encoder that maps the foreign language into a representation BART can read.
Results
On understanding tasks, BART-large performs about as well as RoBERTa-large (e.g., SQuAD 1.1 F1 94.6 vs. 94.6; MNLI 89.9 vs. 90.2; SST-2 96.6 vs. 96.4). So adding a generative decoder costs nothing on classification. On generation tasks, BART was clearly better than earlier systems:
| Task | Metric | Previous best | BART |
|---|---|---|---|
| CNN/DailyMail summarization | ROUGE-1 | 42.13 | 44.16 |
| XSum summarization | ROUGE-1 | 38.81 | 45.14 |
| ELI5 abstractive QA | ROUGE-1 | 28.9 | 30.6 |
| WMT16 Romanian→English | BLEU | 36.80 | 37.96 |
| Model | Encoder layers | Decoder layers | Hidden size | Heads | Parameters |
|---|---|---|---|---|---|
| BART-base | 6 | 6 | 768 | 12 | ≈139M |
| BART-large | 12 | 12 | 1024 | 16 | ≈406M |
3T5: everything is text-to-text
With BART, classification and generation still use different setups: a classifier head for one, the decoder's text output for the other. T5 (Text-to-Text Transfer Transformer) removes the difference: every task takes text as input and produces text as output. A short task prefix tells the model what to do, and the label itself is written as a word.
- Translation:
translate English to German: That is good.→Das ist gut. - Sentiment:
sst2 sentence: …→positive(not the class index 1) - Even regression: STS-B similarity scores (0–5) are rounded to the nearest 0.2 and written as a string, e.g.
3.8.
One model, one loss (cross-entropy on the output tokens), and one decoding procedure for every task.
t5-base checkpoint. Since this checkpoint was trained on a mixture that includes these tasks, the same model translates, judges grammaticality, classifies sentiment, scores similarity, and summarizes, chosen only by the prefix.Pre-training: span corruption
T5 is pre-trained on C4 (Colossal Clean Crawled Corpus), about 750GB of English web text cleaned from Common Crawl. The objective is a sequence-to-sequence version of masked language modeling:
- Corrupt about 15% of the tokens, in spans with an average length of 3.
- Replace each span with a unique sentinel token:
<X>,<Y>, … (in the code,<extra_id_0>,<extra_id_1>, …). - The target contains only the dropped spans, each introduced by its sentinel, followed by a final sentinel.
Unlike BART, the decoder does not rewrite the whole input, so targets are short and training is cheaper.
Here is what the real t5-base model generates for a few span-corrupted inputs:
The predictions are fluent and grammatical, but not always correct: Texas A&M is in College Station, not Austin. Plausible-sounding mistakes like this are a general property of models trained only to fill in text. (BART-large happened to get this one right in Figure 2.)
Multi-task training
Because every task has the same format, many tasks can be mixed into one training set. The T5 paper compared several strategies. Training on a multi-task mixture alone was slightly worse than the standard “pre-train, then fine-tune”. The best results came from multi-task pre-training (unsupervised span corruption mixed with the supervised tasks), followed by fine-tuning on each task. The released T5 checkpoints were trained this way, which is why t5-base in Figure 3 already responds to task prefixes.
Relative position bias
T5 does not add position embeddings to the input. Instead, each attention head adds a learned scalar bias to the attention score between positions $i$ and $j$, and the bias depends only on the relative distance $j-i$:
$$\text{score}(i,j)=\frac{q_i\cdot k_j}{\sqrt{d}}+b_{\text{bucket}(j-i)}$$Distances are grouped into 32 buckets (16 for each direction). Small distances get their own bucket; larger ones share buckets whose width grows logarithmically, and everything beyond 128 tokens falls into the last bucket. The idea: the difference between being 1 and 2 tokens away matters a lot, the difference between 100 and 101 hardly at all. Each head learns its own biases, shared across layers.
Model sizes and results
| Model | T5-Small | T5-Base | T5-Large | T5-3B | T5-11B |
|---|---|---|---|---|---|
| Parameters | ≈60M | ≈220M | ≈770M | ≈3B | ≈11B |
Scaling up kept helping: T5-11B reached state-of-the-art results on GLUE, SuperGLUE, SQuAD, and CNN/DailyMail at the time. On SuperGLUE, a harder successor to GLUE, it averaged 88.9, close to the human baseline of 89.8.
The T5 paper is also a large controlled study. Among its findings: an encoder–decoder with a denoising objective worked best among the architectures compared, span corruption was slightly better than BERT-style masking and cheaper to train, and cleaner, larger pre-training data helped.
4BART vs. T5
| BART | T5 | |
|---|---|---|
| Noise | text infilling + sentence permutation | span corruption with sentinel tokens |
| Target | the entire original text | only the dropped spans |
| Positions | learned absolute embeddings | relative position bias |
| Task format | classification head or text generation | always text-to-text, with a task prefix |
| Data | 160GB (as RoBERTa) | C4, ≈750GB |
| Sizes | 139M, 406M | 60M to 11B |
Both families are still widely used, especially for summarization and other tasks with a clear input and output. Follow-up models include mT5 (T5 trained on 101 languages) and Flan-T5 (T5 further trained on a large collection of tasks written as instructions).
5Summary
- Encoder–decoder models pre-train both a bidirectional encoder and an autoregressive decoder, so they are ready for generation.
- Denoising autoencoder: corrupt the input, then generate the original.
- BART: text infilling (Poisson-length spans, one mask per span) works best; matches RoBERTa on classification and beats prior work on generation.
- T5: every task is text-to-text with a task prefix; span corruption with sentinel tokens; relative position bias; scales to 11B parameters.
6Practice
Exercise 1: span corruption
Original: the cat sat on the mat and looked at the door. The words “sat on” and “the door” are corrupted. Write T5's input and target.
the cat <X> the mat and looked at <Y>Target:
<X> sat on <Y> the door <Z>Each span gets one sentinel; the target ends with the next sentinel.
Exercise 2: text infilling vs. token masking
Why is text infilling harder than BERT-style token masking?
<mask> stands for exactly one token, so the model knows how many tokens are missing. In text infilling, one <mask> can stand for 0, 1, 2, or more tokens, so the model must also decide the length of the missing text.
Exercise 3: text-to-text
How would you write a natural language inference example (premise: “A man is playing soccer.” hypothesis: “A person is outdoors.” label: entailment) for T5?
mnli hypothesis: A person is outdoors. premise: A man is playing soccer.Target:
entailmentThe label is written as a word, and the model is trained to generate it.
Quick quiz
7Further reading
- Lewis et al. (2020). BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension. ACL.
- Raffel et al. (2020). Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer. JMLR.
- Google Research Blog. Exploring Transfer Learning with T5. (A short, readable overview of the T5 paper.)