





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_       (a_			):
	def __init__(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				[]
	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					**_A		):






				'''simple docstring'''







				self.events.append('''on_init_end'''		)
	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					**_A		):






				'''simple docstring'''







				self.events.append('''on_train_begin'''		)
	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					**_A		):






				'''simple docstring'''







				self.events.append('''on_train_end'''		)
	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					**_A		):






				'''simple docstring'''







				self.events.append('''on_epoch_begin'''		)
	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					**_A		):






				'''simple docstring'''







				self.events.append('''on_epoch_end'''		)
	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					**_A		):






				'''simple docstring'''







				self.events.append('''on_step_begin'''		)
	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					**_A		):






				'''simple docstring'''







				self.events.append('''on_step_end'''		)
	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					**_A		):






				'''simple docstring'''







				self.events.append('''on_evaluate'''		)
	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					**_A		):






				'''simple docstring'''







				self.events.append('''on_predict'''		)
	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					**_A		):






				'''simple docstring'''







				self.events.append('''on_save'''		)
	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					**_A		):






				'''simple docstring'''







				self.events.append('''on_log'''		)







	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					**_A		):






				'''simple docstring'''







				self.events.append('''on_prediction_step'''		)

@require_torch
class 	A_       (unittest.TestCase			):
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				tempfile.mkdtemp()
	def      _lowercase				(    self		):






				'''simple docstring'''







				shutil.rmtree(self.output_dir		)
	def      _lowercase				(    self							,					_A=0							,					_A=0							,					_A=6_4							,					_A=6_4							,					_A=None							,					_A=False							,					**_A		):






				'''simple docstring'''







				UpperCAmelCase   		=				RegressionDataset(length=_A		)
				UpperCAmelCase   		=				RegressionDataset(length=_A		)
				UpperCAmelCase   		=				RegressionModelConfig(a=_A							,					b=_A		)
				UpperCAmelCase   		=				RegressionPreTrainedModel(_A		)

				UpperCAmelCase   		=				TrainingArguments(self.output_dir							,					disable_tqdm=_A							,					report_to=[]							,					**_A		)
				return Trainer(
				    _A							,					_A							,					train_dataset=_A							,					eval_dataset=_A							,					callbacks=_A							,					)
	def      _lowercase				(    self							,					_A							,					_A		):






				'''simple docstring'''







				self.assertEqual(len(_A		)							,					len(_A		)		)

				# Order doesn't matter
				UpperCAmelCase   		=				sorted(_A							,					key=lambda _A		: cb.__name__ if isinstance(_A							,					_A		) else cb.__class__.__name__		)
				UpperCAmelCase   		=				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      _lowercase				(    self							,					_A		):






				'''simple docstring'''







				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(_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      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				self.get_trainer()
				UpperCAmelCase   		=				DEFAULT_CALLBACKS.copy() + [ProgressCallback]
				self.check_callbacks_equality(trainer.callback_handler.callbacks							,					_A		)

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






				'''simple docstring'''







				UpperCAmelCase   		=				DEFAULT_CALLBACKS.copy() + [ProgressCallback]
				UpperCAmelCase   		=				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		)

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

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






				'''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		)

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

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

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

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

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

				# A bit of everything
				UpperCAmelCase   		=				self.get_trainer(
				    callbacks=[MyTestTrainerCallback]							,					logging_steps=3							,					save_steps=1_0							,					eval_steps=5							,					evaluation_strategy='''steps'''							,					)
				trainer.train()
				UpperCAmelCase   		=				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:
							UpperCAmelCase   		=				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				:    int       =   logging.get_logger(__name__)

__A				:    Tuple       =   {
    "google/bigbird-roberta-base": "https://huggingface.co/google/bigbird-roberta-base/resolve/main/config.json",
    "google/bigbird-roberta-large": "https://huggingface.co/google/bigbird-roberta-large/resolve/main/config.json",
    "google/bigbird-base-trivia-itc": "https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/config.json",
    # See all BigBird models at https://huggingface.co/models?filter=big_bird
}

class 	A_       (a_			):
	UpperCAmelCase__					=			'''big_bird'''
	def __init__(    self							,					_A=5_0_3_5_8							,					_A=7_6_8							,					_A=1_2							,					_A=1_2							,					_A=3_0_7_2							,					_A="gelu_new"							,					_A=0.1							,					_A=0.1							,					_A=4_0_9_6							,					_A=2							,					_A=0.02							,					_A=1E-12							,					_A=True							,					_A=0							,					_A=1							,					_A=2							,					_A=6_6							,					_A="block_sparse"							,					_A=True							,					_A=False							,					_A=6_4							,					_A=3							,					_A=None							,					**_A							,					):






				'''simple docstring'''







				super().__init__(
				    pad_token_id=_A							,					bos_token_id=_A							,					eos_token_id=_A							,					sep_token_id=_A							,					**_A							,					)

				UpperCAmelCase   		=				vocab_size
				UpperCAmelCase   		=				max_position_embeddings
				UpperCAmelCase   		=				hidden_size
				UpperCAmelCase   		=				num_hidden_layers
				UpperCAmelCase   		=				num_attention_heads
				UpperCAmelCase   		=				intermediate_size
				UpperCAmelCase   		=				hidden_act
				UpperCAmelCase   		=				hidden_dropout_prob
				UpperCAmelCase   		=				attention_probs_dropout_prob
				UpperCAmelCase   		=				initializer_range
				UpperCAmelCase   		=				type_vocab_size
				UpperCAmelCase   		=				layer_norm_eps
				UpperCAmelCase   		=				use_cache

				UpperCAmelCase   		=				rescale_embeddings
				UpperCAmelCase   		=				attention_type
				UpperCAmelCase   		=				use_bias
				UpperCAmelCase   		=				block_size
				UpperCAmelCase   		=				num_random_blocks
				UpperCAmelCase   		=				classifier_dropout

class 	A_       (a_			):
	@property
	def      _lowercase				(    self		):






				'''simple docstring'''







				if self.task == "multiple-choice":
							UpperCAmelCase   		=				{0: '''batch''', 1: '''choice''', 2: '''sequence'''}
				else:
							UpperCAmelCase   		=				{0: '''batch''', 1: '''sequence'''}
				return OrderedDict(
				    [
				        ('''input_ids''', dynamic_axis),
				        ('''attention_mask''', dynamic_axis),
				    ]		)



