



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    :						List[str]  =						logging.getLogger()
def 	snake_case__       (					__lowercase       )		->		Union[str, Any]:







						"""simple docstring"""

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


snake_case    :						Tuple  =						logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)




@require_torch_tpu
class 							lowerCAmelCase__  (	UpperCamelCase						):







					def  _lowercase			(       self      :     List[Any]):
											import xla_spawn

											A__	:							Any						    = self.get_auto_remove_tmp_dir()
											A__	:							int						    = 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(_A			,		"argv"			,		_A):
																	A__	:							List[Any]						    = time()
																	xla_spawn.main()
																	A__	:							List[str]						    = time()

																	A__	:							Optional[int]						    = get_results(_A)
																	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  _lowercase			(       self      :     List[Any]):
											import xla_spawn

											A__	:							Union[str, Any]						    = "\n            ./tests/test_trainer_tpu.py\n            --num_cores=8\n            ./tests/test_trainer_tpu.py\n            ".split()
											with patch.object(_A			,		"argv"			,		_A):
																	xla_spawn.main()




import os
import sys
from contextlib import contextmanager


# Windows only
if os.name == "nt":
		import ctypes
		import msvcrt  # noqa




		class 							lowerCAmelCase__  (	ctypes.Structure						):
							# _fields is a specific attr expected by ctypes
							__A     :    Optional[Any]           =						[('size', ctypes.c_int), ('visible', ctypes.c_byte)]
def 	snake_case__       (					)		->		List[Any]:







						"""simple docstring"""

						if os.name == "nt":
												A__	:							Optional[Any]						    = CursorInfo()
												A__	:							Tuple						    = ctypes.windll.kernelaa.GetStdHandle(-1_1       )
												ctypes.windll.kernelaa.GetConsoleCursorInfo(__lowercase							,     ctypes.byref(__lowercase       )       )
												A__	:							Any						    = False
												ctypes.windll.kernelaa.SetConsoleCursorInfo(__lowercase							,     ctypes.byref(__lowercase       )       )
						elif os.name == "posix":
												sys.stdout.write("\033[?25l"       )
												sys.stdout.flush()
def 	snake_case__       (					)		->		Dict:







						"""simple docstring"""

						if os.name == "nt":
												A__	:							List[str]						    = CursorInfo()
												A__	:							Optional[int]						    = ctypes.windll.kernelaa.GetStdHandle(-1_1       )
												ctypes.windll.kernelaa.GetConsoleCursorInfo(__lowercase							,     ctypes.byref(__lowercase       )       )
												A__	:							int						    = True
												ctypes.windll.kernelaa.SetConsoleCursorInfo(__lowercase							,     ctypes.byref(__lowercase       )       )
						elif os.name == "posix":
												sys.stdout.write("\033[?25h"       )
												sys.stdout.flush()




@contextmanager
def 	snake_case__       (					)		->		Optional[int]:







						"""simple docstring"""

						try:
												hide_cursor()
												yield
						finally:
												show_cursor()