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




					"""simple docstring"""

					def __init__(				self   :	Dict			):
									UpperCAmelCase__             =							[]







					def 		__lowerCAmelCase						(				self   :	int    ,lowerCamelCase__   :	Union[str, Any]    ,lowerCamelCase__   :	List[Any]    ,lowerCamelCase__   :	List[Any]    ,**lowerCamelCase__   :	int			):
									self.events.append('on_init_end'			)







					def 		__lowerCAmelCase						(				self   :	Optional[Any]    ,lowerCamelCase__   :	List[str]    ,lowerCamelCase__   :	Tuple    ,lowerCamelCase__   :	Any    ,**lowerCamelCase__   :	Optional[Any]			):
									self.events.append('on_train_begin'			)







					def 		__lowerCAmelCase						(				self   :	Dict    ,lowerCamelCase__   :	Union[str, Any]    ,lowerCamelCase__   :	Tuple    ,lowerCamelCase__   :	int    ,**lowerCamelCase__   :	int			):
									self.events.append('on_train_end'			)







					def 		__lowerCAmelCase						(				self   :	str    ,lowerCamelCase__   :	Union[str, Any]    ,lowerCamelCase__   :	Dict    ,lowerCamelCase__   :	Any    ,**lowerCamelCase__   :	Optional[Any]			):
									self.events.append('on_epoch_begin'			)







					def 		__lowerCAmelCase						(				self   :	str    ,lowerCamelCase__   :	Dict    ,lowerCamelCase__   :	str    ,lowerCamelCase__   :	Dict    ,**lowerCamelCase__   :	List[Any]			):
									self.events.append('on_epoch_end'			)







					def 		__lowerCAmelCase						(				self   :	Tuple    ,lowerCamelCase__   :	Tuple    ,lowerCamelCase__   :	str    ,lowerCamelCase__   :	Union[str, Any]    ,**lowerCamelCase__   :	Union[str, Any]			):
									self.events.append('on_step_begin'			)







					def 		__lowerCAmelCase						(				self   :	str    ,lowerCamelCase__   :	Any    ,lowerCamelCase__   :	Any    ,lowerCamelCase__   :	List[str]    ,**lowerCamelCase__   :	Optional[Any]			):
									self.events.append('on_step_end'			)







					def 		__lowerCAmelCase						(				self   :	Optional[int]    ,lowerCamelCase__   :	Optional[Any]    ,lowerCamelCase__   :	Optional[Any]    ,lowerCamelCase__   :	List[str]    ,**lowerCamelCase__   :	List[str]			):
									self.events.append('on_evaluate'			)







					def 		__lowerCAmelCase						(				self   :	Any    ,lowerCamelCase__   :	int    ,lowerCamelCase__   :	List[Any]    ,lowerCamelCase__   :	Union[str, Any]    ,**lowerCamelCase__   :	Union[str, Any]			):
									self.events.append('on_predict'			)







					def 		__lowerCAmelCase						(				self   :	Tuple    ,lowerCamelCase__   :	Union[str, Any]    ,lowerCamelCase__   :	Dict    ,lowerCamelCase__   :	Any    ,**lowerCamelCase__   :	Optional[Any]			):
									self.events.append('on_save'			)







					def 		__lowerCAmelCase						(				self   :	Tuple    ,lowerCamelCase__   :	List[str]    ,lowerCamelCase__   :	Optional[int]    ,lowerCamelCase__   :	Optional[int]    ,**lowerCamelCase__   :	Union[str, Any]			):
									self.events.append('on_log'			)







					def 		__lowerCAmelCase						(				self   :	Dict    ,lowerCamelCase__   :	Optional[Any]    ,lowerCamelCase__   :	Any    ,lowerCamelCase__   :	List[Any]    ,**lowerCamelCase__   :	Optional[int]			):
									self.events.append('on_prediction_step'			)






