
'''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 						lowercase__	(  snake_case_   ):






   '''simple docstring'''







   def __init__(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  []







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      self.events.append('''on_init_end'''   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      self.events.append('''on_train_begin'''   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      self.events.append('''on_train_end'''   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      self.events.append('''on_epoch_begin'''   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      self.events.append('''on_epoch_end'''   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      self.events.append('''on_step_begin'''   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      self.events.append('''on_step_end'''   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      self.events.append('''on_evaluate'''   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      self.events.append('''on_predict'''   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      self.events.append('''on_save'''   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      self.events.append('''on_log'''   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      self.events.append('''on_prediction_step'''   )




@require_torch
class 						lowercase__	(  unittest.TestCase   ):






   '''simple docstring'''







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  tempfile.mkdtemp()







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      shutil.rmtree(self.output_dir   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__=0 ,   lowerCamelCase__=0 ,   lowerCamelCase__=6_4 ,   lowerCamelCase__=6_4 ,   lowerCamelCase__=None ,   lowerCamelCase__=False ,   **lowerCamelCase__   ):

      '''simple docstring'''







      UpperCamelCase       =  RegressionDataset(length=lowerCamelCase__   )
      UpperCamelCase       =  RegressionDataset(length=lowerCamelCase__   )
      UpperCamelCase       =  RegressionModelConfig(a=lowerCamelCase__ ,   b=lowerCamelCase__   )
      UpperCamelCase       =  RegressionPreTrainedModel(lowerCamelCase__   )

      UpperCamelCase       =  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 ,   lowerCamelCase__ ,   lowerCamelCase__   ):

      '''simple docstring'''







      self.assertEqual(len(lowerCamelCase__   ) ,   len(lowerCamelCase__   )   )

      # Order doesn't matter
      UpperCamelCase       =  sorted(lowerCamelCase__ ,   key=lambda lowerCamelCase__   : cb.__name__ if isinstance(lowerCamelCase__ ,   lowerCamelCase__   ) else cb.__class__.__name__   )
      UpperCamelCase       =  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 ,   lowerCamelCase__   ):

      '''simple docstring'''







      UpperCamelCase       =  ['''on_init_end''', '''on_train_begin''']
      UpperCamelCase       =  0
      UpperCamelCase       =  len(trainer.get_eval_dataloader()   )
      UpperCamelCase       =  ['''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   ):

      '''simple docstring'''







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

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







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  DEFAULT_CALLBACKS.copy() + [ProgressCallback]
      UpperCamelCase       =  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__   )

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

      UpperCamelCase       =  self.get_trainer()
      UpperCamelCase       =  trainer.callback_handler.callbacks[0]
      UpperCamelCase       =  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   ):

      '''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=lowerCamelCase__   )

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

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

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

      UpperCamelCase       =  self.get_trainer(callbacks=[MyTestTrainerCallback] ,   eval_steps=5 ,   evaluation_strategy='''steps'''   )
      trainer.train()
      UpperCamelCase       =  trainer.callback_handler.callbacks[-2].events
      self.assertEqual(lowerCamelCase__ ,   self.get_expected_events(lowerCamelCase__   )   )

      UpperCamelCase       =  self.get_trainer(callbacks=[MyTestTrainerCallback] ,   evaluation_strategy='''epoch'''   )
      trainer.train()
      UpperCamelCase       =  trainer.callback_handler.callbacks[-2].events
      self.assertEqual(lowerCamelCase__ ,   self.get_expected_events(lowerCamelCase__   )   )

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







'''simple docstring'''


from __future__ import annotations



def  __snake_case     (    _UpperCAmelCase					:       list[int]):
   UpperCamelCase       =  len(_UpperCAmelCase) // 2

   # choose the middle 3 elements
   UpperCamelCase       =  lst[m - 1 : m + 2]

   # if middle element is peak
   if three[1] > three[0] and three[1] > three[2]:
      return three[1]

   # if increasing, recurse on right
   elif three[0] < three[2]:
      if len(lst[:m]) == 2:
         m -= 1
      return peak(lst[m:])

   # decreasing
   else:
      if len(lst[:m]) == 2:
         m += 1
      return peak(lst[:m])


if __name__ == "__main__":
   import doctest

   doctest.testmod()





