




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 lowerCamelCase__       (      _lowercase				):

     '''simple docstring'''


     UpperCAmelCase_      :							List[Any]       =      int(_lowercase				)
     UpperCAmelCase_,   UpperCAmelCase_,   UpperCAmelCase_      :							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 lowerCamelCase__       (      _lowercase			,						_lowercase			,						_lowercase			,						_lowercase			,						_lowercase=300				):

     '''simple docstring'''


     return f'''
    <div>
      {prefix}
      <progress value=\'{value}\' max=\'{total}\' style=\'width:{width}px; height:20px; vertical-align: middle;\'></progress>
      {label}
    </div>
    '''







def lowerCamelCase__       (      _lowercase				):

     '''simple docstring'''


     UpperCAmelCase_      :							Optional[int]       =      '''<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_      :							Union[str, Any]       =      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   __a:






    """simple docstring"""





    lowerCAmelCase        =				5
    lowerCAmelCase        =				0.2





    def __init__(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE = None       ,_SCREAMING_SNAKE_CASE = True       ,_SCREAMING_SNAKE_CASE = None       ,_SCREAMING_SNAKE_CASE = 300       ,)			->		Union[str, Any]:
         UpperCAmelCase_      :							List[Any]       =      total
         UpperCAmelCase_      :							Any       =      '''''' if prefix is None else prefix
         UpperCAmelCase_      :							List[Any]       =      leave
         UpperCAmelCase_      :							Any       =      parent
         UpperCAmelCase_      :							Optional[Any]       =      width
         UpperCAmelCase_      :							int       =      None
         UpperCAmelCase_      :							int       =      None
         UpperCAmelCase_      :							Optional[Any]       =      None





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE = False       ,_SCREAMING_SNAKE_CASE = None  )			->		List[Any]:
         UpperCAmelCase_      :							Union[str, Any]       =      value
         if comment is not None:
              UpperCAmelCase_      :							Optional[int]       =      comment
         if self.last_value is None:
              UpperCAmelCase_      :							List[str]       =      time.time()
              UpperCAmelCase_      :							Optional[int]       =      value
              UpperCAmelCase_      :							Union[str, Any]       =      None
              UpperCAmelCase_      :							str       =      self.warmup
              UpperCAmelCase_      :							Dict       =      1
              self.update_bar(_SCREAMING_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_      :							Optional[Any]       =      time.time()
              UpperCAmelCase_      :							Union[str, 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:
                   UpperCAmelCase_      :							List[Any]       =      self.elapsed_time / (value - self.start_value)
              else:
                   UpperCAmelCase_      :							List[Any]       =      None
              if value >= self.total:
                   UpperCAmelCase_      :							Any       =      self.total
                   UpperCAmelCase_      :							Dict       =      None
                   if not self.leave:
                        self.close()
              elif self.average_time_per_item is not None:
                   UpperCAmelCase_      :							int       =      self.average_time_per_item * (self.total - value)
              self.update_bar(_SCREAMING_SNAKE_CASE  )
              UpperCAmelCase_      :							List[Any]       =      value
              UpperCAmelCase_      :							Dict       =      current_time
              if self.average_time_per_item is None:
                   UpperCAmelCase_      :							Optional[int]       =      1
              else:
                   UpperCAmelCase_      :							Optional[Any]       =      max(int(self.update_every / self.average_time_per_item  )       ,1  )





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





    def 							a__				(				self  )			->		str:
         if self.parent is None and self.output is not None:
              self.output.update(disp.HTML(''''''  )  )


class   __a(					_a       ):






    """simple docstring"""





    def __init__(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE=None  )			->		Optional[int]:
         super().__init__(_SCREAMING_SNAKE_CASE  )
         UpperCAmelCase_      :							Tuple       =      None if column_names is None else [column_names]
         UpperCAmelCase_      :							int       =      None





    def 							a__				(				self  )			->		Optional[int]:
         UpperCAmelCase_      :							Optional[int]       =      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_      :							Any       =      disp.display(disp.HTML(self.html_code  )       ,display_id=_SCREAMING_SNAKE_CASE  )
         else:
              self.output.update(disp.HTML(self.html_code  )  )





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE  )			->		int:
         if self.inner_table is None:
              UpperCAmelCase_      :							int       =      [list(values.keys()  ), list(values.values()  )]
         else:
              UpperCAmelCase_      :							Tuple       =      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(_SCREAMING_SNAKE_CASE  )
                   UpperCAmelCase_      :							str       =      columns
              self.inner_table.append([values[c] for c in columns]  )





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE=None       ,_SCREAMING_SNAKE_CASE=300  )			->		Tuple:
         UpperCAmelCase_      :							int       =      NotebookProgressBar(_SCREAMING_SNAKE_CASE       ,prefix=_SCREAMING_SNAKE_CASE       ,parent=self       ,width=_SCREAMING_SNAKE_CASE  )
         return self.child_bar





    def 							a__				(				self  )			->		List[str]:
         UpperCAmelCase_      :							Tuple       =      None
         self.display()


class   __a(					_a       ):






    """simple docstring"""





    def __init__(				self  )			->		Optional[int]:
         UpperCAmelCase_      :							Optional[int]       =      None
         UpperCAmelCase_      :							str       =      None
         UpperCAmelCase_      :							Any       =      False





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





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,**_SCREAMING_SNAKE_CASE  )			->		List[Any]:
         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_      :							Any       =      False





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE=None       ,**_SCREAMING_SNAKE_CASE  )			->		Optional[Any]:
         if not has_length(_SCREAMING_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(_SCREAMING_SNAKE_CASE  )  )
              else:
                   UpperCAmelCase_      :							List[Any]       =      NotebookProgressBar(len(_SCREAMING_SNAKE_CASE  )  )
              self.prediction_bar.update(1  )
         else:
              self.prediction_bar.update(self.prediction_bar.value + 1  )





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,**_SCREAMING_SNAKE_CASE  )			->		List[Any]:
         if self.prediction_bar is not None:
              self.prediction_bar.close()
         UpperCAmelCase_      :							Any       =      None





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





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE=None       ,**_SCREAMING_SNAKE_CASE  )			->		str:
         if self.training_tracker is not None:
              UpperCAmelCase_      :							Tuple       =      {'''Training Loss''': '''No log''', '''Validation Loss''': '''No log'''}
              for log in reversed(state.log_history  ):
                   if "loss" in log:
                        UpperCAmelCase_      :							int       =      log['''loss''']
                        break

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





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,**_SCREAMING_SNAKE_CASE  )			->		Optional[int]:
         self.training_tracker.update(
             state.global_step       ,comment=f'''Epoch {int(state.epoch  )}/{state.num_train_epochs}'''       ,force_update=_SCREAMING_SNAKE_CASE  )
         UpperCAmelCase_      :							str       =      None





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


__a          =			{
    'configuration_bloom': ['BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BloomConfig', 'BloomOnnxConfig'],
}
try:
     if not is_tokenizers_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     __a          =			['BloomTokenizerFast']

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     __a          =			[
         'BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST',
         'BloomForCausalLM',
         'BloomModel',
         'BloomPreTrainedModel',
         'BloomForSequenceClassification',
         'BloomForTokenClassification',
         'BloomForQuestionAnswering',
     ]

if TYPE_CHECKING:
     from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig

     try:
          if not is_tokenizers_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_bloom_fast import BloomTokenizerFast

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_bloom import (
              BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST,
              BloomForCausalLM,
              BloomForQuestionAnswering,
              BloomForSequenceClassification,
              BloomForTokenClassification,
              BloomModel,
              BloomPreTrainedModel,
          )

else:
     import sys

     __a          =			_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)