Fix BERTScore OverflowError when the tokenizer has no model_max_length - #1
Closed
tonycoder-hub wants to merge 1 commit into
Closed
Fix BERTScore OverflowError when the tokenizer has no model_max_length#1tonycoder-hub wants to merge 1 commit into
tonycoder-hub wants to merge 1 commit into
Conversation
Tokenizers whose config omits `model_max_length` report transformers' VERY_LARGE_INTEGER sentinel (~1e30) instead. bert_score forwards that value to `tokenizer.encode(max_length=...)`, which overflows the Rust tokenizers backend used by transformers>=5, e.g. for microsoft/deberta-xlarge-mnli. Cap the sentinel to 512 before any tokenization happens, and add a `max_length` argument so a different truncation length can be requested. Fixes huggingface#739 Co-authored-by: Tony Coder <407243179@qq.com>
Owner
Author
|
Closing as stale — opened on or before 2026-08-17 and still unmerged. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes huggingface#739
Problem
bertscore.compute(..., model_type="microsoft/deberta-xlarge-mnli")crashes withOverflowError: int too big to convertundertransformers>=5.Tokenizers whose config omits
model_max_length(DeBERTa being the example from the issue) report transformers'VERY_LARGE_INTEGERsentinel instead, andbert_score.utils.sent_encodeforwardstokenizer.model_max_lengthstraight intotokenizer.encode(max_length=...). Intransformers>=5that reaches the Rust tokenizers backend, which cannot represent the value.Verified against the current
main(a7dd338) withtransformers==5.15.0/tokenizers==0.22.2/bert-score==0.3.13:Fix
BERTScore._compute()now replaces the sentinel with 512 — the sequence length of the BERT-family modelsbert_scorerecommends, and the actualmax_position_embeddingsofmicrosoft/deberta-xlarge-mnli— and logs a warning when it does so. Amax_lengthargument is also exposed so a different truncation length can be requested, which is useful because the fallback would otherwise be silent.The cap has to be applied before any tokenization happens.
BERTScorer.__init__tokenizesidf_sentsviacompute_idf(), soidf=Truewould still overflow if the tokenizer were only fixed up after construction.idf_sentsare therefore passed tocompute_idf()after the tokenizer has been adjusted rather than through the constructor;_idf_dictisNoneat that point, so this is equivalent to what the constructor did. The cap is re-applied on cached scorers so thatmax_lengthalso takes effect on subsequentcompute()calls.Models that declare a real
model_max_lengthare untouched.Tests
Two tests in
tests/test_metric_common.py, run withidf=Trueso that real tokenization happens rather than being mocked away:test_bertscore_caps_undefined_model_max_length— revertingbertscore.pymakes it fail with the exact error from the issue (OverflowError: int too big to convertattransformers/tokenization_utils_tokenizers.py).test_bertscore_max_length_overrides_model_max_length— covers the new argument.make quality(black,isort,flake8) is clean on the changed files. The remaining failures intests/test_metric_common.pyin my environment are identical before and after this change (21 in both cases) and come from running on Python 3.12 withnumpy>=2and without the optional dependencies fromadditional-tests-requirements.txt; CI pinsnumpy<2.0.0and Python 3.9.I also confirmed the fix against the real
microsoft/deberta-xlarge-mnlitokenizer with the model forward mocked out:Note
#756 previously proposed a fix for this issue but was closed without merging. That version applied the cap only after
BERTScorerconstruction, so theidf=Truepath still overflowed.