




'''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, prepare_image_inputs


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import LevitImageProcessor





class      lowerCAmelCase (			unittest.TestCase):


       def __init__( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE=7     ,	__SCREAMING_SNAKE_CASE=3     ,	__SCREAMING_SNAKE_CASE=18     ,	__SCREAMING_SNAKE_CASE=30     ,	__SCREAMING_SNAKE_CASE=400     ,	__SCREAMING_SNAKE_CASE=True     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE=True     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE=True     ,	__SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5]     ,	__SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5]     ,	)   ->	Any:



            '''simple docstring'''




            __snake_case            =	size if size is not None else {'''shortest_edge''': 18}
            __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




       def    lowerCAmelCase   ( self	)   ->	Dict:



            '''simple docstring'''




            return {
                "image_mean": self.image_mean,
                "image_std": self.image_std,
                "do_normalize": self.do_normalize,
                "do_resize": self.do_resize,
                "do_center_crop": self.do_center_crop,
                "size": self.size,
                "crop_size": self.crop_size,
            }





@require_torch
@require_vision
class      lowerCAmelCase (			__lowerCAmelCase					, unittest.TestCase):
       __lowercase	:   List[Any]										=      LevitImageProcessor if is_vision_available() else None


       def    lowerCAmelCase   ( self	)   ->	List[Any]:



            '''simple docstring'''




            __snake_case            =	LevitImageProcessingTester(self	)


       @property
       def    lowerCAmelCase   ( self	)   ->	Optional[Any]:



            '''simple docstring'''




            return self.image_processor_tester.prepare_image_processor_dict()


       def    lowerCAmelCase   ( self	)   ->	Union[str, Any]:



            '''simple docstring'''




            __snake_case            =	self.image_processing_class(**self.image_processor_dict	)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE     ,	'''image_mean'''	)	)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE     ,	'''image_std'''	)	)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE     ,	'''do_normalize'''	)	)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE     ,	'''do_resize'''	)	)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE     ,	'''do_center_crop'''	)	)
            self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE     ,	'''size'''	)	)


       def    lowerCAmelCase   ( self	)   ->	Any:



            '''simple docstring'''




            __snake_case            =	self.image_processing_class.from_dict(self.image_processor_dict	)
            self.assertEqual(image_processor.size     ,	{'''shortest_edge''': 18}	)
            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    lowerCAmelCase   ( self	)   ->	Union[str, Any]:



            '''simple docstring'''




            pass


       def    lowerCAmelCase   ( self	)   ->	Optional[int]:



            '''simple docstring'''




            __snake_case            =	self.image_processing_class(**self.image_processor_dict	)
            # create random PIL images
            __snake_case            =	prepare_image_inputs(self.image_processor_tester     ,	equal_resolution=__SCREAMING_SNAKE_CASE	)
            for image in image_inputs:
                 self.assertIsInstance(__SCREAMING_SNAKE_CASE     ,	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(__SCREAMING_SNAKE_CASE     ,	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    lowerCAmelCase   ( self	)   ->	List[str]:



            '''simple docstring'''




            __snake_case            =	self.image_processing_class(**self.image_processor_dict	)
            # create random numpy tensors
            __snake_case            =	prepare_image_inputs(self.image_processor_tester     ,	equal_resolution=__SCREAMING_SNAKE_CASE     ,	numpify=__SCREAMING_SNAKE_CASE	)
            for image in image_inputs:
                 self.assertIsInstance(__SCREAMING_SNAKE_CASE     ,	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(__SCREAMING_SNAKE_CASE     ,	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    lowerCAmelCase   ( self	)   ->	Tuple:



            '''simple docstring'''




            __snake_case            =	self.image_processing_class(**self.image_processor_dict	)
            # create random PyTorch tensors
            __snake_case            =	prepare_image_inputs(self.image_processor_tester     ,	equal_resolution=__SCREAMING_SNAKE_CASE     ,	torchify=__SCREAMING_SNAKE_CASE	)
            for image in image_inputs:
                 self.assertIsInstance(__SCREAMING_SNAKE_CASE     ,	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(__SCREAMING_SNAKE_CASE     ,	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'''],
                )     ,	)









'''simple docstring'''

import importlib.util
import json
import os
import warnings
from dataclasses import dataclass, field

import torch

from ..training_args import TrainingArguments
from ..utils import cached_property, is_sagemaker_dp_enabled, logging


SCREAMING_SNAKE_CASE							:				Optional[Any]     	=			logging.get_logger(__name__)




