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  _lowerCamelCase					(   a_				: Any):
			lowerCamelCase						:Any						  =     int(a_)
			lowerCamelCase  ,    lowerCamelCase  ,    lowerCamelCase						:Optional[int]						  =     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  _lowerCamelCase					(   a_				: Any						,			a_				: Union[str, Any]						,			a_				: Dict						,			a_				: List[str]						,			a_				: List[str]=3_00):
			# 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  _lowerCamelCase					(   a_				: int):
			lowerCamelCase						:int						  =     '''<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						:List[str]						  =     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   _lowerCAmelCase		:
							_UpperCAmelCase						    =     5
							_UpperCAmelCase						    =     0.2







							def __init__(				self     :    Union[str, Any]	,					__snake_case     :    int	,					__snake_case     :    Optional[str] = None	,					__snake_case     :    bool = True	,					__snake_case     :    Optional["NotebookTrainingTracker"] = None	,					__snake_case     :    int = 300	,					):
										lowerCamelCase						:Dict						  =     total
										lowerCamelCase						:Tuple						  =     '''''' if prefix is None else prefix
										lowerCamelCase						:Tuple						  =     leave
										lowerCamelCase						:List[Any]						  =     parent
										lowerCamelCase						:int						  =     width
										lowerCamelCase						:Optional[Any]						  =     None
										lowerCamelCase						:str						  =     None
										lowerCamelCase						:List[Any]						  =     None







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







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







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


class   _lowerCAmelCase		(     __SCREAMING_SNAKE_CASE				):







							def __init__(				self     :    int	,					__snake_case     :    Optional[int]	,					__snake_case     :    List[Any]=None		):
										super().__init__(__snake_case		)
										lowerCamelCase						:List[Any]						  =     None if column_names is None else [column_names]
										lowerCamelCase						:Optional[int]						  =     None







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







							def 				snake_case  (				self     :    Union[str, Any]	,					__snake_case     :    Tuple		):
										if self.inner_table is None:
													lowerCamelCase						:Union[str, Any]						  =     [list(values.keys()		), list(values.values()		)]
										else:
													lowerCamelCase						:Optional[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(__snake_case		)
																lowerCamelCase						:List[Any]						  =     columns
													self.inner_table.append([values[c] for c in columns]		)







							def 				snake_case  (				self     :    Union[str, Any]	,					__snake_case     :    List[str]	,					__snake_case     :    List[Any]=None	,					__snake_case     :    Tuple=300		):
										lowerCamelCase						:Any						  =     NotebookProgressBar(__snake_case	,					prefix=__snake_case	,					parent=self	,					width=__snake_case		)
										return self.child_bar







							def 				snake_case  (				self     :    Optional[Any]		):
										lowerCamelCase						:int						  =     None
										self.display()


class   _lowerCAmelCase		(     __SCREAMING_SNAKE_CASE				):







							def __init__(				self     :    Any		):
										lowerCamelCase						:Optional[Any]						  =     None
										lowerCamelCase						:int						  =     None
										lowerCamelCase						:Optional[int]						  =     False







							def 				snake_case  (				self     :    Any	,					__snake_case     :    Tuple	,					__snake_case     :    Optional[int]	,					__snake_case     :    Any	,					**__snake_case     :    Optional[int]		):
										lowerCamelCase						:int						  =     '''Epoch''' if args.evaluation_strategy == IntervalStrategy.EPOCH else '''Step'''
										lowerCamelCase						:List[Any]						  =     0
										lowerCamelCase						:List[str]						  =     0
										lowerCamelCase						:Tuple						  =     [self.first_column] + ['''Training Loss''']
										if args.evaluation_strategy != IntervalStrategy.NO:
													column_names.append('''Validation Loss'''		)
										lowerCamelCase						:Optional[Any]						  =     NotebookTrainingTracker(state.max_steps	,					__snake_case		)







							def 				snake_case  (				self     :    Optional[Any]	,					__snake_case     :    Dict	,					__snake_case     :    Union[str, Any]	,					__snake_case     :    Dict	,					**__snake_case     :    Any		):
										lowerCamelCase						:List[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						:Optional[int]						  =     False







							def 				snake_case  (				self     :    Tuple	,					__snake_case     :    Tuple	,					__snake_case     :    List[Any]	,					__snake_case     :    str	,					__snake_case     :    int=None	,					**__snake_case     :    Optional[Any]		):
										if not has_length(__snake_case		):
													return
										if self.prediction_bar is None:
													if self.training_tracker is not None:
																lowerCamelCase						:List[str]						  =     self.training_tracker.add_child(len(__snake_case		)		)
													else:
																lowerCamelCase						:str						  =     NotebookProgressBar(len(__snake_case		)		)
													self.prediction_bar.update(1		)
										else:
													self.prediction_bar.update(self.prediction_bar.value + 1		)







							def 				snake_case  (				self     :    List[str]	,					__snake_case     :    Dict	,					__snake_case     :    Any	,					__snake_case     :    str	,					**__snake_case     :    List[Any]		):
										if self.prediction_bar is not None:
													self.prediction_bar.close()
										lowerCamelCase						:List[str]						  =     None







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







							def 				snake_case  (				self     :    int	,					__snake_case     :    List[str]	,					__snake_case     :    List[str]	,					__snake_case     :    str	,					__snake_case     :    Union[str, Any]=None	,					**__snake_case     :    str		):
										if self.training_tracker is not None:
													lowerCamelCase						:Optional[Any]						  =     {'''Training Loss''': '''No log''', '''Validation Loss''': '''No log'''}
													for log in reversed(state.log_history		):
																if "loss" in log:
																			lowerCamelCase						:Any						  =     log['''loss''']
																			break

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







							def 				snake_case  (				self     :    Optional[int]	,					__snake_case     :    Optional[Any]	,					__snake_case     :    Optional[int]	,					__snake_case     :    Tuple	,					**__snake_case     :    Tuple		):
										self.training_tracker.update(
										    state.global_step	,					comment=F"Epoch {int(state.epoch		)}/{state.num_train_epochs}"	,					force_update=__snake_case		)
										lowerCamelCase						:Any						  =     None




import json
import os
import unittest

from transformers import DebertaTokenizer, DebertaTokenizerFast
from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES
from transformers.testing_utils import slow

from ...test_tokenization_common import TokenizerTesterMixin
class   _lowerCAmelCase		(     __SCREAMING_SNAKE_CASE      ,   unittest.TestCase				):
							_UpperCAmelCase						    =     DebertaTokenizer
							_UpperCAmelCase						    =     True
							_UpperCAmelCase						    =     DebertaTokenizerFast







							def 				snake_case  (				self     :    List[str]		):
										super().setUp()

										# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
										lowerCamelCase						:Dict						  =     [
										    '''l''',
										    '''o''',
										    '''w''',
										    '''e''',
										    '''r''',
										    '''s''',
										    '''t''',
										    '''i''',
										    '''d''',
										    '''n''',
										    '''\u0120''',
										    '''\u0120l''',
										    '''\u0120n''',
										    '''\u0120lo''',
										    '''\u0120low''',
										    '''er''',
										    '''\u0120lowest''',
										    '''\u0120newer''',
										    '''\u0120wider''',
										    '''[UNK]''',
										]
										lowerCamelCase						:List[str]						  =     dict(zip(__snake_case	,					range(len(__snake_case		)		)		)		)
										lowerCamelCase						:Any						  =     ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', '''''']
										lowerCamelCase						:Dict						  =     {'''unk_token''': '''[UNK]'''}

										lowerCamelCase						:Any						  =     os.path.join(self.tmpdirname	,					VOCAB_FILES_NAMES['''vocab_file''']		)
										lowerCamelCase						:List[str]						  =     os.path.join(self.tmpdirname	,					VOCAB_FILES_NAMES['''merges_file''']		)
										with open(self.vocab_file	,					'''w'''	,					encoding='''utf-8'''		) as fp:
													fp.write(json.dumps(__snake_case		) + '''\n'''		)
										with open(self.merges_file	,					'''w'''	,					encoding='''utf-8'''		) as fp:
													fp.write('''\n'''.join(__snake_case		)		)







							def 				snake_case  (				self     :    str	,					**__snake_case     :    Dict		):
										kwargs.update(self.special_tokens_map		)
										return self.tokenizer_class.from_pretrained(self.tmpdirname	,					**__snake_case		)







							def 				snake_case  (				self     :    Optional[Any]	,					__snake_case     :    int		):
										lowerCamelCase						:List[Any]						  =     '''lower newer'''
										lowerCamelCase						:List[str]						  =     '''lower newer'''
										return input_text, output_text







							def 				snake_case  (				self     :    str		):
										lowerCamelCase						:Optional[int]						  =     self.get_tokenizer()
										lowerCamelCase						:Union[str, Any]						  =     '''lower newer'''
										lowerCamelCase						:str						  =     ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er''']
										lowerCamelCase						:Optional[int]						  =     tokenizer.tokenize(__snake_case		)
										self.assertListEqual(__snake_case	,					__snake_case		)

										lowerCamelCase						:List[str]						  =     tokens + [tokenizer.unk_token]
										lowerCamelCase						:Optional[int]						  =     [0, 1, 2, 15, 10, 9, 3, 2, 15, 19]
										self.assertListEqual(tokenizer.convert_tokens_to_ids(__snake_case		)	,					__snake_case		)







							def 				snake_case  (				self     :    Optional[int]		):
										lowerCamelCase						:List[str]						  =     self.get_tokenizer()
										lowerCamelCase						:Optional[int]						  =     tokenizer('''Hello'''	,					'''World'''		)
										lowerCamelCase						:List[str]						  =     [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
										self.assertListEqual(tokd['''token_type_ids''']	,					__snake_case		)







							@slow
							def 				snake_case  (				self     :    str		):
										lowerCamelCase						:Union[str, Any]						  =     self.tokenizer_class.from_pretrained('''microsoft/deberta-base'''		)

										lowerCamelCase						:Optional[Any]						  =     tokenizer.encode('''sequence builders'''	,					add_special_tokens=__snake_case		)
										lowerCamelCase						:Tuple						  =     tokenizer.encode('''multi-sequence build'''	,					add_special_tokens=__snake_case		)

										lowerCamelCase						:Union[str, Any]						  =     tokenizer.encode(
										    '''sequence builders'''	,					add_special_tokens=__snake_case	,					add_prefix_space=__snake_case		)
										lowerCamelCase						:str						  =     tokenizer.encode(
										    '''sequence builders'''	,					'''multi-sequence build'''	,					add_special_tokens=__snake_case	,					add_prefix_space=__snake_case		)

										lowerCamelCase						:Any						  =     tokenizer.build_inputs_with_special_tokens(__snake_case		)
										lowerCamelCase						:Dict						  =     tokenizer.build_inputs_with_special_tokens(__snake_case	,					__snake_case		)

										assert encoded_sentence == encoded_text_from_decode
										assert encoded_pair == encoded_pair_from_decode







							@slow
							def 				snake_case  (				self     :    str		):
										lowerCamelCase						:List[str]						  =     [self.tokenizer_class]
										if self.test_rust_tokenizer:
													tokenizer_classes.append(self.rust_tokenizer_class		)

										for tokenizer_class in tokenizer_classes:
													lowerCamelCase						:int						  =     tokenizer_class.from_pretrained('''microsoft/deberta-base'''		)

													lowerCamelCase						:Tuple						  =     [
													    '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''',
													    '''ALBERT incorporates two parameter reduction techniques''',
													    '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary'''
													    ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of'''
													    ''' vocabulary embedding.''',
													]

													lowerCamelCase						:List[Any]						  =     tokenizer(__snake_case	,					padding=__snake_case		)
													lowerCamelCase						:Union[str, Any]						  =     [tokenizer.decode(__snake_case	,					skip_special_tokens=__snake_case		) for seq in encoding['''input_ids''']]

													# fmt: off
													lowerCamelCase						:Any						  =     {
													    '''input_ids''': [
													        [1, 2118, 11126, 565, 35, 83, 25191, 163, 18854, 13, 12156, 12, 16101, 25376, 13807, 9, 22205, 27893, 1635, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
													        [1, 2118, 11126, 565, 24536, 80, 43797, 4878, 7373, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
													        [1, 133, 78, 65, 16, 10, 3724, 1538, 33183, 11303, 43797, 1938, 4, 870, 24165, 29105, 5, 739, 32644, 33183, 11303, 36173, 88, 80, 650, 7821, 45940, 6, 52, 2559, 5, 1836, 9, 5, 7397, 13171, 31, 5, 1836, 9, 32644, 33183, 11303, 4, 2]
													    ],
													    '''token_type_ids''': [
													        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
													        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
													        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
													    ],
													    '''attention_mask''': [
													        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
													        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
													        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
													    ]
													}
													# fmt: on

													lowerCamelCase						:Optional[int]						  =     [
													    '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''',
													    '''ALBERT incorporates two parameter reduction techniques''',
													    '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary'''
													    ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of'''
													    ''' vocabulary embedding.''',
													]

													self.assertDictEqual(encoding.data	,					__snake_case		)

													for expected, decoded in zip(__snake_case	,					__snake_case		):
																self.assertEqual(__snake_case	,					__snake_case		)



