


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






			"""simple docstring"""




			__A=	int(_SCREAMING_SNAKE_CASE )
			__A,	__A,	__A=	t // 3600, (t // 60) % 60, t % 60
			return f"""{h}:{m:02d}:{s:02d}""" if h != 0 else f"""{m:02d}:{s:02d}"""






def 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: List[Any],_SCREAMING_SNAKE_CASE					: Tuple,_SCREAMING_SNAKE_CASE					: Optional[Any],_SCREAMING_SNAKE_CASE					: List[str],_SCREAMING_SNAKE_CASE					: Tuple=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 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: int ):






			"""simple docstring"""




			__A=	'<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:
									__A=	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 						a__					:





			'''simple docstring'''

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

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


class 						a__					(					a_   ):





			'''simple docstring'''

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

			def 					lowerCAmelCase     (  self							:			Tuple	)      ->		Optional[int]:
						__A=	None
						self.display()


class 						a__					(					a_   ):





			'''simple docstring'''

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

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

			def 					lowerCAmelCase     (  self							:			Optional[int]			,    lowerCAmelCase_							:			Any			,    lowerCAmelCase_							:			Optional[int]			,    lowerCAmelCase_							:			List[Any]			,    **lowerCAmelCase_							:			Any	)      ->		Union[str, Any]:
						self.training_tracker.update(
						    state.global_step			,    comment=F"""Epoch {int(state.epoch	)}/{state.num_train_epochs}"""			,    force_update=lowerCAmelCase_	)
						__A=	None







'''simple docstring'''





# Lint as: python3
import itertools
import os
import re


UpperCAmelCase__  			=				re.compile(r'''([A-Z]+)([A-Z][a-z])''')
UpperCAmelCase__  			=				re.compile(r'''([a-z\d])([A-Z])''')

UpperCAmelCase__  			=				re.compile(r'''(?<!_)_(?!_)''')
UpperCAmelCase__  			=				re.compile(r'''(_{2,})''')

UpperCAmelCase__  			=				r'''^\w+(\.\w+)*$'''

UpperCAmelCase__  			=				r'''<>:/\|?*'''






def 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: Union[str, Any] ):






			"""simple docstring"""




			__A=	_uppercase_uppercase_re.sub(r'\1_\2',_SCREAMING_SNAKE_CASE )
			__A=	_lowercase_uppercase_re.sub(r'\1_\2',_SCREAMING_SNAKE_CASE )
			return name.lower()






def 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: int ):






			"""simple docstring"""




			__A=	_single_underscore_re.split(_SCREAMING_SNAKE_CASE )
			__A=	[_multiple_underscores_re.split(_SCREAMING_SNAKE_CASE ) for n in name]
			return "".join(n.capitalize() for n in itertools.chain.from_iterable(_SCREAMING_SNAKE_CASE ) if n != '' )






def 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: str ):






			"""simple docstring"""




			if os.path.basename(_SCREAMING_SNAKE_CASE ) != name:
						raise ValueError(f"""Should be a dataset name, not a path: {name}""" )
			return camelcase_to_snakecase(_SCREAMING_SNAKE_CASE )






def 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: Union[str, Any],_SCREAMING_SNAKE_CASE					: str ):






			"""simple docstring"""




			if os.path.basename(_SCREAMING_SNAKE_CASE ) != name:
						raise ValueError(f"""Should be a dataset name, not a path: {name}""" )
			if not re.match(_split_re,_SCREAMING_SNAKE_CASE ):
						raise ValueError(f"""Split name should match '{_split_re}'' but got '{split}'.""" )
			return f"""{filename_prefix_for_name(_SCREAMING_SNAKE_CASE )}-{split}"""






def 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: Union[str, Any],_SCREAMING_SNAKE_CASE					: Optional[Any],_SCREAMING_SNAKE_CASE					: Optional[int],_SCREAMING_SNAKE_CASE					: Union[str, Any]=None ):






			"""simple docstring"""




			__A=	filename_prefix_for_split(_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE )
			if filetype_suffix:
						prefix += f""".{filetype_suffix}"""
			__A=	os.path.join(_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE )
			return f"""{filepath}*"""






def 						UpperCAmelCase__(				_SCREAMING_SNAKE_CASE					: int,_SCREAMING_SNAKE_CASE					: List[str],_SCREAMING_SNAKE_CASE					: int,_SCREAMING_SNAKE_CASE					: List[Any]=None,_SCREAMING_SNAKE_CASE					: List[str]=None ):






			"""simple docstring"""




			__A=	filename_prefix_for_split(_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE )
			__A=	os.path.join(_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE )

			if shard_lengths:
						__A=	len(_SCREAMING_SNAKE_CASE )
						__A=	[f"""{prefix}-{shard_id:05d}-of-{num_shards:05d}""" for shard_id in range(_SCREAMING_SNAKE_CASE )]
						if filetype_suffix:
									__A=	[filename + f""".{filetype_suffix}""" for filename in filenames]
						return filenames
			else:
						__A=	prefix
						if filetype_suffix:
									filename += f""".{filetype_suffix}"""
						return [filename]