def 							_UpperCamelCase				(   )      -> List[Any]:
							# Get the sagemaker specific mp parameters from smp_options variable.
							SCREAMING_SNAKE_CASE_				=				os.getenv('SM_HP_MP_PARAMETERS'		,'{}'   )
							try:
														# Parse it and check the field "partitions" is included, it is required for model parallel.
														SCREAMING_SNAKE_CASE_				=				json.loads(lowerCAmelCase__   )
														if "partitions" not in smp_options:
																					return False
							except json.JSONDecodeError:
														return False

							# Get the sagemaker specific framework parameters from mpi_options variable.
							SCREAMING_SNAKE_CASE_				=				os.getenv('SM_FRAMEWORK_PARAMS'		,'{}'   )
							try:
														# Parse it and check the field "sagemaker_distributed_dataparallel_enabled".
														SCREAMING_SNAKE_CASE_				=				json.loads(lowerCAmelCase__   )
														if not mpi_options.get('sagemaker_mpi_enabled'		,lowerCAmelCase__   ):
																					return False
							except json.JSONDecodeError:
														return False
							# Lastly, check if the `smdistributed` module is present.
							return importlib.util.find_spec('smdistributed'   ) is not None


if is_sagemaker_model_parallel_available():
							import smdistributed.modelparallel.torch as smp

							smp.init()






@dataclass
class    snake_case     (      lowercase_		):






				"""simple docstring"""
				_a    	=     field(
				    default="""""",						metadata={"""help""": """Used by the SageMaker launcher to send mp-specific args. Ignored in SageMakerTrainer"""},						)



				def   a__					(    self       )     ->							Union[str, Any]:
											super().__post_init__()
											warnings.warn(
											    '`SageMakerTrainingArguments` is deprecated and will be removed in v5 of Transformers. You can use '
											    '`TrainingArguments` instead.',     _lowercase,     )



				@cached_property
				def   a__					(    self       )     ->							"torch.device":
											logger.info('PyTorch: setting up devices'       )
											if torch.distributed.is_available() and torch.distributed.is_initialized() and self.local_rank == -1:
																		logger.warning(
																		    'torch.distributed process group is initialized, but local_rank == -1. '
																		    'In order to use Torch DDP, launch your script with `python -m torch.distributed.launch'       )
											if self.no_cuda:
																		SCREAMING_SNAKE_CASE_				=				torch.device('cpu'       )
																		SCREAMING_SNAKE_CASE_				=				0
											elif is_sagemaker_model_parallel_available():
																		SCREAMING_SNAKE_CASE_				=				smp.local_rank()
																		SCREAMING_SNAKE_CASE_				=				torch.device('cuda',     _lowercase       )
																		SCREAMING_SNAKE_CASE_				=				1
											elif is_sagemaker_dp_enabled():
																		import smdistributed.dataparallel.torch.torch_smddp  # noqa: F401

																		torch.distributed.init_process_group(backend='smddp',     timeout=self.ddp_timeout_delta       )
																		SCREAMING_SNAKE_CASE_				=				int(os.getenv('SMDATAPARALLEL_LOCAL_RANK'       )       )
																		SCREAMING_SNAKE_CASE_				=				torch.device('cuda',     self.local_rank       )
																		SCREAMING_SNAKE_CASE_				=				1
											elif self.local_rank == -1:
																		# if n_gpu is > 1 we'll use nn.DataParallel.
																		# If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0`
																		# Explicitly set CUDA to the first (index 0) CUDA device, otherwise `set_device` will
																		# trigger an error that a device index is missing. Index 0 takes into account the
																		# GPUs available in the environment, so `CUDA_VISIBLE_DEVICES=1,2` with `cuda:0`
																		# will use the first GPU in that env, i.e. GPU#1
																		SCREAMING_SNAKE_CASE_				=				torch.device('cuda:0' if torch.cuda.is_available() else 'cpu'       )
																		# Sometimes the line in the postinit has not been run before we end up here, so just checking we're not at
																		# the default value.
																		SCREAMING_SNAKE_CASE_				=				torch.cuda.device_count()
											else:
																		# Here, we'll use torch.distributed.
																		# Initializes the distributed backend which will take care of synchronizing nodes/GPUs
																		if not torch.distributed.is_initialized():
																									torch.distributed.init_process_group(backend='nccl',     timeout=self.ddp_timeout_delta       )
																		SCREAMING_SNAKE_CASE_				=				torch.device('cuda',     self.local_rank       )
																		SCREAMING_SNAKE_CASE_				=				1

											if device.type == "cuda":
																		torch.cuda.set_device(_lowercase       )

											return device



				@property
				def   a__					(    self       )     ->							Optional[Any]:
											if is_sagemaker_model_parallel_available():
																		return smp.dp_size()

											return super().world_size



				@property
				def   a__					(    self       )     ->							Optional[Any]:
											return not is_sagemaker_model_parallel_available()





				@property
				def   a__					(    self       )     ->							Tuple:
											return False





