

from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import SwinConfig
from transformers.testing_utils import require_tf, require_vision, slow, to_atuple
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
				import tensorflow as tf

				from transformers.models.swin.modeling_tf_swin import (
				    TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFSwinForImageClassification,
				    TFSwinForMaskedImageModeling,
				    TFSwinModel,
				)


if is_vision_available():
				from PIL import Image

				from transformers import AutoImageProcessor

class   __UpperCamelCase      :




	def __init__(						self		:  str						,		lowerCAmelCase		:  List[Any]						,		lowerCAmelCase		:  Optional[Any]=13						,		lowerCAmelCase		:  Tuple=32						,		lowerCAmelCase		:  Dict=2						,		lowerCAmelCase		:  List[Any]=3						,		lowerCAmelCase		:  Optional[int]=16						,		lowerCAmelCase		:  List[Any]=[1, 2, 1]						,		lowerCAmelCase		:  Any=[2, 2, 4]						,		lowerCAmelCase		:  str=2						,		lowerCAmelCase		:  List[Any]=2.0						,		lowerCAmelCase		:  Tuple=True						,		lowerCAmelCase		:  str=0.0						,		lowerCAmelCase		:  Any=0.0						,		lowerCAmelCase		:  List[Any]=0.1						,		lowerCAmelCase		:  List[str]="gelu"						,		lowerCAmelCase		:  str=False						,		lowerCAmelCase		:  List[str]=True						,		lowerCAmelCase		:  List[Any]=0.02						,		lowerCAmelCase		:  str=1e-5						,		lowerCAmelCase		:  str=True						,		lowerCAmelCase		:  Union[str, Any]=None						,		lowerCAmelCase		:  str=True						,		lowerCAmelCase		:  Any=10						,		lowerCAmelCase		:  Tuple=8						,		):


		'''simple docstring'''







		UpperCAmelCase_    			=     parent
		UpperCAmelCase_    			=     batch_size
		UpperCAmelCase_    			=     image_size
		UpperCAmelCase_    			=     patch_size
		UpperCAmelCase_    			=     num_channels
		UpperCAmelCase_    			=     embed_dim
		UpperCAmelCase_    			=     depths
		UpperCAmelCase_    			=     num_heads
		UpperCAmelCase_    			=     window_size
		UpperCAmelCase_    			=     mlp_ratio
		UpperCAmelCase_    			=     qkv_bias
		UpperCAmelCase_    			=     hidden_dropout_prob
		UpperCAmelCase_    			=     attention_probs_dropout_prob
		UpperCAmelCase_    			=     drop_path_rate
		UpperCAmelCase_    			=     hidden_act
		UpperCAmelCase_    			=     use_absolute_embeddings
		UpperCAmelCase_    			=     patch_norm
		UpperCAmelCase_    			=     layer_norm_eps
		UpperCAmelCase_    			=     initializer_range
		UpperCAmelCase_    			=     is_training
		UpperCAmelCase_    			=     scope
		UpperCAmelCase_    			=     use_labels
		UpperCAmelCase_    			=     type_sequence_label_size
		UpperCAmelCase_    			=     encoder_stride




	def  __A   (						self		:  Dict							):


		'''simple docstring'''







		UpperCAmelCase_    			=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

		UpperCAmelCase_    			=     None
		if self.use_labels:
			UpperCAmelCase_    			=     ids_tensor([self.batch_size]						,		self.type_sequence_label_size							)

		UpperCAmelCase_    			=     self.get_config()

		return config, pixel_values, labels




	def  __A   (						self		:  Union[str, Any]							):


		'''simple docstring'''







		return SwinConfig(
		    image_size=self.image_size						,		patch_size=self.patch_size						,		num_channels=self.num_channels						,		embed_dim=self.embed_dim						,		depths=self.depths						,		num_heads=self.num_heads						,		window_size=self.window_size						,		mlp_ratio=self.mlp_ratio						,		qkv_bias=self.qkv_bias						,		hidden_dropout_prob=self.hidden_dropout_prob						,		attention_probs_dropout_prob=self.attention_probs_dropout_prob						,		drop_path_rate=self.drop_path_rate						,		hidden_act=self.hidden_act						,		use_absolute_embeddings=self.use_absolute_embeddings						,		path_norm=self.patch_norm						,		layer_norm_eps=self.layer_norm_eps						,		initializer_range=self.initializer_range						,		encoder_stride=self.encoder_stride						,		)




	def  __A   (						self		:  Tuple						,		lowerCAmelCase		:  str						,		lowerCAmelCase		:  List[str]						,		lowerCAmelCase		:  Optional[int]							):


		'''simple docstring'''







		UpperCAmelCase_    			=     TFSwinModel(config=lowerCAmelCase							)
		UpperCAmelCase_    			=     model(lowerCAmelCase							)

		UpperCAmelCase_    			=     ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths							) - 1))
		UpperCAmelCase_    			=     int(config.embed_dim * 2 ** (len(config.depths							) - 1)							)

		self.parent.assertEqual(result.last_hidden_state.shape						,		(self.batch_size, expected_seq_len, expected_dim)							)




	def  __A   (						self		:  Dict						,		lowerCAmelCase		:  int						,		lowerCAmelCase		:  Tuple						,		lowerCAmelCase		:  List[Any]							):


		'''simple docstring'''







		UpperCAmelCase_    			=     TFSwinForMaskedImageModeling(config=lowerCAmelCase							)
		UpperCAmelCase_    			=     model(lowerCAmelCase							)
		self.parent.assertEqual(
		    result.logits.shape						,		(self.batch_size, self.num_channels, self.image_size, self.image_size)							)

		# test greyscale images
		UpperCAmelCase_    			=     1
		UpperCAmelCase_    			=     TFSwinForMaskedImageModeling(lowerCAmelCase							)

		UpperCAmelCase_    			=     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
		UpperCAmelCase_    			=     model(lowerCAmelCase							)
		self.parent.assertEqual(result.logits.shape						,		(self.batch_size, 1, self.image_size, self.image_size)							)




	def  __A   (						self		:  Any						,		lowerCAmelCase		:  Optional[Any]						,		lowerCAmelCase		:  Dict						,		lowerCAmelCase		:  Any							):


		'''simple docstring'''







		UpperCAmelCase_    			=     self.type_sequence_label_size
		UpperCAmelCase_    			=     TFSwinForImageClassification(lowerCAmelCase							)
		UpperCAmelCase_    			=     model(lowerCAmelCase						,		labels=lowerCAmelCase							)
		self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.type_sequence_label_size)							)

		# test greyscale images
		UpperCAmelCase_    			=     1
		UpperCAmelCase_    			=     TFSwinForImageClassification(lowerCAmelCase							)
		UpperCAmelCase_    			=     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
		UpperCAmelCase_    			=     model(lowerCAmelCase							)
		self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.type_sequence_label_size)							)




	def  __A   (						self		:  Any							):


		'''simple docstring'''







		UpperCAmelCase_    			=     self.prepare_config_and_inputs()
		UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_    			=     config_and_inputs
		UpperCAmelCase_    			=     {"pixel_values": pixel_values}
		return config, inputs_dict


