



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)

SCREAMING_SNAKE_CASE_:Optional[int] 			=		logging.getLogger()




def       __UpperCamelCase      (    _lowerCAmelCase						)			->    Any:



    """simple docstring"""






    A					:			Optional[Any]		=  {}
    A					:			List[str]		=  os.path.join(SCREAMING_SNAKE_CASE_ ,	"""all_results.json"""						)
    if os.path.exists(SCREAMING_SNAKE_CASE_						):
        with open(SCREAMING_SNAKE_CASE_ ,	"""r"""						) as f:
            A					:			str		=  json.load(SCREAMING_SNAKE_CASE_						)
    else:
        raise ValueError(f'''can\'t find {path}'''						)
    return results


SCREAMING_SNAKE_CASE_:Optional[Any] 			=		logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)






@require_torch_tpu
class SCREAMING_SNAKE_CASE__	(						A__       ):




    '''simple docstring'''






    def      _lowerCAmelCase   (			self						):
        import xla_spawn

        A					:			Optional[int]		=  self.get_auto_remove_tmp_dir()
        A					:			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						):
            A					:			Any		=  time()
            xla_spawn.main()
            A					:			Tuple		=  time()

            A					:			int		=  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						):
        import xla_spawn

        A					:			int		=  """
            ./tests/test_trainer_tpu.py
            --num_cores=8
            ./tests/test_trainer_tpu.py
            """.split()
        with patch.object(_UpperCamelCase,						"""argv""",						_UpperCamelCase						):
            xla_spawn.main()









from typing import List

from .keymap import KEYMAP, get_character
def      A__       (    SCREAMING_SNAKE_CASE_   :      str						)  ->  List[str]:






   """simple docstring"""







   def decorator(SCREAMING_SNAKE_CASE_   :      List[Any]						):
      _UpperCAmelCase           =   getattr(SCREAMING_SNAKE_CASE_				,	'''handle_key'''				,	[]						)
      handle += [key]
      setattr(SCREAMING_SNAKE_CASE_				,	'''handle_key'''				,	SCREAMING_SNAKE_CASE_						)
      return func

   return decorator
def      A__       (    *SCREAMING_SNAKE_CASE_   :      List[str]						)  ->  Dict:






   """simple docstring"""







   def decorator(SCREAMING_SNAKE_CASE_   :      Any						):
      _UpperCAmelCase           =   getattr(SCREAMING_SNAKE_CASE_				,	'''handle_key'''				,	[]						)
      handle += keys
      setattr(SCREAMING_SNAKE_CASE_				,	'''handle_key'''				,	SCREAMING_SNAKE_CASE_						)
      return func

   return decorator




class 			__UpperCamelCase    (							A__    ):





     def __new__(     cls				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase					):
        _UpperCAmelCase           =   super().__new__(cls				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase					)
        if not hasattr(_UpperCamelCase				,							'''key_handler'''					):
           setattr(_UpperCamelCase				,							'''key_handler'''				,							{}					)
        setattr(_UpperCamelCase				,							'''handle_input'''				,							KeyHandler.handle_input					)

        for value in attrs.values():
           _UpperCAmelCase           =   getattr(_UpperCamelCase				,							'''handle_key'''				,							[]					)
           for key in handled_keys:
              _UpperCAmelCase           =   value
        return new_cls





     @staticmethod
     def 		UpperCamelCase(     cls					):
        _UpperCAmelCase           =   get_character()
        if char != KEYMAP["undefined"]:
           _UpperCAmelCase           =   ord(_UpperCamelCase					)
        _UpperCAmelCase           =   cls.key_handler.get(_UpperCamelCase					)
        if handler:
           _UpperCAmelCase           =   char
           return handler(cls					)
        else:
           return None




def      A__       (    cls   :      Union[str, Any]						)  ->  Any:






   """simple docstring"""







   return KeyHandler(cls.__name__				,	cls.__bases__				,	cls.__dict__.copy()						)