Foundations to Frontier
Tests
E. Deep Learning › E12 Transformer Architectures

BART

BART (Lewis and colleagues, ACL 2020) is a denoising autoencoder for sequence-to-sequence pre-training: corrupt a document with an arbitrary noising function, then train a bidirectional encoder and an autoregressive decoder to reconstruct the original. Its noising functions include token masking, token deletion, text infilling (spans of Poisson-distributed length replaced by a single mask), sentence permutation and document rotation. Infilling plus sentence permutation worked best, and BART became a strong model for summarisation and generation. This page covers the noising functions, architecture and fine-tuning. In the exercises you implement infilling, permutation and rotation, then train a small encoder–decoder to undo three kinds of noise on mirrored sequences. Masking is easiest to undo (token accuracy 0.891), infilling harder (0.773) because span lengths are hidden, and deletion hardest (0.553) because positions are hidden too.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Repairing damaged documents

BART learns by repairing text that has been damaged on purpose:

  • words blanked out;
  • words deleted;
  • chunks replaced by a single blank;
  • sentences shuffled.

A reader network looks at the whole damaged text, and a writer network rewrites the original word by word. Because it learns to write complete text, it is good at summaries and rewrites. The harder the damage, the more the model must understand, as the research exercise measures. The demo shows the encoder’s and decoder’s attention patterns.

Transformer lab
Computed pairs: 136 of 256 for 16 tokens. Sparse patterns grow linearly with length instead of quadratically.
Why engineers care

Denoising sequence-to-sequence models summarise reports, rewrite instructions and correct noisy text, such as speech-recognised or OCR’d maintenance logs.

📝 Quick check (Level 0)
0/3 answered
  1. 1. BART is trained to…
  2. 2. BART's architecture:
  3. 3. BART is strong at…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Noising functions:
    • token masking (as in BERT);
    • token deletion: remove tokens, so the model must find where;
    • text infilling: replace spans, with Poisson(λ = 3) lengths, by a single <mask>. A length-0 span inserts a mask, which happens with probability e⁻³ = 5%;
    • sentence permutation: 5 sentences have 120 orders;
    • document rotation: start the document at a random token.
  • Example: infilling “ABCDEFG” with spans (start 1, length 2) and (start 5, length 0) gives A <mask> D E <mask> F G.
  • Architecture:
    • a standard Transformer encoder–decoder with GeLU;
    • BART-large has 12 + 12 layers, d = 1024, about 406 M parameters;
    • the decoder cross-attends to the encoder, as in the original Transformer.
  • Loss: cross-entropy of the decoder reconstructing the full original document.
  • Fine-tuning:
    • classification: feed the same input to encoder and decoder and use the final decoder token;
    • generation (summaries, translation): fine-tune end to end.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Infilling replaces a span with…
  2. 2. Probability a Poisson(3) span is empty:
  3. 3. BART-large parameters:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

BART in code

Exercise 1.

  • Text infilling replaced whole spans, including an empty one, by single masks.
  • Rotation and sentence permutation behaved as specified.
  • Simulated Poisson(3) span lengths had mean 3.0 and 5% zeros.

Practice.

  • Hugging Face BartForConditionalGeneration with facebook/bart-large or facebook/bart-large-cnn (summarisation).
  • BART used 30% of tokens masked through infilling, plus sentence permutation, for its main model.
  • Use generate() with beam search and length penalties for summaries (E11-14).
  • mBART applies the same idea to many languages for translation.
from transformers import BartForConditionalGeneration, BartTokenizerFast
tok = BartTokenizerFast.from_pretrained("facebook/bart-large-cnn")
model = BartForConditionalGeneration.from_pretrained("facebook/bart-large-cnn")
ids = tok(["During climb the crew noted a hydraulic pressure drop ..."], return_tensors="pt").input_ids
print(tok.decode(model.generate(ids, num_beams=4, max_new_tokens=40)[0], skip_special_tokens=True))
🐍 Exercise 1: noising functions
Implement text infilling, sentence permutation, document rotation and Poisson span lengths.
📝 Quick check (Level 2)
0/3 answered
  1. 1. BART's main model masked about…
  2. 2. A summarisation checkpoint:
  3. 3. Multilingual BART:

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why an arbitrary noising function.

  • BERT’s masking corrupts tokens in place; GPT has no corruption.
  • A seq2seq denoiser can undo any corruption, including changes in length and order, because the decoder writes the whole document.
  • Harder corruptions force the model to infer more:
    • where tokens are missing (deletion);
    • how many (infilling);
    • how a text is ordered (permutation, rotation).

What worked.

  • Lewis et al. compared noising functions with matched setups.
  • Text infilling was consistently strong.
  • Document rotation and sentence permutation alone were weak.
  • Infilling combined with sentence permutation gave the best overall results, especially for generation tasks.

Difficulty versus usefulness. A pretext task must be hard enough to require understanding but solvable from context. Exercise 2 measures how hard three corruptions are to undo, an ingredient of, but not the same as, their usefulness for downstream tasks.

📝 Quick check (Level 3)
0/3 answered
  1. 1. A seq2seq denoiser can undo corruptions that change…
  2. 2. Which noising worked best in BART?
  3. 3. Deletion additionally hides…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Which corruption is hardest to undo?

History.

  • M. Lewis, Y. Liu, N. Goyal, M. Ghazvininejad, A. Mohamed, O. Levy, V. Stoyanov and L. Zettlemoyer, “BART: denoising sequence-to-sequence pre-training for natural language generation, translation, and comprehension” (ACL 2020).
  • mBART extended it to multilingual translation (2020).

Exercise 2.

  • Model: the tanh-RNN encoder–decoder of E11-8 (48 units), trained for 800 steps to reconstruct mirrored 8-token sequences (vocabulary 6).
  • Noise types:
    • masking: 25% of tokens replaced by a mask;
    • deletion: 25% removed, padded at the end;
    • infilling: one span of 1–3 tokens replaced by a single mask.
  • Measurement: token accuracy and exact reconstruction of whole sequences with greedy decoding (500 test sequences, seed 0).
noise token accuracy sequences exact
masking 0.891 0.596
infilling 0.773 0.372
deletion 0.553 0.158

A second seed gave the same order: 0.915, 0.779 and 0.569 (exact 0.682, 0.416 and 0.168).

  • Masking is easiest: positions are kept, so each masked token’s partner identifies it.
  • Infilling is harder: the model must also infer how many tokens a mask hides.
  • Deletion is hardest: the model must infer where and how many tokens are missing, and every later token shifts.
  • Harder corruption demands more inference from context, consistent with BART’s finding that infilling taught more than plain masking. Usefulness downstream was measured in the paper, not here.
🐍 Exercise 2 (research): which corruption is hardest to undo?
Train an encoder–decoder to reconstruct sequences corrupted by masking, deletion or span infilling.

BART in AI, robotics and aerospace

  • Report summarisation: fine-tuned BART-style models condense incident and maintenance reports.
  • Noisy text correction: denoising pre-training suits repairing speech-recognised or OCR’d logs.
  • Data augmentation: BART’s noising functions themselves create robust training variants of text.

References

  1. M. Lewis et al. (2020). “BART: denoising sequence-to-sequence pre-training for natural language generation, translation, and comprehension.” Proc. ACL.
  2. Y. Liu et al. (2020). “Multilingual denoising pre-training for neural machine translation.” TACL 8.