






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 							A   (	UpperCAmelCase__							):
					'''simple docstring'''






					def __init__(self   :						Optional[int]		)						->       Dict:




							"""simple docstring"""


							lowercase__					      =		[]

					def   lowerCamelCase__					(self   :						List[str]				, _UpperCAmelCase   :						List[str]				, _UpperCAmelCase   :						Any				, _UpperCAmelCase   :						Optional[int]				, **_UpperCAmelCase   :						Tuple		)						->       Optional[Any]:




							"""simple docstring"""


							self.events.append("""on_init_end"""		)

					def   lowerCamelCase__					(self   :						Optional[int]				, _UpperCAmelCase   :						Tuple				, _UpperCAmelCase   :						List[Any]				, _UpperCAmelCase   :						Optional[Any]				, **_UpperCAmelCase   :						int		)						->       Union[str, Any]:




							"""simple docstring"""


							self.events.append("""on_train_begin"""		)

					def   lowerCamelCase__					(self   :						str				, _UpperCAmelCase   :						str				, _UpperCAmelCase   :						List[str]				, _UpperCAmelCase   :						int				, **_UpperCAmelCase   :						int		)						->       str:




							"""simple docstring"""


							self.events.append("""on_train_end"""		)

					def   lowerCamelCase__					(self   :						str				, _UpperCAmelCase   :						Optional[int]				, _UpperCAmelCase   :						Any				, _UpperCAmelCase   :						Optional[int]				, **_UpperCAmelCase   :						int		)						->       int:




							"""simple docstring"""


							self.events.append("""on_epoch_begin"""		)

					def   lowerCamelCase__					(self   :						Optional[int]				, _UpperCAmelCase   :						str				, _UpperCAmelCase   :						int				, _UpperCAmelCase   :						List[str]				, **_UpperCAmelCase   :						List[str]		)						->       List[str]:




							"""simple docstring"""


							self.events.append("""on_epoch_end"""		)

					def   lowerCamelCase__					(self   :						Optional[Any]				, _UpperCAmelCase   :						Tuple				, _UpperCAmelCase   :						Optional[int]				, _UpperCAmelCase   :						List[str]				, **_UpperCAmelCase   :						Union[str, Any]		)						->       int:




							"""simple docstring"""


							self.events.append("""on_step_begin"""		)

					def   lowerCamelCase__					(self   :						Any				, _UpperCAmelCase   :						Tuple				, _UpperCAmelCase   :						str				, _UpperCAmelCase   :						str				, **_UpperCAmelCase   :						str		)						->       Optional[int]:




							"""simple docstring"""


							self.events.append("""on_step_end"""		)

					def   lowerCamelCase__					(self   :						Any				, _UpperCAmelCase   :						int				, _UpperCAmelCase   :						Dict				, _UpperCAmelCase   :						str				, **_UpperCAmelCase   :						List[Any]		)						->       Optional[Any]:




							"""simple docstring"""


							self.events.append("""on_evaluate"""		)

					def   lowerCamelCase__					(self   :						Optional[int]				, _UpperCAmelCase   :						Dict				, _UpperCAmelCase   :						List[Any]				, _UpperCAmelCase   :						List[str]				, **_UpperCAmelCase   :						Optional[int]		)						->       int:




							"""simple docstring"""


							self.events.append("""on_predict"""		)

					def   lowerCamelCase__					(self   :						Tuple				, _UpperCAmelCase   :						Optional[int]				, _UpperCAmelCase   :						Union[str, Any]				, _UpperCAmelCase   :						List[str]				, **_UpperCAmelCase   :						Optional[int]		)						->       List[str]:




							"""simple docstring"""


							self.events.append("""on_save"""		)

					def   lowerCamelCase__					(self   :						Union[str, Any]				, _UpperCAmelCase   :						Union[str, Any]				, _UpperCAmelCase   :						Tuple				, _UpperCAmelCase   :						Any				, **_UpperCAmelCase   :						str		)						->       Tuple:




							"""simple docstring"""


							self.events.append("""on_log"""		)


					def   lowerCamelCase__					(self   :						Any				, _UpperCAmelCase   :						List[Any]				, _UpperCAmelCase   :						Union[str, Any]				, _UpperCAmelCase   :						Dict				, **_UpperCAmelCase   :						Any		)						->       int:




							"""simple docstring"""


							self.events.append("""on_prediction_step"""		)






