


import logging
import sys
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Union

import librosa
import torch
from datasets import DatasetDict, load_dataset
from packaging import version
from torch import nn

from transformers import (
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaForPreTraining,
    is_apex_available,
    trainer_utils,
)
from transformers.models.wavaveca.modeling_wavaveca import _compute_mask_indices


if is_apex_available():
 from apex import amp

if version.parse(version.parse(torch.__version__).base_version) >= version.parse('1.6'):
 lowerCamelCase		   =			True
 from torch.cuda.amp import autocast


lowerCamelCase		   =			logging.getLogger(__name__)




@dataclass
class A    :
      UpperCamelCase__				:    str =field(
          metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'}	)
      UpperCamelCase__				:    Optional[str] =field(
          default=UpperCamelCase_       ,				metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'}       ,				)
      UpperCamelCase__				:    Optional[bool] =field(
          default=UpperCamelCase_       ,				metadata={'help': 'Whether to freeze the feature extractor layers of the model.'}	)
      UpperCamelCase__				:    Optional[bool] =field(
          default=UpperCamelCase_       ,				metadata={'help': 'Whether to log verbose messages or not.'}       ,				)
      UpperCamelCase__				:    Optional[float] =field(
          default=2.0       ,				metadata={'help': 'Maximum temperature for gumbel softmax.'}	)
      UpperCamelCase__				:    Optional[float] =field(
          default=0.5       ,				metadata={'help': 'Minimum temperature for gumbel softmax.'}	)
      UpperCamelCase__				:    Optional[float] =field(
          default=0.999995       ,				metadata={'help': 'Decay of gumbel temperature during training.'}	)




def       a_ (       SCREAMING_SNAKE_CASE__   : ModelArguments						,     SCREAMING_SNAKE_CASE__   : TrainingArguments	):
       '''simple docstring'''





       logging.basicConfig(
           format='%(asctime)s - %(levelname)s - %(name)s - %(message)s'						,     datefmt='%m/%d/%Y %H:%M:%S'						,     handlers=[logging.StreamHandler(sys.stdout	)]						,     )
       _lowerCamelCase			:						Optional[int]            =logging.WARNING
       if model_args.verbose_logging:
              _lowerCamelCase			:						Union[str, Any]            =logging.DEBUG
       elif trainer_utils.is_main_process(training_args.local_rank	):
              _lowerCamelCase			:						Optional[Any]            =logging.INFO
       logger.setLevel(SCREAMING_SNAKE_CASE__	)




@dataclass
class A    :
      UpperCamelCase__				:    str =field(
          default=UpperCamelCase_       ,				metadata={'help': 'The name of the dataset to use (via the datasets library).'}	)
      UpperCamelCase__				:    Optional[str] =field(
          default=UpperCamelCase_       ,				metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'}	)
      UpperCamelCase__				:    Optional[str] =field(
          default='train'       ,				metadata={
              'help': 'The name of the training data set split to use (via the datasets library). Defaults to \'train\''
          }       ,				)
      UpperCamelCase__				:    Optional[str] =field(
          default='validation'       ,				metadata={
              'help': (
                  'The name of the validation data set split to use (via the datasets library). Defaults to \'validation\''
              )
          }       ,				)
      UpperCamelCase__				:    Optional[str] =field(
          default='file'       ,				metadata={'help': 'Column in the dataset that contains speech file path. Defaults to \'file\''}       ,				)
      UpperCamelCase__				:    bool =field(
          default=UpperCamelCase_       ,				metadata={'help': 'Overwrite the cached preprocessed datasets or not.'}	)
      UpperCamelCase__				:    Optional[int] =field(
          default=1       ,				metadata={
              'help': 'The percentage of the train set used as validation set in case there\'s no validation split'
          }       ,				)
      UpperCamelCase__				:    Optional[int] =field(
          default=UpperCamelCase_       ,				metadata={'help': 'The number of processes to use for the preprocessing.'}       ,				)
      UpperCamelCase__				:    Optional[float] =field(
          default=20.0       ,				metadata={'help': 'Filter audio files that are longer than `max_duration_in_seconds` seconds'}	)




@dataclass
class A    :
      UpperCamelCase__				:    WavaVecaForPreTraining
      UpperCamelCase__				:    WavaVecaFeatureExtractor
      UpperCamelCase__				:    Union[bool, str] ="longest"
      UpperCamelCase__				:    Optional[int] =None
      UpperCamelCase__				:    Optional[int] =None






      def __call__(   self		:	Optional[Any]     ,  lowercase_		:	List[Dict[str, Union[List[int], torch.Tensor]]]					)   ->		Dict[str, torch.Tensor]:
             """simple docstring"""




             _lowerCamelCase			:						List[str]            =self.feature_extractor.pad(
                 lowercase_     ,  max_length=self.max_length     ,  padding=self.padding     ,  pad_to_multiple_of=self.pad_to_multiple_of     ,  return_tensors='pt'     ,  )
             _lowerCamelCase			:						Optional[Any]            =self.model._get_feat_extract_output_lengths(batch['input_values'].shape[-1]					)

             _lowerCamelCase			:						Dict            =batch['input_values'].shape[0]

             # make sure that no loss is computed on padded inputs
             if batch["attention_mask"] is not None:
                    # compute real output lengths according to convolution formula
                    _lowerCamelCase			:						List[Any]            =self.model._get_feat_extract_output_lengths(batch['attention_mask'].sum(-1					)					).to(
                        torch.long					)

                    _lowerCamelCase			:						Optional[int]            =torch.zeros(
                        (batch_size, mask_indices_seq_length)     ,  dtype=torch.long     ,  device=batch['input_values'].device					)

                    # these two operations makes sure that all values
                    # before the output lengths indices are attended to
                    _lowerCamelCase			:						List[Any]            =1
                    _lowerCamelCase			:						Tuple            =attention_mask.flip([-1]					).cumsum(-1					).flip([-1]					).bool()

             # sample randomly masked indices
             _lowerCamelCase			:						List[str]            =_compute_mask_indices(
                 (batch_size, mask_indices_seq_length)     ,  self.model.config.mask_time_prob     ,  self.model.config.mask_time_length     ,  attention_mask=lowercase_     ,  min_masks=2     ,  )

             return batch




class A    (  UpperCamelCase_	):






      def __init__(   self		:	Tuple     ,  *lowercase_		:	List[str]     ,  lowercase_		:	Any=1     ,  lowercase_		:	Union[str, Any]=0     ,  lowercase_		:	Optional[Any]=1.0     ,  **lowercase_		:	int					)   ->		Tuple:
             """simple docstring"""




             super().__init__(*lowercase_     ,  **lowercase_					)
             _lowerCamelCase			:						List[str]            =0
             _lowerCamelCase			:						Any            =max_gumbel_temp
             _lowerCamelCase			:						Union[str, Any]            =min_gumbel_temp
             _lowerCamelCase			:						Optional[int]            =gumbel_temp_decay






      def 			lowerCamelCase			(   self		:	List[Any]     ,  lowercase_		:	nn.Module     ,  lowercase_		:	Dict[str, Union[torch.Tensor, Any]]					)   ->		torch.Tensor:
             """simple docstring"""




             model.train()
             _lowerCamelCase			:						int            =self._prepare_inputs(lowercase_					)

             if self.use_amp:
                    with autocast():
                           _lowerCamelCase			:						Optional[Any]            =self.compute_loss(lowercase_     ,  lowercase_					)
             else:
                    _lowerCamelCase			:						List[Any]            =self.compute_loss(lowercase_     ,  lowercase_					)

             if self.args.n_gpu > 1 or self.deepspeed:
                    if model.module.config.ctc_loss_reduction == "mean":
                           _lowerCamelCase			:						Union[str, Any]            =loss.mean()
                    elif model.module.config.ctc_loss_reduction == "sum":
                           _lowerCamelCase			:						Union[str, Any]            =loss.sum() / (inputs['mask_time_indices']).sum()
                    else:
                           raise ValueError(F'''{model.config.ctc_loss_reduction} is not valid. Choose one of [\'mean\', \'sum\']'''					)

             if self.args.gradient_accumulation_steps > 1:
                    _lowerCamelCase			:						int            =loss / self.args.gradient_accumulation_steps

             if self.use_amp:
                    self.scaler.scale(lowercase_					).backward()
             elif self.use_apex:
                    with amp.scale_loss(lowercase_     ,  self.optimizer					) as scaled_loss:
                           scaled_loss.backward()
             elif self.deepspeed:
                    self.deepspeed.backward(lowercase_					)
             else:
                    loss.backward()

             self.num_update_step += 1
             # make sure gumbel softmax temperature is decayed
             if self.args.n_gpu > 1 or self.deepspeed:
                    model.module.set_gumbel_temperature(
                        max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step     ,  self.min_gumbel_temp					)					)
             else:
                    model.set_gumbel_temperature(
                        max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step     ,  self.min_gumbel_temp					)					)

             return loss.detach()




def       a_ (       ):
       '''simple docstring'''





       _lowerCamelCase			:						Tuple            =HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)	)

       _lowerCamelCase      ,	_lowerCamelCase      ,	_lowerCamelCase			:						List[str]            =parser.parse_args_into_dataclasses()
       configure_logger(SCREAMING_SNAKE_CASE__						,     SCREAMING_SNAKE_CASE__	)

       # Downloading and loading a dataset from the hub.
       _lowerCamelCase			:						str            =load_dataset(data_args.dataset_name						,     data_args.dataset_config_name						,     cache_dir=model_args.cache_dir	)

       if "validation" not in datasets.keys():
              # make sure only "validation" and "train" keys remain"
              _lowerCamelCase			:						Union[str, Any]            =DatasetDict()
              _lowerCamelCase			:						str            =load_dataset(
                  data_args.dataset_name						,     data_args.dataset_config_name						,     split=F'''{data_args.train_split_name}[:{data_args.validation_split_percentage}%]'''						,     cache_dir=model_args.cache_dir						,     )
              _lowerCamelCase			:						List[Any]            =load_dataset(
                  data_args.dataset_name						,     data_args.dataset_config_name						,     split=F'''{data_args.train_split_name}[{data_args.validation_split_percentage}%:]'''						,     cache_dir=model_args.cache_dir						,     )
       else:
              # make sure only "validation" and "train" keys remain"
              _lowerCamelCase			:						Tuple            =DatasetDict()
              _lowerCamelCase			:						Optional[Any]            =load_dataset(
                  data_args.dataset_name						,     data_args.dataset_config_name						,     split='validation'						,     cache_dir=model_args.cache_dir						,     )
              _lowerCamelCase			:						int            =load_dataset(
                  data_args.dataset_name						,     data_args.dataset_config_name						,     split=F'''{data_args.train_split_name}'''						,     cache_dir=model_args.cache_dir						,     )

       # only normalized-inputs-training is supported
       _lowerCamelCase			:						List[str]            =WavaVecaFeatureExtractor.from_pretrained(
           model_args.model_name_or_path						,     cache_dir=model_args.cache_dir						,     do_normalize=SCREAMING_SNAKE_CASE__	)

       def prepare_dataset(SCREAMING_SNAKE_CASE__   : Union[str, Any]	):
              # check that all files have the correct sampling rate
              _lowerCamelCase      ,	_lowerCamelCase			:						Optional[int]            =librosa.load(batch[data_args.speech_file_column]						,     sr=feature_extractor.sampling_rate	)
              return batch

       # load audio files into numpy arrays
       _lowerCamelCase			:						Tuple            =datasets.map(
           SCREAMING_SNAKE_CASE__						,     num_proc=data_args.preprocessing_num_workers						,     remove_columns=datasets['train'].column_names	)

       # filter audio files that are too long
       _lowerCamelCase			:						List[str]            =vectorized_datasets.filter(
           lambda SCREAMING_SNAKE_CASE__	: len(data['speech']	) < int(data_args.max_duration_in_seconds * feature_extractor.sampling_rate	)	)

       def normalize(SCREAMING_SNAKE_CASE__   : List[str]	):
              return feature_extractor(batch['speech']						,     sampling_rate=feature_extractor.sampling_rate	)

       # normalize and transform to `BatchFeatures`
       _lowerCamelCase			:						Dict            =vectorized_datasets.map(
           SCREAMING_SNAKE_CASE__						,     batched=SCREAMING_SNAKE_CASE__						,     num_proc=data_args.preprocessing_num_workers						,     load_from_cache_file=not data_args.overwrite_cache						,     remove_columns=vectorized_datasets['train'].column_names						,     )

       # pretraining is only supported for "newer" stable layer norm architecture
       # apply_spec_augment has to be True, mask_feature_prob has to be 0.0
       _lowerCamelCase			:						List[Any]            =WavaVecaConfig.from_pretrained(
           model_args.model_name_or_path						,     cache_dir=model_args.cache_dir						,     gradient_checkpointing=training_args.gradient_checkpointing						,     )

       if not config.do_stable_layer_norm or config.feat_extract_norm != "layer":
              raise ValueError(
                  'PreTraining is only supported for ``config.do_stable_layer_norm=True`` and'
                  ' ``config.feat_extract_norm=\'layer\''	)

       _lowerCamelCase			:						List[Any]            =WavaVecaForPreTraining(SCREAMING_SNAKE_CASE__	)

       _lowerCamelCase			:						List[Any]            =DataCollatorForWavaVecaPretraining(model=SCREAMING_SNAKE_CASE__						,     feature_extractor=SCREAMING_SNAKE_CASE__	)

       _lowerCamelCase			:						Optional[Any]            =WavaVecaPreTrainer(
           model=SCREAMING_SNAKE_CASE__						,     data_collator=SCREAMING_SNAKE_CASE__						,     args=SCREAMING_SNAKE_CASE__						,     train_dataset=vectorized_datasets['train']						,     eval_dataset=vectorized_datasets['validation']						,     tokenizer=SCREAMING_SNAKE_CASE__						,     max_gumbel_temp=model_args.max_gumbel_temperature						,     min_gumbel_temp=model_args.min_gumbel_temperature						,     gumbel_temp_decay=model_args.gumbel_temperature_decay						,     )
       trainer.train()


