Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Every task is “text in, text out”
T5 treats every language task the same way: give it some text and it writes some text back. To translate: “translate English to German: The engine is hot.” To classify: “sst2 sentence: great product” and it writes “positive”. One model, one training method, many tasks. It learns first by repairing sentences with missing chunks, then by practising the tasks. The demo shows the attention patterns of an encoder–decoder.
A single text-to-text model with task prefixes simplifies deployment: one service for summarising reports, extracting fields and answering questions.
- 1. T5 treats every task as…
- 2. The task is specified by…
- 3. T5's architecture:
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Text-to-text examples:
- translation: “translate English to German: That is good.” → “Das ist gut.”;
- classification outputs a label word;
- STS-B regression becomes text by rounding scores from 0 to 5 to the nearest 0.2 (3.62 → “3.6”).
- Span corruption:
- choose 15% of tokens in contiguous spans (mean length 3) and replace each span in the input by one sentinel;
- the target lists each sentinel followed by its span;
- for [1, 2, 3, 4, 5] with noise spans (2, 3) and (5): the input is [1, ⟨s0⟩, 4, ⟨s1⟩] and the target [⟨s0⟩, 2, 3, ⟨s1⟩, 5].
- Lengths:
- with n = round(0.15·L) noise tokens and s = round(n/mean span) spans, the input has L − n + s tokens and the target n + s;
- for L = 512 and span 3: n = 77 and s = 26, so the input has 461 tokens and the target 103.
- Sizes: small 60 M, base 220 M (a T5-base-like count gives 222.9 M), large 770 M, 3 B and 11 B.
- Data: C4 (Colossal Clean Crawled Corpus), about 750 GB of cleaned web text, roughly 170 B tokens at 4.5 bytes per token.
- 1. Target length for L = 512, 15% noise, mean span 3:
- 2. Each noise span in the input becomes…
- 3. C4 is about…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.T5 in code
Exercise 1.
- Task conversion produced prefixed inputs and textual targets.
- The parameter count of a T5-base-like model came to 222,903,552: shared embedding, bias-free layers, relative-position buckets.
Practice.
- Hugging Face
T5ForConditionalGenerationwithT5TokenizerFast(SentencePiece, 32 K vocabulary). - Fine-tune multiple tasks together by mixing examples with their prefixes.
- Architecture details: pre-LayerNorm with RMS-style scaling and no bias, no bias terms in dense layers, relative-position biases (32 buckets, E12-3) shared across layers, and a shared input/output embedding.
- Successors: mT5 (multilingual), Flan-T5 (instruction-tuned on over 1,000 tasks) and UL2 (mixture of denoisers).
from transformers import T5ForConditionalGeneration, T5TokenizerFast
tok = T5TokenizerFast.from_pretrained("t5-base"); model = T5ForConditionalGeneration.from_pretrained("t5-base")
batch = tok(["summarize: Hydraulic pressure dropped during climb; crew followed checklist."], return_tensors="pt")
print(tok.decode(model.generate(**batch, max_new_tokens=20)[0], skip_special_tokens=True))- 1. T5's tokeniser:
- 2. T5's position encoding:
- 3. An instruction-tuned successor:
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why text-to-text.
- One format means one model, one loss (cross-entropy on output tokens) and one decoding procedure for all tasks.
- Systematic comparisons become fair: only the data changes.
- Multi-task training is mixing datasets.
- Even regression and classification become generation, which works but loses calibrated scores unless handled with care.
What the study found.
- Raffel et al. compared architectures (encoder–decoder, decoder-only, prefix LM), objectives, corruption rates and span lengths, datasets and scale.
- Encoder–decoder with a denoising objective worked best.
- Corruption rates around 15% and moderate spans performed similarly; span corruption was chosen partly because it shortens targets and speeds training.
- Clean, large data mattered; scale helped most.
Span corruption’s efficiency.
- Replacing a whole span by one sentinel makes inputs shorter than BERT’s, and targets contain only the spans plus a few sentinels.
- With mean span 1 every masked token needs its own sentinel: a target of 154 tokens. Mean span 3 needs 103 (exercise 2).
- 1. T5's study found best:
- 2. Span corruption was chosen partly because it…
- 3. Turning regression into text can lose…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.How does span length change the work?
History.
- C. Raffel, N. Shazeer, A. Roberts, K. Lee, S. Narang, M. Matena, Y. Zhou, W. Li and P. Liu, “Exploring the limits of transfer learning with a unified text-to-text Transformer” (JMLR 2020).
- T5-11B scored 88.9 on SuperGLUE, near the 89.8 human baseline.
- Flan-T5 (Chung et al., 2022) instruction-tuned T5.
Exercise 2.
- Method: implement T5’s span corruption (noise masks with random span partitions, sentinels) on 512-token sequences at 15% noise.
- Check: every corrupted input and target reconstruct the original exactly, over 200 random masks per setting.
- Measurement: input and target lengths for mean spans 1–10.
| mean span | input length | target length | total tokens |
|---|---|---|---|
| 1 | 512 | 154 | 666 |
| 2 | 473 | 115 | 588 |
| 3 | 461 | 103 | 564 |
| 5 | 450 | 92 | 542 |
| 10 | 443 | 85 | 528 |
BERT-style masking keeps a 512-token input and predicts 76.8 tokens.
- Lengths are exact, not averages: with n noise tokens in s spans, the input has L − n + s and the target n + s.
- Longer spans save sentinels. Span 3 cuts the target by a third against span 1, and the decoder’s work (quadratic in target length for self-attention) by more.
- The savings flatten: span 10 saves little over span 5. Raffel et al. found quality similar across modest span lengths, so span 3 balances efficiency with keeping a realistic denoising task.
- This is an exact computation, not a training run. The quality comparison comes from the paper.
T5 in AI, robotics and aerospace
- Unified text services: one T5-style model with prefixes can summarise incident reports, extract part numbers and classify faults.
- Instruction-tuned variants (Flan-T5) handle new tasks from instructions, useful for internal tools with little labelled data.
- Efficiency: encoder–decoders process inputs once and generate short outputs, suiting extraction and summarisation workloads.
References
- C. Raffel et al. (2020). “Exploring the limits of transfer learning with a unified text-to-text Transformer.” JMLR 21.
- H. W. Chung et al. (2022). “Scaling instruction-finetuned language models.” arXiv:2210.11416.
- L. Xue et al. (2021). “mT5: a massively multilingual pre-trained text-to-text Transformer.” Proc. NAACL.