Fix ineffective is_base_mistral CI patch for HF API rate limiting (#21729)

This commit is contained in:
Liangsheng Yin
2026-03-31 12:54:34 -07:00
committed by GitHub
parent b91f78d255
commit f4505e2ee3
@@ -714,16 +714,19 @@ class TokenizerWarningsFilter(logging.Filter):
_is_base_mistral_patched = False _is_base_mistral_patched = False
# transformers version where is_base_mistral calls model_info() on every tokenizer load # transformers version where _patch_mistral_regex calls model_info() on every tokenizer load
_TRANSFORMERS_PATCHED_VERSION = "5.3.0" _TRANSFORMERS_PATCHED_VERSION = "5.3.0"
def _patch_is_base_mistral_in_ci(): def _patch_is_base_mistral_in_ci():
"""Patch transformers' is_base_mistral to avoid HF API calls in CI. """Patch transformers' _patch_mistral_regex to avoid HF API calls in CI.
transformers calls model_info() inside _patch_mistral_regex -> is_base_mistral transformers defines is_base_mistral as a local function inside
for every tokenizer load, which hits HF API even with HF_HUB_OFFLINE=1. _patch_mistral_regex, so it cannot be patched via module attribute.
In CI this exhausts the 3000 req/5min rate limit and causes 429 errors. Instead we replace the entire _patch_mistral_regex classmethod with a
version that simply returns the tokenizer unchanged.
In CI this prevents exhausting the 3000 req/5min HF API rate limit.
""" """
global _is_base_mistral_patched global _is_base_mistral_patched
if _is_base_mistral_patched: if _is_base_mistral_patched:
@@ -739,18 +742,23 @@ def _patch_is_base_mistral_in_ci():
if transformers.__version__ != _TRANSFORMERS_PATCHED_VERSION: if transformers.__version__ != _TRANSFORMERS_PATCHED_VERSION:
logger.warning( logger.warning(
"transformers version changed to %s (expected %s), " "transformers version changed to %s (expected %s), "
"is_base_mistral patch skipped — may need update if 429 errors recur", "_patch_mistral_regex patch skipped — may need update if 429 errors recur",
transformers.__version__, transformers.__version__,
_TRANSFORMERS_PATCHED_VERSION, _TRANSFORMERS_PATCHED_VERSION,
) )
_is_base_mistral_patched = True # don't warn repeatedly _is_base_mistral_patched = True # don't warn repeatedly
return return
import transformers.tokenization_utils_tokenizers as tut from transformers import PreTrainedTokenizerFast
if hasattr(tut, "is_base_mistral"): if hasattr(PreTrainedTokenizerFast, "_patch_mistral_regex"):
tut.is_base_mistral = lambda *a, **kw: False
logger.info("CI: patched is_base_mistral to skip HF API calls") @classmethod
def _noop_patch_mistral_regex(cls, tokenizer, *args, **kwargs):
return tokenizer
PreTrainedTokenizerFast._patch_mistral_regex = _noop_patch_mistral_regex
logger.info("CI: patched _patch_mistral_regex to skip HF API calls")
_is_base_mistral_patched = True _is_base_mistral_patched = True