
"""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 			_SCREAMING_SNAKE_CASE					(			A__							):






			def __init__(					self       )			->     Tuple:
						lowerCAmelCase_    :Dict			     =  []






			def  __lowerCAmelCase (					self       ,   __A       ,   __A       ,   __A       ,   **__A       )			->     List[str]:
						self.events.append("""on_init_end"""       )






			def  __lowerCAmelCase (					self       ,   __A       ,   __A       ,   __A       ,   **__A       )			->     Any:
						self.events.append("""on_train_begin"""       )






			def  __lowerCAmelCase (					self       ,   __A       ,   __A       ,   __A       ,   **__A       )			->     Tuple:
						self.events.append("""on_train_end"""       )






			def  __lowerCAmelCase (					self       ,   __A       ,   __A       ,   __A       ,   **__A       )			->     Any:
						self.events.append("""on_epoch_begin"""       )






			def  __lowerCAmelCase (					self       ,   __A       ,   __A       ,   __A       ,   **__A       )			->     Optional[Any]:
						self.events.append("""on_epoch_end"""       )






			def  __lowerCAmelCase (					self       ,   __A       ,   __A       ,   __A       ,   **__A       )			->     Optional[Any]:
						self.events.append("""on_step_begin"""       )






			def  __lowerCAmelCase (					self       ,   __A       ,   __A       ,   __A       ,   **__A       )			->     Any:
						self.events.append("""on_step_end"""       )






			def  __lowerCAmelCase (					self       ,   __A       ,   __A       ,   __A       ,   **__A       )			->     Any:
						self.events.append("""on_evaluate"""       )






			def  __lowerCAmelCase (					self       ,   __A       ,   __A       ,   __A       ,   **__A       )			->     int:
						self.events.append("""on_predict"""       )






			def  __lowerCAmelCase (					self       ,   __A       ,   __A       ,   __A       ,   **__A       )			->     Union[str, Any]:
						self.events.append("""on_save"""       )






			def  __lowerCAmelCase (					self       ,   __A       ,   __A       ,   __A       ,   **__A       )			->     Dict:
						self.events.append("""on_log"""       )






			def  __lowerCAmelCase (					self       ,   __A       ,   __A       ,   __A       ,   **__A       )			->     Optional[Any]:
						self.events.append("""on_prediction_step"""       )




@require_torch
class 			_SCREAMING_SNAKE_CASE					(			unittest.TestCase							):






			def  __lowerCAmelCase (					self       )			->     Tuple:
						lowerCAmelCase_    :Optional[int]			     =  tempfile.mkdtemp()






			def  __lowerCAmelCase (					self       )			->     Union[str, Any]:
						shutil.rmtree(self.output_dir       )






			def  __lowerCAmelCase (					self       ,   __A=0       ,   __A=0       ,   __A=64       ,   __A=64       ,   __A=None       ,   __A=False       ,   **__A       )			->     Optional[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.
						lowerCAmelCase_    :Optional[Any]			     =  RegressionDataset(length=__A       )
						lowerCAmelCase_    :str			     =  RegressionDataset(length=__A       )
						lowerCAmelCase_    :Dict			     =  RegressionModelConfig(a=__A       ,   b=__A       )
						lowerCAmelCase_    :Dict			     =  RegressionPreTrainedModel(__A       )

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






			def  __lowerCAmelCase (					self       ,   __A       ,   __A       )			->     List[Any]:
						self.assertEqual(len(__A       )       ,   len(__A       )       )

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

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






			def  __lowerCAmelCase (					self       )			->     Tuple:
						lowerCAmelCase_    :Dict			     =  DEFAULT_CALLBACKS.copy() + [ProgressCallback]
						lowerCAmelCase_    :Optional[int]			     =  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       )

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

						lowerCAmelCase_    :int			     =  self.get_trainer()
						lowerCAmelCase_    :Tuple			     =  trainer.callback_handler.callbacks[0]
						lowerCAmelCase_    :Dict			     =  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  __lowerCAmelCase (					self       )			->     Optional[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=__A       )

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

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

						lowerCAmelCase_    :Optional[Any]			     =  self.get_trainer(callbacks=[MyTestTrainerCallback]       ,   save_steps=5       )
						trainer.train()
						lowerCAmelCase_    :Dict			     =  trainer.callback_handler.callbacks[-2].events
						self.assertEqual(__A       ,   self.get_expected_events(__A       )       )

						lowerCAmelCase_    :Any			     =  self.get_trainer(callbacks=[MyTestTrainerCallback]       ,   eval_steps=5       ,   evaluation_strategy="""steps"""       )
						trainer.train()
						lowerCAmelCase_    :List[Any]			     =  trainer.callback_handler.callbacks[-2].events
						self.assertEqual(__A       ,   self.get_expected_events(__A       )       )

						lowerCAmelCase_    :Optional[Any]			     =  self.get_trainer(callbacks=[MyTestTrainerCallback]       ,   evaluation_strategy="""epoch"""       )
						trainer.train()
						lowerCAmelCase_    :Optional[Any]			     =  trainer.callback_handler.callbacks[-2].events
						self.assertEqual(__A       ,   self.get_expected_events(__A       )       )

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











from collections import OrderedDict
from typing import Mapping

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


A__  :  Union[str, Any]								=					logging.get_logger(__name__)

A__  :  Tuple								=					{
    '''facebook/xlm-roberta-xl''': '''https://huggingface.co/facebook/xlm-roberta-xl/resolve/main/config.json''',
    '''facebook/xlm-roberta-xxl''': '''https://huggingface.co/facebook/xlm-roberta-xxl/resolve/main/config.json''',
    # See all XLM-RoBERTa-XL models at https://huggingface.co/models?filter=xlm-roberta-xl
}



class 				__snake_case						(     UpperCamelCase_						):
							_a			      = '''xlm-roberta-xl'''




							def __init__(       self     :       int				,				A_     :       List[str]=2_5_0_8_8_0				,				A_     :       List[str]=2_5_6_0				,				A_     :       Optional[int]=3_6				,				A_     :       List[Any]=3_2				,				A_     :       Optional[int]=1_0_2_4_0				,				A_     :       Dict="gelu"				,				A_     :       int=0.1				,				A_     :       Optional[Any]=0.1				,				A_     :       int=5_1_4				,				A_     :       Any=1				,				A_     :       Optional[Any]=0.02				,				A_     :       str=1e-05				,				A_     :       Dict=1				,				A_     :       Any=0				,				A_     :       Tuple=2				,				A_     :       str="absolute"				,				A_     :       str=True				,				A_     :       List[str]=None				,				**A_     :       Dict				,				):
									super().__init__(pad_token_id=A_				,				bos_token_id=A_				,				eos_token_id=A_				,				**A_)
									lowerCAmelCase_			:     Tuple									= vocab_size
									lowerCAmelCase_			:     List[str]									= hidden_size
									lowerCAmelCase_			:     int									= num_hidden_layers
									lowerCAmelCase_			:     int									= num_attention_heads
									lowerCAmelCase_			:     Dict									= hidden_act
									lowerCAmelCase_			:     int									= intermediate_size
									lowerCAmelCase_			:     Tuple									= hidden_dropout_prob
									lowerCAmelCase_			:     Optional[Any]									= attention_probs_dropout_prob
									lowerCAmelCase_			:     Union[str, Any]									= max_position_embeddings
									lowerCAmelCase_			:     Dict									= type_vocab_size
									lowerCAmelCase_			:     str									= initializer_range
									lowerCAmelCase_			:     str									= layer_norm_eps
									lowerCAmelCase_			:     Optional[Any]									= position_embedding_type
									lowerCAmelCase_			:     Optional[Any]									= use_cache
									lowerCAmelCase_			:     List[str]									= classifier_dropout





class 				__snake_case						(     UpperCamelCase_						):




							@property
							def    UpperCAmelCase__			(       self     :       List[str]):
									if self.task == "multiple-choice":
											lowerCAmelCase_			:     Union[str, Any]									= {0: '''batch''', 1: '''choice''', 2: '''sequence'''}
									else:
											lowerCAmelCase_			:     List[Any]									= {0: '''batch''', 1: '''sequence'''}
									return OrderedDict(
									    [
									        ('''input_ids''', dynamic_axis),
									        ('''attention_mask''', dynamic_axis),
									    ])





