






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







 """simple docstring"""



 def __init__(							self       )						->  List[Any]:
       lowercase_      :   Tuple        =			[]
 def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      **_lowercase       )						->  int:
       self.events.append('on_init_end'       )
 def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      **_lowercase       )						->  List[str]:
       self.events.append('on_train_begin'       )
 def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      **_lowercase       )						->  Dict:
       self.events.append('on_train_end'       )
 def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      **_lowercase       )						->  Tuple:
       self.events.append('on_epoch_begin'       )
 def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      **_lowercase       )						->  List[Any]:
       self.events.append('on_epoch_end'       )
 def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      **_lowercase       )						->  Optional[int]:
       self.events.append('on_step_begin'       )
 def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      **_lowercase       )						->  Any:
       self.events.append('on_step_end'       )
 def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      **_lowercase       )						->  List[Any]:
       self.events.append('on_evaluate'       )
 def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      **_lowercase       )						->  Tuple:
       self.events.append('on_predict'       )
 def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      **_lowercase       )						->  str:
       self.events.append('on_save'       )
 def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      **_lowercase       )						->  Optional[Any]:
       self.events.append('on_log'       )







 def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      **_lowercase       )						->  List[Any]:
       self.events.append('on_prediction_step'       )






@require_torch
class 							__magic_name__     (	unittest.TestCase				):







 """simple docstring"""



 def        lowerCamelCase__		(							self       )						->  Union[str, Any]:
       lowercase_      :   Union[str, Any]        =			tempfile.mkdtemp()
 def        lowerCamelCase__		(							self       )						->  Any:
       shutil.rmtree(self.output_dir       )
 def        lowerCamelCase__		(							self  ,      _lowercase=0  ,      _lowercase=0  ,      _lowercase=64  ,      _lowercase=64  ,      _lowercase=None  ,      _lowercase=False  ,      **_lowercase       )						->  Union[str, Any]:
       lowercase_      :   Any        =			RegressionDataset(length=lowerCAmelCase_       )
       lowercase_      :   Optional[int]        =			RegressionDataset(length=lowerCAmelCase_       )
       lowercase_      :   Any        =			RegressionModelConfig(a=lowerCAmelCase_  ,      b=lowerCAmelCase_       )
       lowercase_      :   Optional[int]        =			RegressionPreTrainedModel(lowerCAmelCase_       )

       lowercase_      :   Tuple        =			TrainingArguments(self.output_dir  ,      disable_tqdm=lowerCAmelCase_  ,      report_to=[]  ,      **lowerCAmelCase_       )
       return Trainer(
           lowerCAmelCase_  ,      lowerCAmelCase_  ,      train_dataset=lowerCAmelCase_  ,      eval_dataset=lowerCAmelCase_  ,      callbacks=lowerCAmelCase_  ,      )
 def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase       )						->  Optional[Any]:
       self.assertEqual(len(lowerCAmelCase_       )  ,      len(lowerCAmelCase_       )       )

       # Order doesn't matter
       lowercase_      :   Optional[int]        =			sorted(lowerCAmelCase_  ,      key=lambda _lowercase       : cb.__name__ if isinstance(lowerCAmelCase_  ,      lowerCAmelCase_       ) else cb.__class__.__name__       )
       lowercase_      :   List[str]        =			sorted(lowerCAmelCase_  ,      key=lambda _lowercase       : 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        lowerCamelCase__		(							self  ,      _lowercase       )						->  Optional[Any]:
       lowercase_      :   Tuple        =			['on_init_end', 'on_train_begin']
       lowercase_      :   int        =			0
       lowercase_      :   List[str]        =			len(trainer.get_eval_dataloader()       )
       lowercase_      :   Optional[Any]        =			['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        lowerCamelCase__		(							self       )						->  List[str]:
       lowercase_      :   List[Any]        =			self.get_trainer()
       lowercase_      :   Tuple        =			DEFAULT_CALLBACKS.copy() + [ProgressCallback]
       self.check_callbacks_equality(trainer.callback_handler.callbacks  ,      lowerCAmelCase_       )

       # Callbacks passed at init are added to the default callbacks
       lowercase_      :   str        =			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
       lowercase_      :   Tuple        =			self.get_trainer(disable_tqdm=lowerCAmelCase_       )
       lowercase_      :   List[str]        =			DEFAULT_CALLBACKS.copy() + [PrinterCallback]
       self.check_callbacks_equality(trainer.callback_handler.callbacks  ,      lowerCAmelCase_       )
 def        lowerCamelCase__		(							self       )						->  List[Any]:
       lowercase_      :   Optional[Any]        =			DEFAULT_CALLBACKS.copy() + [ProgressCallback]
       lowercase_      :   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_       )

       lowercase_      :   Tuple        =			self.get_trainer()
       lowercase_      :   Optional[Any]        =			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
       lowercase_      :   List[Any]        =			self.get_trainer()
       lowercase_      :   Union[str, Any]        =			trainer.callback_handler.callbacks[0]
       trainer.remove_callback(lowerCAmelCase_       )
       expected_callbacks.remove(lowerCAmelCase_       )
       self.check_callbacks_equality(trainer.callback_handler.callbacks  ,      lowerCAmelCase_       )

       lowercase_      :   int        =			self.get_trainer()
       lowercase_      :   Tuple        =			trainer.callback_handler.callbacks[0]
       lowercase_      :   Optional[Any]        =			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        lowerCamelCase__		(							self       )						->  Optional[int]:
       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_       )

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

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

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

       lowercase_      :   str        =			self.get_trainer(callbacks=[MyTestTrainerCallback]  ,      eval_steps=5  ,      evaluation_strategy='steps'       )
       trainer.train()
       lowercase_      :   Union[str, Any]        =			trainer.callback_handler.callbacks[-2].events
       self.assertEqual(lowerCAmelCase_  ,      self.get_expected_events(lowerCAmelCase_       )       )

       lowercase_      :   Dict        =			self.get_trainer(callbacks=[MyTestTrainerCallback]  ,      evaluation_strategy='epoch'       )
       trainer.train()
       lowercase_      :   List[str]        =			trainer.callback_handler.callbacks[-2].events
       self.assertEqual(lowerCAmelCase_  ,      self.get_expected_events(lowerCAmelCase_       )       )

       # A bit of everything
       lowercase_      :   Optional[Any]        =			self.get_trainer(
           callbacks=[MyTestTrainerCallback]  ,      logging_steps=3  ,      save_steps=10  ,      eval_steps=5  ,      evaluation_strategy='steps'  ,      )
       trainer.train()
       lowercase_      :   List[Any]        =			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:
             lowercase_      :   Dict        =			self.get_trainer(
                 callbacks=[MyTestTrainerCallback, MyTestTrainerCallback]  ,      )
             assert str(lowerCAmelCase_       ) in warn_mock.call_args[0][0]








