



'''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 		_snake_case		( _SCREAMING_SNAKE_CASE					:						Optional[int]							)      ->						Dict:







	"""simple docstring"""
	lowerCAmelCase   =     int(_SCREAMING_SNAKE_CASE							)
	lowerCAmelCase,       lowerCAmelCase,       lowerCAmelCase   =     t // 3_600, (t // 60) % 60, t % 60
	return f'{h}:{m:02d}:{s:02d}' if h != 0 else f'{m:02d}:{s:02d}'






def 		_snake_case		( _SCREAMING_SNAKE_CASE					:						Tuple							,		_SCREAMING_SNAKE_CASE					:						int							,		_SCREAMING_SNAKE_CASE					:						Union[str, Any]							,		_SCREAMING_SNAKE_CASE					:						List[Any]							,		_SCREAMING_SNAKE_CASE					:						List[str]=300							)      ->						Dict:







	"""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 		_snake_case		( _SCREAMING_SNAKE_CASE					:						Tuple							)      ->						int:







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


						'''simple docstring'''



						UpperCAmelCase							:   List[Any]									=		5
						UpperCAmelCase							:   List[str]									=		0.2


						def __init__( self     ,	A_     ,	A_ = None     ,	A_ = True     ,	A_ = None     ,	A_ = 300     ,	)     ->     Optional[Any]:
							lowerCAmelCase   =     total
							lowerCAmelCase   =     """""" if prefix is None else prefix
							lowerCAmelCase   =     leave
							lowerCAmelCase   =     parent
							lowerCAmelCase   =     width
							lowerCAmelCase   =     None
							lowerCAmelCase   =     None
							lowerCAmelCase   =     None


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


						def     __snake_case   ( self     ,	A_     ,	A_=None				)     ->     Union[str, Any]:
							lowerCAmelCase   =     """ """ * (len(str(self.total				)				) - len(str(A_				)				)) + str(A_				)
							if self.elapsed_time is None:
								lowerCAmelCase   =     f'[{spaced_value}/{self.total} : < :'
							elif self.predicted_remaining is None:
								lowerCAmelCase   =     f'[{spaced_value}/{self.total} {format_time(self.elapsed_time				)}'
							else:
								lowerCAmelCase   =     (
								    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     __snake_case   ( self				)     ->     Optional[int]:
							lowerCAmelCase   =     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   =     disp.display(disp.HTML(self.html_code				)     ,	display_id=A_				)
							else:
								self.output.update(disp.HTML(self.html_code				)				)




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







class __snake_case(				_lowerCAmelCase ):


						'''simple docstring'''



						def __init__( self     ,	A_     ,	A_=None				)     ->     Any:
							super().__init__(A_				)
							lowerCAmelCase   =     None if column_names is None else [column_names]
							lowerCAmelCase   =     None


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


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


						def     __snake_case   ( self     ,	A_     ,	A_=None     ,	A_=300				)     ->     Any:
							lowerCAmelCase   =     NotebookProgressBar(A_     ,	prefix=A_     ,	parent=self     ,	width=A_				)
							return self.child_bar




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







class __snake_case(				_lowerCAmelCase ):


						'''simple docstring'''



						def __init__( self				)     ->     Dict:
							lowerCAmelCase   =     None
							lowerCAmelCase   =     None
							lowerCAmelCase   =     False


						def     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	**A_				)     ->     List[Any]:
							lowerCAmelCase   =     """Epoch""" if args.evaluation_strategy == IntervalStrategy.EPOCH else """Step"""
							lowerCAmelCase   =     0
							lowerCAmelCase   =     0
							lowerCAmelCase   =     [self.first_column] + ["""Training Loss"""]
							if args.evaluation_strategy != IntervalStrategy.NO:
								column_names.append("""Validation Loss"""				)
							lowerCAmelCase   =     NotebookTrainingTracker(state.max_steps     ,	A_				)


						def     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	**A_				)     ->     List[str]:
							lowerCAmelCase   =     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   =     False


						def     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_=None     ,	**A_				)     ->     Dict:
							if not has_length(A_				):
								return
							if self.prediction_bar is None:
								if self.training_tracker is not None:
									lowerCAmelCase   =     self.training_tracker.add_child(len(A_				)				)
								else:
									lowerCAmelCase   =     NotebookProgressBar(len(A_				)				)
								self.prediction_bar.update(1				)
							else:
								self.prediction_bar.update(self.prediction_bar.value + 1				)


						def     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	**A_				)     ->     Tuple:
							if self.prediction_bar is not None:
								self.prediction_bar.close()
							lowerCAmelCase   =     None


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


						def     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_=None     ,	**A_				)     ->     Dict:
							if self.training_tracker is not None:
								lowerCAmelCase   =     {"""Training Loss""": """No log""", """Validation Loss""": """No log"""}
								for log in reversed(state.log_history				):
									if "loss" in log:
										lowerCAmelCase   =     log["""loss"""]
										break

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




						def     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	**A_				)     ->     int:
							self.training_tracker.update(
							    state.global_step     ,	comment=f'Epoch {int(state.epoch				)}/{state.num_train_epochs}'     ,	force_update=A_				)
							lowerCAmelCase   =     None




'''simple docstring'''

from typing import List, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase     		=   logging.get_logger(__name__)

UpperCAmelCase     		=   {
    'huggingface/time-series-transformer-tourism-monthly': (
        'https://huggingface.co/huggingface/time-series-transformer-tourism-monthly/resolve/main/config.json'
    ),
    # See all TimeSeriesTransformer models at https://huggingface.co/models?filter=time_series_transformer
}

class __snake_case(				_lowerCAmelCase ):


						'''simple docstring'''



						UpperCAmelCase							:   List[Any]									=		"time_series_transformer"
						UpperCAmelCase							:   int									=		{
						    "hidden_size": "d_model",
						    "num_attention_heads": "encoder_attention_heads",
						    "num_hidden_layers": "encoder_layers",
						}


						def __init__( self     ,	A_ = None     ,	A_ = None     ,	A_ = "student_t"     ,	A_ = "nll"     ,	A_ = 1     ,	A_ = [1, 2, 3, 4, 5, 6, 7]     ,	A_ = "mean"     ,	A_ = 0     ,	A_ = 0     ,	A_ = 0     ,	A_ = 0     ,	A_ = None     ,	A_ = None     ,	A_ = 32     ,	A_ = 32     ,	A_ = 2     ,	A_ = 2     ,	A_ = 2     ,	A_ = 2     ,	A_ = True     ,	A_ = "gelu"     ,	A_ = 64     ,	A_ = 0.1     ,	A_ = 0.1     ,	A_ = 0.1     ,	A_ = 0.1     ,	A_ = 0.1     ,	A_ = 100     ,	A_ = 0.0_2     ,	A_=True     ,	**A_     ,	)     ->     Optional[Any]:
							# time series specific configuration
							lowerCAmelCase   =     prediction_length
							lowerCAmelCase   =     context_length or prediction_length
							lowerCAmelCase   =     distribution_output
							lowerCAmelCase   =     loss
							lowerCAmelCase   =     input_size
							lowerCAmelCase   =     num_time_features
							lowerCAmelCase   =     lags_sequence
							lowerCAmelCase   =     scaling
							lowerCAmelCase   =     num_dynamic_real_features
							lowerCAmelCase   =     num_static_real_features
							lowerCAmelCase   =     num_static_categorical_features
							if cardinality and num_static_categorical_features > 0:
								if len(A_				) != num_static_categorical_features:
									raise ValueError(
									    """The cardinality should be a list of the same length as `num_static_categorical_features`"""				)
								lowerCAmelCase   =     cardinality
							else:
								lowerCAmelCase   =     [0]
							if embedding_dimension and num_static_categorical_features > 0:
								if len(A_				) != num_static_categorical_features:
									raise ValueError(
									    """The embedding dimension should be a list of the same length as `num_static_categorical_features`"""				)
								lowerCAmelCase   =     embedding_dimension
							else:
								lowerCAmelCase   =     [min(50     ,	(cat + 1) // 2				) for cat in self.cardinality]
							lowerCAmelCase   =     num_parallel_samples

							# Transformer architecture configuration
							lowerCAmelCase   =     input_size * len(A_				) + self._number_of_features
							lowerCAmelCase   =     d_model
							lowerCAmelCase   =     encoder_attention_heads
							lowerCAmelCase   =     decoder_attention_heads
							lowerCAmelCase   =     encoder_ffn_dim
							lowerCAmelCase   =     decoder_ffn_dim
							lowerCAmelCase   =     encoder_layers
							lowerCAmelCase   =     decoder_layers

							lowerCAmelCase   =     dropout
							lowerCAmelCase   =     attention_dropout
							lowerCAmelCase   =     activation_dropout
							lowerCAmelCase   =     encoder_layerdrop
							lowerCAmelCase   =     decoder_layerdrop

							lowerCAmelCase   =     activation_function
							lowerCAmelCase   =     init_std

							lowerCAmelCase   =     use_cache

							super().__init__(is_encoder_decoder=A_     ,	**A_				)




						@property
						def     __snake_case   ( self				)     ->     int:
							return (
							    sum(self.embedding_dimension				)
							    + self.num_dynamic_real_features
							    + self.num_time_features
							    + self.num_static_real_features
							    + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
							)