TAI BUI
Back to curriculum
BuildPython

Machine Translation

Translation funded NLP research for thirty years. It's still cutting the checks.

TIME: ~75 minutesPREREQUISITES: Phase 5 · 10 (Attention Mechanism), Phase 5 · 04 (GloVe, FastText, Subword)

Learning Objectives

  • Run a pretrained multilingual MT model end to end (NLLB-200): tokenize with the correct source/target language codes, decode with beam search, and detokenize the output.
  • Score MT output with BLEU and chrF via sacrebleu, and evaluate it properly using the 2026 three-tier stack: heuristic, learned (COMET), and LLM-as-judge metrics.
  • Recognize the five MT failure modes that ship silently to production: hallucination, off-target generation, terminology drift, formality mismatch, and length explosion, and apply the correct mitigation for each.
  • Pick the right MT model for a use case from the 2026 landscape, and fine-tune a pretrained model on a small domain-specific parallel corpus.

The Problem

A machine translation model reads a sentence in one language and produces a sentence in another. That's the whole job description, and it's harder than it sounds: length varies, word order varies, and source words rarely map one-to-one onto target words. Idioms refuse the mapping outright. "I miss you" in French is "tu me manques," literally "you are lacking to me." No word-level alignment survives that sentence.

Machine translation is the task that forced NLP to invent encoder-decoders, attention, transformers, and eventually the whole LLM paradigm. Every step forward arrived because translation quality was measurable, and the gap between human and machine was stubborn.

This lesson skips the history and goes straight to the working 2026 pipeline: a pretrained multilingual encoder-decoder (NLLB-200 or mBART), subword tokenization, beam search, BLEU and chrF evaluation, and the handful of failure modes that still ship to production uncaught.

Warm-up0/3
1. In the encoder-decoder architecture from the Attention Mechanism lesson, what does the decoder consult at each step besides its own previous output?Warm-up
2. Two human translators independently translate the same paragraph and choose different words, but both versions mean the same thing. Common sense says:Warm-up
3. A language model must handle words it never saw during training. Why do modern models split text into subword pieces instead of keeping one vocabulary entry per whole word?Warm-up

The Concept

MT pipeline: tokenize → encode → decode with attention → detokenize

Modern MT is a transformer encoder-decoder trained on parallel text. The encoder reads the source in its own tokenization; the decoder generates the target one subword at a time, using the encoder's output via cross-attention (lesson 10). Decoding runs beam search to avoid the greedy-decoding trap: picking the single most-likely next word at every step, with no way to recover once an early pick turns out wrong. The output gets detokenized, detruecased, and scored against a reference.

Three operational choices decide whether that pipeline ships good translations in practice.

  • Tokenizer. SentencePiece BPE trained on a mixed-language corpus. Shared vocabulary across languages is what enables zero-shot pairs in NLLB.
  • Model size. NLLB-200 distilled 600M fits on a laptop. NLLB-200 3.3B is the published production default. 54.5B is the research ceiling.
  • Decoding. Beam width 4-5 for general content. Length penalty to avoid too-short output. Constrained decoding when you need terminology consistency.
Check: The Concept0/7
1. What is NMT, as the lesson uses the term?Define
2. What is NLLB?Define
3. What is constrained decoding?Define
4. What problem does beam search address that greedy decoding has?Explain
5. Why does the lesson stress a SentencePiece BPE vocabulary shared across languages in NLLB?Explain
6. How do the three named NLLB-200 model sizes differ in intended use?Explain
7. Which decoding configuration does the lesson recommend for general MT content?Explain

Build It

Step 1: a pretrained MT call

python
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
 
model_id = "facebook/nllb-200-distilled-600M"
tok = AutoTokenizer.from_pretrained(model_id, src_lang="eng_Latn")
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
 
src = "The cats are running."
inputs = tok(src, return_tensors="pt")
 
out = model.generate(
    **inputs,
    forced_bos_token_id=tok.convert_tokens_to_ids("fra_Latn"),
    num_beams=5,
    length_penalty=1.0,
    max_new_tokens=64,
)
print(tok.batch_decode(out, skip_special_tokens=True)[0])
text
Les chats courent.

Three things matter here. src_lang tells the tokenizer which script and segmentation to apply. forced_bos_token_id tells the decoder which language to generate. Both are NLLB-specific tricks. mBART and M2M-100 use their own conventions, and none of the three are interchangeable.

Step 2: BLEU and chrF

BLEU measures n-gram overlap between output and reference. Four reference n-gram sizes (1-4), geometric mean of precisions, brevity penalty for too-short output. The score is in [0, 100]. Commonly used. Frustrating to interpret: 30 BLEU is "usable"; 40 is "good"; 50 is "exceptional"; differences under 1 BLEU are noise.

chrF measures character-level F-score. More sensitive to morphologically rich languages where BLEU undercounts matches. Often reported alongside BLEU.

python
import sacrebleu
 
hypotheses = ["Les chats courent."]
references = [["Les chats courent."]]
 
