





import shutil
import tempfile
import unittest
from unittest.mock import patch

from transformers import (
    DefaultFlowCallback,
    IntervalStrategy,
    PrinterCallback,
    ProgressCallback,
    Trainer,
    TrainerCallback,
    TrainingArguments,
    is_torch_available,
)
from transformers.testing_utils import require_torch


if is_torch_available():
				from transformers.trainer import DEFAULT_CALLBACKS

				from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel
class        snake_case_		(			_A):







		def __init__(		self   )				->   str:
				lowerCamelCase   : Union[str, Any]								=[]







		def 			__lowercase   (		self			,     __lowercase			,     __lowercase			,     __lowercase			,     **__lowercase   )				->   List[Any]:
				self.events.append('''on_init_end'''   )







		def 			__lowercase   (		self			,     __lowercase			,     __lowercase			,     __lowercase			,     **__lowercase   )				->   Optional[Any]:
				self.events.append('''on_train_begin'''   )







		def 			__lowercase   (		self			,     __lowercase			,     __lowercase			,     __lowercase			,     **__lowercase   )				->   str:
				self.events.append('''on_train_end'''   )







		def 			__lowercase   (		self			,     __lowercase			,     __lowercase			,     __lowercase			,     **__lowercase   )				->   List[str]:
				self.events.append('''on_epoch_begin'''   )







		def 			__lowercase   (		self			,     __lowercase			,     __lowercase			,     __lowercase			,     **__lowercase   )				->   Dict:
				self.events.append('''on_epoch_end'''   )







		def 			__lowercase   (		self			,     __lowercase			,     __lowercase			,     __lowercase			,     **__lowercase   )				->   int:
				self.events.append('''on_step_begin'''   )







		def 			__lowercase   (		self			,     __lowercase			,     __lowercase			,     __lowercase			,     **__lowercase   )				->   int:
				self.events.append('''on_step_end'''   )







		def 			__lowercase   (		self			,     __lowercase			,     __lowercase			,     __lowercase			,     **__lowercase   )				->   List[Any]:
				self.events.append('''on_evaluate'''   )







		def 			__lowercase   (		self			,     __lowercase			,     __lowercase			,     __lowercase			,     **__lowercase   )				->   List[str]:
				self.events.append('''on_predict'''   )







		def 			__lowercase   (		self			,     __lowercase			,     __lowercase			,     __lowercase			,     **__lowercase   )				->   str:
				self.events.append('''on_save'''   )







		def 			__lowercase   (		self			,     __lowercase			,     __lowercase			,     __lowercase			,     **__lowercase   )				->   Any:
				self.events.append('''on_log'''   )







		def 			__lowercase   (		self			,     __lowercase			,     __lowercase			,     __lowercase			,     **__lowercase   )				->   Union[str, Any]:
				self.events.append('''on_prediction_step'''   )

