





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__							(   SCREAMING_SNAKE_CASE__)       ->   Optional[int]:
  __snake_case:						Any			  =						int(lowerCamelCase__)
  __snake_case  ,       __snake_case  ,       __snake_case:						str			  =						t // 3600, (t // 60) % 60, t % 60
  return F'''{h}:{m:02d}:{s:02d}''' if h != 0 else F'''{m:02d}:{s:02d}'''







def       A__							(   SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=300)       ->   List[Any]:
  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__							(   SCREAMING_SNAKE_CASE__)       ->   str:
  __snake_case:						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:
      __snake_case:						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    __snake_case	:
       lowerCAmelCase__						=					5
       lowerCAmelCase__						=					0.2




       def __init__(  self			:   Union[str, Any]							,    A			:   Tuple							,    A			:   str = None							,    A			:   str = True							,    A			:   Any = None							,    A			:   List[str] = 300							,    ):
         __snake_case:						Union[str, Any]			  =						total
         __snake_case:						List[str]			  =						"""""" if prefix is None else prefix
         __snake_case:						Optional[Any]			  =						leave
         __snake_case:						Dict			  =						parent
         __snake_case:						str			  =						width
         __snake_case:						Optional[Any]			  =						None
         __snake_case:						Any			  =						None
         __snake_case:						List[str]			  =						None




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




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






       def 							UpperCAmelCase__			(  self			:   int		):







         if self.parent is None and self.output is not None:
           self.output.update(disp.HTML(""""""		)		)







class    __snake_case	(							a_      ):




       def __init__(  self			:   Dict							,    A			:   Dict							,    A			:   Optional[Any]=None		):
         super().__init__(A		)
         __snake_case:						Optional[int]			  =						None if column_names is None else [column_names]
         __snake_case:						Optional[int]			  =						None




       def 							UpperCAmelCase__			(  self			:   List[str]		):
         __snake_case:						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:
           __snake_case:						List[str]			  =						disp.display(disp.HTML(self.html_code		)							,    display_id=A		)
         else:
           self.output.update(disp.HTML(self.html_code		)		)




       def 							UpperCAmelCase__			(  self			:   Tuple							,    A			:   Any		):







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




       def 							UpperCAmelCase__			(  self			:   Union[str, Any]							,    A			:   List[Any]							,    A			:   Union[str, Any]=None							,    A			:   Any=300		):
         __snake_case:						str			  =						NotebookProgressBar(A							,    prefix=A							,    parent=self							,    width=A		)
         return self.child_bar






       def 							UpperCAmelCase__			(  self			:   Union[str, Any]		):
         __snake_case:						Dict			  =						None
         self.display()







class    __snake_case	(							a_      ):




       def __init__(  self			:   Any		):
         __snake_case:						Optional[Any]			  =						None
         __snake_case:						List[str]			  =						None
         __snake_case:						int			  =						False




       def 							UpperCAmelCase__			(  self			:   Optional[int]							,    A			:   Tuple							,    A			:   List[Any]							,    A			:   int							,    **A			:   Tuple		):
         __snake_case:						List[Any]			  =						"""Epoch""" if args.evaluation_strategy == IntervalStrategy.EPOCH else """Step"""
         __snake_case:						Optional[Any]			  =						0
         __snake_case:						Dict			  =						0
         __snake_case:						Optional[int]			  =						[self.first_column] + ["""Training Loss"""]
         if args.evaluation_strategy != IntervalStrategy.NO:
           column_names.append("""Validation Loss"""		)
         __snake_case:						Tuple			  =						NotebookTrainingTracker(state.max_steps							,    A		)




       def 							UpperCAmelCase__			(  self			:   List[Any]							,    A			:   Tuple							,    A			:   Tuple							,    A			:   Optional[int]							,    **A			:   Any		):
         __snake_case:						int			  =						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:						Union[str, Any]			  =						False




       def 							UpperCAmelCase__			(  self			:   Tuple							,    A			:   Any							,    A			:   Dict							,    A			:   Union[str, Any]							,    A			:   Any=None							,    **A			:   int		):







         if not has_length(A		):
           return
         if self.prediction_bar is None:
           if self.training_tracker is not None:
             __snake_case:						List[Any]			  =						self.training_tracker.add_child(len(A		)		)
           else:
             __snake_case:						str			  =						NotebookProgressBar(len(A		)		)
           self.prediction_bar.update(1		)
         else:
           self.prediction_bar.update(self.prediction_bar.value + 1		)




       def 							UpperCAmelCase__			(  self			:   List[str]							,    A			:   Optional[Any]							,    A			:   int							,    A			:   Any							,    **A			:   Optional[int]		):







         if self.prediction_bar is not None:
           self.prediction_bar.close()
         __snake_case:						Union[str, Any]			  =						None




       def 							UpperCAmelCase__			(  self			:   List[str]							,    A			:   List[str]							,    A			:   int							,    A			:   Tuple							,    A			:   Optional[int]=None							,    **A			:   List[str]		):







         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:						Optional[int]			  =						state.global_step
           self.training_tracker.write_line(A		)




       def 							UpperCAmelCase__			(  self			:   Optional[int]							,    A			:   Dict							,    A			:   str							,    A			:   List[Any]							,    A			:   int=None							,    **A			:   Any		):







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

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






       def 							UpperCAmelCase__			(  self			:   Tuple							,    A			:   Union[str, Any]							,    A			:   List[str]							,    A			:   Optional[Any]							,    **A			:   int		):
         self.training_tracker.update(
             state.global_step							,    comment=f'''Epoch {int(state.epoch		)}/{state.num_train_epochs}'''							,    force_update=A		)
         __snake_case:						str			  =						None













