

"""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     a__    (    __SCREAMING_SNAKE_CASE )	->	Any:
		__lowerCAmelCase:       Optional[int]          =			int(__SCREAMING_SNAKE_CASE )
		__lowerCAmelCase      , __lowerCAmelCase      , __lowerCAmelCase:       Dict          =			t // 3_6_0_0, (t // 6_0) % 6_0, t % 6_0
		return F"{h}:{m:02d}:{s:02d}" if h != 0 else F"{m:02d}:{s:02d}"






def     a__    (    __SCREAMING_SNAKE_CASE					, __SCREAMING_SNAKE_CASE					, __SCREAMING_SNAKE_CASE					, __SCREAMING_SNAKE_CASE					, __SCREAMING_SNAKE_CASE=3_0_0 )	->	List[str]:
		# 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     a__    (    __SCREAMING_SNAKE_CASE )	->	List[str]:
		__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:       int          =			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	:
				SCREAMING_SNAKE_CASE_   :      int         =  5
				SCREAMING_SNAKE_CASE_   :      Optional[int]         =  0.2

				def __init__(	self							:				List[Any]     ,  UpperCamelCase__							:				int     ,  UpperCamelCase__							:				Optional[str] = None     ,  UpperCamelCase__							:				bool = True     ,  UpperCamelCase__							:				Optional["NotebookTrainingTracker"] = None     ,  UpperCamelCase__							:				int = 3_0_0     ,  )->   str:



						'''simple docstring'''


						__lowerCAmelCase:       str          =			total
						__lowerCAmelCase:       int          =			"" if prefix is None else prefix
						__lowerCAmelCase:       List[Any]          =			leave
						__lowerCAmelCase:       Dict          =			parent
						__lowerCAmelCase:       int          =			width
						__lowerCAmelCase:       Optional[int]          =			None
						__lowerCAmelCase:       List[str]          =			None
						__lowerCAmelCase:       Optional[Any]          =			None

				def 							lowercase_     (	self							:				List[Any]     ,  UpperCamelCase__							:				int     ,  UpperCamelCase__							:				bool = False     ,  UpperCamelCase__							:				str = None)->   List[str]:



						'''simple docstring'''


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

				def 							lowercase_     (	self							:				Optional[int]     ,  UpperCamelCase__							:				Union[str, Any]     ,  UpperCamelCase__							:				Union[str, Any]=None)->   Optional[int]:



						'''simple docstring'''


						__lowerCAmelCase:       Union[str, Any]          =			" " * (len(str(self.total)) - len(str(UpperCamelCase__))) + str(UpperCamelCase__)
						if self.elapsed_time is None:
								__lowerCAmelCase:       int          =			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:       int          =			(
								    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 							lowercase_     (	self							:				Union[str, Any])->   Dict:



						'''simple docstring'''


						__lowerCAmelCase:       int          =			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[Any]          =			disp.display(disp.HTML(self.html_code)     ,  display_id=UpperCamelCase__)
						else:
								self.output.update(disp.HTML(self.html_code))




				def 							lowercase_     (	self							:				List[Any])->   Union[str, Any]:



						'''simple docstring'''


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





class     snake_case	(				__snake_case  ):

				def __init__(	self							:				Dict     ,  UpperCamelCase__							:				int     ,  UpperCamelCase__							:				Dict=None)->   Optional[int]:



						'''simple docstring'''


						super().__init__(UpperCamelCase__)
						__lowerCAmelCase:       Dict          =			None if column_names is None else [column_names]
						__lowerCAmelCase:       List[str]          =			None

				def 							lowercase_     (	self							:				Union[str, Any])->   List[str]:



						'''simple docstring'''


						__lowerCAmelCase:       List[str]          =			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:       Union[str, Any]          =			disp.display(disp.HTML(self.html_code)     ,  display_id=UpperCamelCase__)
						else:
								self.output.update(disp.HTML(self.html_code))

				def 							lowercase_     (	self							:				List[Any]     ,  UpperCamelCase__							:				Any)->   List[Any]:



						'''simple docstring'''


						if self.inner_table is None:
								__lowerCAmelCase:       Optional[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(UpperCamelCase__)
										__lowerCAmelCase:       List[str]          =			columns
								self.inner_table.append([values[c] for c in columns])

				def 							lowercase_     (	self							:				Union[str, Any]     ,  UpperCamelCase__							:				List[str]     ,  UpperCamelCase__							:				int=None     ,  UpperCamelCase__							:				Optional[int]=3_0_0)->   Union[str, Any]:



						'''simple docstring'''


						__lowerCAmelCase:       int          =			NotebookProgressBar(UpperCamelCase__     ,  prefix=UpperCamelCase__     ,  parent=self     ,  width=UpperCamelCase__)
						return self.child_bar




				def 							lowercase_     (	self							:				str)->   List[str]:



						'''simple docstring'''


						__lowerCAmelCase:       int          =			None
						self.display()





class     snake_case	(				__snake_case  ):

				def __init__(	self							:				Optional[int])->   Dict:



						'''simple docstring'''


						__lowerCAmelCase:       List[Any]          =			None
						__lowerCAmelCase:       str          =			None
						__lowerCAmelCase:       List[Any]          =			False

				def 							lowercase_     (	self							:				int     ,  UpperCamelCase__							:				Tuple     ,  UpperCamelCase__							:				Optional[int]     ,  UpperCamelCase__							:				Optional[int]     ,  **UpperCamelCase__							:				List[str])->   Tuple:



						'''simple docstring'''


						__lowerCAmelCase:       Dict          =			"Epoch" if args.evaluation_strategy == IntervalStrategy.EPOCH else "Step"
						__lowerCAmelCase:       str          =			0
						__lowerCAmelCase:       Any          =			0
						__lowerCAmelCase:       List[str]          =			[self.first_column] + ["Training Loss"]
						if args.evaluation_strategy != IntervalStrategy.NO:
								column_names.append("Validation Loss")
						__lowerCAmelCase:       Optional[Any]          =			NotebookTrainingTracker(state.max_steps     ,  UpperCamelCase__)

				def 							lowercase_     (	self							:				str     ,  UpperCamelCase__							:				Any     ,  UpperCamelCase__							:				Union[str, Any]     ,  UpperCamelCase__							:				Dict     ,  **UpperCamelCase__							:				str)->   Union[str, Any]:



						'''simple docstring'''


						__lowerCAmelCase:       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[Any]          =			False

				def 							lowercase_     (	self							:				Dict     ,  UpperCamelCase__							:				Optional[int]     ,  UpperCamelCase__							:				Optional[int]     ,  UpperCamelCase__							:				Optional[int]     ,  UpperCamelCase__							:				int=None     ,  **UpperCamelCase__							:				List[Any])->   Union[str, Any]:



						'''simple docstring'''


						if not has_length(UpperCamelCase__):
								return
						if self.prediction_bar is None:
								if self.training_tracker is not None:
										__lowerCAmelCase:       Tuple          =			self.training_tracker.add_child(len(UpperCamelCase__))
								else:
										__lowerCAmelCase:       List[str]          =			NotebookProgressBar(len(UpperCamelCase__))
								self.prediction_bar.update(1)
						else:
								self.prediction_bar.update(self.prediction_bar.value + 1)

				def 							lowercase_     (	self							:				Tuple     ,  UpperCamelCase__							:				Optional[int]     ,  UpperCamelCase__							:				List[str]     ,  UpperCamelCase__							:				List[Any]     ,  **UpperCamelCase__							:				int)->   Dict:



						'''simple docstring'''


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

				def 							lowercase_     (	self							:				List[Any]     ,  UpperCamelCase__							:				Optional[int]     ,  UpperCamelCase__							:				Tuple     ,  UpperCamelCase__							:				str     ,  UpperCamelCase__							:				int=None     ,  **UpperCamelCase__							:				Optional[int])->   Union[str, Any]:



						'''simple docstring'''


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

				def 							lowercase_     (	self							:				Union[str, Any]     ,  UpperCamelCase__							:				Optional[Any]     ,  UpperCamelCase__							:				Tuple     ,  UpperCamelCase__							:				int     ,  UpperCamelCase__							:				Dict=None     ,  **UpperCamelCase__							:				Optional[Any])->   List[str]:



						'''simple docstring'''


						if self.training_tracker is not None:
								__lowerCAmelCase:       List[Any]          =			{"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:       int          =			int(state.epoch)
								else:
										__lowerCAmelCase:       Any          =			state.global_step
								__lowerCAmelCase:       Tuple          =			"eval"
								for k in metrics:
										if k.endswith("_loss"):
												__lowerCAmelCase:       Optional[Any]          =			re.sub(R"\_loss$"     ,  ""     ,  UpperCamelCase__)
								__lowerCAmelCase:       Tuple          =			metrics.pop("total_flos"     ,  UpperCamelCase__)
								__lowerCAmelCase:       List[Any]          =			metrics.pop("epoch"     ,  UpperCamelCase__)
								__lowerCAmelCase:       Any          =			metrics.pop(f"{metric_key_prefix}_runtime"     ,  UpperCamelCase__)
								__lowerCAmelCase:       Dict          =			metrics.pop(f"{metric_key_prefix}_samples_per_second"     ,  UpperCamelCase__)
								__lowerCAmelCase:       Tuple          =			metrics.pop(f"{metric_key_prefix}_steps_per_second"     ,  UpperCamelCase__)
								__lowerCAmelCase:       Optional[int]          =			metrics.pop(f"{metric_key_prefix}_jit_compilation_time"     ,  UpperCamelCase__)
								for k, v in metrics.items():
										if k == f"{metric_key_prefix}_loss":
												__lowerCAmelCase:       Optional[int]          =			v
										else:
												__lowerCAmelCase:       Tuple          =			k.split("_")
												__lowerCAmelCase:       Union[str, Any]          =			" ".join([part.capitalize() for part in splits[1:]])
												__lowerCAmelCase:       Dict          =			v
								self.training_tracker.write_line(UpperCamelCase__)
								self.training_tracker.remove_child()
								__lowerCAmelCase:       List[str]          =			None
								# Evaluation takes a long time so we should force the next update.
								__lowerCAmelCase:       Tuple          =			True




				def 							lowercase_     (	self							:				str     ,  UpperCamelCase__							:				Optional[int]     ,  UpperCamelCase__							:				Optional[int]     ,  UpperCamelCase__							:				Tuple     ,  **UpperCamelCase__							:				Union[str, Any])->   Optional[int]:



						'''simple docstring'''


						self.training_tracker.update(
						    state.global_step     ,  comment=f"Epoch {int(state.epoch)}/{state.num_train_epochs}"     ,  force_update=UpperCamelCase__)
						__lowerCAmelCase:       List[str]          =			None





"""simple docstring"""


import unittest

from datasets import load_dataset

from transformers import BloomTokenizerFast
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin





@require_tokenizers
class     snake_case	(				__snake_case,							unittest.TestCase  ):
				SCREAMING_SNAKE_CASE_   :      List[str]         =  None
				SCREAMING_SNAKE_CASE_   :      Tuple         =  BloomTokenizerFast
				SCREAMING_SNAKE_CASE_   :      str         =  BloomTokenizerFast
				SCREAMING_SNAKE_CASE_   :      int         =  True
				SCREAMING_SNAKE_CASE_   :      Tuple         =  False
				SCREAMING_SNAKE_CASE_   :      int         =  """tokenizer_file"""
				SCREAMING_SNAKE_CASE_   :      List[str]         =  {"""bos_token""": """<s>""", """eos_token""": """</s>""", """unk_token""": """<unk>""", """pad_token""": """<pad>"""}

				def 							lowercase_     (	self							:				List[Any])->   Dict:



						'''simple docstring'''


						super().setUp()
						__lowerCAmelCase:       Optional[Any]          =			BloomTokenizerFast.from_pretrained("bigscience/tokenizer")
						tokenizer.save_pretrained(self.tmpdirname)

				def 							lowercase_     (	self							:				List[Any]     ,  **UpperCamelCase__							:				Union[str, Any])->   Optional[Any]:



						'''simple docstring'''


						kwargs.update(self.special_tokens_map)
						return BloomTokenizerFast.from_pretrained(self.tmpdirname     ,  **UpperCamelCase__)

				def 							lowercase_     (	self							:				Union[str, Any])->   Union[str, Any]:



						'''simple docstring'''


						__lowerCAmelCase:       str          =			self.get_rust_tokenizer()

						__lowerCAmelCase:       int          =			["The quick brown fox</s>", "jumps over the lazy dog</s>"]
						__lowerCAmelCase:       List[str]          =			[[2_1_7_5, 2_3_7_1_4, 7_3_1_7_3, 1_4_4_2_5_2, 2], [7_7, 1_3_2_6_1_9, 3_4_7_8, 3_6_8, 1_0_9_5_8_6, 3_5_4_3_3, 2]]

						__lowerCAmelCase:       List[str]          =			tokenizer.batch_encode_plus(UpperCamelCase__)["input_ids"]
						self.assertListEqual(UpperCamelCase__     ,  UpperCamelCase__)

						__lowerCAmelCase:       List[Any]          =			tokenizer.batch_decode(UpperCamelCase__)
						self.assertListEqual(UpperCamelCase__     ,  UpperCamelCase__)

				def 							lowercase_     (	self							:				Optional[Any]     ,  UpperCamelCase__							:				Tuple=6)->   Tuple:



						'''simple docstring'''


						for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
								with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})"):
										__lowerCAmelCase:       Optional[Any]          =			self.rust_tokenizer_class.from_pretrained(UpperCamelCase__     ,  **UpperCamelCase__)
										# tokenizer_r.pad_token = None # Hotfixing padding = None
										# Simple input
										__lowerCAmelCase:       Dict          =			"This is a simple input"
										__lowerCAmelCase:       str          =			["This is a simple input 1", "This is a simple input 2"]
										__lowerCAmelCase:       int          =			("This is a simple input", "This is a pair")
										__lowerCAmelCase:       Union[str, Any]          =			[
										    ("This is a simple input 1", "This is a simple input 2"),
										    ("This is a simple pair 1", "This is a simple pair 2"),
										]

										# Simple input tests
										try:
												tokenizer_r.encode(UpperCamelCase__     ,  max_length=UpperCamelCase__)
												tokenizer_r.encode_plus(UpperCamelCase__     ,  max_length=UpperCamelCase__)

												tokenizer_r.batch_encode_plus(UpperCamelCase__     ,  max_length=UpperCamelCase__)
												tokenizer_r.encode(UpperCamelCase__     ,  max_length=UpperCamelCase__)
												tokenizer_r.batch_encode_plus(UpperCamelCase__     ,  max_length=UpperCamelCase__)
										except ValueError:
												self.fail("Bloom Tokenizer should be able to deal with padding")

										__lowerCAmelCase:       Tuple          =			None  # Hotfixing padding = None
										self.assertRaises(UpperCamelCase__     ,  tokenizer_r.encode     ,  UpperCamelCase__     ,  max_length=UpperCamelCase__     ,  padding="max_length")

										# Simple input
										self.assertRaises(UpperCamelCase__     ,  tokenizer_r.encode_plus     ,  UpperCamelCase__     ,  max_length=UpperCamelCase__     ,  padding="max_length")

										# Simple input
										self.assertRaises(
										    UpperCamelCase__     ,  tokenizer_r.batch_encode_plus     ,  UpperCamelCase__     ,  max_length=UpperCamelCase__     ,  padding="max_length"     ,  )

										# Pair input
										self.assertRaises(UpperCamelCase__     ,  tokenizer_r.encode     ,  UpperCamelCase__     ,  max_length=UpperCamelCase__     ,  padding="max_length")

										# Pair input
										self.assertRaises(UpperCamelCase__     ,  tokenizer_r.encode_plus     ,  UpperCamelCase__     ,  max_length=UpperCamelCase__     ,  padding="max_length")

										# Pair input
										self.assertRaises(
										    UpperCamelCase__     ,  tokenizer_r.batch_encode_plus     ,  UpperCamelCase__     ,  max_length=UpperCamelCase__     ,  padding="max_length"     ,  )

				def 							lowercase_     (	self							:				Optional[Any])->   List[str]:



						'''simple docstring'''


						__lowerCAmelCase:       Dict          =			self.get_rust_tokenizer()
						__lowerCAmelCase:       List[str]          =			load_dataset("xnli"     ,  "all_languages"     ,  split="test"     ,  streaming=UpperCamelCase__)

						__lowerCAmelCase:       Union[str, Any]          =			next(iter(UpperCamelCase__))["premise"]  # pick up one data
						__lowerCAmelCase:       Any          =			list(sample_data.values())

						__lowerCAmelCase:       int          =			list(map(tokenizer.encode     ,  UpperCamelCase__))
						__lowerCAmelCase:       str          =			[tokenizer.decode(UpperCamelCase__     ,  clean_up_tokenization_spaces=UpperCamelCase__) for x in output_tokens]
						self.assertListEqual(UpperCamelCase__     ,  UpperCamelCase__)




				def 							lowercase_     (	self							:				Optional[int])->   str:



						'''simple docstring'''


						self.assertGreaterEqual(len(self.tokenizer_class.pretrained_vocab_files_map)     ,  1)
						self.assertGreaterEqual(len(list(self.tokenizer_class.pretrained_vocab_files_map.values())[0])     ,  1)