@require_torch
class        snake_case_		(			unittest.TestCase):







		def 			__lowercase   (		self   )				->   Tuple:
				lowerCamelCase   : str								=tempfile.mkdtemp()







		def 			__lowercase   (		self   )				->   List[Any]:
				shutil.rmtree(self.output_dir   )







		def 			__lowercase   (		self			,     __lowercase=0			,     __lowercase=0			,     __lowercase=6_4			,     __lowercase=6_4			,     __lowercase=None			,     __lowercase=False			,     **__lowercase   )				->   List[str]:
				# disable_tqdm in TrainingArguments has a flaky default since it depends on the level of logging. We make sure
				# its set to False since the tests later on depend on its value.
				lowerCamelCase   : int								=RegressionDataset(length=__lowercase   )
				lowerCamelCase   : str								=RegressionDataset(length=__lowercase   )
				lowerCamelCase   : Union[str, Any]								=RegressionModelConfig(a=__lowercase			,     b=__lowercase   )
				lowerCamelCase   : int								=RegressionPreTrainedModel(__lowercase   )

				lowerCamelCase   : Optional[int]								=TrainingArguments(self.output_dir			,     disable_tqdm=__lowercase			,     report_to=[]			,     **__lowercase   )
				return Trainer(
				    __lowercase			,     __lowercase			,     train_dataset=__lowercase			,     eval_dataset=__lowercase			,     callbacks=__lowercase			,     )







		def 			__lowercase   (		self			,     __lowercase			,     __lowercase   )				->   List[str]:
				self.assertEqual(len(__lowercase   )			,     len(__lowercase   )   )

				# Order doesn't matter
				lowerCamelCase   : List[str]								=sorted(__lowercase			,     key=lambda __lowercase   : cb.__name__ if isinstance(__lowercase			,     __lowercase   ) else cb.__class__.__name__   )
				lowerCamelCase   : Tuple								=sorted(__lowercase			,     key=lambda __lowercase   : cb.__name__ if isinstance(__lowercase			,     __lowercase   ) else cb.__class__.__name__   )

				for cba, cba in zip(__lowercase			,     __lowercase   ):
						if isinstance(__lowercase			,     __lowercase   ) and isinstance(__lowercase			,     __lowercase   ):
								self.assertEqual(__lowercase			,     __lowercase   )
						elif isinstance(__lowercase			,     __lowercase   ) and not isinstance(__lowercase			,     __lowercase   ):
								self.assertEqual(__lowercase			,     cba.__class__   )
						elif not isinstance(__lowercase			,     __lowercase   ) and isinstance(__lowercase			,     __lowercase   ):
								self.assertEqual(cba.__class__			,     __lowercase   )
						else:
								self.assertEqual(__lowercase			,     __lowercase   )







		def 			__lowercase   (		self			,     __lowercase   )				->   int:
				lowerCamelCase   : List[str]								=['''on_init_end''', '''on_train_begin''']
				lowerCamelCase   : Optional[Any]								=0
				lowerCamelCase   : Dict								=len(trainer.get_eval_dataloader()   )
				lowerCamelCase   : Union[str, Any]								=['''on_prediction_step'''] * len(trainer.get_eval_dataloader()   ) + ['''on_log''', '''on_evaluate''']
				for _ in range(trainer.state.num_train_epochs   ):
						expected_events.append('''on_epoch_begin'''   )
						for _ in range(__lowercase   ):
								step += 1
								expected_events += ["on_step_begin", "on_step_end"]
								if step % trainer.args.logging_steps == 0:
										expected_events.append('''on_log'''   )
								if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0:
										expected_events += evaluation_events.copy()
								if step % trainer.args.save_steps == 0:
										expected_events.append('''on_save'''   )
						expected_events.append('''on_epoch_end'''   )
						if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH:
								expected_events += evaluation_events.copy()
				expected_events += ["on_log", "on_train_end"]
				return expected_events







		def 			__lowercase   (		self   )				->   int:
				lowerCamelCase   : Optional[int]								=self.get_trainer()
				lowerCamelCase   : List[Any]								=DEFAULT_CALLBACKS.copy() + [ProgressCallback]
				self.check_callbacks_equality(trainer.callback_handler.callbacks			,     __lowercase   )

				# Callbacks passed at init are added to the default callbacks
				lowerCamelCase   : str								=self.get_trainer(callbacks=[MyTestTrainerCallback]   )
				expected_callbacks.append(__lowercase   )
				self.check_callbacks_equality(trainer.callback_handler.callbacks			,     __lowercase   )

				# TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback
				lowerCamelCase   : int								=self.get_trainer(disable_tqdm=__lowercase   )
				lowerCamelCase   : Dict								=DEFAULT_CALLBACKS.copy() + [PrinterCallback]
				self.check_callbacks_equality(trainer.callback_handler.callbacks			,     __lowercase   )







		def 			__lowercase   (		self   )				->   Tuple:
				lowerCamelCase   : Optional[Any]								=DEFAULT_CALLBACKS.copy() + [ProgressCallback]
				lowerCamelCase   : List[str]								=self.get_trainer()

				# We can add, pop, or remove by class name
				trainer.remove_callback(__lowercase   )
				expected_callbacks.remove(__lowercase   )
				self.check_callbacks_equality(trainer.callback_handler.callbacks			,     __lowercase   )

				lowerCamelCase   : Union[str, Any]								=self.get_trainer()
				lowerCamelCase   : Union[str, Any]								=trainer.pop_callback(__lowercase   )
				self.assertEqual(cb.__class__			,     __lowercase   )
				self.check_callbacks_equality(trainer.callback_handler.callbacks			,     __lowercase   )

				trainer.add_callback(__lowercase   )
				expected_callbacks.insert(0			,     __lowercase   )
				self.check_callbacks_equality(trainer.callback_handler.callbacks			,     __lowercase   )

				# We can also add, pop, or remove by instance
				lowerCamelCase   : Any								=self.get_trainer()
				lowerCamelCase   : Optional[int]								=trainer.callback_handler.callbacks[0]
				trainer.remove_callback(__lowercase   )
				expected_callbacks.remove(__lowercase   )
				self.check_callbacks_equality(trainer.callback_handler.callbacks			,     __lowercase   )

				lowerCamelCase   : Tuple								=self.get_trainer()
				lowerCamelCase   : Any								=trainer.callback_handler.callbacks[0]
				lowerCamelCase   : Any								=trainer.pop_callback(__lowercase   )
				self.assertEqual(__lowercase			,     __lowercase   )
				self.check_callbacks_equality(trainer.callback_handler.callbacks			,     __lowercase   )

				trainer.add_callback(__lowercase   )
				expected_callbacks.insert(0			,     __lowercase   )
				self.check_callbacks_equality(trainer.callback_handler.callbacks			,     __lowercase   )







		def 			__lowercase   (		self   )				->   Any:
				import warnings

				# XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested
				warnings.simplefilter(action='''ignore'''			,     category=__lowercase   )

				lowerCamelCase   : Dict								=self.get_trainer(callbacks=[MyTestTrainerCallback]   )
				trainer.train()
				lowerCamelCase   : str								=trainer.callback_handler.callbacks[-2].events
				self.assertEqual(__lowercase			,     self.get_expected_events(__lowercase   )   )

				# Independent log/save/eval
				lowerCamelCase   : Any								=self.get_trainer(callbacks=[MyTestTrainerCallback]			,     logging_steps=5   )
				trainer.train()
				lowerCamelCase   : Union[str, Any]								=trainer.callback_handler.callbacks[-2].events
				self.assertEqual(__lowercase			,     self.get_expected_events(__lowercase   )   )

				lowerCamelCase   : Optional[Any]								=self.get_trainer(callbacks=[MyTestTrainerCallback]			,     save_steps=5   )
				trainer.train()
				lowerCamelCase   : List[str]								=trainer.callback_handler.callbacks[-2].events
				self.assertEqual(__lowercase			,     self.get_expected_events(__lowercase   )   )

				lowerCamelCase   : Optional[Any]								=self.get_trainer(callbacks=[MyTestTrainerCallback]			,     eval_steps=5			,     evaluation_strategy='''steps'''   )
				trainer.train()
				lowerCamelCase   : Union[str, Any]								=trainer.callback_handler.callbacks[-2].events
				self.assertEqual(__lowercase			,     self.get_expected_events(__lowercase   )   )

				lowerCamelCase   : Tuple								=self.get_trainer(callbacks=[MyTestTrainerCallback]			,     evaluation_strategy='''epoch'''   )
				trainer.train()
				lowerCamelCase   : str								=trainer.callback_handler.callbacks[-2].events
				self.assertEqual(__lowercase			,     self.get_expected_events(__lowercase   )   )

				# A bit of everything
				lowerCamelCase   : Any								=self.get_trainer(
				    callbacks=[MyTestTrainerCallback]			,     logging_steps=3			,     save_steps=1_0			,     eval_steps=5			,     evaluation_strategy='''steps'''			,     )
				trainer.train()
				lowerCamelCase   : Union[str, Any]								=trainer.callback_handler.callbacks[-2].events
				self.assertEqual(__lowercase			,     self.get_expected_events(__lowercase   )   )

				# warning should be emitted for duplicated callbacks
				with patch('''transformers.trainer_callback.logger.warning'''   ) as warn_mock:
						lowerCamelCase   : Tuple								=self.get_trainer(
						    callbacks=[MyTestTrainerCallback, MyTestTrainerCallback]			,     )
						assert str(__lowercase   ) in warn_mock.call_args[0][0]








