






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   (    UpperCamelCase_       :					str		)  ->   List[Any]:

						"""simple docstring"""



						lowerCAmelCase__    						=						int(UpperCamelCase_		)
						lowerCAmelCase__						,       lowerCAmelCase__						,       lowerCAmelCase__    						=						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   (    UpperCamelCase_       :					List[Any]    ,       UpperCamelCase_       :					Tuple    ,       UpperCamelCase_       :					Union[str, Any]    ,       UpperCamelCase_       :					int    ,       UpperCamelCase_       :					Optional[Any]=300		)  ->   int:

						"""simple docstring"""



						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   (    UpperCamelCase_       :					Optional[Any]		)  ->   Union[str, Any]:

						"""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(UpperCamelCase_    ,       UpperCamelCase_		) else str(UpperCamelCase_		)
																		html_code += F"      <td>{elt}</td>\n"
												html_code += "    </tr>\n"
						html_code += "  </tbody>\n</table><p>"
						return html_code

class     __SCREAMING_SNAKE_CASE					:
						_SCREAMING_SNAKE_CASE	:       List[str]   =					5
						_SCREAMING_SNAKE_CASE	:       Union[str, Any]   =					0.2






						def __init__(	self     ,						_UpperCamelCase     ,						_UpperCamelCase = None     ,						_UpperCamelCase = True     ,						_UpperCamelCase = None     ,						_UpperCamelCase = 3_00     ,						):






												"""simple docstring"""
												lowerCAmelCase__    						=						total
												lowerCAmelCase__    						=						'' if prefix is None else prefix
												lowerCAmelCase__    						=						leave
												lowerCAmelCase__    						=						parent
												lowerCAmelCase__    						=						width
												lowerCAmelCase__    						=						None
												lowerCAmelCase__    						=						None
												lowerCAmelCase__    						=						None






						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase = False     ,						_UpperCamelCase = None   ):






												"""simple docstring"""
												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(_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__    						=						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(_UpperCamelCase   )
																		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       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase=None   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						' ' * (len(str(self.total   )   ) - len(str(_UpperCamelCase   )   )) + str(_UpperCamelCase   )
												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       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												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=_UpperCamelCase   )
												else:
																		self.output.update(disp.HTML(self.html_code   )   )







						def       UpperCamelCase__      (	self   ):






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




class     __SCREAMING_SNAKE_CASE					(   __lowercase):






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






												"""simple docstring"""
												super().__init__(_UpperCamelCase   )
												lowerCAmelCase__    						=						None if column_names is None else [column_names]
												lowerCAmelCase__    						=						None






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												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=_UpperCamelCase   )
												else:
																		self.output.update(disp.HTML(self.html_code   )   )






						def       UpperCamelCase__      (	self     ,						_UpperCamelCase   ):






												"""simple docstring"""
												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(_UpperCamelCase   )
																								lowerCAmelCase__    						=						columns
																		self.inner_table.append([values[c] for c in columns]   )






						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase=None     ,						_UpperCamelCase=3_00   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						NotebookProgressBar(_UpperCamelCase     ,						prefix=_UpperCamelCase     ,						parent=self     ,						width=_UpperCamelCase   )
												return self.child_bar







						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						None
												self.display()




class     __SCREAMING_SNAKE_CASE					(   __lowercase):






						def __init__(	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						None
												lowerCAmelCase__    						=						None
												lowerCAmelCase__    						=						False






						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						**_UpperCamelCase   ):






												"""simple docstring"""
												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     ,						_UpperCamelCase   )






						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						**_UpperCamelCase   ):






												"""simple docstring"""
												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       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase=None     ,						**_UpperCamelCase   ):






												"""simple docstring"""
												if not has_length(_UpperCamelCase   ):
																		return
												if self.prediction_bar is None:
																		if self.training_tracker is not None:
																								lowerCAmelCase__    						=						self.training_tracker.add_child(len(_UpperCamelCase   )   )
																		else:
																								lowerCAmelCase__    						=						NotebookProgressBar(len(_UpperCamelCase   )   )
																		self.prediction_bar.update(1   )
												else:
																		self.prediction_bar.update(self.prediction_bar.value + 1   )






						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						**_UpperCamelCase   ):






												"""simple docstring"""
												if self.prediction_bar is not None:
																		self.prediction_bar.close()
												lowerCAmelCase__    						=						None






						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase=None     ,						**_UpperCamelCase   ):






												"""simple docstring"""
												# 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(_UpperCamelCase   )






						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase=None     ,						**_UpperCamelCase   ):






												"""simple docstring"""
												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$'     ,						''     ,						_UpperCamelCase   )
																		lowerCAmelCase__    						=						metrics.pop('total_flos'     ,						_UpperCamelCase   )
																		lowerCAmelCase__    						=						metrics.pop('epoch'     ,						_UpperCamelCase   )
																		lowerCAmelCase__    						=						metrics.pop(F"{metric_key_prefix}_runtime"     ,						_UpperCamelCase   )
																		lowerCAmelCase__    						=						metrics.pop(F"{metric_key_prefix}_samples_per_second"     ,						_UpperCamelCase   )
																		lowerCAmelCase__    						=						metrics.pop(F"{metric_key_prefix}_steps_per_second"     ,						_UpperCamelCase   )
																		lowerCAmelCase__    						=						metrics.pop(F"{metric_key_prefix}_jit_compilation_time"     ,						_UpperCamelCase   )
																		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(_UpperCamelCase   )
																		self.training_tracker.remove_child()
																		lowerCAmelCase__    						=						None
																		# Evaluation takes a long time so we should force the next update.
																		lowerCAmelCase__    						=						True







						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						**_UpperCamelCase   ):






												"""simple docstring"""
												self.training_tracker.update(
												    state.global_step     ,						comment=F"Epoch {int(state.epoch   )}/{state.num_train_epochs}"     ,						force_update=_UpperCamelCase   )
												lowerCAmelCase__    						=						None













import json
from typing import TYPE_CHECKING, List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_base import BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging


if TYPE_CHECKING:
					from transformers.pipelines.conversational import Conversation


__snake_case    :    Any         =     logging.get_logger(__name__)

__snake_case    :    Tuple         =     {"""tokenizer_file""": """tokenizer.json"""}

__snake_case    :    str         =     {
    """tokenizer_file""": {
        """bigscience/tokenizer""": """https://huggingface.co/bigscience/tokenizer/blob/main/tokenizer.json""",
        """bigscience/bloom-560m""": """https://huggingface.co/bigscience/bloom-560m/blob/main/tokenizer.json""",
        """bigscience/bloom-1b1""": """https://huggingface.co/bigscience/bloom-1b1/blob/main/tokenizer.json""",
        """bigscience/bloom-1b7""": """https://huggingface.co/bigscience/bloom-1b7/blob/main/tokenizer.json""",
        """bigscience/bloom-3b""": """https://huggingface.co/bigscience/bloom-3b/blob/main/tokenizer.json""",
        """bigscience/bloom-7b1""": """https://huggingface.co/bigscience/bloom-7b1/blob/main/tokenizer.json""",
        """bigscience/bloom""": """https://huggingface.co/bigscience/bloom/blob/main/tokenizer.json""",
    },
}


