declare-lab/tango2
92
1# Copyright 2023 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"""17import importlib.util18import operator as op19import os20import sys21from collections import OrderedDict22from typing import Union23 24from huggingface_hub.utils import is_jinja_available # noqa: F40125from packaging import version26from packaging.version import Version, parse27 28from . import logging29 30 31# The package importlib_metadata is in a different place, depending on the python version.32if sys.version_info < (3, 8):33 import importlib_metadata34else:35 import importlib.metadata as importlib_metadata36 37 38logger = logging.get_logger(__name__) # pylint: disable=invalid-name39 40ENV_VARS_TRUE_VALUES = {"1", "ON", "YES", "TRUE"}41ENV_VARS_TRUE_AND_AUTO_VALUES = ENV_VARS_TRUE_VALUES.union({"AUTO"})42 43USE_TF = os.environ.get("USE_TF", "AUTO").upper()44USE_TORCH = os.environ.get("USE_TORCH", "AUTO").upper()45USE_JAX = os.environ.get("USE_FLAX", "AUTO").upper()46USE_SAFETENSORS = os.environ.get("USE_SAFETENSORS", "AUTO").upper()47 48STR_OPERATION_TO_FUNC = {">": op.gt, ">=": op.ge, "==": op.eq, "!=": op.ne, "<=": op.le, "<": op.lt}49 50_torch_version = "N/A"51if USE_TORCH in ENV_VARS_TRUE_AND_AUTO_VALUES and USE_TF not in ENV_VARS_TRUE_VALUES:52 _torch_available = importlib.util.find_spec("torch") is not None53 if _torch_available:54 try:55 _torch_version = importlib_metadata.version("torch")56 logger.info(f"PyTorch version {_torch_version} available.")57 except importlib_metadata.PackageNotFoundError:58 _torch_available = False59else:60 logger.info("Disabling PyTorch because USE_TORCH is set")61 _torch_available = False62 63 64_tf_version = "N/A"65if USE_TF in ENV_VARS_TRUE_AND_AUTO_VALUES and USE_TORCH not in ENV_VARS_TRUE_VALUES:66 _tf_available = importlib.util.find_spec("tensorflow") is not None67 if _tf_available:68 candidates = (69 "tensorflow",70 "tensorflow-cpu",71 "tensorflow-gpu",72 "tf-nightly",73 "tf-nightly-cpu",74 "tf-nightly-gpu",75 "intel-tensorflow",76 "intel-tensorflow-avx512",77 "tensorflow-rocm",78 "tensorflow-macos",79 "tensorflow-aarch64",80 )81 _tf_version = None82 # For the metadata, we have to look for both tensorflow and tensorflow-cpu83 for pkg in candidates:84 try:85 _tf_version = importlib_metadata.version(pkg)86 break87 except importlib_metadata.PackageNotFoundError:88 pass89 _tf_available = _tf_version is not None90 if _tf_available:91 if version.parse(_tf_version) < version.parse("2"):92 logger.info(f"TensorFlow found but with version {_tf_version}. Diffusers requires version 2 minimum.")93 _tf_available = False94 else:95 logger.info(f"TensorFlow version {_tf_version} available.")96else:97 logger.info("Disabling Tensorflow because USE_TORCH is set")98 _tf_available = False99 100_jax_version = "N/A"101_flax_version = "N/A"102if USE_JAX in ENV_VARS_TRUE_AND_AUTO_VALUES:103 _flax_available = importlib.util.find_spec("jax") is not None and importlib.util.find_spec("flax") is not None104 if _flax_available:105 try:106 _jax_version = importlib_metadata.version("jax")107 _flax_version = importlib_metadata.version("flax")108 logger.info(f"JAX version {_jax_version}, Flax version {_flax_version} available.")109 except importlib_metadata.PackageNotFoundError:110 _flax_available = False111else:112 _flax_available = False113 114if USE_SAFETENSORS in ENV_VARS_TRUE_AND_AUTO_VALUES:115 _safetensors_available = importlib.util.find_spec("safetensors") is not None116 if _safetensors_available:117 try:118 _safetensors_version = importlib_metadata.version("safetensors")119 logger.info(f"Safetensors version {_safetensors_version} available.")120 except importlib_metadata.PackageNotFoundError:121 _safetensors_available = False122else:123 logger.info("Disabling Safetensors because USE_TF is set")124 _safetensors_available = False125 126_transformers_available = importlib.util.find_spec("transformers") is not None127try:128 _transformers_version = importlib_metadata.version("transformers")129 logger.debug(f"Successfully imported transformers version {_transformers_version}")130except importlib_metadata.PackageNotFoundError:131 _transformers_available = False132 133 134_inflect_available = importlib.util.find_spec("inflect") is not None135try:136 _inflect_version = importlib_metadata.version("inflect")137 logger.debug(f"Successfully imported inflect version {_inflect_version}")138except importlib_metadata.PackageNotFoundError:139 _inflect_available = False140 141 142_unidecode_available = importlib.util.find_spec("unidecode") is not None143try:144 _unidecode_version = importlib_metadata.version("unidecode")145 logger.debug(f"Successfully imported unidecode version {_unidecode_version}")146except importlib_metadata.PackageNotFoundError:147 _unidecode_available = False148 149 150_onnxruntime_version = "N/A"151_onnx_available = importlib.util.find_spec("onnxruntime") is not None152if _onnx_available:153 candidates = (154 "onnxruntime",155 "onnxruntime-gpu",156 "ort_nightly_gpu",157 "onnxruntime-directml",158 "onnxruntime-openvino",159 "ort_nightly_directml",160 "onnxruntime-rocm",161 "onnxruntime-training",162 )163 _onnxruntime_version = None164 # For the metadata, we have to look for both onnxruntime and onnxruntime-gpu165 for pkg in candidates:166 try:167 _onnxruntime_version = importlib_metadata.version(pkg)168 break169 except importlib_metadata.PackageNotFoundError:170 pass171 _onnx_available = _onnxruntime_version is not None172 if _onnx_available:173 logger.debug(f"Successfully imported onnxruntime version {_onnxruntime_version}")174 175# (sayakpaul): importlib.util.find_spec("opencv-python") returns None even when it's installed.176# _opencv_available = importlib.util.find_spec("opencv-python") is not None177try:178 candidates = (179 "opencv-python",180 "opencv-contrib-python",181 "opencv-python-headless",182 "opencv-contrib-python-headless",183 )184 _opencv_version = None185 for pkg in candidates:186 try:187 _opencv_version = importlib_metadata.version(pkg)188 break189 except importlib_metadata.PackageNotFoundError:190 pass191 _opencv_available = _opencv_version is not None192 if _opencv_available:193 logger.debug(f"Successfully imported cv2 version {_opencv_version}")194except importlib_metadata.PackageNotFoundError:195 _opencv_available = False196 197_scipy_available = importlib.util.find_spec("scipy") is not None198try:199 _scipy_version = importlib_metadata.version("scipy")200 logger.debug(f"Successfully imported scipy version {_scipy_version}")201except importlib_metadata.PackageNotFoundError:202 _scipy_available = False203 204_librosa_available = importlib.util.find_spec("librosa") is not None205try:206 _librosa_version = importlib_metadata.version("librosa")207 logger.debug(f"Successfully imported librosa version {_librosa_version}")208except importlib_metadata.PackageNotFoundError:209 _librosa_available = False210 211_accelerate_available = importlib.util.find_spec("accelerate") is not None212try:213 _accelerate_version = importlib_metadata.version("accelerate")214 logger.debug(f"Successfully imported accelerate version {_accelerate_version}")215except importlib_metadata.PackageNotFoundError:216 _accelerate_available = False217 218_xformers_available = importlib.util.find_spec("xformers") is not None219try:220 _xformers_version = importlib_metadata.version("xformers")221 if _torch_available:222 import torch223 224 if version.Version(torch.__version__) < version.Version("1.12"):225 raise ValueError("PyTorch should be >= 1.12")226 logger.debug(f"Successfully imported xformers version {_xformers_version}")227except importlib_metadata.PackageNotFoundError:228 _xformers_available = False229 230_k_diffusion_available = importlib.util.find_spec("k_diffusion") is not None231try:232 _k_diffusion_version = importlib_metadata.version("k_diffusion")233 logger.debug(f"Successfully imported k-diffusion version {_k_diffusion_version}")234except importlib_metadata.PackageNotFoundError:235 _k_diffusion_available = False236 237_note_seq_available = importlib.util.find_spec("note_seq") is not None238try:239 _note_seq_version = importlib_metadata.version("note_seq")240 logger.debug(f"Successfully imported note-seq version {_note_seq_version}")241except importlib_metadata.PackageNotFoundError:242 _note_seq_available = False243 244_wandb_available = importlib.util.find_spec("wandb") is not None245try:246 _wandb_version = importlib_metadata.version("wandb")247 logger.debug(f"Successfully imported wandb version {_wandb_version }")248except importlib_metadata.PackageNotFoundError:249 _wandb_available = False250 251_omegaconf_available = importlib.util.find_spec("omegaconf") is not None252try:253 _omegaconf_version = importlib_metadata.version("omegaconf")254 logger.debug(f"Successfully imported omegaconf version {_omegaconf_version}")255except importlib_metadata.PackageNotFoundError:256 _omegaconf_available = False257 258_tensorboard_available = importlib.util.find_spec("tensorboard")259try:260 _tensorboard_version = importlib_metadata.version("tensorboard")261 logger.debug(f"Successfully imported tensorboard version {_tensorboard_version}")262except importlib_metadata.PackageNotFoundError:263 _tensorboard_available = False264 265 266_compel_available = importlib.util.find_spec("compel")267try:268 _compel_version = importlib_metadata.version("compel")269 logger.debug(f"Successfully imported compel version {_compel_version}")270except importlib_metadata.PackageNotFoundError:271 _compel_available = False272 273 274def is_torch_available():275 return _torch_available276 277 278def is_safetensors_available():279 return _safetensors_available280 281 282def is_tf_available():283 return _tf_available284 285 286def is_flax_available():287 return _flax_available288 289 290def is_transformers_available():291 return _transformers_available292 293 294def is_inflect_available():295 return _inflect_available296 297 298def is_unidecode_available():299 return _unidecode_available300 301 302def is_onnx_available():303 return _onnx_available304 305 306def is_opencv_available():307 return _opencv_available308 309 310def is_scipy_available():311 return _scipy_available312 313 314def is_librosa_available():315 return _librosa_available316 317 318def is_xformers_available():319 return _xformers_available320 321 322def is_accelerate_available():323 return _accelerate_available324 325 326def is_k_diffusion_available():327 return _k_diffusion_available328 329 330def is_note_seq_available():331 return _note_seq_available332 333 334def is_wandb_available():335 return _wandb_available336 337 338def is_omegaconf_available():339 return _omegaconf_available340 341 342def is_tensorboard_available():343 return _tensorboard_available344 345 346def is_compel_available():347 return _compel_available348 349 350# docstyle-ignore351FLAX_IMPORT_ERROR = """352{0} requires the FLAX library but it was not found in your environment. Checkout the instructions on the353installation page: https://github.com/google/flax and follow the ones that match your environment.354"""355 356# docstyle-ignore357INFLECT_IMPORT_ERROR = """358{0} requires the inflect library but it was not found in your environment. You can install it with pip: `pip install359inflect`360"""361 362# docstyle-ignore363PYTORCH_IMPORT_ERROR = """364{0} requires the PyTorch library but it was not found in your environment. Checkout the instructions on the365installation page: https://pytorch.org/get-started/locally/ and follow the ones that match your environment.366"""367 368# docstyle-ignore369ONNX_IMPORT_ERROR = """370{0} requires the onnxruntime library but it was not found in your environment. You can install it with pip: `pip371install onnxruntime`372"""373 374# docstyle-ignore375OPENCV_IMPORT_ERROR = """376{0} requires the OpenCV library but it was not found in your environment. You can install it with pip: `pip377install opencv-python`378"""379 380# docstyle-ignore381SCIPY_IMPORT_ERROR = """382{0} requires the scipy library but it was not found in your environment. You can install it with pip: `pip install383scipy`384"""385 386# docstyle-ignore387LIBROSA_IMPORT_ERROR = """388{0} requires the librosa library but it was not found in your environment. Checkout the instructions on the389installation page: https://librosa.org/doc/latest/install.html and follow the ones that match your environment.390"""391 392# docstyle-ignore393TRANSFORMERS_IMPORT_ERROR = """394{0} requires the transformers library but it was not found in your environment. You can install it with pip: `pip395install transformers`396"""397 398# docstyle-ignore399UNIDECODE_IMPORT_ERROR = """400{0} requires the unidecode library but it was not found in your environment. You can install it with pip: `pip install401Unidecode`402"""403 404# docstyle-ignore405K_DIFFUSION_IMPORT_ERROR = """406{0} requires the k-diffusion library but it was not found in your environment. You can install it with pip: `pip407install k-diffusion`408"""409 410# docstyle-ignore411NOTE_SEQ_IMPORT_ERROR = """412{0} requires the note-seq library but it was not found in your environment. You can install it with pip: `pip413install note-seq`414"""415 416# docstyle-ignore417WANDB_IMPORT_ERROR = """418{0} requires the wandb library but it was not found in your environment. You can install it with pip: `pip419install wandb`420"""421 422# docstyle-ignore423OMEGACONF_IMPORT_ERROR = """424{0} requires the omegaconf library but it was not found in your environment. You can install it with pip: `pip425install omegaconf`426"""427 428# docstyle-ignore429TENSORBOARD_IMPORT_ERROR = """430{0} requires the tensorboard library but it was not found in your environment. You can install it with pip: `pip431install tensorboard`432"""433 434 435# docstyle-ignore436COMPEL_IMPORT_ERROR = """437{0} requires the compel library but it was not found in your environment. You can install it with pip: `pip install compel`438"""439 440BACKENDS_MAPPING = OrderedDict(441 [442 ("flax", (is_flax_available, FLAX_IMPORT_ERROR)),443 ("inflect", (is_inflect_available, INFLECT_IMPORT_ERROR)),444 ("onnx", (is_onnx_available, ONNX_IMPORT_ERROR)),445 ("opencv", (is_opencv_available, OPENCV_IMPORT_ERROR)),446 ("scipy", (is_scipy_available, SCIPY_IMPORT_ERROR)),447 ("torch", (is_torch_available, PYTORCH_IMPORT_ERROR)),448 ("transformers", (is_transformers_available, TRANSFORMERS_IMPORT_ERROR)),449 ("unidecode", (is_unidecode_available, UNIDECODE_IMPORT_ERROR)),450 ("librosa", (is_librosa_available, LIBROSA_IMPORT_ERROR)),451 ("k_diffusion", (is_k_diffusion_available, K_DIFFUSION_IMPORT_ERROR)),452 ("note_seq", (is_note_seq_available, NOTE_SEQ_IMPORT_ERROR)),453 ("wandb", (is_wandb_available, WANDB_IMPORT_ERROR)),454 ("omegaconf", (is_omegaconf_available, OMEGACONF_IMPORT_ERROR)),455 ("tensorboard", (_tensorboard_available, TENSORBOARD_IMPORT_ERROR)),456 ("compel", (_compel_available, COMPEL_IMPORT_ERROR)),457 ]458)459 460 461def requires_backends(obj, backends):462 if not isinstance(backends, (list, tuple)):463 backends = [backends]464 465 name = obj.__name__ if hasattr(obj, "__name__") else obj.__class__.__name__466 checks = (BACKENDS_MAPPING[backend] for backend in backends)467 failed = [msg.format(name) for available, msg in checks if not available()]468 if failed:469 raise ImportError("".join(failed))470 471 if name in [472 "VersatileDiffusionTextToImagePipeline",473 "VersatileDiffusionPipeline",474 "VersatileDiffusionDualGuidedPipeline",475 "StableDiffusionImageVariationPipeline",476 "UnCLIPPipeline",477 ] and is_transformers_version("<", "4.25.0"):478 raise ImportError(479 f"You need to install `transformers>=4.25` in order to use {name}: \n```\n pip install"480 " --upgrade transformers \n```"481 )482 483 if name in ["StableDiffusionDepth2ImgPipeline", "StableDiffusionPix2PixZeroPipeline"] and is_transformers_version(484 "<", "4.26.0"485 ):486 raise ImportError(487 f"You need to install `transformers>=4.26` in order to use {name}: \n```\n pip install"488 " --upgrade transformers \n```"489 )490 491 492class DummyObject(type):493 """494 Metaclass for the dummy objects. Any class inheriting from it will return the ImportError generated by495 `requires_backend` each time a user tries to access any method of that class.496 """497 498 def __getattr__(cls, key):499 if key.startswith("_"):500 return super().__getattr__(cls, key)501 requires_backends(cls, cls._backends)502 503 504# This function was copied from: https://github.com/huggingface/accelerate/blob/874c4967d94badd24f893064cc3bef45f57cadf7/src/accelerate/utils/versions.py#L319505def compare_versions(library_or_version: Union[str, Version], operation: str, requirement_version: str):506 """507 Args:508 Compares a library version to some requirement using a given operation.509 library_or_version (`str` or `packaging.version.Version`):510 A library name or a version to check.511 operation (`str`):512 A string representation of an operator, such as `">"` or `"<="`.513 requirement_version (`str`):514 The version to compare the library version against515 """516 if operation not in STR_OPERATION_TO_FUNC.keys():517 raise ValueError(f"`operation` must be one of {list(STR_OPERATION_TO_FUNC.keys())}, received {operation}")518 operation = STR_OPERATION_TO_FUNC[operation]519 if isinstance(library_or_version, str):520 library_or_version = parse(importlib_metadata.version(library_or_version))521 return operation(library_or_version, parse(requirement_version))522 523 524# This function was copied from: https://github.com/huggingface/accelerate/blob/874c4967d94badd24f893064cc3bef45f57cadf7/src/accelerate/utils/versions.py#L338525def is_torch_version(operation: str, version: str):526 """527 Args:528 Compares the current PyTorch version to a given reference with an operation.529 operation (`str`):530 A string representation of an operator, such as `">"` or `"<="`531 version (`str`):532 A string version of PyTorch533 """534 return compare_versions(parse(_torch_version), operation, version)535 536 537def is_transformers_version(operation: str, version: str):538 """539 Args:540 Compares the current Transformers version to a given reference with an operation.541 operation (`str`):542 A string representation of an operator, such as `">"` or `"<="`543 version (`str`):544 A version string545 """546 if not _transformers_available:547 return False548 return compare_versions(parse(_transformers_version), operation, version)549 550 551def is_accelerate_version(operation: str, version: str):552 """553 Args:554 Compares the current Accelerate version to a given reference with an operation.555 operation (`str`):556 A string representation of an operator, such as `">"` or `"<="`557 version (`str`):558 A version string559 """560 if not _accelerate_available:561 return False562 return compare_versions(parse(_accelerate_version), operation, version)563 564 565def is_k_diffusion_version(operation: str, version: str):566 """567 Args:568 Compares the current k-diffusion version to a given reference with an operation.569 operation (`str`):570 A string representation of an operator, such as `">"` or `"<="`571 version (`str`):572 A version string573 """574 if not _k_diffusion_available:575 return False576 return compare_versions(parse(_k_diffusion_version), operation, version)577 578 579class OptionalDependencyNotAvailable(BaseException):580 """An error indicating that an optional dependency of Diffusers was not found in the environment."""581 