Sub topic 6
Choosing a Pretrained Model: BART, T5, MarianMT and mBART
Assume you know basic Python, Transformer terminology, and how tokenisation and model generation work. This chapter focuses on choosing a pretrained encoder-decoder model rather than implementing the architecture.
Start with the task and languages
A pretrained model is a model that has already learned from a large text collection. You adapt its existing knowledge to your task instead of training all parameters from the beginning.
The task usually narrows the choice:
- BART is a strong general-purpose choice for English summarisation and related text-generation tasks.
- T5 treats every task as text-to-text. An instruction-like prefix, such as
summarize:ortranslate English to German:, tells the model what to do. - MarianMT provides many separate models for specific translation language pairs, such as English to German.
- mBART is designed for multilingual generation. It is useful when one model must handle several languages or when the source and target languages vary.
| Model family | Typical strength | Language coverage | Main selection concern |
|---|---|---|---|
| BART | English summarisation and generation | Mostly English checkpoints | Less convenient for multilingual workflows |
| T5 | Multiple text-to-text tasks | Depends on checkpoint | Task prefix and checkpoint choice matter |
| MarianMT | Dedicated translation pairs | Many pair-specific checkpoints | Usually one model per language direction |
| mBART | Multilingual translation and generation | Many languages in one checkpoint | Language codes and larger resource needs |
Model names describe families, not guaranteed capabilities. Always read the checkpoint card. It identifies the training languages, intended task, maximum input length, license, and expected input format.
BART: a practical summarisation choice
BART combines a bidirectional encoder with an autoregressive decoder. Its pretraining corrupts text and trains the model to reconstruct the original. This teaches the decoder to generate fluent text while using the encoder's representation of the complete input.
A checkpoint such as facebook/bart-large-cnn was fine-tuned on news summarisation data. It is therefore a sensible first choice for English news-style summaries. It may perform poorly on legal, medical, or highly technical documents if those domains differ greatly from its training data.
from transformers import pipeline
summarizer = pipeline(
"summarization",
model="facebook/bart-large-cnn",
)
article = (
"The city council approved a plan to add 40 kilometres of bike lanes "
"over three years. Supporters said the plan would reduce traffic."
)
result = summarizer(
article,
max_new_tokens=40,
min_new_tokens=10,
do_sample=False,
)
print(result[0]["summary_text"])T5: one interface for many tasks
T5 frames every problem as text-to-text. The input and output are both strings, even when the underlying task is classification, translation, or summarisation.
For example:
summarize: The council approved a new plan...translate English to German: The house is wonderful.
This design makes T5 flexible and easy to fine-tune. However, the prefix is part of the model's expected input format. Omitting it, changing its wording, or selecting a checkpoint trained for another task can reduce quality.
The original t5-small checkpoint is useful for experiments because it is relatively light. Larger checkpoints usually produce better results but require more memory and compute.
MarianMT: choose a language pair directly
MarianMT checkpoints are commonly specialised for one translation direction. For example, Helsinki-NLP/opus-mt-en-de translates English into German. This specialisation simplifies inference because no language instruction or target-language code is usually required.
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
name = "Helsinki-NLP/opus-mt-en-de"
tokenizer = AutoTokenizer.from_pretrained(name)
model = AutoModelForSeq2SeqLM.from_pretrained(name)
batch = tokenizer(
["Where is the nearest train station?"],
return_tensors="pt",
)
outputs = model.generate(**batch, num_beams=4, max_new_tokens=40)
print(tokenizer.batch_decode(outputs, skip_special_tokens=True))Use MarianMT when the language pair is known and supported. A pair-specific model can outperform a multilingual model on that pair, while requiring a separate checkpoint for each direction.
mBART: multilingual generation
mBART uses one multilingual model for many languages. It is a better fit when an application translates several languages or processes multilingual documents. Its flexibility adds configuration work. Depending on the checkpoint, you may need to set the source language and the forced beginning-of-sentence token for the target language.
A selection procedure
- Identify the task: summarisation, translation, or another text-to-text problem.
- List the source and target languages.
- Prefer a task-specific checkpoint when one exists.
- Check the domain and expected input length.
- Compare quality, memory use, speed, and license on representative examples.
- Inspect outputs for factual errors before putting the model into production.
Exercise
You need English-to-German translation for a high-volume service and do not need other language pairs. Which family should you test first, and what should you verify?
Show answer
Test a pair-specific MarianMT checkpoint first. Verify that it supports English-to-German translation, fits the input length, has an acceptable license, and produces accurate results on your own examples.