Search

Search papers and lessons

BART: Denoising Sequence-to-Sequence Pre-training for Text Generation

Mike Lewis, Yinhan Liu, Naman Goyal et al. · 2019

BART pretrains one encoder-decoder Transformer by corrupting real text and learning to rebuild it, so the same model handles both reading tasks and writing tasks after fine-tuning.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Neural network basics
  • Linear algebra
  • Probability
  • Architectures
  • ReadReady to readNot yet

TL;DR

BART is a sequence-to-sequence Transformer pretrained by damaging real text on purpose and training the model to rebuild the original. The encoder reads the damaged version in full, and the decoder writes the clean version one token at a time. After fine-tuning, one pretrained model handled both understanding tasks and text generation, matching RoBERTa on GLUE and SQuAD and beating earlier systems on several summarization, dialogue and question-answering benchmarks.

Builds on: Encoder-Decoder Architecture, Masked Language Modeling, Autoregressive Model, Transformer · Leads to: Fine-Tuning, Language Model

Why it mattered

BART became a standard starting point for systems that have to write text from text. The clearest case is abstractive summarization on XSum, a set of BBC news articles paired with one-sentence summaries. XSum rewards new wording, and the paper notes that extractive systems, which only copy sentences, perform poorly on it. BART improved ROUGE by about 6 points over the previous best model, which was built on BERT.

The same recipe covers dialogue (ConvAI2, where the model reads a persona and the conversation so far), long-form question answering (ELI5, where it writes answers from supporting documents), and machine translation, where a pretrained BART was stacked onto a Romanian-to-English system. Before BART, a team that needed both a classifier and a text writer often trained two different pretrained models. BART showed one model could do both with a single fine-tuning recipe.

It also clarified the design space. Pretraining in 2019 came in three families: encoder-only models that read text (BERT), decoder-only models that write text (GPT), and encoder-decoder models. BART is the encoder-decoder answer, and its controlled comparison of corruption schemes is still a useful reference for anyone choosing a pretraining objective.

How it came about

By 2018 two results were established. Transformers trained on large unlabelled text could be fine-tuned well, as shown by left-to-right models in the GPT line and by BERT. Sequence-to-sequence models had been used for machine translation since 2014, with a separate encoder and decoder trained on labelled pairs. What was missing was a way to pretrain the encoder and decoder together, without labels, so that both were ready for generation tasks.

BART was introduced by Mike Lewis and colleagues at Facebook AI, first as an arXiv preprint in October 2019, and it was later published at the ACL 2020 conference. It caught on because it combined the strengths of the two families in one model and came with a careful comparison of corruption schemes, all trained on the same data with the same code.

The problem

By 2019 two families of pretraining were each good at one thing. Masked language models such as BERT hide some tokens and predict them from both sides. They are strong at understanding text, for example deciding whether two sentences agree or finding an answer span in a paragraph. But they have no decoder, so generating text needs a new output head trained from scratch.

Left-to-right language models can write fluent text, but they read a context only from the past. A question placed at the start of a document gets no visibility of the words that follow it.

Encoder-decoder models could already generate text, but their pretraining was usually one fixed trick such as masking a span and predicting it. The open question was whether a single denoising objective, with a flexible way to damage the input, could train an encoder and a decoder that work well for both kinds of task.

How it works

The diagram shows the pretraining loop. The original text is never shown to the encoder; only its damaged copy is.

flowchart LR
  A["Original text: the cat sat on the mat"] --> B["Corrupt the text with a noising function"]
  B --> C["Encoder reads the damaged text, all positions at once"]
  C --> D["Decoder writes the original, one token at a time"]
  D --> E["Loss: how far each written token is from the original"]

Take one tiny instance, a six-word sentence, and trace it from start to finish.

  1. Start with the original. The text is "the cat sat on the mat". It has six words, and we treat each word as one token.

  2. Corrupt it with text infilling. BART draws a span length from a Poisson distribution with mean 3, so the length is a whole number that is usually between 0 and 5. Say the draw is 2 and the span is the words "cat sat". The whole span is replaced by one [MASK] token, giving "the [MASK] on the mat". Check: six words went in and five tokens came out, because two words were removed and one mask was added.

  3. Encode the damaged text. The encoder is a stack of Transformer layers that uses attention with no left-to-right restriction. Each token's representation can see the words on both sides, so the [MASK] vector knows "the" before it and "on the mat" after it. Check: the encoder produces five vectors, one for each input token.

  4. Decode the original. The decoder starts from a start symbol and must output "the cat sat on the mat". At every step it looks at the words it has already written, and it also looks at the encoder's output through cross-attention.

    Check: the decoder must write six tokens from five inputs. It has to work out that the mask stood for two words, which BERT never has to do.

  5. Score every written token. For each position in the original, the loss takes the probability the decoder gave to the true token and penalises it with Cross-Entropy Loss. A correct, confident token costs almost nothing. A token the decoder got wrong costs a lot.

  6. Update and repeat. The gradients from that loss adjust every weight in the encoder and decoder. The same step is repeated over a very large corpus, each document with a fresh random corruption. Check: the model never sees the clean sentence as input, yet it is trained to produce it.