import string


def 					A__	(       SCREAMING_SNAKE_CASE_		)   ->     str:
		lowerCamelCase   : Optional[Any]								=''''''
		for i in sequence:
				lowerCamelCase   : int								=ord(SCREAMING_SNAKE_CASE_		)
				if 6_5 <= extract <= 9_0:
						output += chr(1_5_5 - extract		)
				elif 9_7 <= extract <= 1_2_2:
						output += chr(2_1_9 - extract		)
				else:
						output += i
		return output


def 					A__	(       SCREAMING_SNAKE_CASE_		)   ->     str:
		lowerCamelCase   : Tuple								=string.ascii_letters
		lowerCamelCase   : int								=string.ascii_lowercase[::-1] + string.ascii_uppercase[::-1]
		return "".join(
		    letters_reversed[letters.index(SCREAMING_SNAKE_CASE_		)] if c in letters else c for c in sequence		)


def 					A__	(       )   ->     None:
		from timeit import timeit

		print('''Running performance benchmarks...'''		)
		lowerCamelCase   : Tuple								='''from string import printable ; from __main__ import atbash, atbash_slow'''
		print(F"> atbash_slow(): {timeit('atbash_slow(printable)'							,       setup=SCREAMING_SNAKE_CASE_		)} seconds"		)
		print(F">      atbash(): {timeit('atbash(printable)'							,       setup=SCREAMING_SNAKE_CASE_		)} seconds"		)


if __name__ == "__main__":
				for example in ("ABCDEFGH", "123GGjj", "testStringtest", "with space"):
								print(F"""{example} encrypted in atbash: {atbash(example)}""")
				benchmark()

