




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(		UpperCamelCase     ):



 '''simple docstring'''


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

       """simple docstring"""






       lowerCamelCase_	  =					[]

 def        a__				(       self      :						Any		,							A_      :						Optional[int]		,							A_      :						Any		,							A_      :						Tuple		,							**A_      :						List[str]						)     ->			List[Any]:

       """simple docstring"""






       self.events.append('on_init_end'						)

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

       """simple docstring"""






       self.events.append('on_train_begin'						)

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

       """simple docstring"""






       self.events.append('on_train_end'						)

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

       """simple docstring"""






       self.events.append('on_epoch_begin'						)

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

       """simple docstring"""






       self.events.append('on_epoch_end'						)

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

       """simple docstring"""






       self.events.append('on_step_begin'						)

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

       """simple docstring"""






       self.events.append('on_step_end'						)

 def        a__				(       self      :						Union[str, Any]		,							A_      :						int		,							A_      :						Dict		,							A_      :						Union[str, Any]		,							**A_      :						Dict						)     ->			List[str]:

       """simple docstring"""






       self.events.append('on_evaluate'						)

 def        a__				(       self      :						Union[str, Any]		,							A_      :						Tuple		,							A_      :						int		,							A_      :						Optional[Any]		,							**A_      :						Any						)     ->			Optional[int]:

       """simple docstring"""






       self.events.append('on_predict'						)

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

       """simple docstring"""






       self.events.append('on_save'						)

 def        a__				(       self      :						int		,							A_      :						str		,							A_      :						Dict		,							A_      :						Tuple		,							**A_      :						List[str]						)     ->			List[Any]:

       """simple docstring"""






       self.events.append('on_log'						)





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

       """simple docstring"""






       self.events.append('on_prediction_step'						)







@require_torch
class 						A(		unittest.TestCase     ):



 '''simple docstring'''


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

       """simple docstring"""






       lowerCamelCase_	  =					tempfile.mkdtemp()

 def        a__				(       self      :						List[Any]						)     ->			Tuple:

       """simple docstring"""






       shutil.rmtree(self.output_dir						)

 def        a__				(       self      :						Dict		,							A_      :						Optional[int]=0		,							A_      :						Any=0		,							A_      :						Any=64		,							A_      :						Optional[int]=64		,							A_      :						Tuple=None		,							A_      :						Dict=False		,							**A_      :						List[Any]						)     ->			List[Any]:

       """simple docstring"""






       lowerCamelCase_	  =					RegressionDataset(length=A_						)
       lowerCamelCase_	  =					RegressionDataset(length=A_						)
       lowerCamelCase_	  =					RegressionModelConfig(a=A_		,							b=A_						)
       lowerCamelCase_	  =					RegressionPreTrainedModel(A_						)

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

 def        a__				(       self      :						Tuple		,							A_      :						Optional[Any]		,							A_      :						Union[str, Any]						)     ->			Optional[Any]:

       """simple docstring"""






       self.assertEqual(len(A_						)		,							len(A_						)						)

       # Order doesn't matter
       lowerCamelCase_	  =					sorted(A_		,							key=lambda A_						: cb.__name__ if isinstance(A_		,							A_						) else cb.__class__.__name__						)
       lowerCamelCase_	  =					sorted(A_		,							key=lambda A_						: cb.__name__ if isinstance(A_		,							A_						) else cb.__class__.__name__						)

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

 def        a__				(       self      :						Optional[int]		,							A_      :						int						)     ->			List[str]:

       """simple docstring"""






       lowerCamelCase_	  =					['on_init_end', 'on_train_begin']
       lowerCamelCase_	  =					0
       lowerCamelCase_	  =					len(trainer.get_eval_dataloader()						)
       lowerCamelCase_	  =					['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(A_						):
                   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        a__				(       self      :						Optional[int]						)     ->			Optional[Any]:

       """simple docstring"""






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

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

       # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback
       lowerCamelCase_	  =					self.get_trainer(disable_tqdm=A_						)
       lowerCamelCase_	  =					DEFAULT_CALLBACKS.copy() + [PrinterCallback]
       self.check_callbacks_equality(trainer.callback_handler.callbacks		,							A_						)

 def        a__				(       self      :						Optional[int]						)     ->			Optional[Any]:

       """simple docstring"""






       lowerCamelCase_	  =					DEFAULT_CALLBACKS.copy() + [ProgressCallback]
       lowerCamelCase_	  =					self.get_trainer()

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

       lowerCamelCase_	  =					self.get_trainer()
       lowerCamelCase_	  =					trainer.pop_callback(A_						)
       self.assertEqual(cb.__class__		,							A_						)
       self.check_callbacks_equality(trainer.callback_handler.callbacks		,							A_						)

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

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

       lowerCamelCase_	  =					self.get_trainer()
       lowerCamelCase_	  =					trainer.callback_handler.callbacks[0]
       lowerCamelCase_	  =					trainer.pop_callback(A_						)
       self.assertEqual(A_		,							A_						)
       self.check_callbacks_equality(trainer.callback_handler.callbacks		,							A_						)

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





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

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

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

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

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

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

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

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

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









from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
    is_vision_available,
)


lowerCamelCase       :	Dict									=     {"configuration_vit": ["VIT_PRETRAINED_CONFIG_ARCHIVE_MAP", "ViTConfig", "ViTOnnxConfig"]}

try:
 if not is_vision_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase       :	Optional[int]									=     ["ViTFeatureExtractor"]
 lowerCamelCase       :	Dict									=     ["ViTImageProcessor"]

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase       :	int									=     [
     "VIT_PRETRAINED_MODEL_ARCHIVE_LIST",
     "ViTForImageClassification",
     "ViTForMaskedImageModeling",
     "ViTModel",
     "ViTPreTrainedModel",
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase       :	Tuple									=     [
     "TFViTForImageClassification",
     "TFViTModel",
     "TFViTPreTrainedModel",
 ]

try:
 if not is_flax_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase       :	Dict									=     [
     "FlaxViTForImageClassification",
     "FlaxViTModel",
     "FlaxViTPreTrainedModel",
 ]

if TYPE_CHECKING:
 from .configuration_vit import VIT_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTConfig, ViTOnnxConfig

 try:
  if not is_vision_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .feature_extraction_vit import ViTFeatureExtractor
  from .image_processing_vit import ViTImageProcessor

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_vit import (
      VIT_PRETRAINED_MODEL_ARCHIVE_LIST,
      ViTForImageClassification,
      ViTForMaskedImageModeling,
      ViTModel,
      ViTPreTrainedModel,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_vit import TFViTForImageClassification, TFViTModel, TFViTPreTrainedModel

 try:
  if not is_flax_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel, FlaxViTPreTrainedModel


else:
 import sys

 lowerCamelCase       :	Any									=     _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



