CoolFace
Apppublic

DoruC/Grounded-Segment-Anything

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
testing_utils.py2179 linesDownload Raw Back to transformers_4_35_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 collections16import contextlib17import doctest18import functools19import importlib20import inspect21import logging22import multiprocessing23import os24import re25import shlex26import shutil27import subprocess28import sys29import tempfile30import time31import unittest32from collections.abc import Mapping33from io import StringIO34from pathlib import Path35from typing import Iterable, Iterator, List, Optional, Union36from unittest import mock37 38import huggingface_hub39import requests40 41from transformers import logging as transformers_logging42 43from .integrations import (44    is_clearml_available,45    is_optuna_available,46    is_ray_available,47    is_sigopt_available,48    is_wandb_available,49)50from .integrations.deepspeed import is_deepspeed_available51from .utils import (52    is_accelerate_available,53    is_apex_available,54    is_auto_gptq_available,55    is_bitsandbytes_available,56    is_bs4_available,57    is_cv2_available,58    is_cython_available,59    is_decord_available,60    is_detectron2_available,61    is_essentia_available,62    is_faiss_available,63    is_flash_attn_available,64    is_flax_available,65    is_fsdp_available,66    is_ftfy_available,67    is_ipex_available,68    is_jieba_available,69    is_jinja_available,70    is_jumanpp_available,71    is_keras_nlp_available,72    is_levenshtein_available,73    is_librosa_available,74    is_natten_available,75    is_nltk_available,76    is_onnx_available,77    is_optimum_available,78    is_pandas_available,79    is_peft_available,80    is_phonemizer_available,81    is_pretty_midi_available,82    is_pyctcdecode_available,83    is_pytesseract_available,84    is_pytest_available,85    is_pytorch_quantization_available,86    is_rjieba_available,87    is_safetensors_available,88    is_scipy_available,89    is_sentencepiece_available,90    is_seqio_available,91    is_soundfile_availble,92    is_spacy_available,93    is_sudachi_available,94    is_tensorflow_probability_available,95    is_tensorflow_text_available,96    is_tf2onnx_available,97    is_tf_available,98    is_timm_available,99    is_tokenizers_available,100    is_torch_available,101    is_torch_bf16_cpu_available,102    is_torch_bf16_gpu_available,103    is_torch_neuroncore_available,104    is_torch_npu_available,105    is_torch_tensorrt_fx_available,106    is_torch_tf32_available,107    is_torch_tpu_available,108    is_torch_xpu_available,109    is_torchaudio_available,110    is_torchdynamo_available,111    is_torchvision_available,112    is_vision_available,113    strtobool,114)115 116 117if is_accelerate_available():118    from accelerate.state import AcceleratorState, PartialState119 120 121if is_pytest_available():122    from _pytest.doctest import (123        Module,124        _get_checker,125        _get_continue_on_failure,126        _get_runner,127        _is_mocked,128        _patch_unwrap_mock_aware,129        get_optionflags,130        import_path,131    )132    from _pytest.outcomes import skip133    from pytest import DoctestItem134else:135    Module = object136    DoctestItem = object137 138 139SMALL_MODEL_IDENTIFIER = "julien-c/bert-xsmall-dummy"140DUMMY_UNKNOWN_IDENTIFIER = "julien-c/dummy-unknown"141DUMMY_DIFF_TOKENIZER_IDENTIFIER = "julien-c/dummy-diff-tokenizer"142# Used to test Auto{Config, Model, Tokenizer} model_type detection.143 144# Used to test the hub145USER = "__DUMMY_TRANSFORMERS_USER__"146ENDPOINT_STAGING = "https://hub-ci.huggingface.co"147 148# Not critical, only usable on the sandboxed CI instance.149TOKEN = "hf_94wBhPGp6KrrTH3KDchhKpRxZwd6dmHWLL"150 151 152def parse_flag_from_env(key, default=False):153    try:154        value = os.environ[key]155    except KeyError:156        # KEY isn't set, default to `default`.157        _value = default158    else:159        # KEY is set, convert it to True or False.160        try:161            _value = strtobool(value)162        except ValueError:163            # More values are supported, but let's keep the message simple.164            raise ValueError(f"If set, {key} must be yes or no.")165    return _value166 167 168def parse_int_from_env(key, default=None):169    try:170        value = os.environ[key]171    except KeyError:172        _value = default173    else:174        try:175            _value = int(value)176        except ValueError:177            raise ValueError(f"If set, {key} must be a int.")178    return _value179 180 181_run_slow_tests = parse_flag_from_env("RUN_SLOW", default=False)182_run_pt_tf_cross_tests = parse_flag_from_env("RUN_PT_TF_CROSS_TESTS", default=True)183_run_pt_flax_cross_tests = parse_flag_from_env("RUN_PT_FLAX_CROSS_TESTS", default=True)184_run_custom_tokenizers = parse_flag_from_env("RUN_CUSTOM_TOKENIZERS", default=False)185_run_staging = parse_flag_from_env("HUGGINGFACE_CO_STAGING", default=False)186_tf_gpu_memory_limit = parse_int_from_env("TF_GPU_MEMORY_LIMIT", default=None)187_run_pipeline_tests = parse_flag_from_env("RUN_PIPELINE_TESTS", default=True)188_run_tool_tests = parse_flag_from_env("RUN_TOOL_TESTS", default=False)189_run_third_party_device_tests = parse_flag_from_env("RUN_THIRD_PARTY_DEVICE_TESTS", default=False)190 191 192def is_pt_tf_cross_test(test_case):193    """194    Decorator marking a test as a test that control interactions between PyTorch and TensorFlow.195 196    PT+TF tests are skipped by default and we can run only them by setting RUN_PT_TF_CROSS_TESTS environment variable197    to a truthy value and selecting the is_pt_tf_cross_test pytest mark.198 199    """200    if not _run_pt_tf_cross_tests or not is_torch_available() or not is_tf_available():201        return unittest.skip("test is PT+TF test")(test_case)202    else:203        try:204            import pytest  # We don't need a hard dependency on pytest in the main library205        except ImportError:206            return test_case207        else:208            return pytest.mark.is_pt_tf_cross_test()(test_case)209 210 211def is_pt_flax_cross_test(test_case):212    """213    Decorator marking a test as a test that control interactions between PyTorch and Flax214 215    PT+FLAX tests are skipped by default and we can run only them by setting RUN_PT_FLAX_CROSS_TESTS environment216    variable to a truthy value and selecting the is_pt_flax_cross_test pytest mark.217 218    """219    if not _run_pt_flax_cross_tests or not is_torch_available() or not is_flax_available():220        return unittest.skip("test is PT+FLAX test")(test_case)221    else:222        try:223            import pytest  # We don't need a hard dependency on pytest in the main library224        except ImportError:225            return test_case226        else:227            return pytest.mark.is_pt_flax_cross_test()(test_case)228 229 230def is_staging_test(test_case):231    """232    Decorator marking a test as a staging test.233 234    Those tests will run using the staging environment of huggingface.co instead of the real model hub.235    """236    if not _run_staging:237        return unittest.skip("test is staging test")(test_case)238    else:239        try:240            import pytest  # We don't need a hard dependency on pytest in the main library241        except ImportError:242            return test_case243        else:244            return pytest.mark.is_staging_test()(test_case)245 246 247def is_pipeline_test(test_case):248    """249    Decorator marking a test as a pipeline test. If RUN_PIPELINE_TESTS is set to a falsy value, those tests will be250    skipped.251    """252    if not _run_pipeline_tests:253        return unittest.skip("test is pipeline test")(test_case)254    else:255        try:256            import pytest  # We don't need a hard dependency on pytest in the main library257        except ImportError:258            return test_case259        else:260            return pytest.mark.is_pipeline_test()(test_case)261 262 263def is_tool_test(test_case):264    """265    Decorator marking a test as a tool test. If RUN_TOOL_TESTS is set to a falsy value, those tests will be skipped.266    """267    if not _run_tool_tests:268        return unittest.skip("test is a tool test")(test_case)269    else:270        try:271            import pytest  # We don't need a hard dependency on pytest in the main library272        except ImportError:273            return test_case274        else:275            return pytest.mark.is_tool_test()(test_case)276 277 278def slow(test_case):279    """280    Decorator marking a test as slow.281 282    Slow tests are skipped by default. Set the RUN_SLOW environment variable to a truthy value to run them.283 284    """285    return unittest.skipUnless(_run_slow_tests, "test is slow")(test_case)286 287 288def tooslow(test_case):289    """290    Decorator marking a test as too slow.291 292    Slow tests are skipped while they're in the process of being fixed. No test should stay tagged as "tooslow" as293    these will not be tested by the CI.294 295    """296    return unittest.skip("test is too slow")(test_case)297 298 299def custom_tokenizers(test_case):300    """301    Decorator marking a test for a custom tokenizer.302 303    Custom tokenizers require additional dependencies, and are skipped by default. Set the RUN_CUSTOM_TOKENIZERS304    environment variable to a truthy value to run them.305    """306    return unittest.skipUnless(_run_custom_tokenizers, "test of custom tokenizers")(test_case)307 308 309def require_bs4(test_case):310    """311    Decorator marking a test that requires BeautifulSoup4. These tests are skipped when BeautifulSoup4 isn't installed.312    """313    return unittest.skipUnless(is_bs4_available(), "test requires BeautifulSoup4")(test_case)314 315 316def require_cv2(test_case):317    """318    Decorator marking a test that requires OpenCV.319 320    These tests are skipped when OpenCV isn't installed.321 322    """323    return unittest.skipUnless(is_cv2_available(), "test requires OpenCV")(test_case)324 325 326def require_levenshtein(test_case):327    """328    Decorator marking a test that requires Levenshtein.329 330    These tests are skipped when Levenshtein isn't installed.331 332    """333    return unittest.skipUnless(is_levenshtein_available(), "test requires Levenshtein")(test_case)334 335 336def require_nltk(test_case):337    """338    Decorator marking a test that requires NLTK.339 340    These tests are skipped when NLTK isn't installed.341 342    """343    return unittest.skipUnless(is_nltk_available(), "test requires NLTK")(test_case)344 345 346def require_accelerate(test_case):347    """348    Decorator marking a test that requires accelerate. These tests are skipped when accelerate isn't installed.349    """350    return unittest.skipUnless(is_accelerate_available(), "test requires accelerate")(test_case)351 352 353def require_fsdp(test_case, min_version: str = "1.12.0"):354    """355    Decorator marking a test that requires fsdp. These tests are skipped when fsdp isn't installed.356    """357    return unittest.skipUnless(is_fsdp_available(min_version), f"test requires torch version >= {min_version}")(358        test_case359    )360 361 362def require_safetensors(test_case):363    """364    Decorator marking a test that requires safetensors. These tests are skipped when safetensors isn't installed.365    """366    return unittest.skipUnless(is_safetensors_available(), "test requires safetensors")(test_case)367 368 369def require_rjieba(test_case):370    """371    Decorator marking a test that requires rjieba. These tests are skipped when rjieba isn't installed.372    """373    return unittest.skipUnless(is_rjieba_available(), "test requires rjieba")(test_case)374 375 376def require_jieba(test_case):377    """378    Decorator marking a test that requires jieba. These tests are skipped when jieba isn't installed.379    """380    return unittest.skipUnless(is_jieba_available(), "test requires jieba")(test_case)381 382 383def require_jinja(test_case):384    """385    Decorator marking a test that requires jinja. These tests are skipped when jinja isn't installed.386    """387    return unittest.skipUnless(is_jinja_available(), "test requires jinja")(test_case)388 389 390def require_tf2onnx(test_case):391    return unittest.skipUnless(is_tf2onnx_available(), "test requires tf2onnx")(test_case)392 393 394def require_onnx(test_case):395    return unittest.skipUnless(is_onnx_available(), "test requires ONNX")(test_case)396 397 398def require_timm(test_case):399    """400    Decorator marking a test that requires Timm.401 402    These tests are skipped when Timm isn't installed.403 404    """405    return unittest.skipUnless(is_timm_available(), "test requires Timm")(test_case)406 407 408def require_natten(test_case):409    """410    Decorator marking a test that requires NATTEN.411 412    These tests are skipped when NATTEN isn't installed.413 414    """415    return unittest.skipUnless(is_natten_available(), "test requires natten")(test_case)416 417 418def require_torch(test_case):419    """420    Decorator marking a test that requires PyTorch.421 422    These tests are skipped when PyTorch isn't installed.423 424    """425    return unittest.skipUnless(is_torch_available(), "test requires PyTorch")(test_case)426 427 428def require_flash_attn(test_case):429    """430    Decorator marking a test that requires Flash Attention.431 432    These tests are skipped when Flash Attention isn't installed.433 434    """435    return unittest.skipUnless(is_flash_attn_available(), "test requires Flash Attention")(test_case)436 437 438def require_peft(test_case):439    """440    Decorator marking a test that requires PEFT.441 442    These tests are skipped when PEFT isn't installed.443 444    """445    return unittest.skipUnless(is_peft_available(), "test requires PEFT")(test_case)446 447 448def require_torchvision(test_case):449    """450    Decorator marking a test that requires Torchvision.451 452    These tests are skipped when Torchvision isn't installed.453 454    """455    return unittest.skipUnless(is_torchvision_available(), "test requires Torchvision")(test_case)456 457 458def require_torch_or_tf(test_case):459    """460    Decorator marking a test that requires PyTorch or TensorFlow.461 462    These tests are skipped when neither PyTorch not TensorFlow is installed.463 464    """465    return unittest.skipUnless(is_torch_available() or is_tf_available(), "test requires PyTorch or TensorFlow")(466        test_case467    )468 469 470def require_intel_extension_for_pytorch(test_case):471    """472    Decorator marking a test that requires Intel Extension for PyTorch.473 474    These tests are skipped when Intel Extension for PyTorch isn't installed or it does not match current PyTorch475    version.476 477    """478    return unittest.skipUnless(479        is_ipex_available(),480        "test requires Intel Extension for PyTorch to be installed and match current PyTorch version, see"481        " https://github.com/intel/intel-extension-for-pytorch",482    )(test_case)483 484 485def require_tensorflow_probability(test_case):486    """487    Decorator marking a test that requires TensorFlow probability.488 489    These tests are skipped when TensorFlow probability isn't installed.490 491    """492    return unittest.skipUnless(is_tensorflow_probability_available(), "test requires TensorFlow probability")(493        test_case494    )495 496 497def require_torchaudio(test_case):498    """499    Decorator marking a test that requires torchaudio. These tests are skipped when torchaudio isn't installed.500    """501    return unittest.skipUnless(is_torchaudio_available(), "test requires torchaudio")(test_case)502 503 504def require_tf(test_case):505    """506    Decorator marking a test that requires TensorFlow. These tests are skipped when TensorFlow isn't installed.507    """508    return unittest.skipUnless(is_tf_available(), "test requires TensorFlow")(test_case)509 510 511def require_flax(test_case):512    """513    Decorator marking a test that requires JAX & Flax. These tests are skipped when one / both are not installed514    """515    return unittest.skipUnless(is_flax_available(), "test requires JAX & Flax")(test_case)516 517 518def require_sentencepiece(test_case):519    """520    Decorator marking a test that requires SentencePiece. These tests are skipped when SentencePiece isn't installed.521    """522    return unittest.skipUnless(is_sentencepiece_available(), "test requires SentencePiece")(test_case)523 524 525def require_seqio(test_case):526    """527    Decorator marking a test that requires SentencePiece. These tests are skipped when SentencePiece isn't installed.528    """529    return unittest.skipUnless(is_seqio_available(), "test requires Seqio")(test_case)530 531 532def require_scipy(test_case):533    """534    Decorator marking a test that requires Scipy. These tests are skipped when SentencePiece isn't installed.535    """536    return unittest.skipUnless(is_scipy_available(), "test requires Scipy")(test_case)537 538 539def require_tokenizers(test_case):540    """541    Decorator marking a test that requires 🤗 Tokenizers. These tests are skipped when 🤗 Tokenizers isn't installed.542    """543    return unittest.skipUnless(is_tokenizers_available(), "test requires tokenizers")(test_case)544 545 546def require_tensorflow_text(test_case):547    """548    Decorator marking a test that requires tensorflow_text. These tests are skipped when tensroflow_text isn't549    installed.550    """551    return unittest.skipUnless(is_tensorflow_text_available(), "test requires tensorflow_text")(test_case)552 553 554def require_keras_nlp(test_case):555    """556    Decorator marking a test that requires keras_nlp. These tests are skipped when keras_nlp isn't installed.557    """558    return unittest.skipUnless(is_keras_nlp_available(), "test requires keras_nlp")(test_case)559 560 561def require_pandas(test_case):562    """563    Decorator marking a test that requires pandas. These tests are skipped when pandas isn't installed.564    """565    return unittest.skipUnless(is_pandas_available(), "test requires pandas")(test_case)566 567 568def require_pytesseract(test_case):569    """570    Decorator marking a test that requires PyTesseract. These tests are skipped when PyTesseract isn't installed.571    """572    return unittest.skipUnless(is_pytesseract_available(), "test requires PyTesseract")(test_case)573 574 575def require_pytorch_quantization(test_case):576    """577    Decorator marking a test that requires PyTorch Quantization Toolkit. These tests are skipped when PyTorch578    Quantization Toolkit isn't installed.579    """580    return unittest.skipUnless(is_pytorch_quantization_available(), "test requires PyTorch Quantization Toolkit")(581        test_case582    )583 584 585def require_vision(test_case):586    """587    Decorator marking a test that requires the vision dependencies. These tests are skipped when torchaudio isn't588    installed.589    """590    return unittest.skipUnless(is_vision_available(), "test requires vision")(test_case)591 592 593def require_ftfy(test_case):594    """595    Decorator marking a test that requires ftfy. These tests are skipped when ftfy isn't installed.596    """597    return unittest.skipUnless(is_ftfy_available(), "test requires ftfy")(test_case)598 599 600def require_spacy(test_case):601    """602    Decorator marking a test that requires SpaCy. These tests are skipped when SpaCy isn't installed.603    """604    return unittest.skipUnless(is_spacy_available(), "test requires spacy")(test_case)605 606 607def require_decord(test_case):608    """609    Decorator marking a test that requires decord. These tests are skipped when decord isn't installed.610    """611    return unittest.skipUnless(is_decord_available(), "test requires decord")(test_case)612 613 614def require_torch_multi_gpu(test_case):615    """616    Decorator marking a test that requires a multi-GPU setup (in PyTorch). These tests are skipped on a machine without617    multiple GPUs.618 619    To run *only* the multi_gpu tests, assuming all test names contain multi_gpu: $ pytest -sv ./tests -k "multi_gpu"620    """621    if not is_torch_available():622        return unittest.skip("test requires PyTorch")(test_case)623 624    import torch625 626    return unittest.skipUnless(torch.cuda.device_count() > 1, "test requires multiple GPUs")(test_case)627 628 629def require_torch_non_multi_gpu(test_case):630    """631    Decorator marking a test that requires 0 or 1 GPU setup (in PyTorch).632    """633    if not is_torch_available():634        return unittest.skip("test requires PyTorch")(test_case)635 636    import torch637 638    return unittest.skipUnless(torch.cuda.device_count() < 2, "test requires 0 or 1 GPU")(test_case)639 640 641def require_torch_up_to_2_gpus(test_case):642    """643    Decorator marking a test that requires 0 or 1 or 2 GPU setup (in PyTorch).644    """645    if not is_torch_available():646        return unittest.skip("test requires PyTorch")(test_case)647 648    import torch649 650    return unittest.skipUnless(torch.cuda.device_count() < 3, "test requires 0 or 1 or 2 GPUs")(test_case)651 652 653def require_torch_tpu(test_case):654    """655    Decorator marking a test that requires a TPU (in PyTorch).656    """657    return unittest.skipUnless(is_torch_tpu_available(check_device=False), "test requires PyTorch TPU")(test_case)658 659 660def require_torch_neuroncore(test_case):661    """662    Decorator marking a test that requires NeuronCore (in PyTorch).663    """664    return unittest.skipUnless(is_torch_neuroncore_available(check_device=False), "test requires PyTorch NeuronCore")(665        test_case666    )667 668 669def require_torch_npu(test_case):670    """671    Decorator marking a test that requires NPU (in PyTorch).672    """673    return unittest.skipUnless(is_torch_npu_available(), "test requires PyTorch NPU")(test_case)674 675 676def require_torch_multi_npu(test_case):677    """678    Decorator marking a test that requires a multi-NPU setup (in PyTorch). These tests are skipped on a machine without679    multiple NPUs.680 681    To run *only* the multi_npu tests, assuming all test names contain multi_npu: $ pytest -sv ./tests -k "multi_npu"682    """683    if not is_torch_npu_available():684        return unittest.skip("test requires PyTorch NPU")(test_case)685 686    return unittest.skipUnless(torch.npu.device_count() > 1, "test requires multiple NPUs")(test_case)687 688 689def require_torch_xpu(test_case):690    """691    Decorator marking a test that requires XPU and IPEX.692 693    These tests are skipped when Intel Extension for PyTorch isn't installed or it does not match current PyTorch694    version.695    """696    return unittest.skipUnless(is_torch_xpu_available(), "test requires IPEX and an XPU device")(test_case)697 698 699def require_torch_multi_xpu(test_case):700    """701    Decorator marking a test that requires a multi-XPU setup with IPEX and atleast one XPU device. These tests are702    skipped on a machine without IPEX or multiple XPUs.703 704    To run *only* the multi_xpu tests, assuming all test names contain multi_xpu: $ pytest -sv ./tests -k "multi_xpu"705    """706    if not is_torch_xpu_available():707        return unittest.skip("test requires IPEX and atleast one XPU device")(test_case)708 709    return unittest.skipUnless(torch.xpu.device_count() > 1, "test requires multiple XPUs")(test_case)710 711 712if is_torch_available():713    # Set env var CUDA_VISIBLE_DEVICES="" to force cpu-mode714    import torch715 716    if "TRANSFORMERS_TEST_DEVICE" in os.environ:717        torch_device = os.environ["TRANSFORMERS_TEST_DEVICE"]718        try:719            # try creating device to see if provided device is valid720            _ = torch.device(torch_device)721        except RuntimeError as e:722            raise RuntimeError(723                f"Unknown testing device specified by environment variable `TRANSFORMERS_TEST_DEVICE`: {torch_device}"724            ) from e725    elif torch.cuda.is_available():726        torch_device = "cuda"727    elif _run_third_party_device_tests and is_torch_npu_available():728        torch_device = "npu"729    elif _run_third_party_device_tests and is_torch_xpu_available():730        torch_device = "xpu"731    else:732        torch_device = "cpu"733 734    if "TRANSFORMERS_TEST_BACKEND" in os.environ:735        backend = os.environ["TRANSFORMERS_TEST_BACKEND"]736        try:737            _ = importlib.import_module(backend)738        except ModuleNotFoundError as e:739            raise ModuleNotFoundError(740                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"741                f" traceback):\n{e}"742            ) from e743 744else:745    torch_device = None746 747if is_tf_available():748    import tensorflow as tf749 750if is_flax_available():751    import jax752 753    jax_device = jax.default_backend()754else:755    jax_device = None756 757 758def require_torchdynamo(test_case):759    """Decorator marking a test that requires TorchDynamo"""760    return unittest.skipUnless(is_torchdynamo_available(), "test requires TorchDynamo")(test_case)761 762 763def require_torch_tensorrt_fx(test_case):764    """Decorator marking a test that requires Torch-TensorRT FX"""765    return unittest.skipUnless(is_torch_tensorrt_fx_available(), "test requires Torch-TensorRT FX")(test_case)766 767 768def require_torch_gpu(test_case):769    """Decorator marking a test that requires CUDA and PyTorch."""770    return unittest.skipUnless(torch_device == "cuda", "test requires CUDA")(test_case)771 772 773def require_torch_bf16_gpu(test_case):774    """Decorator marking a test that requires torch>=1.10, using Ampere GPU or newer arch with cuda>=11.0"""775    return unittest.skipUnless(776        is_torch_bf16_gpu_available(),777        "test requires torch>=1.10, using Ampere GPU or newer arch with cuda>=11.0",778    )(test_case)779 780 781def require_torch_bf16_cpu(test_case):782    """Decorator marking a test that requires torch>=1.10, using CPU."""783    return unittest.skipUnless(784        is_torch_bf16_cpu_available(),785        "test requires torch>=1.10, using CPU",786    )(test_case)787 788 789def require_torch_tf32(test_case):790    """Decorator marking a test that requires Ampere or a newer GPU arch, cuda>=11 and torch>=1.7."""791    return unittest.skipUnless(792        is_torch_tf32_available(), "test requires Ampere or a newer GPU arch, cuda>=11 and torch>=1.7"793    )(test_case)794 795 796def require_detectron2(test_case):797    """Decorator marking a test that requires detectron2."""798    return unittest.skipUnless(is_detectron2_available(), "test requires `detectron2`")(test_case)799 800 801def require_faiss(test_case):802    """Decorator marking a test that requires faiss."""803    return unittest.skipUnless(is_faiss_available(), "test requires `faiss`")(test_case)804 805 806def require_optuna(test_case):807    """808    Decorator marking a test that requires optuna.809 810    These tests are skipped when optuna isn't installed.811 812    """813    return unittest.skipUnless(is_optuna_available(), "test requires optuna")(test_case)814 815 816def require_ray(test_case):817    """818    Decorator marking a test that requires Ray/tune.819 820    These tests are skipped when Ray/tune isn't installed.821 822    """823    return unittest.skipUnless(is_ray_available(), "test requires Ray/tune")(test_case)824 825 826def require_sigopt(test_case):827    """828    Decorator marking a test that requires SigOpt.829 830    These tests are skipped when SigOpt isn't installed.831 832    """833    return unittest.skipUnless(is_sigopt_available(), "test requires SigOpt")(test_case)834 835 836def require_wandb(test_case):837    """838    Decorator marking a test that requires wandb.839 840    These tests are skipped when wandb isn't installed.841 842    """843    return unittest.skipUnless(is_wandb_available(), "test requires wandb")(test_case)844 845 846def require_clearml(test_case):847    """848    Decorator marking a test requires clearml.849 850    These tests are skipped when clearml isn't installed.851 852    """853    return unittest.skipUnless(is_clearml_available(), "test requires clearml")(test_case)854 855 856def require_soundfile(test_case):857    """858    Decorator marking a test that requires soundfile859 860    These tests are skipped when soundfile isn't installed.861 862    """863    return unittest.skipUnless(is_soundfile_availble(), "test requires soundfile")(test_case)864 865 866def require_deepspeed(test_case):867    """868    Decorator marking a test that requires deepspeed869    """870    return unittest.skipUnless(is_deepspeed_available(), "test requires deepspeed")(test_case)871 872 873def require_apex(test_case):874    """875    Decorator marking a test that requires apex876    """877    return unittest.skipUnless(is_apex_available(), "test requires apex")(test_case)878 879 880def require_bitsandbytes(test_case):881    """882    Decorator for bits and bytes (bnb) dependency883    """884    return unittest.skipUnless(is_bitsandbytes_available(), "test requires bnb")(test_case)885 886 887def require_optimum(test_case):888    """889    Decorator for optimum dependency890    """891    return unittest.skipUnless(is_optimum_available(), "test requires optimum")(test_case)892 893 894def require_auto_gptq(test_case):895    """896    Decorator for auto_gptq dependency897    """898    return unittest.skipUnless(is_auto_gptq_available(), "test requires auto-gptq")(test_case)899 900 901def require_phonemizer(test_case):902    """903    Decorator marking a test that requires phonemizer904    """905    return unittest.skipUnless(is_phonemizer_available(), "test requires phonemizer")(test_case)906 907 908def require_pyctcdecode(test_case):909    """910    Decorator marking a test that requires pyctcdecode911    """912    return unittest.skipUnless(is_pyctcdecode_available(), "test requires pyctcdecode")(test_case)913 914 915def require_librosa(test_case):916    """917    Decorator marking a test that requires librosa918    """919    return unittest.skipUnless(is_librosa_available(), "test requires librosa")(test_case)920 921 922def require_essentia(test_case):923    """924    Decorator marking a test that requires essentia925    """926    return unittest.skipUnless(is_essentia_available(), "test requires essentia")(test_case)927 928 929def require_pretty_midi(test_case):930    """931    Decorator marking a test that requires pretty_midi932    """933    return unittest.skipUnless(is_pretty_midi_available(), "test requires pretty_midi")(test_case)934 935 936def cmd_exists(cmd):937    return shutil.which(cmd) is not None938 939 940def require_usr_bin_time(test_case):941    """942    Decorator marking a test that requires `/usr/bin/time`943    """944    return unittest.skipUnless(cmd_exists("/usr/bin/time"), "test requires /usr/bin/time")(test_case)945 946 947def require_sudachi(test_case):948    """949    Decorator marking a test that requires sudachi950    """951    return unittest.skipUnless(is_sudachi_available(), "test requires sudachi")(test_case)952 953 954def require_jumanpp(test_case):955    """956    Decorator marking a test that requires jumanpp957    """958    return unittest.skipUnless(is_jumanpp_available(), "test requires jumanpp")(test_case)959 960 961def require_cython(test_case):962    """963    Decorator marking a test that requires jumanpp964    """965    return unittest.skipUnless(is_cython_available(), "test requires cython")(test_case)966 967 968def get_gpu_count():969    """970    Return the number of available gpus (regardless of whether torch, tf or jax is used)971    """972    if is_torch_available():973        import torch974 975        return torch.cuda.device_count()976    elif is_tf_available():977        import tensorflow as tf978 979        return len(tf.config.list_physical_devices("GPU"))980    elif is_flax_available():981        import jax982 983        return jax.device_count()984    else:985        return 0986 987 988def get_tests_dir(append_path=None):989    """990    Args:991        append_path: optional path to append to the tests dir path992 993    Return:994        The full path to the `tests` dir, so that the tests can be invoked from anywhere. Optionally `append_path` is995        joined after the `tests` dir the former is provided.996 997    """998    # this function caller's __file__999    caller__file__ = inspect.stack()[1][1]1000    tests_dir = os.path.abspath(os.path.dirname(caller__file__))1001 1002    while not tests_dir.endswith("tests"):1003        tests_dir = os.path.dirname(tests_dir)1004 1005    if append_path:1006        return os.path.join(tests_dir, append_path)1007    else:1008        return tests_dir1009 1010 1011#1012# Helper functions for dealing with testing text outputs1013# The original code came from:1014# https://github.com/fastai/fastai/blob/master/tests/utils/text.py1015 1016 1017# When any function contains print() calls that get overwritten, like progress bars,1018# a special care needs to be applied, since under pytest -s captured output (capsys1019# or contextlib.redirect_stdout) contains any temporary printed strings, followed by1020# \r's. This helper function ensures that the buffer will contain the same output1021# with and without -s in pytest, by turning:1022# foo bar\r tar mar\r final message1023# into:1024# final message1025# it can handle a single string or a multiline buffer1026def apply_print_resets(buf):1027    return re.sub(r"^.*\r", "", buf, 0, re.M)1028 1029 1030def assert_screenout(out, what):1031    out_pr = apply_print_resets(out).lower()1032    match_str = out_pr.find(what.lower())1033    assert match_str != -1, f"expecting to find {what} in output: f{out_pr}"1034 1035 1036class CaptureStd:1037    """1038    Context manager to capture:1039 1040        - stdout: replay it, clean it up and make it available via `obj.out`1041        - stderr: replay it and make it available via `obj.err`1042 1043    Args:1044        out (`bool`, *optional*, defaults to `True`): Whether to capture stdout or not.1045        err (`bool`, *optional*, defaults to `True`): Whether to capture stderr or not.1046        replay (`bool`, *optional*, defaults to `True`): Whether to replay or not.1047            By default each captured stream gets replayed back on context's exit, so that one can see what the test was1048            doing. If this is a not wanted behavior and the captured data shouldn't be replayed, pass `replay=False` to1049            disable this feature.1050 1051    Examples:1052 1053    ```python1054    # to capture stdout only with auto-replay1055    with CaptureStdout() as cs:1056        print("Secret message")1057    assert "message" in cs.out1058 1059    # to capture stderr only with auto-replay1060    import sys1061 1062    with CaptureStderr() as cs:1063        print("Warning: ", file=sys.stderr)1064    assert "Warning" in cs.err1065 1066    # to capture both streams with auto-replay1067    with CaptureStd() as cs:1068        print("Secret message")1069        print("Warning: ", file=sys.stderr)1070    assert "message" in cs.out1071    assert "Warning" in cs.err1072 1073    # to capture just one of the streams, and not the other, with auto-replay1074    with CaptureStd(err=False) as cs:1075        print("Secret message")1076    assert "message" in cs.out1077    # but best use the stream-specific subclasses1078 1079    # to capture without auto-replay1080    with CaptureStd(replay=False) as cs:1081        print("Secret message")1082    assert "message" in cs.out1083    ```"""1084 1085    def __init__(self, out=True, err=True, replay=True):1086        self.replay = replay1087 1088        if out:1089            self.out_buf = StringIO()1090            self.out = "error: CaptureStd context is unfinished yet, called too early"1091        else:1092            self.out_buf = None1093            self.out = "not capturing stdout"1094 1095        if err:1096            self.err_buf = StringIO()1097            self.err = "error: CaptureStd context is unfinished yet, called too early"1098        else:1099            self.err_buf = None1100            self.err = "not capturing stderr"1101 1102    def __enter__(self):1103        if self.out_buf:1104            self.out_old = sys.stdout1105            sys.stdout = self.out_buf1106 1107        if self.err_buf:1108            self.err_old = sys.stderr1109            sys.stderr = self.err_buf1110 1111        return self1112 1113    def __exit__(self, *exc):1114        if self.out_buf:1115            sys.stdout = self.out_old1116            captured = self.out_buf.getvalue()1117            if self.replay:1118                sys.stdout.write(captured)1119            self.out = apply_print_resets(captured)1120 1121        if self.err_buf:1122            sys.stderr = self.err_old1123            captured = self.err_buf.getvalue()1124            if self.replay:1125                sys.stderr.write(captured)1126            self.err = captured1127 1128    def __repr__(self):1129        msg = ""1130        if self.out_buf:1131            msg += f"stdout: {self.out}\n"1132        if self.err_buf:1133            msg += f"stderr: {self.err}\n"1134        return msg1135 1136 1137# in tests it's the best to capture only the stream that's wanted, otherwise1138# it's easy to miss things, so unless you need to capture both streams, use the1139# subclasses below (less typing). Or alternatively, configure `CaptureStd` to1140# disable the stream you don't need to test.1141 1142 1143class CaptureStdout(CaptureStd):1144    """Same as CaptureStd but captures only stdout"""1145 1146    def __init__(self, replay=True):1147        super().__init__(err=False, replay=replay)1148 1149 1150class CaptureStderr(CaptureStd):1151    """Same as CaptureStd but captures only stderr"""1152 1153    def __init__(self, replay=True):1154        super().__init__(out=False, replay=replay)1155 1156 1157class CaptureLogger:1158    """1159    Context manager to capture `logging` streams1160 1161    Args:1162        logger: 'logging` logger object1163 1164    Returns:1165        The captured output is available via `self.out`1166 1167    Example:1168 1169    ```python1170    >>> from transformers import logging1171    >>> from transformers.testing_utils import CaptureLogger1172 1173    >>> msg = "Testing 1, 2, 3"1174    >>> logging.set_verbosity_info()1175    >>> logger = logging.get_logger("transformers.models.bart.tokenization_bart")1176    >>> with CaptureLogger(logger) as cl:1177    ...     logger.info(msg)1178    >>> assert cl.out, msg + "\n"1179    ```1180    """1181 1182    def __init__(self, logger):1183        self.logger = logger1184        self.io = StringIO()1185        self.sh = logging.StreamHandler(self.io)1186        self.out = ""1187 1188    def __enter__(self):1189        self.logger.addHandler(self.sh)1190        return self1191 1192    def __exit__(self, *exc):1193        self.logger.removeHandler(self.sh)1194        self.out = self.io.getvalue()1195 1196    def __repr__(self):1197        return f"captured: {self.out}\n"1198 1199 1200@contextlib.contextmanager

Showing the first 1,200 of 2179 lines. Download the file for the rest.