



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      __lowercase       (			a__       )    ->			str:
							__SCREAMING_SNAKE_CASE									= int(a__       )
							__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE									= t // 36_00, (t // 60) % 60, t % 60
							return f"""{h}:{m:02d}:{s:02d}""" if h != 0 else f"""{m:02d}:{s:02d}"""






def      __lowercase       (			a__							,       a__							,       a__							,       a__							,       a__=3_00       )    ->			str:
							# docstyle-ignore
							return f"""
    <div>
      {prefix}
      <progress value='{value}' max='{total}' style='width:{width}px; height:20px; vertical-align: middle;'></progress>
      {label}
    </div>
    """






def      __lowercase       (			a__       )    ->			List[Any]:
							__SCREAMING_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:
																					__SCREAMING_SNAKE_CASE									= f"""{elt:.6f}""" if isinstance(a__							,       a__       ) else str(a__       )
																					html_code += f"""      <td>{elt}</td>\n"""
														html_code += "    </tr>\n"
							html_code += "  </tbody>\n</table><p>"
							return html_code

class        UpperCAmelCase_  :

						'''simple docstring'''




						UpperCamelCase__      :			str      =  5
						UpperCamelCase__      :			Dict      =  0.2






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







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= total
													__SCREAMING_SNAKE_CASE									= '' if prefix is None else prefix
													__SCREAMING_SNAKE_CASE									= leave
													__SCREAMING_SNAKE_CASE									= parent
													__SCREAMING_SNAKE_CASE									= width
													__SCREAMING_SNAKE_CASE									= None
													__SCREAMING_SNAKE_CASE									= None
													__SCREAMING_SNAKE_CASE									= None






						def 	_A     (   self				,							_A				,							_A = False				,							_A = None		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= value
													if comment is not None:
																				__SCREAMING_SNAKE_CASE									= comment
													if self.last_value is None:
																				__SCREAMING_SNAKE_CASE									= __SCREAMING_SNAKE_CASE									= time.time()
																				__SCREAMING_SNAKE_CASE									= __SCREAMING_SNAKE_CASE									= value
																				__SCREAMING_SNAKE_CASE									= __SCREAMING_SNAKE_CASE									= None
																				__SCREAMING_SNAKE_CASE									= self.warmup
																				__SCREAMING_SNAKE_CASE									= 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
																				__SCREAMING_SNAKE_CASE									= time.time()
																				__SCREAMING_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:
																											__SCREAMING_SNAKE_CASE									= self.elapsed_time / (value - self.start_value)
																				else:
																											__SCREAMING_SNAKE_CASE									= None
																				if value >= self.total:
																											__SCREAMING_SNAKE_CASE									= self.total
																											__SCREAMING_SNAKE_CASE									= None
																											if not self.leave:
																																		self.close()
																				elif self.average_time_per_item is not None:
																											__SCREAMING_SNAKE_CASE									= self.average_time_per_item * (self.total - value)
																				self.update_bar(_A		)
																				__SCREAMING_SNAKE_CASE									= value
																				__SCREAMING_SNAKE_CASE									= current_time
																				if self.average_time_per_item is None:
																											__SCREAMING_SNAKE_CASE									= 1
																				else:
																											__SCREAMING_SNAKE_CASE									= max(int(self.update_every / self.average_time_per_item		)				,							1		)






						def 	_A     (   self				,							_A				,							_A=None		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= ' ' * (len(str(self.total		)		) - len(str(_A		)		)) + str(_A		)
													if self.elapsed_time is None:
																				__SCREAMING_SNAKE_CASE									= f"""[{spaced_value}/{self.total} : < :"""
													elif self.predicted_remaining is None:
																				__SCREAMING_SNAKE_CASE									= f"""[{spaced_value}/{self.total} {format_time(self.elapsed_time		)}"""
													else:
																				__SCREAMING_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		):







													'''simple docstring'''

													__SCREAMING_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:
																				__SCREAMING_SNAKE_CASE									= disp.display(disp.HTML(self.html_code		)				,							display_id=_A		)
													else:
																				self.output.update(disp.HTML(self.html_code		)		)






						def 	_A     (   self		):







													'''simple docstring'''

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







class        UpperCAmelCase_  (       UpperCamelCase_	):

						'''simple docstring'''




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







													'''simple docstring'''

													super().__init__(_A		)
													__SCREAMING_SNAKE_CASE									= None if column_names is None else [column_names]
													__SCREAMING_SNAKE_CASE									= None






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_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:
																				__SCREAMING_SNAKE_CASE									= disp.display(disp.HTML(self.html_code		)				,							display_id=_A		)
													else:
																				self.output.update(disp.HTML(self.html_code		)		)






						def 	_A     (   self				,							_A		):







													'''simple docstring'''

													if self.inner_table is None:
																				__SCREAMING_SNAKE_CASE									= [list(values.keys()		), list(values.values()		)]
													else:
																				__SCREAMING_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(_A		)
																											__SCREAMING_SNAKE_CASE									= columns
																				self.inner_table.append([values[c] for c in columns]		)






						def 	_A     (   self				,							_A				,							_A=None				,							_A=300		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= NotebookProgressBar(_A				,							prefix=_A				,							parent=self				,							width=_A		)
													return self.child_bar






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= None
													self.display()







class        UpperCAmelCase_  (       UpperCamelCase_	):

						'''simple docstring'''




						def __init__(   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= None
													__SCREAMING_SNAKE_CASE									= None
													__SCREAMING_SNAKE_CASE									= False






						def 	_A     (   self				,							_A				,							_A				,							_A				,							**_A		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= 'Epoch' if args.evaluation_strategy == IntervalStrategy.EPOCH else 'Step'
													__SCREAMING_SNAKE_CASE									= 0
													__SCREAMING_SNAKE_CASE									= 0
													__SCREAMING_SNAKE_CASE									= [self.first_column] + ['Training Loss']
													if args.evaluation_strategy != IntervalStrategy.NO:
																				column_names.append('Validation Loss'		)
													__SCREAMING_SNAKE_CASE									= NotebookTrainingTracker(state.max_steps				,							_A		)






						def 	_A     (   self				,							_A				,							_A				,							_A				,							**_A		):







													'''simple docstring'''

													__SCREAMING_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				,							)
													__SCREAMING_SNAKE_CASE									= False






						def 	_A     (   self				,							_A				,							_A				,							_A				,							_A=None				,							**_A		):







													'''simple docstring'''

													if not has_length(_A		):
																				return
													if self.prediction_bar is None:
																				if self.training_tracker is not None:
																											__SCREAMING_SNAKE_CASE									= self.training_tracker.add_child(len(_A		)		)
																				else:
																											__SCREAMING_SNAKE_CASE									= NotebookProgressBar(len(_A		)		)
																				self.prediction_bar.update(1		)
													else:
																				self.prediction_bar.update(self.prediction_bar.value + 1		)






						def 	_A     (   self				,							_A				,							_A				,							_A				,							**_A		):







													'''simple docstring'''

													if self.prediction_bar is not None:
																				self.prediction_bar.close()
													__SCREAMING_SNAKE_CASE									= None






						def 	_A     (   self				,							_A				,							_A				,							_A				,							_A=None				,							**_A		):







													'''simple docstring'''

													if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs:
																				__SCREAMING_SNAKE_CASE									= {'Training Loss': logs['loss']}
																				# First column is necessarily Step sine we're not in epoch eval strategy
																				__SCREAMING_SNAKE_CASE									= state.global_step
																				self.training_tracker.write_line(_A		)






						def 	_A     (   self				,							_A				,							_A				,							_A				,							_A=None				,							**_A		):







													'''simple docstring'''

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

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






						def 	_A     (   self				,							_A				,							_A				,							_A				,							**_A		):







													'''simple docstring'''

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











lowerCAmelCase__		:					Optional[int]							=9.80_665






def      __lowercase       (			a__							,       a__							,       a__ = g       )    ->			float:

							if fluid_density <= 0:
														raise ValueError('Impossible fluid density'       )
							if volume < 0:
														raise ValueError('Impossible Object volume'       )
							if gravity <= 0:
														raise ValueError('Impossible Gravity'       )

							return fluid_density * gravity * volume


if __name__ == "__main__":
			import doctest

			# run doctest
			doctest.testmod()






