




"""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					(      A__:     Dict  )	->  Tuple:
			__lowerCamelCase       :						Dict													=					int(lowerCamelCase_  )
			__lowerCamelCase       :						Union[str, Any]													=					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					(      A__:     str				,					A__:     List[Any]				,					A__:     Optional[Any]				,					A__:     Optional[int]				,					A__:     Optional[int]=300  )	->  Optional[int]:
			# 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     UpperCAmelCase					(      A__:     List[Any]  )	->  Optional[int]:
			__lowerCamelCase       :						Dict													=					'''<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 __lowercase:




					'''simple docstring'''


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





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



class __lowercase(   _SCREAMING_SNAKE_CASE					):




					'''simple docstring'''


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





					def      snake_case_					(     self  ):
								__lowerCamelCase       :						List[str]													=					None
								self.display()



class __lowercase(   _SCREAMING_SNAKE_CASE					):




					'''simple docstring'''


					def __init__(     self  ):
								__lowerCamelCase       :						List[str]													=					None
								__lowerCamelCase       :						str													=					None
								__lowerCamelCase       :						Any													=					False
					def      snake_case_					(     self    ,						__a    ,						__a    ,						__a    ,						**__a  ):
								__lowerCamelCase       :						Union[str, Any]													=					'''Epoch''' if args.evaluation_strategy == IntervalStrategy.EPOCH else '''Step'''
								__lowerCamelCase       :						Any													=					0
								__lowerCamelCase       :						List[Any]													=					0
								__lowerCamelCase       :						int													=					[self.first_column] + ['''Training Loss''']
								if args.evaluation_strategy != IntervalStrategy.NO:
											column_names.append('Validation Loss'  )
								__lowerCamelCase       :						Optional[int]													=					NotebookTrainingTracker(state.max_steps    ,						__UpperCAmelCase  )
					def      snake_case_					(     self    ,						__a    ,						__a    ,						__a    ,						**__a  ):
								__lowerCamelCase       :						Union[str, 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       :						List[Any]													=					False
					def      snake_case_					(     self    ,						__a    ,						__a    ,						__a    ,						__a=None    ,						**__a  ):
								if not has_length(__UpperCAmelCase  ):
											return
								if self.prediction_bar is None:
											if self.training_tracker is not None:
														__lowerCamelCase       :						Any													=					self.training_tracker.add_child(len(__UpperCAmelCase  )  )
											else:
														__lowerCamelCase       :						List[Any]													=					NotebookProgressBar(len(__UpperCAmelCase  )  )
											self.prediction_bar.update(1  )
								else:
											self.prediction_bar.update(self.prediction_bar.value + 1  )
					def      snake_case_					(     self    ,						__a    ,						__a    ,						__a    ,						**__a  ):
								if self.prediction_bar is not None:
											self.prediction_bar.close()
								__lowerCamelCase       :						Union[str, Any]													=					None
					def      snake_case_					(     self    ,						__a    ,						__a    ,						__a    ,						__a=None    ,						**__a  ):
								# Only for when there is no evaluation
								if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs:
											__lowerCamelCase       :						Optional[int]													=					{'''Training Loss''': logs['''loss''']}
											# First column is necessarily Step sine we're not in epoch eval strategy
											__lowerCamelCase       :						Optional[int]													=					state.global_step
											self.training_tracker.write_line(__UpperCAmelCase  )
					def      snake_case_					(     self    ,						__a    ,						__a    ,						__a    ,						__a=None    ,						**__a  ):
								if self.training_tracker is not None:
											__lowerCamelCase       :						Optional[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       :						Dict													=					int(state.epoch  )
											else:
														__lowerCamelCase       :						Union[str, Any]													=					state.global_step
											__lowerCamelCase       :						Union[str, Any]													=					'''eval'''
											for k in metrics:
														if k.endswith('_loss'  ):
																	__lowerCamelCase       :						Union[str, Any]													=					re.sub(r'\_loss$'    ,						''    ,						__UpperCAmelCase  )
											__lowerCamelCase       :						List[Any]													=					metrics.pop('total_flos'    ,						__UpperCAmelCase  )
											__lowerCamelCase       :						Optional[Any]													=					metrics.pop('epoch'    ,						__UpperCAmelCase  )
											__lowerCamelCase       :						Optional[Any]													=					metrics.pop(f'''{metric_key_prefix}_runtime'''    ,						__UpperCAmelCase  )
											__lowerCamelCase       :						int													=					metrics.pop(f'''{metric_key_prefix}_samples_per_second'''    ,						__UpperCAmelCase  )
											__lowerCamelCase       :						Tuple													=					metrics.pop(f'''{metric_key_prefix}_steps_per_second'''    ,						__UpperCAmelCase  )
											__lowerCamelCase       :						Dict													=					metrics.pop(f'''{metric_key_prefix}_jit_compilation_time'''    ,						__UpperCAmelCase  )
											for k, v in metrics.items():
														if k == f'''{metric_key_prefix}_loss''':
																	__lowerCamelCase       :						Tuple													=					v
														else:
																	__lowerCamelCase       :						Tuple													=					k.split('_'  )
																	__lowerCamelCase       :						List[Any]													=					''' '''.join([part.capitalize() for part in splits[1:]]  )
																	__lowerCamelCase       :						str													=					v
											self.training_tracker.write_line(__UpperCAmelCase  )
											self.training_tracker.remove_child()
											__lowerCamelCase       :						int													=					None
											# Evaluation takes a long time so we should force the next update.
											__lowerCamelCase       :						Dict													=					True





					def      snake_case_					(     self    ,						__a    ,						__a    ,						__a    ,						**__a  ):
								self.training_tracker.update(
								    state.global_step    ,						comment=f'''Epoch {int(state.epoch  )}/{state.num_train_epochs}'''    ,						force_update=__UpperCAmelCase  )
								__lowerCamelCase       :						Optional[int]													=					None






from typing import Tuple, Union

from ...modeling_outputs import BackboneOutput
from ...modeling_utils import PreTrainedModel
from ...utils import is_timm_available, is_torch_available, requires_backends
from ...utils.backbone_utils import BackboneMixin
from .configuration_timm_backbone import TimmBackboneConfig


if is_timm_available():
		import timm


if is_torch_available():
		from torch import Tensor







class 					__lowercase  (						_SCREAMING_SNAKE_CASE		,       _SCREAMING_SNAKE_CASE				):





							"""simple docstring"""

							UpperCAmelCase_						:		int								=	'''pixel_values'''
							UpperCAmelCase_						:		List[str]								=	False
							UpperCAmelCase_						:		Tuple								=	TimmBackboneConfig




							def __init__(					self    ,						__UpperCAmelCase    ,						**__UpperCAmelCase						)		->       int:
														requires_backends(self    ,						'''timm'''						)
														super().__init__(__UpperCAmelCase						)
														A      :					List[str]      		=					config

														if config.backbone is None:
																					raise ValueError('''backbone is not set in the config. Please set it to a timm model name.'''						)

														if config.backbone not in timm.list_models():
																					raise ValueError(f'backbone {config.backbone} is not supported by timm.'						)

														if hasattr(__UpperCAmelCase    ,						'''out_features'''						) and config.out_features is not None:
																					raise ValueError('''out_features is not supported by TimmBackbone. Please use out_indices instead.'''						)

														A      :					str      		=					getattr(__UpperCAmelCase    ,						'''use_pretrained_backbone'''    ,						__UpperCAmelCase						)
														if pretrained is None:
																					raise ValueError('''use_pretrained_backbone is not set in the config. Please set it to True or False.'''						)

														# We just take the final layer by default. This matches the default for the transformers models.
														A      :					Optional[int]      		=					config.out_indices if getattr(__UpperCAmelCase    ,						'''out_indices'''    ,						__UpperCAmelCase						) is not None else (-1,)

														A      :					str      		=					timm.create_model(
														    config.backbone    ,						pretrained=__UpperCAmelCase    ,						features_only=config.features_only    ,						in_chans=config.num_channels    ,						out_indices=__UpperCAmelCase    ,						**__UpperCAmelCase    ,						)
														# These are used to control the output of the model when called. If output_hidden_states is True, then
														# return_layers is modified to include all layers.
														A      :					str      		=					self._backbone.return_layers
														A      :					Any      		=					{layer['''module''']: str(__UpperCAmelCase						) for i, layer in enumerate(self._backbone.feature_info.info						)}
														super()._init_backbone(__UpperCAmelCase						)




							@classmethod
							def       snake_case			(					cls    ,						__UpperCAmelCase    ,						*__UpperCAmelCase    ,						**__UpperCAmelCase						)		->       Any:
														requires_backends(cls    ,						['''vision''', '''timm''']						)
														from ...models.timm_backbone import TimmBackboneConfig

														A      :					Optional[Any]      		=					kwargs.pop('''config'''    ,						TimmBackboneConfig()						)

														A      :					str      		=					kwargs.pop('''use_timm_backbone'''    ,						__UpperCAmelCase						)
														if not use_timm:
																					raise ValueError('''use_timm_backbone must be True for timm backbones'''						)

														A      :					Optional[int]      		=					kwargs.pop('''num_channels'''    ,						config.num_channels						)
														A      :					List[str]      		=					kwargs.pop('''features_only'''    ,						config.features_only						)
														A      :					Optional[int]      		=					kwargs.pop('''use_pretrained_backbone'''    ,						config.use_pretrained_backbone						)
														A      :					Optional[int]      		=					kwargs.pop('''out_indices'''    ,						config.out_indices						)
														A      :					int      		=					TimmBackboneConfig(
														    backbone=__UpperCAmelCase    ,						num_channels=__UpperCAmelCase    ,						features_only=__UpperCAmelCase    ,						use_pretrained_backbone=__UpperCAmelCase    ,						out_indices=__UpperCAmelCase    ,						)
														return super()._from_config(__UpperCAmelCase    ,						**__UpperCAmelCase						)




							def       snake_case			(					self    ,						__UpperCAmelCase						)		->       Any:
														pass






							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase=None    ,						__UpperCAmelCase=None    ,						__UpperCAmelCase=None    ,						**__UpperCAmelCase						)		->       Union[BackboneOutput, Tuple[Tensor, ...]]:
														A      :					Optional[int]      		=					return_dict if return_dict is not None else self.config.use_return_dict
														A      :					str      		=					(
														    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
														)
														A      :					Any      		=					output_attentions if output_attentions is not None else self.config.output_attentions

														if output_attentions:
																					raise ValueError('''Cannot output attentions for timm backbones at the moment'''						)

														if output_hidden_states:
																					# We modify the return layers to include all the stages of the backbone
																					A      :					Dict      		=					self._all_layers
																					A      :					int      		=					self._backbone(__UpperCAmelCase    ,						**__UpperCAmelCase						)
																					A      :					Any      		=					self._return_layers
																					A      :					Union[str, Any]      		=					tuple(hidden_states[i] for i in self.out_indices						)
														else:
																					A      :					Optional[int]      		=					self._backbone(__UpperCAmelCase    ,						**__UpperCAmelCase						)
																					A      :					List[Any]      		=					None

														A      :					str      		=					tuple(__UpperCAmelCase						)
														A      :					int      		=					tuple(__UpperCAmelCase						) if hidden_states is not None else None

														if not return_dict:
																					A      :					Optional[Any]      		=					(feature_maps,)
																					if output_hidden_states:
																												A      :					Tuple      		=					output + (hidden_states,)
																					return output

														return BackboneOutput(feature_maps=__UpperCAmelCase    ,						hidden_states=__UpperCAmelCase    ,						attentions=__UpperCAmelCase						)






