



'''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 	UpperCAmelCase	(		__a):


 '''simple docstring'''




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



      """simple docstring"""







      a_       		=[]




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  **lowerCAmelCase_) ->      int:



      """simple docstring"""







      self.events.append("on_init_end")




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  **lowerCAmelCase_) ->      List[str]:



      """simple docstring"""







      self.events.append("on_train_begin")




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  **lowerCAmelCase_) ->      Dict:



      """simple docstring"""







      self.events.append("on_train_end")




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  **lowerCAmelCase_) ->      Tuple:



      """simple docstring"""







      self.events.append("on_epoch_begin")




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  **lowerCAmelCase_) ->      List[Any]:



      """simple docstring"""







      self.events.append("on_epoch_end")




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  **lowerCAmelCase_) ->      Optional[int]:



      """simple docstring"""







      self.events.append("on_step_begin")




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  **lowerCAmelCase_) ->      Any:



      """simple docstring"""







      self.events.append("on_step_end")




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  **lowerCAmelCase_) ->      List[Any]:



      """simple docstring"""







      self.events.append("on_evaluate")




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  **lowerCAmelCase_) ->      Tuple:



      """simple docstring"""







      self.events.append("on_predict")




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  **lowerCAmelCase_) ->      str:



      """simple docstring"""







      self.events.append("on_save")




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  **lowerCAmelCase_) ->      Optional[Any]:



      """simple docstring"""







      self.events.append("on_log")




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  **lowerCAmelCase_) ->      List[Any]:



      """simple docstring"""







      self.events.append("on_prediction_step")







@require_torch
class 	UpperCAmelCase	(		unittest.TestCase):


 '''simple docstring'''




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



      """simple docstring"""







      a_       		=tempfile.mkdtemp()




 def       lowercase_  (   self) ->      Any:



      """simple docstring"""







      shutil.rmtree(self.output_dir)




 def       lowercase_  (   self							,  lowerCAmelCase_=0							,  lowerCAmelCase_=0							,  lowerCAmelCase_=6_4							,  lowerCAmelCase_=6_4							,  lowerCAmelCase_=None							,  lowerCAmelCase_=False							,  **lowerCAmelCase_) ->      Union[str, Any]:



      """simple docstring"""







      a_       		=RegressionDataset(length=lowerCAmelCase_)
      a_       		=RegressionDataset(length=lowerCAmelCase_)
      a_       		=RegressionModelConfig(a=lowerCAmelCase_							,  b=lowerCAmelCase_)
      a_       		=RegressionPreTrainedModel(lowerCAmelCase_)

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




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_) ->      Optional[Any]:



      """simple docstring"""







      self.assertEqual(len(lowerCAmelCase_)							,  len(lowerCAmelCase_))

      # Order doesn't matter
      a_       		=sorted(lowerCAmelCase_							,  key=lambda lowerCAmelCase_: cb.__name__ if isinstance(lowerCAmelCase_							,  lowerCAmelCase_) else cb.__class__.__name__)
      a_       		=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       lowercase_  (   self							,  lowerCAmelCase_) ->      Optional[Any]:



      """simple docstring"""







      a_       		=["on_init_end", "on_train_begin"]
      a_       		=0
      a_       		=len(trainer.get_eval_dataloader())
      a_       		=["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       lowercase_  (   self) ->      List[str]:



      """simple docstring"""







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

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




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



      """simple docstring"""







      a_       		=DEFAULT_CALLBACKS.copy() + [ProgressCallback]
      a_       		=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_       		=self.get_trainer()
      a_       		=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_       		=self.get_trainer()
      a_       		=trainer.callback_handler.callbacks[0]
      trainer.remove_callback(lowerCAmelCase_)
      expected_callbacks.remove(lowerCAmelCase_)
      self.check_callbacks_equality(trainer.callback_handler.callbacks							,  lowerCAmelCase_)

      a_       		=self.get_trainer()
      a_       		=trainer.callback_handler.callbacks[0]
      a_       		=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       lowercase_  (   self) ->      Optional[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=lowerCAmelCase_)

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

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

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

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

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

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







'''simple docstring'''


import os

# Precomputes a list of the 100 first triangular numbers
lowercase							       =			[int(0.5 * n * (n + 1)) for n in range(1, 101)]






def  UpperCAmelCase_       (		):


     '''simple docstring'''







     a_       		=os.path.dirname(os.path.realpath(lowercase__       )       )
     a_       		=os.path.join(lowercase__       ,	"words.txt"       )

     a_       		=""
     with open(lowercase__       ) as f:
          a_       		=f.readline()

     a_       		=[word.strip("\""       ) for word in words.strip("\r\n"       ).split(","       )]
     a_       		=[
         word
         for word in [sum(ord(lowercase__       ) - 6_4 for x in word       ) for word in words]
         if word in TRIANGULAR_NUMBERS
     ]
     return len(lowercase__       )


if __name__ == "__main__":
  print(solution())