bleu = sacrebleu.corpus_bleu(hypotheses, references)
chrf = sacrebleu.corpus_chrf(hypotheses, references)
print(f"BLEU: {bleu.score:.1f}  chrF: {chrf.score:.1f}")

Always use sacrebleu. It normalizes tokenization so scores are comparable across papers. Rolling your own BLEU computation is how misleading benchmarks happen.

The three-tier evaluation hierarchy (2026)

Modern MT evaluation uses three complementary metric families. Ship with at least two.

  • Heuristic (BLEU, chrF). Fast, reference-based, interpretable, insensitive to paraphrase. Use for legacy comparison and regression detection.
  • Learned (COMET, BLEURT, BERTScore). Neural models trained on human judgment; compare semantic similarity of translation to source and reference. COMET has the highest association with MT research since 2023 and is the 2026 production default where quality matters.
  • LLM-as-judge (reference-free). Prompt a large model to score translations on fluency, adequacy, tone, cultural appropriateness. GPT-4-as-judge matches human agreement ~80% of the time when the rubric is well designed. Use for open-ended content where no reference exists.

Practical 2026 stack: sacrebleu for BLEU and chrF, unbabel-comet for COMET, and a prompted LLM for the final human-facing signal. Calibrate every metric against 50-100 human-labeled examples before trusting it on production data.

Reference-free metrics (COMET-QE, BLEURT-QE, LLM-as-judge) let you evaluate a translation without a reference at all, which matters for long-tail language pairs that don't have one.

Step 3: what breaks in production

The working pipeline above will translate fluently 80% of the time and silently fail the remaining 20%. Named failure modes:

  • Hallucination. Model invents content that was not in the source. Common in unfamiliar domain vocabulary. Symptom: output is fluent but claims facts the source did not state. Mitigation: constrained decoding on domain terms, human review on regulated content, monitoring for output much longer than input.
  • Off-target generation. Model translates into the wrong language. NLLB is surprisingly prone to this on rare language pairs. Mitigation: verify forced_bos_token_id and always decode with a language-ID model check on output.
  • Terminology drift. "Sign up" becomes "s'inscrire" in doc 1 and "créer un compte" in doc 2. For UI text and user-facing strings, consistency matters more than raw quality. Mitigation: glossary-constrained decoding or post-edit dictionary.
  • Formality mismatch. French "tu" vs "vous", Japanese politeness levels. The model picks whichever form was more common in training. For customer-facing content this is usually wrong. Mitigation: prompt prefix with a formality token if the model supports it, or fine-tune a small model on formal-only corpora.
  • Length explosion on short input. Very short input sentences often produce overlong translations because the length penalty falls off a cliff below ~5 source tokens. Mitigation: hard max-length cap proportional to source length.

Step 4: fine-tuning for a domain

Pretrained models are generalists. Legal, medical, or game-dialog translation benefits measurably from fine-tuning on domain parallel data. The recipe is not exotic:

python
from transformers import Trainer, TrainingArguments
from datasets import Dataset
 
pairs = [
    {"src": "The defendant pleaded guilty.", "tgt": "L'accusé a plaidé coupable."},
]
 
ds = Dataset.from_list(pairs)
 
 
def preprocess(ex):
    return tok(
        ex["src"],
        text_target=ex["tgt"],
        truncation=True,
        max_length=128,
        padding="max_length",
    )
 
 
ds = ds.map(preprocess, remove_columns=["src", "tgt"])
 
args = TrainingArguments(output_dir="out", per_device_train_batch_size=4, num_train_epochs=3, learning_rate=3e-5)
Trainer(model=model, args=args, train_dataset=ds).train()

A few thousand high-quality parallel examples beats a few hundred thousand noisy web-scraped ones. Quality of training data is the single largest production lever.

Check: Build It0/23
1. What does BLEU measure?Define
2. What is chrF?Define
3. What is 'hallucination' in production MT, and what mitigates it?Define
4. What is the LLM-as-judge approach used for, and how well does it match human agreement?Define
5. Which metric family is the 2026 production default for MT quality when you need it to track human judgment?Explain
6. When does chrF tend to be more informative than BLEU?Explain
7. How should BLEU scores be read in practice?Explain
8. Why does the lesson say to always compute BLEU with sacrebleu rather than your own code?Explain
9. Why is a heuristic metric like BLEU a poor sole judge of a translation that is a valid paraphrase of the reference?Explain
10. What do reference-free metrics like COMET-QE and BLEURT-QE enable that BLEU cannot?Explain
11. What is 'terminology drift' in production MT, and what mitigates it?Explain
12. What is the 'formality mismatch' failure mode, and what mitigates it?Explain
13. In the NLLB call from Step 1, which argument makes the decoder generate French?Apply
14. In the fine-tuning preprocess function, what does passing text_target=ex["tgt"] to the tokenizer do?Apply
15. The fine-tuning code uses per_device_train_batch_size=4 and num_train_epochs=3. On a single device with 5,000 pairs, how many training steps run?Apply
16. Two versions of a translation model score 31.2 and 31.8 BLEU on the same test set. What is the safest conclusion?Apply
17. Your pipeline translates Vietnamese into French with NLLB, and QA flags outputs that occasionally come back in Spanish. What is happening, and what would have caught it before shipping?Analyze
18. A user submits a two-word input to your MT service and gets back three full sentences. What is the most likely cause, and how do you prevent it?Analyze
19. A French customer-support bot scores well on BLEU, yet customers complain it addresses them with informal 'tu'. What explains the gap, and what fits?Analyze
20. Your own BLEU script reports 35 on a test set, while a paper reports 33 for the same system and data using sacrebleu. What is the likeliest cause?Analyze
21. An NLLB pipeline returns text in the wrong language on a rare pair, though src_lang is correct. What should you inspect first?Analyze
22. You have 5,000 clean, hand-checked legal-domain sentence pairs and 500,000 noisy web-scraped pairs from the same domain, but can fine-tune on only one set. Which should you pick, and why?Design
23. You must translate regulated medical text for a rare language pair that has no reference translations. Which plan fits best?Design