if __name__ == "__main__":
 main()






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    (  UpperCamelCase_	):






      def __init__(   self		:	str					)   ->		Union[str, Any]:
             """simple docstring"""




             _lowerCamelCase			:						Optional[Any]            =[]






      def 			lowerCamelCase			(   self		:	Dict     ,  lowercase_		:	List[Any]     ,  lowercase_		:	Optional[Any]     ,  lowercase_		:	Optional[int]     ,  **lowercase_		:	List[Any]					)   ->		List[Any]:
             """simple docstring"""




             self.events.append('on_init_end'					)






      def 			lowerCamelCase			(   self		:	int     ,  lowercase_		:	Tuple     ,  lowercase_		:	Any     ,  lowercase_		:	Optional[Any]     ,  **lowercase_		:	Any					)   ->		Union[str, Any]:
             """simple docstring"""




             self.events.append('on_train_begin'					)






      def 			lowerCamelCase			(   self		:	Optional[int]     ,  lowercase_		:	Dict     ,  lowercase_		:	Dict     ,  lowercase_		:	Tuple     ,  **lowercase_		:	str					)   ->		Optional[Any]:
             """simple docstring"""




             self.events.append('on_train_end'					)






      def 			lowerCamelCase			(   self		:	Optional[Any]     ,  lowercase_		:	str     ,  lowercase_		:	Dict     ,  lowercase_		:	int     ,  **lowercase_		:	List[str]					)   ->		Dict:
             """simple docstring"""




             self.events.append('on_epoch_begin'					)






      def 			lowerCamelCase			(   self		:	Dict     ,  lowercase_		:	Any     ,  lowercase_		:	Dict     ,  lowercase_		:	List[Any]     ,  **lowercase_		:	Dict					)   ->		Optional[Any]:
             """simple docstring"""




             self.events.append('on_epoch_end'					)






      def 			lowerCamelCase			(   self		:	List[str]     ,  lowercase_		:	List[Any]     ,  lowercase_		:	Optional[Any]     ,  lowercase_		:	Any     ,  **lowercase_		:	Dict					)   ->		Tuple:
             """simple docstring"""




             self.events.append('on_step_begin'					)






      def 			lowerCamelCase			(   self		:	List[str]     ,  lowercase_		:	Dict     ,  lowercase_		:	str     ,  lowercase_		:	Optional[Any]     ,  **lowercase_		:	Optional[int]					)   ->		Tuple:
             """simple docstring"""




             self.events.append('on_step_end'					)






      def 			lowerCamelCase			(   self		:	Tuple     ,  lowercase_		:	int     ,  lowercase_		:	List[Any]     ,  lowercase_		:	Union[str, Any]     ,  **lowercase_		:	List[Any]					)   ->		Any:
             """simple docstring"""




             self.events.append('on_evaluate'					)






      def 			lowerCamelCase			(   self		:	Tuple     ,  lowercase_		:	Union[str, Any]     ,  lowercase_		:	Dict     ,  lowercase_		:	List[str]     ,  **lowercase_		:	Dict					)   ->		Dict:
             """simple docstring"""




             self.events.append('on_predict'					)






      def 			lowerCamelCase			(   self		:	Union[str, Any]     ,  lowercase_		:	List[str]     ,  lowercase_		:	Optional[int]     ,  lowercase_		:	List[Any]     ,  **lowercase_		:	Optional[int]					)   ->		Any:
             """simple docstring"""




             self.events.append('on_save'					)






      def 			lowerCamelCase			(   self		:	str     ,  lowercase_		:	List[Any]     ,  lowercase_		:	Any     ,  lowercase_		:	Optional[int]     ,  **lowercase_		:	Optional[int]					)   ->		List[str]:
             """simple docstring"""




             self.events.append('on_log'					)






      def 			lowerCamelCase			(   self		:	Any     ,  lowercase_		:	List[Any]     ,  lowercase_		:	int     ,  lowercase_		:	List[Any]     ,  **lowercase_		:	str					)   ->		List[Any]:
             """simple docstring"""




             self.events.append('on_prediction_step'					)