'''simple docstring'''

from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


A:      Dict  				=				logging.get_logger(__name__)

A:      Optional[Any]  				=				{
    "google/vit-base-patch16-224": "https://huggingface.co/vit-base-patch16-224/resolve/main/config.json",
    # See all ViT models at https://huggingface.co/models?filter=vit
}






class 							__magic_name__     (	UpperCAmelCase_				):







	"""simple docstring"""



	SCREAMING_SNAKE_CASE_					:      Optional[Any]        =  'vit'
	def __init__(							self  ,      _lowercase=768  ,      _lowercase=12  ,      _lowercase=12  ,      _lowercase=3072  ,      _lowercase="gelu"  ,      _lowercase=0.0  ,      _lowercase=0.0  ,      _lowercase=0.02  ,      _lowercase=1E-1_2  ,      _lowercase=224  ,      _lowercase=16  ,      _lowercase=3  ,      _lowercase=True  ,      _lowercase=16  ,      **_lowercase  ,      )						->  List[str]:
							super().__init__(**_lowercase       )

							lowercase_      :   Optional[int]        =			hidden_size
							lowercase_      :   str        =			num_hidden_layers
							lowercase_      :   str        =			num_attention_heads
							lowercase_      :   int        =			intermediate_size
							lowercase_      :   List[Any]        =			hidden_act
							lowercase_      :   Any        =			hidden_dropout_prob
							lowercase_      :   List[str]        =			attention_probs_dropout_prob
							lowercase_      :   str        =			initializer_range
							lowercase_      :   List[str]        =			layer_norm_eps
							lowercase_      :   Any        =			image_size
							lowercase_      :   Tuple        =			patch_size
							lowercase_      :   Optional[Any]        =			num_channels
							lowercase_      :   str        =			qkv_bias
							lowercase_      :   List[str]        =			encoder_stride






class 							__magic_name__     (	UpperCAmelCase_				):







	"""simple docstring"""



	SCREAMING_SNAKE_CASE_					:      Any        =  version.parse('1.11'				)
	@property
	def        lowerCamelCase__		(							self       )						->  Mapping[str, Mapping[int, str]]:
							return OrderedDict(
							    [
							        ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
							    ]       )







	@property
	def        lowerCamelCase__		(							self       )						->  float:
							return 1E-4