The paper tries five corruptions in total. Token masking (from BERT) replaces random tokens with [MASK]. Token deletion removes random tokens, so the model must find the gaps itself. Text infilling, the main novelty, replaces whole spans with a single mask. Sentence permutation shuffles the sentences of a document. Document rotation starts the document at a random token, which teaches the model where a document begins. The best combination in the paper's ablations was text infilling together with sentence permutation.

Worked examples

Example
1. One span, one mask, by hand
  1. Original text: "the cat sat on the mat", which is 6 tokens.
  2. The span length drawn is 2, and the span is "cat sat".
  3. Replace the span with a single [MASK]: "the [MASK] on the mat", which is 5 tokens.
  4. Check the count: 6 original tokens, minus 2 removed, plus 1 mask added, equals 5.
  5. Interpretation: the decoder must write 6 tokens from an input of 5, so it has to recover that the mask hid two words and what those two words were.
Example
2. Scoring the decoder's guesses

Suppose the decoder gives the true next tokens these probabilities: 0.5 for the first, 0.8 for the second and 0.25 for the third.

  1. Per-token losses are -ln(0.5) = 0.693, -ln(0.8) = 0.223 and -ln(0.25) = 1.386.
  2. Add them: 0.693 + 0.223 + 1.386 = 2.302 nats.
  3. Divide by the number of tokens: 2.302 / 3 = 0.767 nats per token.
  4. Interpretation: the third token is the one the model was least sure of, and it contributes most of the loss. A perfect model gives probability 1 to every true token, and its loss is 0. Training pushes the average down.
Example
3. A realistic document, with a try-it at the end

Take a three-sentence news snippet of nine words: "Rain fell all day. The river rose. Farmers waited."

  1. Sentence permutation shuffles the sentences into "Farmers waited. Rain fell all day. The river rose."
  2. Text infilling then picks the span "fell all" (length 2) and replaces it with one mask, giving "Farmers waited. Rain [MASK] day. The river rose." That is eight tokens.
  3. The encoder reads those eight tokens with full context in both directions.
  4. The decoder must write the original nine words in their original order: "Rain fell all day. The river rose. Farmers waited." It has to undo the shuffle and restore the missing words in one pass.
  5. The check: nine original words, minus two removed, plus one mask, is eight, as in step 2.

Try it: a 40-token document has three infilled spans of lengths 2, 0 and 4. How many tokens long is the encoder input? Answer: 40 - (2 + 0 + 4) + 3 = 37 tokens. Each span, including the zero-length one, contributes exactly one mask, so three masks replace the removed words.

The math

Definition: Denoising objective

Let x = (x_1, ..., x_T) be an original document of T tokens, and let g be a noising function that turns x into a damaged version g(x). BART learns parameters θ that make the original as likely as possible given the damaged copy. Written as a loss to minimise, this is the negative log-likelihood of the original.

L(θ)=−∑t=1Tlog⁡pθ(xt∣x<t, g(x))\mathcal{L}(\theta) = -\sum_{t=1}^{T} \log p_\theta\left(x_t \mid x_{\lt t},\, g(x)\right)

Here x_t is the token at position t, the subscript <t\lt t collects everything before it, which is what the decoder has already written, and p_θ is the probability the model assigns to the next token. The sum runs over the whole original document.

Step 2 in symbols

Text infilling draws each span length l from a Poisson distribution with mean λ = 3:

P(l=k)=λke−λk!,k=0,1,2,…P(l = k) = \frac{\lambda^{k} e^{-\lambda}}{k!}, \qquad k = 0, 1, 2, \dots

Plugging in λ = 3 gives P(l = 0) = e^-3 ≈ 0.050 and P(l = 3) = 27 e^-3 / 6 ≈ 0.224. A length of 0 is allowed: it inserts a [MASK] without removing any word, which teaches the model to notice that something is missing. The paper contrasts this with SpanBERT, which replaces a span with one mask per word it removed, so the model learns the exact gap length for free.

Steps 3 and 4 in symbols

The encoder maps the damaged text to hidden states h:

h=Encoder(g(x))h = \mathrm{Encoder}\big(g(x)\big)

At each decoder position t, the decoder mixes the words it has written so far (self-attention over the earlier positions) with the encoder states h (cross-attention), and a Softmax over the vocabulary turns the result into probabilities:

pθ(xt∣x<t,g(x))=softmax(W dt),dt=Decoder(x<t,h)p_\theta\left(x_t \mid x_{\lt t}, g(x)\right) = \mathrm{softmax}\big(W\, d_t\big), \qquad d_t = \mathrm{Decoder}\big(x_{\lt t}, h\big)

