


import re
import time
from typing import Optional

import IPython.display as disp

from ..trainer_callback import TrainerCallback
from ..trainer_utils import IntervalStrategy, has_length
def      snake_case_						( __lowercase       ):
   UpperCAmelCase_							:      Optional[Any]						=   int(__lowercase       )
   UpperCAmelCase_		,      UpperCAmelCase_		,      UpperCAmelCase_							:      Dict						=   t // 3_6_0_0, (t // 6_0) % 6_0, t % 6_0
   return F'''{h}:{m:02d}:{s:02d}''' if h != 0 else F'''{m:02d}:{s:02d}'''
def      snake_case_						( __lowercase			,       __lowercase			,       __lowercase			,       __lowercase			,       __lowercase=3_0_0       ):
   # docstyle-ignore
   return F'''
    <div>
      {prefix}
      <progress value=\'{value}\' max=\'{total}\' style=\'width:{width}px; height:20px; vertical-align: middle;\'></progress>
      {label}
    </div>
    '''
def      snake_case_						( __lowercase       ):
   UpperCAmelCase_							:      List[Any]						=   '''<table border="1" class="dataframe">\n'''
   html_code += """  <thead>\n <tr style="text-align: left;">\n"""
   for i in items[0]:
      html_code += F'''      <th>{i}</th>\n'''
   html_code += "    </tr>\n  </thead>\n  <tbody>\n"
   for line in items[1:]:
      html_code += "    <tr>\n"
      for elt in line:
         UpperCAmelCase_							:      Tuple						=   F'''{elt:.6f}''' if isinstance(__lowercase			,       __lowercase       ) else str(__lowercase       )
         html_code += F'''      <td>{elt}</td>\n'''
      html_code += "    </tr>\n"
   html_code += "  </tbody>\n</table><p>"
   return html_code