import math
import time

from transformers import Trainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
      import torch_xla.core.xla_model as xm
      import torch_xla.debug.metrics as met







class    __snake_case	(							__lowerCamelCase      ):





       '''simple docstring'''







       def __init__(  self			:   str							,    *A			:   Dict							,    A			:   Optional[int]=None							,    A			:   Tuple=None							,    **A			:   Optional[int]		):
         super().__init__(*A							,    **A		)
         __snake_case:						List[Any]			  =						eval_examples
         __snake_case:						str			  =						post_process_function




       def 							UpperCAmelCase__			(  self			:   List[Any]							,    A			:   Dict=None							,    A			:   int=None							,    A			:   List[Any]=None							,    A			:   str = "eval"		):
         __snake_case:						int			  =						self.eval_dataset if eval_dataset is None else eval_dataset
         __snake_case:						Any			  =						self.get_eval_dataloader(A		)
         __snake_case:						Optional[Any]			  =						self.eval_examples if eval_examples is None else eval_examples

         # Temporarily disable metric computation, we will do it in the loop here.
         __snake_case:						Union[str, Any]			  =						self.compute_metrics
         __snake_case:						List[str]			  =						None
         __snake_case:						Tuple			  =						self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
         __snake_case:						Tuple			  =						time.time()
         try:
           __snake_case:						Any			  =						eval_loop(
               A							,    description="""Evaluation"""							,    prediction_loss_only=True if compute_metrics is None else None							,    ignore_keys=A							,    metric_key_prefix=A							,    )
         finally:
           __snake_case:						Optional[int]			  =						compute_metrics
         __snake_case:						Union[str, Any]			  =						self.args.eval_batch_size * self.args.world_size
         if f'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
           start_time += output.metrics[f'''{metric_key_prefix}_jit_compilation_time''']
         output.metrics.update(
             speed_metrics(
                 A							,    A							,    num_samples=output.num_samples							,    num_steps=math.ceil(output.num_samples / total_batch_size		)							,    )		)
         if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
           # Only the main node write the results by default
           __snake_case:						List[str]			  =						self.post_process_function(A							,    A							,    output.predictions		)
           __snake_case:						List[Any]			  =						self.compute_metrics(A		)

           # Prefix all keys with metric_key_prefix + '_'
           for key in list(metrics.keys()		):
             if not key.startswith(f'''{metric_key_prefix}_'''		):
               __snake_case:						str			  =						metrics.pop(A		)
           metrics.update(output.metrics		)
         else:
           __snake_case:						List[Any]			  =						output.metrics

         if self.args.should_log:
           # Only the main node log the results by default
           self.log(A		)

         if self.args.tpu_metrics_debug or self.args.debug:
           # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
           xm.master_print(met.metrics_report()		)

         __snake_case:						str			  =						self.callback_handler.on_evaluate(self.args							,    self.state							,    self.control							,    A		)
         return metrics






       def 							UpperCAmelCase__			(  self			:   Optional[Any]							,    A			:   List[Any]							,    A			:   List[str]							,    A			:   str=None							,    A			:   str = "test"		):
         __snake_case:						Optional[Any]			  =						self.get_test_dataloader(A		)

         # Temporarily disable metric computation, we will do it in the loop here.
         __snake_case:						Optional[int]			  =						self.compute_metrics
         __snake_case:						List[Any]			  =						None
         __snake_case:						str			  =						self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
         __snake_case:						Dict			  =						time.time()
         try:
           __snake_case:						str			  =						eval_loop(
               A							,    description="""Prediction"""							,    prediction_loss_only=True if compute_metrics is None else None							,    ignore_keys=A							,    metric_key_prefix=A							,    )
         finally:
           __snake_case:						List[Any]			  =						compute_metrics
         __snake_case:						Dict			  =						self.args.eval_batch_size * self.args.world_size
         if f'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
           start_time += output.metrics[f'''{metric_key_prefix}_jit_compilation_time''']
         output.metrics.update(
             speed_metrics(
                 A							,    A							,    num_samples=output.num_samples							,    num_steps=math.ceil(output.num_samples / total_batch_size		)							,    )		)

         if self.post_process_function is None or self.compute_metrics is None:
           return output

         __snake_case:						Union[str, Any]			  =						self.post_process_function(A							,    A							,    output.predictions							,    """predict"""		)
         __snake_case:						str			  =						self.compute_metrics(A		)

         # Prefix all keys with metric_key_prefix + '_'
         for key in list(metrics.keys()		):
           if not key.startswith(f'''{metric_key_prefix}_'''		):
             __snake_case:						List[str]			  =						metrics.pop(A		)
         metrics.update(output.metrics		)
         return PredictionOutput(predictions=predictions.predictions							,    label_ids=predictions.label_ids							,    metrics=A		)






