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




  '''simple docstring'''



  def __init__(   self      :							str	)					->Union[str, Any]:
        UpperCAmelCase_      =	[]






  def 			lowerCAmelCase__			(   self      :							List[Any]	,				UpperCAmelCase__      :							Any	,				UpperCAmelCase__      :							Dict	,				UpperCAmelCase__      :							Dict	,				**UpperCAmelCase__      :							Dict	)					->List[str]:
        self.events.append('''on_init_end'''	)






  def 			lowerCAmelCase__			(   self      :							Optional[int]	,				UpperCAmelCase__      :							str	,				UpperCAmelCase__      :							Tuple	,				UpperCAmelCase__      :							Union[str, Any]	,				**UpperCAmelCase__      :							List[str]	)					->int:
        self.events.append('''on_train_begin'''	)






  def 			lowerCAmelCase__			(   self      :							Dict	,				UpperCAmelCase__      :							str	,				UpperCAmelCase__      :							str	,				UpperCAmelCase__      :							str	,				**UpperCAmelCase__      :							Optional[Any]	)					->Optional[int]:
        self.events.append('''on_train_end'''	)






  def 			lowerCAmelCase__			(   self      :							Optional[Any]	,				UpperCAmelCase__      :							List[Any]	,				UpperCAmelCase__      :							Dict	,				UpperCAmelCase__      :							List[Any]	,				**UpperCAmelCase__      :							Union[str, Any]	)					->str:
        self.events.append('''on_epoch_begin'''	)






  def 			lowerCAmelCase__			(   self      :							Tuple	,				UpperCAmelCase__      :							Optional[int]	,				UpperCAmelCase__      :							Optional[Any]	,				UpperCAmelCase__      :							Union[str, Any]	,				**UpperCAmelCase__      :							Any	)					->Optional[int]:
        self.events.append('''on_epoch_end'''	)






  def 			lowerCAmelCase__			(   self      :							Dict	,				UpperCAmelCase__      :							int	,				UpperCAmelCase__      :							Dict	,				UpperCAmelCase__      :							Optional[Any]	,				**UpperCAmelCase__      :							List[str]	)					->List[str]:
        self.events.append('''on_step_begin'''	)






  def 			lowerCAmelCase__			(   self      :							Optional[int]	,				UpperCAmelCase__      :							Optional[int]	,				UpperCAmelCase__      :							Optional[Any]	,				UpperCAmelCase__      :							List[Any]	,				**UpperCAmelCase__      :							int	)					->List[str]:
        self.events.append('''on_step_end'''	)






  def 			lowerCAmelCase__			(   self      :							List[str]	,				UpperCAmelCase__      :							Union[str, Any]	,				UpperCAmelCase__      :							List[Any]	,				UpperCAmelCase__      :							str	,				**UpperCAmelCase__      :							Optional[Any]	)					->Union[str, Any]:
        self.events.append('''on_evaluate'''	)






  def 			lowerCAmelCase__			(   self      :							Tuple	,				UpperCAmelCase__      :							Optional[int]	,				UpperCAmelCase__      :							Union[str, Any]	,				UpperCAmelCase__      :							str	,				**UpperCAmelCase__      :							str	)					->List[Any]:
        self.events.append('''on_predict'''	)






  def 			lowerCAmelCase__			(   self      :							Optional[Any]	,				UpperCAmelCase__      :							Optional[Any]	,				UpperCAmelCase__      :							str	,				UpperCAmelCase__      :							List[str]	,				**UpperCAmelCase__      :							Optional[Any]	)					->List[Any]:
        self.events.append('''on_save'''	)






  def 			lowerCAmelCase__			(   self      :							Any	,				UpperCAmelCase__      :							Optional[Any]	,				UpperCAmelCase__      :							Tuple	,				UpperCAmelCase__      :							Any	,				**UpperCAmelCase__      :							List[Any]	)					->Optional[Any]:
        self.events.append('''on_log'''	)






  def 			lowerCAmelCase__			(   self      :							Tuple	,				UpperCAmelCase__      :							Dict	,				UpperCAmelCase__      :							int	,				UpperCAmelCase__      :							int	,				**UpperCAmelCase__      :							Dict	)					->List[str]:
        self.events.append('''on_prediction_step'''	)







