



'''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 DeiTImageProcessor



class _A	(     unittest.TestCase     ):





		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.5, 0.5, 0.5]			,							__UpperCAmelCase=[0.5, 0.5, 0.5]			,							)							->  List[Any]:

					'''simple docstring'''
					__UpperCAmelCase :   List[str]    					=  size if size is not None else {"""height""": 20, """width""": 20}
					__UpperCAmelCase :   Tuple    					=  crop_size if crop_size is not None else {"""height""": 18, """width""": 18}

					__UpperCAmelCase :   Optional[Any]    					=  parent
					__UpperCAmelCase :   int    					=  batch_size
					__UpperCAmelCase :   Optional[Any]    					=  num_channels
					__UpperCAmelCase :   Union[str, Any]    					=  image_size
					__UpperCAmelCase :   Tuple    					=  min_resolution
					__UpperCAmelCase :   Optional[Any]    					=  max_resolution
					__UpperCAmelCase :   Optional[int]    					=  do_resize
					__UpperCAmelCase :   Tuple    					=  size
					__UpperCAmelCase :   Union[str, Any]    					=  do_center_crop
					__UpperCAmelCase :   Dict    					=  crop_size
					__UpperCAmelCase :   List[str]    					=  do_normalize
					__UpperCAmelCase :   Any    					=  image_mean
					__UpperCAmelCase :   Optional[Any]    					=  image_std





		def 				__A							(  self					)							->  Tuple:

					'''simple docstring'''
					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,
					}



@require_torch
@require_vision
class _A	(     __SCREAMING_SNAKE_CASE						,					unittest.TestCase     ):
		_SCREAMING_SNAKE_CASE       :     str       =      DeiTImageProcessor if is_vision_available() else None
		_SCREAMING_SNAKE_CASE       :     Tuple       =      True





		def 				__A							(  self					)							->  str:

					'''simple docstring'''
					__UpperCAmelCase :   Union[str, Any]    					=  DeiTImageProcessingTester(self					)





		@property
		def 				__A							(  self					)							->  int:

					'''simple docstring'''
					return self.image_processor_tester.prepare_image_processor_dict()





		def 				__A							(  self					)							->  str:

					'''simple docstring'''
					__UpperCAmelCase :   str    					=  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"""					)					)





		def 				__A							(  self					)							->  Any:

					'''simple docstring'''
					__UpperCAmelCase :   Any    					=  self.image_processing_class.from_dict(self.image_processor_dict					)
					self.assertEqual(image_processor.size			,							{"""height""": 20, """width""": 20}					)
					self.assertEqual(image_processor.crop_size			,							{"""height""": 18, """width""": 18}					)

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





		def 				__A							(  self					)							->  Tuple:

					'''simple docstring'''
					pass





		def 				__A							(  self					)							->  str:

					'''simple docstring'''
					# Initialize image_processing
					__UpperCAmelCase :   List[Any]    					=  self.image_processing_class(**self.image_processor_dict					)
					# create random PIL images
					__UpperCAmelCase :   Union[str, Any]    					=  prepare_image_inputs(self.image_processor_tester			,							equal_resolution=__UpperCAmelCase					)
					for image in image_inputs:
								self.assertIsInstance(__UpperCAmelCase			,							Image.Image					)

					# Test not batched input
					__UpperCAmelCase :   Any    					=  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
					__UpperCAmelCase :   Optional[Any]    					=  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 				__A							(  self					)							->  List[str]:

					'''simple docstring'''
					# Initialize image_processing
					__UpperCAmelCase :   Any    					=  self.image_processing_class(**self.image_processor_dict					)
					# create random numpy tensors
					__UpperCAmelCase :   Union[str, Any]    					=  prepare_image_inputs(self.image_processor_tester			,							equal_resolution=__UpperCAmelCase			,							numpify=__UpperCAmelCase					)
					for image in image_inputs:
								self.assertIsInstance(__UpperCAmelCase			,							np.ndarray					)

					# Test not batched input
					__UpperCAmelCase :   Dict    					=  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
					__UpperCAmelCase :   Tuple    					=  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 				__A							(  self					)							->  List[str]:

					'''simple docstring'''
					# Initialize image_processing
					__UpperCAmelCase :   Union[str, Any]    					=  self.image_processing_class(**self.image_processor_dict					)
					# create random PyTorch tensors
					__UpperCAmelCase :   Any    					=  prepare_image_inputs(self.image_processor_tester			,							equal_resolution=__UpperCAmelCase			,							torchify=__UpperCAmelCase					)
					for image in image_inputs:
								self.assertIsInstance(__UpperCAmelCase			,							torch.Tensor					)

					# Test not batched input
					__UpperCAmelCase :   Any    					=  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
					__UpperCAmelCase :   int    					=  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"""],
					    )			,							)









'''simple docstring'''
import inspect
import os
import unittest

import torch

import accelerate
from accelerate import debug_launcher
from accelerate.test_utils import (
    execute_subprocess_async,
    require_cpu,
    require_multi_gpu,
    require_single_gpu,
    test_sync,
)
from accelerate.utils import patch_environment



class _A	(     unittest.TestCase     ):





		def 				__A							(  self					)							->  Optional[Any]:

					'''simple docstring'''
					__UpperCAmelCase :   int    					=  inspect.getfile(accelerate.test_utils					)
					__UpperCAmelCase :   int    					=  os.path.sep.join(mod_file.split(os.path.sep					)[:-1] + ["""scripts""", """test_sync.py"""]					)





		@require_cpu
		def 				__A							(  self					)							->  Optional[Any]:

					'''simple docstring'''
					debug_launcher(test_sync.main			,							num_processes=1					)





		@require_cpu
		def 				__A							(  self					)							->  Optional[Any]:

					'''simple docstring'''
					debug_launcher(test_sync.main					)





		@require_single_gpu
		def 				__A							(  self					)							->  Tuple:

					'''simple docstring'''
					test_sync.main()





		@require_multi_gpu
		def 				__A							(  self					)							->  List[Any]:

					'''simple docstring'''
					print(f'Found {torch.cuda.device_count()} devices.'					)
					__UpperCAmelCase :   List[str]    					=  ["""torchrun""", f'--nproc_per_node={torch.cuda.device_count()}', self.test_file_path]
					with patch_environment(omp_num_threads=1					):
								execute_subprocess_async(__UpperCAmelCase			,							env=os.environ.copy()					)




