CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
import_utils.py2900 linesDownload Raw Back to utils
1# Copyright 2022 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 utilities: Utilities related to imports and our lazy inits.16"""17 18import importlib.machinery19import importlib.metadata20import importlib.util21import json22import operator23import os24import re25import shutil26import subprocess27import sys28import warnings29from collections import OrderedDict30from enum import Enum31from functools import lru_cache32from itertools import chain33from types import ModuleType34from typing import Any, Callable, Optional, Union35 36from packaging import version37 38from . import logging39 40 41logger = logging.get_logger(__name__)  # pylint: disable=invalid-name42 43 44# TODO: This doesn't work for all packages (`bs4`, `faiss`, etc.) Talk to Sylvain to see how to do with it better.45def _is_package_available(pkg_name: str, return_version: bool = False) -> Union[tuple[bool, str], bool]:46    # Check if the package spec exists and grab its version to avoid importing a local directory47    package_exists = importlib.util.find_spec(pkg_name) is not None48    package_version = "N/A"49    if package_exists:50        try:51            # TODO: Once python 3.9 support is dropped, `importlib.metadata.packages_distributions()`52            # should be used here to map from package name to distribution names53            # e.g. PIL -> Pillow, Pillow-SIMD; quark -> amd-quark; onnxruntime -> onnxruntime-gpu.54            # `importlib.metadata.packages_distributions()` is not available in Python 3.9.55 56            # Primary method to get the package version57            package_version = importlib.metadata.version(pkg_name)58        except importlib.metadata.PackageNotFoundError:59            # Fallback method: Only for "torch" and versions containing "dev"60            if pkg_name == "torch":61                try:62                    package = importlib.import_module(pkg_name)63                    temp_version = getattr(package, "__version__", "N/A")64                    # Check if the version contains "dev"65                    if "dev" in temp_version:66                        package_version = temp_version67                        package_exists = True68                    else:69                        package_exists = False70                except ImportError:71                    # If the package can't be imported, it's not available72                    package_exists = False73            elif pkg_name == "quark":74                # TODO: remove once `importlib.metadata.packages_distributions()` is supported.75                try:76                    package_version = importlib.metadata.version("amd-quark")77                except Exception:78                    package_exists = False79            elif pkg_name == "triton":80                try:81                    # import triton works for both linux and windows82                    package = importlib.import_module(pkg_name)83                    package_version = getattr(package, "__version__", "N/A")84                except Exception:85                    try:86                        package_version = importlib.metadata.version("pytorch-triton")  # pytorch-triton87                    except Exception:88                        package_exists = False89            else:90                # For packages other than "torch", don't attempt the fallback and set as not available91                package_exists = False92        logger.debug(f"Detected {pkg_name} version: {package_version}")93    if return_version:94        return package_exists, package_version95    else:96        return package_exists97 98 99ENV_VARS_TRUE_VALUES = {"1", "ON", "YES", "TRUE"}100ENV_VARS_TRUE_AND_AUTO_VALUES = ENV_VARS_TRUE_VALUES.union({"AUTO"})101 102USE_TF = os.environ.get("USE_TF", "AUTO").upper()103USE_TORCH = os.environ.get("USE_TORCH", "AUTO").upper()104USE_JAX = os.environ.get("USE_FLAX", "AUTO").upper()105 106# Try to run a native pytorch job in an environment with TorchXLA installed by setting this value to 0.107USE_TORCH_XLA = os.environ.get("USE_TORCH_XLA", "1").upper()108 109FORCE_TF_AVAILABLE = os.environ.get("FORCE_TF_AVAILABLE", "AUTO").upper()110 111# `transformers` requires `torch>=1.11` but this variable is exposed publicly, and we can't simply remove it.112# This is the version of torch required to run torch.fx features and torch.onnx with dictionary inputs.113TORCH_FX_REQUIRED_VERSION = version.parse("1.10")114 115ACCELERATE_MIN_VERSION = "0.26.0"116SCHEDULEFREE_MIN_VERSION = "1.2.6"117FSDP_MIN_VERSION = "1.12.0"118GGUF_MIN_VERSION = "0.10.0"119XLA_FSDPV2_MIN_VERSION = "2.2.0"120HQQ_MIN_VERSION = "0.2.1"121VPTQ_MIN_VERSION = "0.0.4"122TORCHAO_MIN_VERSION = "0.4.0"123AUTOROUND_MIN_VERSION = "0.5.0"124TRITON_MIN_VERSION = "1.0.0"125 126_accelerate_available, _accelerate_version = _is_package_available("accelerate", return_version=True)127_apex_available = _is_package_available("apex")128_apollo_torch_available = _is_package_available("apollo_torch")129_aqlm_available = _is_package_available("aqlm")130_vptq_available, _vptq_version = _is_package_available("vptq", return_version=True)131_av_available = importlib.util.find_spec("av") is not None132_decord_available = importlib.util.find_spec("decord") is not None133_torchcodec_available = importlib.util.find_spec("torchcodec") is not None134_libcst_available = _is_package_available("libcst")135_bitsandbytes_available = _is_package_available("bitsandbytes")136_eetq_available = _is_package_available("eetq")137_fbgemm_gpu_available = _is_package_available("fbgemm_gpu")138_galore_torch_available = _is_package_available("galore_torch")139_lomo_available = _is_package_available("lomo_optim")140_grokadamw_available = _is_package_available("grokadamw")141_schedulefree_available, _schedulefree_version = _is_package_available("schedulefree", return_version=True)142_torch_optimi_available = importlib.util.find_spec("optimi") is not None143# `importlib.metadata.version` doesn't work with `bs4` but `beautifulsoup4`. For `importlib.util.find_spec`, reversed.144_bs4_available = importlib.util.find_spec("bs4") is not None145_coloredlogs_available = _is_package_available("coloredlogs")146# `importlib.metadata.util` doesn't work with `opencv-python-headless`.147_cv2_available = importlib.util.find_spec("cv2") is not None148_yt_dlp_available = importlib.util.find_spec("yt_dlp") is not None149_datasets_available = _is_package_available("datasets")150_detectron2_available = _is_package_available("detectron2")151# We need to check `faiss`, `faiss-cpu` and `faiss-gpu`.152_faiss_available = importlib.util.find_spec("faiss") is not None153try:154    _faiss_version = importlib.metadata.version("faiss")155    logger.debug(f"Successfully imported faiss version {_faiss_version}")156except importlib.metadata.PackageNotFoundError:157    try:158        _faiss_version = importlib.metadata.version("faiss-cpu")159        logger.debug(f"Successfully imported faiss version {_faiss_version}")160    except importlib.metadata.PackageNotFoundError:161        try:162            _faiss_version = importlib.metadata.version("faiss-gpu")163            logger.debug(f"Successfully imported faiss version {_faiss_version}")164        except importlib.metadata.PackageNotFoundError:165            _faiss_available = False166_ftfy_available = _is_package_available("ftfy")167_g2p_en_available = _is_package_available("g2p_en")168_hadamard_available = _is_package_available("fast_hadamard_transform")169_ipex_available, _ipex_version = _is_package_available("intel_extension_for_pytorch", return_version=True)170_jinja_available = _is_package_available("jinja2")171_kenlm_available = _is_package_available("kenlm")172_keras_nlp_available = _is_package_available("keras_nlp")173_levenshtein_available = _is_package_available("Levenshtein")174_librosa_available = _is_package_available("librosa")175_natten_available = _is_package_available("natten")176_nltk_available = _is_package_available("nltk")177_onnx_available = _is_package_available("onnx")178_openai_available = _is_package_available("openai")179_optimum_available = _is_package_available("optimum")180_auto_gptq_available = _is_package_available("auto_gptq")181_gptqmodel_available = _is_package_available("gptqmodel")182_auto_round_available, _auto_round_version = _is_package_available("auto_round", return_version=True)183# `importlib.metadata.version` doesn't work with `awq`184_auto_awq_available = importlib.util.find_spec("awq") is not None185_quark_available = _is_package_available("quark")186_fp_quant_available, _fp_quant_version = _is_package_available("fp_quant", return_version=True)187_qutlass_available, _qutlass_version = _is_package_available("qutlass", return_version=True)188_is_optimum_quanto_available = False189try:190    importlib.metadata.version("optimum_quanto")191    _is_optimum_quanto_available = True192except importlib.metadata.PackageNotFoundError:193    _is_optimum_quanto_available = False194# For compressed_tensors, only check spec to allow compressed_tensors-nightly package195_compressed_tensors_available = importlib.util.find_spec("compressed_tensors") is not None196_pandas_available = _is_package_available("pandas")197_peft_available = _is_package_available("peft")198_phonemizer_available = _is_package_available("phonemizer")199_uroman_available = _is_package_available("uroman")200_psutil_available = _is_package_available("psutil")201_py3nvml_available = _is_package_available("py3nvml")202_pyctcdecode_available = _is_package_available("pyctcdecode")203_pygments_available = _is_package_available("pygments")204_pytesseract_available = _is_package_available("pytesseract")205_pytest_available = _is_package_available("pytest")206_pytorch_quantization_available = _is_package_available("pytorch_quantization")207_rjieba_available = _is_package_available("rjieba")208_sacremoses_available = _is_package_available("sacremoses")209_safetensors_available = _is_package_available("safetensors")210_scipy_available = _is_package_available("scipy")211_sentencepiece_available = _is_package_available("sentencepiece")212_is_seqio_available = _is_package_available("seqio")213_is_gguf_available, _gguf_version = _is_package_available("gguf", return_version=True)214_sklearn_available = importlib.util.find_spec("sklearn") is not None215if _sklearn_available:216    try:217        importlib.metadata.version("scikit-learn")218    except importlib.metadata.PackageNotFoundError:219        _sklearn_available = False220_smdistributed_available = importlib.util.find_spec("smdistributed") is not None221_soundfile_available = _is_package_available("soundfile")222_spacy_available = _is_package_available("spacy")223_sudachipy_available, _sudachipy_version = _is_package_available("sudachipy", return_version=True)224_tensorflow_probability_available = _is_package_available("tensorflow_probability")225_tensorflow_text_available = _is_package_available("tensorflow_text")226_tf2onnx_available = _is_package_available("tf2onnx")227_timm_available = _is_package_available("timm")228_tokenizers_available = _is_package_available("tokenizers")229_torchaudio_available = _is_package_available("torchaudio")230_torchao_available, _torchao_version = _is_package_available("torchao", return_version=True)231_torchdistx_available = _is_package_available("torchdistx")232_torchvision_available, _torchvision_version = _is_package_available("torchvision", return_version=True)233_mlx_available = _is_package_available("mlx")234_num2words_available = _is_package_available("num2words")235_hqq_available, _hqq_version = _is_package_available("hqq", return_version=True)236_tiktoken_available = _is_package_available("tiktoken")237_blobfile_available = _is_package_available("blobfile")238_liger_kernel_available = _is_package_available("liger_kernel")239_spqr_available = _is_package_available("spqr_quant")240_rich_available = _is_package_available("rich")241_kernels_available = _is_package_available("kernels")242_matplotlib_available = _is_package_available("matplotlib")243_mistral_common_available = _is_package_available("mistral_common")244_triton_available, _triton_version = _is_package_available("triton", return_version=True)245 246_torch_version = "N/A"247_torch_available = False248if USE_TORCH in ENV_VARS_TRUE_AND_AUTO_VALUES and USE_TF not in ENV_VARS_TRUE_VALUES:249    _torch_available, _torch_version = _is_package_available("torch", return_version=True)250    if _torch_available:251        _torch_available = version.parse(_torch_version) >= version.parse("2.1.0")252        if not _torch_available:253            logger.warning(f"Disabling PyTorch because PyTorch >= 2.1 is required but found {_torch_version}")254else:255    logger.info("Disabling PyTorch because USE_TF is set")256    _torch_available = False257 258 259_tf_version = "N/A"260_tf_available = False261if FORCE_TF_AVAILABLE in ENV_VARS_TRUE_VALUES:262    _tf_available = True263else:264    if USE_TF in ENV_VARS_TRUE_AND_AUTO_VALUES and USE_TORCH not in ENV_VARS_TRUE_VALUES:265        # Note: _is_package_available("tensorflow") fails for tensorflow-cpu. Please test any changes to the line below266        # with tensorflow-cpu to make sure it still works!267        _tf_available = importlib.util.find_spec("tensorflow") is not None268        if _tf_available:269            candidates = (270                "tensorflow",271                "tensorflow-cpu",272                "tensorflow-gpu",273                "tf-nightly",274                "tf-nightly-cpu",275                "tf-nightly-gpu",276                "tf-nightly-rocm",277                "intel-tensorflow",278                "intel-tensorflow-avx512",279                "tensorflow-rocm",280                "tensorflow-macos",281                "tensorflow-aarch64",282            )283            _tf_version = None284            # For the metadata, we have to look for both tensorflow and tensorflow-cpu285            for pkg in candidates:286                try:287                    _tf_version = importlib.metadata.version(pkg)288                    break289                except importlib.metadata.PackageNotFoundError:290                    pass291            _tf_available = _tf_version is not None292        if _tf_available:293            if version.parse(_tf_version) < version.parse("2"):294                logger.info(295                    f"TensorFlow found but with version {_tf_version}. Transformers requires version 2 minimum."296                )297                _tf_available = False298    else:299        logger.info("Disabling Tensorflow because USE_TORCH is set")300 301 302_essentia_available = importlib.util.find_spec("essentia") is not None303try:304    _essentia_version = importlib.metadata.version("essentia")305    logger.debug(f"Successfully imported essentia version {_essentia_version}")306except importlib.metadata.PackageNotFoundError:307    _essentia_version = False308 309 310_pydantic_available = importlib.util.find_spec("pydantic") is not None311try:312    _pydantic_version = importlib.metadata.version("pydantic")313    logger.debug(f"Successfully imported pydantic version {_pydantic_version}")314except importlib.metadata.PackageNotFoundError:315    _pydantic_available = False316 317 318_fastapi_available = importlib.util.find_spec("fastapi") is not None319try:320    _fastapi_version = importlib.metadata.version("fastapi")321    logger.debug(f"Successfully imported pydantic version {_fastapi_version}")322except importlib.metadata.PackageNotFoundError:323    _fastapi_available = False324 325 326_uvicorn_available = importlib.util.find_spec("uvicorn") is not None327try:328    _uvicorn_version = importlib.metadata.version("uvicorn")329    logger.debug(f"Successfully imported pydantic version {_uvicorn_version}")330except importlib.metadata.PackageNotFoundError:331    _uvicorn_available = False332 333 334_pretty_midi_available = importlib.util.find_spec("pretty_midi") is not None335try:336    _pretty_midi_version = importlib.metadata.version("pretty_midi")337    logger.debug(f"Successfully imported pretty_midi version {_pretty_midi_version}")338except importlib.metadata.PackageNotFoundError:339    _pretty_midi_available = False340 341 342ccl_version = "N/A"343_is_ccl_available = (344    importlib.util.find_spec("torch_ccl") is not None345    or importlib.util.find_spec("oneccl_bindings_for_pytorch") is not None346)347try:348    ccl_version = importlib.metadata.version("oneccl_bind_pt")349    logger.debug(f"Detected oneccl_bind_pt version {ccl_version}")350except importlib.metadata.PackageNotFoundError:351    _is_ccl_available = False352 353 354_flax_available = False355if USE_JAX in ENV_VARS_TRUE_AND_AUTO_VALUES:356    _flax_available, _flax_version = _is_package_available("flax", return_version=True)357    if _flax_available:358        _jax_available, _jax_version = _is_package_available("jax", return_version=True)359        if _jax_available:360            logger.info(f"JAX version {_jax_version}, Flax version {_flax_version} available.")361        else:362            _flax_available = _jax_available = False363            _jax_version = _flax_version = "N/A"364 365 366_torch_xla_available = False367if USE_TORCH_XLA in ENV_VARS_TRUE_VALUES:368    _torch_xla_available, _torch_xla_version = _is_package_available("torch_xla", return_version=True)369    if _torch_xla_available:370        logger.info(f"Torch XLA version {_torch_xla_version} available.")371 372 373def is_kenlm_available() -> Union[tuple[bool, str], bool]:374    return _kenlm_available375 376 377def is_kernels_available() -> Union[tuple[bool, str], bool]:378    return _kernels_available379 380 381def is_cv2_available() -> Union[tuple[bool, str], bool]:382    return _cv2_available383 384 385def is_yt_dlp_available() -> Union[tuple[bool, str], bool]:386    return _yt_dlp_available387 388 389def is_torch_available() -> Union[tuple[bool, str], bool]:390    return _torch_available391 392 393def is_libcst_available() -> Union[tuple[bool, str], bool]:394    return _libcst_available395 396 397def is_accelerate_available(min_version: str = ACCELERATE_MIN_VERSION) -> bool:398    return _accelerate_available and version.parse(_accelerate_version) >= version.parse(min_version)399 400 401def is_torch_accelerator_available() -> bool:402    if is_torch_available():403        import torch404 405        return hasattr(torch, "accelerator")406 407    return False408 409 410def is_torch_deterministic() -> bool:411    """412    Check whether pytorch uses deterministic algorithms by looking if torch.set_deterministic_debug_mode() is set to 1 or 2"413    """414    if is_torch_available():415        import torch416 417        if torch.get_deterministic_debug_mode() == 0:418            return False419        else:420            return True421 422    return False423 424 425def is_triton_available(min_version: str = TRITON_MIN_VERSION) -> bool:426    return _triton_available and version.parse(_triton_version) >= version.parse(min_version)427 428 429def is_hadamard_available() -> Union[tuple[bool, str], bool]:430    return _hadamard_available431 432 433def is_hqq_available(min_version: str = HQQ_MIN_VERSION) -> bool:434    return _hqq_available and version.parse(_hqq_version) >= version.parse(min_version)435 436 437def is_pygments_available() -> Union[tuple[bool, str], bool]:438    return _pygments_available439 440 441def get_torch_version() -> str:442    return _torch_version443 444 445def get_torch_major_and_minor_version() -> str:446    if _torch_version == "N/A":447        return "N/A"448    parsed_version = version.parse(_torch_version)449    return str(parsed_version.major) + "." + str(parsed_version.minor)450 451 452def is_torch_sdpa_available():453    # Mostly retained for backward compatibility in remote code, since sdpa works correctly on all torch versions >= 2.2454    if not is_torch_available() or _torch_version == "N/A":455        return False456    return True457 458 459def is_torch_flex_attn_available() -> bool:460    if not is_torch_available() or _torch_version == "N/A":461        return False462 463    # TODO check if some bugs cause push backs on the exact version464    # NOTE: We require torch>=2.5.0 as it is the first release465    return version.parse(_torch_version) >= version.parse("2.5.0")466 467 468def is_torchvision_available() -> bool:469    return _torchvision_available470 471 472def is_torchvision_v2_available() -> bool:473    return is_torchvision_available()474 475 476def is_galore_torch_available() -> Union[tuple[bool, str], bool]:477    return _galore_torch_available478 479 480def is_apollo_torch_available() -> Union[tuple[bool, str], bool]:481    return _apollo_torch_available482 483 484def is_torch_optimi_available() -> Union[tuple[bool, str], bool]:485    return _torch_optimi_available486 487 488def is_lomo_available() -> Union[tuple[bool, str], bool]:489    return _lomo_available490 491 492def is_grokadamw_available() -> Union[tuple[bool, str], bool]:493    return _grokadamw_available494 495 496def is_schedulefree_available(min_version: str = SCHEDULEFREE_MIN_VERSION) -> bool:497    return _schedulefree_available and version.parse(_schedulefree_version) >= version.parse(min_version)498 499 500def is_pyctcdecode_available() -> Union[tuple[bool, str], bool]:501    return _pyctcdecode_available502 503 504def is_librosa_available() -> Union[tuple[bool, str], bool]:505    return _librosa_available506 507 508def is_essentia_available() -> Union[tuple[bool, str], bool]:509    return _essentia_available510 511 512def is_pydantic_available() -> Union[tuple[bool, str], bool]:513    return _pydantic_available514 515 516def is_fastapi_available() -> Union[tuple[bool, str], bool]:517    return _fastapi_available518 519 520def is_uvicorn_available() -> Union[tuple[bool, str], bool]:521    return _uvicorn_available522 523 524def is_openai_available() -> Union[tuple[bool, str], bool]:525    return _openai_available526 527 528def is_pretty_midi_available() -> Union[tuple[bool, str], bool]:529    return _pretty_midi_available530 531 532def is_torch_cuda_available() -> bool:533    if is_torch_available():534        import torch535 536        return torch.cuda.is_available()537    else:538        return False539 540 541def is_cuda_platform() -> bool:542    if is_torch_available():543        import torch544 545        return torch.version.cuda is not None546    else:547        return False548 549 550def is_rocm_platform() -> bool:551    if is_torch_available():552        import torch553 554        return torch.version.hip is not None555    else:556        return False557 558 559def is_mamba_ssm_available() -> Union[tuple[bool, str], bool]:560    if is_torch_available():561        import torch562 563        if not torch.cuda.is_available():564            return False565        else:566            return _is_package_available("mamba_ssm")567    return False568 569 570def is_mamba_2_ssm_available() -> bool:571    if is_torch_available():572        import torch573 574        if not torch.cuda.is_available():575            return False576        else:577            if _is_package_available("mamba_ssm"):578                import mamba_ssm579 580                if version.parse(mamba_ssm.__version__) >= version.parse("2.0.4"):581                    return True582    return False583 584 585def is_flash_linear_attention_available():586    if is_torch_available():587        import torch588 589        if not torch.cuda.is_available():590            return False591 592        try:593            import fla594 595            if version.parse(fla.__version__) >= version.parse("0.2.2"):596                return True597        except Exception:598            pass599    return False600 601 602def is_causal_conv1d_available() -> Union[tuple[bool, str], bool]:603    if is_torch_available():604        import torch605 606        if not torch.cuda.is_available():607            return False608        return _is_package_available("causal_conv1d")609    return False610 611 612def is_xlstm_available() -> Union[tuple[bool, str], bool]:613    if is_torch_available():614        return _is_package_available("xlstm")615    return False616 617 618def is_mambapy_available() -> Union[tuple[bool, str], bool]:619    if is_torch_available():620        return _is_package_available("mambapy")621    return False622 623 624def is_torch_mps_available(min_version: Optional[str] = None) -> bool:625    if is_torch_available():626        import torch627 628        if hasattr(torch.backends, "mps"):629            backend_available = torch.backends.mps.is_available() and torch.backends.mps.is_built()630            if min_version is not None:631                flag = version.parse(_torch_version) >= version.parse(min_version)632                backend_available = backend_available and flag633            return backend_available634    return False635 636 637def is_torch_bf16_gpu_available() -> bool:638    if not is_torch_available():639        return False640 641    import torch642 643    if torch.cuda.is_available():644        return torch.cuda.is_bf16_supported()645    if is_torch_xpu_available():646        return torch.xpu.is_bf16_supported()647    if is_torch_hpu_available():648        return True649    if is_torch_npu_available():650        return torch.npu.is_bf16_supported()651    if is_torch_mps_available():652        # Note: Emulated in software by Metal using fp32 for hardware without native support (like M1/M2)653        return torch.backends.mps.is_macos_or_newer(14, 0)654    if is_torch_musa_available():655        return torch.musa.is_bf16_supported()656    return False657 658 659def is_torch_bf16_cpu_available() -> Union[tuple[bool, str], bool]:660    return is_torch_available()661 662 663def is_torch_bf16_available() -> bool:664    # the original bf16 check was for gpu only, but later a cpu/bf16 combo has emerged so this util665    # has become ambiguous and therefore deprecated666    warnings.warn(667        "The util is_torch_bf16_available is deprecated, please use is_torch_bf16_gpu_available "668        "or is_torch_bf16_cpu_available instead according to whether it's used with cpu or gpu",669        FutureWarning,670    )671    return is_torch_bf16_gpu_available()672 673 674@lru_cache675def is_torch_fp16_available_on_device(device: str) -> bool:676    if not is_torch_available():677        return False678 679    if is_torch_hpu_available():680        if is_habana_gaudi1():681            return False682        else:683            return True684 685    import torch686 687    try:688        x = torch.zeros(2, 2, dtype=torch.float16, device=device)689        _ = x @ x690 691        # At this moment, let's be strict of the check: check if `LayerNorm` is also supported on device, because many692        # models use this layer.693        batch, sentence_length, embedding_dim = 3, 4, 5694        embedding = torch.randn(batch, sentence_length, embedding_dim, dtype=torch.float16, device=device)695        layer_norm = torch.nn.LayerNorm(embedding_dim, dtype=torch.float16, device=device)696        _ = layer_norm(embedding)697 698    except:  # noqa: E722699        # TODO: more precise exception matching, if possible.700        # most backends should return `RuntimeError` however this is not guaranteed.701        return False702 703    return True704 705 706@lru_cache707def is_torch_bf16_available_on_device(device: str) -> bool:708    if not is_torch_available():709        return False710 711    import torch712 713    if device == "cuda":714        return is_torch_bf16_gpu_available()715 716    if device == "hpu":717        return True718 719    try:720        x = torch.zeros(2, 2, dtype=torch.bfloat16, device=device)721        _ = x @ x722    except:  # noqa: E722723        # TODO: more precise exception matching, if possible.724        # most backends should return `RuntimeError` however this is not guaranteed.725        return False726 727    return True728 729 730def is_torch_tf32_available() -> bool:731    if not is_torch_available():732        return False733 734    import torch735 736    if is_torch_musa_available():737        device_info = torch.musa.get_device_properties(torch.musa.current_device())738        if f"{device_info.major}{device_info.minor}" >= "22":739            return True740        return False741    if not torch.cuda.is_available() or torch.version.cuda is None:742        return False743    if torch.cuda.get_device_properties(torch.cuda.current_device()).major < 8:744        return False745    return True746 747 748def is_torch_fx_available() -> Union[tuple[bool, str], bool]:749    return is_torch_available()750 751 752def is_peft_available() -> Union[tuple[bool, str], bool]:753    return _peft_available754 755 756def is_bs4_available() -> Union[tuple[bool, str], bool]:757    return _bs4_available758 759 760def is_tf_available() -> bool:761    return _tf_available762 763 764def is_coloredlogs_available() -> Union[tuple[bool, str], bool]:765    return _coloredlogs_available766 767 768def is_tf2onnx_available() -> Union[tuple[bool, str], bool]:769    return _tf2onnx_available770 771 772def is_onnx_available() -> Union[tuple[bool, str], bool]:773    return _onnx_available774 775 776def is_flax_available() -> bool:777    return _flax_available778 779 780def is_flute_available() -> bool:781    try:782        return importlib.util.find_spec("flute") is not None and importlib.metadata.version("flute-kernel") >= "0.4.1"783    except importlib.metadata.PackageNotFoundError:784        return False785 786 787def is_ftfy_available() -> Union[tuple[bool, str], bool]:788    return _ftfy_available789 790 791def is_g2p_en_available() -> Union[tuple[bool, str], bool]:792    return _g2p_en_available793 794 795@lru_cache796def is_torch_xla_available(check_is_tpu=False, check_is_gpu=False) -> bool:797    """798    Check if `torch_xla` is available. To train a native pytorch job in an environment with torch xla installed, set799    the USE_TORCH_XLA to false.800    """801    assert not (check_is_tpu and check_is_gpu), "The check_is_tpu and check_is_gpu cannot both be true."802 803    if not _torch_xla_available:804        return False805 806    import torch_xla807 808    if check_is_gpu:809        return torch_xla.runtime.device_type() in ["GPU", "CUDA"]810    elif check_is_tpu:811        return torch_xla.runtime.device_type() == "TPU"812 813    return True814 815 816@lru_cache817def is_torch_neuroncore_available(check_device=True) -> bool:818    if importlib.util.find_spec("torch_neuronx") is not None:819        return is_torch_xla_available()820    return False821 822 823@lru_cache824def is_torch_npu_available(check_device=False) -> bool:825    "Checks if `torch_npu` is installed and potentially if a NPU is in the environment"826    if not _torch_available or importlib.util.find_spec("torch_npu") is None:827        return False828 829    import torch830    import torch_npu  # noqa: F401831 832    if check_device:833        try:834            # Will raise a RuntimeError if no NPU is found835            _ = torch.npu.device_count()836            return torch.npu.is_available()837        except RuntimeError:838            return False839    return hasattr(torch, "npu") and torch.npu.is_available()840 841 842@lru_cache843def is_torch_mlu_available() -> bool:844    """845    Checks if `mlu` is available via an `cndev-based` check which won't trigger the drivers and leave mlu846    uninitialized.847    """848    if not _torch_available or importlib.util.find_spec("torch_mlu") is None:849        return False850 851    import torch852    import torch_mlu  # noqa: F401853 854    pytorch_cndev_based_mlu_check_previous_value = os.environ.get("PYTORCH_CNDEV_BASED_MLU_CHECK")855    try:856        os.environ["PYTORCH_CNDEV_BASED_MLU_CHECK"] = str(1)857        available = torch.mlu.is_available()858    finally:859        if pytorch_cndev_based_mlu_check_previous_value:860            os.environ["PYTORCH_CNDEV_BASED_MLU_CHECK"] = pytorch_cndev_based_mlu_check_previous_value861        else:862            os.environ.pop("PYTORCH_CNDEV_BASED_MLU_CHECK", None)863 864    return available865 866 867@lru_cache868def is_torch_musa_available(check_device=False) -> bool:869    "Checks if `torch_musa` is installed and potentially if a MUSA is in the environment"870    if not _torch_available or importlib.util.find_spec("torch_musa") is None:871        return False872 873    import torch874    import torch_musa  # noqa: F401875 876    torch_musa_min_version = "0.33.0"877    if _accelerate_available and version.parse(_accelerate_version) < version.parse(torch_musa_min_version):878        return False879 880    if check_device:881        try:882            # Will raise a RuntimeError if no MUSA is found883            _ = torch.musa.device_count()884            return torch.musa.is_available()885        except RuntimeError:886            return False887    return hasattr(torch, "musa") and torch.musa.is_available()888 889 890@lru_cache891def is_torch_hpu_available() -> bool:892    "Checks if `torch.hpu` is available and potentially if a HPU is in the environment"893    if (894        not _torch_available895        or importlib.util.find_spec("habana_frameworks") is None896        or importlib.util.find_spec("habana_frameworks.torch") is None897    ):898        return False899 900    torch_hpu_min_accelerate_version = "1.5.0"901    if _accelerate_available and version.parse(_accelerate_version) < version.parse(torch_hpu_min_accelerate_version):902        return False903 904    import torch905 906    if os.environ.get("PT_HPU_LAZY_MODE", "1") == "1":907        # import habana_frameworks.torch in case of lazy mode to patch torch with torch.hpu908        import habana_frameworks.torch  # noqa: F401909 910    if not hasattr(torch, "hpu") or not torch.hpu.is_available():911        return False912 913    # We patch torch.gather for int64 tensors to avoid a bug on Gaudi914    # Graph compile failed with synStatus 26 [Generic failure]915    # This can be removed once bug is fixed but for now we need it.916    original_gather = torch.gather917 918    def patched_gather(input: torch.Tensor, dim: int, index: torch.LongTensor) -> torch.Tensor:919        if input.dtype == torch.int64 and input.device.type == "hpu":920            return original_gather(input.to(torch.int32), dim, index).to(torch.int64)921        else:922            return original_gather(input, dim, index)923 924    torch.gather = patched_gather925    torch.Tensor.gather = patched_gather926 927    original_take_along_dim = torch.take_along_dim928 929    def patched_take_along_dim(930        input: torch.Tensor, indices: torch.LongTensor, dim: Optional[int] = None931    ) -> torch.Tensor:932        if input.dtype == torch.int64 and input.device.type == "hpu":933            return original_take_along_dim(input.to(torch.int32), indices, dim).to(torch.int64)934        else:935            return original_take_along_dim(input, indices, dim)936 937    torch.take_along_dim = patched_take_along_dim938 939    original_cholesky = torch.linalg.cholesky940 941    def safe_cholesky(A, *args, **kwargs):942        output = original_cholesky(A, *args, **kwargs)943 944        if torch.isnan(output).any():945            jitter_value = 1e-9946            diag_jitter = torch.eye(A.size(-1), dtype=A.dtype, device=A.device) * jitter_value947            output = original_cholesky(A + diag_jitter, *args, **kwargs)948 949        return output950 951    torch.linalg.cholesky = safe_cholesky952 953    original_scatter = torch.scatter954 955    def patched_scatter(956        input: torch.Tensor, dim: int, index: torch.Tensor, src: torch.Tensor, *args, **kwargs957    ) -> torch.Tensor:958        if input.device.type == "hpu" and input is src:959            return original_scatter(input, dim, index, src.clone(), *args, **kwargs)960        else:961            return original_scatter(input, dim, index, src, *args, **kwargs)962 963    torch.scatter = patched_scatter964    torch.Tensor.scatter = patched_scatter965 966    # IlyasMoutawwakil: we patch torch.compile to use the HPU backend by default967    # https://github.com/huggingface/transformers/pull/38790#discussion_r2157043944968    # This is necessary for cases where torch.compile is used as a decorator (defaulting to inductor)969    # https://github.com/huggingface/transformers/blob/af6120b3eb2470b994c21421bb6eaa76576128b0/src/transformers/models/modernbert/modeling_modernbert.py#L204970    original_compile = torch.compile971 972    def hpu_backend_compile(*args, **kwargs):973        if kwargs.get("backend") not in ["hpu_backend", "eager"]:974            logger.warning(975                f"Calling torch.compile with backend={kwargs.get('backend')} on a Gaudi device is not supported. "976                "We will override the backend with 'hpu_backend' to avoid errors."977            )978            kwargs["backend"] = "hpu_backend"979 980        return original_compile(*args, **kwargs)981 982    torch.compile = hpu_backend_compile983 984    return True985 986 987@lru_cache988def is_habana_gaudi1() -> bool:989    if not is_torch_hpu_available():990        return False991 992    import habana_frameworks.torch.utils.experimental as htexp993 994    # Check if the device is Gaudi1 (vs Gaudi2, Gaudi3)995    return htexp._get_device_type() == htexp.synDeviceType.synDeviceGaudi996 997 998def is_torchdynamo_available() -> Union[tuple[bool, str], bool]:999    return is_torch_available()1000 1001 1002def is_torch_compile_available() -> Union[tuple[bool, str], bool]:1003    return is_torch_available()1004 1005 1006def is_torchdynamo_compiling() -> Union[tuple[bool, str], bool]:1007    if not is_torch_available():1008        return False1009 1010    # Importing torch._dynamo causes issues with PyTorch profiler (https://github.com/pytorch/pytorch/issues/130622)1011    # hence rather relying on `torch.compiler.is_compiling()` when possible (torch>=2.3)1012    try:1013        import torch1014 1015        return torch.compiler.is_compiling()1016    except Exception:1017        try:1018            import torch._dynamo as dynamo1019 1020            return dynamo.is_compiling()1021        except Exception:1022            return False1023 1024 1025def is_torchdynamo_exporting() -> bool:1026    if not is_torch_available():1027        return False1028 1029    try:1030        import torch1031 1032        return torch.compiler.is_exporting()1033    except Exception:1034        try:1035            import torch._dynamo as dynamo1036 1037            return dynamo.is_exporting()1038        except Exception:1039            return False1040 1041 1042def is_torch_tensorrt_fx_available() -> bool:1043    if importlib.util.find_spec("torch_tensorrt") is None:1044        return False1045    return importlib.util.find_spec("torch_tensorrt.fx") is not None1046 1047 1048def is_datasets_available() -> Union[tuple[bool, str], bool]:1049    return _datasets_available1050 1051 1052def is_detectron2_available() -> Union[tuple[bool, str], bool]:1053    return _detectron2_available1054 1055 1056def is_rjieba_available() -> Union[tuple[bool, str], bool]:1057    return _rjieba_available1058 1059 1060def is_psutil_available() -> Union[tuple[bool, str], bool]:1061    return _psutil_available1062 1063 1064def is_py3nvml_available() -> Union[tuple[bool, str], bool]:1065    return _py3nvml_available1066 1067 1068def is_sacremoses_available() -> Union[tuple[bool, str], bool]:1069    return _sacremoses_available1070 1071 1072def is_apex_available() -> Union[tuple[bool, str], bool]:1073    return _apex_available1074 1075 1076def is_aqlm_available() -> Union[tuple[bool, str], bool]:1077    return _aqlm_available1078 1079 1080def is_vptq_available(min_version: str = VPTQ_MIN_VERSION) -> bool:1081    return _vptq_available and version.parse(_vptq_version) >= version.parse(min_version)1082 1083 1084def is_av_available() -> bool:1085    return _av_available1086 1087 1088def is_decord_available() -> bool:1089    return _decord_available1090 1091 1092def is_torchcodec_available() -> bool:1093    return _torchcodec_available1094 1095 1096def is_ninja_available() -> bool:1097    r"""1098    Code comes from *torch.utils.cpp_extension.is_ninja_available()*. Returns `True` if the1099    [ninja](https://ninja-build.org/) build system is available on the system, `False` otherwise.1100    """1101    try:1102        subprocess.check_output(["ninja", "--version"])1103    except Exception:1104        return False1105    else:1106        return True1107 1108 1109def is_ipex_available(min_version: str = "") -> bool:1110    def get_major_and_minor_from_version(full_version):1111        return str(version.parse(full_version).major) + "." + str(version.parse(full_version).minor)1112 1113    if not is_torch_available() or not _ipex_available:1114        return False1115 1116    torch_major_and_minor = get_major_and_minor_from_version(_torch_version)1117    ipex_major_and_minor = get_major_and_minor_from_version(_ipex_version)1118    if torch_major_and_minor != ipex_major_and_minor:1119        logger.warning(1120            f"Intel Extension for PyTorch {ipex_major_and_minor} needs to work with PyTorch {ipex_major_and_minor}.*,"1121            f" but PyTorch {_torch_version} is found. Please switch to the matching version and run again."1122        )1123        return False1124    if min_version:1125        return version.parse(_ipex_version) >= version.parse(min_version)1126    return True1127 1128 1129@lru_cache1130def is_torch_xpu_available(check_device: bool = False) -> bool:1131    """1132    Checks if XPU acceleration is available either via native PyTorch (>=2.6),1133    `intel_extension_for_pytorch` or via stock PyTorch (>=2.4) and potentially1134    if a XPU is in the environment.1135    """1136    if not is_torch_available():1137        return False1138 1139    torch_version = version.parse(_torch_version)1140    if torch_version.major == 2 and torch_version.minor < 6:1141        if is_ipex_available():1142            import intel_extension_for_pytorch  # noqa: F4011143        elif torch_version.major == 2 and torch_version.minor < 4:1144            return False1145 1146    import torch1147 1148    if check_device:1149        try:1150            # Will raise a RuntimeError if no XPU  is found1151            _ = torch.xpu.device_count()1152            return torch.xpu.is_available()1153        except RuntimeError:1154            return False1155    return hasattr(torch, "xpu") and torch.xpu.is_available()1156 1157 1158@lru_cache1159def is_bitsandbytes_available(check_library_only: bool = False) -> bool:1160    if not _bitsandbytes_available:1161        return False1162 1163    if check_library_only:1164        return True1165 1166    if not is_torch_available():1167        return False1168 1169    import torch1170 1171    # `bitsandbytes` versions older than 0.43.1 eagerly require CUDA at import time,1172    # so those versions of the library are practically only available when CUDA is too.1173    if version.parse(importlib.metadata.version("bitsandbytes")) < version.parse("0.43.1"):1174        return torch.cuda.is_available()1175 1176    # Newer versions of `bitsandbytes` can be imported on systems without CUDA.1177    return True1178 1179 1180def is_bitsandbytes_multi_backend_available() -> bool:1181    if not is_bitsandbytes_available():1182        return False1183 1184    import bitsandbytes as bnb1185 1186    return "multi_backend" in getattr(bnb, "features", set())1187 1188 1189def is_flash_attn_2_available() -> bool:1190    if not is_torch_available():1191        return False1192 1193    if not _is_package_available("flash_attn"):1194        return False1195 1196    # Let's add an extra check to see if cuda is available1197    import torch1198 1199    if not (torch.cuda.is_available() or is_torch_mlu_available()):1200        return False

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

Aluode/PerceptionLabPortable · CoolFace