



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       (      _lowerCamelCase						):





       '''simple docstring'''




       _lowerCAmelCase  :							Tuple				     =					int(_lowerCamelCase						)
       _lowerCAmelCase  :							Optional[Any]				     =					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        A       (      _lowerCamelCase       ,				_lowerCamelCase       ,				_lowerCamelCase       ,				_lowerCamelCase       ,				_lowerCamelCase=300						):





       '''simple docstring'''




       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        A       (      _lowerCamelCase						):





       '''simple docstring'''




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



class 						UpperCAmelCase_					:
       lowerCamelCase__         =	5
       lowerCamelCase__         =	0.2







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

              '''simple docstring'''

              _lowerCAmelCase  :							List[Any]				     =					total
              _lowerCAmelCase  :							Any				     =					"" if prefix is None else prefix
              _lowerCAmelCase  :							str				     =					leave
              _lowerCAmelCase  :							Optional[Any]				     =					parent
              _lowerCAmelCase  :							Optional[int]				     =					width
              _lowerCAmelCase  :							Tuple				     =					None
              _lowerCAmelCase  :							Dict				     =					None
              _lowerCAmelCase  :							Tuple				     =					None







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

              '''simple docstring'''

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







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

              '''simple docstring'''

              _lowerCAmelCase  :							Union[str, Any]				     =					" " * (len(str(self.total)) - len(str(__a))) + str(__a)
              if self.elapsed_time is None:
                     _lowerCAmelCase  :							Optional[int]				     =					f"[{spaced_value}/{self.total} : < :"
              elif self.predicted_remaining is None:
                     _lowerCAmelCase  :							Optional[int]				     =					f"[{spaced_value}/{self.total} {format_time(self.elapsed_time)}"
              else:
                     _lowerCAmelCase  :							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      snake_case__   (     self):

              '''simple docstring'''

              _lowerCAmelCase  :							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:
                     _lowerCAmelCase  :							List[str]				     =					disp.display(disp.HTML(self.html_code),    display_id=__a)
              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 						UpperCAmelCase_					( a):







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

              '''simple docstring'''

              super().__init__(__a)
              _lowerCAmelCase  :							str				     =					None if column_names is None else [column_names]
              _lowerCAmelCase  :							List[Any]				     =					None







       def      snake_case__   (     self):

              '''simple docstring'''

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







       def      snake_case__   (     self,    __a):

              '''simple docstring'''

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







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

              '''simple docstring'''

              _lowerCAmelCase  :							Any				     =					NotebookProgressBar(__a,    prefix=__a,    parent=self,    width=__a)
              return self.child_bar







       def      snake_case__   (     self):

              '''simple docstring'''

              _lowerCAmelCase  :							Optional[Any]				     =					None
              self.display()



class 						UpperCAmelCase_					( a):







       def __init__(     self):

              '''simple docstring'''

              _lowerCAmelCase  :							Tuple				     =					None
              _lowerCAmelCase  :							List[Any]				     =					None
              _lowerCAmelCase  :							List[str]				     =					False







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

              '''simple docstring'''

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







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

              '''simple docstring'''

              _lowerCAmelCase  :							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,    )
              _lowerCAmelCase  :							Tuple				     =					False







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

              '''simple docstring'''

              if not has_length(__a):
                     return
              if self.prediction_bar is None:
                     if self.training_tracker is not None:
                            _lowerCAmelCase  :							Any				     =					self.training_tracker.add_child(len(__a))
                     else:
                            _lowerCAmelCase  :							Tuple				     =					NotebookProgressBar(len(__a))
                     self.prediction_bar.update(1)
              else:
                     self.prediction_bar.update(self.prediction_bar.value + 1)







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

              '''simple docstring'''

              if self.prediction_bar is not None:
                     self.prediction_bar.close()
              _lowerCAmelCase  :							List[Any]				     =					None







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

              '''simple docstring'''

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







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

              '''simple docstring'''

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

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







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

              '''simple docstring'''

              self.training_tracker.update(
                  state.global_step,    comment=f"Epoch {int(state.epoch)}/{state.num_train_epochs}",    force_update=__a)
              _lowerCAmelCase  :							Dict				     =					None





import baseaa






def        A       (      _lowerCamelCase						):





							'''simple docstring'''




							return baseaa.aaaencode(string.encode("utf-8"						)						)






def        A       (      _lowerCamelCase						):





							'''simple docstring'''




							return baseaa.aaadecode(_lowerCamelCase						).decode("utf-8"						)


if __name__ == "__main__":
						import doctest

						doctest.testmod()