class  lowerCAmelCase__:






    '''simple docstring'''





    A_  :     int			= 5
    A_  :     List[Any]			= 0.2





    def __init__(	self				:   List[str]       ,   __snake_case				:   int       ,   __snake_case				:   Optional[str] = None       ,   __snake_case				:   bool = True       ,   __snake_case				:   Optional["NotebookTrainingTracker"] = None       ,   __snake_case				:   int = 300       ,   ):





       '''simple docstring'''
       UpperCAmelCase_							:      Any						=   total
       UpperCAmelCase_							:      Union[str, Any]						=   '''''' if prefix is None else prefix
       UpperCAmelCase_							:      str						=   leave
       UpperCAmelCase_							:      Optional[int]						=   parent
       UpperCAmelCase_							:      List[Any]						=   width
       UpperCAmelCase_							:      str						=   None
       UpperCAmelCase_							:      Optional[int]						=   None
       UpperCAmelCase_							:      Any						=   None





    def 					_lowerCamelCase (	self				:   Optional[int]       ,   __snake_case				:   int       ,   __snake_case				:   bool = False       ,   __snake_case				:   str = None	):





       '''simple docstring'''
       UpperCAmelCase_							:      Union[str, Any]						=   value
       if comment is not None:
          UpperCAmelCase_							:      int						=   comment
       if self.last_value is None:
          UpperCAmelCase_							:      int						=   time.time()
          UpperCAmelCase_							:      Optional[int]						=   value
          UpperCAmelCase_							:      Union[str, Any]						=   None
          UpperCAmelCase_							:      int						=   self.warmup
          UpperCAmelCase_							:      List[Any]						=   1
          self.update_bar(__snake_case	)
       elif value <= self.last_value and not force_update:
          return
       elif force_update or self.first_calls > 0 or value >= min(self.last_value + self.wait_for       ,   self.total	):
          if self.first_calls > 0:
             self.first_calls -= 1
          UpperCAmelCase_							:      Union[str, Any]						=   time.time()
          UpperCAmelCase_							:      List[str]						=   current_time - self.start_time
          # We could have value = self.start_value if the update is called twixe with the same start value.
          if value > self.start_value:
             UpperCAmelCase_							:      int						=   self.elapsed_time / (value - self.start_value)
          else:
             UpperCAmelCase_							:      Optional[int]						=   None
          if value >= self.total:
             UpperCAmelCase_							:      Optional[int]						=   self.total
             UpperCAmelCase_							:      str						=   None
             if not self.leave:
                self.close()
          elif self.average_time_per_item is not None:
             UpperCAmelCase_							:      Any						=   self.average_time_per_item * (self.total - value)
          self.update_bar(__snake_case	)
          UpperCAmelCase_							:      List[str]						=   value
          UpperCAmelCase_							:      Tuple						=   current_time
          if self.average_time_per_item is None:
             UpperCAmelCase_							:      Dict						=   1
          else:
             UpperCAmelCase_							:      int						=   max(int(self.update_every / self.average_time_per_item	)       ,   1	)





    def 					_lowerCamelCase (	self				:   Tuple       ,   __snake_case				:   List[Any]       ,   __snake_case				:   List[str]=None	):





       '''simple docstring'''
       UpperCAmelCase_							:      Union[str, Any]						=   ''' ''' * (len(str(self.total	)	) - len(str(__snake_case	)	)) + str(__snake_case	)
       if self.elapsed_time is None:
          UpperCAmelCase_							:      int						=   f'''[{spaced_value}/{self.total} : < :'''
       elif self.predicted_remaining is None:
          UpperCAmelCase_							:      Optional[Any]						=   f'''[{spaced_value}/{self.total} {format_time(self.elapsed_time	)}'''
       else:
          UpperCAmelCase_							:      Tuple						=   (
              f'''[{spaced_value}/{self.total} {format_time(self.elapsed_time	)} <'''
              f''' {format_time(self.predicted_remaining	)}'''
          )
          self.label += f''', {1/self.average_time_per_item:.2f} it/s'''
       self.label += "]" if self.comment is None or len(self.comment	) == 0 else f''', {self.comment}]'''
       self.display()





    def 					_lowerCamelCase (	self				:   Dict	):





       '''simple docstring'''
       UpperCAmelCase_							:      List[Any]						=   html_progress_bar(self.value       ,   self.total       ,   self.prefix       ,   self.label       ,   self.width	)
       if self.parent is not None:
          # If this is a child bar, the parent will take care of the display.
          self.parent.display()
          return
       if self.output is None:
          UpperCAmelCase_							:      Optional[int]						=   disp.display(disp.HTML(self.html_code	)       ,   display_id=__snake_case	)
       else:
          self.output.update(disp.HTML(self.html_code	)	)





    def 					_lowerCamelCase (	self				:   List[str]	):





       '''simple docstring'''
       if self.parent is None and self.output is not None:
          self.output.update(disp.HTML(''''''	)	)





class  lowerCAmelCase__(    snake_case__						):






    '''simple docstring'''





    def __init__(	self				:   Dict       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Optional[Any]=None	):





       '''simple docstring'''
       super().__init__(__snake_case	)
       UpperCAmelCase_							:      Tuple						=   None if column_names is None else [column_names]
       UpperCAmelCase_							:      Dict						=   None





    def 					_lowerCamelCase (	self				:   Optional[int]	):





       '''simple docstring'''
       UpperCAmelCase_							:      Optional[Any]						=   html_progress_bar(self.value       ,   self.total       ,   self.prefix       ,   self.label       ,   self.width	)
       if self.inner_table is not None:
          self.html_code += text_to_html_table(self.inner_table	)
       if self.child_bar is not None:
          self.html_code += self.child_bar.html_code
       if self.output is None:
          UpperCAmelCase_							:      Dict						=   disp.display(disp.HTML(self.html_code	)       ,   display_id=__snake_case	)
       else:
          self.output.update(disp.HTML(self.html_code	)	)





    def 					_lowerCamelCase (	self				:   Optional[int]       ,   __snake_case				:   Optional[int]	):





       '''simple docstring'''
       if self.inner_table is None:
          UpperCAmelCase_							:      List[Any]						=   [list(values.keys()	), list(values.values()	)]
       else:
          UpperCAmelCase_							:      Union[str, Any]						=   self.inner_table[0]
          if len(self.inner_table	) == 1:
             # We give a chance to update the column names at the first iteration
             for key in values.keys():
                if key not in columns:
                   columns.append(__snake_case	)
             UpperCAmelCase_							:      Union[str, Any]						=   columns
          self.inner_table.append([values[c] for c in columns]	)





    def 					_lowerCamelCase (	self				:   Dict       ,   __snake_case				:   Dict       ,   __snake_case				:   Any=None       ,   __snake_case				:   List[str]=300	):





       '''simple docstring'''
       UpperCAmelCase_							:      str						=   NotebookProgressBar(__snake_case       ,   prefix=__snake_case       ,   parent=self       ,   width=__snake_case	)
       return self.child_bar





    def 					_lowerCamelCase (	self				:   Dict	):





       '''simple docstring'''
       UpperCAmelCase_							:      Dict						=   None
       self.display()





