




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__ :   Optional[int]						=							logging.getLogger()





def        __UpperCAmelCase						(							lowerCamelCase_ :     List[str]  )       ->  Dict:







			"""simple docstring"""





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


UpperCamelCase__ :   Dict						=							logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)







@require_torch_tpu
class 				lowerCAmelCase_       (					lowerCamelCase_							):






				def 		snake_case	(			self ):
							import xla_spawn

							SCREAMING_SNAKE_CASE_   : Dict					  =      self.get_auto_remove_tmp_dir()
							SCREAMING_SNAKE_CASE_   : str					  =      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(snake_case__      ,'argv'      ,snake_case__ ):
										SCREAMING_SNAKE_CASE_   : Optional[int]					  =      time()
										xla_spawn.main()
										SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      time()

										SCREAMING_SNAKE_CASE_   : Any					  =      get_results(snake_case__ )
										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 		snake_case	(			self ):
							import xla_spawn

							SCREAMING_SNAKE_CASE_   : str					  =      '\n            ./tests/test_trainer_tpu.py\n            --num_cores=8\n            ./tests/test_trainer_tpu.py\n            '.split()
							with patch.object(snake_case__      ,'argv'      ,snake_case__ ):
										xla_spawn.main()









from __future__ import annotations

import math

import numpy as np
from numpy.linalg import norm





def        __UpperCAmelCase						(							lowerCamelCase_ :     np.ndarray  ,							lowerCamelCase_ :     np.ndarray  )       ->  float:







			"""simple docstring"""





			return math.sqrt(sum(pow(a - b  ,							2  ) for a, b in zip(lowerCamelCase_  ,							lowerCamelCase_  )  )  )





def        __UpperCAmelCase						(							lowerCamelCase_ :     np.ndarray  ,							lowerCamelCase_ :     np.ndarray  )       ->  list[list[list[float] | float]]:







			"""simple docstring"""





			if dataset.ndim != value_array.ndim:
						SCREAMING_SNAKE_CASE_   : Optional[int]					  =      (
						    'Wrong input data\'s dimensions... '
						    F'dataset : {dataset.ndim}, value_array : {value_array.ndim}'
						)
						raise ValueError(lowerCamelCase_  )

			try:
						if dataset.shape[1] != value_array.shape[1]:
									SCREAMING_SNAKE_CASE_   : List[Any]					  =      (
									    'Wrong input data\'s shape... '
									    F'dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}'
									)
									raise ValueError(lowerCamelCase_  )
			except IndexError:
						if dataset.ndim != value_array.ndim:
									raise TypeError('Wrong shape'  )

			if dataset.dtype != value_array.dtype:
						SCREAMING_SNAKE_CASE_   : Tuple					  =      (
						    'Input data have different datatype... '
						    F'dataset : {dataset.dtype}, value_array : {value_array.dtype}'
						)
						raise TypeError(lowerCamelCase_  )

			SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      []

			for value in value_array:
						SCREAMING_SNAKE_CASE_   : str					  =      euclidean(lowerCamelCase_  ,							dataset[0]  )
						SCREAMING_SNAKE_CASE_   : List[Any]					  =      dataset[0].tolist()

						for dataset_value in dataset[1:]:
									SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      euclidean(lowerCamelCase_  ,							lowerCamelCase_  )

									if dist > temp_dist:
												SCREAMING_SNAKE_CASE_   : Optional[int]					  =      temp_dist
												SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      dataset_value.tolist()

						answer.append([vector, dist]  )

			return answer





def        __UpperCAmelCase						(							lowerCamelCase_ :     np.ndarray  ,							lowerCamelCase_ :     np.ndarray  )       ->  float:







			"""simple docstring"""





			return np.dot(lowerCamelCase_  ,							lowerCamelCase_  ) / (norm(lowerCamelCase_  ) * norm(lowerCamelCase_  ))


if __name__ == "__main__":
						import doctest

						doctest.testmod()



