





"""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 ):
						UpperCAmelCase_ =			inspect.getfile(accelerate.test_utils )
						UpperCAmelCase_ =			os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["scripts", "test_cli.py"] )

						UpperCAmelCase_ =			["accelerate", "launch"]
						UpperCAmelCase_ =			Path.home() / ".cache/huggingface/accelerate"
						UpperCAmelCase_ =			"default_config.yaml"
						UpperCAmelCase_ =			config_folder / config_file
						UpperCAmelCase_ =			config_folder / "_default_config.yaml"

						UpperCAmelCase_ =			Path("tests/test_configs" )


						@classmethod
						def     snake_case_   (cls		) ->   List[str]:
								if cls.config_path.is_file():
										cls.config_path.rename(cls.changed_path		)


						@classmethod
						def     snake_case_   (cls		) ->   Optional[Any]:
								if cls.changed_path.is_file():
										cls.changed_path.rename(cls.config_path		)


						def     snake_case_   (self		) ->   Optional[Any]:
								UpperCamelCase   =	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     snake_case_   (self		) ->   List[Any]:
								for config in sorted(self.test_config_path.glob("**/*.yaml"		)		):
										with self.subTest(config_file=__a		):
												execute_subprocess_async(
												    self.base_cmd + ["--config_file", str(__a		), self.test_file_path]   ,							env=os.environ.copy()		)




						def     snake_case_   (self		) ->   List[str]:
								execute_subprocess_async(["accelerate", "test"]   ,							env=os.environ.copy()		)





class        _lowerCamelCase      (       unittest.TestCase ):
						UpperCAmelCase_ =			"test-tpu"
						UpperCAmelCase_ =			"us-central1-a"
						UpperCAmelCase_ =			"ls"
						UpperCAmelCase_ =			["accelerate", "tpu-config"]
						UpperCAmelCase_ =			"cd /usr/share"
						UpperCAmelCase_ =			"tests/test_samples/test_command_file.sh"
						UpperCAmelCase_ =			"Running gcloud compute tpus tpu-vm ssh"


						def     snake_case_   (self		) ->   Any:
								UpperCamelCase   =	run_command(
								    self.cmd
								    + ["--command", self.command, "--tpu_zone", self.tpu_zone, "--tpu_name", self.tpu_name, "--debug"]   ,							return_stdout=__a   ,							)
								self.assertIn(
								    F"{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all"   ,							__a   ,							)


						def     snake_case_   (self		) ->   Any:
								UpperCamelCase   =	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=__a   ,							)
								self.assertIn(
								    F"{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all"   ,							__a   ,							)


						def     snake_case_   (self		) ->   str:
								UpperCamelCase   =	run_command(
								    self.cmd + ["--config_file", "tests/test_configs/latest.yaml", "--debug"]   ,							return_stdout=__a		)
								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"   ,							__a   ,							)


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


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


						def     snake_case_   (self		) ->   str:
								UpperCamelCase   =	run_command(
								    self.cmd
								    + ["--config_file", "tests/test_configs/latest.yaml", "--command_file", self.command_file, "--debug"]   ,							return_stdout=__a   ,							)
								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"   ,							__a   ,							)


						def     snake_case_   (self		) ->   int:
								UpperCamelCase   =	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=__a   ,							)
								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"   ,							__a   ,							)


						def     snake_case_   (self		) ->   int:
								UpperCamelCase   =	run_command(
								    self.cmd + ["--config_file", "tests/test_configs/latest.yaml", "--install_accelerate", "--debug"]   ,							return_stdout=__a   ,							)
								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"   ,							__a   ,							)




						def     snake_case_   (self		) ->   Union[str, Any]:
								UpperCamelCase   =	run_command(
								    self.cmd
								    + [
								        "--config_file",
								        "tests/test_configs/latest.yaml",
								        "--install_accelerate",
								        "--accelerate_version",
								        "12.0.0",
								        "--debug",
								    ]   ,							return_stdout=__a   ,							)
								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"   ,							__a   ,							)













"""simple docstring"""





import importlib
import os
import sys


# This is required to make the module import works (when the python process is running from the root of the repo)
sys.path.append('''.''')


def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	test_file.split(os.path.sep    )
		if components[0:2] != ["tests", "models"]:
				raise ValueError(
				    "`test_file` should start with `tests/models/` (with `/` being the OS specific path separator). Got "
				    F"{test_file} instead."    )
		UpperCamelCase   =	components[-1]
		if not test_fn.endswith("py"    ):
				raise ValueError(F"`test_file` should be a python file. Got {test_fn} instead."    )
		if not test_fn.startswith("test_modeling_"    ):
				raise ValueError(
				    F"`test_file` should point to a file name of the form `test_modeling_*.py`. Got {test_fn} instead."    )

		UpperCamelCase   =	components[:-1] + [test_fn.replace(".py"  ,	""    )]
		UpperCamelCase   =	".".join(_SCREAMING_SNAKE_CASE    )

		return test_module_path


def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	get_module_path(_SCREAMING_SNAKE_CASE    )
		UpperCamelCase   =	importlib.import_module(_SCREAMING_SNAKE_CASE    )

		return test_module


def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	[]
		UpperCamelCase   =	get_test_module(_SCREAMING_SNAKE_CASE    )
		for attr in dir(_SCREAMING_SNAKE_CASE    ):
				if attr.endswith("ModelTester"    ):
						tester_classes.append(getattr(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    )    )

    # sort with class names
		return sorted(_SCREAMING_SNAKE_CASE  ,	key=lambda _SCREAMING_SNAKE_CASE    : x.__name__    )


def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	[]
		UpperCamelCase   =	get_test_module(_SCREAMING_SNAKE_CASE    )
		for attr in dir(_SCREAMING_SNAKE_CASE    ):
				UpperCamelCase   =	getattr(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    )
				# (TF/Flax)ModelTesterMixin is also an attribute in specific model test module. Let's exclude them by checking
				# `all_model_classes` is not empty (which also excludes other special classes).
				UpperCamelCase   =	getattr(_SCREAMING_SNAKE_CASE  ,	"all_model_classes"  ,	[]    )
				if len(_SCREAMING_SNAKE_CASE    ) > 0:
						test_classes.append(_SCREAMING_SNAKE_CASE    )

    # sort with class names
		return sorted(_SCREAMING_SNAKE_CASE  ,	key=lambda _SCREAMING_SNAKE_CASE    : x.__name__    )


def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	get_test_classes(_SCREAMING_SNAKE_CASE    )
		UpperCamelCase   =	set()
		for test_class in test_classes:
				model_classes.update(test_class.all_model_classes    )

		# sort with class names
		return sorted(_SCREAMING_SNAKE_CASE  ,	key=lambda _SCREAMING_SNAKE_CASE    : x.__name__    )


def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	test_class()
		if hasattr(_SCREAMING_SNAKE_CASE  ,	"setUp"    ):
				test.setUp()

		UpperCamelCase   =	None
		if hasattr(_SCREAMING_SNAKE_CASE  ,	"model_tester"    ):
				# `(TF/Flax)ModelTesterMixin` has this attribute default to `None`. Let's skip this case.
				if test.model_tester is not None:
						UpperCamelCase   =	test.model_tester.__class__

		return model_tester


def       a__					(					_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	get_test_classes(_SCREAMING_SNAKE_CASE    )

		UpperCamelCase   =	[]
		for test_class in test_classes:
				if model_class in test_class.all_model_classes:
						target_test_classes.append(_SCREAMING_SNAKE_CASE    )

    # sort with class names
		return sorted(_SCREAMING_SNAKE_CASE  ,	key=lambda _SCREAMING_SNAKE_CASE    : x.__name__    )


def       a__					(					_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	get_test_classes_for_model(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    )

		UpperCamelCase   =	[]
		for test_class in test_classes:
				UpperCamelCase   =	get_model_tester_from_test_class(_SCREAMING_SNAKE_CASE    )
				if tester_class is not None:
						tester_classes.append(_SCREAMING_SNAKE_CASE    )

    # sort with class names
		return sorted(_SCREAMING_SNAKE_CASE  ,	key=lambda _SCREAMING_SNAKE_CASE    : x.__name__    )


def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	get_test_classes(_SCREAMING_SNAKE_CASE    )
		UpperCamelCase   =	{test_class: get_model_tester_from_test_class(_SCREAMING_SNAKE_CASE    ) for test_class in test_classes}
		return test_tester_mapping


def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	get_model_classes(_SCREAMING_SNAKE_CASE    )
		UpperCamelCase   =	{
		    model_class: get_test_classes_for_model(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    ) for model_class in model_classes
		}
		return model_test_mapping


def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	get_model_classes(_SCREAMING_SNAKE_CASE    )
		UpperCamelCase   =	{
		    model_class: get_tester_classes_for_model(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    ) for model_class in model_classes
		}
		return model_to_tester_mapping






def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		if isinstance(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    ):
				return o
		elif isinstance(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    ):
				return o.__name__
		elif isinstance(_SCREAMING_SNAKE_CASE  ,	(list, tuple)    ):
				return [to_json(_SCREAMING_SNAKE_CASE    ) for x in o]
		elif isinstance(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    ):
				return {to_json(_SCREAMING_SNAKE_CASE    ): to_json(_SCREAMING_SNAKE_CASE    ) for k, v in o.items()}
		else:
				return o