class  lowerCAmelCase__(    snake_case__						):






    '''simple docstring'''





    def __init__(	self				:   Any	):





       '''simple docstring'''
       UpperCAmelCase_							:      str						=   None
       UpperCAmelCase_							:      List[Any]						=   None
       UpperCAmelCase_							:      Optional[Any]						=   False





    def 					_lowerCamelCase (	self				:   Optional[Any]       ,   __snake_case				:   str       ,   __snake_case				:   Any       ,   __snake_case				:   str       ,   **__snake_case				:   int	):





       '''simple docstring'''
       UpperCAmelCase_							:      List[str]						=   '''Epoch''' if args.evaluation_strategy == IntervalStrategy.EPOCH else '''Step'''
       UpperCAmelCase_							:      List[str]						=   0
       UpperCAmelCase_							:      Union[str, Any]						=   0
       UpperCAmelCase_							:      Optional[int]						=   [self.first_column] + ['''Training Loss''']
       if args.evaluation_strategy != IntervalStrategy.NO:
          column_names.append('''Validation Loss'''	)
       UpperCAmelCase_							:      Any						=   NotebookTrainingTracker(state.max_steps       ,   __snake_case	)





    def 					_lowerCamelCase (	self				:   Any       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Optional[Any]       ,   **__snake_case				:   str	):





       '''simple docstring'''
       UpperCAmelCase_							:      List[Any]						=   int(state.epoch	) if int(state.epoch	) == state.epoch else f'''{state.epoch:.2f}'''
       self.training_tracker.update(
           state.global_step + 1       ,   comment=f'''Epoch {epoch}/{state.num_train_epochs}'''       ,   force_update=self._force_next_update       ,   )
       UpperCAmelCase_							:      str						=   False





    def 					_lowerCamelCase (	self				:   str       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   int       ,   __snake_case				:   int       ,   __snake_case				:   Dict=None       ,   **__snake_case				:   int	):





       '''simple docstring'''
       if not has_length(__snake_case	):
          return
       if self.prediction_bar is None:
          if self.training_tracker is not None:
             UpperCAmelCase_							:      List[Any]						=   self.training_tracker.add_child(len(__snake_case	)	)
          else:
             UpperCAmelCase_							:      List[str]						=   NotebookProgressBar(len(__snake_case	)	)
          self.prediction_bar.update(1	)
       else:
          self.prediction_bar.update(self.prediction_bar.value + 1	)





    def 					_lowerCamelCase (	self				:   List[Any]       ,   __snake_case				:   Optional[Any]       ,   __snake_case				:   Dict       ,   __snake_case				:   Dict       ,   **__snake_case				:   Union[str, Any]	):





       '''simple docstring'''
       if self.prediction_bar is not None:
          self.prediction_bar.close()
       UpperCAmelCase_							:      List[str]						=   None





    def 					_lowerCamelCase (	self				:   Optional[Any]       ,   __snake_case				:   List[Any]       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Union[str, Any]=None       ,   **__snake_case				:   Dict	):





       '''simple docstring'''
       # Only for when there is no evaluation
       if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs:
          UpperCAmelCase_							:      Union[str, Any]						=   {'''Training Loss''': logs['''loss''']}
          # First column is necessarily Step sine we're not in epoch eval strategy
          UpperCAmelCase_							:      str						=   state.global_step
          self.training_tracker.write_line(__snake_case	)





    def 					_lowerCamelCase (	self				:   Any       ,   __snake_case				:   Dict       ,   __snake_case				:   List[Any]       ,   __snake_case				:   List[Any]       ,   __snake_case				:   int=None       ,   **__snake_case				:   int	):





       '''simple docstring'''
       if self.training_tracker is not None:
          UpperCAmelCase_							:      List[Any]						=   {'''Training Loss''': '''No log''', '''Validation Loss''': '''No log'''}
          for log in reversed(state.log_history	):
             if "loss" in log:
                UpperCAmelCase_							:      Dict						=   log['''loss''']
                break

          if self.first_column == "Epoch":
             UpperCAmelCase_							:      Dict						=   int(state.epoch	)
          else:
             UpperCAmelCase_							:      Dict						=   state.global_step
          UpperCAmelCase_							:      Dict						=   '''eval'''
          for k in metrics:
             if k.endswith('''_loss'''	):
                UpperCAmelCase_							:      Optional[Any]						=   re.sub(R'''\_loss$'''       ,   ''''''       ,   __snake_case	)
          UpperCAmelCase_							:      Union[str, Any]						=   metrics.pop('''total_flos'''       ,   __snake_case	)
          UpperCAmelCase_							:      str						=   metrics.pop('''epoch'''       ,   __snake_case	)
          UpperCAmelCase_							:      Optional[Any]						=   metrics.pop(f'''{metric_key_prefix}_runtime'''       ,   __snake_case	)
          UpperCAmelCase_							:      int						=   metrics.pop(f'''{metric_key_prefix}_samples_per_second'''       ,   __snake_case	)
          UpperCAmelCase_							:      int						=   metrics.pop(f'''{metric_key_prefix}_steps_per_second'''       ,   __snake_case	)
          UpperCAmelCase_							:      Optional[int]						=   metrics.pop(f'''{metric_key_prefix}_jit_compilation_time'''       ,   __snake_case	)
          for k, v in metrics.items():
             if k == f'''{metric_key_prefix}_loss''':
                UpperCAmelCase_							:      Tuple						=   v
             else:
                UpperCAmelCase_							:      Optional[Any]						=   k.split('''_'''	)
                UpperCAmelCase_							:      Optional[Any]						=   ''' '''.join([part.capitalize() for part in splits[1:]]	)
                UpperCAmelCase_							:      Optional[Any]						=   v
          self.training_tracker.write_line(__snake_case	)
          self.training_tracker.remove_child()
          UpperCAmelCase_							:      Tuple						=   None
          # Evaluation takes a long time so we should force the next update.
          UpperCAmelCase_							:      Any						=   True





    def 					_lowerCamelCase (	self				:   str       ,   __snake_case				:   List[Any]       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Tuple       ,   **__snake_case				:   str	):





       '''simple docstring'''
       self.training_tracker.update(
           state.global_step       ,   comment=f'''Epoch {int(state.epoch	)}/{state.num_train_epochs}'''       ,   force_update=__snake_case	)
       UpperCAmelCase_							:      List[str]						=   None