Here d_t is the decoder's state at position t and W maps it to one score per vocabulary entry. During training the decoder is fed the true previous tokens, which is the Teacher Forcing setup.

Step 5 in symbols

The per-token loss is -log p, the negative log of the probability given to the true token. The total loss is the sum of these losses over positions, which is exactly the formula above. Because the logarithm of a probability is negative, the leading minus sign makes the loss positive.

Results

The paper's main claim is that BART is competitive on understanding tasks and clearly better on generation. The numbers below are the paper's own.

  • Understanding (Table 2, large models). On SQuAD 1.1, BART scored 88.8 EM and 94.6 F1, against 88.9 and 94.6 for RoBERTa. On MNLI it scored 89.9 and 90.1 against RoBERTa's 90.2 and 90.2. On SST it scored 96.6 accuracy against RoBERTa's 96.4. On RTE it was higher (87.0 against 86.6), and on CoLA it was clearly lower (62.8 against 68.0). "Comparable" is the fair word.
  • Summarization (Table 3). On XSum BART improved ROUGE by roughly 6 points over the best earlier model, which used BERT. On CNN/DailyMail it also beat previous work on all metrics.
  • Machine translation. On WMT16 Romanian-to-English, adding a pretrained BART as the decoder gave a gain of about 1.1 BLEU over a strong back-translation baseline, which the paper reports as its headline translation result.
  • Ablations. The corruption comparison is run with one controlled setup. The paper's conclusion is qualitative: text infilling, alone or with sentence shuffling, performs best across tasks, and document rotation performs worst. Read the ablation table for the exact numbers.

Common mistakes and tradeoffs

It is tempting to think BART is just BERT with a decoder bolted on. In fact the objectives differ: BERT predicts the masked tokens in place, while BART regenerates the entire original sequence, so its output can be longer or shorter than its damaged input. That is why BART can be used for generation without a new head.

It is tempting to think that more corruption is always better. In fact the ablations show the choice matters a lot. Document rotation alone does worst of the schemes compared, because the damage has to leave a task the model can learn from the damaged input.

It is tempting to think a BART model writes useful text straight after pretraining. In fact the pretrained model has only learned to repair damage. It is fine-tuned on each downstream task before it is useful for that task.

Use BART-style models when the job is to read a source and write new text from it: summarization, translation, dialogue, or answering from a document. Use encoder-only models such as BERT when the job is classification or span extraction and you want cheap inference, because they do not run a decoder at all. Decoder-only language models are the other alternative, and they are the natural choice when one general model has to take many different instructions in a prompt.

Two practical costs are worth knowing. Generation needs one decoder pass for every output token, so long outputs are slow. And the decoder layers add parameters on top of an encoder of BERT's size, so training needs more memory than an encoder-only model of the same depth.

Reading guide

The chain of ideas in the paper, in the order the lesson built them: a Transformer encoder-decoder, a denoising objective that corrupts and rebuilds, a set of corruption schemes compared in one controlled setup, fine-tuning the same model for classification and generation, and results that match RoBERTa on understanding and beat earlier systems on generation.

The paper's sections map to prerequisites as follows. The introduction assumes Masked Language Modeling and Transformer. Section 2 (Model) uses Encoder-Decoder Architecture, Autoregressive Model and Gaussian Error Linear Unit (GELU) activations. The pretraining objectives in section 2.2 use Cross-Entropy Loss and Poisson Distribution. Section 3 (fine-tuning) uses Fine-Tuning and Teacher Forcing during training. The ablation experiments compare pretraining objectives, and the translation results use BLEU Score. For the comparison point, read the RoBERTa paper, which BART is measured against.

Check yourself

QuestionWhat does the BART decoder learn to predict during pretraining?Show answer

The original, uncorrupted text, one token at a time, conditioned on the tokens it has already written and on the encoder's reading of the damaged input.

QuestionA 20-token text has three infilled spans of lengths 0, 1 and 2. How many tokens does the encoder input contain?Show answer

20 - (0 + 1 + 2) + 3 = 20. The three spans remove three tokens in total and the three masks add three back, so the length is unchanged.

QuestionWhy does text infilling teach the model more than plain token masking does?Show answer

A single mask gives no hint about how many words it replaced. The model has to predict the length of the gap as well as its content, a skill that token masking never trains: deciding how much text is missing.

QuestionYou need a model that writes a summary of customer support tickets. Why might a BART-style starting point suit this better than a BERT-style one?Show answer

The output is new text of a different length from the input, which is the situation BART was pretrained for, since its decoder regenerates a sequence. A BERT-style encoder has no decoder, so summarization would need a new output head trained from scratch, while BART already has one and was measured on abstractive summarization.

Sources

Read the full paper

Mike Lewis, Yinhan Liu, Naman Goyal et al., 2019. arXiv:1910.13461