



import timeit


class 	lowerCamelCase__ :


						def __init__( self						:  Any      ,       _a						:  Any			):
													if isinstance(_a      ,       _a			):  # Initializes a matrix identical to the one provided.
																				a__:       Optional[int]											=arg
																				a__:       Dict											=len(_a			)
													else:  # Initializes a square matrix of the given size and set values to zero.
																				a__:       int											=arg
																				a__:       Any											=[[0 for _ in range(self.n			)] for _ in range(self.n			)]







						def __mul__( self						:  str      ,       _a						:  Union[str, Any]			):
													a__:       List[str]											=Matrix(self.n			)
													for i in range(self.n			):
																				for j in range(self.n			):
																											for k in range(self.n			):
																																		matrix.t[i][j] += self.t[i][k] * b.t[k][j]
													return matrix





def 						__lowerCamelCase       (      __magic_name__	:  Dict     , __magic_name__	:  Any						):
							a__:       List[str]											=Matrix([[1, 0], [0, 1]]						)
							while b > 0:
														if b & 1:
																					matrix *= a
														a *= a
														b >>= 1
							return matrix





def 						__lowerCamelCase       (      __magic_name__	:  Union[str, Any]     , __magic_name__	:  Any     , __magic_name__	:  str						):
							# Trivial Cases
							if n == 1:
														return fa
							elif n == 2:
														return fa
							a__:       List[Any]											=Matrix([[1, 1], [1, 0]]						)
							a__:       Union[str, Any]											=modular_exponentiation(__magic_name__     , n - 2						)
							return fa * matrix.t[0][0] + fa * matrix.t[0][1]





def 						__lowerCamelCase       (      __magic_name__	:  List[str]     , __magic_name__	:  List[Any]     , __magic_name__	:  Optional[Any]						):
							# Trivial Cases
							if n == 1:
														return fa
							elif n == 2:
														return fa

							a__:       Any											=fa
							a__:       int											=fa
							n -= 2

							while n > 0:
														a__							,			a__:       int											=fn_a + fn_a, fn_a
														n -= 1

							return fn_a





def 						__lowerCamelCase       (      ):
							a__:       List[Any]											="\nfrom random import randint\nfrom __main__ import fibonacci_with_matrix_exponentiation\n"
							a__:       int											="fibonacci_with_matrix_exponentiation(randint(1,70000), 1, 1)"
							a__:       Optional[int]											=timeit.timeit(setup=__magic_name__     , stmt=__magic_name__     , number=100						)
							print("With matrix exponentiation the average execution time is "     , exec_time / 100						)
							return exec_time





def 						__lowerCamelCase       (      ):
							a__:       Union[str, Any]											="\nfrom random import randint\nfrom __main__ import simple_fibonacci\n"
							a__:       Optional[int]											="simple_fibonacci(randint(1,70000), 1, 1)"
							a__:       Union[str, Any]											=timeit.timeit(setup=__magic_name__     , stmt=__magic_name__     , number=100						)
							print(
							    "Without matrix exponentiation the average execution time is "     , exec_time / 100						)
							return exec_time





def 						__lowerCamelCase       (      ):
							matrix_exponentiation_time()
							simple_fibonacci_time()


if __name__ == "__main__":
		main()










# This test is meant to be run in on an instance with TPUs like this:
#
#   python examples/pytorch/xla_spawn.py --num_cores=8 tests/test_trainer_tpu.py
#
# Replace 8 with the number of TPU cores you have.
#

import sys
from typing import Dict

from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available
from transformers.utils import logging


__UpperCAmelCase						  =     logging.get_logger(__name__)


if is_torch_available():
		import torch
		from torch import nn
		from torch.utils.data import Dataset

		from transformers import Trainer


		class 	lowerCamelCase__ (		_a   ):


								def __init__( self						:  Any      ,       _a						:  int = 1_0_1			):
															a__:       Dict											=length


								def __len__( self						:  Optional[Any]			):
															return self.length







								def __getitem__( self						:  Dict      ,       _a						:  int			):
															return i


		class 	lowerCamelCase__ :


								def __call__( self						:  Any      ,       _a						:  List[str]			):
															return {"input_ids": torch.tensor(_a			), "labels": torch.tensor(_a			)}


		class 	lowerCamelCase__ (		nn.Module   ):


								def __init__( self						:  Tuple			):
															super().__init__()
															# Add some (unused) params otherwise DDP will complain.
															a__:       int											=nn.Linear(1_2_0      ,       8_0			)







								def   _lowerCamelCase			( self						:  str      ,       _a						:  List[Any]      ,       _a						:  List[str]=None			):
															if labels is not None:
																						return torch.tensor(0.0      ,       device=input_ids.device			), input_ids
															else:
																						return input_ids





def 						__lowerCamelCase       (      ):
							a__:       List[str]											=HfArgumentParser((TrainingArguments,)						)
							sys.argv += ["--output_dir", "./examples"]
							a__:       int											=parser.parse_args_into_dataclasses()[0]

							logger.warning(
							    F"Process rank: {training_args.local_rank}, device: {training_args.device}, "
							    F"tpu_num_cores: {training_args.tpu_num_cores}"     , )

							# Essentially, what we want to verify in the distributed case is
							# that we get all samples back, in the right order.
							# (this is crucial for prediction for instance)
							for dataset_length in [1_001, 256, 15]:
														a__:       Optional[Any]											=DummyDataset(__magic_name__						)

														def compute_metrics(__magic_name__	:  EvalPrediction						) -> Dict:
																					a__:       List[str]											=list(range(len(__magic_name__						)						)						)
																					a__:       Union[str, Any]											=p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential
																					return {"success": success}

														a__:       Union[str, Any]											=Trainer(
														    model=DummyModel()     , args=__magic_name__     , data_collator=DummyDataCollator()     , eval_dataset=__magic_name__     , compute_metrics=__magic_name__     , )
														a__:       str											=trainer.evaluate()
														logger.info(__magic_name__						)
														if metrics["eval_success"] is not True:
																					logger.error(__magic_name__						)
																					exit(1						)

														a__:       List[str]											=trainer.predict(__magic_name__						)
														logger.info(p.metrics						)
														if p.metrics["test_success"] is not True:
																					logger.error(p.metrics						)
																					exit(1						)

														a__:       Tuple											=2

														a__:       Optional[int]											=trainer.evaluate()
														logger.info(__magic_name__						)
														if metrics["eval_success"] is not True:
																					logger.error(__magic_name__						)
																					exit(1						)

														a__:       List[str]											=trainer.predict(__magic_name__						)
														logger.info(p.metrics						)
														if p.metrics["test_success"] is not True:
																					logger.error(p.metrics						)
																					exit(1						)

														a__:       List[str]											=None

							logger.info("🔥 All distributed tests successful"						)





def 						__lowerCamelCase       (      __magic_name__	:  Optional[Any]						):
							# For xla_spawn (TPUs)
							main()


if __name__ == "__main__":
		main()





