






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  (     UpperCamelCase_					:				Optional[int]   ) ->   Optional[Any]:
						"""simple docstring"""







						lowerCAmelCase__       =						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."   )
						lowerCAmelCase__       =						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."   )

						lowerCAmelCase__       =						components[:-1] + [test_fn.replace(".py"    ,   ""   )]
						lowerCAmelCase__       =						'.'.join(_lowercase   )

						return test_module_path


def        _a  (     UpperCamelCase_					:				List[str]   ) ->   int:
						"""simple docstring"""







						lowerCAmelCase__       =						get_module_path(_lowercase   )
						lowerCAmelCase__       =						importlib.import_module(_lowercase   )

						return test_module


def        _a  (     UpperCamelCase_					:				str   ) ->   int:
						"""simple docstring"""







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

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


def        _a  (     UpperCamelCase_					:				str   ) ->   Optional[int]:
						"""simple docstring"""







						lowerCAmelCase__       =						[]
						lowerCAmelCase__       =						get_test_module(_lowercase   )
						for attr in dir(_lowercase   ):
												lowerCAmelCase__       =						getattr(_lowercase    ,   _lowercase   )
												# (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).
												lowerCAmelCase__       =						getattr(_lowercase    ,   "all_model_classes"    ,   []   )
												if len(_lowercase   ) > 0:
																		test_classes.append(_lowercase   )

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


def        _a  (     UpperCamelCase_					:				str   ) ->   str:
						"""simple docstring"""







						lowerCAmelCase__       =						get_test_classes(_lowercase   )
						lowerCAmelCase__       =						set()
						for test_class in test_classes:
												model_classes.update(test_class.all_model_classes   )

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


def        _a  (     UpperCamelCase_					:				List[Any]   ) ->   int:
						"""simple docstring"""







						lowerCAmelCase__       =						test_class()
						if hasattr(_lowercase    ,   "setUp"   ):
												test.setUp()

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

						return model_tester


def        _a  (     UpperCamelCase_					:				List[str]    ,   UpperCamelCase_					:				Union[str, Any]   ) ->   Dict:
						"""simple docstring"""







						lowerCAmelCase__       =						get_test_classes(_lowercase   )

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

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


def        _a  (     UpperCamelCase_					:				List[str]    ,   UpperCamelCase_					:				List[str]   ) ->   Optional[int]:
						"""simple docstring"""







						lowerCAmelCase__       =						get_test_classes_for_model(_lowercase    ,   _lowercase   )

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

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


def        _a  (     UpperCamelCase_					:				Optional[Any]   ) ->   int:
						"""simple docstring"""







						lowerCAmelCase__       =						get_test_classes(_lowercase   )
						lowerCAmelCase__       =						{test_class: get_model_tester_from_test_class(_lowercase   ) for test_class in test_classes}
						return test_tester_mapping


def        _a  (     UpperCamelCase_					:				Optional[Any]   ) ->   int:
						"""simple docstring"""







						lowerCAmelCase__       =						get_model_classes(_lowercase   )
						lowerCAmelCase__       =						{
						    model_class: get_test_classes_for_model(_lowercase    ,   _lowercase   ) for model_class in model_classes
						}
						return model_test_mapping


def        _a  (     UpperCamelCase_					:				Union[str, Any]   ) ->   str:
						"""simple docstring"""







						lowerCAmelCase__       =						get_model_classes(_lowercase   )
						lowerCAmelCase__       =						{
						    model_class: get_tester_classes_for_model(_lowercase    ,   _lowercase   ) for model_class in model_classes
						}
						return model_to_tester_mapping





def        _a  (     UpperCamelCase_					:				int   ) ->   Union[str, Any]:
						"""simple docstring"""







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














import json
import logging
import os
import sys
from time import time
from unittest.mock import patch

from transformers.testing_utils import TestCasePlus, require_torch_tpu


logging.basicConfig(level=logging.DEBUG)

a_							=    logging.getLogger()


def        _a  (     UpperCamelCase_					:				Union[str, Any]   ) ->   Dict:
						"""simple docstring"""







						lowerCAmelCase__       =						{}
						lowerCAmelCase__       =						os.path.join(UpperCamelCase_    ,   "all_results.json"   )
						if os.path.exists(UpperCamelCase_   ):
												with open(UpperCamelCase_    ,   "r"   ) as f:
																		lowerCAmelCase__       =						json.load(UpperCamelCase_   )
						else:
												raise ValueError(F"can't find {path}"   )
						return results


a_							=    logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)





@require_torch_tpu
class      lowercase__     (			_UpperCAmelCase	):

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







							'''simple docstring'''





							import xla_spawn

							lowerCAmelCase__       =						self.get_auto_remove_tmp_dir()
							lowerCAmelCase__       =						F"\n            ./examples/pytorch/text-classification/run_glue.py\n            --num_cores=8\n            ./examples/pytorch/text-classification/run_glue.py\n            --model_name_or_path distilbert-base-uncased\n            --output_dir {tmp_dir}\n            --overwrite_output_dir\n            --train_file ./tests/fixtures/tests_samples/MRPC/train.csv\n            --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv\n            --do_train\n            --do_eval\n            --debug tpu_metrics_debug\n            --per_device_train_batch_size=2\n            --per_device_eval_batch_size=1\n            --learning_rate=1e-4\n            --max_steps=10\n            --warmup_steps=2\n            --seed=42\n            --max_seq_length=128\n            ".split()

							with patch.object(__UpperCAmelCase		,    "argv"		,    __UpperCAmelCase       ):
													lowerCAmelCase__       =						time()
													xla_spawn.main()
													lowerCAmelCase__       =						time()

													lowerCAmelCase__       =						get_results(__UpperCAmelCase       )
													self.assertGreaterEqual(result["eval_accuracy"]		,    0.75       )

													# Assert that the script takes less than 500 seconds to make sure it doesn't hang.
													self.assertLess(end - start		,    500       )


	def 		UpperCAmelCase  (							self       )->					Dict:







							'''simple docstring'''





							import xla_spawn

							lowerCAmelCase__       =						"\n            ./tests/test_trainer_tpu.py\n            --num_cores=8\n            ./tests/test_trainer_tpu.py\n            ".split()
							with patch.object(__UpperCAmelCase		,    "argv"		,    __UpperCAmelCase       ):
													xla_spawn.main()






