




'''simple docstring'''







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

			_lowerCamelCase							:						Any							     =  True
except ImportError:
			_lowerCamelCase							:						Tuple							     =  False


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






def 		_lowercase		(     lowercase_  ):



		'''simple docstring'''







		_snake_case     	=	importlib.util.find_spec(snake_case__  ) is not None
		if package_exists:
				try:
						_snake_case     	=	importlib.metadata.metadata(snake_case__  )
						return True
				except importlib.metadata.PackageNotFoundError:
						return False






def 		_lowercase		(     ):



		'''simple docstring'''







		return _torch_distributed_available






def 		_lowercase		(     ):



		'''simple docstring'''







		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 		_lowercase		(     ):



		'''simple docstring'''







		return importlib.metadata.version("oneccl_bind_pt"  )






def 		_lowercase		(     ):



		'''simple docstring'''







		return _is_package_available("transformer_engine"  )






@lru_cache
def 		_lowercase		(     lowercase_=True  ):



		'''simple docstring'''







		if torch.cuda.is_available():
				return False
		if _tpu_available and check_device:
				try:
						# Will raise a RuntimeError if no XLA configuration is found
						_snake_case     	=	xm.xla_device()
						return True
				except RuntimeError:
						return False
		return _tpu_available






def 		_lowercase		(     ):



		'''simple docstring'''







		return _is_package_available("deepspeed"  )






def 		_lowercase		(     lowercase_=False  ):



		'''simple docstring'''







		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 		_lowercase		(     ):



		'''simple docstring'''







		_snake_case     	=	_is_package_available("bitsandbytes"  )
		if package_exists:
				_snake_case     	=	version.parse(importlib.metadata.version("bitsandbytes"  )  )
				return compare_versions(snake_case__						,							">="						,							"0.39.0"  )
		return False






def 		_lowercase		(     ):



		'''simple docstring'''







		_snake_case     	=	_is_package_available("bitsandbytes"  )
		if package_exists:
				_snake_case     	=	version.parse(importlib.metadata.version("bitsandbytes"  )  )
				return compare_versions(snake_case__						,							">="						,							"0.37.2"  )
		return False






def 		_lowercase		(     ):



		'''simple docstring'''







		return _is_package_available("bitsandbytes"  )






def 		_lowercase		(     ):



		'''simple docstring'''







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






def 		_lowercase		(     ):



		'''simple docstring'''







		return _is_package_available("safetensors"  )






def 		_lowercase		(     ):



		'''simple docstring'''







		return _is_package_available("transformers"  )






def 		_lowercase		(     ):



		'''simple docstring'''







		return _is_package_available("datasets"  )






def 		_lowercase		(     ):



		'''simple docstring'''







		return _is_package_available("aim"  )






def 		_lowercase		(     ):



		'''simple docstring'''







		return _is_package_available("tensorboard"  ) or _is_package_available("tensorboardX"  )






def 		_lowercase		(     ):



		'''simple docstring'''







		return _is_package_available("wandb"  )






def 		_lowercase		(     ):



		'''simple docstring'''







		return _is_package_available("comet_ml"  )






def 		_lowercase		(     ):



		'''simple docstring'''







		return _is_package_available("boto3"  )






def 		_lowercase		(     ):



		'''simple docstring'''







		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"						,							snake_case__  )
				return parse_flag_from_env("ACCELERATE_ENABLE_RICH"						,							snake_case__  )
		return False






def 		_lowercase		(     ):



		'''simple docstring'''







		return _is_package_available("sagemaker"  )






def 		_lowercase		(     ):



		'''simple docstring'''







		return _is_package_available("tqdm"  )






def 		_lowercase		(     ):



		'''simple docstring'''







		return _is_package_available("mlflow"  )






def 		_lowercase		(     ):



		'''simple docstring'''







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






def 		_lowercase		(     ):



		'''simple docstring'''







		def get_major_and_minor_from_version(lowercase_  ):
				return str(version.parse(snake_case__  ).major  ) + "." + str(version.parse(snake_case__  ).minor  )

		_snake_case     	=	importlib.metadata.version("torch"  )
		if importlib.util.find_spec("intel_extension_for_pytorch"  ) is None:
				return False
		_snake_case     	=	"N/A"
		try:
				_snake_case     	=	importlib.metadata.version("intel_extension_for_pytorch"  )
		except importlib.metadata.PackageNotFoundError:
				return False
		_snake_case     	=	get_major_and_minor_from_version(_torch_version  )
		_snake_case     	=	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 		_lowercase		(     lowercase_=False  ):



		'''simple docstring'''







		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
						_snake_case     	=	torch.npu.device_count()
						return torch.npu.is_available()
				except RuntimeError:
						return False
		return hasattr(snake_case__						,							"npu"  ) and torch.npu.is_available()