@require_torch
class 				snake_case       ( unittest.TestCase				):




					"""simple docstring"""

					def 		__lowerCAmelCase						(				self   :	Union[str, Any]			):
									UpperCAmelCase__             =							tempfile.mkdtemp()







					def 		__lowerCAmelCase						(				self   :	int			):
									shutil.rmtree(self.output_dir			)







					def 		__lowerCAmelCase						(				self   :	Optional[Any]    ,lowerCamelCase__   :	Optional[Any]=0    ,lowerCamelCase__   :	str=0    ,lowerCamelCase__   :	List[str]=64    ,lowerCamelCase__   :	Tuple=64    ,lowerCamelCase__   :	Union[str, Any]=None    ,lowerCamelCase__   :	Any=False    ,**lowerCamelCase__   :	List[Any]			):
									# 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=lowerCamelCase__			)
									UpperCAmelCase__             =							RegressionDataset(length=lowerCamelCase__			)
									UpperCAmelCase__             =							RegressionModelConfig(a=lowerCamelCase__    ,b=lowerCamelCase__			)
									UpperCAmelCase__             =							RegressionPreTrainedModel(lowerCamelCase__			)

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







					def 		__lowerCAmelCase						(				self   :	Optional[int]    ,lowerCamelCase__   :	Dict    ,lowerCamelCase__   :	List[str]			):
									self.assertEqual(len(lowerCamelCase__			)    ,len(lowerCamelCase__			)			)

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

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







					def 		__lowerCAmelCase						(				self   :	List[str]    ,lowerCamelCase__   :	Optional[int]			):
									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(lowerCamelCase__			):
																	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   :	Union[str, Any]			):
									UpperCAmelCase__             =							self.get_trainer()
									UpperCAmelCase__             =							DEFAULT_CALLBACKS.copy() + [ProgressCallback]
									self.check_callbacks_equality(trainer.callback_handler.callbacks    ,lowerCamelCase__			)

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

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







					def 		__lowerCAmelCase						(				self   :	Optional[int]			):
									UpperCAmelCase__             =							DEFAULT_CALLBACKS.copy() + [ProgressCallback]
									UpperCAmelCase__             =							self.get_trainer()

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

									UpperCAmelCase__             =							self.get_trainer()
									UpperCAmelCase__             =							trainer.pop_callback(lowerCamelCase__			)
									self.assertEqual(cb.__class__    ,lowerCamelCase__			)
									self.check_callbacks_equality(trainer.callback_handler.callbacks    ,lowerCamelCase__			)

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

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

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

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







					def 		__lowerCAmelCase						(				self   :	Tuple			):
									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=lowerCamelCase__			)

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

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

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

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

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

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

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




"""simple docstring"""






from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase__  :  int						     =	logging.get_logger(__name__)

lowerCAmelCase__  :  str						     =	{
    'facebook/xglm-564M': 'https://huggingface.co/facebook/xglm-564M/resolve/main/config.json',
    # See all XGLM models at https://huggingface.co/models?filter=xglm
}
class 				snake_case       ( __UpperCAmelCase				):




					"""simple docstring"""

					snake_case__				    = "xglm"
					snake_case__				    = ["past_key_values"]

					snake_case__				    = {
					    "num_attention_heads": "attention_heads",
					    "hidden_size": "d_model",
					    "num_hidden_layers": "num_layers",
					}







					def __init__(				self   :	Any    ,lowerCamelCase__   :	Any=256_008    ,lowerCamelCase__   :	Optional[Any]=2_048    ,lowerCamelCase__   :	List[str]=1_024    ,lowerCamelCase__   :	List[str]=4_096    ,lowerCamelCase__   :	Tuple=24    ,lowerCamelCase__   :	Optional[int]=16    ,lowerCamelCase__   :	int="gelu"    ,lowerCamelCase__   :	Dict=0.1    ,lowerCamelCase__   :	int=0.1    ,lowerCamelCase__   :	List[Any]=0.0    ,lowerCamelCase__   :	List[str]=0.0    ,lowerCamelCase__   :	Optional[Any]=0.0_2    ,lowerCamelCase__   :	List[str]=True    ,lowerCamelCase__   :	Optional[Any]=True    ,lowerCamelCase__   :	str=2    ,lowerCamelCase__   :	Dict=1    ,lowerCamelCase__   :	Optional[int]=0    ,lowerCamelCase__   :	Tuple=2    ,**lowerCamelCase__   :	List[Any]    ,):
									UpperCAmelCase__             =							vocab_size
									UpperCAmelCase__             =							max_position_embeddings
									UpperCAmelCase__             =							d_model
									UpperCAmelCase__             =							ffn_dim
									UpperCAmelCase__             =							num_layers
									UpperCAmelCase__             =							attention_heads
									UpperCAmelCase__             =							activation_function
									UpperCAmelCase__             =							dropout
									UpperCAmelCase__             =							attention_dropout
									UpperCAmelCase__             =							activation_dropout
									UpperCAmelCase__             =							layerdrop
									UpperCAmelCase__             =							init_std
									UpperCAmelCase__             =							scale_embedding  # scale factor will be sqrt(d_model) if True
									UpperCAmelCase__             =							use_cache

									super().__init__(
									    pad_token_id=lowerCamelCase__    ,bos_token_id=lowerCamelCase__    ,eos_token_id=lowerCamelCase__    ,decoder_start_token_id=lowerCamelCase__    ,**lowerCamelCase__    ,)