@require_tf
class   __UpperCamelCase      (   lowercase       ,       lowercase       ,       unittest.TestCase  ):
	SCREAMING_SNAKE_CASE__      			=       (
	    (
	        TFSwinModel,
	        TFSwinForImageClassification,
	        TFSwinForMaskedImageModeling,
	    )
	    if is_tf_available()
	    else ()
	)
	SCREAMING_SNAKE_CASE__      			=       (
	    {'feature-extraction': TFSwinModel, 'image-classification': TFSwinForImageClassification}
	    if is_tf_available()
	    else {}
	)

	SCREAMING_SNAKE_CASE__      			=       False
	SCREAMING_SNAKE_CASE__      			=       False
	SCREAMING_SNAKE_CASE__      			=       False
	SCREAMING_SNAKE_CASE__      			=       False




	def  __A   (						self		:  Union[str, Any]							):


		'''simple docstring'''







		UpperCAmelCase_    			=     TFSwinModelTester(self							)
		UpperCAmelCase_    			=     ConfigTester(self						,		config_class=lowerCAmelCase						,		embed_dim=37							)




	def  __A   (						self		:  Tuple							):


		'''simple docstring'''







		self.create_and_test_config_common_properties()
		self.config_tester.create_and_test_config_to_json_string()
		self.config_tester.create_and_test_config_to_json_file()
		self.config_tester.create_and_test_config_from_and_save_pretrained()
		self.config_tester.create_and_test_config_with_num_labels()
		self.config_tester.check_config_can_be_init_without_params()
		self.config_tester.check_config_arguments_init()




	def  __A   (						self		:  Tuple							):


		'''simple docstring'''







		return




	def  __A   (						self		:  Optional[int]							):


		'''simple docstring'''







		UpperCAmelCase_    			=     self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_model(*lowerCAmelCase							)




	def  __A   (						self		:  Tuple							):


		'''simple docstring'''







		UpperCAmelCase_    			=     self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase							)




	def  __A   (						self		:  Any							):


		'''simple docstring'''







		UpperCAmelCase_    			=     self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase							)




	@unittest.skip(reason="Swin does not use inputs_embeds"							)
	def  __A   (						self		:  Tuple							):


		'''simple docstring'''







		pass




	def  __A   (						self		:  List[str]							):


		'''simple docstring'''







		UpperCAmelCase_			,				UpperCAmelCase_    			=     self.model_tester.prepare_config_and_inputs_for_common()

		for model_class in self.all_model_classes:
			UpperCAmelCase_    			=     model_class(lowerCAmelCase							)
			self.assertIsInstance(model.get_input_embeddings()						,		tf.keras.layers.Layer							)
			UpperCAmelCase_    			=     model.get_output_embeddings()
			self.assertTrue(x is None or isinstance(lowerCAmelCase						,		tf.keras.layers.Dense							)							)




	def  __A   (						self		:  List[Any]							):


		'''simple docstring'''







		UpperCAmelCase_			,				UpperCAmelCase_    			=     self.model_tester.prepare_config_and_inputs_for_common()

		for model_class in self.all_model_classes:
			UpperCAmelCase_    			=     model_class(lowerCAmelCase							)
			UpperCAmelCase_    			=     inspect.signature(model.call							)
			# signature.parameters is an OrderedDict => so arg_names order is deterministic
			UpperCAmelCase_    			=     [*signature.parameters.keys()]

			UpperCAmelCase_    			=     ["pixel_values"]
			self.assertListEqual(arg_names[:1]						,		lowerCAmelCase							)




	def  __A   (						self		:  Optional[int]							):


		'''simple docstring'''







		UpperCAmelCase_			,				UpperCAmelCase_    			=     self.model_tester.prepare_config_and_inputs_for_common()
		UpperCAmelCase_    			=     True

		for model_class in self.all_model_classes:
			UpperCAmelCase_    			=     True
			UpperCAmelCase_    			=     False
			UpperCAmelCase_    			=     True
			UpperCAmelCase_    			=     model_class(lowerCAmelCase							)
			UpperCAmelCase_    			=     model(**self._prepare_for_class(lowerCAmelCase						,		lowerCAmelCase							)							)
			UpperCAmelCase_    			=     outputs.attentions
			UpperCAmelCase_    			=     len(self.model_tester.depths							)
			self.assertEqual(len(lowerCAmelCase							)						,		lowerCAmelCase							)

			# check that output_attentions also work using config
			del inputs_dict["output_attentions"]
			UpperCAmelCase_    			=     True
			UpperCAmelCase_    			=     config.window_size**2
			UpperCAmelCase_    			=     model_class(lowerCAmelCase							)
			UpperCAmelCase_    			=     model(**self._prepare_for_class(lowerCAmelCase						,		lowerCAmelCase							)							)
			UpperCAmelCase_    			=     outputs.attentions
			self.assertEqual(len(lowerCAmelCase							)						,		lowerCAmelCase							)

			self.assertListEqual(
			    list(attentions[0].shape[-3:]							)						,		[self.model_tester.num_heads[0], window_size_squared, window_size_squared]						,		)
			UpperCAmelCase_    			=     len(lowerCAmelCase							)

			# Check attention is always last and order is fine
			UpperCAmelCase_    			=     True
			UpperCAmelCase_    			=     True
			UpperCAmelCase_    			=     model_class(lowerCAmelCase							)
			UpperCAmelCase_    			=     model(**self._prepare_for_class(lowerCAmelCase						,		lowerCAmelCase							)							)

			if hasattr(self.model_tester						,		"num_hidden_states_types"							):
				UpperCAmelCase_    			=     self.model_tester.num_hidden_states_types
			else:
				# also another +1 for reshaped_hidden_states
				UpperCAmelCase_    			=     2
			self.assertEqual(out_len + added_hidden_states						,		len(lowerCAmelCase							)							)

			UpperCAmelCase_    			=     outputs.attentions

			self.assertEqual(len(lowerCAmelCase							)						,		lowerCAmelCase							)

			self.assertListEqual(
			    list(self_attentions[0].shape[-3:]							)						,		[self.model_tester.num_heads[0], window_size_squared, window_size_squared]						,		)




	def  __A   (						self		:  str						,		lowerCAmelCase		:  Optional[Any]						,		lowerCAmelCase		:  Union[str, Any]						,		lowerCAmelCase		:  Optional[Any]						,		lowerCAmelCase		:  Dict							):


		'''simple docstring'''







		UpperCAmelCase_    			=     model_class(lowerCAmelCase							)
		UpperCAmelCase_    			=     model(**self._prepare_for_class(lowerCAmelCase						,		lowerCAmelCase							)							)
		UpperCAmelCase_    			=     outputs.hidden_states

		UpperCAmelCase_    			=     getattr(
		    self.model_tester						,		"expected_num_hidden_layers"						,		len(self.model_tester.depths							) + 1							)
		self.assertEqual(len(lowerCAmelCase							)						,		lowerCAmelCase							)

		# Swin has a different seq_length
		UpperCAmelCase_    			=     to_atuple(config.patch_size							)

		UpperCAmelCase_    			=     (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

		self.assertListEqual(
		    list(hidden_states[0].shape[-2:]							)						,		[num_patches, self.model_tester.embed_dim]						,		)

		UpperCAmelCase_    			=     outputs.reshaped_hidden_states
		self.assertEqual(len(lowerCAmelCase							)						,		lowerCAmelCase							)

		UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_    			=     reshaped_hidden_states[0].shape

		UpperCAmelCase_    			=     tf.reshape(reshaped_hidden_states[0]						,		(batch_size, num_channels, height * width)							)
		UpperCAmelCase_    			=     tf.transpose(lowerCAmelCase						,		(0, 2, 1)							)

		self.assertListEqual(
		    list(reshaped_hidden_states.shape[-2:]							)						,		[num_patches, self.model_tester.embed_dim]						,		)




	def  __A   (						self		:  int							):


		'''simple docstring'''







		UpperCAmelCase_			,				UpperCAmelCase_    			=     self.model_tester.prepare_config_and_inputs_for_common()

		UpperCAmelCase_    			=     to_atuple(self.model_tester.image_size							)

		for model_class in self.all_model_classes:
			UpperCAmelCase_    			=     True
			self.check_hidden_states_output(lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase							)

			# check that output_hidden_states also work using config
			del inputs_dict["output_hidden_states"]
			UpperCAmelCase_    			=     True

			self.check_hidden_states_output(lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase							)




	def  __A   (						self		:  Union[str, Any]							):


		'''simple docstring'''







		UpperCAmelCase_			,				UpperCAmelCase_    			=     self.model_tester.prepare_config_and_inputs_for_common()
		UpperCAmelCase_    			=     3

		UpperCAmelCase_    			=     to_atuple(self.model_tester.image_size							)
		UpperCAmelCase_    			=     to_atuple(config.patch_size							)

		UpperCAmelCase_    			=     image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
		UpperCAmelCase_    			=     image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

		for model_class in self.all_model_classes:
			UpperCAmelCase_    			=     True
			self.check_hidden_states_output(lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase						,		(padded_height, padded_width)							)

			# check that output_hidden_states also work using config
			del inputs_dict["output_hidden_states"]
			UpperCAmelCase_    			=     True
			self.check_hidden_states_output(lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase						,		(padded_height, padded_width)							)




	@slow
	def  __A   (						self		:  Tuple							):


		'''simple docstring'''







		for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			UpperCAmelCase_    			=     TFSwinModel.from_pretrained(lowerCAmelCase							)
			self.assertIsNotNone(lowerCAmelCase							)


@require_vision
@require_tf
class   __UpperCamelCase      (   unittest.TestCase  ):




	@cached_property
	def  __A   (						self		:  int							):


		'''simple docstring'''







		return (
		    AutoImageProcessor.from_pretrained("microsoft/swin-tiny-patch4-window7-224"							)
		    if is_vision_available()
		    else None
		)




	@slow
	def  __A   (						self		:  str							):


		'''simple docstring'''







		UpperCAmelCase_    			=     TFSwinForImageClassification.from_pretrained("microsoft/swin-tiny-patch4-window7-224"							)
		UpperCAmelCase_    			=     self.default_image_processor

		UpperCAmelCase_    			=     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
		UpperCAmelCase_    			=     image_processor(images=lowerCAmelCase						,		return_tensors="tf"							)

		# forward pass
		UpperCAmelCase_    			=     model(lowerCAmelCase							)

		# verify the logits
		UpperCAmelCase_    			=     tf.TensorShape((1, 1_000)							)
		self.assertEqual(outputs.logits.shape						,		lowerCAmelCase							)
		UpperCAmelCase_    			=     tf.constant([-0.0_948, -0.6_454, -0.0_921]							)
		self.assertTrue(np.allclose(outputs.logits[0, :3]						,		lowerCAmelCase						,		atol=1e-4							)							)






import importlib
import importlib.metadata
import os
import warnings
from distutils.util import strtobool
from functools import lru_cache

import torch
from packaging import version
from packaging.version import parse

from .environment import parse_flag_from_env
from .versions import compare_versions, is_torch_version


try:
				import torch_xla.core.xla_model as xm  # noqa: F401

				_a:      List[Any]								=     True
except ImportError:
				_a:      int								=     False


# Cache this result has it's a C FFI call which can be pretty time-consuming
_a:      str								=     torch.distributed.is_available()



def  __lowerCAmelCase (  A      ):
	# Check we're not importing a "pkg_name" directory somewhere but the actual library by trying to grab the version
	UpperCAmelCase_    			=     importlib.util.find_spec(A      ) is not None
	if package_exists:
		try:
			UpperCAmelCase_    			=     importlib.metadata.metadata(A      )
			return True
		except importlib.metadata.PackageNotFoundError:
			return False



def  __lowerCAmelCase (  ):
	return _torch_distributed_available



def  __lowerCAmelCase (  ):
	try:
		pass
	except ImportError:
		print(
		    "Intel(R) oneCCL Bindings for PyTorch* is required to run DDP on Intel(R) GPUs, but it is not"
		    " detected. If you see \"ValueError: Invalid backend: 'ccl'\" error, please install Intel(R) oneCCL"
		    " Bindings for PyTorch*."      )
	return (
	    importlib.util.find_spec("torch_ccl"      ) is not None
	    or importlib.util.find_spec("oneccl_bindings_for_pytorch"      ) is not None
	)



def  __lowerCAmelCase (  ):
	return importlib.metadata.version("oneccl_bind_pt"      )



def  __lowerCAmelCase (  ):
	return _is_package_available("transformer_engine"      )



@lru_cache
def  __lowerCAmelCase (  A=True      ):
	# Due to bugs on the amp series GPUs, we disable torch-xla on them
	if torch.cuda.is_available():
		return False
	if _tpu_available and check_device:
		try:
			# Will raise a RuntimeError if no XLA configuration is found
			UpperCAmelCase_    			=     xm.xla_device()
			return True
		except RuntimeError:
			return False
	return _tpu_available



def  __lowerCAmelCase (  ):
	return _is_package_available("deepspeed"      )



def  __lowerCAmelCase (  A=False      ):
	if is_tpu_available():
		return not ignore_tpu
	if torch.cuda.is_available():
		return torch.cuda.is_bfaa_supported()
	if is_npu_available():
		return False
	return True



def  __lowerCAmelCase (  ):
	UpperCAmelCase_    			=     _is_package_available("bitsandbytes"      )
	if package_exists:
		UpperCAmelCase_    			=     version.parse(importlib.metadata.version("bitsandbytes"      )      )
		return compare_versions(A    ,       ">="    ,       "0.39.0"      )
	return False



def  __lowerCAmelCase (  ):
	UpperCAmelCase_    			=     _is_package_available("bitsandbytes"      )
	if package_exists:
		UpperCAmelCase_    			=     version.parse(importlib.metadata.version("bitsandbytes"      )      )
		return compare_versions(A    ,       ">="    ,       "0.37.2"      )
	return False



def  __lowerCAmelCase (  ):
	return _is_package_available("bitsandbytes"      )



def  __lowerCAmelCase (  ):
	if strtobool(os.environ.get("ACCELERATE_USE_MEGATRON_LM"    ,       "False"      )      ) == 1:
		UpperCAmelCase_    			=     importlib.util.find_spec("megatron"      ) is not None
		if package_exists:
			try:
				UpperCAmelCase_    			=     parse(importlib.metadata.version("megatron-lm"      )      )
				return compare_versions(A    ,       ">="    ,       "2.2.0"      )
			except Exception as e:
				warnings.warn(F"Parse Megatron version failed. Exception:{e}"      )
				return False



def  __lowerCAmelCase (  ):
	return _is_package_available("safetensors"      )



def  __lowerCAmelCase (  ):
	return _is_package_available("transformers"      )



def  __lowerCAmelCase (  ):
	return _is_package_available("datasets"      )



def  __lowerCAmelCase (  ):
	return _is_package_available("aim"      )



def  __lowerCAmelCase (  ):
	return _is_package_available("tensorboard"      ) or _is_package_available("tensorboardX"      )



def  __lowerCAmelCase (  ):
	return _is_package_available("wandb"      )



def  __lowerCAmelCase (  ):
	return _is_package_available("comet_ml"      )



def  __lowerCAmelCase (  ):
	return _is_package_available("boto3"      )



def  __lowerCAmelCase (  ):
	if _is_package_available("rich"      ):
		if "ACCELERATE_DISABLE_RICH" in os.environ:
			warnings.warn(
			    "`ACCELERATE_DISABLE_RICH` is deprecated and will be removed in v0.22.0 and deactivated by default. Please use `ACCELERATE_ENABLE_RICH` if you wish to use `rich`."      )
			return not parse_flag_from_env("ACCELERATE_DISABLE_RICH"    ,       A      )
		return parse_flag_from_env("ACCELERATE_ENABLE_RICH"    ,       A      )
	return False



def  __lowerCAmelCase (  ):
	return _is_package_available("sagemaker"      )



def  __lowerCAmelCase (  ):
	return _is_package_available("tqdm"      )



def  __lowerCAmelCase (  ):
	return _is_package_available("mlflow"      )



def  __lowerCAmelCase (  ):
	return is_torch_version(">="    ,       "1.12"      ) and torch.backends.mps.is_available() and torch.backends.mps.is_built()



def  __lowerCAmelCase (  ):
	def get_major_and_minor_from_version(A      ):
		return str(version.parse(A      ).major      ) + "." + str(version.parse(A      ).minor      )

	UpperCAmelCase_    			=     importlib.metadata.version("torch"      )
	if importlib.util.find_spec("intel_extension_for_pytorch"      ) is None:
		return False
	UpperCAmelCase_    			=     "N/A"
	try:
		UpperCAmelCase_    			=     importlib.metadata.version("intel_extension_for_pytorch"      )
	except importlib.metadata.PackageNotFoundError:
		return False
	UpperCAmelCase_    			=     get_major_and_minor_from_version(_torch_version      )
	UpperCAmelCase_    			=     get_major_and_minor_from_version(_ipex_version      )
	if torch_major_and_minor != ipex_major_and_minor:
		warnings.warn(
		    F"Intel Extension for PyTorch {ipex_major_and_minor} needs to work with PyTorch {ipex_major_and_minor}.*,"
		    F" but PyTorch {_torch_version} is found. Please switch to the matching version and run again."      )
		return False
	return True



@lru_cache
def  __lowerCAmelCase (  A=False      ):
	if importlib.util.find_spec("torch"      ) is None or importlib.util.find_spec("torch_npu"      ) is None:
		return False

	import torch
	import torch_npu  # noqa: F401

	if check_device:
		try:
			# Will raise a RuntimeError if no NPU is found
			UpperCAmelCase_    			=     torch.npu.device_count()
			return torch.npu.is_available()
		except RuntimeError:
			return False
	return hasattr(A    ,       "npu"      ) and torch.npu.is_available()




@lru_cache
def  __lowerCAmelCase (  A=False      ):
	if not parse_flag_from_env("ACCELERATE_USE_XPU"    ,       default=A      ):
		return False
	if is_ipex_available():
		import torch

		if is_torch_version("<="    ,       "1.12"      ):
			return False
	else:
		return False

	import intel_extension_for_pytorch  # noqa: F401

	if check_device:
		try:
			# Will raise a RuntimeError if no XPU  is found
			UpperCAmelCase_    			=     torch.xpu.device_count()
			return torch.xpu.is_available()
		except RuntimeError:
			return False
	return hasattr(A    ,       "xpu"      ) and torch.xpu.is_available()