






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





	"""simple docstring"""






	def __init__(							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			[]

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    **UpperCAmelCase    ):
							"""simple docstring"""







							self.events.append('on_init_end'    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    **UpperCAmelCase    ):
							"""simple docstring"""







							self.events.append('on_train_begin'    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    **UpperCAmelCase    ):
							"""simple docstring"""







							self.events.append('on_train_end'    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    **UpperCAmelCase    ):
							"""simple docstring"""







							self.events.append('on_epoch_begin'    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    **UpperCAmelCase    ):
							"""simple docstring"""







							self.events.append('on_epoch_end'    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    **UpperCAmelCase    ):
							"""simple docstring"""







							self.events.append('on_step_begin'    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    **UpperCAmelCase    ):
							"""simple docstring"""







							self.events.append('on_step_end'    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    **UpperCAmelCase    ):
							"""simple docstring"""







							self.events.append('on_evaluate'    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    **UpperCAmelCase    ):
							"""simple docstring"""







							self.events.append('on_predict'    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    **UpperCAmelCase    ):
							"""simple docstring"""







							self.events.append('on_save'    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    **UpperCAmelCase    ):
							"""simple docstring"""







							self.events.append('on_log'    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    **UpperCAmelCase    ):
							"""simple docstring"""







							self.events.append('on_prediction_step'    )





@require_torch
class __lowerCamelCase (      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       ,    UpperCAmelCase=0       ,    UpperCAmelCase=0       ,    UpperCAmelCase=64       ,    UpperCAmelCase=64       ,    UpperCAmelCase=None       ,    UpperCAmelCase=False       ,    **UpperCAmelCase    ):
							"""simple docstring"""







							_UpperCAmelCase									=			RegressionDataset(length=UpperCAmelCase    )
							_UpperCAmelCase									=			RegressionDataset(length=UpperCAmelCase    )
							_UpperCAmelCase									=			RegressionModelConfig(a=UpperCAmelCase       ,    b=UpperCAmelCase    )
							_UpperCAmelCase									=			RegressionPreTrainedModel(UpperCAmelCase    )

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

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase    ):
							"""simple docstring"""







							self.assertEqual(len(UpperCAmelCase    )       ,    len(UpperCAmelCase    )    )

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

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

	def        UpperCamelCase    (							self       ,    UpperCAmelCase    ):
							"""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(UpperCAmelCase    ):
																			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       ,    UpperCAmelCase    )

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

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

	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(UpperCAmelCase    )
							expected_callbacks.remove(UpperCAmelCase    )
							self.check_callbacks_equality(trainer.callback_handler.callbacks       ,    UpperCAmelCase    )

							_UpperCAmelCase									=			self.get_trainer()
							_UpperCAmelCase									=			trainer.pop_callback(UpperCAmelCase    )
							self.assertEqual(cb.__class__       ,    UpperCAmelCase    )
							self.check_callbacks_equality(trainer.callback_handler.callbacks       ,    UpperCAmelCase    )

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

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

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

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

	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=UpperCAmelCase    )

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

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

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

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

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

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

							# 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(UpperCAmelCase    ) in warn_mock.call_args[0][0]

"""simple docstring"""






from collections import OrderedDict
from typing import List, Mapping

from packaging import version

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


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

__lowerCAmelCase    :       Union[str, Any]					     ={
    """google/efficientnet-b7""": """https://huggingface.co/google/efficientnet-b7/resolve/main/config.json""",
}





class  _A						(       lowerCAmelCase	):
      snake_case__			:   Optional[int]							  =	'efficientnet'

      def __init__(						self		,       __lowerCAmelCase = 3		,       __lowerCAmelCase = 600		,       __lowerCAmelCase = 2.0		,       __lowerCAmelCase = 3.1		,       __lowerCAmelCase = 8		,       __lowerCAmelCase = [3, 3, 5, 3, 5, 5, 3]		,       __lowerCAmelCase = [32, 16, 24, 40, 80, 112, 192]		,       __lowerCAmelCase = [16, 24, 40, 80, 112, 192, 320]		,       __lowerCAmelCase = []		,       __lowerCAmelCase = [1, 2, 2, 2, 1, 2, 1]		,       __lowerCAmelCase = [1, 2, 2, 3, 3, 4, 1]		,       __lowerCAmelCase = [1, 6, 6, 6, 6, 6, 6]		,       __lowerCAmelCase = 0.2_5		,       __lowerCAmelCase = "swish"		,       __lowerCAmelCase = 2560		,       __lowerCAmelCase = "mean"		,       __lowerCAmelCase = 0.0_2		,       __lowerCAmelCase = 0.0_0_1		,       __lowerCAmelCase = 0.9_9		,       __lowerCAmelCase = 0.5		,       __lowerCAmelCase = 0.2		,       **__lowerCAmelCase		,       ):







            """simple docstring"""



            super().__init__(**__lowerCAmelCase						)

            lowercase														=	num_channels
            lowercase														=	image_size
            lowercase														=	width_coefficient
            lowercase														=	depth_coefficient
            lowercase														=	depth_divisor
            lowercase														=	kernel_sizes
            lowercase														=	in_channels
            lowercase														=	out_channels
            lowercase														=	depthwise_padding
            lowercase														=	strides
            lowercase														=	num_block_repeats
            lowercase														=	expand_ratios
            lowercase														=	squeeze_expansion_ratio
            lowercase														=	hidden_act
            lowercase														=	hidden_dim
            lowercase														=	pooling_type
            lowercase														=	initializer_range
            lowercase														=	batch_norm_eps
            lowercase														=	batch_norm_momentum
            lowercase														=	dropout_rate
            lowercase														=	drop_connect_rate
            lowercase														=	sum(__lowerCAmelCase						) * 4






class  _A						(       lowerCAmelCase	):
      snake_case__			:   Dict							  =	version.parse('1.11'	)

      @property
      def 				A__       (						self						):







            """simple docstring"""



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







      @property
      def 				A__       (						self						):







            """simple docstring"""



            return 1E-5



