Encoder–decoder models: T5, BART and the "text-to-text" format

Encoder–Decoder Models: T5, BART and Text-to-Text

Why we need the encoder and the decoder together, how T5 unified all tasks in a "text-to-text" format with the span corruption objective, how BART trained a bidirectional encoder and a causal decoder to remove various kinds of noise, the multilingual and Arabic versions (mT5, AraT5 and mBART), and when to choose each family.

Lesson 25 of 38Intermediate40 minutes

Lesson objectives

  • Explain the "text-to-text" format and the span corruption objective in T5.
  • Compare the noising functions in BART.
  • Choose between encoder-only, decoder-only and encoder–decoder models.
Page contents
  1. Three families from one model
  2. T5: everything is text to text
  3. BART: denoising
  4. Multilingual models and Arabic
  5. When should you choose an encoder–decoder?

Three families from one model

FamilyAttentionTypical pretraining objectiveExamplesBest suited for
Encoder onlyBidirectionalPredicting masked tokensBERT, RoBERTa, AraBERTClassification, tagging, retrieval
Decoder onlyCausalThe next tokenGPT, LLaMA, JaisOpen-ended generation, general assistants
The encoder–decoderBidirectional in the encoder + causal and cross in the decoderDenoising / span corruptionT5, BART, mT5, AraT5Translation, summarization, paraphrasing, question generation

In an encoder–decoder, the input is read in full and in both directions once, then the decoder generates the output while looking at the input representation through cross-attention. This suits cases where the input and output are clearly different texts.

Encoder-decoder model (T5 and BART) Encoder–decoder
Thank you <X> me to your party <Y>N×Encoderbidirectional self-attentionN×Decodercausal self-attention+ cross-attention<start> <X> for inviting <Y> lastK, V<X> for inviting <Y> last <Z>T5: span corruptionBART: denoising

The encoder reads the whole input in both directions, and the decoder generates the output causally with cross-attention over the encoder's representations. Pre-training corrupts the text and then reconstructs it.

T5: everything is text to text

Raffel et al. (2020) introduced T5 (Text-to-Text Transfer Transformer), whose idea is that every task is formulated as an input text and an output text:

Input: translate English to German: That is good.     → Output: Das ist gut.
Input: summarize: [long article]                          → Output: [summary]
Input: cola sentence: The course is jumping well.      → Output: not acceptable
Input: stsb sentence1: ... sentence2: ...               → Output: 3.8

Even regression (a similarity score) is generated as text. The benefit: one architecture, one loss function and one decoding method for all tasks.

The span corruption objective (Span Corruption)

Random spans are selected (with an average length of 3 tokens, about 15% of the tokens overall), each is replaced with a unique sentinel token, and the task is to generate only the removed spans:

Original:  Thank you for inviting me to your party last week.
Input: Thank you <X> me to your party <Y> week.
Target:  <X> for inviting <Y> last <Z>

This is more efficient than predicting the full sequence because the output is short.

A systematic study

The paper is really a large empirical study comparing architectures, training objectives, data, transfer strategies and size. It produced the C4 (Colossal Clean Crawled Corpus): about 750 GB of English Common Crawl text cleaned with rules (removing short pages, code, offensive words and duplicates). Its largest model is T5-11B with 11 billion parameters.

BART: denoising

Lewis et al. (2020) introduced BART: a bidirectional encoder (like BERT) with a causal decoder (like GPT), trained to reconstruct the original text from a corrupted version. Several corruption functions were tried:

  1. Token masking (like BERT).
  2. Token deletion: the model has to locate the deletions.
  3. Text infilling: a span of random length (Poisson distribution with mean 3) is replaced with a single mask, so the model has to work out how many tokens are missing.
  4. Sentence permutation.
  5. Document rotation: the document starts from a random token, and the model has to find the start.

The best results overall came from combining text infilling and sentence permutation. BART achieved strong results in summarization (CNN/DailyMail and XSum), with performance close to RoBERTa on understanding tasks.

Multilingual models and Arabic

  • mBART (2020): BART trained on 25 languages including Arabic, especially useful for translation.
  • mT5 (Xue et al., 2021): T5 trained on mC4 (101 languages), in sizes up to 13 billion parameters, with a SentencePiece vocabulary of 250 thousand tokens.
  • AraT5 (Nagoudi et al., 2022): Arabic T5 models (MSA, tweets and mixed), with the ARGEN benchmark for evaluating Arabic generation (translation, summarization, title generation, paraphrasing, dialect transfer…). They outperformed mT5 on most of the benchmark's tasks.

When should you choose an encoder–decoder?

  • Translation or summarization on a limited compute budget: a fine-tuned medium encoder–decoder model is often more efficient than a large decoder.
  • Tasks with a long input and a short output: the encoder reads the input only once.
  • For general multi-task assistants and open dialogue, however, decoder-only models have prevailed because they are simpler to scale and to train on diverse instructions.

Lesson summary

  • The encoder–decoder suits tasks where a complete input is transformed into a different output (translation, summarization, paraphrasing).
  • T5 turns every task into an input text and an output text, with a prefix describing the task.
  • BART learns to reconstruct the original text from a version corrupted in several ways.

Check what you understood

Choose an answer for each question to see the correction and the reasoning. Your answers are not sent anywhere.

  1. How does T5 formulate the translation task?

  2. Which noising function in BART gave the best results overall, combined with sentence permutation?

References

  1. Raffel, C. et al. (2020). Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer. JMLR 21 arxiv.org/abs/1910.10683
  2. Lewis, M. et al. (2020). BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension. ACL arxiv.org/abs/1910.13461
  3. Xue, L. et al. (2021). mT5: A Massively Multilingual Pre-trained Text-to-Text Transformer. NAACL arxiv.org/abs/2010.11934
  4. Nagoudi, E. M. B., Elmadany, A. & Abdul-Mageed, M. (2022). AraT5: Text-to-Text Transformers for Arabic Language Generation. ACL arxiv.org/abs/2109.12068
  5. Liu, Y. et al. (2020). Multilingual Denoising Pre-training for Neural Machine Translation (mBART). TACL arxiv.org/abs/2001.08210