Sub topic 7
Measuring transfer: XNLI, XTREME and per-language reporting
This chapter assumes you can fine-tune a Hugging Face classifier with Trainer, and that you know accuracy, F1 and the idea of a held-out test set. You have already seen zero-shot transfer in practice. Here the question is how to measure it honestly. A single headline number hides most of what matters, so the goal is to report results that show where transfer works and where it fails.
Why transfer needs its own evaluation
A monolingual model is evaluated on one test set. A multilingual model makes a claim about many languages at once, so one test set cannot support it. Three problems appear.
- Averages hide gaps. A model with 80% on English and 60% on Swahili can still average above 70% if most languages are well supported.
- Test sets are not comparable by default. If every language has different questions, a score difference may reflect question difficulty and not model quality.
- The setting changes the meaning of a score. Zero-shot, translate-train and few-shot results answer different questions and must be labeled.
Cross-lingual benchmarks address the second problem by making the test content parallel. They address the third by defining the training setting. You address the first problem yourself, by reporting per language.
XNLI: a parallel test set for one task
XNLI is the standard small benchmark for cross-lingual transfer. The task is natural language inference. Given a premise and a hypothesis, the model predicts one of three labels: entailment, neutral or contradiction.
The key design choice is that the dev and test sets (2,490 and 5,010 examples) were professionally translated from English into 14 other languages, giving 15 languages in total. Every language therefore has the same examples with the same gold labels. A difference between two languages comes from the model's handling of the language, not from different data.
The standard training setting is:
- Fine-tune on the English MultiNLI training data (about 393k pairs).
- Evaluate on the test set of each language without any further training.
This is zero-shot cross-lingual transfer. The facebook/xnli dataset also provides machine-translated training sets for each language, which are used for the translate-train setting covered in a later chapter.
A caveat about translated tests
Translated test sets are convenient, but they are not natural text. Translation tends to keep English sentence structure and word choice ("translationese"). A model can score well on translated text partly because it resembles English. Scores on translated benchmarks may therefore be higher than scores on text that native speakers wrote. Benchmarks that collect data natively, such as XQuAD's cousin TyDi QA, exist for this reason.
Running the per-language evaluation
The fine-tuning script from the previous chapter already evaluates on three languages. The part that matters for reporting is turning the results into a table with a gap against English. The sketch below assumes you have a dictionary of accuracies from trainer.evaluate. The values are placeholders so the code runs on its own. Replace them with your own results.
import pandas as pd
# Replace these placeholder values with your own evaluation results.
scores = {"en": 0.82, "de": 0.74, "sw": 0.65}
df = pd.DataFrame(
{"language": list(scores), "accuracy": list(scores.values())}
).set_index("language")
df["gap_to_en"] = df["accuracy"] - df.loc["en", "accuracy"]
df["relative_drop_pct"] = -100 * df["gap_to_en"] / df.loc["en", "accuracy"]
print(df.round(3).to_string())
print("macro average:", round(df["accuracy"].mean(), 3))
print("worst language:", df["accuracy"].idxmin(), round(df["accuracy"].min(), 3))The output has three kinds of information: the score per language, the gap to the source language, and two summaries (mean and worst case). The gap to the source language is often called the transfer gap. It is the most direct measure of how much is lost by not having labeled data in the target language.
XTREME: many tasks, many languages
XNLI covers one task. A model can transfer well on classification and badly on structured prediction. XTREME (Cross-lingual TRansfer Evaluation of Multilingual Encoders) combines nine tasks across 40 languages in four families:
| Task family | Tasks | What it tests |
|---|---|---|
| Sentence classification | XNLI, PAWS-X | Meaning at sentence level |
| Structured prediction | UD part-of-speech tagging, WikiANN named entity recognition | Token-level labels and syntax |
| Question answering | XQuAD, MLQA, TyDi QA-GoldP | Span extraction in context |
| Sentence retrieval | BUCC, Tatoeba | Finding translation pairs |
XTREME defines a zero-shot protocol. Models are fine-tuned on English training data (the retrieval tasks need no fine-tuning) and tested on all other languages. A follow-up, XTREME-R, revised the task set and added a public leaderboard.
The tasks differ in how hard transfer is. In the original XTREME results, sentence retrieval and classification transferred relatively well, while structured prediction and question answering showed larger gaps for distant languages. This pattern is why a benchmark with one task type can mislead you about overall quality.
Metrics differ per task
You cannot compare raw numbers across tasks without knowing the metric.
| Task | Metric | Note |
|---|---|---|
| XNLI, PAWS-X | Accuracy | Chance is 33% for XNLI and 50% for PAWS-X |
| POS tagging | Accuracy | Per token |
| NER | F1 | Span-level, entity exact match |
| XQuAD, MLQA, TyDi QA | F1 and exact match | Token overlap versus identical span |
| BUCC, Tatoeba | F1 or accuracy | Retrieval of the correct translation |
Always state the chance level when you report a score. An accuracy of 55% on XNLI is far above chance, while 55% on PAWS-X is close to it.
Per-language reporting
The XTREME headline is a mean over tasks, and each task score is itself a mean over languages. This is useful for a leaderboard, but it fails as a diagnostic. A good report includes the following.
- Every language, not only the average. Put the table in the report or an appendix.
- The transfer gap against the source language. It shows the cost of zero-shot transfer directly.
- The worst-case language. For a product that must serve all its languages, the minimum matters more than the mean.
- Grouping by property. Group languages by resource level, script or family. Then you can see whether the gap tracks a cause such as pretraining data size (covered in the previous chapter on transfer factors).
- Uncertainty. Fine-tuning runs vary with the random seed. Run several seeds and report the mean and spread. On a 5,010-example test set, a 1-point difference between two models can be noise.
Mean versus macro versus weighted
A plain mean over languages treats each language equally, which is called a macro average. If a benchmark includes many closely related European languages, they dominate the mean. A model that improves only on those languages will gain on the average while a low-resource language gets no benefit. When you compare two models, show both the macro average and the worst-group average, for example the mean over low-resource languages only.
import numpy as np
# Placeholder results: accuracy per language for two models.
model_a = {"en": 0.84, "de": 0.76, "fr": 0.77, "es": 0.77, "sw": 0.63, "ur": 0.64}
model_b = {"en": 0.84, "de": 0.78, "fr": 0.79, "es": 0.79, "sw": 0.60, "ur": 0.62}
low_resource = ["sw", "ur"]
def summarize(name, scores):
macro = np.mean(list(scores.values()))
low = np.mean([scores[lang] for lang in low_resource])
print(f"{name}: macro={macro:.3f} low-resource={low:.3f}")
summarize("A", model_a)
summarize("B", model_b)Here model B has the higher macro average, but model A is better on the two low-resource languages. A single number would pick B. The grouped numbers show the trade-off.
Common errors in cross-lingual evaluation
- Selecting checkpoints on target-language data. Strict zero-shot means you use no target-language labels. If you pick the best epoch by target dev accuracy, you have used target labels. Select the checkpoint on English dev data, or state clearly that you did otherwise.
- Comparing different settings. A zero-shot score and a translate-train score are not comparable as "the same model." Name the setting in every table.
- Reporting only translated test sets. Translationese can inflate scores. Add at least one natively written test set when you can.
- Ignoring tokenization effects. Truncating at 128 tokens cuts more text from languages that split into more pieces. The first code example in this tutorial showed that the same sentence takes different token counts. A low score may partly reflect truncation. Check the fraction of truncated examples per language.
- Treating a gap as a pure language effect. Gaps combine pretraining data, script, vocabulary coverage and translation quality of the test set. Do not attribute them to one cause without a controlled comparison.
- Data leakage. Check that the test sets are not part of the pretraining or fine-tuning corpus, especially for web-scale models.
A reporting checklist
Before you publish or share a cross-lingual result, confirm each item.
- The transfer setting is named (zero-shot, translate-train, translate-test or few-shot).
- The source language and the model-selection data are stated.
- All languages are shown in a table, with the metric and the chance level.
- The gap to the source language is computed.
- Group averages (by resource level or script) accompany the overall mean.
- Results come from several seeds, with the spread shown.
- The share of truncated inputs is checked per language.
Exercise
Two multilingual classifiers are evaluated zero-shot on XNLI. Model A has a macro average of 71.0 and an accuracy of 62.0 on Swahili and Urdu. Model B has a macro average of 72.0 and an accuracy of 58.0 on Swahili and Urdu. Your product mainly serves low-resource languages. Which model do you choose, and which extra checks do you run before deciding?
Show answer
Choose model A as the starting candidate, because the languages that matter for the product score 4 points higher, even though its macro average is 1 point lower. The macro average is dominated by languages where both models do well. Before deciding, check the seed variance (a 1-point macro difference may be noise, though a 4-point low-resource difference is less likely to be), confirm that checkpoints were selected on English dev data and not on target data, check per-language truncation rates, and test on a natively written dataset for the target languages if one exists.
Exercise
Why is XNLI a better basis for comparing languages than evaluating on separately collected natural-language-inference datasets in each language?
Show answer
XNLI's dev and test sets are translations of the same English examples, with the same gold labels. Every language is tested on the same content, so score differences reflect how well the model handles each language and not differences in question difficulty or label distribution. Separately collected datasets would differ in content, difficulty and annotation, so a gap between two languages could not be attributed to the model. The cost is that translated text may be easier than natural text because it preserves English structure.