





from ..utils import DummyObject, requires_backends



class        _SCREAMING_SNAKE_CASE						(     metaclass=__SCREAMING_SNAKE_CASE   ):







	'''simple docstring'''
	lowercase_		=      ["torch", "torchsde"]

	def __init__(self    :       Union[str, Any]	,			*UpperCAmelCase_    :       Dict	,			**UpperCAmelCase_    :       Tuple)	->Any:



			'''simple docstring'''



			requires_backends(self	,			["torch", "torchsde"])

	@classmethod
	def 							SCREAMING_SNAKE_CASE_ (cls    :       Dict	,			*UpperCAmelCase_    :       Optional[int]	,			**UpperCAmelCase_    :       Optional[int])	->List[Any]:



			'''simple docstring'''



			requires_backends(cls	,			["torch", "torchsde"])







	@classmethod
	def 							SCREAMING_SNAKE_CASE_ (cls    :       Tuple	,			*UpperCAmelCase_    :       Tuple	,			**UpperCAmelCase_    :       Any)	->Any:



			'''simple docstring'''



			requires_backends(cls	,			["torch", "torchsde"])








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        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''

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



   '''simple docstring'''



   lowerCamelCase__:				List[Any]        =[]

 def 							SCREAMING_SNAKE_CASE_ (self    :       Any	,			UpperCAmelCase_    :       int	,			UpperCAmelCase_    :       str	,			UpperCAmelCase_    :       Dict	,			**UpperCAmelCase_    :       Any)	->Dict:



   '''simple docstring'''



   self.events.append("on_init_end")

 def 							SCREAMING_SNAKE_CASE_ (self    :       Union[str, Any]	,			UpperCAmelCase_    :       Any	,			UpperCAmelCase_    :       Dict	,			UpperCAmelCase_    :       Dict	,			**UpperCAmelCase_    :       str)	->List[str]:



   '''simple docstring'''



   self.events.append("on_train_begin")

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



   '''simple docstring'''



   self.events.append("on_train_end")

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



   '''simple docstring'''



   self.events.append("on_epoch_begin")

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[int]	,			UpperCAmelCase_    :       Dict	,			UpperCAmelCase_    :       List[Any]	,			UpperCAmelCase_    :       int	,			**UpperCAmelCase_    :       Any)	->Tuple:



   '''simple docstring'''



   self.events.append("on_epoch_end")

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[Any]	,			UpperCAmelCase_    :       Dict	,			UpperCAmelCase_    :       Any	,			UpperCAmelCase_    :       Any	,			**UpperCAmelCase_    :       List[Any])	->Optional[int]:



   '''simple docstring'''



   self.events.append("on_step_begin")

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str]	,			UpperCAmelCase_    :       List[str]	,			UpperCAmelCase_    :       List[str]	,			UpperCAmelCase_    :       Optional[int]	,			**UpperCAmelCase_    :       List[str])	->Tuple:



   '''simple docstring'''



   self.events.append("on_step_end")

 def 							SCREAMING_SNAKE_CASE_ (self    :       Union[str, Any]	,			UpperCAmelCase_    :       Optional[Any]	,			UpperCAmelCase_    :       Dict	,			UpperCAmelCase_    :       Optional[Any]	,			**UpperCAmelCase_    :       str)	->Optional[int]:



   '''simple docstring'''



   self.events.append("on_evaluate")

 def 							SCREAMING_SNAKE_CASE_ (self    :       Tuple	,			UpperCAmelCase_    :       List[str]	,			UpperCAmelCase_    :       List[Any]	,			UpperCAmelCase_    :       Union[str, Any]	,			**UpperCAmelCase_    :       Any)	->int:



   '''simple docstring'''



   self.events.append("on_predict")

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



   '''simple docstring'''



   self.events.append("on_save")

 def 							SCREAMING_SNAKE_CASE_ (self    :       str	,			UpperCAmelCase_    :       Any	,			UpperCAmelCase_    :       List[str]	,			UpperCAmelCase_    :       Any	,			**UpperCAmelCase_    :       Optional[Any])	->str:



   '''simple docstring'''



   self.events.append("on_log")







 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[Any]	,			UpperCAmelCase_    :       Any	,			UpperCAmelCase_    :       List[Any]	,			UpperCAmelCase_    :       str	,			**UpperCAmelCase_    :       List[str])	->Optional[int]:



   '''simple docstring'''



   self.events.append("on_prediction_step")



