



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 	SCREAMING_SNAKE_CASE_							(	_snake_case	:List[Any]				)  ->     Tuple:
   _A      =       int(_snake_case				)
   _A  ,    _A  ,    _A      =       t // 3_600, (t // 60) % 60, t % 60
   return F'''{h}:{m:02d}:{s:02d}''' if h != 0 else F'''{m:02d}:{s:02d}'''

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:int	,   _snake_case	:Optional[Any]	,   _snake_case	:str	,   _snake_case	:int	,   _snake_case	:Optional[Any]=300				)  ->     Union[str, Any]:
   # docstyle-ignore
   return F'''
    <div>
      {prefix}
      <progress value=\'{value}\' max=\'{total}\' style=\'width:{width}px; height:20px; vertical-align: middle;\'></progress>
      {label}
    </div>
    '''

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:int				)  ->     Dict:
   _A      =       '''<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      =       F'''{elt:.6f}''' if isinstance(_snake_case	,   _snake_case				) else str(_snake_case				)
         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__					: str =  5
 a__					: Optional[Any] =  0.2

 def __init__(    self					:					int			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					Optional[str] = None			,						__lowerCAmelCase					:					bool = True			,						__lowerCAmelCase					:					Optional["NotebookTrainingTracker"] = None			,						__lowerCAmelCase					:					int = 3_00			,						)   ->	Optional[int]:
    _A      =       total
    _A      =       '''''' if prefix is None else prefix
    _A      =       leave
    _A      =       parent
    _A      =       width
    _A      =       None
    _A      =       None
    _A      =       None

 def     snake_case_      (    self					:					int			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					bool = False			,						__lowerCAmelCase					:					str = None      )   ->	str:
    _A      =       value
    if comment is not None:
       _A      =       comment
    if self.last_value is None:
       _A      =       _A      =       time.time()
       _A      =       _A      =       value
       _A      =       _A      =       None
       _A      =       self.warmup
       _A      =       1
       self.update_bar(__lowerCAmelCase      )
    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      =       time.time()
       _A      =       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      =       self.elapsed_time / (value - self.start_value)
       else:
          _A      =       None
       if value >= self.total:
          _A      =       self.total
          _A      =       None
          if not self.leave:
             self.close()
       elif self.average_time_per_item is not None:
          _A      =       self.average_time_per_item * (self.total - value)
       self.update_bar(__lowerCAmelCase      )
       _A      =       value
       _A      =       current_time
       if self.average_time_per_item is None:
          _A      =       1
       else:
          _A      =       max(int(self.update_every / self.average_time_per_item      )			,						1      )

 def     snake_case_      (    self					:					List[str]			,						__lowerCAmelCase					:					List[Any]			,						__lowerCAmelCase					:					Any=None      )   ->	List[Any]:
    _A      =       ''' ''' * (len(str(self.total      )      ) - len(str(__lowerCAmelCase      )      )) + str(__lowerCAmelCase      )
    if self.elapsed_time is None:
       _A      =       f'''[{spaced_value}/{self.total} : < :'''
    elif self.predicted_remaining is None:
       _A      =       f'''[{spaced_value}/{self.total} {format_time(self.elapsed_time      )}'''
    else:
       _A      =       (
           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					:					List[Any]      )   ->	Union[str, Any]:
    _A      =       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      =       disp.display(disp.HTML(self.html_code      )			,						display_id=__lowerCAmelCase      )
    else:
       self.output.update(disp.HTML(self.html_code      )      )

 def     snake_case_      (    self					:					List[Any]      )   ->	Dict:
    if self.parent is None and self.output is not None:
       self.output.update(disp.HTML(''''''      )      )


class 	lowerCamelCase__	(       _A):



 """simple docstring"""

 def __init__(    self					:					Optional[Any]			,						__lowerCAmelCase					:					Dict			,						__lowerCAmelCase					:					List[Any]=None      )   ->	Optional[int]:
    super().__init__(__lowerCAmelCase      )
    _A      =       None if column_names is None else [column_names]
    _A      =       None

 def     snake_case_      (    self					:					str      )   ->	int:
    _A      =       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      =       disp.display(disp.HTML(self.html_code      )			,						display_id=__lowerCAmelCase      )
    else:
       self.output.update(disp.HTML(self.html_code      )      )

 def     snake_case_      (    self					:					Any			,						__lowerCAmelCase					:					Dict      )   ->	int:
    if self.inner_table is None:
       _A      =       [list(values.keys()      ), list(values.values()      )]
    else:
       _A      =       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(__lowerCAmelCase      )
          _A      =       columns
       self.inner_table.append([values[c] for c in columns]      )

 def     snake_case_      (    self					:					Tuple			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					Optional[int]=None			,						__lowerCAmelCase					:					List[Any]=3_00      )   ->	Optional[int]:
    _A      =       NotebookProgressBar(__lowerCAmelCase			,						prefix=__lowerCAmelCase			,						parent=self			,						width=__lowerCAmelCase      )
    return self.child_bar

 def     snake_case_      (    self					:					Dict      )   ->	int:
    _A      =       None
    self.display()


class 	lowerCamelCase__	(       _A):



 """simple docstring"""

 def __init__(    self					:					Dict      )   ->	Tuple:
    _A      =       None
    _A      =       None
    _A      =       False

 def     snake_case_      (    self					:					Any			,						__lowerCAmelCase					:					List[Any]			,						__lowerCAmelCase					:					Union[str, Any]			,						__lowerCAmelCase					:					str			,						**__lowerCAmelCase					:					Dict      )   ->	Tuple:
    _A      =       '''Epoch''' if args.evaluation_strategy == IntervalStrategy.EPOCH else '''Step'''
    _A      =       0
    _A      =       0
    _A      =       [self.first_column] + ['''Training Loss''']
    if args.evaluation_strategy != IntervalStrategy.NO:
       column_names.append('''Validation Loss'''      )
    _A      =       NotebookTrainingTracker(state.max_steps			,						__lowerCAmelCase      )

 def     snake_case_      (    self					:					Optional[int]			,						__lowerCAmelCase					:					str			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					Dict			,						**__lowerCAmelCase					:					Union[str, Any]      )   ->	Tuple:
    _A      =       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      =       False

 def     snake_case_      (    self					:					Any			,						__lowerCAmelCase					:					str			,						__lowerCAmelCase					:					Optional[int]			,						__lowerCAmelCase					:					Any			,						__lowerCAmelCase					:					List[Any]=None			,						**__lowerCAmelCase					:					int      )   ->	Union[str, Any]:
    if not has_length(__lowerCAmelCase      ):
       return
    if self.prediction_bar is None:
       if self.training_tracker is not None:
          _A      =       self.training_tracker.add_child(len(__lowerCAmelCase      )      )
       else:
          _A      =       NotebookProgressBar(len(__lowerCAmelCase      )      )
       self.prediction_bar.update(1      )
    else:
       self.prediction_bar.update(self.prediction_bar.value + 1      )

 def     snake_case_      (    self					:					List[Any]			,						__lowerCAmelCase					:					Optional[Any]			,						__lowerCAmelCase					:					List[Any]			,						__lowerCAmelCase					:					str			,						**__lowerCAmelCase					:					Any      )   ->	Optional[int]:
    if self.prediction_bar is not None:
       self.prediction_bar.close()
    _A      =       None

 def     snake_case_      (    self					:					str			,						__lowerCAmelCase					:					Union[str, Any]			,						__lowerCAmelCase					:					Any			,						__lowerCAmelCase					:					Dict			,						__lowerCAmelCase					:					int=None			,						**__lowerCAmelCase					:					Optional[int]      )   ->	List[Any]:
    # Only for when there is no evaluation
    if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs:
       _A      =       {'''Training Loss''': logs['''loss''']}
       # First column is necessarily Step sine we're not in epoch eval strategy
       _A      =       state.global_step
       self.training_tracker.write_line(__lowerCAmelCase      )

 def     snake_case_      (    self					:					Union[str, Any]			,						__lowerCAmelCase					:					str			,						__lowerCAmelCase					:					Any			,						__lowerCAmelCase					:					List[Any]			,						__lowerCAmelCase					:					List[str]=None			,						**__lowerCAmelCase					:					Dict      )   ->	str:
    if self.training_tracker is not None:
       _A      =       {'''Training Loss''': '''No log''', '''Validation Loss''': '''No log'''}
       for log in reversed(state.log_history      ):
          if "loss" in log:
             _A      =       log['''loss''']
             break

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

 def     snake_case_      (    self					:					Tuple			,						__lowerCAmelCase					:					str			,						__lowerCAmelCase					:					Optional[Any]			,						__lowerCAmelCase					:					Optional[int]			,						**__lowerCAmelCase					:					Dict      )   ->	Tuple:
    self.training_tracker.update(
        state.global_step			,						comment=f'''Epoch {int(state.epoch      )}/{state.num_train_epochs}'''			,						force_update=__lowerCAmelCase      )
    _A      =       None





"""simple docstring"""






import json
from typing import List, Optional, Tuple

from tokenizers import normalizers
from tokenizers.pre_tokenizers import BertPreTokenizer, PreTokenizer

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_roformer import RoFormerTokenizer
from .tokenization_utils import JiebaPreTokenizer


__SCREAMING_SNAKE_CASE							=     logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE							=     {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

__SCREAMING_SNAKE_CASE							=     {
    'vocab_file': {
        'junnyu/roformer_chinese_small': 'https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/vocab.txt',
        'junnyu/roformer_chinese_base': 'https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/vocab.txt',
        'junnyu/roformer_chinese_char_small': (
            'https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/vocab.txt'
        ),
        'junnyu/roformer_chinese_char_base': (
            'https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/vocab.txt'
        ),
        'junnyu/roformer_small_discriminator': (
            'https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/vocab.txt'
        ),
        'junnyu/roformer_small_generator': (
            'https://huggingface.co/junnyu/roformer_small_generator/resolve/main/vocab.txt'
        ),
    }
}

__SCREAMING_SNAKE_CASE							=     {
    'junnyu/roformer_chinese_small': 1_536,
    'junnyu/roformer_chinese_base': 1_536,
    'junnyu/roformer_chinese_char_small': 512,
    'junnyu/roformer_chinese_char_base': 512,
    'junnyu/roformer_small_discriminator': 128,
    'junnyu/roformer_small_generator': 128,
}


__SCREAMING_SNAKE_CASE							=     {
    'junnyu/roformer_chinese_small': {'do_lower_case': True},
    'junnyu/roformer_chinese_base': {'do_lower_case': True},
    'junnyu/roformer_chinese_char_small': {'do_lower_case': True},
    'junnyu/roformer_chinese_char_base': {'do_lower_case': True},
    'junnyu/roformer_small_discriminator': {'do_lower_case': True},
    'junnyu/roformer_small_generator': {'do_lower_case': True},
}





class 				a__					(						A__       ):
     UpperCAmelCase__         =      VOCAB_FILES_NAMES
     UpperCAmelCase__         =      PRETRAINED_VOCAB_FILES_MAP
     UpperCAmelCase__         =      PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
     UpperCAmelCase__         =      PRETRAINED_INIT_CONFIGURATION
     UpperCAmelCase__         =      RoFormerTokenizer



     def __init__(							self :Tuple      ,  _lowerCamelCase :Dict=None      ,  _lowerCamelCase :Dict=None      ,  _lowerCamelCase :List[Any]=True      ,  _lowerCamelCase :Dict="[UNK]"      ,  _lowerCamelCase :List[str]="[SEP]"      ,  _lowerCamelCase :str="[PAD]"      ,  _lowerCamelCase :Optional[Any]="[CLS]"      ,  _lowerCamelCase :Optional[int]="[MASK]"      ,  _lowerCamelCase :str=True      ,  _lowerCamelCase :Tuple=None      ,  **_lowerCamelCase :Any      ,  ):







      '''simple docstring'''



      super().__init__(
          _lowerCamelCase      ,  tokenizer_file=_lowerCamelCase      ,  do_lower_case=_lowerCamelCase      ,  unk_token=_lowerCamelCase      ,  sep_token=_lowerCamelCase      ,  pad_token=_lowerCamelCase      ,  cls_token=_lowerCamelCase      ,  mask_token=_lowerCamelCase      ,  tokenize_chinese_chars=_lowerCamelCase      ,  strip_accents=_lowerCamelCase      ,  **_lowerCamelCase      ,  )

      UpperCamelCase_     :					Any   	=json.loads(self.backend_tokenizer.normalizer.__getstate__()   )
      if (
          pre_tok_state.get('lowercase'      ,  _lowerCamelCase   ) != do_lower_case
          or pre_tok_state.get('strip_accents'      ,  _lowerCamelCase   ) != strip_accents
      ):
       UpperCamelCase_     :					Optional[Any]   	=getattr(_lowerCamelCase      ,  pre_tok_state.pop('type'   )   )
       UpperCamelCase_     :					Tuple   	=do_lower_case
       UpperCamelCase_     :					Union[str, Any]   	=strip_accents
       UpperCamelCase_     :					Union[str, Any]   	=pre_tok_class(**_lowerCamelCase   )

      UpperCamelCase_     :					List[str]   	=do_lower_case



     def __getstate__(							self :Any   ):







      '''simple docstring'''



      UpperCamelCase_     :					str   	=self.__dict__.copy()
      UpperCamelCase_     :					Union[str, Any]   	=BertPreTokenizer()
      return state



     def __setstate__(							self :str      ,  _lowerCamelCase :Optional[Any]   ):







      '''simple docstring'''



      UpperCamelCase_     :					int   	=d
      UpperCamelCase_     :					Optional[int]   	=self.__dict__['_tokenizer'].get_vocab()
      UpperCamelCase_     :					Any   	=PreTokenizer.custom(JiebaPreTokenizer(_lowerCamelCase   )   )



     def  lowerCamelCase_						(							self :Dict      ,  _lowerCamelCase :Optional[Any]      ,  _lowerCamelCase :Any=None   ):







      '''simple docstring'''



      UpperCamelCase_     :					int   	=[self.cls_token_id] + token_ids_a + [self.sep_token_id]

      if token_ids_a:
       output += token_ids_a + [self.sep_token_id]

      return output



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







      '''simple docstring'''



      UpperCamelCase_     :					List[Any]   	=[self.sep_token_id]
      UpperCamelCase_     :					List[Any]   	=[self.cls_token_id]
      if token_ids_a is None:
       return len(cls + token_ids_a + sep   ) * [0]
      return len(cls + token_ids_a + sep   ) * [0] + len(token_ids_a + sep   ) * [1]



     def  lowerCamelCase_						(							self :Optional[int]      ,  _lowerCamelCase :str      ,  _lowerCamelCase :Optional[str] = None   ):







      '''simple docstring'''



      UpperCamelCase_     :					List[Any]   	=self._tokenizer.model.save(_lowerCamelCase      ,  name=_lowerCamelCase   )
      return tuple(_lowerCamelCase   )




     def  lowerCamelCase_						(							self :str      ,  _lowerCamelCase :List[str]      ,  _lowerCamelCase :List[Any]=None      ,  _lowerCamelCase :Optional[Any]=None      ,  _lowerCamelCase :int=False      ,  **_lowerCamelCase :Optional[int]      ,  ):







      '''simple docstring'''



      UpperCamelCase_     :					str   	=BertPreTokenizer()
      return super().save_pretrained(_lowerCamelCase      ,  _lowerCamelCase      ,  _lowerCamelCase      ,  _lowerCamelCase      ,  **_lowerCamelCase   )
