

"""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      __a							(UpperCamelCase_):






     '''simple docstring'''

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


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	[]

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


           """simple docstring"""


           self.events.append("""on_init_end"""  )

     def 					_a			(						self	,    _a	,    _a	,    _a	,    **_a  )  ->      str:


           """simple docstring"""


           self.events.append("""on_train_begin"""  )

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


           """simple docstring"""


           self.events.append("""on_train_end"""  )

     def 					_a			(						self	,    _a	,    _a	,    _a	,    **_a  )  ->      str:


           """simple docstring"""


           self.events.append("""on_epoch_begin"""  )

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


           """simple docstring"""


           self.events.append("""on_epoch_end"""  )

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


           """simple docstring"""


           self.events.append("""on_step_begin"""  )

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


           """simple docstring"""


           self.events.append("""on_step_end"""  )

     def 					_a			(						self	,    _a	,    _a	,    _a	,    **_a  )  ->      int:


           """simple docstring"""


           self.events.append("""on_evaluate"""  )

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


           """simple docstring"""


           self.events.append("""on_predict"""  )

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


           """simple docstring"""


           self.events.append("""on_save"""  )

     def 					_a			(						self	,    _a	,    _a	,    _a	,    **_a  )  ->      Dict:


           """simple docstring"""


           self.events.append("""on_log"""  )





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


           """simple docstring"""


           self.events.append("""on_prediction_step"""  )





@require_torch
class      __a							(unittest.TestCase):






     '''simple docstring'''

     def 					_a			(						self  )  ->      List[str]:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      str      		=	tempfile.mkdtemp()

     def 					_a			(						self  )  ->      int:


           """simple docstring"""


           shutil.rmtree(self.output_dir  )

     def 					_a			(						self	,    _a=0	,    _a=0	,    _a=64	,    _a=64	,    _a=None	,    _a=False	,    **_a  )  ->      str:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      Tuple      		=	RegressionDataset(length=_a  )
           SCREAMING_SNAKE_CASE__   :      List[Any]      		=	RegressionDataset(length=_a  )
           SCREAMING_SNAKE_CASE__   :      List[str]      		=	RegressionModelConfig(a=_a	,    b=_a  )
           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	RegressionPreTrainedModel(_a  )

           SCREAMING_SNAKE_CASE__   :      Any      		=	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	,    _a	,    _a  )  ->      int:


           """simple docstring"""


           self.assertEqual(len(_a  )	,    len(_a  )  )

           # Order doesn't matter
           SCREAMING_SNAKE_CASE__   :      List[str]      		=	sorted(_a	,    key=lambda _a  : cb.__name__ if isinstance(_a	,    _a  ) else cb.__class__.__name__  )
           SCREAMING_SNAKE_CASE__   :      List[Any]      		=	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	,    _a  )  ->      List[str]:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	["""on_init_end""", """on_train_begin"""]
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	0
           SCREAMING_SNAKE_CASE__   :      List[Any]      		=	len(trainer.get_eval_dataloader()  )
           SCREAMING_SNAKE_CASE__   :      Union[str, 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(_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[Any]:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      Tuple      		=	self.get_trainer()
           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	DEFAULT_CALLBACKS.copy() + [ProgressCallback]
           self.check_callbacks_equality(trainer.callback_handler.callbacks	,    _a  )

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

     def 					_a			(						self  )  ->      Optional[int]:


           """simple docstring"""


           SCREAMING_SNAKE_CASE__   :      str      		=	DEFAULT_CALLBACKS.copy() + [ProgressCallback]
           SCREAMING_SNAKE_CASE__   :      Any      		=	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  )

           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	self.get_trainer()
           SCREAMING_SNAKE_CASE__   :      List[Any]      		=	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
           SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	self.get_trainer()
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	trainer.callback_handler.callbacks[0]
           trainer.remove_callback(_a  )
           expected_callbacks.remove(_a  )
           self.check_callbacks_equality(trainer.callback_handler.callbacks	,    _a  )

           SCREAMING_SNAKE_CASE__   :      List[Any]      		=	self.get_trainer()
           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	trainer.callback_handler.callbacks[0]
           SCREAMING_SNAKE_CASE__   :      int      		=	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  )  ->      Optional[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  )

           SCREAMING_SNAKE_CASE__   :      Dict      		=	self.get_trainer(callbacks=[MyTestTrainerCallback]  )
           trainer.train()
           SCREAMING_SNAKE_CASE__   :      str      		=	trainer.callback_handler.callbacks[-2].events
           self.assertEqual(_a	,    self.get_expected_events(_a  )  )

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

           SCREAMING_SNAKE_CASE__   :      Any      		=	self.get_trainer(callbacks=[MyTestTrainerCallback]	,    save_steps=5  )
           trainer.train()
           SCREAMING_SNAKE_CASE__   :      Tuple      		=	trainer.callback_handler.callbacks[-2].events
           self.assertEqual(_a	,    self.get_expected_events(_a  )  )

           SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	self.get_trainer(callbacks=[MyTestTrainerCallback]	,    eval_steps=5	,    evaluation_strategy="""steps"""  )
           trainer.train()
           SCREAMING_SNAKE_CASE__   :      str      		=	trainer.callback_handler.callbacks[-2].events
           self.assertEqual(_a	,    self.get_expected_events(_a  )  )

           SCREAMING_SNAKE_CASE__   :      List[str]      		=	self.get_trainer(callbacks=[MyTestTrainerCallback]	,    evaluation_strategy="""epoch"""  )
           trainer.train()
           SCREAMING_SNAKE_CASE__   :      List[Any]      		=	trainer.callback_handler.callbacks[-2].events
           self.assertEqual(_a	,    self.get_expected_events(_a  )  )

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







"""simple docstring"""


from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


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

a					:Optional[int]    = {
    "microsoft/focalnet-tiny": "https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json",
}

class      __a							(UpperCamelCase_   ,	UpperCamelCase_):






     '''simple docstring'''
     _SCREAMING_SNAKE_CASE						:Any       = """focalnet"""

     def __init__(						self	,    _a=224	,    _a=4	,    _a=3	,    _a=96	,    _a=False	,    _a=[192, 384, 768, 768]	,    _a=[2, 2, 6, 2]	,    _a=[2, 2, 2, 2]	,    _a=[3, 3, 3, 3]	,    _a="gelu"	,    _a=4.0	,    _a=0.0	,    _a=0.1	,    _a=False	,    _a=1E-4	,    _a=False	,    _a=False	,    _a=False	,    _a=0.02	,    _a=1E-5	,    _a=32	,    _a=None	,    _a=None	,    **_a	,    )  ->      Optional[Any]:


           """simple docstring"""


           super().__init__(**_a  )

           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	image_size
           SCREAMING_SNAKE_CASE__   :      str      		=	patch_size
           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	num_channels
           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	embed_dim
           SCREAMING_SNAKE_CASE__   :      List[str]      		=	use_conv_embed
           SCREAMING_SNAKE_CASE__   :      List[str]      		=	hidden_sizes
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	depths
           SCREAMING_SNAKE_CASE__   :      Any      		=	focal_levels
           SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	focal_windows
           SCREAMING_SNAKE_CASE__   :      Any      		=	hidden_act
           SCREAMING_SNAKE_CASE__   :      Tuple      		=	mlp_ratio
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	hidden_dropout_prob
           SCREAMING_SNAKE_CASE__   :      Optional[Any]      		=	drop_path_rate
           SCREAMING_SNAKE_CASE__   :      str      		=	use_layerscale
           SCREAMING_SNAKE_CASE__   :      int      		=	layerscale_value
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	use_post_layernorm
           SCREAMING_SNAKE_CASE__   :      Any      		=	use_post_layernorm_in_modulation
           SCREAMING_SNAKE_CASE__   :      Union[str, Any]      		=	normalize_modulator
           SCREAMING_SNAKE_CASE__   :      str      		=	initializer_range
           SCREAMING_SNAKE_CASE__   :      Any      		=	layer_norm_eps
           SCREAMING_SNAKE_CASE__   :      Any      		=	encoder_stride
           SCREAMING_SNAKE_CASE__   :      Optional[int]      		=	["""stem"""] + [f'''stage{idx}''' for idx in range(1	,    len(self.depths  ) + 1  )]
           SCREAMING_SNAKE_CASE__		,		SCREAMING_SNAKE_CASE__   :      Dict      		=	get_aligned_output_features_output_indices(
               out_features=_a	,    out_indices=_a	,    stage_names=self.stage_names  )




