
"""simple docstring"""




import inspect
import os
import unittest
from pathlib import Path

import torch

import accelerate
from accelerate.test_utils import execute_subprocess_async
from accelerate.test_utils.testing import run_command



class 			lowerCAmelCase_		(unittest.TestCase						):


				"""simple docstring"""



				__UpperCamelCase   :		Dict       =     inspect.getfile(accelerate.test_utils						)
				__UpperCamelCase   :		Any       =     os.path.sep.join(mod_file.split(os.path.sep						)[:-1] + ['''scripts''', '''test_cli.py''']						)

				__UpperCamelCase   :		List[Any]       =     ['''accelerate''', '''launch''']
				__UpperCamelCase   :		List[str]       =     Path.home() / '''.cache/huggingface/accelerate'''
				__UpperCamelCase   :		str       =     '''default_config.yaml'''
				__UpperCamelCase   :		str       =     config_folder / config_file
				__UpperCamelCase   :		Optional[Any]       =     config_folder / '''_default_config.yaml'''

				__UpperCamelCase   :		Tuple       =     Path('''tests/test_configs'''						)



				@classmethod
				def 			__magic_name__					(cls					)      ->	Tuple:



											"""simple docstring"""




											if cls.config_path.is_file():
																		cls.config_path.rename(cls.changed_path					)



				@classmethod
				def 			__magic_name__					(cls					)      ->	Tuple:



											"""simple docstring"""




											if cls.changed_path.is_file():
																		cls.changed_path.rename(cls.config_path					)



				def 			__magic_name__					(self					)      ->	List[str]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     self.base_cmd
											if torch.cuda.is_available() and (torch.cuda.device_count() > 1):
																		cmd += ["--multi_gpu"]
											execute_subprocess_async(cmd + [self.test_file_path]     , env=os.environ.copy()					)



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



											"""simple docstring"""




											for config in sorted(self.test_config_path.glob("""**/*.yaml"""					)					):
																		with self.subTest(config_file=SCREAMING_SNAKE_CASE__					):
																									execute_subprocess_async(
																									    self.base_cmd + ["""--config_file""", str(SCREAMING_SNAKE_CASE__					), self.test_file_path]     , env=os.environ.copy()					)



				def 			__magic_name__					(self					)      ->	Dict:



											"""simple docstring"""




											execute_subprocess_async(["""accelerate""", """test"""]     , env=os.environ.copy()					)






class 			lowerCAmelCase_		(unittest.TestCase						):


				"""simple docstring"""



				__UpperCamelCase   :		List[str]       =     '''test-tpu'''
				__UpperCamelCase   :		List[str]       =     '''us-central1-a'''
				__UpperCamelCase   :		Tuple       =     '''ls'''
				__UpperCamelCase   :		Tuple       =     ['''accelerate''', '''tpu-config''']
				__UpperCamelCase   :		List[str]       =     '''cd /usr/share'''
				__UpperCamelCase   :		Any       =     '''tests/test_samples/test_command_file.sh'''
				__UpperCamelCase   :		Any       =     '''Running gcloud compute tpus tpu-vm ssh'''



				def 			__magic_name__					(self					)      ->	List[str]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[str]     =     run_command(
											    self.cmd
											    + ["""--command""", self.command, """--tpu_zone""", self.tpu_zone, """--tpu_name""", self.tpu_name, """--debug"""]     , return_stdout=SCREAMING_SNAKE_CASE__     , )
											self.assertIn(
											    F'''{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all'''     , SCREAMING_SNAKE_CASE__     , )



				def 			__magic_name__					(self					)      ->	Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[Any]     =     run_command(
											    self.cmd
											    + [
											        """--config_file""",
											        """tests/test_configs/0_12_0.yaml""",
											        """--command""",
											        self.command,
											        """--tpu_zone""",
											        self.tpu_zone,
											        """--tpu_name""",
											        self.tpu_name,
											        """--debug""",
											    ]     , return_stdout=SCREAMING_SNAKE_CASE__     , )
											self.assertIn(
											    F'''{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all'''     , SCREAMING_SNAKE_CASE__     , )



				def 			__magic_name__					(self					)      ->	Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: int     =     run_command(
											    self.cmd + ["""--config_file""", """tests/test_configs/latest.yaml""", """--debug"""]     , return_stdout=SCREAMING_SNAKE_CASE__					)
											self.assertIn(
											    F'''{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo "hello world"; echo "this is a second command" --worker all'''     , SCREAMING_SNAKE_CASE__     , )



				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Tuple     =     run_command(
											    self.cmd + ["""--config_file""", """tests/test_configs/latest.yaml""", """--command""", self.command, """--debug"""]     , return_stdout=SCREAMING_SNAKE_CASE__     , )
											self.assertIn(
											    F'''{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all'''     , SCREAMING_SNAKE_CASE__     , )



				def 			__magic_name__					(self					)      ->	str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[Any]     =     run_command(
											    self.cmd
											    + [
											        """--config_file""",
											        """tests/test_configs/latest.yaml""",
											        """--command""",
											        self.command,
											        """--command""",
											        """echo \"Hello World\"""",
											        """--debug""",
											    ]     , return_stdout=SCREAMING_SNAKE_CASE__     , )
											self.assertIn(
											    F'''{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls; echo "Hello World" --worker all'''     , SCREAMING_SNAKE_CASE__     , )



				def 			__magic_name__					(self					)      ->	Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[Any]     =     run_command(
											    self.cmd
											    + ["""--config_file""", """tests/test_configs/latest.yaml""", """--command_file""", self.command_file, """--debug"""]     , return_stdout=SCREAMING_SNAKE_CASE__     , )
											self.assertIn(
											    F'''{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo "hello world"; echo "this is a second command" --worker all'''     , SCREAMING_SNAKE_CASE__     , )



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     run_command(
											    self.cmd
											    + [
											        """--config_file""",
											        """tests/test_configs/0_12_0.yaml""",
											        """--command_file""",
											        self.command_file,
											        """--tpu_zone""",
											        self.tpu_zone,
											        """--tpu_name""",
											        self.tpu_name,
											        """--debug""",
											    ]     , return_stdout=SCREAMING_SNAKE_CASE__     , )
											self.assertIn(
											    F'''{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo "hello world"; echo "this is a second command" --worker all'''     , SCREAMING_SNAKE_CASE__     , )



				def 			__magic_name__					(self					)      ->	List[str]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Dict     =     run_command(
											    self.cmd + ["""--config_file""", """tests/test_configs/latest.yaml""", """--install_accelerate""", """--debug"""]     , return_stdout=SCREAMING_SNAKE_CASE__     , )
											self.assertIn(
											    F'''{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate -U; echo "hello world"; echo "this is a second command" --worker all'''     , SCREAMING_SNAKE_CASE__     , )



				def 			__magic_name__					(self					)      ->	Tuple:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: int     =     run_command(
											    self.cmd
											    + [
											        """--config_file""",
											        """tests/test_configs/latest.yaml""",
											        """--install_accelerate""",
											        """--accelerate_version""",
											        """12.0.0""",
											        """--debug""",
											    ]     , return_stdout=SCREAMING_SNAKE_CASE__     , )
											self.assertIn(
											    F'''{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate==12.0.0; echo "hello world"; echo "this is a second command" --worker all'''     , SCREAMING_SNAKE_CASE__     , )






