



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 		a__			(		_UpperCamelCase			: Optional[int]							):
   __lowerCamelCase													=    int(_UpperCamelCase							)
   __lowerCamelCase							,__lowerCamelCase							,__lowerCamelCase													=    t // 36_00, (t // 60) % 60, t % 60
   return F"""{h}:{m:02d}:{s:02d}""" if h != 0 else F"""{m:02d}:{s:02d}"""





def 		a__			(		_UpperCamelCase			: Optional[Any] ,_UpperCamelCase			: Dict ,_UpperCamelCase			: List[Any] ,_UpperCamelCase			: Tuple ,_UpperCamelCase			: Optional[int]=3_00							):
   # docstyle-ignore
   return F"""
    <div>
      {prefix}
      <progress value='{value}' max='{total}' style='width:{width}px; height:20px; vertical-align: middle;'></progress>
      {label}
    </div>
    """





def 		a__			(		_UpperCamelCase			: List[str]							):
   __lowerCamelCase													=    '''<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:
         __lowerCamelCase													=    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     :
 lowerCAmelCase__       					=  5
 lowerCAmelCase__       					=  0.2




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



    '''simple docstring'''
    __lowerCamelCase													=    total
    __lowerCamelCase													=    '''''' if prefix is None else prefix
    __lowerCamelCase													=    leave
    __lowerCamelCase													=    parent
    __lowerCamelCase													=    width
    __lowerCamelCase													=    None
    __lowerCamelCase													=    None
    __lowerCamelCase													=    None




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase = False	,				__UpperCAmelCase = None				):



    '''simple docstring'''
    __lowerCamelCase													=    value
    if comment is not None:
       __lowerCamelCase													=    comment
    if self.last_value is None:
       __lowerCamelCase													=    __lowerCamelCase													=    time.time()
       __lowerCamelCase													=    __lowerCamelCase													=    value
       __lowerCamelCase													=    __lowerCamelCase													=    None
       __lowerCamelCase													=    self.warmup
       __lowerCamelCase													=    1
       self.update_bar(__UpperCAmelCase				)
    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
       __lowerCamelCase													=    time.time()
       __lowerCamelCase													=    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:
          __lowerCamelCase													=    self.elapsed_time / (value - self.start_value)
       else:
          __lowerCamelCase													=    None
       if value >= self.total:
          __lowerCamelCase													=    self.total
          __lowerCamelCase													=    None
          if not self.leave:
             self.close()
       elif self.average_time_per_item is not None:
          __lowerCamelCase													=    self.average_time_per_item * (self.total - value)
       self.update_bar(__UpperCAmelCase				)
       __lowerCamelCase													=    value
       __lowerCamelCase													=    current_time
       if self.average_time_per_item is None:
          __lowerCamelCase													=    1
       else:
          __lowerCamelCase													=    max(int(self.update_every / self.average_time_per_item				)	,				1				)




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase=None				):



    '''simple docstring'''
    __lowerCamelCase													=    ''' ''' * (len(str(self.total				)				) - len(str(__UpperCAmelCase				)				)) + str(__UpperCAmelCase				)
    if self.elapsed_time is None:
       __lowerCamelCase													=    F"""[{spaced_value}/{self.total} : < :"""
    elif self.predicted_remaining is None:
       __lowerCamelCase													=    F"""[{spaced_value}/{self.total} {format_time(self.elapsed_time				)}"""
    else:
       __lowerCamelCase													=    (
           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				):



    '''simple docstring'''
    __lowerCamelCase													=    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:
       __lowerCamelCase													=    disp.display(disp.HTML(self.html_code				)	,				display_id=__UpperCAmelCase				)
    else:
       self.output.update(disp.HTML(self.html_code				)				)




 def        lowerCamelCase      (						self				):



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



class  __lowerCAmelCase     (	lowerCAmelCase__   ):




 def __init__(						self	,				__UpperCAmelCase	,				__UpperCAmelCase=None				):



    '''simple docstring'''
    super().__init__(__UpperCAmelCase				)
    __lowerCamelCase													=    None if column_names is None else [column_names]
    __lowerCamelCase													=    None




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    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:
       __lowerCamelCase													=    disp.display(disp.HTML(self.html_code				)	,				display_id=__UpperCAmelCase				)
    else:
       self.output.update(disp.HTML(self.html_code				)				)




 def        lowerCamelCase      (						self	,				__UpperCAmelCase				):



    '''simple docstring'''
    if self.inner_table is None:
       __lowerCamelCase													=    [list(values.keys()				), list(values.values()				)]
    else:
       __lowerCamelCase													=    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(__UpperCAmelCase				)
          __lowerCamelCase													=    columns
       self.inner_table.append([values[c] for c in columns]				)




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase=None	,				__UpperCAmelCase=300				):



    '''simple docstring'''
    __lowerCamelCase													=    NotebookProgressBar(__UpperCAmelCase	,				prefix=__UpperCAmelCase	,				parent=self	,				width=__UpperCAmelCase				)
    return self.child_bar




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    None
    self.display()



class  __lowerCAmelCase     (	lowerCAmelCase__   ):




 def __init__(						self				):



    '''simple docstring'''
    __lowerCamelCase													=    None
    __lowerCamelCase													=    None
    __lowerCamelCase													=    False




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				**__UpperCAmelCase				):



    '''simple docstring'''
    __lowerCamelCase													=    '''Epoch''' if args.evaluation_strategy == IntervalStrategy.EPOCH else '''Step'''
    __lowerCamelCase													=    0
    __lowerCamelCase													=    0
    __lowerCamelCase													=    [self.first_column] + ['''Training Loss''']
    if args.evaluation_strategy != IntervalStrategy.NO:
       column_names.append('''Validation Loss'''				)
    __lowerCamelCase													=    NotebookTrainingTracker(state.max_steps	,				__UpperCAmelCase				)




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				**__UpperCAmelCase				):



    '''simple docstring'''
    __lowerCamelCase													=    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	,				)
    __lowerCamelCase													=    False




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase=None	,				**__UpperCAmelCase				):



    '''simple docstring'''
    if not has_length(__UpperCAmelCase				):
       return
    if self.prediction_bar is None:
       if self.training_tracker is not None:
          __lowerCamelCase													=    self.training_tracker.add_child(len(__UpperCAmelCase				)				)
       else:
          __lowerCamelCase													=    NotebookProgressBar(len(__UpperCAmelCase				)				)
       self.prediction_bar.update(1				)
    else:
       self.prediction_bar.update(self.prediction_bar.value + 1				)




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				**__UpperCAmelCase				):



    '''simple docstring'''
    if self.prediction_bar is not None:
       self.prediction_bar.close()
    __lowerCamelCase													=    None




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase=None	,				**__UpperCAmelCase				):



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




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase=None	,				**__UpperCAmelCase				):



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

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




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				**__UpperCAmelCase				):



    '''simple docstring'''
    self.training_tracker.update(
        state.global_step	,				comment=F"""Epoch {int(state.epoch				)}/{state.num_train_epochs}"""	,				force_update=__UpperCAmelCase				)
    __lowerCamelCase													=    None











def 		a__			(		_UpperCamelCase			: str ,_UpperCamelCase			: str = " "							):
   __lowerCamelCase													=    []

   __lowerCamelCase													=    0
   for index, char in enumerate(_UpperCamelCase							):
      if char == separator:
         split_words.append(string[last_index:index]							)
         __lowerCamelCase													=    index + 1
      elif index + 1 == len(_UpperCamelCase							):
         split_words.append(string[last_index : index + 1]							)
   return split_words


if __name__ == "__main__":
  from doctest import testmod

  testmod()






