
'''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 			lowerCAmelCase__       (  lowerCamelCase_					):






					def __init__(       self		):


											"""simple docstring"""





											lowercase_     :					int													=	[]






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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







@require_torch
class 			lowerCAmelCase__       (  unittest.TestCase					):






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Any													=	tempfile.mkdtemp()






					def      _snake_case   (       self		):


											"""simple docstring"""





											shutil.rmtree(self.output_dir		)






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE=0    ,							__SCREAMING_SNAKE_CASE=0    ,							__SCREAMING_SNAKE_CASE=64    ,							__SCREAMING_SNAKE_CASE=64    ,							__SCREAMING_SNAKE_CASE=None    ,							__SCREAMING_SNAKE_CASE=False    ,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_     :					Any													=	RegressionDataset(length=__SCREAMING_SNAKE_CASE		)
											lowercase_     :					List[str]													=	RegressionDataset(length=__SCREAMING_SNAKE_CASE		)
											lowercase_     :					int													=	RegressionModelConfig(a=__SCREAMING_SNAKE_CASE    ,							b=__SCREAMING_SNAKE_CASE		)
											lowercase_     :					Any													=	RegressionPreTrainedModel(__SCREAMING_SNAKE_CASE		)

											lowercase_     :					List[Any]													=	TrainingArguments(self.output_dir    ,							disable_tqdm=__SCREAMING_SNAKE_CASE    ,							report_to=[]    ,							**__SCREAMING_SNAKE_CASE		)
											return Trainer(
											    __SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							train_dataset=__SCREAMING_SNAKE_CASE    ,							eval_dataset=__SCREAMING_SNAKE_CASE    ,							callbacks=__SCREAMING_SNAKE_CASE    ,							)






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											self.assertEqual(len(__SCREAMING_SNAKE_CASE		)    ,							len(__SCREAMING_SNAKE_CASE		)		)

											# Order doesn't matter
											lowercase_     :					Tuple													=	sorted(__SCREAMING_SNAKE_CASE    ,							key=lambda __SCREAMING_SNAKE_CASE		: cb.__name__ if isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		) else cb.__class__.__name__		)
											lowercase_     :					Any													=	sorted(__SCREAMING_SNAKE_CASE    ,							key=lambda __SCREAMING_SNAKE_CASE		: cb.__name__ if isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		) else cb.__class__.__name__		)

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






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_     :					Optional[Any]													=	['''on_init_end''', '''on_train_begin''']
											lowercase_     :					Union[str, Any]													=	0
											lowercase_     :					List[Any]													=	len(trainer.get_eval_dataloader()		)
											lowercase_     :					List[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(__SCREAMING_SNAKE_CASE		):
																							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      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					List[str]													=	self.get_trainer()
											lowercase_     :					str													=	DEFAULT_CALLBACKS.copy() + [ProgressCallback]
											self.check_callbacks_equality(trainer.callback_handler.callbacks    ,							__SCREAMING_SNAKE_CASE		)

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

											# TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback
											lowercase_     :					List[Any]													=	self.get_trainer(disable_tqdm=__SCREAMING_SNAKE_CASE		)
											lowercase_     :					List[str]													=	DEFAULT_CALLBACKS.copy() + [PrinterCallback]
											self.check_callbacks_equality(trainer.callback_handler.callbacks    ,							__SCREAMING_SNAKE_CASE		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Union[str, Any]													=	DEFAULT_CALLBACKS.copy() + [ProgressCallback]
											lowercase_     :					List[Any]													=	self.get_trainer()

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

											lowercase_     :					int													=	self.get_trainer()
											lowercase_     :					Tuple													=	trainer.pop_callback(__SCREAMING_SNAKE_CASE		)
											self.assertEqual(cb.__class__    ,							__SCREAMING_SNAKE_CASE		)
											self.check_callbacks_equality(trainer.callback_handler.callbacks    ,							__SCREAMING_SNAKE_CASE		)

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

											# We can also add, pop, or remove by instance
											lowercase_     :					Tuple													=	self.get_trainer()
											lowercase_     :					Any													=	trainer.callback_handler.callbacks[0]
											trainer.remove_callback(__SCREAMING_SNAKE_CASE		)
											expected_callbacks.remove(__SCREAMING_SNAKE_CASE		)
											self.check_callbacks_equality(trainer.callback_handler.callbacks    ,							__SCREAMING_SNAKE_CASE		)

											lowercase_     :					str													=	self.get_trainer()
											lowercase_     :					str													=	trainer.callback_handler.callbacks[0]
											lowercase_     :					Tuple													=	trainer.pop_callback(__SCREAMING_SNAKE_CASE		)
											self.assertEqual(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)
											self.check_callbacks_equality(trainer.callback_handler.callbacks    ,							__SCREAMING_SNAKE_CASE		)

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






					def      _snake_case   (       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=__SCREAMING_SNAKE_CASE		)

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

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

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

											lowercase_     :					Tuple													=	self.get_trainer(callbacks=[MyTestTrainerCallback]    ,							eval_steps=5    ,							evaluation_strategy='''steps'''		)
											trainer.train()
											lowercase_     :					int													=	trainer.callback_handler.callbacks[-2].events
											self.assertEqual(__SCREAMING_SNAKE_CASE    ,							self.get_expected_events(__SCREAMING_SNAKE_CASE		)		)

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

											# A bit of everything
											lowercase_     :					Optional[int]													=	self.get_trainer(
											    callbacks=[MyTestTrainerCallback]    ,							logging_steps=3    ,							save_steps=10    ,							eval_steps=5    ,							evaluation_strategy='''steps'''    ,							)
											trainer.train()
											lowercase_     :					List[str]													=	trainer.callback_handler.callbacks[-2].events
											self.assertEqual(__SCREAMING_SNAKE_CASE    ,							self.get_expected_events(__SCREAMING_SNAKE_CASE		)		)

											# warning should be emitted for duplicated callbacks
											with patch('''transformers.trainer_callback.logger.warning'''		) as warn_mock:
																	lowercase_     :					Optional[int]													=	self.get_trainer(
																	    callbacks=[MyTestTrainerCallback, MyTestTrainerCallback]    ,							)
																	assert str(__SCREAMING_SNAKE_CASE		) 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


_lowercase			:  List[Any]   							=	logging.get_logger(__name__)

_lowercase			:  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 			lowerCAmelCase__       (  lowerCamelCase_					):
					lowerCAmelCase_					       =					'''vit'''






					def __init__(       self    ,							__SCREAMING_SNAKE_CASE=7_68    ,							__SCREAMING_SNAKE_CASE=12    ,							__SCREAMING_SNAKE_CASE=12    ,							__SCREAMING_SNAKE_CASE=30_72    ,							__SCREAMING_SNAKE_CASE="gelu"    ,							__SCREAMING_SNAKE_CASE=0.0    ,							__SCREAMING_SNAKE_CASE=0.0    ,							__SCREAMING_SNAKE_CASE=0.02    ,							__SCREAMING_SNAKE_CASE=1E-1_2    ,							__SCREAMING_SNAKE_CASE=2_24    ,							__SCREAMING_SNAKE_CASE=16    ,							__SCREAMING_SNAKE_CASE=3    ,							__SCREAMING_SNAKE_CASE=True    ,							__SCREAMING_SNAKE_CASE=16    ,							**__SCREAMING_SNAKE_CASE    ,							):


											"""simple docstring"""





											super().__init__(**__SCREAMING_SNAKE_CASE		)

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







class 			lowerCAmelCase__       (  lowerCamelCase_					):
					lowerCAmelCase_					       =					version.parse('''1.11'''					)






					@property
					def      _snake_case   (       self		):


											"""simple docstring"""





											return OrderedDict(
											    [
											        ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
											    ]		)






					@property
					def      _snake_case   (       self		):


											"""simple docstring"""





											return 1E-4