"""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 MobileViTImageProcessor



class 			lowerCAmelCase_		(unittest.TestCase						):


				"""simple docstring"""



				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__=4_00     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=None     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=None     , SCREAMING_SNAKE_CASE__=True     , )      ->	List[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[str]     =     size if size is not None else {"""shortest_edge""": 20}
											SCREAMING_SNAKE_CASE__		: List[Any]     =     crop_size if crop_size is not None else {"""height""": 18, """width""": 18}
											SCREAMING_SNAKE_CASE__		: Any     =     parent
											SCREAMING_SNAKE_CASE__		: List[str]     =     batch_size
											SCREAMING_SNAKE_CASE__		: Dict     =     num_channels
											SCREAMING_SNAKE_CASE__		: List[Any]     =     image_size
											SCREAMING_SNAKE_CASE__		: Dict     =     min_resolution
											SCREAMING_SNAKE_CASE__		: Dict     =     max_resolution
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     do_resize
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     size
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     do_center_crop
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     crop_size
											SCREAMING_SNAKE_CASE__		: List[str]     =     do_flip_channel_order



				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											return {
											    "do_resize": self.do_resize,
											    "size": self.size,
											    "do_center_crop": self.do_center_crop,
											    "crop_size": self.crop_size,
											    "do_flip_channel_order": self.do_flip_channel_order,
											}






@require_torch
@require_vision
class 			lowerCAmelCase_		(a__				,      unittest.TestCase						):


				"""simple docstring"""



				__UpperCamelCase   :		str       =     MobileViTImageProcessor if is_vision_available() else None



				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     MobileViTImageProcessingTester(self					)



				@property
				def 			__magic_name__					(self					)      ->	Any:



											"""simple docstring"""




											return self.image_processor_tester.prepare_image_processor_dict()



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     self.image_processing_class(**self.image_processor_dict					)
											self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__     , """do_resize"""					)					)
											self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__     , """size"""					)					)
											self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__     , """do_center_crop"""					)					)
											self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__     , """center_crop"""					)					)
											self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__     , """do_flip_channel_order"""					)					)



				def 			__magic_name__					(self					)      ->	Tuple:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Dict     =     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}					)

											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     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 			__magic_name__					(self					)      ->	Union[str, Any]:



											"""simple docstring"""




											pass



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[str]     =     self.image_processing_class(**self.image_processor_dict					)
											# create random PIL images
											SCREAMING_SNAKE_CASE__		: List[str]     =     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
											SCREAMING_SNAKE_CASE__		: 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
											SCREAMING_SNAKE_CASE__		: List[str]     =     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 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Optional[int]     =     self.image_processing_class(**self.image_processor_dict					)
											# create random numpy tensors
											SCREAMING_SNAKE_CASE__		: Tuple     =     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
											SCREAMING_SNAKE_CASE__		: Optional[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
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     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 			__magic_name__					(self					)      ->	Optional[int]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[Any]     =     self.image_processing_class(**self.image_processor_dict					)
											# create random PyTorch tensors
											SCREAMING_SNAKE_CASE__		: Any     =     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
											SCREAMING_SNAKE_CASE__		: List[str]     =     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
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     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"""],
											    )     , )