@require_torch
class lowerCamelCase				(						unittest.TestCase       ):




  '''simple docstring'''



  def 			lowerCAmelCase__			(   self      :							Optional[Any]	)					->Optional[int]:
        UpperCAmelCase_      =	tempfile.mkdtemp()






  def 			lowerCAmelCase__			(   self      :							List[Any]	)					->Dict:
        shutil.rmtree(self.output_dir	)






  def 			lowerCAmelCase__			(   self      :							Optional[Any]	,				UpperCAmelCase__      :							List[str]=0	,				UpperCAmelCase__      :							int=0	,				UpperCAmelCase__      :							str=64	,				UpperCAmelCase__      :							Any=64	,				UpperCAmelCase__      :							Tuple=None	,				UpperCAmelCase__      :							List[Any]=False	,				**UpperCAmelCase__      :							Dict	)					->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.
        UpperCAmelCase_      =	RegressionDataset(length=_lowercase	)
        UpperCAmelCase_      =	RegressionDataset(length=_lowercase	)
        UpperCAmelCase_      =	RegressionModelConfig(a=_lowercase	,				b=_lowercase	)
        UpperCAmelCase_      =	RegressionPreTrainedModel(_lowercase	)

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






  def 			lowerCAmelCase__			(   self      :							Union[str, Any]	,				UpperCAmelCase__      :							Optional[Any]	,				UpperCAmelCase__      :							Optional[Any]	)					->List[Any]:
        self.assertEqual(len(_lowercase	)	,				len(_lowercase	)	)

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

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






  def 			lowerCAmelCase__			(   self      :							Tuple	)					->Union[str, Any]:
        UpperCAmelCase_      =	DEFAULT_CALLBACKS.copy() + [ProgressCallback]
        UpperCAmelCase_      =	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	)

        UpperCAmelCase_      =	self.get_trainer()
        UpperCAmelCase_      =	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
        UpperCAmelCase_      =	self.get_trainer()
        UpperCAmelCase_      =	trainer.callback_handler.callbacks[0]
        trainer.remove_callback(_lowercase	)
        expected_callbacks.remove(_lowercase	)
        self.check_callbacks_equality(trainer.callback_handler.callbacks	,				_lowercase	)

        UpperCAmelCase_      =	self.get_trainer()
        UpperCAmelCase_      =	trainer.callback_handler.callbacks[0]
        UpperCAmelCase_      =	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 			lowerCAmelCase__			(   self      :							Tuple	)					->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=_lowercase	)

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

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

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

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

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

        # 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(_lowercase	,				self.get_expected_events(_lowercase	)	)

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











'''simple docstring'''
from ...configuration_utils import PretrainedConfig
from ...utils import logging


_SCREAMING_SNAKE_CASE			=						logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE			=						{
    "facebook/dpr-ctx_encoder-single-nq-base": (
        "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json"
    ),
    "facebook/dpr-question_encoder-single-nq-base": (
        "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json"
    ),
    "facebook/dpr-reader-single-nq-base": (
        "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json"
    ),
    "facebook/dpr-ctx_encoder-multiset-base": (
        "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json"
    ),
    "facebook/dpr-question_encoder-multiset-base": (
        "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json"
    ),
    "facebook/dpr-reader-multiset-base": (
        "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json"
    ),
}
class   __UpperCAmelCase				(     lowerCAmelCase				):



					'''simple docstring'''





					_UpperCamelCase							=		"""dpr"""



					def __init__(	self     :						Optional[Any]      ,       _lowercase     :						Dict=30_522      ,       _lowercase     :						Tuple=768      ,       _lowercase     :						int=12      ,       _lowercase     :						List[str]=12      ,       _lowercase     :						Union[str, Any]=3_072      ,       _lowercase     :						Dict="gelu"      ,       _lowercase     :						Dict=0.1      ,       _lowercase     :						Any=0.1      ,       _lowercase     :						Union[str, Any]=512      ,       _lowercase     :						Optional[int]=2      ,       _lowercase     :						Union[str, Any]=0.02      ,       _lowercase     :						List[Any]=1E-12      ,       _lowercase     :						List[str]=0      ,       _lowercase     :						Dict="absolute"      ,       _lowercase     :						int = 0      ,       **_lowercase     :						int      ,       )    ->							int:
								super().__init__(pad_token_id=_lowercase      ,       **_lowercase)

								A_									= vocab_size
								A_									= hidden_size
								A_									= num_hidden_layers
								A_									= num_attention_heads
								A_									= hidden_act
								A_									= intermediate_size
								A_									= hidden_dropout_prob
								A_									= attention_probs_dropout_prob
								A_									= max_position_embeddings
								A_									= type_vocab_size
								A_									= initializer_range
								A_									= layer_norm_eps
								A_									= projection_dim
								A_									= position_embedding_type




