
"""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   :   Tuple    ):


  '''simple docstring'''







  lowerCAmelCase					:					Any								=							int(__lowerCAmelCase    )
  lowerCAmelCase						,  lowerCAmelCase						,  lowerCAmelCase					:					str								=							t // 3_6_0_0, (t // 6_0) % 6_0, t % 6_0
  return f"""{h}:{m:02d}:{s:02d}""" if h != 0 else f"""{m:02d}:{s:02d}"""
def  a__      (    SCREAMING_SNAKE_CASE   :   List[Any]    ,				SCREAMING_SNAKE_CASE   :   Dict    ,				SCREAMING_SNAKE_CASE   :   str    ,				SCREAMING_SNAKE_CASE   :   str    ,				SCREAMING_SNAKE_CASE   :   Optional[Any]=3_0_0    ):


  '''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__      (    SCREAMING_SNAKE_CASE   :   Union[str, Any]    ):


  '''simple docstring'''







  lowerCAmelCase					:					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:
      lowerCAmelCase					:					Any								=							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 				SCREAMING_SNAKE_CASE__    :
       """simple docstring"""


       a  :    Optional[int]       =5
       a  :    str       =0.2

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






         """simple docstring"""





         lowerCAmelCase					:					Optional[Any]								=							total
         lowerCAmelCase					:					Optional[int]								=							"" if prefix is None else prefix
         lowerCAmelCase					:					Any								=							leave
         lowerCAmelCase					:					Dict								=							parent
         lowerCAmelCase					:					int								=							width
         lowerCAmelCase					:					int								=							None
         lowerCAmelCase					:					Any								=							None
         lowerCAmelCase					:					Dict								=							None

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






         """simple docstring"""





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

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






         """simple docstring"""





         lowerCAmelCase					:					str								=							" " * (len(str(self.total						)						) - len(str(__snake_case						)						)) + str(__snake_case						)
         if self.elapsed_time is None:
           lowerCAmelCase					:					Tuple								=							f"""[{spaced_value}/{self.total} : < :"""
         elif self.predicted_remaining is None:
           lowerCAmelCase					:					List[Any]								=							f"""[{spaced_value}/{self.total} {format_time(self.elapsed_time						)}"""
         else:
           lowerCAmelCase					:					Union[str, 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 	lowercase__							(						self						):






         """simple docstring"""





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







       def 	lowercase__							(						self						):






         """simple docstring"""





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


class 				SCREAMING_SNAKE_CASE__    (		lowerCamelCase_    ):
       """simple docstring"""


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






         """simple docstring"""





         super().__init__(__snake_case						)
         lowerCAmelCase					:					Tuple								=							None if column_names is None else [column_names]
         lowerCAmelCase					:					List[Any]								=							None

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[int]								=							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					:					int								=							disp.display(disp.HTML(self.html_code						)		,					display_id=__snake_case						)
         else:
           self.output.update(disp.HTML(self.html_code						)						)

       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





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

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






         """simple docstring"""





         lowerCAmelCase					:					Any								=							NotebookProgressBar(__snake_case		,					prefix=__snake_case		,					parent=self		,					width=__snake_case						)
         return self.child_bar







       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Tuple								=							None
         self.display()


class 				SCREAMING_SNAKE_CASE__    (		lowerCamelCase_    ):
       """simple docstring"""


       def __init__(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Dict								=							None
         lowerCAmelCase					:					Tuple								=							None
         lowerCAmelCase					:					List[Any]								=							False

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






         """simple docstring"""





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

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






         """simple docstring"""





         lowerCAmelCase					:					Optional[Any]								=							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					:					int								=							False

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






         """simple docstring"""





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

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






         """simple docstring"""





         if self.prediction_bar is not None:
           self.prediction_bar.close()
         lowerCAmelCase					:					List[str]								=							None

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






         """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(__snake_case						)

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






         """simple docstring"""





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

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







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






         """simple docstring"""





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









"""simple docstring"""







# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING, Dict, Optional

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.logging import get_logger
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
     import jax
     import jaxlib

lowerCAmelCase__				    =       get_logger()

lowerCAmelCase__				    =       None


class 				SCREAMING_SNAKE_CASE__    (		TensorFormatter[Mapping, "jax.Array", Mapping]    ):
       """simple docstring"""


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






         """simple docstring"""





         super().__init__(features=snake_case__						)
         import jax
         from jaxlib.xla_client import Device

         if isinstance(snake_case__		,					snake_case__						):
           raise ValueError(
               f"""Expected {device} to be a `str` not {type(snake_case__						)}, as `jaxlib.xla_extension.Device` """
               "is not serializable neither with `pickle` nor with `dill`. Instead you can surround "
               "the device with `str()` to get its string identifier that will be internally mapped "
               "to the actual `jaxlib.xla_extension.Device`."						)
         lowerCAmelCase					:					List[str]								=							device if isinstance(snake_case__		,					snake_case__						) else str(jax.devices()[0]						)
         # using global variable since `jaxlib.xla_extension.Device` is not serializable neither
         # with `pickle` nor with `dill`, so we need to use a global variable instead
         global DEVICE_MAPPING
         if DEVICE_MAPPING is None:
           lowerCAmelCase					:					Optional[Any]								=							self._map_devices_to_str()
         if self.device not in list(DEVICE_MAPPING.keys()						):
           logger.warning(
               f"""Device with string identifier {self.device} not listed among the available """
               f"""devices: {list(DEVICE_MAPPING.keys()						)}, so falling back to the default """
               f"""device: {str(jax.devices()[0]						)}."""						)
           lowerCAmelCase					:					Dict								=							str(jax.devices()[0]						)
         lowerCAmelCase					:					Union[str, Any]								=							jnp_array_kwargs

       @staticmethod
       def 	lowercase__							(						):






         """simple docstring"""





         import jax

         return {str(snake_case__						): device for device in jax.devices()}

       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





         import jax
         import jax.numpy as jnp

         if isinstance(snake_case__		,					snake_case__						) and column:
           if all(
               isinstance(snake_case__		,					jax.Array						) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column						):
             return jnp.stack(snake_case__		,					axis=0						)
         return column

       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





         import jax
         import jax.numpy as jnp

         if isinstance(snake_case__		,					(str, bytes, type(snake_case__						))						):
           return value
         elif isinstance(snake_case__		,					(np.character, np.ndarray)						) and np.issubdtype(value.dtype		,					np.character						):
           return value.tolist()

         lowerCAmelCase					:					Tuple								=							{}

         if isinstance(snake_case__		,					(np.number, np.ndarray)						) and np.issubdtype(value.dtype		,					np.integer						):
           # the default int precision depends on the jax config
           # see https://jax.readthedocs.io/en/latest/notebooks/Common_Gotchas_in_JAX.html#double-64bit-precision
           if jax.config.jax_enable_xaa:
             lowerCAmelCase					:					List[Any]								=							{"dtype": jnp.intaa}
           else:
             lowerCAmelCase					:					Optional[Any]								=							{"dtype": jnp.intaa}
         elif isinstance(snake_case__		,					(np.number, np.ndarray)						) and np.issubdtype(value.dtype		,					np.floating						):
           lowerCAmelCase					:					int								=							{"dtype": jnp.floataa}
         elif config.PIL_AVAILABLE and "PIL" in sys.modules:
           import PIL.Image

           if isinstance(snake_case__		,					PIL.Image.Image						):
             lowerCAmelCase					:					Dict								=							np.asarray(snake_case__						)

        # using global variable since `jaxlib.xla_extension.Device` is not serializable neither
        # with `pickle` nor with `dill`, so we need to use a global variable instead
         global DEVICE_MAPPING
         if DEVICE_MAPPING is None:
           lowerCAmelCase					:					str								=							self._map_devices_to_str()

         with jax.default_device(DEVICE_MAPPING[self.device]						):
           # calling jnp.array on a np.ndarray does copy the data
           # see https://github.com/google/jax/issues/4486
           return jnp.array(snake_case__		,					**{**default_dtype, **self.jnp_array_kwargs}						)

       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





         import jax

         # support for torch, tf, jax etc.
         if config.TORCH_AVAILABLE and "torch" in sys.modules:
           import torch

           if isinstance(snake_case__		,					torch.Tensor						):
             return self._tensorize(data_struct.detach().cpu().numpy()[()]						)
         if hasattr(snake_case__		,					"__array__"						) and not isinstance(snake_case__		,					jax.Array						):
           lowerCAmelCase					:					Dict								=							data_struct.__array__()
         # support for nested types like struct of list of struct
         if isinstance(snake_case__		,					np.ndarray						):
           if data_struct.dtype == object:  # jax arrays cannot be instantied from an array of objects
             return self._consolidate([self.recursive_tensorize(snake_case__						) for substruct in data_struct]						)
         elif isinstance(snake_case__		,					(list, tuple)						):
           return self._consolidate([self.recursive_tensorize(snake_case__						) for substruct in data_struct]						)
         return self._tensorize(snake_case__						)

       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





         return map_nested(self._recursive_tensorize		,					snake_case__		,					map_list=snake_case__						)

       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					List[Any]								=							self.numpy_arrow_extractor().extract_row(snake_case__						)
         lowerCAmelCase					:					Tuple								=							self.python_features_decoder.decode_row(snake_case__						)
         return self.recursive_tensorize(snake_case__						)

       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[int]								=							self.numpy_arrow_extractor().extract_column(snake_case__						)
         lowerCAmelCase					:					Dict								=							self.python_features_decoder.decode_column(snake_case__		,					pa_table.column_names[0]						)
         lowerCAmelCase					:					Optional[Any]								=							self.recursive_tensorize(snake_case__						)
         lowerCAmelCase					:					Dict								=							self._consolidate(snake_case__						)
         return column







       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[Any]								=							self.numpy_arrow_extractor().extract_batch(snake_case__						)
         lowerCAmelCase					:					str								=							self.python_features_decoder.decode_batch(snake_case__						)
         lowerCAmelCase					:					Tuple								=							self.recursive_tensorize(snake_case__						)
         for column_name in batch:
           lowerCAmelCase					:					Optional[int]								=							self._consolidate(batch[column_name]						)
         return batch