class     __SCREAMING_SNAKE_CASE					(   __lowercase):
						_SCREAMING_SNAKE_CASE	:       Dict   =					VOCAB_FILES_NAMES
						_SCREAMING_SNAKE_CASE	:       Union[str, Any]   =					PRETRAINED_VOCAB_FILES_MAP
						_SCREAMING_SNAKE_CASE	:       Union[str, Any]   =					['''input_ids''', '''attention_mask''']
						_SCREAMING_SNAKE_CASE	:       Union[str, Any]   =					None






						def __init__(	self     ,						_UpperCamelCase=None     ,						_UpperCamelCase=None     ,						_UpperCamelCase=None     ,						_UpperCamelCase="<unk>"     ,						_UpperCamelCase="<s>"     ,						_UpperCamelCase="</s>"     ,						_UpperCamelCase="<pad>"     ,						_UpperCamelCase=False     ,						_UpperCamelCase=False     ,						**_UpperCamelCase     ,						):






												"""simple docstring"""
												super().__init__(
												    _UpperCamelCase     ,						_UpperCamelCase     ,						tokenizer_file=_UpperCamelCase     ,						unk_token=_UpperCamelCase     ,						bos_token=_UpperCamelCase     ,						eos_token=_UpperCamelCase     ,						pad_token=_UpperCamelCase     ,						add_prefix_space=_UpperCamelCase     ,						clean_up_tokenization_spaces=_UpperCamelCase     ,						**_UpperCamelCase     ,						)
												lowerCAmelCase__    						=						json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()   )
												if pre_tok_state.get('add_prefix_space'     ,						_UpperCamelCase   ) != add_prefix_space:
																		lowerCAmelCase__    						=						getattr(_UpperCamelCase     ,						pre_tok_state.pop('type'   )   )
																		lowerCAmelCase__    						=						add_prefix_space
																		lowerCAmelCase__    						=						pre_tok_class(**_UpperCamelCase   )

												lowerCAmelCase__    						=						add_prefix_space






						def       UpperCamelCase__      (	self     ,						*_UpperCamelCase     ,						**_UpperCamelCase   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						kwargs.get('is_split_into_words'     ,						_UpperCamelCase   )
												if not (self.add_prefix_space or not is_split_into_words):
																		raise Exception(
																		    F"You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with"
																		    ' pretokenized inputs.'   )

												return super()._batch_encode_plus(*_UpperCamelCase     ,						**_UpperCamelCase   )






						def       UpperCamelCase__      (	self     ,						*_UpperCamelCase     ,						**_UpperCamelCase   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						kwargs.get('is_split_into_words'     ,						_UpperCamelCase   )

												if not (self.add_prefix_space or not is_split_into_words):
																		raise Exception(
																		    F"You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with"
																		    ' pretokenized inputs.'   )

												return super()._encode_plus(*_UpperCamelCase     ,						**_UpperCamelCase   )






						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase = None   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						self._tokenizer.model.save(_UpperCamelCase     ,						name=_UpperCamelCase   )
												return tuple(_UpperCamelCase   )







						def       UpperCamelCase__      (	self     ,						_UpperCamelCase   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						[]
												for is_user, text in conversation.iter_texts():
																		input_ids.extend(self.encode(_UpperCamelCase     ,						add_special_tokens=_UpperCamelCase   ) + [self.eos_token_id]   )

												if len(_UpperCamelCase   ) > self.model_max_length:
																		lowerCAmelCase__    						=						input_ids[-self.model_max_length :]
												return input_ids





