





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






   '''simple docstring'''





   def __init__(							self       ):
    snake_case__     :					str    =  []

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		**__SCREAMING_SNAKE_CASE       ):
    self.events.append("""on_init_end"""       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		**__SCREAMING_SNAKE_CASE       ):
    self.events.append("""on_train_begin"""       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		**__SCREAMING_SNAKE_CASE       ):
    self.events.append("""on_train_end"""       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		**__SCREAMING_SNAKE_CASE       ):
    self.events.append("""on_epoch_begin"""       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		**__SCREAMING_SNAKE_CASE       ):
    self.events.append("""on_epoch_end"""       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		**__SCREAMING_SNAKE_CASE       ):
    self.events.append("""on_step_begin"""       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		**__SCREAMING_SNAKE_CASE       ):
    self.events.append("""on_step_end"""       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		**__SCREAMING_SNAKE_CASE       ):
    self.events.append("""on_evaluate"""       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		**__SCREAMING_SNAKE_CASE       ):
    self.events.append("""on_predict"""       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		**__SCREAMING_SNAKE_CASE       ):
    self.events.append("""on_save"""       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		**__SCREAMING_SNAKE_CASE       ):
    self.events.append("""on_log"""       )


   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		**__SCREAMING_SNAKE_CASE       ):
    self.events.append("""on_prediction_step"""       )



