Skip to content

Fix BLEU brevity penalty with multiple references (use the closest reference length) - #892

Open
0xrichardlu wants to merge 2 commits into
huggingface:mainfrom
0xrichardlu:fix/bleu-closest-ref-length
Open

0xrichardlu wants to merge 2 commits into
huggingface:mainfrom
0xrichardlu:fix/bleu-closest-ref-length

Conversation

@0xrichardlu

Copy link
Copy Markdown

Fixes #891.

What does this PR do?

compute_bleu from tensorflow/nmt takes the effective reference length to be the shortest reference. BLEU as defined in Papineni et al. (2002), multi-bleu.perl, sacrebleu and NLTK all use the reference whose length is closest to the prediction, with ties going to the shorter one. tensorflow/nmt is archived, so this PR keeps compute_bleu for the n-gram precisions. In Bleu._compute it then recomputes the reference length, length ratio, brevity penalty and score with the closest-length rule. Single-reference results are unchanged.

The README examples documented the old behaviour. A prediction identical to one of its references showed length_ratio: 1.1666666666666667 and reference_length: 6; these are now 1.0 and 7. I also added a docstring example with references of different lengths as a regression test, since tests/test_metric_common.py doctests every metric.

Testing

  • sacrebleu comparison: 500 random corpora with 1–3 references per prediction, compared against sacrebleu.corpus_bleu(..., smooth_method="none", tokenize="13a").
    • BLEU and reference_length now match sacrebleu in 500/500.
    • Before the change, reference_length differed in 253/500 and the score differed whenever the brevity penalty was active.
  • Regression doctest: pytest tests/test_metric_common.py -k "bleu and not bleurt and not google" passes. With the old computation and the new docstring example, the test fails.
  • README examples: I re-ran all four and they print the updated outputs.
  • Lint: black --line-length 119, isort and flake8 pass on metrics/bleu/bleu.py.

compute_bleu from tensorflow/nmt uses the shortest reference as the effective reference length. BLEU (Papineni et al., 2002), multi-bleu.perl, sacrebleu and NLTK use the reference closest in length to the prediction. Recompute the reference length, ratio, brevity penalty and score with the closest-length rule and update the README examples that showed the old behaviour.

@atishay-kasliwal atishay-kasliwal left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compared BLEU with sacrebleu.corpus_bleu (13a tokenizer) on 6 cases with several references of different lengths. Here the score, brevity penalty and reference length match sacrebleu in all 6. Main picked the wrong reference length in two (3 instead of 10, and 3 instead of 6), which gave 100 instead of 65.144 in the first.

@0xrichardlu

Copy link
Copy Markdown
Author

Thanks for cross-checking against sacrebleu!

@kiroqian

kiroqian commented Dec 1, 2026

Copy link
Copy Markdown

The closest-reference calculation matches SacreBLEU on a real multi-reference corpus. I also found a case that still fails before the new calculation runs on 57cfa412:

metric.compute(
    predictions=["a valid four word answer"],
    references=[["a valid four word answer", ""]],
)

The HF metric raises ZeroDivisionError. SacreBLEU with smooth_method="none", tokenize="13a" gives 100: the exact, nonempty reference is the closest one. The imported TensorFlow compute_bleu still computes a ratio using the shortest reference before returning its n-gram precisions, so adding a blank alternate for every segment short-circuits this PR's correction.

For a corpus check I used the 1,997 aligned German WMT19 reference translations, with ar as the candidate and arp, wmtp as references. This tests metric behavior on real text, not translation-system quality. Both the PR and SacreBLEU give 25.382102 (0–100), effective reference length 49,703. Adding an empty third reference to each segment makes the PR raise; SacreBLEU returns 25.391446, length 49,685. An empty reference can legitimately be closest for a very short hypothesis, so simply discarding every blank before calculating the length would also change the result.

Computing the clipped n-gram precisions independently of the old brevity-penalty division would allow this case to reach the new closest-length rule. The one-sentence example would make a compact regression test.

@0xrichardlu

0xrichardlu commented Dec 1, 2026 •

Copy link
Copy Markdown
Author

Thanks @kiroqian, that was a real gap. The TF compute_bleu divides by the shortest reference length before it returns, so an empty alternate reference crashed it before the closest-length rule could run. In eeab356, the metric computes the clipped n-gram precisions itself (same counting as the TF script) and no longer calls compute_bleu. Your example is now a doctest and returns bleu 1.0 with reference_length 5, matching SacreBLEU.

Checks:

  • The precisions match the TF compute_bleu exactly on 2,000 random corpora (max_order 1/2/4, with and without smoothing).
  • BLEU and reference length match sacrebleu.corpus_bleu(smooth_method="none", tokenize="13a") in 400/400 random multi-reference cases, including empty alternate references.
  • tests/test_metric_common.py::LocalMetricTest::test_load_metric_bleu passes.

@kiroqian

kiroqian commented Dec 2, 2026

Copy link
Copy Markdown

I reran the 1,997-segment WMT19 case against eeab356. With the two original references, HF and SacreBLEU still agree at 25.382102 (reference length 49,703). With an empty alternate added to each segment they now also agree at 25.391446 (49,685). The single-sentence example returns 1.0. This resolves the empty-reference case I reported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bleu: brevity penalty uses the shortest reference instead of the closest one when there are multiple references

3 participants