


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__     ( A__  ):



  """simple docstring"""



  def __init__( self			:  Any       ):
        a__	:  List[Any]     =   []

  def    _UpperCamelCase( self			:  Optional[int]				,				lowerCamelCase__			:  Union[str, Any]				,				lowerCamelCase__			:  Optional[int]				,				lowerCamelCase__			:  Dict				,				**lowerCamelCase__			:  Optional[Any]       ):
        self.events.append("on_init_end"       )

  def    _UpperCamelCase( self			:  List[Any]				,				lowerCamelCase__			:  Dict				,				lowerCamelCase__			:  List[Any]				,				lowerCamelCase__			:  Any				,				**lowerCamelCase__			:  Optional[int]       ):
        self.events.append("on_train_begin"       )

  def    _UpperCamelCase( self			:  Tuple				,				lowerCamelCase__			:  str				,				lowerCamelCase__			:  Union[str, Any]				,				lowerCamelCase__			:  str				,				**lowerCamelCase__			:  Tuple       ):
        self.events.append("on_train_end"       )

  def    _UpperCamelCase( self			:  Optional[int]				,				lowerCamelCase__			:  int				,				lowerCamelCase__			:  str				,				lowerCamelCase__			:  Any				,				**lowerCamelCase__			:  Optional[int]       ):
        self.events.append("on_epoch_begin"       )

  def    _UpperCamelCase( self			:  List[Any]				,				lowerCamelCase__			:  Union[str, Any]				,				lowerCamelCase__			:  List[Any]				,				lowerCamelCase__			:  List[Any]				,				**lowerCamelCase__			:  Union[str, Any]       ):
        self.events.append("on_epoch_end"       )

  def    _UpperCamelCase( self			:  Dict				,				lowerCamelCase__			:  str				,				lowerCamelCase__			:  Union[str, Any]				,				lowerCamelCase__			:  Optional[int]				,				**lowerCamelCase__			:  int       ):
        self.events.append("on_step_begin"       )

  def    _UpperCamelCase( self			:  Tuple				,				lowerCamelCase__			:  Union[str, Any]				,				lowerCamelCase__			:  Dict				,				lowerCamelCase__			:  Optional[Any]				,				**lowerCamelCase__			:  Optional[Any]       ):
        self.events.append("on_step_end"       )

  def    _UpperCamelCase( self			:  Optional[int]				,				lowerCamelCase__			:  Dict				,				lowerCamelCase__			:  Optional[int]				,				lowerCamelCase__			:  Any				,				**lowerCamelCase__			:  int       ):
        self.events.append("on_evaluate"       )

  def    _UpperCamelCase( self			:  Tuple				,				lowerCamelCase__			:  str				,				lowerCamelCase__			:  List[str]				,				lowerCamelCase__			:  List[str]				,				**lowerCamelCase__			:  Dict       ):
        self.events.append("on_predict"       )

  def    _UpperCamelCase( self			:  int				,				lowerCamelCase__			:  Union[str, Any]				,				lowerCamelCase__			:  int				,				lowerCamelCase__			:  str				,				**lowerCamelCase__			:  int       ):
        self.events.append("on_save"       )

  def    _UpperCamelCase( self			:  Union[str, Any]				,				lowerCamelCase__			:  int				,				lowerCamelCase__			:  List[Any]				,				lowerCamelCase__			:  List[Any]				,				**lowerCamelCase__			:  Optional[int]       ):
        self.events.append("on_log"       )

  def    _UpperCamelCase( self			:  Any				,				lowerCamelCase__			:  int				,				lowerCamelCase__			:  Optional[int]				,				lowerCamelCase__			:  Tuple				,				**lowerCamelCase__			:  List[str]       ):
        self.events.append("on_prediction_step"       )