import functools
import operator

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


__UpperCamelCase				:  Union[str, Any]      					=							logging.get_logger(__name__)

__UpperCamelCase				:  str      					=							{
    'microsoft/unispeech-sat-base-100h-libri-ft': (
        'https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json'
    ),
    # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat
}





class  lowerCAmelCase__(    snake_case__						):






    '''simple docstring'''





    A_  :     List[Any]			= 'unispeech-sat'





    def __init__(	self				:   int       ,   __snake_case				:   Optional[int]=32       ,   __snake_case				:   Dict=768       ,   __snake_case				:   Optional[Any]=12       ,   __snake_case				:   Optional[int]=12       ,   __snake_case				:   Dict=3_072       ,   __snake_case				:   List[str]="gelu"       ,   __snake_case				:   Any=0.1       ,   __snake_case				:   Tuple=0.1       ,   __snake_case				:   Optional[int]=0.1       ,   __snake_case				:   Tuple=0.0       ,   __snake_case				:   List[Any]=0.0       ,   __snake_case				:   Tuple=0.1       ,   __snake_case				:   Union[str, Any]=0.1       ,   __snake_case				:   Dict=0.02       ,   __snake_case				:   Optional[Any]=1E-5       ,   __snake_case				:   Optional[int]="group"       ,   __snake_case				:   str="gelu"       ,   __snake_case				:   Union[str, Any]=(512, 512, 512, 512, 512, 512, 512)       ,   __snake_case				:   str=(5, 2, 2, 2, 2, 2, 2)       ,   __snake_case				:   Tuple=(10, 3, 3, 3, 3, 2, 2)       ,   __snake_case				:   int=False       ,   __snake_case				:   Optional[int]=128       ,   __snake_case				:   Any=16       ,   __snake_case				:   Union[str, Any]=False       ,   __snake_case				:   Union[str, Any]=True       ,   __snake_case				:   List[Any]=0.05       ,   __snake_case				:   Dict=10       ,   __snake_case				:   int=2       ,   __snake_case				:   Optional[Any]=0.0       ,   __snake_case				:   Optional[int]=10       ,   __snake_case				:   List[Any]=0       ,   __snake_case				:   Optional[int]=320       ,   __snake_case				:   int=2       ,   __snake_case				:   Any=0.1       ,   __snake_case				:   Optional[int]=100       ,   __snake_case				:   Tuple=256       ,   __snake_case				:   List[str]=256       ,   __snake_case				:   List[Any]=0.1       ,   __snake_case				:   Tuple="mean"       ,   __snake_case				:   List[Any]=False       ,   __snake_case				:   List[str]=False       ,   __snake_case				:   Optional[Any]=256       ,   __snake_case				:   Tuple=(512, 512, 512, 512, 1_500)       ,   __snake_case				:   Optional[int]=(5, 3, 3, 1, 1)       ,   __snake_case				:   Any=(1, 2, 3, 1, 1)       ,   __snake_case				:   int=512       ,   __snake_case				:   Optional[int]=0       ,   __snake_case				:   Dict=1       ,   __snake_case				:   Tuple=2       ,   __snake_case				:   Union[str, Any]=504       ,   **__snake_case				:   List[str]       ,   ):





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

       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
       UpperCAmelCase_							:      int						=   apply_spec_augment
       UpperCAmelCase_							:      Optional[Any]						=   mask_time_prob
       UpperCAmelCase_							:      str						=   mask_time_length
       UpperCAmelCase_							:      Any						=   mask_time_min_masks
       UpperCAmelCase_							:      str						=   mask_feature_prob
       UpperCAmelCase_							:      str						=   mask_feature_length
       UpperCAmelCase_							:      Tuple						=   mask_feature_min_masks

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

       # ctc loss
       UpperCAmelCase_							:      Any						=   ctc_loss_reduction
       UpperCAmelCase_							:      Optional[Any]						=   ctc_zero_infinity

       # SequenceClassification-specific parameter. Feel free to ignore for other classes.
       UpperCAmelCase_							:      Optional[int]						=   classifier_proj_size

       # XVector-specific parameters. Feel free to ignore for other classes.
       UpperCAmelCase_							:      Dict						=   list(__snake_case	)
       UpperCAmelCase_							:      Dict						=   list(__snake_case	)
       UpperCAmelCase_							:      Dict						=   list(__snake_case	)
       UpperCAmelCase_							:      Union[str, Any]						=   xvector_output_dim





    @property
    def 					_lowerCamelCase (	self				:   List[str]	):





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