






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)

UpperCamelCase			    =     logging.getLogger()

def 	__lowerCamelCase      (		snake_case__		)							->  Optional[int]:






						"""simple docstring"""





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


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




@require_torch_tpu
class 				__UpperCAmelCase		(UpperCamelCase__   ):







			def 							UpperCamelCase				(     self:  str					):







									'''simple docstring'''




									import xla_spawn

									_SCREAMING_SNAKE_CASE     				=  self.get_auto_remove_tmp_dir()
									_SCREAMING_SNAKE_CASE     				=  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(__lowerCamelCase				,     """argv"""				,     __lowerCamelCase					):
															_SCREAMING_SNAKE_CASE     				=  time()
															xla_spawn.main()
															_SCREAMING_SNAKE_CASE     				=  time()

															_SCREAMING_SNAKE_CASE     				=  get_results(__lowerCamelCase					)
															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:  Tuple					):







									'''simple docstring'''




									import xla_spawn

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









UpperCamelCase			    =     {0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []}

UpperCamelCase			    =     {0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]}

def 	__lowerCamelCase      (		snake_case__	,snake_case__	,snake_case__		)							->  list[int]:






						"""simple docstring"""





						_SCREAMING_SNAKE_CASE     				=  True
						_SCREAMING_SNAKE_CASE     				=  []

						for neighbour in graph[vert]:
												if not visited[neighbour]:
																		order += topology_sort(snake_case__	,snake_case__	,snake_case__		)

						order.append(snake_case__		)

						return order

def 	__lowerCamelCase      (		snake_case__	,snake_case__	,snake_case__		)							->  list[int]:






						"""simple docstring"""





						_SCREAMING_SNAKE_CASE     				=  True
						_SCREAMING_SNAKE_CASE     				=  [vert]

						for neighbour in reversed_graph[vert]:
												if not visited[neighbour]:
																		component += find_components(snake_case__	,snake_case__	,snake_case__		)

						return component




def 	__lowerCamelCase      (		snake_case__		)							->  list[list[int]]:






						"""simple docstring"""





						_SCREAMING_SNAKE_CASE     				=  len(snake_case__		) * [False]
						_SCREAMING_SNAKE_CASE     				=  {vert: [] for vert in range(len(snake_case__		)		)}

						for vert, neighbours in graph.items():
												for neighbour in neighbours:
																		reversed_graph[neighbour].append(snake_case__		)

						_SCREAMING_SNAKE_CASE     				=  []
						for i, was_visited in enumerate(snake_case__		):
												if not was_visited:
																		order += topology_sort(snake_case__	,snake_case__	,snake_case__		)

						_SCREAMING_SNAKE_CASE     				=  []
						_SCREAMING_SNAKE_CASE     				=  len(snake_case__		) * [False]

						for i in range(len(snake_case__		)		):
												_SCREAMING_SNAKE_CASE     				=  order[len(snake_case__		) - i - 1]
												if not visited[vert]:
																		_SCREAMING_SNAKE_CASE     				=  find_components(snake_case__	,snake_case__	,snake_case__		)
																		components_list.append(snake_case__		)

						return components_list

