Aluode/PerceptionLabPortable
0
1# Copyright 2020 The HuggingFace Team. All rights reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14 15import ast16import collections17import contextlib18import copy19import doctest20import functools21import gc22import importlib23import inspect24import logging25import multiprocessing26import os27import re28import shlex29import shutil30import subprocess31import sys32import tempfile33import threading34import time35import traceback36import types37import unittest38from collections import UserDict, defaultdict39from collections.abc import Generator, Iterable, Iterator, Mapping40from dataclasses import MISSING, fields41from functools import cache, wraps42from io import StringIO43from pathlib import Path44from typing import Any, Callable, Optional, Union45from unittest import mock46from unittest.mock import patch47 48import huggingface_hub.utils49import requests50import urllib351from huggingface_hub import delete_repo52from packaging import version53 54from transformers import Trainer55from transformers import logging as transformers_logging56 57from .integrations import (58 is_clearml_available,59 is_optuna_available,60 is_ray_available,61 is_sigopt_available,62 is_swanlab_available,63 is_tensorboard_available,64 is_trackio_available,65 is_wandb_available,66)67from .integrations.deepspeed import is_deepspeed_available68from .utils import (69 ACCELERATE_MIN_VERSION,70 GGUF_MIN_VERSION,71 TRITON_MIN_VERSION,72 is_accelerate_available,73 is_apex_available,74 is_apollo_torch_available,75 is_aqlm_available,76 is_auto_awq_available,77 is_auto_gptq_available,78 is_auto_round_available,79 is_av_available,80 is_bitsandbytes_available,81 is_bitsandbytes_multi_backend_available,82 is_bs4_available,83 is_compressed_tensors_available,84 is_cv2_available,85 is_cython_available,86 is_decord_available,87 is_detectron2_available,88 is_eetq_available,89 is_essentia_available,90 is_faiss_available,91 is_fbgemm_gpu_available,92 is_flash_attn_2_available,93 is_flash_attn_3_available,94 is_flax_available,95 is_flute_available,96 is_fp_quant_available,97 is_fsdp_available,98 is_ftfy_available,99 is_g2p_en_available,100 is_galore_torch_available,101 is_gguf_available,102 is_gptqmodel_available,103 is_grokadamw_available,104 is_hadamard_available,105 is_hqq_available,106 is_huggingface_hub_greater_or_equal,107 is_ipex_available,108 is_jinja_available,109 is_jumanpp_available,110 is_keras_nlp_available,111 is_kernels_available,112 is_levenshtein_available,113 is_librosa_available,114 is_liger_kernel_available,115 is_lomo_available,116 is_mistral_common_available,117 is_natten_available,118 is_nltk_available,119 is_onnx_available,120 is_openai_available,121 is_optimum_available,122 is_optimum_quanto_available,123 is_pandas_available,124 is_peft_available,125 is_phonemizer_available,126 is_pretty_midi_available,127 is_psutil_available,128 is_pyctcdecode_available,129 is_pytesseract_available,130 is_pytest_available,131 is_pytorch_quantization_available,132 is_quark_available,133 is_qutlass_available,134 is_rjieba_available,135 is_sacremoses_available,136 is_safetensors_available,137 is_schedulefree_available,138 is_scipy_available,139 is_sentencepiece_available,140 is_seqio_available,141 is_spacy_available,142 is_speech_available,143 is_spqr_available,144 is_sudachi_available,145 is_sudachi_projection_available,146 is_tf_available,147 is_tiktoken_available,148 is_timm_available,149 is_tokenizers_available,150 is_torch_available,151 is_torch_bf16_available_on_device,152 is_torch_bf16_gpu_available,153 is_torch_fp16_available_on_device,154 is_torch_greater_or_equal,155 is_torch_hpu_available,156 is_torch_mlu_available,157 is_torch_neuroncore_available,158 is_torch_npu_available,159 is_torch_optimi_available,160 is_torch_tensorrt_fx_available,161 is_torch_tf32_available,162 is_torch_xla_available,163 is_torch_xpu_available,164 is_torchao_available,165 is_torchaudio_available,166 is_torchcodec_available,167 is_torchdynamo_available,168 is_torchvision_available,169 is_triton_available,170 is_vision_available,171 is_vptq_available,172 strtobool,173)174 175 176if is_accelerate_available():177 from accelerate.state import AcceleratorState, PartialState178 from accelerate.utils.imports import is_fp8_available179 180 181if is_pytest_available():182 from _pytest.doctest import (183 Module,184 _get_checker,185 _get_continue_on_failure,186 _get_runner,187 _is_mocked,188 _patch_unwrap_mock_aware,189 get_optionflags,190 )191 from _pytest.outcomes import skip192 from _pytest.pathlib import import_path193 from pytest import DoctestItem194else:195 Module = object196 DoctestItem = object197 198 199SMALL_MODEL_IDENTIFIER = "julien-c/bert-xsmall-dummy"200DUMMY_UNKNOWN_IDENTIFIER = "julien-c/dummy-unknown"201DUMMY_DIFF_TOKENIZER_IDENTIFIER = "julien-c/dummy-diff-tokenizer"202# Used to test Auto{Config, Model, Tokenizer} model_type detection.203 204# Used to test the hub205USER = "__DUMMY_TRANSFORMERS_USER__"206ENDPOINT_STAGING = "https://hub-ci.huggingface.co"207 208# Not critical, only usable on the sandboxed CI instance.209TOKEN = "hf_94wBhPGp6KrrTH3KDchhKpRxZwd6dmHWLL"210 211 212# Used in CausalLMModelTester (and related classes/methods) to infer the common model classes from the base model class213_COMMON_MODEL_NAMES_MAP = {214 "config_class": "Config",215 "causal_lm_class": "ForCausalLM",216 "question_answering_class": "ForQuestionAnswering",217 "sequence_classification_class": "ForSequenceClassification",218 "token_classification_class": "ForTokenClassification",219}220 221 222if is_torch_available():223 import torch224 225 IS_ROCM_SYSTEM = torch.version.hip is not None226 IS_CUDA_SYSTEM = torch.version.cuda is not None227 IS_XPU_SYSTEM = getattr(torch.version, "xpu", None) is not None228else:229 IS_ROCM_SYSTEM = False230 IS_CUDA_SYSTEM = False231 IS_XPU_SYSTEM = False232 233logger = transformers_logging.get_logger(__name__)234 235 236def parse_flag_from_env(key, default=False):237 try:238 value = os.environ[key]239 except KeyError:240 # KEY isn't set, default to `default`.241 _value = default242 else:243 # KEY is set, convert it to True or False.244 try:245 _value = strtobool(value)246 except ValueError:247 # More values are supported, but let's keep the message simple.248 raise ValueError(f"If set, {key} must be yes or no.")249 return _value250 251 252def parse_int_from_env(key, default=None):253 try:254 value = os.environ[key]255 except KeyError:256 _value = default257 else:258 try:259 _value = int(value)260 except ValueError:261 raise ValueError(f"If set, {key} must be a int.")262 return _value263 264 265_run_slow_tests = parse_flag_from_env("RUN_SLOW", default=False)266_run_flaky_tests = parse_flag_from_env("RUN_FLAKY", default=True)267_run_custom_tokenizers = parse_flag_from_env("RUN_CUSTOM_TOKENIZERS", default=False)268_run_staging = parse_flag_from_env("HUGGINGFACE_CO_STAGING", default=False)269_run_pipeline_tests = parse_flag_from_env("RUN_PIPELINE_TESTS", default=True)270_run_agent_tests = parse_flag_from_env("RUN_AGENT_TESTS", default=False)271 272 273def is_staging_test(test_case):274 """275 Decorator marking a test as a staging test.276 277 Those tests will run using the staging environment of huggingface.co instead of the real model hub.278 """279 if not _run_staging:280 return unittest.skip(reason="test is staging test")(test_case)281 else:282 try:283 import pytest # We don't need a hard dependency on pytest in the main library284 except ImportError:285 return test_case286 else:287 return pytest.mark.is_staging_test()(test_case)288 289 290def is_pipeline_test(test_case):291 """292 Decorator marking a test as a pipeline test. If RUN_PIPELINE_TESTS is set to a falsy value, those tests will be293 skipped.294 """295 if not _run_pipeline_tests:296 return unittest.skip(reason="test is pipeline test")(test_case)297 else:298 try:299 import pytest # We don't need a hard dependency on pytest in the main library300 except ImportError:301 return test_case302 else:303 return pytest.mark.is_pipeline_test()(test_case)304 305 306def is_agent_test(test_case):307 """308 Decorator marking a test as an agent test. If RUN_TOOL_TESTS is set to a falsy value, those tests will be skipped.309 """310 if not _run_agent_tests:311 return unittest.skip(reason="test is an agent test")(test_case)312 else:313 try:314 import pytest # We don't need a hard dependency on pytest in the main library315 except ImportError:316 return test_case317 else:318 return pytest.mark.is_agent_test()(test_case)319 320 321def slow(test_case):322 """323 Decorator marking a test as slow.324 325 Slow tests are skipped by default. Set the RUN_SLOW environment variable to a truthy value to run them.326 327 """328 return unittest.skipUnless(_run_slow_tests, "test is slow")(test_case)329 330 331def tooslow(test_case):332 """333 Decorator marking a test as too slow.334 335 Slow tests are skipped while they're in the process of being fixed. No test should stay tagged as "tooslow" as336 these will not be tested by the CI.337 338 """339 return unittest.skip(reason="test is too slow")(test_case)340 341 342def skip_if_not_implemented(test_func):343 @functools.wraps(test_func)344 def wrapper(*args, **kwargs):345 try:346 return test_func(*args, **kwargs)347 except NotImplementedError as e:348 raise unittest.SkipTest(f"Test skipped due to NotImplementedError: {e}")349 350 return wrapper351 352 353def apply_skip_if_not_implemented(cls):354 """355 Class decorator to apply @skip_if_not_implemented to all test methods.356 """357 for attr_name in dir(cls):358 if attr_name.startswith("test_"):359 attr = getattr(cls, attr_name)360 if callable(attr):361 setattr(cls, attr_name, skip_if_not_implemented(attr))362 return cls363 364 365def custom_tokenizers(test_case):366 """367 Decorator marking a test for a custom tokenizer.368 369 Custom tokenizers require additional dependencies, and are skipped by default. Set the RUN_CUSTOM_TOKENIZERS370 environment variable to a truthy value to run them.371 """372 return unittest.skipUnless(_run_custom_tokenizers, "test of custom tokenizers")(test_case)373 374 375def require_bs4(test_case):376 """377 Decorator marking a test that requires BeautifulSoup4. These tests are skipped when BeautifulSoup4 isn't installed.378 """379 return unittest.skipUnless(is_bs4_available(), "test requires BeautifulSoup4")(test_case)380 381 382def require_galore_torch(test_case):383 """384 Decorator marking a test that requires GaLore. These tests are skipped when GaLore isn't installed.385 https://github.com/jiaweizzhao/GaLore386 """387 return unittest.skipUnless(is_galore_torch_available(), "test requires GaLore")(test_case)388 389 390def require_apollo_torch(test_case):391 """392 Decorator marking a test that requires GaLore. These tests are skipped when APOLLO isn't installed.393 https://github.com/zhuhanqing/APOLLO394 """395 return unittest.skipUnless(is_apollo_torch_available(), "test requires APOLLO")(test_case)396 397 398def require_torch_optimi(test_case):399 """400 Decorator marking a test that requires torch-optimi. These tests are skipped when torch-optimi isn't installed.401 https://github.com/jxnl/torch-optimi402 """403 return unittest.skipUnless(is_torch_optimi_available(), "test requires torch-optimi")(test_case)404 405 406def require_lomo(test_case):407 """408 Decorator marking a test that requires LOMO. These tests are skipped when LOMO-optim isn't installed.409 https://github.com/OpenLMLab/LOMO410 """411 return unittest.skipUnless(is_lomo_available(), "test requires LOMO")(test_case)412 413 414def require_grokadamw(test_case):415 """416 Decorator marking a test that requires GrokAdamW. These tests are skipped when GrokAdamW isn't installed.417 """418 return unittest.skipUnless(is_grokadamw_available(), "test requires GrokAdamW")(test_case)419 420 421def require_schedulefree(test_case):422 """423 Decorator marking a test that requires schedulefree. These tests are skipped when schedulefree isn't installed.424 https://github.com/facebookresearch/schedule_free425 """426 return unittest.skipUnless(is_schedulefree_available(), "test requires schedulefree")(test_case)427 428 429def require_cv2(test_case):430 """431 Decorator marking a test that requires OpenCV.432 433 These tests are skipped when OpenCV isn't installed.434 435 """436 return unittest.skipUnless(is_cv2_available(), "test requires OpenCV")(test_case)437 438 439def require_levenshtein(test_case):440 """441 Decorator marking a test that requires Levenshtein.442 443 These tests are skipped when Levenshtein isn't installed.444 445 """446 return unittest.skipUnless(is_levenshtein_available(), "test requires Levenshtein")(test_case)447 448 449def require_nltk(test_case):450 """451 Decorator marking a test that requires NLTK.452 453 These tests are skipped when NLTK isn't installed.454 455 """456 return unittest.skipUnless(is_nltk_available(), "test requires NLTK")(test_case)457 458 459def require_accelerate(test_case, min_version: str = ACCELERATE_MIN_VERSION):460 """461 Decorator marking a test that requires accelerate. These tests are skipped when accelerate isn't installed.462 """463 return unittest.skipUnless(464 is_accelerate_available(min_version), f"test requires accelerate version >= {min_version}"465 )(test_case)466 467 468def require_triton(min_version: str = TRITON_MIN_VERSION):469 """470 Decorator marking a test that requires triton. These tests are skipped when triton isn't installed.471 """472 473 def decorator(test_case):474 return unittest.skipUnless(is_triton_available(min_version), f"test requires triton version >= {min_version}")(475 test_case476 )477 478 return decorator479 480 481def require_gguf(test_case, min_version: str = GGUF_MIN_VERSION):482 """483 Decorator marking a test that requires ggguf. These tests are skipped when gguf isn't installed.484 """485 return unittest.skipUnless(is_gguf_available(min_version), f"test requires gguf version >= {min_version}")(486 test_case487 )488 489 490def require_fsdp(test_case, min_version: str = "1.12.0"):491 """492 Decorator marking a test that requires fsdp. These tests are skipped when fsdp isn't installed.493 """494 return unittest.skipUnless(is_fsdp_available(min_version), f"test requires torch version >= {min_version}")(495 test_case496 )497 498 499def require_g2p_en(test_case):500 """501 Decorator marking a test that requires g2p_en. These tests are skipped when SentencePiece isn't installed.502 """503 return unittest.skipUnless(is_g2p_en_available(), "test requires g2p_en")(test_case)504 505 506def require_safetensors(test_case):507 """508 Decorator marking a test that requires safetensors. These tests are skipped when safetensors isn't installed.509 """510 return unittest.skipUnless(is_safetensors_available(), "test requires safetensors")(test_case)511 512 513def require_rjieba(test_case):514 """515 Decorator marking a test that requires rjieba. These tests are skipped when rjieba isn't installed.516 """517 return unittest.skipUnless(is_rjieba_available(), "test requires rjieba")(test_case)518 519 520def require_jinja(test_case):521 """522 Decorator marking a test that requires jinja. These tests are skipped when jinja isn't installed.523 """524 return unittest.skipUnless(is_jinja_available(), "test requires jinja")(test_case)525 526 527def require_onnx(test_case):528 return unittest.skipUnless(is_onnx_available(), "test requires ONNX")(test_case)529 530 531def require_timm(test_case):532 """533 Decorator marking a test that requires Timm.534 535 These tests are skipped when Timm isn't installed.536 537 """538 return unittest.skipUnless(is_timm_available(), "test requires Timm")(test_case)539 540 541def require_natten(test_case):542 """543 Decorator marking a test that requires NATTEN.544 545 These tests are skipped when NATTEN isn't installed.546 547 """548 return unittest.skipUnless(is_natten_available(), "test requires natten")(test_case)549 550 551def require_torch(test_case):552 """553 Decorator marking a test that requires PyTorch.554 555 These tests are skipped when PyTorch isn't installed.556 557 """558 return unittest.skipUnless(is_torch_available(), "test requires PyTorch")(test_case)559 560 561def require_torch_greater_or_equal(version: str):562 """563 Decorator marking a test that requires PyTorch version >= `version`.564 565 These tests are skipped when PyTorch version is less than `version`.566 """567 568 def decorator(test_case):569 return unittest.skipUnless(is_torch_greater_or_equal(version), f"test requires PyTorch version >= {version}")(570 test_case571 )572 573 return decorator574 575 576def require_huggingface_hub_greater_or_equal(version: str):577 """578 Decorator marking a test that requires huggingface_hub version >= `version`.579 580 These tests are skipped when huggingface_hub version is less than `version`.581 """582 583 def decorator(test_case):584 return unittest.skipUnless(585 is_huggingface_hub_greater_or_equal(version), f"test requires huggingface_hub version >= {version}"586 )(test_case)587 588 return decorator589 590 591def require_flash_attn(test_case):592 """593 Decorator marking a test that requires Flash Attention.594 595 These tests are skipped when Flash Attention isn't installed.596 597 """598 flash_attn_available = is_flash_attn_2_available()599 kernels_available = is_kernels_available()600 try:601 from kernels import get_kernel602 603 get_kernel("kernels-community/flash-attn")604 except Exception as _:605 kernels_available = False606 607 return unittest.skipUnless(kernels_available | flash_attn_available, "test requires Flash Attention")(test_case)608 609 610def require_kernels(test_case):611 """612 Decorator marking a test that requires the kernels library.613 614 These tests are skipped when the kernels library isn't installed.615 616 """617 return unittest.skipUnless(is_kernels_available(), "test requires the kernels library")(test_case)618 619 620def require_flash_attn_3(test_case):621 """622 Decorator marking a test that requires Flash Attention 3.623 624 These tests are skipped when Flash Attention 3 isn't installed.625 """626 return unittest.skipUnless(is_flash_attn_3_available(), "test requires Flash Attention 3")(test_case)627 628 629def require_read_token(test_case):630 """631 A decorator that loads the HF token for tests that require to load gated models.632 """633 token = os.getenv("HF_HUB_READ_TOKEN")634 635 if isinstance(test_case, type):636 for attr_name in dir(test_case):637 attr = getattr(test_case, attr_name)638 if isinstance(attr, types.FunctionType):639 if getattr(attr, "__require_read_token__", False):640 continue641 wrapped = require_read_token(attr)642 setattr(test_case, attr_name, wrapped)643 return test_case644 else:645 if getattr(test_case, "__require_read_token__", False):646 return test_case647 648 @functools.wraps(test_case)649 def wrapper(*args, **kwargs):650 if token is not None:651 with patch("huggingface_hub.utils._headers.get_token", return_value=token):652 return test_case(*args, **kwargs)653 else: # Allow running locally with the default token env variable654 # dealing with static/class methods and called by `self.xxx`655 if "staticmethod" in inspect.getsource(test_case).strip():656 if len(args) > 0 and isinstance(args[0], unittest.TestCase):657 return test_case(*args[1:], **kwargs)658 return test_case(*args, **kwargs)659 660 wrapper.__require_read_token__ = True661 return wrapper662 663 664def require_peft(test_case):665 """666 Decorator marking a test that requires PEFT.667 668 These tests are skipped when PEFT isn't installed.669 670 """671 return unittest.skipUnless(is_peft_available(), "test requires PEFT")(test_case)672 673 674def require_torchvision(test_case):675 """676 Decorator marking a test that requires Torchvision.677 678 These tests are skipped when Torchvision isn't installed.679 680 """681 return unittest.skipUnless(is_torchvision_available(), "test requires Torchvision")(test_case)682 683 684def require_torchcodec(test_case):685 """686 Decorator marking a test that requires Torchcodec.687 688 These tests are skipped when Torchcodec isn't installed.689 690 """691 return unittest.skipUnless(is_torchcodec_available(), "test requires Torchcodec")(test_case)692 693 694def require_torch_or_tf(test_case):695 """696 Decorator marking a test that requires PyTorch or TensorFlow.697 698 These tests are skipped when neither PyTorch not TensorFlow is installed.699 700 """701 return unittest.skipUnless(is_torch_available() or is_tf_available(), "test requires PyTorch or TensorFlow")(702 test_case703 )704 705 706def require_intel_extension_for_pytorch(test_case):707 """708 Decorator marking a test that requires Intel Extension for PyTorch.709 710 These tests are skipped when Intel Extension for PyTorch isn't installed or it does not match current PyTorch711 version.712 713 """714 return unittest.skipUnless(715 is_ipex_available(),716 "test requires Intel Extension for PyTorch to be installed and match current PyTorch version, see"717 " https://github.com/intel/intel-extension-for-pytorch",718 )(test_case)719 720 721def require_torchaudio(test_case):722 """723 Decorator marking a test that requires torchaudio. These tests are skipped when torchaudio isn't installed.724 """725 return unittest.skipUnless(is_torchaudio_available(), "test requires torchaudio")(test_case)726 727 728def require_sentencepiece(test_case):729 """730 Decorator marking a test that requires SentencePiece. These tests are skipped when SentencePiece isn't installed.731 """732 return unittest.skipUnless(is_sentencepiece_available(), "test requires SentencePiece")(test_case)733 734 735def require_sacremoses(test_case):736 """737 Decorator marking a test that requires Sacremoses. These tests are skipped when Sacremoses isn't installed.738 """739 return unittest.skipUnless(is_sacremoses_available(), "test requires Sacremoses")(test_case)740 741 742def require_seqio(test_case):743 """744 Decorator marking a test that requires SentencePiece. These tests are skipped when SentencePiece isn't installed.745 """746 return unittest.skipUnless(is_seqio_available(), "test requires Seqio")(test_case)747 748 749def require_scipy(test_case):750 """751 Decorator marking a test that requires Scipy. These tests are skipped when SentencePiece isn't installed.752 """753 return unittest.skipUnless(is_scipy_available(), "test requires Scipy")(test_case)754 755 756def require_tokenizers(test_case):757 """758 Decorator marking a test that requires 🤗 Tokenizers. These tests are skipped when 🤗 Tokenizers isn't installed.759 """760 return unittest.skipUnless(is_tokenizers_available(), "test requires tokenizers")(test_case)761 762 763def require_keras_nlp(test_case):764 """765 Decorator marking a test that requires keras_nlp. These tests are skipped when keras_nlp isn't installed.766 """767 return unittest.skipUnless(is_keras_nlp_available(), "test requires keras_nlp")(test_case)768 769 770def require_pandas(test_case):771 """772 Decorator marking a test that requires pandas. These tests are skipped when pandas isn't installed.773 """774 return unittest.skipUnless(is_pandas_available(), "test requires pandas")(test_case)775 776 777def require_pytesseract(test_case):778 """779 Decorator marking a test that requires PyTesseract. These tests are skipped when PyTesseract isn't installed.780 """781 return unittest.skipUnless(is_pytesseract_available(), "test requires PyTesseract")(test_case)782 783 784def require_pytorch_quantization(test_case):785 """786 Decorator marking a test that requires PyTorch Quantization Toolkit. These tests are skipped when PyTorch787 Quantization Toolkit isn't installed.788 """789 return unittest.skipUnless(is_pytorch_quantization_available(), "test requires PyTorch Quantization Toolkit")(790 test_case791 )792 793 794def require_vision(test_case):795 """796 Decorator marking a test that requires the vision dependencies. These tests are skipped when torchaudio isn't797 installed.798 """799 return unittest.skipUnless(is_vision_available(), "test requires vision")(test_case)800 801 802def require_ftfy(test_case):803 """804 Decorator marking a test that requires ftfy. These tests are skipped when ftfy isn't installed.805 """806 return unittest.skipUnless(is_ftfy_available(), "test requires ftfy")(test_case)807 808 809def require_spacy(test_case):810 """811 Decorator marking a test that requires SpaCy. These tests are skipped when SpaCy isn't installed.812 """813 return unittest.skipUnless(is_spacy_available(), "test requires spacy")(test_case)814 815 816def require_torch_multi_gpu(test_case):817 """818 Decorator marking a test that requires a multi-GPU CUDA setup (in PyTorch). These tests are skipped on a machine without819 multiple CUDA GPUs.820 821 To run *only* the multi_gpu tests, assuming all test names contain multi_gpu: $ pytest -sv ./tests -k "multi_gpu"822 """823 if not is_torch_available():824 return unittest.skip(reason="test requires PyTorch")(test_case)825 826 import torch827 828 return unittest.skipUnless(torch.cuda.device_count() > 1, "test requires multiple CUDA GPUs")(test_case)829 830 831def require_torch_multi_accelerator(test_case):832 """833 Decorator marking a test that requires a multi-accelerator (in PyTorch). These tests are skipped on a machine834 without multiple accelerators. To run *only* the multi_accelerator tests, assuming all test names contain835 multi_accelerator: $ pytest -sv ./tests -k "multi_accelerator"836 """837 if not is_torch_available():838 return unittest.skip(reason="test requires PyTorch")(test_case)839 840 return unittest.skipUnless(backend_device_count(torch_device) > 1, "test requires multiple accelerators")(841 test_case842 )843 844 845def require_torch_non_multi_gpu(test_case):846 """847 Decorator marking a test that requires 0 or 1 GPU setup (in PyTorch).848 """849 if not is_torch_available():850 return unittest.skip(reason="test requires PyTorch")(test_case)851 852 import torch853 854 return unittest.skipUnless(torch.cuda.device_count() < 2, "test requires 0 or 1 GPU")(test_case)855 856 857def require_torch_non_multi_accelerator(test_case):858 """859 Decorator marking a test that requires 0 or 1 accelerator setup (in PyTorch).860 """861 if not is_torch_available():862 return unittest.skip(reason="test requires PyTorch")(test_case)863 864 return unittest.skipUnless(backend_device_count(torch_device) < 2, "test requires 0 or 1 accelerator")(test_case)865 866 867def require_torch_up_to_2_gpus(test_case):868 """869 Decorator marking a test that requires 0 or 1 or 2 GPU setup (in PyTorch).870 """871 if not is_torch_available():872 return unittest.skip(reason="test requires PyTorch")(test_case)873 874 import torch875 876 return unittest.skipUnless(torch.cuda.device_count() < 3, "test requires 0 or 1 or 2 GPUs")(test_case)877 878 879def require_torch_up_to_2_accelerators(test_case):880 """881 Decorator marking a test that requires 0 or 1 or 2 accelerator setup (in PyTorch).882 """883 if not is_torch_available():884 return unittest.skip(reason="test requires PyTorch")(test_case)885 886 return unittest.skipUnless(backend_device_count(torch_device) < 3, "test requires 0 or 1 or 2 accelerators")(887 test_case888 )889 890 891def require_torch_xla(test_case):892 """893 Decorator marking a test that requires TorchXLA (in PyTorch).894 """895 return unittest.skipUnless(is_torch_xla_available(), "test requires TorchXLA")(test_case)896 897 898def require_torch_neuroncore(test_case):899 """900 Decorator marking a test that requires NeuronCore (in PyTorch).901 """902 return unittest.skipUnless(is_torch_neuroncore_available(check_device=False), "test requires PyTorch NeuronCore")(903 test_case904 )905 906 907def require_torch_npu(test_case):908 """909 Decorator marking a test that requires NPU (in PyTorch).910 """911 return unittest.skipUnless(is_torch_npu_available(), "test requires PyTorch NPU")(test_case)912 913 914def require_torch_multi_npu(test_case):915 """916 Decorator marking a test that requires a multi-NPU setup (in PyTorch). These tests are skipped on a machine without917 multiple NPUs.918 919 To run *only* the multi_npu tests, assuming all test names contain multi_npu: $ pytest -sv ./tests -k "multi_npu"920 """921 if not is_torch_npu_available():922 return unittest.skip(reason="test requires PyTorch NPU")(test_case)923 924 return unittest.skipUnless(torch.npu.device_count() > 1, "test requires multiple NPUs")(test_case)925 926 927def require_non_hpu(test_case):928 """929 Decorator marking a test that should be skipped for HPU.930 """931 return unittest.skipUnless(torch_device != "hpu", "test requires a non-HPU")(test_case)932 933 934def require_torch_xpu(test_case):935 """936 Decorator marking a test that requires XPU (in PyTorch).937 938 These tests are skipped when XPU backend is not available. XPU backend might be available either via stock939 PyTorch (>=2.4) or via Intel Extension for PyTorch. In the latter case, if IPEX is installed, its version940 must match match current PyTorch version.941 """942 return unittest.skipUnless(is_torch_xpu_available(), "test requires XPU device")(test_case)943 944 945def require_non_xpu(test_case):946 """947 Decorator marking a test that should be skipped for XPU.948 """949 return unittest.skipUnless(torch_device != "xpu", "test requires a non-XPU")(test_case)950 951 952def require_torch_multi_xpu(test_case):953 """954 Decorator marking a test that requires a multi-XPU setup (in PyTorch). These tests are skipped on a machine without955 multiple XPUs.956 957 To run *only* the multi_xpu tests, assuming all test names contain multi_xpu: $ pytest -sv ./tests -k "multi_xpu"958 """959 if not is_torch_xpu_available():960 return unittest.skip(reason="test requires PyTorch XPU")(test_case)961 962 return unittest.skipUnless(torch.xpu.device_count() > 1, "test requires multiple XPUs")(test_case)963 964 965def require_torch_multi_hpu(test_case):966 """967 Decorator marking a test that requires a multi-HPU setup (in PyTorch). These tests are skipped on a machine without968 multiple HPUs.969 970 To run *only* the multi_hpu tests, assuming all test names contain multi_hpu: $ pytest -sv ./tests -k "multi_hpu"971 """972 if not is_torch_hpu_available():973 return unittest.skip(reason="test requires PyTorch HPU")(test_case)974 975 return unittest.skipUnless(torch.hpu.device_count() > 1, "test requires multiple HPUs")(test_case)976 977 978if is_torch_available():979 # Set env var CUDA_VISIBLE_DEVICES="" to force cpu-mode980 import torch981 982 if "TRANSFORMERS_TEST_BACKEND" in os.environ:983 backend = os.environ["TRANSFORMERS_TEST_BACKEND"]984 try:985 _ = importlib.import_module(backend)986 except ModuleNotFoundError as e:987 raise ModuleNotFoundError(988 f"Failed to import `TRANSFORMERS_TEST_BACKEND` '{backend}'! This should be the name of an installed module. The original error (look up to see its"989 f" traceback):\n{e}"990 ) from e991 992 if "TRANSFORMERS_TEST_DEVICE" in os.environ:993 torch_device = os.environ["TRANSFORMERS_TEST_DEVICE"]994 if torch_device == "cuda" and not torch.cuda.is_available():995 raise ValueError(996 f"TRANSFORMERS_TEST_DEVICE={torch_device}, but CUDA is unavailable. Please double-check your testing environment."997 )998 if torch_device == "xpu" and not is_torch_xpu_available():999 raise ValueError(1000 f"TRANSFORMERS_TEST_DEVICE={torch_device}, but XPU is unavailable. Please double-check your testing environment."1001 )1002 if torch_device == "npu" and not is_torch_npu_available():1003 raise ValueError(1004 f"TRANSFORMERS_TEST_DEVICE={torch_device}, but NPU is unavailable. Please double-check your testing environment."1005 )1006 if torch_device == "mlu" and not is_torch_mlu_available():1007 raise ValueError(1008 f"TRANSFORMERS_TEST_DEVICE={torch_device}, but MLU is unavailable. Please double-check your testing environment."1009 )1010 if torch_device == "hpu" and not is_torch_hpu_available():1011 raise ValueError(1012 f"TRANSFORMERS_TEST_DEVICE={torch_device}, but HPU is unavailable. Please double-check your testing environment."1013 )1014 1015 try:1016 # try creating device to see if provided device is valid1017 _ = torch.device(torch_device)1018 except RuntimeError as e:1019 raise RuntimeError(1020 f"Unknown testing device specified by environment variable `TRANSFORMERS_TEST_DEVICE`: {torch_device}"1021 ) from e1022 elif torch.cuda.is_available():1023 torch_device = "cuda"1024 elif is_torch_npu_available():1025 torch_device = "npu"1026 elif is_torch_mlu_available():1027 torch_device = "mlu"1028 elif is_torch_hpu_available():1029 torch_device = "hpu"1030 elif is_torch_xpu_available():1031 torch_device = "xpu"1032 else:1033 torch_device = "cpu"1034else:1035 torch_device = None1036 1037if is_tf_available():1038 import tensorflow as tf1039 1040if is_flax_available():1041 import jax1042 1043 jax_device = jax.default_backend()1044else:1045 jax_device = None1046 1047 1048def require_torchdynamo(test_case):1049 """Decorator marking a test that requires TorchDynamo"""1050 return unittest.skipUnless(is_torchdynamo_available(), "test requires TorchDynamo")(test_case)1051 1052 1053def require_torchao(test_case):1054 """Decorator marking a test that requires torchao"""1055 return unittest.skipUnless(is_torchao_available(), "test requires torchao")(test_case)1056 1057 1058def require_torchao_version_greater_or_equal(torchao_version):1059 def decorator(test_case):1060 correct_torchao_version = is_torchao_available() and version.parse(1061 version.parse(importlib.metadata.version("torchao")).base_version1062 ) >= version.parse(torchao_version)1063 return unittest.skipUnless(1064 correct_torchao_version, f"Test requires torchao with the version greater than {torchao_version}."1065 )(test_case)1066 1067 return decorator1068 1069 1070def require_torch_tensorrt_fx(test_case):1071 """Decorator marking a test that requires Torch-TensorRT FX"""1072 return unittest.skipUnless(is_torch_tensorrt_fx_available(), "test requires Torch-TensorRT FX")(test_case)1073 1074 1075def require_torch_gpu(test_case):1076 """Decorator marking a test that requires CUDA and PyTorch."""1077 return unittest.skipUnless(torch_device == "cuda", "test requires CUDA")(test_case)1078 1079 1080def require_torch_mps(test_case):1081 """Decorator marking a test that requires CUDA and PyTorch."""1082 return unittest.skipUnless(torch_device == "mps", "test requires MPS")(test_case)1083 1084 1085def require_large_cpu_ram(test_case, memory: float = 80):1086 """Decorator marking a test that requires a CPU RAM with more than `memory` GiB of memory."""1087 if not is_psutil_available():1088 return test_case1089 1090 import psutil1091 1092 return unittest.skipUnless(1093 psutil.virtual_memory().total / 1024**3 > memory,1094 f"test requires a machine with more than {memory} GiB of CPU RAM memory",1095 )(test_case)1096 1097 1098def require_torch_large_gpu(test_case, memory: float = 20):1099 """Decorator marking a test that requires a CUDA GPU with more than `memory` GiB of memory."""1100 if torch_device != "cuda":1101 return unittest.skip(reason=f"test requires a CUDA GPU with more than {memory} GiB of memory")(test_case)1102 1103 return unittest.skipUnless(1104 torch.cuda.get_device_properties(0).total_memory / 1024**3 > memory,1105 f"test requires a GPU with more than {memory} GiB of memory",1106 )(test_case)1107 1108 1109def require_torch_large_accelerator(test_case, memory: float = 20):1110 """Decorator marking a test that requires an accelerator with more than `memory` GiB of memory."""1111 if torch_device != "cuda" and torch_device != "xpu":1112 return unittest.skip(reason=f"test requires a GPU or XPU with more than {memory} GiB of memory")(test_case)1113 1114 torch_accelerator_module = getattr(torch, torch_device)1115 1116 return unittest.skipUnless(1117 torch_accelerator_module.get_device_properties(0).total_memory / 1024**3 > memory,1118 f"test requires a GPU or XPU with more than {memory} GiB of memory",1119 )(test_case)1120 1121 1122def require_torch_gpu_if_bnb_not_multi_backend_enabled(test_case):1123 """1124 Decorator marking a test that requires a GPU if bitsandbytes multi-backend feature is not enabled.1125 """1126 if is_bitsandbytes_available() and is_bitsandbytes_multi_backend_available():1127 return test_case1128 return require_torch_gpu(test_case)1129 1130 1131def require_torch_accelerator(test_case):1132 """Decorator marking a test that requires an accessible accelerator and PyTorch."""1133 return unittest.skipUnless(torch_device is not None and torch_device != "cpu", "test requires accelerator")(1134 test_case1135 )1136 1137 1138def require_torch_fp16(test_case):1139 """Decorator marking a test that requires a device that supports fp16"""1140 return unittest.skipUnless(1141 is_torch_fp16_available_on_device(torch_device), "test requires device with fp16 support"1142 )(test_case)1143 1144 1145def require_fp8(test_case):1146 """Decorator marking a test that requires supports for fp8"""1147 return unittest.skipUnless(is_accelerate_available() and is_fp8_available(), "test requires fp8 support")(1148 test_case1149 )1150 1151 1152def require_torch_bf16(test_case):1153 """Decorator marking a test that requires a device that supports bf16"""1154 return unittest.skipUnless(1155 is_torch_bf16_available_on_device(torch_device), "test requires device with bf16 support"1156 )(test_case)1157 1158 1159def require_torch_bf16_gpu(test_case):1160 """Decorator marking a test that requires torch>=1.10, using Ampere GPU or newer arch with cuda>=11.0"""1161 return unittest.skipUnless(1162 is_torch_bf16_gpu_available(),1163 "test requires torch>=1.10, using Ampere GPU or newer arch with cuda>=11.0",1164 )(test_case)1165 1166 1167def require_deterministic_for_xpu(test_case):1168 @wraps(test_case)1169 def wrapper(*args, **kwargs):1170 if is_torch_xpu_available():1171 original_state = torch.are_deterministic_algorithms_enabled()1172 try:1173 torch.use_deterministic_algorithms(True)1174 return test_case(*args, **kwargs)1175 finally:1176 torch.use_deterministic_algorithms(original_state)1177 else:1178 return test_case(*args, **kwargs)1179 1180 return wrapper1181 1182 1183def require_torch_tf32(test_case):1184 """Decorator marking a test that requires Ampere or a newer GPU arch, cuda>=11 and torch>=1.7."""1185 return unittest.skipUnless(1186 is_torch_tf32_available(), "test requires Ampere or a newer GPU arch, cuda>=11 and torch>=1.7"1187 )(test_case)1188 1189 1190def require_detectron2(test_case):1191 """Decorator marking a test that requires detectron2."""1192 return unittest.skipUnless(is_detectron2_available(), "test requires `detectron2`")(test_case)1193 1194 1195def require_faiss(test_case):1196 """Decorator marking a test that requires faiss."""1197 return unittest.skipUnless(is_faiss_available(), "test requires `faiss`")(test_case)1198 1199 1200def require_optuna(test_case):