@require_torch
class        _SCREAMING_SNAKE_CASE						(     unittest.TestCase   ):







 '''simple docstring'''

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[Any])	->int:



   '''simple docstring'''



   lowerCamelCase__:				Tuple        =tempfile.mkdtemp()

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str])	->Tuple:



   '''simple docstring'''



   shutil.rmtree(self.output_dir)

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



   '''simple docstring'''



   lowerCamelCase__:				Union[str, Any]        =RegressionDataset(length=UpperCAmelCase_)
   lowerCamelCase__:				int        =RegressionDataset(length=UpperCAmelCase_)
   lowerCamelCase__:				str        =RegressionModelConfig(a=UpperCAmelCase_	,			b=UpperCAmelCase_)
   lowerCamelCase__:				Union[str, Any]        =RegressionPreTrainedModel(UpperCAmelCase_)

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

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str]	,			UpperCAmelCase_    :       str	,			UpperCAmelCase_    :       List[str])	->Dict:



   '''simple docstring'''



   self.assertEqual(len(UpperCAmelCase_)	,			len(UpperCAmelCase_))

   # Order doesn't matter
   lowerCamelCase__:				Dict        =sorted(UpperCAmelCase_	,			key=lambda UpperCAmelCase_: cb.__name__ if isinstance(UpperCAmelCase_	,			UpperCAmelCase_) else cb.__class__.__name__)
   lowerCamelCase__:				Optional[int]        =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 							SCREAMING_SNAKE_CASE_ (self    :       List[Any]	,			UpperCAmelCase_    :       Optional[Any])	->Optional[int]:



   '''simple docstring'''



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



   '''simple docstring'''



   lowerCamelCase__:				Optional[Any]        =self.get_trainer()
   lowerCamelCase__:				Any        =DEFAULT_CALLBACKS.copy() + [ProgressCallback]
   self.check_callbacks_equality(trainer.callback_handler.callbacks	,			UpperCAmelCase_)

   # Callbacks passed at init are added to the default callbacks
   lowerCamelCase__:				Dict        =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
   lowerCamelCase__:				int        =self.get_trainer(disable_tqdm=UpperCAmelCase_)
   lowerCamelCase__:				Tuple        =DEFAULT_CALLBACKS.copy() + [PrinterCallback]
   self.check_callbacks_equality(trainer.callback_handler.callbacks	,			UpperCAmelCase_)

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



   '''simple docstring'''



   lowerCamelCase__:				Union[str, Any]        =DEFAULT_CALLBACKS.copy() + [ProgressCallback]
   lowerCamelCase__:				Optional[int]        =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_)

   lowerCamelCase__:				Dict        =self.get_trainer()
   lowerCamelCase__:				str        =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
   lowerCamelCase__:				List[str]        =self.get_trainer()
   lowerCamelCase__:				List[str]        =trainer.callback_handler.callbacks[0]
   trainer.remove_callback(UpperCAmelCase_)
   expected_callbacks.remove(UpperCAmelCase_)
   self.check_callbacks_equality(trainer.callback_handler.callbacks	,			UpperCAmelCase_)

   lowerCamelCase__:				str        =self.get_trainer()
   lowerCamelCase__:				List[Any]        =trainer.callback_handler.callbacks[0]
   lowerCamelCase__:				Dict        =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 							SCREAMING_SNAKE_CASE_ (self    :       List[Any])	->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=UpperCAmelCase_)

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

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

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

   lowerCamelCase__:				int        =self.get_trainer(callbacks=[MyTestTrainerCallback]	,			eval_steps=5	,			evaluation_strategy="steps")
   trainer.train()
   lowerCamelCase__:				str        =trainer.callback_handler.callbacks[-2].events
   self.assertEqual(UpperCAmelCase_	,			self.get_expected_events(UpperCAmelCase_))

   lowerCamelCase__:				Dict        =self.get_trainer(callbacks=[MyTestTrainerCallback]	,			evaluation_strategy="epoch")
   trainer.train()
   lowerCamelCase__:				Tuple        =trainer.callback_handler.callbacks[-2].events
   self.assertEqual(UpperCAmelCase_	,			self.get_expected_events(UpperCAmelCase_))

   # A bit of everything
   lowerCamelCase__:				Tuple        =self.get_trainer(
       callbacks=[MyTestTrainerCallback]	,			logging_steps=3	,			save_steps=10	,			eval_steps=5	,			evaluation_strategy="steps"	,			)
   trainer.train()
   lowerCamelCase__:				int        =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:
     lowerCamelCase__:				Optional[int]        =self.get_trainer(
         callbacks=[MyTestTrainerCallback, MyTestTrainerCallback]	,			)
     assert str(UpperCAmelCase_) in warn_mock.call_args[0][0]