@lru_cache
def 		_lowercase		(     lowercase_=False  ):



		'''simple docstring'''







		if not parse_flag_from_env("ACCELERATE_USE_XPU"						,							default=snake_case__  ):
				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
						_snake_case     	=	torch.xpu.device_count()
						return torch.xpu.is_available()
				except RuntimeError:
						return False
		return hasattr(snake_case__						,							"xpu"  ) and torch.xpu.is_available()











'''simple docstring'''







import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

						from transformers import CLIPImageProcessor





class 					lowerCamelCase_							(	unittest.TestCase							):




					'''simple docstring'''






					def __init__(   self,  __UpperCamelCase,  __UpperCamelCase=7,  __UpperCamelCase=3,  __UpperCamelCase=18,  __UpperCamelCase=30,  __UpperCamelCase=400,  __UpperCamelCase=True,  __UpperCamelCase=None,  __UpperCamelCase=True,  __UpperCamelCase=None,  __UpperCamelCase=True,  __UpperCamelCase=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73],  __UpperCamelCase=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11],  __UpperCamelCase=True,  ):
							_snake_case     	=	size if size is not None else {"shortest_edge": 20}
							_snake_case     	=	crop_size if crop_size is not None else {"height": 18, "width": 18}
							_snake_case     	=	parent
							_snake_case     	=	batch_size
							_snake_case     	=	num_channels
							_snake_case     	=	image_size
							_snake_case     	=	min_resolution
							_snake_case     	=	max_resolution
							_snake_case     	=	do_resize
							_snake_case     	=	size
							_snake_case     	=	do_center_crop
							_snake_case     	=	crop_size
							_snake_case     	=	do_normalize
							_snake_case     	=	image_mean
							_snake_case     	=	image_std
							_snake_case     	=	do_convert_rgb






					def     __SCREAMING_SNAKE_CASE     (   self			):
							return {
							    "do_resize": self.do_resize,
							    "size": self.size,
							    "do_center_crop": self.do_center_crop,
							    "crop_size": self.crop_size,
							    "do_normalize": self.do_normalize,
							    "image_mean": self.image_mean,
							    "image_std": self.image_std,
							    "do_convert_rgb": self.do_convert_rgb,
							}






					def     __SCREAMING_SNAKE_CASE     (   self,  __UpperCamelCase=False,  __UpperCamelCase=False,  __UpperCamelCase=False			):
							assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

							if equal_resolution:
									_snake_case     	=	[]
									for i in range(self.batch_size			):
											image_inputs.append(
											    np.random.randint(
											        255,  size=(self.num_channels, self.max_resolution, self.max_resolution),  dtype=np.uinta			)			)
							else:
									_snake_case     	=	[]
									for i in range(self.batch_size			):
											_snake_case	,   _snake_case     	=	np.random.choice(np.arange(self.min_resolution,  self.max_resolution			),  2			)
											image_inputs.append(np.random.randint(255,  size=(self.num_channels, width, height),  dtype=np.uinta			)			)

							if not numpify and not torchify:
									# PIL expects the channel dimension as last dimension
									_snake_case     	=	[Image.fromarray(np.moveaxis(__UpperCamelCase,  0,  -1			)			) for x in image_inputs]

							if torchify:
									_snake_case     	=	[torch.from_numpy(__UpperCamelCase			) for x in image_inputs]

							return image_inputs





