



'''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__						(    _UpperCamelCase   ):







							def __init__(							self					:       Union[str, Any]		):
									_a     :      Dict        =							[]







							def 							__lowercase			(							self					:       Union[str, Any]	,_UpperCAmelCase					:       Any	,_UpperCAmelCase					:       Union[str, Any]	,_UpperCAmelCase					:       Optional[int]	,**_UpperCAmelCase					:       Any		):
									self.events.append('on_init_end'		)







							def 							__lowercase			(							self					:       List[str]	,_UpperCAmelCase					:       List[Any]	,_UpperCAmelCase					:       Union[str, Any]	,_UpperCAmelCase					:       Tuple	,**_UpperCAmelCase					:       Dict		):
									self.events.append('on_train_begin'		)







							def 							__lowercase			(							self					:       List[str]	,_UpperCAmelCase					:       Any	,_UpperCAmelCase					:       Optional[int]	,_UpperCAmelCase					:       Union[str, Any]	,**_UpperCAmelCase					:       List[Any]		):
									self.events.append('on_train_end'		)







							def 							__lowercase			(							self					:       Any	,_UpperCAmelCase					:       str	,_UpperCAmelCase					:       Optional[int]	,_UpperCAmelCase					:       Any	,**_UpperCAmelCase					:       Optional[Any]		):
									self.events.append('on_epoch_begin'		)







							def 							__lowercase			(							self					:       Optional[int]	,_UpperCAmelCase					:       Union[str, Any]	,_UpperCAmelCase					:       Any	,_UpperCAmelCase					:       Union[str, Any]	,**_UpperCAmelCase					:       Optional[Any]		):
									self.events.append('on_epoch_end'		)







							def 							__lowercase			(							self					:       Dict	,_UpperCAmelCase					:       int	,_UpperCAmelCase					:       List[Any]	,_UpperCAmelCase					:       str	,**_UpperCAmelCase					:       Optional[int]		):
									self.events.append('on_step_begin'		)







							def 							__lowercase			(							self					:       str	,_UpperCAmelCase					:       Optional[Any]	,_UpperCAmelCase					:       int	,_UpperCAmelCase					:       Any	,**_UpperCAmelCase					:       Dict		):
									self.events.append('on_step_end'		)







							def 							__lowercase			(							self					:       Dict	,_UpperCAmelCase					:       Dict	,_UpperCAmelCase					:       Optional[int]	,_UpperCAmelCase					:       Optional[Any]	,**_UpperCAmelCase					:       Any		):
									self.events.append('on_evaluate'		)







							def 							__lowercase			(							self					:       int	,_UpperCAmelCase					:       Any	,_UpperCAmelCase					:       Tuple	,_UpperCAmelCase					:       Optional[int]	,**_UpperCAmelCase					:       int		):
									self.events.append('on_predict'		)







							def 							__lowercase			(							self					:       int	,_UpperCAmelCase					:       Union[str, Any]	,_UpperCAmelCase					:       int	,_UpperCAmelCase					:       Optional[int]	,**_UpperCAmelCase					:       Tuple		):
									self.events.append('on_save'		)







							def 							__lowercase			(							self					:       Optional[int]	,_UpperCAmelCase					:       Optional[int]	,_UpperCAmelCase					:       Optional[int]	,_UpperCAmelCase					:       Optional[int]	,**_UpperCAmelCase					:       Union[str, Any]		):
									self.events.append('on_log'		)







							def 							__lowercase			(							self					:       Optional[int]	,_UpperCAmelCase					:       int	,_UpperCAmelCase					:       Tuple	,_UpperCAmelCase					:       int	,**_UpperCAmelCase					:       List[str]		):
									self.events.append('on_prediction_step'		)

@require_torch
class 	__magic_name__						(    unittest.TestCase   ):







							def 							__lowercase			(							self					:       int		):
									_a     :      Optional[int]        =							tempfile.mkdtemp()







							def 							__lowercase			(							self					:       str		):
									shutil.rmtree(self.output_dir		)







							def 							__lowercase			(							self					:       int	,_UpperCAmelCase					:       Dict=0	,_UpperCAmelCase					:       str=0	,_UpperCAmelCase					:       Dict=64	,_UpperCAmelCase					:       List[str]=64	,_UpperCAmelCase					:       Union[str, Any]=None	,_UpperCAmelCase					:       List[Any]=False	,**_UpperCAmelCase					:       int		):
									# disable_tqdm in TrainingArguments has a flaky default since it depends on the level of logging. We make sure
									# its set to False since the tests later on depend on its value.
									_a     :      int        =							RegressionDataset(length=_UpperCAmelCase		)
									_a     :      str        =							RegressionDataset(length=_UpperCAmelCase		)
									_a     :      List[Any]        =							RegressionModelConfig(a=_UpperCAmelCase	,b=_UpperCAmelCase		)
									_a     :      Optional[int]        =							RegressionPreTrainedModel(_UpperCAmelCase		)

									_a     :      Optional[int]        =							TrainingArguments(self.output_dir	,disable_tqdm=_UpperCAmelCase	,report_to=[]	,**_UpperCAmelCase		)
									return Trainer(
									    _UpperCAmelCase	,_UpperCAmelCase	,train_dataset=_UpperCAmelCase	,eval_dataset=_UpperCAmelCase	,callbacks=_UpperCAmelCase	,)







							def 							__lowercase			(							self					:       int	,_UpperCAmelCase					:       Tuple	,_UpperCAmelCase					:       Union[str, Any]		):
									self.assertEqual(len(_UpperCAmelCase		)	,len(_UpperCAmelCase		)		)

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

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







							def 							__lowercase			(							self					:       Tuple		):
									_a     :      List[str]        =							DEFAULT_CALLBACKS.copy() + [ProgressCallback]
									_a     :      Optional[int]        =							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		)

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

									_a     :      Tuple        =							self.get_trainer()
									_a     :      Optional[Any]        =							trainer.callback_handler.callbacks[0]
									_a     :      Optional[Any]        =							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 							__lowercase			(							self					:       List[Any]		):
									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		)

									_a     :      List[Any]        =							self.get_trainer(callbacks=[MyTestTrainerCallback]		)
									trainer.train()
									_a     :      Optional[int]        =							trainer.callback_handler.callbacks[-2].events
									self.assertEqual(_UpperCAmelCase	,self.get_expected_events(_UpperCAmelCase		)		)

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

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

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

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

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









'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


__lowerCAmelCase									=   {'''configuration_swin''': ['''SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SwinConfig''', '''SwinOnnxConfig''']}


try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__lowerCAmelCase									=   [
							    '''SWIN_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''SwinForImageClassification''',
							    '''SwinForMaskedImageModeling''',
							    '''SwinModel''',
							    '''SwinPreTrainedModel''',
							    '''SwinBackbone''',
							]

try:
							if not is_tf_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__lowerCAmelCase									=   [
							    '''TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''TFSwinForImageClassification''',
							    '''TFSwinForMaskedImageModeling''',
							    '''TFSwinModel''',
							    '''TFSwinPreTrainedModel''',
							]

if TYPE_CHECKING:
							from .configuration_swin import SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinConfig, SwinOnnxConfig

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_swin import (
														    SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
														    SwinBackbone,
														    SwinForImageClassification,
														    SwinForMaskedImageModeling,
														    SwinModel,
														    SwinPreTrainedModel,
														)

							try:
														if not is_tf_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_tf_swin import (
														    TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
														    TFSwinForImageClassification,
														    TFSwinForMaskedImageModeling,
														    TFSwinModel,
														    TFSwinPreTrainedModel,
														)

else:
							import sys

							__lowerCAmelCase									=   _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)