@require_torch
class A    (  unittest.TestCase	):






      def 			lowerCamelCase			(   self		:	List[Any]					)   ->		Union[str, Any]:
             """simple docstring"""




             _lowerCamelCase			:						Dict            =tempfile.mkdtemp()






      def 			lowerCamelCase			(   self		:	Union[str, Any]					)   ->		int:
             """simple docstring"""




             shutil.rmtree(self.output_dir					)






      def 			lowerCamelCase			(   self		:	Optional[Any]     ,  lowercase_		:	Optional[int]=0     ,  lowercase_		:	int=0     ,  lowercase_		:	Dict=64     ,  lowercase_		:	List[str]=64     ,  lowercase_		:	int=None     ,  lowercase_		:	Tuple=False     ,  **lowercase_		:	Dict					)   ->		Optional[int]:
             """simple docstring"""




             _lowerCamelCase			:						int            =RegressionDataset(length=lowercase_					)
             _lowerCamelCase			:						Optional[Any]            =RegressionDataset(length=lowercase_					)
             _lowerCamelCase			:						str            =RegressionModelConfig(a=lowercase_     ,  b=lowercase_					)
             _lowerCamelCase			:						Optional[Any]            =RegressionPreTrainedModel(lowercase_					)

             _lowerCamelCase			:						str            =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		:	List[str]     ,  lowercase_		:	int     ,  lowercase_		:	int					)   ->		str:
             """simple docstring"""




             self.assertEqual(len(lowercase_					)     ,  len(lowercase_					)					)

             # Order doesn't matter
             _lowerCamelCase			:						Any            =sorted(lowercase_     ,  key=lambda lowercase_					: cb.__name__ if isinstance(lowercase_     ,  lowercase_					) else cb.__class__.__name__					)
             _lowerCamelCase			:						Any            =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 			lowerCamelCase			(   self		:	Dict     ,  lowercase_		:	List[str]					)   ->		str:
             """simple docstring"""




             _lowerCamelCase			:						List[Any]            =['on_init_end', 'on_train_begin']
             _lowerCamelCase			:						Dict            =0
             _lowerCamelCase			:						str            =len(trainer.get_eval_dataloader()					)
             _lowerCamelCase			:						List[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(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		:	Optional[Any]					)   ->		List[str]:
             """simple docstring"""




             _lowerCamelCase			:						Any            =self.get_trainer()
             _lowerCamelCase			:						Optional[Any]            =DEFAULT_CALLBACKS.copy() + [ProgressCallback]
             self.check_callbacks_equality(trainer.callback_handler.callbacks     ,  lowercase_					)

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






      def 			lowerCamelCase			(   self		:	str					)   ->		Optional[Any]:
             """simple docstring"""




             _lowerCamelCase			:						int            =DEFAULT_CALLBACKS.copy() + [ProgressCallback]
             _lowerCamelCase			:						List[str]            =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_					)

             _lowerCamelCase			:						Tuple            =self.get_trainer()
             _lowerCamelCase			:						Dict            =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
             _lowerCamelCase			:						Optional[int]            =self.get_trainer()
             _lowerCamelCase			:						Any            =trainer.callback_handler.callbacks[0]
             trainer.remove_callback(lowercase_					)
             expected_callbacks.remove(lowercase_					)
             self.check_callbacks_equality(trainer.callback_handler.callbacks     ,  lowercase_					)

             _lowerCamelCase			:						Any            =self.get_trainer()
             _lowerCamelCase			:						Optional[int]            =trainer.callback_handler.callbacks[0]
             _lowerCamelCase			:						Dict            =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					)   ->		Union[str, Any]:
             """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=lowercase_					)

             _lowerCamelCase			:						str            =self.get_trainer(callbacks=[MyTestTrainerCallback]					)
             trainer.train()
             _lowerCamelCase			:						List[Any]            =trainer.callback_handler.callbacks[-2].events
             self.assertEqual(lowercase_     ,  self.get_expected_events(lowercase_					)					)

             # Independent log/save/eval
             _lowerCamelCase			:						str            =self.get_trainer(callbacks=[MyTestTrainerCallback]     ,  logging_steps=5					)
             trainer.train()
             _lowerCamelCase			:						Union[str, Any]            =trainer.callback_handler.callbacks[-2].events
             self.assertEqual(lowercase_     ,  self.get_expected_events(lowercase_					)					)

             _lowerCamelCase			:						int            =self.get_trainer(callbacks=[MyTestTrainerCallback]     ,  save_steps=5					)
             trainer.train()
             _lowerCamelCase			:						Optional[Any]            =trainer.callback_handler.callbacks[-2].events
             self.assertEqual(lowercase_     ,  self.get_expected_events(lowercase_					)					)

             _lowerCamelCase			:						str            =self.get_trainer(callbacks=[MyTestTrainerCallback]     ,  eval_steps=5     ,  evaluation_strategy='steps'					)
             trainer.train()
             _lowerCamelCase			:						Any            =trainer.callback_handler.callbacks[-2].events
             self.assertEqual(lowercase_     ,  self.get_expected_events(lowercase_					)					)

             _lowerCamelCase			:						Optional[int]            =self.get_trainer(callbacks=[MyTestTrainerCallback]     ,  evaluation_strategy='epoch'					)
             trainer.train()
             _lowerCamelCase			:						Optional[int]            =trainer.callback_handler.callbacks[-2].events
             self.assertEqual(lowercase_     ,  self.get_expected_events(lowercase_					)					)

             # A bit of everything
             _lowerCamelCase			:						Optional[Any]            =self.get_trainer(
                 callbacks=[MyTestTrainerCallback]     ,  logging_steps=3     ,  save_steps=10     ,  eval_steps=5     ,  evaluation_strategy='steps'     ,  )
             trainer.train()
             _lowerCamelCase			:						List[str]            =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:
                    _lowerCamelCase			:						Union[str, Any]            =self.get_trainer(
                        callbacks=[MyTestTrainerCallback, MyTestTrainerCallback]     ,  )
                    assert str(lowercase_					) in warn_mock.call_args[0][0]


