



"""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 						__snake_case						(				SCREAMING_SNAKE_CASE__     :						Optional[Any]    )  ->       Optional[int]:





     '''simple docstring'''

     _UpperCAmelCase      :     List[str]        =   int(SCREAMING_SNAKE_CASE__    )
     _UpperCAmelCase    ,	_UpperCAmelCase    ,	_UpperCAmelCase      :     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 						__snake_case						(				SCREAMING_SNAKE_CASE__     :						Dict						,  SCREAMING_SNAKE_CASE__     :						Tuple						,  SCREAMING_SNAKE_CASE__     :						Dict						,  SCREAMING_SNAKE_CASE__     :						Any						,  SCREAMING_SNAKE_CASE__     :						List[Any]=300    )  ->       Any:





     '''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 						__snake_case						(				SCREAMING_SNAKE_CASE__     :						Any    )  ->       Tuple:





     '''simple docstring'''

     _UpperCAmelCase      :     Any        =   "<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      :     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      UpperCAmelCase_  :
      __SCREAMING_SNAKE_CASE						:       int								=			5
      __SCREAMING_SNAKE_CASE						:       Union[str, Any]								=			0.2



      def __init__(					self					:     Optional[Any]					,     A					:     int					,     A					:     Optional[str] = None					,     A					:     bool = True					,     A					:     Optional["NotebookTrainingTracker"] = None					,     A					:     int = 3_0_0					,     ):
           _UpperCAmelCase      :     List[str]        =   total
           _UpperCAmelCase      :     Union[str, Any]        =   "" if prefix is None else prefix
           _UpperCAmelCase      :     int        =   leave
           _UpperCAmelCase      :     str        =   parent
           _UpperCAmelCase      :     Tuple        =   width
           _UpperCAmelCase      :     Tuple        =   None
           _UpperCAmelCase      :     Optional[int]        =   None
           _UpperCAmelCase      :     Tuple        =   None



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



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



      def 						snake_case_ (					self					:     Optional[int]						):
           if self.parent is None and self.output is not None:
                self.output.update(disp.HTML(""						)						)







class      UpperCAmelCase_  (    _UpperCamelCase						):



      def __init__(					self					:     Union[str, Any]					,     A					:     Any					,     A					:     Optional[int]=None						):
           super().__init__(A						)
           _UpperCAmelCase      :     Tuple        =   None if column_names is None else [column_names]
           _UpperCAmelCase      :     Optional[Any]        =   None



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



      def 						snake_case_ (					self					:     List[str]					,     A					:     Tuple						):
           if self.inner_table is None:
                _UpperCAmelCase      :     Any        =   [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(A						)
                     _UpperCAmelCase      :     Optional[Any]        =   columns
                self.inner_table.append([values[c] for c in columns]						)



      def 						snake_case_ (					self					:     List[str]					,     A					:     int					,     A					:     Any=None					,     A					:     Dict=3_0_0						):
           _UpperCAmelCase      :     str        =   NotebookProgressBar(A					,     prefix=A					,     parent=self					,     width=A						)
           return self.child_bar



      def 						snake_case_ (					self					:     Union[str, Any]						):
           _UpperCAmelCase      :     Any        =   None
           self.display()







class      UpperCAmelCase_  (    _UpperCamelCase						):



      def __init__(					self					:     List[str]						):
           _UpperCAmelCase      :     Optional[Any]        =   None
           _UpperCAmelCase      :     Tuple        =   None
           _UpperCAmelCase      :     Any        =   False



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



      def 						snake_case_ (					self					:     int					,     A					:     Any					,     A					:     List[str]					,     A					:     Dict					,     **A					:     Optional[Any]						):
           _UpperCAmelCase      :     Optional[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					,     )
           _UpperCAmelCase      :     List[str]        =   False



      def 						snake_case_ (					self					:     Optional[int]					,     A					:     Dict					,     A					:     str					,     A					:     Optional[Any]					,     A					:     Dict=None					,     **A					:     Optional[Any]						):
           if not has_length(A						):
                return
           if self.prediction_bar is None:
                if self.training_tracker is not None:
                     _UpperCAmelCase      :     int        =   self.training_tracker.add_child(len(A						)						)
                else:
                     _UpperCAmelCase      :     Tuple        =   NotebookProgressBar(len(A						)						)
                self.prediction_bar.update(1						)
           else:
                self.prediction_bar.update(self.prediction_bar.value + 1						)



      def 						snake_case_ (					self					:     Optional[Any]					,     A					:     Optional[Any]					,     A					:     Dict					,     A					:     Any					,     **A					:     Union[str, Any]						):
           if self.prediction_bar is not None:
                self.prediction_bar.close()
           _UpperCAmelCase      :     Optional[int]        =   None



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



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

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



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





"""simple docstring"""




import io
import json
import unittest

from parameterized import parameterized

from transformers import FSMTForConditionalGeneration, FSMTTokenizer
from transformers.testing_utils import get_tests_dir, require_torch, slow, torch_device
from utils import calculate_bleu


_lowerCAmelCase    : Union[str, Any]							=							get_tests_dir() + "/test_data/fsmt/fsmt_val_data.json"
with io.open(filename, "r", encoding="utf-8") as f:
 _lowerCAmelCase    : Tuple							=							json.load(f)






@require_torch
class      UpperCAmelCase_  (    unittest.TestCase						):



      def 						snake_case_ (					self					:     str					,     A					:     Union[str, Any]						):
           return FSMTTokenizer.from_pretrained(A						)



      def 						snake_case_ (					self					:     Union[str, Any]					,     A					:     Union[str, Any]						):
           _UpperCAmelCase      :     List[Any]        =   FSMTForConditionalGeneration.from_pretrained(A						).to(A						)
           if torch_device == "cuda":
                model.half()
           return model



      @parameterized.expand(
          [
              ["en-ru", 26.0],
              ["ru-en", 22.0],
              ["en-de", 22.0],
              ["de-en", 29.0],
          ]						)
      @slow
      def 						snake_case_ (					self					:     Any					,     A					:     Dict					,     A					:     List[str]						):
           # note: this test is not testing the best performance since it only evals a small batch
           # but it should be enough to detect a regression in the output quality
           _UpperCAmelCase      :     Any        =   f'facebook/wmt19-{pair}'
           _UpperCAmelCase      :     Dict        =   self.get_tokenizer(A						)
           _UpperCAmelCase      :     Optional[int]        =   self.get_model(A						)

           _UpperCAmelCase      :     int        =   bleu_data[pair]["src"]
           _UpperCAmelCase      :     Optional[int]        =   bleu_data[pair]["tgt"]

           _UpperCAmelCase      :     List[str]        =   tokenizer(A					,     return_tensors="pt"					,     truncation=A					,     padding="longest"						).to(A						)
           _UpperCAmelCase      :     List[str]        =   model.generate(
               input_ids=batch.input_ids					,     num_beams=8					,     )
           _UpperCAmelCase      :     Any        =   tokenizer.batch_decode(
               A					,     skip_special_tokens=A					,     clean_up_tokenization_spaces=A						)
           _UpperCAmelCase      :     Any        =   calculate_bleu(A					,     A						)
           print(A						)
           self.assertGreaterEqual(scores["bleu"]					,     A						)