@require_torch
class 					__snake_case  (					unittest.TestCase							):






   '''simple docstring'''





   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					Tuple    =  tempfile.mkdtemp()

   def 		__UpperCamelCase						(							self       ):
    shutil.rmtree(self.output_dir       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE=0	,		__SCREAMING_SNAKE_CASE=0	,		__SCREAMING_SNAKE_CASE=6_4	,		__SCREAMING_SNAKE_CASE=6_4	,		__SCREAMING_SNAKE_CASE=None	,		__SCREAMING_SNAKE_CASE=False	,		**__SCREAMING_SNAKE_CASE       ):
    # 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.
    snake_case__     :					List[Any]    =  RegressionDataset(length=__SCREAMING_SNAKE_CASE       )
    snake_case__     :					List[str]    =  RegressionDataset(length=__SCREAMING_SNAKE_CASE       )
    snake_case__     :					List[str]    =  RegressionModelConfig(a=__SCREAMING_SNAKE_CASE	,		b=__SCREAMING_SNAKE_CASE       )
    snake_case__     :					List[str]    =  RegressionPreTrainedModel(__SCREAMING_SNAKE_CASE       )

    snake_case__     :					Dict    =  TrainingArguments(self.output_dir	,		disable_tqdm=__SCREAMING_SNAKE_CASE	,		report_to=[]	,		**__SCREAMING_SNAKE_CASE       )
    return Trainer(
        __SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		train_dataset=__SCREAMING_SNAKE_CASE	,		eval_dataset=__SCREAMING_SNAKE_CASE	,		callbacks=__SCREAMING_SNAKE_CASE	,		)

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
    self.assertEqual(len(__SCREAMING_SNAKE_CASE       )	,		len(__SCREAMING_SNAKE_CASE       )       )

    # Order doesn't matter
    snake_case__     :					Tuple    =  sorted(__SCREAMING_SNAKE_CASE	,		key=lambda __SCREAMING_SNAKE_CASE       : cb.__name__ if isinstance(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ) else cb.__class__.__name__       )
    snake_case__     :					List[str]    =  sorted(__SCREAMING_SNAKE_CASE	,		key=lambda __SCREAMING_SNAKE_CASE       : cb.__name__ if isinstance(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ) else cb.__class__.__name__       )

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

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE       ):
    snake_case__     :					Tuple    =  ["""on_init_end""", """on_train_begin"""]
    snake_case__     :					Union[str, Any]    =  0
    snake_case__     :					Dict    =  len(trainer.get_eval_dataloader()       )
    snake_case__     :					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(__SCREAMING_SNAKE_CASE       ):
      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 		__UpperCamelCase						(							self       ):
    snake_case__     :					Any    =  self.get_trainer()
    snake_case__     :					str    =  DEFAULT_CALLBACKS.copy() + [ProgressCallback]
    self.check_callbacks_equality(trainer.callback_handler.callbacks	,		__SCREAMING_SNAKE_CASE       )

    # Callbacks passed at init are added to the default callbacks
    snake_case__     :					List[str]    =  self.get_trainer(callbacks=[MyTestTrainerCallback]       )
    expected_callbacks.append(__SCREAMING_SNAKE_CASE       )
    self.check_callbacks_equality(trainer.callback_handler.callbacks	,		__SCREAMING_SNAKE_CASE       )

    # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback
    snake_case__     :					Optional[Any]    =  self.get_trainer(disable_tqdm=__SCREAMING_SNAKE_CASE       )
    snake_case__     :					Tuple    =  DEFAULT_CALLBACKS.copy() + [PrinterCallback]
    self.check_callbacks_equality(trainer.callback_handler.callbacks	,		__SCREAMING_SNAKE_CASE       )

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					str    =  DEFAULT_CALLBACKS.copy() + [ProgressCallback]
    snake_case__     :					int    =  self.get_trainer()

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

    snake_case__     :					Union[str, Any]    =  self.get_trainer()
    snake_case__     :					List[str]    =  trainer.pop_callback(__SCREAMING_SNAKE_CASE       )
    self.assertEqual(cb.__class__	,		__SCREAMING_SNAKE_CASE       )
    self.check_callbacks_equality(trainer.callback_handler.callbacks	,		__SCREAMING_SNAKE_CASE       )

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

    # We can also add, pop, or remove by instance
    snake_case__     :					List[Any]    =  self.get_trainer()
    snake_case__     :					List[str]    =  trainer.callback_handler.callbacks[0]
    trainer.remove_callback(__SCREAMING_SNAKE_CASE       )
    expected_callbacks.remove(__SCREAMING_SNAKE_CASE       )
    self.check_callbacks_equality(trainer.callback_handler.callbacks	,		__SCREAMING_SNAKE_CASE       )

    snake_case__     :					Optional[int]    =  self.get_trainer()
    snake_case__     :					Any    =  trainer.callback_handler.callbacks[0]
    snake_case__     :					Optional[Any]    =  trainer.pop_callback(__SCREAMING_SNAKE_CASE       )
    self.assertEqual(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       )
    self.check_callbacks_equality(trainer.callback_handler.callbacks	,		__SCREAMING_SNAKE_CASE       )

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


   def 		__UpperCamelCase						(							self       ):
    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=__SCREAMING_SNAKE_CASE       )

    snake_case__     :					Any    =  self.get_trainer(callbacks=[MyTestTrainerCallback]       )
    trainer.train()
    snake_case__     :					Any    =  trainer.callback_handler.callbacks[-2].events
    self.assertEqual(__SCREAMING_SNAKE_CASE	,		self.get_expected_events(__SCREAMING_SNAKE_CASE       )       )

    # Independent log/save/eval
    snake_case__     :					Dict    =  self.get_trainer(callbacks=[MyTestTrainerCallback]	,		logging_steps=5       )
    trainer.train()
    snake_case__     :					int    =  trainer.callback_handler.callbacks[-2].events
    self.assertEqual(__SCREAMING_SNAKE_CASE	,		self.get_expected_events(__SCREAMING_SNAKE_CASE       )       )

    snake_case__     :					Any    =  self.get_trainer(callbacks=[MyTestTrainerCallback]	,		save_steps=5       )
    trainer.train()
    snake_case__     :					Any    =  trainer.callback_handler.callbacks[-2].events
    self.assertEqual(__SCREAMING_SNAKE_CASE	,		self.get_expected_events(__SCREAMING_SNAKE_CASE       )       )

    snake_case__     :					Tuple    =  self.get_trainer(callbacks=[MyTestTrainerCallback]	,		eval_steps=5	,		evaluation_strategy="""steps"""       )
    trainer.train()
    snake_case__     :					str    =  trainer.callback_handler.callbacks[-2].events
    self.assertEqual(__SCREAMING_SNAKE_CASE	,		self.get_expected_events(__SCREAMING_SNAKE_CASE       )       )

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

    # A bit of everything
    snake_case__     :					Dict    =  self.get_trainer(
        callbacks=[MyTestTrainerCallback]	,		logging_steps=3	,		save_steps=1_0	,		eval_steps=5	,		evaluation_strategy="""steps"""	,		)
    trainer.train()
    snake_case__     :					Tuple    =  trainer.callback_handler.callbacks[-2].events
    self.assertEqual(__SCREAMING_SNAKE_CASE	,		self.get_expected_events(__SCREAMING_SNAKE_CASE       )       )

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











"""simple docstring"""



import functools
import operator

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


a_					=     logging.get_logger(__name__)

a_					=     {
    """microsoft/unispeech-large-1500h-cv""": (
        """https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json"""
    ),
    # See all UniSpeech models at https://huggingface.co/models?filter=unispeech
}






