


'''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		(lowercase__			:   Optional[Any]						)			->			Optional[Any]:






     """simple docstring"""
     __snake_case           =						int(lowercase__						)
     __snake_case      ,       __snake_case      ,       __snake_case           =						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		(lowercase__			:   Optional[int]							,   lowercase__			:   Optional[int]							,   lowercase__			:   Optional[int]							,   lowercase__			:   Tuple							,   lowercase__			:   List[str]=3_0_0						)			->			Optional[int]:






     """simple docstring"""
     # docstyle-ignore
     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		(lowercase__			:   Optional[int]						)			->			Optional[Any]:






     """simple docstring"""
     __snake_case           =						'<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           =						f'{elt:.6f}' if isinstance(lowercase__							,   lowercase__						) else str(lowercase__						)
               html_code += f'      <td>{elt}</td>\n'
          html_code += "    </tr>\n"
     html_code += "  </tbody>\n</table><p>"
     return html_code




class 			_lowercase :
   _SCREAMING_SNAKE_CASE  :  Union[str, Any]          =	5
   _SCREAMING_SNAKE_CASE  :  List[str]          =	0.2
   def __init__(						self       :      Tuple       ,   SCREAMING_SNAKE_CASE_       :      int       ,   SCREAMING_SNAKE_CASE_       :      Optional[str] = None       ,   SCREAMING_SNAKE_CASE_       :      bool = True       ,   SCREAMING_SNAKE_CASE_       :      Optional["NotebookTrainingTracker"] = None       ,   SCREAMING_SNAKE_CASE_       :      int = 300       ,   ) ->							List[Any]:
        __snake_case           =						total
        __snake_case           =						'' if prefix is None else prefix
        __snake_case           =						leave
        __snake_case           =						parent
        __snake_case           =						width
        __snake_case           =						None
        __snake_case           =						None
        __snake_case           =						None
   def a  (						self       :      str       ,   SCREAMING_SNAKE_CASE_       :      int       ,   SCREAMING_SNAKE_CASE_       :      bool = False       ,   SCREAMING_SNAKE_CASE_       :      str = None      ) ->							Any:
        __snake_case           =						value
        if comment is not None:
             __snake_case           =						comment
        if self.last_value is None:
             __snake_case           =						__snake_case           =						time.time()
             __snake_case           =						__snake_case           =						value
             __snake_case           =						__snake_case           =						None
             __snake_case           =						self.warmup
             __snake_case           =						1
             self.update_bar(SCREAMING_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
             __snake_case           =						time.time()
             __snake_case           =						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           =						self.elapsed_time / (value - self.start_value)
             else:
                  __snake_case           =						None
             if value >= self.total:
                  __snake_case           =						self.total
                  __snake_case           =						None
                  if not self.leave:
                       self.close()
             elif self.average_time_per_item is not None:
                  __snake_case           =						self.average_time_per_item * (self.total - value)
             self.update_bar(SCREAMING_SNAKE_CASE_      )
             __snake_case           =						value
             __snake_case           =						current_time
             if self.average_time_per_item is None:
                  __snake_case           =						1
             else:
                  __snake_case           =						max(int(self.update_every / self.average_time_per_item      )       ,   1      )
   def a  (						self       :      List[Any]       ,   SCREAMING_SNAKE_CASE_       :      List[str]       ,   SCREAMING_SNAKE_CASE_       :      List[str]=None      ) ->							str:
        __snake_case           =						' ' * (len(str(self.total      )      ) - len(str(SCREAMING_SNAKE_CASE_      )      )) + str(SCREAMING_SNAKE_CASE_      )
        if self.elapsed_time is None:
             __snake_case           =						f'[{spaced_value}/{self.total} : < :'
        elif self.predicted_remaining is None:
             __snake_case           =						f'[{spaced_value}/{self.total} {format_time(self.elapsed_time      )}'
        else:
             __snake_case           =						(
                 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 a  (						self       :      Optional[int]      ) ->							Any:
        __snake_case           =						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           =						disp.display(disp.HTML(self.html_code      )       ,   display_id=SCREAMING_SNAKE_CASE_      )
        else:
             self.output.update(disp.HTML(self.html_code      )      )





   def a  (						self       :      Dict      ) ->							Union[str, Any]:
        if self.parent is None and self.output is not None:
             self.output.update(disp.HTML(''      )      )




class 			_lowercase (	__lowercase							):
   def __init__(						self       :      Optional[int]       ,   SCREAMING_SNAKE_CASE_       :      Union[str, Any]       ,   SCREAMING_SNAKE_CASE_       :      List[Any]=None      ) ->							Union[str, Any]:
        super().__init__(SCREAMING_SNAKE_CASE_      )
        __snake_case           =						None if column_names is None else [column_names]
        __snake_case           =						None
   def a  (						self       :      Dict      ) ->							Optional[Any]:
        __snake_case           =						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           =						disp.display(disp.HTML(self.html_code      )       ,   display_id=SCREAMING_SNAKE_CASE_      )
        else:
             self.output.update(disp.HTML(self.html_code      )      )
   def a  (						self       :      Optional[Any]       ,   SCREAMING_SNAKE_CASE_       :      Optional[int]      ) ->							Optional[Any]:
        if self.inner_table is None:
             __snake_case           =						[list(values.keys()      ), list(values.values()      )]
        else:
             __snake_case           =						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(SCREAMING_SNAKE_CASE_      )
                  __snake_case           =						columns
             self.inner_table.append([values[c] for c in columns]      )
   def a  (						self       :      List[Any]       ,   SCREAMING_SNAKE_CASE_       :      Tuple       ,   SCREAMING_SNAKE_CASE_       :      int=None       ,   SCREAMING_SNAKE_CASE_       :      Optional[int]=300      ) ->							Any:
        __snake_case           =						NotebookProgressBar(SCREAMING_SNAKE_CASE_       ,   prefix=SCREAMING_SNAKE_CASE_       ,   parent=self       ,   width=SCREAMING_SNAKE_CASE_      )
        return self.child_bar





   def a  (						self       :      Optional[int]      ) ->							Any:
        __snake_case           =						None
        self.display()




class 			_lowercase (	__lowercase							):
   def __init__(						self       :      Any      ) ->							List[Any]:
        __snake_case           =						None
        __snake_case           =						None
        __snake_case           =						False
   def a  (						self       :      Optional[Any]       ,   SCREAMING_SNAKE_CASE_       :      Any       ,   SCREAMING_SNAKE_CASE_       :      Tuple       ,   SCREAMING_SNAKE_CASE_       :      Optional[Any]       ,   **SCREAMING_SNAKE_CASE_       :      Any      ) ->							Union[str, Any]:
        __snake_case           =						'Epoch' if args.evaluation_strategy == IntervalStrategy.EPOCH else 'Step'
        __snake_case           =						0
        __snake_case           =						0
        __snake_case           =						[self.first_column] + ['Training Loss']
        if args.evaluation_strategy != IntervalStrategy.NO:
             column_names.append('Validation Loss'      )
        __snake_case           =						NotebookTrainingTracker(state.max_steps       ,   SCREAMING_SNAKE_CASE_      )
   def a  (						self       :      Union[str, Any]       ,   SCREAMING_SNAKE_CASE_       :      Union[str, Any]       ,   SCREAMING_SNAKE_CASE_       :      Union[str, Any]       ,   SCREAMING_SNAKE_CASE_       :      Union[str, Any]       ,   **SCREAMING_SNAKE_CASE_       :      Tuple      ) ->							Any:
        __snake_case           =						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           =						False
   def a  (						self       :      List[Any]       ,   SCREAMING_SNAKE_CASE_       :      Union[str, Any]       ,   SCREAMING_SNAKE_CASE_       :      Optional[Any]       ,   SCREAMING_SNAKE_CASE_       :      Optional[int]       ,   SCREAMING_SNAKE_CASE_       :      Optional[int]=None       ,   **SCREAMING_SNAKE_CASE_       :      Tuple      ) ->							Tuple:
        if not has_length(SCREAMING_SNAKE_CASE_      ):
             return
        if self.prediction_bar is None:
             if self.training_tracker is not None:
                  __snake_case           =						self.training_tracker.add_child(len(SCREAMING_SNAKE_CASE_      )      )
             else:
                  __snake_case           =						NotebookProgressBar(len(SCREAMING_SNAKE_CASE_      )      )
             self.prediction_bar.update(1      )
        else:
             self.prediction_bar.update(self.prediction_bar.value + 1      )
   def a  (						self       :      List[str]       ,   SCREAMING_SNAKE_CASE_       :      Union[str, Any]       ,   SCREAMING_SNAKE_CASE_       :      List[str]       ,   SCREAMING_SNAKE_CASE_       :      Dict       ,   **SCREAMING_SNAKE_CASE_       :      List[str]      ) ->							Optional[int]:
        if self.prediction_bar is not None:
             self.prediction_bar.close()
        __snake_case           =						None
   def a  (						self       :      Any       ,   SCREAMING_SNAKE_CASE_       :      List[Any]       ,   SCREAMING_SNAKE_CASE_       :      List[str]       ,   SCREAMING_SNAKE_CASE_       :      str       ,   SCREAMING_SNAKE_CASE_       :      List[str]=None       ,   **SCREAMING_SNAKE_CASE_       :      List[str]      ) ->							Optional[int]:
        # Only for when there is no evaluation
        if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs:
             __snake_case           =						{'Training Loss': logs['loss']}
             # First column is necessarily Step sine we're not in epoch eval strategy
             __snake_case           =						state.global_step
             self.training_tracker.write_line(SCREAMING_SNAKE_CASE_      )
   def a  (						self       :      Dict       ,   SCREAMING_SNAKE_CASE_       :      List[str]       ,   SCREAMING_SNAKE_CASE_       :      Tuple       ,   SCREAMING_SNAKE_CASE_       :      str       ,   SCREAMING_SNAKE_CASE_       :      Dict=None       ,   **SCREAMING_SNAKE_CASE_       :      List[str]      ) ->							List[Any]:
        if self.training_tracker is not None:
             __snake_case           =						{'Training Loss': 'No log', 'Validation Loss': 'No log'}
             for log in reversed(state.log_history      ):
                  if "loss" in log:
                       __snake_case           =						log['loss']
                       break

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





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












'''simple docstring'''





from typing import List, Optional, Union

import numpy as np
import tensorflow as tf

from .utils import logging


_UpperCamelCase						:						int  =					logging.get_logger(__name__)
def __UpperCAmelCase   (					A					:			Union[tf.Tensor, np.ndarray]		)    ->   List[int]:
			if isinstance(A			,       np.ndarray		):
						return list(tensor.shape		)

			UpperCAmelCase_      :       str          =       tf.shape(A		)

			if tensor.shape == tf.TensorShape(A		):
						return dynamic

			UpperCAmelCase_      :       Any          =       tensor.shape.as_list()

			return [dynamic[i] if s is None else s for i, s in enumerate(A		)]
def __UpperCAmelCase   (					A					:			tf.Tensor			,       A					:			Optional[int] = None			,       A					:			Optional[str] = None		)    ->   tf.Tensor:
			return tf.nn.softmax(logits=logits + 1e-9			,       axis=A			,       name=A		)
def __UpperCAmelCase   (					A					:			List[str]			,       A					:			Union[str, Any]			,       A					:			List[str]			,       A					:			str=1e-5			,       A					:			Dict=-1		)    ->   List[Any]:
			# This is a very simplified functional layernorm, designed to duplicate
			# the functionality of PyTorch nn.functional.layer_norm when this is needed to port
			# models in Transformers.

			if weight.shape.rank != 1 or bias.shape.rank != 1 or not isinstance(A			,       A		):
						raise NotImplementedError('''Only 1D weight and bias tensors are supported for now, with only a single axis.'''		)

			# Get mean and variance on the axis to be normalized
			UpperCAmelCase_    ,      UpperCAmelCase_      :       int          =       tf.nn.moments(A			,       axes=[axis]			,       keepdims=A		)

			if axis != -1:
						# Reshape scale and weight to have the same rank as inputs, but with 1 dimensions
						# on every dimension except axis
						UpperCAmelCase_      :       Tuple          =       [1] * inputs.shape.rank
						UpperCAmelCase_      :       int          =       shape_list(A		)[axis]
						UpperCAmelCase_      :       Dict          =       tf.reshape(A			,       A		)
						UpperCAmelCase_      :       Optional[Any]          =       tf.reshape(A			,       A		)

			# Compute layer normalization using the batch_normalization
			# function.
			UpperCAmelCase_      :       Dict          =       tf.nn.batch_normalization(
			    A			,       A			,       A			,       offset=A			,       scale=A			,       variance_epsilon=A			,       )
			return outputs
def __UpperCAmelCase   (					A					:			Dict			,       A					:			int=0			,       A					:			Optional[int]=-1		)    ->   str:
			# Replicates the behavior of torch.flatten in TF

			# If end_dim or start_dim is negative, count them from the end
			if end_dim < 0:
						end_dim += input.shape.rank
			if start_dim < 0:
						start_dim += input.shape.rank

			if start_dim == end_dim:
						return input

			UpperCAmelCase_      :       Any          =       tf.shape(A		)
			UpperCAmelCase_      :       Dict          =       tf.math.reduce_prod(in_shape[start_dim : end_dim + 1]		)
			UpperCAmelCase_      :       Optional[int]          =       tf.concat([in_shape[:start_dim], [flattened_dim], in_shape[end_dim + 1 :]]			,       axis=0		)
			return tf.reshape(A			,       A		)
def __UpperCAmelCase   (					A					:			tf.Tensor		)    ->   tf.Tensor:
			if not isinstance(A			,       tf.Tensor		):
						UpperCAmelCase_      :       Optional[Any]          =       tf.convert_to_tensor(A		)  # Catches stray NumPy inputs
			if encoder_attention_mask.shape.rank == 3:
						UpperCAmelCase_      :       Any          =       encoder_attention_mask[:, None, :, :]
			if encoder_attention_mask.shape.rank == 2:
						UpperCAmelCase_      :       Tuple          =       encoder_attention_mask[:, None, None, :]
			# T5 has a mask that can compare sequence ids, we can simulate this here with this transposition
			# Cf. https://github.com/tensorflow/mesh/blob/8d2465e9bc93129b913b5ccc6a59aa97abd96ec6/mesh_tensorflow
			# /transformer/transformer_layers.py#L270
			# encoder_extended_attention_mask = (encoder_extended_attention_mask ==
			# encoder_extended_attention_mask.transpose(-1, -2))
			UpperCAmelCase_      :       int          =       (
			    tf.cast(1			,       encoder_attention_mask.dtype		) - encoder_extended_attention_mask
			) * encoder_extended_attention_mask.dtype.min

			return encoder_extended_attention_mask
def __UpperCAmelCase   (					A					:			tf.Tensor			,       A					:			int			,       A					:			str = "input_ids"		)    ->   None:
			tf.debugging.assert_less(
			    A			,       tf.cast(A			,       dtype=tensor.dtype		)			,       message=(
			        F"The maximum value of {tensor_name} ({tf.math.reduce_max(A		)}) must be smaller than the embedding "
			        F"layer's input dimension ({embed_dim}). The likely cause is some problem at tokenization time."
			    )			,       )
def __UpperCAmelCase   (					A					:			Dict			,       A					:			Tuple			,       A					:			List[str]		)    ->   Union[str, Any]:
			UpperCAmelCase_      :       List[Any]          =       6_4_5_1_2
			# Check that no item in `data` is larger than `HDF5_OBJECT_HEADER_LIMIT`
			# because in that case even chunking the array would not make the saving
			# possible.
			UpperCAmelCase_      :       Tuple          =       [x for x in data if len(A		) > HDF5_OBJECT_HEADER_LIMIT]

			# Expecting this to never be true.
			if bad_attributes:
						raise RuntimeError(
						    '''The following attributes cannot be saved to HDF5 file because '''
						    F"they are larger than {HDF5_OBJECT_HEADER_LIMIT} "
						    F"bytes: {bad_attributes}"		)

			UpperCAmelCase_      :       Union[str, Any]          =       np.asarray(A		)

			UpperCAmelCase_      :       Optional[int]          =       1
			UpperCAmelCase_      :       str          =       np.array_split(A			,       A		)

			# This will never loop forever thanks to the test above.
			while any(x.nbytes > HDF5_OBJECT_HEADER_LIMIT for x in chunked_data		):
						num_chunks += 1
						UpperCAmelCase_      :       Dict          =       np.array_split(A			,       A		)

			if num_chunks > 1:
						for chunk_id, chunk_data in enumerate(A		):
									UpperCAmelCase_      :       int          =       chunk_data
			else:
						UpperCAmelCase_      :       List[Any]          =       data
def __UpperCAmelCase   (					A					:			int			,       A					:			Optional[Any]		)    ->   Tuple:
			if name in group.attrs:
						UpperCAmelCase_      :       Optional[int]          =       [n.decode('''utf8'''		) if hasattr(A			,       '''decode'''		) else n for n in group.attrs[name]]
			else:
						UpperCAmelCase_      :       Optional[int]          =       []
						UpperCAmelCase_      :       Optional[int]          =       0
						while "%s%d" % (name, chunk_id) in group.attrs:
									data.extend(
									    [n.decode('''utf8'''		) if hasattr(A			,       '''decode'''		) else n for n in group.attrs['''%s%d''' % (name, chunk_id)]]		)
									chunk_id += 1
			return data







def __UpperCAmelCase   (					A					:			Tuple		)    ->   str:

			def _expand_single_ad_tensor(A					:			int		):
						if isinstance(A			,       tf.Tensor		) and t.shape.rank == 1:
									return tf.expand_dims(A			,       axis=-1		)
						return t

			return tf.nest.map_structure(_expand_single_ad_tensor			,       A		)


