

"""simple docstring"""




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 			lowerCAmelCase_		(a__						):


				"""simple docstring"""



				def __init__(self					)      ->	List[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Tuple     =     []



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)      ->	Any:



											"""simple docstring"""




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



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)      ->	List[str]:



											"""simple docstring"""




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



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)      ->	Dict:



											"""simple docstring"""




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



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)      ->	Union[str, Any]:



											"""simple docstring"""




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



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)      ->	Any:



											"""simple docstring"""




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



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)      ->	Dict:



											"""simple docstring"""




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



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)      ->	List[str]:



											"""simple docstring"""




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



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)      ->	List[str]:



											"""simple docstring"""




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



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)      ->	int:



											"""simple docstring"""




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



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)      ->	Dict:



											"""simple docstring"""




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



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)      ->	Optional[int]:



											"""simple docstring"""




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



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)      ->	List[Any]:



											"""simple docstring"""




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






@require_torch
class 			lowerCAmelCase_		(unittest.TestCase						):


				"""simple docstring"""



				def 			__magic_name__					(self					)      ->	Tuple:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Optional[int]     =     tempfile.mkdtemp()



				def 			__magic_name__					(self					)      ->	Dict:



											"""simple docstring"""




											shutil.rmtree(self.output_dir					)



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__=0     , SCREAMING_SNAKE_CASE__=0     , SCREAMING_SNAKE_CASE__=64     , SCREAMING_SNAKE_CASE__=64     , SCREAMING_SNAKE_CASE__=None     , SCREAMING_SNAKE_CASE__=False     , **SCREAMING_SNAKE_CASE__					)      ->	List[str]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     RegressionDataset(length=SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Tuple     =     RegressionDataset(length=SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: str     =     RegressionModelConfig(a=SCREAMING_SNAKE_CASE__     , b=SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     RegressionPreTrainedModel(SCREAMING_SNAKE_CASE__					)

											SCREAMING_SNAKE_CASE__		: Any     =     TrainingArguments(self.output_dir     , disable_tqdm=SCREAMING_SNAKE_CASE__     , report_to=[]     , **SCREAMING_SNAKE_CASE__					)
											return Trainer(
											    SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , train_dataset=SCREAMING_SNAKE_CASE__     , eval_dataset=SCREAMING_SNAKE_CASE__     , callbacks=SCREAMING_SNAKE_CASE__     , )



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)      ->	List[Any]:



											"""simple docstring"""




											self.assertEqual(len(SCREAMING_SNAKE_CASE__					)     , len(SCREAMING_SNAKE_CASE__					)					)

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

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



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__					)      ->	Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: int     =     ["""on_init_end""", """on_train_begin"""]
											SCREAMING_SNAKE_CASE__		: str     =     0
											SCREAMING_SNAKE_CASE__		: List[str]     =     len(trainer.get_eval_dataloader()					)
											SCREAMING_SNAKE_CASE__		: int     =     ["""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(SCREAMING_SNAKE_CASE__					):
																									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 			__magic_name__					(self					)      ->	Optional[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: int     =     self.get_trainer()
											SCREAMING_SNAKE_CASE__		: Any     =     DEFAULT_CALLBACKS.copy() + [ProgressCallback]
											self.check_callbacks_equality(trainer.callback_handler.callbacks     , SCREAMING_SNAKE_CASE__					)

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

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



				def 			__magic_name__					(self					)      ->	Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[Any]     =     DEFAULT_CALLBACKS.copy() + [ProgressCallback]
											SCREAMING_SNAKE_CASE__		: str     =     self.get_trainer()

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

											SCREAMING_SNAKE_CASE__		: List[Any]     =     self.get_trainer()
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     trainer.pop_callback(SCREAMING_SNAKE_CASE__					)
											self.assertEqual(cb.__class__     , SCREAMING_SNAKE_CASE__					)
											self.check_callbacks_equality(trainer.callback_handler.callbacks     , SCREAMING_SNAKE_CASE__					)

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

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

											SCREAMING_SNAKE_CASE__		: int     =     self.get_trainer()
											SCREAMING_SNAKE_CASE__		: int     =     trainer.callback_handler.callbacks[0]
											SCREAMING_SNAKE_CASE__		: int     =     trainer.pop_callback(SCREAMING_SNAKE_CASE__					)
											self.assertEqual(SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)
											self.check_callbacks_equality(trainer.callback_handler.callbacks     , SCREAMING_SNAKE_CASE__					)

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



				def 			__magic_name__					(self					)      ->	int:



											"""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=SCREAMING_SNAKE_CASE__					)

											SCREAMING_SNAKE_CASE__		: Tuple     =     self.get_trainer(callbacks=[MyTestTrainerCallback]					)
											trainer.train()
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     trainer.callback_handler.callbacks[-2].events
											self.assertEqual(SCREAMING_SNAKE_CASE__     , self.get_expected_events(SCREAMING_SNAKE_CASE__					)					)

											# Independent log/save/eval
											SCREAMING_SNAKE_CASE__		: List[Any]     =     self.get_trainer(callbacks=[MyTestTrainerCallback]     , logging_steps=5					)
											trainer.train()
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     trainer.callback_handler.callbacks[-2].events
											self.assertEqual(SCREAMING_SNAKE_CASE__     , self.get_expected_events(SCREAMING_SNAKE_CASE__					)					)

											SCREAMING_SNAKE_CASE__		: int     =     self.get_trainer(callbacks=[MyTestTrainerCallback]     , save_steps=5					)
											trainer.train()
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     trainer.callback_handler.callbacks[-2].events
											self.assertEqual(SCREAMING_SNAKE_CASE__     , self.get_expected_events(SCREAMING_SNAKE_CASE__					)					)

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

											SCREAMING_SNAKE_CASE__		: List[Any]     =     self.get_trainer(callbacks=[MyTestTrainerCallback]     , evaluation_strategy="""epoch"""					)
											trainer.train()
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     trainer.callback_handler.callbacks[-2].events
											self.assertEqual(SCREAMING_SNAKE_CASE__     , self.get_expected_events(SCREAMING_SNAKE_CASE__					)					)

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

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






"""simple docstring"""




UpperCAmelCase__   :					Dict												=				[
    (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 lowercase_						(     _snake_case		):
							SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     {"""I""": 1, """V""": 5, """X""": 10, """L""": 50, """C""": 100, """D""": 500, """M""": 1_000}
							SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     0
							SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     0
							while place < len(_snake_case		):
														if (place + 1 < len(_snake_case		)) 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 lowercase_						(     _snake_case		):
							SCREAMING_SNAKE_CASE__		: Optional[Any]     =     []
							for arabic, roman in ROMAN:
														((SCREAMING_SNAKE_CASE__)   ,   (SCREAMING_SNAKE_CASE__))		: List[Any]     =     divmod(_snake_case     ,_snake_case		)
														result.append(roman * factor		)
														if number == 0:
																					break
							return "".join(_snake_case		)


if __name__ == "__main__":
					import doctest

					doctest.testmod()




