

'''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    snake_case     (      lowercase_		):






				"""simple docstring"""



				def __init__(    self       )     ->							Optional[Any]:
											SCREAMING_SNAKE_CASE_				=				[]



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     **_lowercase       )     ->							Any:
											self.events.append('on_init_end'       )



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     **_lowercase       )     ->							Tuple:
											self.events.append('on_train_begin'       )



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     **_lowercase       )     ->							str:
											self.events.append('on_train_end'       )



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     **_lowercase       )     ->							str:
											self.events.append('on_epoch_begin'       )



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     **_lowercase       )     ->							int:
											self.events.append('on_epoch_end'       )



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     **_lowercase       )     ->							Optional[int]:
											self.events.append('on_step_begin'       )



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     **_lowercase       )     ->							List[Any]:
											self.events.append('on_step_end'       )



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     **_lowercase       )     ->							Optional[int]:
											self.events.append('on_evaluate'       )



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     **_lowercase       )     ->							str:
											self.events.append('on_predict'       )



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     **_lowercase       )     ->							int:
											self.events.append('on_save'       )



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     **_lowercase       )     ->							List[str]:
											self.events.append('on_log'       )





				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     **_lowercase       )     ->							Tuple:
											self.events.append('on_prediction_step'       )






@require_torch
class    snake_case     (      unittest.TestCase		):






				"""simple docstring"""



				def   a__					(    self       )     ->							List[Any]:
											SCREAMING_SNAKE_CASE_				=				tempfile.mkdtemp()



				def   a__					(    self       )     ->							Tuple:
											shutil.rmtree(self.output_dir       )



				def   a__					(    self,     _lowercase=0,     _lowercase=0,     _lowercase=64,     _lowercase=64,     _lowercase=None,     _lowercase=False,     **_lowercase       )     ->							str:
											# 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.
											SCREAMING_SNAKE_CASE_				=				RegressionDataset(length=_lowercase       )
											SCREAMING_SNAKE_CASE_				=				RegressionDataset(length=_lowercase       )
											SCREAMING_SNAKE_CASE_				=				RegressionModelConfig(a=_lowercase,     b=_lowercase       )
											SCREAMING_SNAKE_CASE_				=				RegressionPreTrainedModel(_lowercase       )

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



				def   a__					(    self,     _lowercase,     _lowercase       )     ->							List[Any]:
											self.assertEqual(len(_lowercase       ),     len(_lowercase       )       )

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

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



				def   a__					(    self,     _lowercase       )     ->							Dict:
											SCREAMING_SNAKE_CASE_				=				['on_init_end', 'on_train_begin']
											SCREAMING_SNAKE_CASE_				=				0
											SCREAMING_SNAKE_CASE_				=				len(trainer.get_eval_dataloader()       )
											SCREAMING_SNAKE_CASE_				=				['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(_lowercase       ):
																									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       )     ->							int:
											SCREAMING_SNAKE_CASE_				=				self.get_trainer()
											SCREAMING_SNAKE_CASE_				=				DEFAULT_CALLBACKS.copy() + [ProgressCallback]
											self.check_callbacks_equality(trainer.callback_handler.callbacks,     _lowercase       )

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

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



				def   a__					(    self       )     ->							Any:
											SCREAMING_SNAKE_CASE_				=				DEFAULT_CALLBACKS.copy() + [ProgressCallback]
											SCREAMING_SNAKE_CASE_				=				self.get_trainer()

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

											SCREAMING_SNAKE_CASE_				=				self.get_trainer()
											SCREAMING_SNAKE_CASE_				=				trainer.pop_callback(_lowercase       )
											self.assertEqual(cb.__class__,     _lowercase       )
											self.check_callbacks_equality(trainer.callback_handler.callbacks,     _lowercase       )

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

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

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

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





				def   a__					(    self       )     ->							str:
											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=_lowercase       )

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

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

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

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

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

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

											# warning should be emitted for duplicated callbacks
											with patch('transformers.trainer_callback.logger.warning'       ) as warn_mock:
																		SCREAMING_SNAKE_CASE_				=				self.get_trainer(
																		    callbacks=[MyTestTrainerCallback, MyTestTrainerCallback],     )
																		assert str(_lowercase       ) in warn_mock.call_args[0][0]








'''simple docstring'''

from collections import OrderedDict
from typing import Mapping

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


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

SCREAMING_SNAKE_CASE							:				Tuple     	=			{
    "roberta-base": "https://huggingface.co/roberta-base/resolve/main/config.json",
    "roberta-large": "https://huggingface.co/roberta-large/resolve/main/config.json",
    "roberta-large-mnli": "https://huggingface.co/roberta-large-mnli/resolve/main/config.json",
    "distilroberta-base": "https://huggingface.co/distilroberta-base/resolve/main/config.json",
    "roberta-base-openai-detector": "https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json",
    "roberta-large-openai-detector": "https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json",
}






class    snake_case     (      lowercase_		):






				"""simple docstring"""
				_a    	=     """roberta"""



				def __init__(    self,     _lowercase=50265,     _lowercase=768,     _lowercase=12,     _lowercase=12,     _lowercase=3072,     _lowercase="gelu",     _lowercase=0.1,     _lowercase=0.1,     _lowercase=512,     _lowercase=2,     _lowercase=0.02,     _lowercase=1E-12,     _lowercase=1,     _lowercase=0,     _lowercase=2,     _lowercase="absolute",     _lowercase=True,     _lowercase=None,     **_lowercase,     )     ->							Dict:
											super().__init__(pad_token_id=_lowercase,     bos_token_id=_lowercase,     eos_token_id=_lowercase,     **_lowercase       )

											SCREAMING_SNAKE_CASE_				=				vocab_size
											SCREAMING_SNAKE_CASE_				=				hidden_size
											SCREAMING_SNAKE_CASE_				=				num_hidden_layers
											SCREAMING_SNAKE_CASE_				=				num_attention_heads
											SCREAMING_SNAKE_CASE_				=				hidden_act
											SCREAMING_SNAKE_CASE_				=				intermediate_size
											SCREAMING_SNAKE_CASE_				=				hidden_dropout_prob
											SCREAMING_SNAKE_CASE_				=				attention_probs_dropout_prob
											SCREAMING_SNAKE_CASE_				=				max_position_embeddings
											SCREAMING_SNAKE_CASE_				=				type_vocab_size
											SCREAMING_SNAKE_CASE_				=				initializer_range
											SCREAMING_SNAKE_CASE_				=				layer_norm_eps
											SCREAMING_SNAKE_CASE_				=				position_embedding_type
											SCREAMING_SNAKE_CASE_				=				use_cache
											SCREAMING_SNAKE_CASE_				=				classifier_dropout






class    snake_case     (      lowercase_		):






				"""simple docstring"""



				@property
				def   a__					(    self       )     ->							Mapping[str, Mapping[int, str]]:
											if self.task == "multiple-choice":
																		SCREAMING_SNAKE_CASE_				=				{0: 'batch', 1: 'choice', 2: 'sequence'}
											else:
																		SCREAMING_SNAKE_CASE_				=				{0: 'batch', 1: 'sequence'}
											return OrderedDict(
											    [
											        ('input_ids', dynamic_axis),
											        ('attention_mask', dynamic_axis),
											    ]       )





