


'''simple docstring'''
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       _lowercase							(					UpperCamelCase__							:       List[Any]      ):
 __A	:		Tuple	  =      int(UpperCamelCase__      )
 __A  ,__A  ,__A	:		Optional[Any]	  =      t // 3600, (t // 60) % 60, t % 60
 return f"""{h}:{m:02d}:{s:02d}""" if h != 0 else f"""{m:02d}:{s:02d}"""


def       _lowercase							(					UpperCamelCase__							:       str,					UpperCamelCase__							:       Optional[Any],					UpperCamelCase__							:       List[Any],					UpperCamelCase__							:       Dict,					UpperCamelCase__							:       Optional[int]=300      ):
 # docstyle-ignore
 return f"""
    <div>
      {prefix}
      <progress value='{value}' max='{total}' style='width:{width}px; height:20px; vertical-align: middle;'></progress>
      {label}
    </div>
    """


def       _lowercase							(					UpperCamelCase__							:       Optional[int]      ):
 __A	:		Union[str, 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:
   __A	:		Any	  =      f"""{elt:.6f}""" if isinstance(UpperCamelCase__,					UpperCamelCase__      ) else str(UpperCamelCase__      )
   html_code += f"""      <td>{elt}</td>\n"""
  html_code += "    </tr>\n"
 html_code += "  </tbody>\n</table><p>"
 return html_code

class     _lowerCamelCase       :




 '''simple docstring'''
 __lowercase					:		List[str]     				=    5
 __lowercase					:		Any     				=    0.2


 def __init__(   self   ,   __lowercase   ,   __lowercase = None   ,   __lowercase = True   ,   __lowercase = None   ,   __lowercase = 300   ,   ):

  """simple docstring"""







  __A	:		Any	  =      total
  __A	:		int	  =      '' if prefix is None else prefix
  __A	:		List[Any]	  =      leave
  __A	:		Tuple	  =      parent
  __A	:		int	  =      width
  __A	:		Dict	  =      None
  __A	:		str	  =      None
  __A	:		Tuple	  =      None


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase = False   ,   __lowercase = None    ):

  """simple docstring"""







  __A	:		int	  =      value
  if comment is not None:
   __A	:		List[str]	  =      comment
  if self.last_value is None:
   __A	:		List[Any]	  =      time.time()
   __A	:		str	  =      value
   __A	:		Tuple	  =      None
   __A	:		Any	  =      self.warmup
   __A	:		Optional[int]	  =      1
   self.update_bar(__lowercase    )
  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
   __A	:		List[Any]	  =      time.time()
   __A	:		Any	  =      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:
    __A	:		Tuple	  =      self.elapsed_time / (value - self.start_value)
   else:
    __A	:		Tuple	  =      None
   if value >= self.total:
    __A	:		Tuple	  =      self.total
    __A	:		Tuple	  =      None
    if not self.leave:
     self.close()
   elif self.average_time_per_item is not None:
    __A	:		Optional[int]	  =      self.average_time_per_item * (self.total - value)
   self.update_bar(__lowercase    )
   __A	:		Optional[int]	  =      value
   __A	:		Optional[int]	  =      current_time
   if self.average_time_per_item is None:
    __A	:		int	  =      1
   else:
    __A	:		Tuple	  =      max(int(self.update_every / self.average_time_per_item    )   ,   1    )


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase=None    ):

  """simple docstring"""







  __A	:		Dict	  =      ' ' * (len(str(self.total    )    ) - len(str(__lowercase    )    )) + str(__lowercase    )
  if self.elapsed_time is None:
   __A	:		Optional[int]	  =      F"""[{spaced_value}/{self.total} : < :"""
  elif self.predicted_remaining is None:
   __A	:		List[Any]	  =      F"""[{spaced_value}/{self.total} {format_time(self.elapsed_time    )}"""
  else:
   __A	:		List[Any]	  =      (
       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 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Union[str, 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:
   __A	:		Any	  =      disp.display(disp.HTML(self.html_code    )   ,   display_id=__lowercase    )
  else:
   self.output.update(disp.HTML(self.html_code    )    )







 def 			snake_case__ (   self    ):

  """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   ,   __lowercase   ,   __lowercase=None    ):

  """simple docstring"""







  super().__init__(__lowercase    )
  __A	:		Optional[int]	  =      None if column_names is None else [column_names]
  __A	:		int	  =      None


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		str	  =      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:
   __A	:		Union[str, Any]	  =      disp.display(disp.HTML(self.html_code    )   ,   display_id=__lowercase    )
  else:
   self.output.update(disp.HTML(self.html_code    )    )


 def 			snake_case__ (   self   ,   __lowercase    ):

  """simple docstring"""







  if self.inner_table is None:
   __A	:		Union[str, Any]	  =      [list(values.keys()    ), list(values.values()    )]
  else:
   __A	:		str	  =      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(__lowercase    )
    __A	:		Any	  =      columns
   self.inner_table.append([values[c] for c in columns]    )


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase=None   ,   __lowercase=300    ):

  """simple docstring"""







  __A	:		Dict	  =      NotebookProgressBar(__lowercase   ,   prefix=__lowercase   ,   parent=self   ,   width=__lowercase    )
  return self.child_bar







 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Optional[int]	  =      None
  self.display()


class     _lowerCamelCase       (     snake_case_					):




 '''simple docstring'''


 def __init__(   self    ):

  """simple docstring"""







  __A	:		List[str]	  =      None
  __A	:		Any	  =      None
  __A	:		int	  =      False


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase   ,   **__lowercase    ):

  """simple docstring"""







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


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase   ,   **__lowercase    ):

  """simple docstring"""







  __A	:		Union[str, 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   ,   )
  __A	:		str	  =      False


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase   ,   __lowercase=None   ,   **__lowercase    ):

  """simple docstring"""







  if not has_length(__lowercase    ):
   return
  if self.prediction_bar is None:
   if self.training_tracker is not None:
    __A	:		Optional[Any]	  =      self.training_tracker.add_child(len(__lowercase    )    )
   else:
    __A	:		Optional[int]	  =      NotebookProgressBar(len(__lowercase    )    )
   self.prediction_bar.update(1    )
  else:
   self.prediction_bar.update(self.prediction_bar.value + 1    )


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase   ,   **__lowercase    ):

  """simple docstring"""







  if self.prediction_bar is not None:
   self.prediction_bar.close()
  __A	:		Union[str, Any]	  =      None


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase   ,   __lowercase=None   ,   **__lowercase    ):

  """simple docstring"""







  if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs:
   __A	:		str	  =      {'Training Loss': logs['loss']}
   # First column is necessarily Step sine we're not in epoch eval strategy
   __A	:		Dict	  =      state.global_step
   self.training_tracker.write_line(__lowercase    )


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase   ,   __lowercase=None   ,   **__lowercase    ):

  """simple docstring"""







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

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







 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase   ,   **__lowercase    ):

  """simple docstring"""







  self.training_tracker.update(
      state.global_step   ,   comment=F"""Epoch {int(state.epoch    )}/{state.num_train_epochs}"""   ,   force_update=__lowercase    )
  __A	:		Optional[Any]	  =      None










'''simple docstring'''
import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


UpperCAmelCase_			:  Any										=   logging.get_logger(__name__)

UpperCAmelCase_			:  Tuple										=   {'vocab_file': 'spm_char.model'}

UpperCAmelCase_			:  int										=   {
    'vocab_file': {
        'microsoft/speecht5_asr': 'https://huggingface.co/microsoft/speecht5_asr/resolve/main/spm_char.model',
        'microsoft/speecht5_tts': 'https://huggingface.co/microsoft/speecht5_tts/resolve/main/spm_char.model',
        'microsoft/speecht5_vc': 'https://huggingface.co/microsoft/speecht5_vc/resolve/main/spm_char.model',
    }
}

UpperCAmelCase_			:  List[Any]										=   {
    'microsoft/speecht5_asr': 1_0_2_4,
    'microsoft/speecht5_tts': 1_0_2_4,
    'microsoft/speecht5_vc': 1_0_2_4,
}

class     _lowerCamelCase       (     snake_case_					):




 '''simple docstring'''
 __lowercase					:		Optional[Any]     				=    VOCAB_FILES_NAMES
 __lowercase					:		int     				=    PRETRAINED_VOCAB_FILES_MAP
 __lowercase					:		int     				=    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 __lowercase					:		Optional[int]     				=    ['''input_ids''', '''attention_mask''']


 def __init__(   self   ,   __lowercase   ,   __lowercase="<s>"   ,   __lowercase="</s>"   ,   __lowercase="<unk>"   ,   __lowercase="<pad>"   ,   __lowercase = None   ,   **__lowercase   ,   ):

  """simple docstring"""







  __A	:		Dict	  =      {} if sp_model_kwargs is None else sp_model_kwargs

  super().__init__(
      bos_token=__lowercase   ,   eos_token=__lowercase   ,   unk_token=__lowercase   ,   pad_token=__lowercase   ,   sp_model_kwargs=self.sp_model_kwargs   ,   **__lowercase   ,   )

  __A	:		Optional[Any]	  =      vocab_file

  __A	:		Any	  =      spm.SentencePieceProcessor(**self.sp_model_kwargs    )
  self.sp_model.Load(__lowercase    )


 @property
 def 			snake_case__ (   self    ):

  """simple docstring"""







  return self.sp_model.get_piece_size()


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Tuple	  =      {self.convert_ids_to_tokens(__lowercase    ): i for i in range(self.vocab_size    )}
  vocab.update(self.added_tokens_encoder    )
  return vocab


 def __getstate__(   self    ):

  """simple docstring"""







  __A	:		List[Any]	  =      self.__dict__.copy()
  __A	:		str	  =      None
  return state


 def __setstate__(   self   ,   __lowercase    ):

  """simple docstring"""







  __A	:		List[str]	  =      d

  # for backward compatibility
  if not hasattr(self   ,   'sp_model_kwargs'    ):
   __A	:		Any	  =      {}

  __A	:		List[str]	  =      spm.SentencePieceProcessor(**self.sp_model_kwargs    )
  self.sp_model.Load(self.vocab_file    )


 def 			snake_case__ (   self   ,   __lowercase    ):

  """simple docstring"""







  return self.sp_model.encode(__lowercase   ,   out_type=__lowercase    )


 def 			snake_case__ (   self   ,   __lowercase    ):

  """simple docstring"""







  return self.sp_model.piece_to_id(__lowercase    )


 def 			snake_case__ (   self   ,   __lowercase    ):

  """simple docstring"""







  __A	:		Optional[Any]	  =      self.sp_model.IdToPiece(__lowercase    )
  return token


 def 			snake_case__ (   self   ,   __lowercase    ):

  """simple docstring"""







  __A	:		str	  =      []
  __A	:		str	  =      ''
  for token in tokens:
   # make sure that special tokens are not decoded using sentencepiece model
   if token in self.all_special_tokens:
    out_string += self.sp_model.decode(__lowercase    ) + token
    __A	:		Any	  =      []
   else:
    current_sub_tokens.append(__lowercase    )
  out_string += self.sp_model.decode(__lowercase    )
  return out_string.strip()


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase=None    ):

  """simple docstring"""







  if token_ids_a is None:
   return token_ids_a + [self.eos_token_id]
  # We don't expect to process pairs, but leave the pair logic for API consistency
  return token_ids_a + token_ids_a + [self.eos_token_id]


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase = None   ,   __lowercase = False    ):

  """simple docstring"""







  if already_has_special_tokens:
   return super().get_special_tokens_mask(
       token_ids_a=__lowercase   ,   token_ids_a=__lowercase   ,   already_has_special_tokens=__lowercase    )

  __A	:		Dict	  =      [1]
  if token_ids_a is None:
   return ([0] * len(__lowercase    )) + suffix_ones
  return ([0] * len(__lowercase    )) + ([0] * len(__lowercase    )) + suffix_ones







 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase = None    ):

  """simple docstring"""







  if not os.path.isdir(__lowercase    ):
   logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""    )
   return
  __A	:		Optional[int]	  =      os.path.join(
      __lowercase   ,   (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']    )

  if os.path.abspath(self.vocab_file    ) != os.path.abspath(__lowercase    ) and os.path.isfile(self.vocab_file    ):
   copyfile(self.vocab_file   ,   __lowercase    )
  elif not os.path.isfile(self.vocab_file    ):
   with open(__lowercase   ,   'wb'    ) as fi:
    __A	:		Tuple	  =      self.sp_model.serialized_model_proto()
    fi.write(__lowercase    )

  return (out_vocab_file,)