@require_torch
class A__     ( unittest.TestCase  ):



  """simple docstring"""



  def    _UpperCamelCase( self			:  Optional[Any]       ):
        a__	:  str     =   tempfile.mkdtemp()

  def    _UpperCamelCase( self			:  str       ):
        shutil.rmtree(self.output_dir       )

  def    _UpperCamelCase( self			:  List[str]				,				lowerCamelCase__			:  Optional[int]=0				,				lowerCamelCase__			:  Optional[Any]=0				,				lowerCamelCase__			:  Optional[Any]=64				,				lowerCamelCase__			:  Union[str, Any]=64				,				lowerCamelCase__			:  Optional[int]=None				,				lowerCamelCase__			:  Dict=False				,				**lowerCamelCase__			:  Optional[Any]       ):
        # 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.
        a__	:  Optional[Any]     =   RegressionDataset(length=lowerCamelCase__       )
        a__	:  Union[str, Any]     =   RegressionDataset(length=lowerCamelCase__       )
        a__	:  Dict     =   RegressionModelConfig(a=lowerCamelCase__				,				b=lowerCamelCase__       )
        a__	:  List[str]     =   RegressionPreTrainedModel(lowerCamelCase__       )

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

  def    _UpperCamelCase( self			:  Optional[int]				,				lowerCamelCase__			:  str				,				lowerCamelCase__			:  List[Any]       ):
        self.assertEqual(len(lowerCamelCase__       )				,				len(lowerCamelCase__       )       )

        # Order doesn't matter
        a__	:  Union[str, Any]     =   sorted(lowerCamelCase__				,				key=lambda lowerCamelCase__       : cb.__name__ if isinstance(lowerCamelCase__				,				lowerCamelCase__       ) else cb.__class__.__name__       )
        a__	:  Optional[Any]     =   sorted(lowerCamelCase__				,				key=lambda lowerCamelCase__       : cb.__name__ if isinstance(lowerCamelCase__				,				lowerCamelCase__       ) else cb.__class__.__name__       )

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

  def    _UpperCamelCase( self			:  Optional[Any]				,				lowerCamelCase__			:  str       ):
        a__	:  Optional[int]     =   ["on_init_end", "on_train_begin"]
        a__	:  Dict     =   0
        a__	:  List[Any]     =   len(trainer.get_eval_dataloader()       )
        a__	:  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(lowerCamelCase__       ):
                    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    _UpperCamelCase( self			:  str       ):
        a__	:  str     =   self.get_trainer()
        a__	:  int     =   DEFAULT_CALLBACKS.copy() + [ProgressCallback]
        self.check_callbacks_equality(trainer.callback_handler.callbacks				,				lowerCamelCase__       )

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

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

  def    _UpperCamelCase( self			:  Tuple       ):
        a__	:  List[str]     =   DEFAULT_CALLBACKS.copy() + [ProgressCallback]
        a__	:  str     =   self.get_trainer()

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

        a__	:  List[Any]     =   self.get_trainer()
        a__	:  Dict     =   trainer.pop_callback(lowerCamelCase__       )
        self.assertEqual(cb.__class__				,				lowerCamelCase__       )
        self.check_callbacks_equality(trainer.callback_handler.callbacks				,				lowerCamelCase__       )

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

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

        a__	:  Dict     =   self.get_trainer()
        a__	:  List[Any]     =   trainer.callback_handler.callbacks[0]
        a__	:  Optional[int]     =   trainer.pop_callback(lowerCamelCase__       )
        self.assertEqual(lowerCamelCase__				,				lowerCamelCase__       )
        self.check_callbacks_equality(trainer.callback_handler.callbacks				,				lowerCamelCase__       )

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

  def    _UpperCamelCase( self			:  str       ):
        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=lowerCamelCase__       )

        a__	:  List[str]     =   self.get_trainer(callbacks=[MyTestTrainerCallback]       )
        trainer.train()
        a__	:  List[str]     =   trainer.callback_handler.callbacks[-2].events
        self.assertEqual(lowerCamelCase__				,				self.get_expected_events(lowerCamelCase__       )       )

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

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

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

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

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

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








def       UpperCamelCase__(			UpperCamelCase__     :			str = "The quick brown fox jumps over the lazy dog"					,	)->bool:
       A__		   =		set()

       # Replace all the whitespace in our sentence
       A__		   =		input_str.replace(''' '''					,	''''''    )
       for alpha in input_str:
              if "a" <= alpha.lower() <= "z":
                     frequency.add(alpha.lower()    )
       return len(UpperCamelCase__    ) == 26


def       UpperCamelCase__(			UpperCamelCase__     :			str = "The quick brown fox jumps over the lazy dog"					,	)->bool:
       A__		   =		[False] * 26
       for char in input_str:
              if char.islower():
                     A__		   =		True
              elif char.isupper():
                     A__		   =		True
       return all(UpperCamelCase__    )


def       UpperCamelCase__(			UpperCamelCase__     :			str = "The quick brown fox jumps over the lazy dog"					,	)->bool:
       return len({char for char in input_str.lower() if char.isalpha()}    ) == 26


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

       A__		   =		'''from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest'''
       print(timeit('''is_pangram()'''					,	setup=UpperCamelCase__    )    )
       print(timeit('''is_pangram_faster()'''					,	setup=UpperCamelCase__    )    )
       print(timeit('''is_pangram_fastest()'''					,	setup=UpperCamelCase__    )    )
       # 5.348480500048026, 2.6477354579837993, 1.8470395830227062
       # 5.036091582966037, 2.644472333951853,  1.8869528750656173


if __name__ == "__main__":
       import doctest

       doctest.testmod()
       benchmark()