Use It

The 2026 production stack for MT:

Use caseRecommended starting point
Any-to-any, 200 languagesfacebook/nllb-200-distilled-600M (laptop) or nllb-200-3.3B (production)
English-centric, high quality, 50 languagesfacebook/mbart-large-50-many-to-many-mmt
Short runs, cheap inference, English-French/German/SpanishHelsinki-NLP / Marian models
Latency-critical browser-sideONNX-quantized Marian (~50 MB)
Maximum quality, willing to payGPT-4 / Claude / Gemini with translation prompts

LLMs now outperform specialized MT models on several language pairs as of 2026, particularly on idiomatic content and long context. The tradeoff is per-token cost and latency. Pick an LLM when context length, stylistic consistency, or domain adaptation via prompting matters more than throughput.

Check: Use It0/2
1. When is an LLM (e.g. GPT-4) likely to outperform a specialized MT model in 2026?Explain
2. You must translate English UI strings into French, German and Spanish inside the user's browser with minimal latency. Which starting point fits best?Design

Ship It

Save as outputs/skill-mt-evaluator.md:

markdown
---
name: mt-evaluator
description: Evaluate a machine translation output for shipping.
version: 1.0.0
phase: 5
lesson: 11
tags: [nlp, translation, evaluation]
---
 
Given a source text and a candidate translation, output:
 
1. Automatic score estimate. BLEU and chrF ranges you would expect. State whether a reference is available.
2. Five-point human-verifiable check list: (a) content preservation (no hallucinations), (b) correct language, (c) register / formality match, (d) terminology consistency with glossary if provided, (e) no truncation or length explosion.
3. One domain-specific issue to probe. E.g., for legal: named entities and statute citations. For medical: drug names and dosages. For UI: placeholder variables `{name}`.
4. Confidence flag. "Ship" / "Ship with review" / "Do not ship". Tie to the severity of issues found in step 2.
 
Refuse to ship a translation without a language-ID check on output. Refuse to evaluate without a reference unless the user explicitly opts in to reference-free scoring (COMET-QE, BLEURT-QE). Flag any content over 1000 tokens as likely needing chunked translation.
Check: Ship It0/1
1. A user asks the mt-evaluator skill to score a translation but gives no reference and never mentions reference-free scoring. What does the skill do?Apply
Author's Note

The gap between a good BLEU score and a translation nobody complains about is bigger than the number suggests. I've watched a team hit 42 BLEU on held-out data, ship, and then spend the next month fielding tickets about a chatbot that calls the same customer "tu" in one reply and "vous" in the next. BLEU never caught that, because BLEU doesn't know what formality is. Neither does chrF. The three-tier hierarchy above exists because no single tier catches everything that gets escalated to you at 2am: terminology drift, off-target generation, a hallucinated clause that reads perfectly and states something the source never said. Run all three metric families before you trust a model on a language pair nobody on your team can actually read.

Exercises

  1. Easy. Translate a 5-sentence English paragraph to French and back to English using nllb-200-distilled-600M. Measure how close the round-trip is to the original. You should see semantic preservation with word-choice drift.
  2. Medium. Implement a language-ID check on translation outputs using fasttext lid.176 or langdetect. Integrate into the MT call so off-target generations are caught before returning.
  3. Hard. Fine-tune nllb-200-distilled-600M on a 5,000-pair domain corpus of your choice. Measure BLEU on a held-out set before and after fine-tuning. Report which kinds of sentences improved and which regressed.

Key Terms

TermWhat people sayWhat it actually means
BLEUTranslation scoreN-gram precision with brevity penalty. [0, 100].
chrFCharacter F-scoreCharacter-level F-score. More sensitive for morphologically rich languages.
NMTNeural MTTransformer encoder-decoder trained on parallel text. The 2017+ default.
NLLBNo Language Left BehindMeta's 200-language MT model family.
Constrained decodingControlled outputForce specific tokens or n-grams to appear / not appear in the output.
HallucinationInvented contentModel output that is not supported by the source.

Further Reading

Code

Ships With