@require_torch
class 							A   (	unittest.TestCase							):
					'''simple docstring'''






					def   lowerCamelCase__					(self   :						int		)						->       int:




							"""simple docstring"""


							lowercase__					      =		tempfile.mkdtemp()

					def   lowerCamelCase__					(self   :						Union[str, Any]		)						->       Union[str, Any]:




							"""simple docstring"""


							shutil.rmtree(self.output_dir		)

					def   lowerCamelCase__					(self   :						int				, _UpperCAmelCase   :						Any=0				, _UpperCAmelCase   :						List[Any]=0				, _UpperCAmelCase   :						Any=64				, _UpperCAmelCase   :						Tuple=64				, _UpperCAmelCase   :						int=None				, _UpperCAmelCase   :						Union[str, Any]=False				, **_UpperCAmelCase   :						int		)						->       str:




							"""simple docstring"""


							lowercase__					      =		RegressionDataset(length=_UpperCAmelCase		)
							lowercase__					      =		RegressionDataset(length=_UpperCAmelCase		)
							lowercase__					      =		RegressionModelConfig(a=_UpperCAmelCase				, b=_UpperCAmelCase		)
							lowercase__					      =		RegressionPreTrainedModel(_UpperCAmelCase		)

							lowercase__					      =		TrainingArguments(self.output_dir				, disable_tqdm=_UpperCAmelCase				, report_to=[]				, **_UpperCAmelCase		)
							return Trainer(
							    _UpperCAmelCase				, _UpperCAmelCase				, train_dataset=_UpperCAmelCase				, eval_dataset=_UpperCAmelCase				, callbacks=_UpperCAmelCase				, )

					def   lowerCamelCase__					(self   :						List[Any]				, _UpperCAmelCase   :						Dict				, _UpperCAmelCase   :						Any		)						->       Tuple:




							"""simple docstring"""


							self.assertEqual(len(_UpperCAmelCase		)				, len(_UpperCAmelCase		)		)

							# Order doesn't matter
							lowercase__					      =		sorted(_UpperCAmelCase				, key=lambda _UpperCAmelCase		: cb.__name__ if isinstance(_UpperCAmelCase				, _UpperCAmelCase		) else cb.__class__.__name__		)
							lowercase__					      =		sorted(_UpperCAmelCase				, key=lambda _UpperCAmelCase		: cb.__name__ if isinstance(_UpperCAmelCase				, _UpperCAmelCase		) else cb.__class__.__name__		)

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

					def   lowerCamelCase__					(self   :						Union[str, Any]				, _UpperCAmelCase   :						Optional[Any]		)						->       Optional[int]:




							"""simple docstring"""


							lowercase__					      =		["""on_init_end""", """on_train_begin"""]
							lowercase__					      =		0
							lowercase__					      =		len(trainer.get_eval_dataloader()		)
							lowercase__					      =		["""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(_UpperCAmelCase		):
											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   lowerCamelCase__					(self   :						Union[str, Any]		)						->       int:




							"""simple docstring"""


							lowercase__					      =		self.get_trainer()
							lowercase__					      =		DEFAULT_CALLBACKS.copy() + [ProgressCallback]
							self.check_callbacks_equality(trainer.callback_handler.callbacks				, _UpperCAmelCase		)

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

							# TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback
							lowercase__					      =		self.get_trainer(disable_tqdm=_UpperCAmelCase		)
							lowercase__					      =		DEFAULT_CALLBACKS.copy() + [PrinterCallback]
							self.check_callbacks_equality(trainer.callback_handler.callbacks				, _UpperCAmelCase		)

					def   lowerCamelCase__					(self   :						List[str]		)						->       List[str]:




							"""simple docstring"""


							lowercase__					      =		DEFAULT_CALLBACKS.copy() + [ProgressCallback]
							lowercase__					      =		self.get_trainer()

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

							lowercase__					      =		self.get_trainer()
							lowercase__					      =		trainer.pop_callback(_UpperCAmelCase		)
							self.assertEqual(cb.__class__				, _UpperCAmelCase		)
							self.check_callbacks_equality(trainer.callback_handler.callbacks				, _UpperCAmelCase		)

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

							# We can also add, pop, or remove by instance
							lowercase__					      =		self.get_trainer()
							lowercase__					      =		trainer.callback_handler.callbacks[0]
							trainer.remove_callback(_UpperCAmelCase		)
							expected_callbacks.remove(_UpperCAmelCase		)
							self.check_callbacks_equality(trainer.callback_handler.callbacks				, _UpperCAmelCase		)

							lowercase__					      =		self.get_trainer()
							lowercase__					      =		trainer.callback_handler.callbacks[0]
							lowercase__					      =		trainer.pop_callback(_UpperCAmelCase		)
							self.assertEqual(_UpperCAmelCase				, _UpperCAmelCase		)
							self.check_callbacks_equality(trainer.callback_handler.callbacks				, _UpperCAmelCase		)

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


					def   lowerCamelCase__					(self   :						str		)						->       List[Any]:




							"""simple docstring"""


							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=_UpperCAmelCase		)

							lowercase__					      =		self.get_trainer(callbacks=[MyTestTrainerCallback]		)
							trainer.train()
							lowercase__					      =		trainer.callback_handler.callbacks[-2].events
							self.assertEqual(_UpperCAmelCase				, self.get_expected_events(_UpperCAmelCase		)		)

							# Independent log/save/eval
							lowercase__					      =		self.get_trainer(callbacks=[MyTestTrainerCallback]				, logging_steps=5		)
							trainer.train()
							lowercase__					      =		trainer.callback_handler.callbacks[-2].events
							self.assertEqual(_UpperCAmelCase				, self.get_expected_events(_UpperCAmelCase		)		)

							lowercase__					      =		self.get_trainer(callbacks=[MyTestTrainerCallback]				, save_steps=5		)
							trainer.train()
							lowercase__					      =		trainer.callback_handler.callbacks[-2].events
							self.assertEqual(_UpperCAmelCase				, self.get_expected_events(_UpperCAmelCase		)		)

							lowercase__					      =		self.get_trainer(callbacks=[MyTestTrainerCallback]				, eval_steps=5				, evaluation_strategy="""steps"""		)
							trainer.train()
							lowercase__					      =		trainer.callback_handler.callbacks[-2].events
							self.assertEqual(_UpperCAmelCase				, self.get_expected_events(_UpperCAmelCase		)		)

							lowercase__					      =		self.get_trainer(callbacks=[MyTestTrainerCallback]				, evaluation_strategy="""epoch"""		)
							trainer.train()
							lowercase__					      =		trainer.callback_handler.callbacks[-2].events
							self.assertEqual(_UpperCAmelCase				, self.get_expected_events(_UpperCAmelCase		)		)

							# A bit of everything
							lowercase__					      =		self.get_trainer(
							    callbacks=[MyTestTrainerCallback]				, logging_steps=3				, save_steps=10				, eval_steps=5				, evaluation_strategy="""steps"""				, )
							trainer.train()
							lowercase__					      =		trainer.callback_handler.callbacks[-2].events
							self.assertEqual(_UpperCAmelCase				, self.get_expected_events(_UpperCAmelCase		)		)

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





A	:		Union[str, Any]    =       [
    (1_0_0_0, 'M'),
    (9_0_0, 'CM'),
    (5_0_0, 'D'),
    (4_0_0, 'CD'),
    (1_0_0, 'C'),
    (9_0, 'XC'),
    (5_0, 'L'),
    (4_0, 'XL'),
    (1_0, 'X'),
    (9, 'IX'),
    (5, 'V'),
    (4, 'IV'),
    (1, 'I'),
]






def 			UpperCamelCase (     __magic_name__					:							str					)       ->  int:
		"""simple docstring"""




		lowercase__					      =		{"""I""": 1, """V""": 5, """X""": 10, """L""": 50, """C""": 100, """D""": 500, """M""": 1000}
		lowercase__					      =		0
		lowercase__					      =		0
		while place < len(__magic_name__					):
				if (place + 1 < len(__magic_name__					)) and (vals[roman[place]] < vals[roman[place + 1]]):
						total += vals[roman[place + 1]] - vals[roman[place]]
						place += 2
				else:
						total += vals[roman[place]]
						place += 1
		return total






def 			UpperCamelCase (     __magic_name__					:							int					)       ->  str:
		"""simple docstring"""




		lowercase__					      =		[]
		for arabic, roman in ROMAN:
				((lowercase__)      ,     (lowercase__))					      =		divmod(__magic_name__   ,   __magic_name__					)
				result.append(roman * factor					)
				if number == 0:
						break
		return "".join(__magic_name__					)


if __name__ == "__main__":
	import doctest

	doctest.testmod()

