




'''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 		UpperCamelCase__      (		__SCREAMING_SNAKE_CASE		)    ->   Any:
      snake_case__       :   List[str]	      =    int(__SCREAMING_SNAKE_CASE		)
      snake_case__      ,							snake_case__      ,							snake_case__       :   Union[str, 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 		UpperCamelCase__      (		__SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE=300		)    ->   List[Any]:
      # docstyle-ignore
      return f"\n    <div>\n      {prefix}\n      <progress value='{value}' max='{total}' style='width:{width}px; height:20px; vertical-align: middle;'></progress>\n      {label}\n    </div>\n    "






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






class        lowercase_      :
     A_											=			5
     A_											=			0.2





     def __init__(     self     :					Optional[int]     ,							__lowerCamelCase     :					int     ,							__lowerCamelCase     :					Optional[str] = None     ,							__lowerCamelCase     :					bool = True     ,							__lowerCamelCase     :					Optional["NotebookTrainingTracker"] = None     ,							__lowerCamelCase     :					int = 300     ,							):
           snake_case__       :   int	      =    total
           snake_case__       :   Tuple	      =    '' if prefix is None else prefix
           snake_case__       :   Optional[Any]	      =    leave
           snake_case__       :   Optional[int]	      =    parent
           snake_case__       :   Optional[Any]	      =    width
           snake_case__       :   List[str]	      =    None
           snake_case__       :   Union[str, Any]	      =    None
           snake_case__       :   Dict	      =    None





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





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





     def 			_lowerCAmelCase      (     self     :					Dict			):
           if self.parent is None and self.output is not None:
                 self.output.update(disp.HTML(''			)			)






class        lowercase_      (						lowerCAmelCase_							):





     def __init__(     self     :					Any     ,							__lowerCamelCase     :					Union[str, Any]     ,							__lowerCamelCase     :					int=None			):
           super().__init__(__lowerCamelCase			)
           snake_case__       :   Tuple	      =    None if column_names is None else [column_names]
           snake_case__       :   Dict	      =    None





     def 			_lowerCAmelCase      (     self     :					List[Any]			):
           snake_case__       :   Tuple	      =    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:
                 snake_case__       :   Optional[int]	      =    disp.display(disp.HTML(self.html_code			)     ,							display_id=__lowerCamelCase			)
           else:
                 self.output.update(disp.HTML(self.html_code			)			)





     def 			_lowerCAmelCase      (     self     :					Union[str, Any]     ,							__lowerCamelCase     :					Union[str, Any]			):
           if self.inner_table is None:
                 snake_case__       :   Optional[int]	      =    [list(values.keys()			), list(values.values()			)]
           else:
                 snake_case__       :   List[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(__lowerCamelCase			)
                       snake_case__       :   Optional[int]	      =    columns
                 self.inner_table.append([values[c] for c in columns]			)





     def 			_lowerCAmelCase      (     self     :					Optional[int]     ,							__lowerCamelCase     :					List[Any]     ,							__lowerCamelCase     :					str=None     ,							__lowerCamelCase     :					List[Any]=300			):
           snake_case__       :   Optional[int]	      =    NotebookProgressBar(__lowerCamelCase     ,							prefix=__lowerCamelCase     ,							parent=self     ,							width=__lowerCamelCase			)
           return self.child_bar





     def 			_lowerCAmelCase      (     self     :					Optional[Any]			):
           snake_case__       :   List[str]	      =    None
           self.display()






class        lowercase_      (						lowerCAmelCase_							):





     def __init__(     self     :					Optional[int]			):
           snake_case__       :   Any	      =    None
           snake_case__       :   Optional[int]	      =    None
           snake_case__       :   Optional[Any]	      =    False





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





     def 			_lowerCAmelCase      (     self     :					int     ,							__lowerCamelCase     :					List[Any]     ,							__lowerCamelCase     :					Any     ,							__lowerCamelCase     :					Tuple     ,							**__lowerCamelCase     :					List[Any]			):
           snake_case__       :   str	      =    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     ,							)
           snake_case__       :   str	      =    False





     def 			_lowerCAmelCase      (     self     :					Optional[Any]     ,							__lowerCamelCase     :					Dict     ,							__lowerCamelCase     :					Union[str, Any]     ,							__lowerCamelCase     :					List[Any]     ,							__lowerCamelCase     :					List[Any]=None     ,							**__lowerCamelCase     :					List[str]			):
           if not has_length(__lowerCamelCase			):
                 return
           if self.prediction_bar is None:
                 if self.training_tracker is not None:
                       snake_case__       :   Optional[Any]	      =    self.training_tracker.add_child(len(__lowerCamelCase			)			)
                 else:
                       snake_case__       :   Optional[Any]	      =    NotebookProgressBar(len(__lowerCamelCase			)			)
                 self.prediction_bar.update(1			)
           else:
                 self.prediction_bar.update(self.prediction_bar.value + 1			)





     def 			_lowerCAmelCase      (     self     :					List[str]     ,							__lowerCamelCase     :					Union[str, Any]     ,							__lowerCamelCase     :					str     ,							__lowerCamelCase     :					Optional[Any]     ,							**__lowerCamelCase     :					List[str]			):
           if self.prediction_bar is not None:
                 self.prediction_bar.close()
           snake_case__       :   Union[str, Any]	      =    None





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





     def 			_lowerCAmelCase      (     self     :					List[Any]     ,							__lowerCamelCase     :					Optional[int]     ,							__lowerCamelCase     :					Dict     ,							__lowerCamelCase     :					List[str]     ,							__lowerCamelCase     :					List[Any]=None     ,							**__lowerCamelCase     :					Optional[int]			):
           if self.training_tracker is not None:
                 snake_case__       :   str	      =    {'Training Loss': 'No log', 'Validation Loss': 'No log'}
                 for log in reversed(state.log_history			):
                       if "loss" in log:
                             snake_case__       :   List[str]	      =    log['loss']
                             break

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





     def 			_lowerCAmelCase      (     self     :					Optional[int]     ,							__lowerCamelCase     :					str     ,							__lowerCamelCase     :					Optional[int]     ,							__lowerCamelCase     :					str     ,							**__lowerCamelCase     :					str			):
           self.training_tracker.update(
               state.global_step     ,							comment=F"Epoch {int(state.epoch			)}/{state.num_train_epochs}"     ,							force_update=__lowerCamelCase			)
           snake_case__       :   Any	      =    None






'''simple docstring'''






# flake8: noqa
# Lint as: python3
A_			      =		[
    "VerificationMode",
    "Version",
    "disable_progress_bar",
    "enable_progress_bar",
    "is_progress_bar_enabled",
    "experimental",
]

from .info_utils import VerificationMode
from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled
from .version import Version
from .experimental import experimental