@require_torch
@require_vision
class 					lowerCamelCase_							(	UpperCAmelCase_,	unittest.TestCase							):




					'''simple docstring'''






					lowercase__   =			CLIPImageProcessor if is_vision_available() else None






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	CLIPImageProcessingTester(self			)






					@property
					def     __SCREAMING_SNAKE_CASE     (   self			):
							return self.image_processor_tester.prepare_image_processor_dict()






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.image_processing_class(**self.image_processor_dict			)
							self.assertTrue(hasattr(__UpperCamelCase,  "do_resize"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "size"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "do_center_crop"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "center_crop"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "do_normalize"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "image_mean"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "image_std"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "do_convert_rgb"			)			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.image_processing_class.from_dict(self.image_processor_dict			)
							self.assertEqual(image_processor.size,  {"shortest_edge": 20}			)
							self.assertEqual(image_processor.crop_size,  {"height": 18, "width": 18}			)

							_snake_case     	=	self.image_processing_class.from_dict(self.image_processor_dict,  size=42,  crop_size=84			)
							self.assertEqual(image_processor.size,  {"shortest_edge": 42}			)
							self.assertEqual(image_processor.crop_size,  {"height": 84, "width": 84}			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							pass






					def     __SCREAMING_SNAKE_CASE     (   self			):
							# Initialize image_processing
							_snake_case     	=	self.image_processing_class(**self.image_processor_dict			)
							# create random PIL images
							_snake_case     	=	self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCamelCase			)
							for image in image_inputs:
									self.assertIsInstance(__UpperCamelCase,  Image.Image			)

							# Test not batched input
							_snake_case     	=	image_processing(image_inputs[0],  return_tensors="pt"			).pixel_values
							self.assertEqual(
							    encoded_images.shape,  (
							        1,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    ),  )

							# Test batched
							_snake_case     	=	image_processing(__UpperCamelCase,  return_tensors="pt"			).pixel_values
							self.assertEqual(
							    encoded_images.shape,  (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    ),  )






					def     __SCREAMING_SNAKE_CASE     (   self			):
							# Initialize image_processing
							_snake_case     	=	self.image_processing_class(**self.image_processor_dict			)
							# create random numpy tensors
							_snake_case     	=	self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCamelCase,  numpify=__UpperCamelCase			)
							for image in image_inputs:
									self.assertIsInstance(__UpperCamelCase,  np.ndarray			)

							# Test not batched input
							_snake_case     	=	image_processing(image_inputs[0],  return_tensors="pt"			).pixel_values
							self.assertEqual(
							    encoded_images.shape,  (
							        1,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    ),  )

							# Test batched
							_snake_case     	=	image_processing(__UpperCamelCase,  return_tensors="pt"			).pixel_values
							self.assertEqual(
							    encoded_images.shape,  (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    ),  )






					def     __SCREAMING_SNAKE_CASE     (   self			):
							# Initialize image_processing
							_snake_case     	=	self.image_processing_class(**self.image_processor_dict			)
							# create random PyTorch tensors
							_snake_case     	=	self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCamelCase,  torchify=__UpperCamelCase			)
							for image in image_inputs:
									self.assertIsInstance(__UpperCamelCase,  torch.Tensor			)

							# Test not batched input
							_snake_case     	=	image_processing(image_inputs[0],  return_tensors="pt"			).pixel_values
							self.assertEqual(
							    encoded_images.shape,  (
							        1,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    ),  )

							# Test batched
							_snake_case     	=	image_processing(__UpperCamelCase,  return_tensors="pt"			).pixel_values
							self.assertEqual(
							    encoded_images.shape,  (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    ),  )





@require_torch
@require_vision
class 					lowerCamelCase_							(	UpperCAmelCase_,	unittest.TestCase							):




					'''simple docstring'''






					lowercase__   =			CLIPImageProcessor if is_vision_available() else None






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	CLIPImageProcessingTester(self,  num_channels=4			)
							_snake_case     	=	3






					@property
					def     __SCREAMING_SNAKE_CASE     (   self			):
							return self.image_processor_tester.prepare_image_processor_dict()






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.image_processing_class(**self.image_processor_dict			)
							self.assertTrue(hasattr(__UpperCamelCase,  "do_resize"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "size"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "do_center_crop"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "center_crop"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "do_normalize"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "image_mean"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "image_std"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "do_convert_rgb"			)			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							pass






					def     __SCREAMING_SNAKE_CASE     (   self			):
							# Initialize image_processing
							_snake_case     	=	self.image_processing_class(**self.image_processor_dict			)
							# create random PIL images
							_snake_case     	=	self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCamelCase			)
							for image in image_inputs:
									self.assertIsInstance(__UpperCamelCase,  Image.Image			)

							# Test not batched input
							_snake_case     	=	image_processing(image_inputs[0],  return_tensors="pt"			).pixel_values
							self.assertEqual(
							    encoded_images.shape,  (
							        1,
							        self.expected_encoded_image_num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    ),  )

							# Test batched
							_snake_case     	=	image_processing(__UpperCamelCase,  return_tensors="pt"			).pixel_values
							self.assertEqual(
							    encoded_images.shape,  (
							        self.image_processor_tester.batch_size,
							        self.expected_encoded_image_num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    ),  )