class 						__snake_case   (						SCREAMING_SNAKE_CASE__	):





 """simple docstring"""


 _lowerCamelCase								=	"""unispeech"""

 def __init__(       self   ,		__lowerCamelCase=32   ,		__lowerCamelCase=768   ,		__lowerCamelCase=12   ,		__lowerCamelCase=12   ,		__lowerCamelCase=3072   ,		__lowerCamelCase="gelu"   ,		__lowerCamelCase=0.1   ,		__lowerCamelCase=0.1   ,		__lowerCamelCase=0.1   ,		__lowerCamelCase=0.0   ,		__lowerCamelCase=0.0   ,		__lowerCamelCase=0.1   ,		__lowerCamelCase=0.1   ,		__lowerCamelCase=0.0_2   ,		__lowerCamelCase=1e-5   ,		__lowerCamelCase="group"   ,		__lowerCamelCase="gelu"   ,		__lowerCamelCase=(512, 512, 512, 512, 512, 512, 512)   ,		__lowerCamelCase=(5, 2, 2, 2, 2, 2, 2)   ,		__lowerCamelCase=(10, 3, 3, 3, 3, 2, 2)   ,		__lowerCamelCase=False   ,		__lowerCamelCase=128   ,		__lowerCamelCase=16   ,		__lowerCamelCase=False   ,		__lowerCamelCase=True   ,		__lowerCamelCase=0.0_5   ,		__lowerCamelCase=10   ,		__lowerCamelCase=2   ,		__lowerCamelCase=0.0   ,		__lowerCamelCase=10   ,		__lowerCamelCase=0   ,		__lowerCamelCase=320   ,		__lowerCamelCase=2   ,		__lowerCamelCase=0.1   ,		__lowerCamelCase=100   ,		__lowerCamelCase=256   ,		__lowerCamelCase=256   ,		__lowerCamelCase=0.1   ,		__lowerCamelCase="mean"   ,		__lowerCamelCase=False   ,		__lowerCamelCase=False   ,		__lowerCamelCase=256   ,		__lowerCamelCase=80   ,		__lowerCamelCase=0   ,		__lowerCamelCase=1   ,		__lowerCamelCase=2   ,		__lowerCamelCase=0.5   ,		**__lowerCamelCase   ,		):



     '''simple docstring'''




     super().__init__(**__lowerCamelCase   ,		pad_token_id=__lowerCamelCase   ,		bos_token_id=__lowerCamelCase   ,		eos_token_id=__lowerCamelCase    )
     __A       :		Dict							=	hidden_size
     __A       :		Dict							=	feat_extract_norm
     __A       :		int							=	feat_extract_activation
     __A       :		Dict							=	list(__lowerCamelCase    )
     __A       :		str							=	list(__lowerCamelCase    )
     __A       :		Dict							=	list(__lowerCamelCase    )
     __A       :		str							=	conv_bias
     __A       :		Optional[int]							=	num_conv_pos_embeddings
     __A       :		int							=	num_conv_pos_embedding_groups
     __A       :		Union[str, Any]							=	len(self.conv_dim    )
     __A       :		List[str]							=	num_hidden_layers
     __A       :		Dict							=	intermediate_size
     __A       :		List[str]							=	hidden_act
     __A       :		int							=	num_attention_heads
     __A       :		str							=	hidden_dropout
     __A       :		str							=	attention_dropout
     __A       :		Optional[Any]							=	activation_dropout
     __A       :		Optional[int]							=	feat_proj_dropout
     __A       :		Optional[Any]							=	final_dropout
     __A       :		List[Any]							=	layerdrop
     __A       :		Any							=	layer_norm_eps
     __A       :		str							=	initializer_range
     __A       :		List[str]							=	num_ctc_classes
     __A       :		Dict							=	vocab_size
     __A       :		Dict							=	do_stable_layer_norm
     __A       :		Union[str, Any]							=	use_weighted_layer_sum
     __A       :		Tuple							=	classifier_proj_size

     if (
         (len(self.conv_stride    ) != self.num_feat_extract_layers)
         or (len(self.conv_kernel    ) != self.num_feat_extract_layers)
         or (len(self.conv_dim    ) != self.num_feat_extract_layers)
     ):
         raise ValueError(
             '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =='''
             ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ='''
             F""" {len(self.conv_dim    )}`, `len(config.conv_stride) = {len(self.conv_stride    )}`,"""
             F""" `len(config.conv_kernel) = {len(self.conv_kernel    )}`."""    )

     # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
     __A       :		List[Any]							=	apply_spec_augment
     __A       :		str							=	mask_time_prob
     __A       :		Dict							=	mask_time_length
     __A       :		List[str]							=	mask_time_min_masks
     __A       :		Union[str, Any]							=	mask_feature_prob
     __A       :		Tuple							=	mask_feature_length
     __A       :		Any							=	mask_feature_min_masks

     # parameters for pretraining with codevector quantized representations
     __A       :		int							=	num_codevectors_per_group
     __A       :		Any							=	num_codevector_groups
     __A       :		Optional[Any]							=	contrastive_logits_temperature
     __A       :		Dict							=	feat_quantizer_dropout
     __A       :		Tuple							=	num_negatives
     __A       :		List[str]							=	codevector_dim
     __A       :		str							=	proj_codevector_dim
     __A       :		Union[str, Any]							=	diversity_loss_weight

     # ctc loss
     __A       :		Tuple							=	ctc_loss_reduction
     __A       :		Optional[int]							=	ctc_zero_infinity

     # pretraining loss
     __A       :		str							=	replace_prob







 @property
 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     return functools.reduce(operator.mul   ,		self.conv_stride   ,		1    )




