


'''simple docstring'''
import importlib.metadata
import operator
import re
import sys
from typing import Optional

from packaging import version


__snake_case							   =  {
    '''<''': operator.lt,
    '''<=''': operator.le,
    '''==''': operator.eq,
    '''!=''': operator.ne,
    '''>=''': operator.ge,
    '''>''': operator.gt,
}


def     a   (    __a				,      __a				,      __a				,      __a				,      __a				,      __a     )						->			Dict:



					'''simple docstring'''






					if got_ver is None or want_ver is None:
										raise ValueError(
										    f'''Unable to compare versions for {requirement}: need={want_ver} found={got_ver}. This is unusual. Consider'''
										    f''' reinstalling {pkg}.'''     )
					if not ops[op](version.parse(__a     )				,      version.parse(__a     )     ):
										raise ImportError(
										    f'''{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}'''     )


def     a   (    __a				,      __a = None     )						->			None:



					'''simple docstring'''






					UpperCamelCase__     :Dict							      =				f'''\n{hint}''' if hint is not None else ''''''

					# non-versioned check
					if re.match(R'''^[\w_\-\d]+$'''				,      __a     ):
										UpperCamelCase__      ,       UpperCamelCase__      ,       UpperCamelCase__     :Any							      =				requirement, None, None
					else:
										UpperCamelCase__     :List[Any]							      =				re.findall(R'''^([^!=<>\s]+)([\s!=<>]{1,2}.+)'''				,      __a     )
										if not match:
															raise ValueError(
															    '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but'''
															    f''' got {requirement}'''     )
										UpperCamelCase__      ,       UpperCamelCase__     :int							      =				match[0]
										UpperCamelCase__     :Tuple							      =				want_full.split(''','''     )  # there could be multiple requirements
										UpperCamelCase__     :Dict							      =				{}
										for w in want_range:
															UpperCamelCase__     :List[Any]							      =				re.findall(R'''^([\s!=<>]{1,2})(.+)'''				,      __a     )
															if not match:
																				raise ValueError(
																				    '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,'''
																				    f''' but got {requirement}'''     )
															UpperCamelCase__      ,       UpperCamelCase__     :Union[str, Any]							      =				match[0]
															UpperCamelCase__     :List[str]							      =				want_ver
															if op not in ops:
																				raise ValueError(f'''{requirement}: need one of {list(ops.keys()     )}, but got {op}'''     )

    # special case
					if pkg == "python":
										UpperCamelCase__     :Optional[int]							      =				'''.'''.join([str(__a     ) for x in sys.version_info[:3]]     )
										for op, want_ver in wanted.items():
															_compare_versions(__a				,      __a				,      __a				,      __a				,      __a				,      __a     )
										return

					# check if any version is installed
					try:
										UpperCamelCase__     :List[Any]							      =				importlib.metadata.version(__a     )
					except importlib.metadata.PackageNotFoundError:
										raise importlib.metadata.PackageNotFoundError(
										    f'''The \'{requirement}\' distribution was not found and is required by this application. {hint}'''     )

					# check that the right version is installed if version number or a range was provided
					if want_ver is not None:
										for op, want_ver in wanted.items():
															_compare_versions(__a				,      __a				,      __a				,      __a				,      __a				,      __a     )







def     a   (    __a     )						->			List[Any]:



					'''simple docstring'''






					UpperCamelCase__     :Optional[int]							      =				'''Try: pip install transformers -U or pip install -e \'.[dev]\' if you\'re working with git main'''
					return require_version(__a				,      __a     )



'''simple docstring'''
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)

__snake_case							   =  logging.getLogger()


def     a   (    __a     )						->			Dict:



					'''simple docstring'''






					UpperCamelCase__     :str							      =				{}
					UpperCamelCase__     :Dict							      =				os.path.join(__a				,      '''all_results.json'''     )
					if os.path.exists(__a     ):
										with open(__a				,      '''r'''     ) as f:
															UpperCamelCase__     :int							      =				json.load(__a     )
					else:
										raise ValueError(f'''can\'t find {path}'''     )
					return results


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







@require_torch_tpu
class      lowercase			(							A__      ):

		"""simple docstring"""

		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							import xla_spawn

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

							with patch.object(UpperCamelCase_					,     '''argv'''					,     UpperCamelCase_							):
												UpperCamelCase__     :List[Any]							      =				time()
												xla_spawn.main()
												UpperCamelCase__     :List[Any]							      =				time()

												UpperCamelCase__     :Tuple							      =				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 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							import xla_spawn

							UpperCamelCase__     :Tuple							      =				'''
            ./tests/test_trainer_tpu.py
            --num_cores=8
            ./tests/test_trainer_tpu.py
            '''.split()
							with patch.object(UpperCamelCase_					,     '''argv'''					,     UpperCamelCase_							):
												xla_spawn.main()