





'''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 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  _UpperCamelCase       			=int(__SCREAMING_SNAKE_CASE   )
  _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase       			=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   ):


  """simple docstring"""

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


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  _UpperCamelCase       			='''<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       			=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   :


  """simple docstring"""







  lowerCAmelCase_					=				5
  lowerCAmelCase_					=				0.2


  def __init__(				self    :					str							,	UpperCamelCase__    :					int							,	UpperCamelCase__    :					Optional[str] = None							,	UpperCamelCase__    :					bool = True							,	UpperCamelCase__    :					Optional["NotebookTrainingTracker"] = None							,	UpperCamelCase__    :					int = 300							,	)     ->						Tuple:
    _UpperCamelCase       			=total
    _UpperCamelCase       			='''''' if prefix is None else prefix
    _UpperCamelCase       			=leave
    _UpperCamelCase       			=parent
    _UpperCamelCase       			=width
    _UpperCamelCase       			=None
    _UpperCamelCase       			=None
    _UpperCamelCase       			=None


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


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




  def   UpperCamelCase__      (				self    :					Optional[Any]  )     ->						str:
    if self.parent is None and self.output is not None:
      self.output.update(disp.HTML(''''''  )  )







class 		UpperCAmelCase   (				lowercase_):


  """simple docstring"""







  def __init__(				self    :					str							,	UpperCamelCase__    :					Optional[Any]							,	UpperCamelCase__    :					str=None  )     ->						Dict:
    super().__init__(UpperCamelCase__  )
    _UpperCamelCase       			=None if column_names is None else [column_names]
    _UpperCamelCase       			=None


  def   UpperCamelCase__      (				self    :					Any  )     ->						Dict:
    _UpperCamelCase       			=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       			=disp.display(disp.HTML(self.html_code  )							,	display_id=UpperCamelCase__  )
    else:
      self.output.update(disp.HTML(self.html_code  )  )


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


  def   UpperCamelCase__      (				self    :					List[str]							,	UpperCamelCase__    :					Dict							,	UpperCamelCase__    :					Any=None							,	UpperCamelCase__    :					Dict=300  )     ->						str:
    _UpperCamelCase       			=NotebookProgressBar(UpperCamelCase__							,	prefix=UpperCamelCase__							,	parent=self							,	width=UpperCamelCase__  )
    return self.child_bar




  def   UpperCamelCase__      (				self    :					str  )     ->						Dict:
    _UpperCamelCase       			=None
    self.display()







class 		UpperCAmelCase   (				lowercase_):


  """simple docstring"""







  def __init__(				self    :					Tuple  )     ->						Tuple:
    _UpperCamelCase       			=None
    _UpperCamelCase       			=None
    _UpperCamelCase       			=False


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


  def   UpperCamelCase__      (				self    :					int							,	UpperCamelCase__    :					Tuple							,	UpperCamelCase__    :					int							,	UpperCamelCase__    :					Union[str, Any]							,	**UpperCamelCase__    :					str  )     ->						Tuple:
    _UpperCamelCase       			=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       			=False


  def   UpperCamelCase__      (				self    :					str							,	UpperCamelCase__    :					Any							,	UpperCamelCase__    :					Optional[int]							,	UpperCamelCase__    :					Optional[Any]							,	UpperCamelCase__    :					Union[str, Any]=None							,	**UpperCamelCase__    :					Dict  )     ->						Dict:
    if not has_length(UpperCamelCase__  ):
      return
    if self.prediction_bar is None:
      if self.training_tracker is not None:
        _UpperCamelCase       			=self.training_tracker.add_child(len(UpperCamelCase__  )  )
      else:
        _UpperCamelCase       			=NotebookProgressBar(len(UpperCamelCase__  )  )
      self.prediction_bar.update(1  )
    else:
      self.prediction_bar.update(self.prediction_bar.value + 1  )


  def   UpperCamelCase__      (				self    :					str							,	UpperCamelCase__    :					Dict							,	UpperCamelCase__    :					Any							,	UpperCamelCase__    :					Optional[Any]							,	**UpperCamelCase__    :					List[Any]  )     ->						Any:
    if self.prediction_bar is not None:
      self.prediction_bar.close()
    _UpperCamelCase       			=None


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


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

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




  def   UpperCamelCase__      (				self    :					List[Any]							,	UpperCamelCase__    :					Optional[int]							,	UpperCamelCase__    :					List[Any]							,	UpperCamelCase__    :					List[Any]							,	**UpperCamelCase__    :					Dict  )     ->						Optional[Any]:
    self.training_tracker.update(
        state.global_step							,	comment=F'''Epoch {int(state.epoch  )}/{state.num_train_epochs}'''							,	force_update=UpperCamelCase__  )
    _UpperCamelCase       			=None













'''simple docstring'''






# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__lowerCamelCase						: Optional[int]		=   {
    'configuration_xmod': [
        'XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'XmodConfig',
        'XmodOnnxConfig',
    ],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __lowerCamelCase						: int		=   [
          'XMOD_PRETRAINED_MODEL_ARCHIVE_LIST',
          'XmodForCausalLM',
          'XmodForMaskedLM',
          'XmodForMultipleChoice',
          'XmodForQuestionAnswering',
          'XmodForSequenceClassification',
          'XmodForTokenClassification',
          'XmodModel',
          'XmodPreTrainedModel',
      ]

if TYPE_CHECKING:
      from .configuration_xmod import XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP, XmodConfig, XmodOnnxConfig

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_xmod import (
                XMOD_PRETRAINED_MODEL_ARCHIVE_LIST,
                XmodForCausalLM,
                XmodForMaskedLM,
                XmodForMultipleChoice,
                XmodForQuestionAnswering,
                XmodForSequenceClassification,
                XmodForTokenClassification,
                XmodModel,
                XmodPreTrainedModel,
            )

else:
      import sys

      __lowerCamelCase						: Dict		=   _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)






