
'''simple docstring'''


import collections

from .utils import ExplicitEnum, is_torch_available, logging


if is_torch_available():
	import torch


__SCREAMING_SNAKE_CASE						=      logging.get_logger(__name__)




class     __snake_case			:




		def __init__(  self					:List[Any]			,    _SCREAMING_SNAKE_CASE					:List[str]			,    _SCREAMING_SNAKE_CASE					:Optional[Any]=2_1			,    _SCREAMING_SNAKE_CASE					:Any=[]			,    _SCREAMING_SNAKE_CASE					:str=None)					->			Any:






								'''simple docstring'''






								UpperCamelCase_     :	List[Any]					     =     model
								UpperCamelCase_     :	int					     =     trace_batch_nums
								UpperCamelCase_     :	List[str]					     =     abort_after_batch_num

								# keep a LIFO buffer of frames to dump as soon as inf/nan is encountered to give context to the problem emergence
								UpperCamelCase_     :	Any					     =     collections.deque([]			,    _SCREAMING_SNAKE_CASE)
								UpperCamelCase_     :	Dict					     =     []
								UpperCamelCase_     :	int					     =     0
								UpperCamelCase_     :	Any					     =     0
								UpperCamelCase_     :	Optional[int]					     =     False
								UpperCamelCase_     :	Tuple					     =     """                 """

								self.analyse_model()

								self.register_forward_hook()




		def 	_snake_case	(  self					:str			,    _SCREAMING_SNAKE_CASE					:int=None)					->			Any:






								'''simple docstring'''






								if frame is not None:
														self.expand_frame(_SCREAMING_SNAKE_CASE)
								self.frames.append("""\n""".join(self.frame))
								UpperCamelCase_     :	Optional[int]					     =     []  # start a new frame




		def 	_snake_case	(  self					:int			,    _SCREAMING_SNAKE_CASE					:Dict)					->			Any:






								'''simple docstring'''






								self.frame.append(_SCREAMING_SNAKE_CASE)




		def 	_snake_case	(  self					:str)					->			Any:






								'''simple docstring'''






								print("""\n""".join(self.frames))
								UpperCamelCase_     :	Optional[int]					     =     []




		def 	_snake_case	(  self					:int)					->			List[str]:






								'''simple docstring'''






								UpperCamelCase_     :	int					     =     []




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






								'''simple docstring'''






								print(F"""\nDetected inf/nan during batch_number={self.batch_number}""")
								print(F"""Last {len(self.frames)} forward frames:""")
								print(F"""{'abs min':8} {'abs max':8} metadata""")
								print("""\n""".join(self.frames))
								print("""\n\n""")
								UpperCamelCase_     :	str					     =     []




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






								'''simple docstring'''






								UpperCamelCase_     :	Optional[int]					     =     {m: name for name, m in self.model.named_modules()}
								# self.longest_module_name = max(len(v) for v in self.module_names.values())




		def 	_snake_case	(  self					:Any			,    _SCREAMING_SNAKE_CASE					:List[str]			,    _SCREAMING_SNAKE_CASE					:Any)					->			str:






								'''simple docstring'''






								if torch.is_tensor(_SCREAMING_SNAKE_CASE):
														self.expand_frame(get_abs_min_max(_SCREAMING_SNAKE_CASE			,    _SCREAMING_SNAKE_CASE))
														if detect_overflow(_SCREAMING_SNAKE_CASE			,    _SCREAMING_SNAKE_CASE):
																				UpperCamelCase_     :	Union[str, Any]					     =     True
								elif var is None:
														self.expand_frame(F"""{'None':>17} {ctx}""")
								else:
														self.expand_frame(F"""{'not a tensor':>17} {ctx}""")




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






								'''simple docstring'''






								self.expand_frame(F"""\n\n{self.prefix} *** Starting batch number={self.batch_number} ***""")
								self.expand_frame(F"""{'abs min':8} {'abs max':8} metadata""")




		def 	_snake_case	(  self					:Optional[int])					->			int:






								'''simple docstring'''






								self.expand_frame(F"""{self.prefix} *** Finished batch number={self.batch_number-1} ***\n\n""")




		def 	_snake_case	(  self					:str			,    _SCREAMING_SNAKE_CASE					:int			,    _SCREAMING_SNAKE_CASE					:Dict			,    _SCREAMING_SNAKE_CASE					:Any)					->			Tuple:






								'''simple docstring'''






								self.expand_frame(F"""{self.prefix} {self.module_names[module]} {module.__class__.__name__}""")

								# params
								for name, p in module.named_parameters(recurse=_SCREAMING_SNAKE_CASE):
														self.analyse_variable(_SCREAMING_SNAKE_CASE			,    _SCREAMING_SNAKE_CASE)

								# inputs
								if isinstance(_SCREAMING_SNAKE_CASE			,    _SCREAMING_SNAKE_CASE):
														for i, x in enumerate(_SCREAMING_SNAKE_CASE):
																				self.analyse_variable(_SCREAMING_SNAKE_CASE			,    F"""input[{i}]""")
								else:
														self.analyse_variable(_SCREAMING_SNAKE_CASE			,    """input""")

								# outputs
								if isinstance(_SCREAMING_SNAKE_CASE			,    _SCREAMING_SNAKE_CASE):
														for i, x in enumerate(_SCREAMING_SNAKE_CASE):
																				# possibly a tuple of tuples
																				if isinstance(_SCREAMING_SNAKE_CASE			,    _SCREAMING_SNAKE_CASE):
																										for j, y in enumerate(_SCREAMING_SNAKE_CASE):
																																self.analyse_variable(_SCREAMING_SNAKE_CASE			,    F"""output[{i}][{j}]""")
																				else:
																										self.analyse_variable(_SCREAMING_SNAKE_CASE			,    F"""output[{i}]""")
								else:
														self.analyse_variable(_SCREAMING_SNAKE_CASE			,    """output""")

								self.save_frame()




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






								'''simple docstring'''






								self.model.apply(self._register_forward_hook)




		def 	_snake_case	(  self					:str			,    _SCREAMING_SNAKE_CASE					:Dict)					->			str:






								'''simple docstring'''






								module.register_forward_hook(self.forward_hook)





		def 	_snake_case	(  self					:Optional[Any]			,    _SCREAMING_SNAKE_CASE					:Optional[int]			,    _SCREAMING_SNAKE_CASE					:Dict			,    _SCREAMING_SNAKE_CASE					:Optional[int])					->			Any:






								'''simple docstring'''






								UpperCamelCase_     :	int					     =     False

								UpperCamelCase_     :	Any					     =     True if self.batch_number in self.trace_batch_nums else False
								if trace_mode:
														self.reset_saved_frames()

								if self.total_calls == 0:
														self.batch_start_frame()
								self.total_calls += 1

								# count batch numbers - the very first forward hook of the batch will be called when the
								# batch completes - i.e. it gets called very last - we know this batch has finished
								if module == self.model:
														self.batch_number += 1
														UpperCamelCase_     :	Union[str, Any]					     =     True

								self.create_frame(_SCREAMING_SNAKE_CASE			,    _SCREAMING_SNAKE_CASE			,    _SCREAMING_SNAKE_CASE)

								# if last_frame_of_batch:
								#     self.batch_end_frame()

								if trace_mode:
														self.trace_frames()

								if last_frame_of_batch:
														self.batch_start_frame()

								if self.detected_overflow and not trace_mode:
														self.dump_saved_frames()

														# now we can abort, as it's pointless to continue running
														raise ValueError(
														    """DebugUnderflowOverflow: inf/nan detected, aborting as there is no point running further. """
														    """Please scroll up above this traceback to see the activation values prior to this event.""")

								# abort after certain batch if requested to do so
								if self.abort_after_batch_num is not None and self.batch_number > self.abort_after_batch_num:
														raise ValueError(
														    F"""DebugUnderflowOverflow: aborting after {self.batch_number} batches due to"""
														    F""" `abort_after_batch_num={self.abort_after_batch_num}` arg""")







def 					__magic_name__(       UpperCAmelCase__ :       Union[str, Any]							,				UpperCAmelCase__ :       List[str]	):
						UpperCamelCase_     :	Optional[Any]					     =     var.abs()
						return f"""{abs_var.min():8.2e} {abs_var.max():8.2e} {ctx}"""
def 					__magic_name__(       UpperCAmelCase__ :       Optional[int]							,				UpperCAmelCase__ :       Optional[int]	):
						UpperCamelCase_     :	Optional[int]					     =     False
						if torch.isnan(UpperCAmelCase__	).any().item():
												UpperCamelCase_     :	Dict					     =     True
												print(f"""{ctx} has nans"""	)
						if torch.isinf(UpperCAmelCase__	).any().item():
												UpperCamelCase_     :	Any					     =     True
												print(f"""{ctx} has infs"""	)

						# if needed to monitor large elements can enable the following
						if 0:  # and detected:
												UpperCamelCase_     :	Any					     =     var[torch.ge(var.abs()							,				100	)]
												if naaa.numel() > 0:
																		print(f"""{ctx}:  n100={naaa.numel()}"""	)
												UpperCamelCase_     :	int					     =     var[torch.ge(var.abs()							,				1000	)]
												if naaaa.numel() > 0:
																		print(f"""{ctx}: n1000={naaaa.numel()}"""	)
												UpperCamelCase_     :	Optional[int]					     =     var[torch.ge(var.abs()							,				10000	)]
												if naaaaa.numel() > 0:
																		print(f"""{ctx}: n10000={naaaaa.numel()}"""	)

						if 0:
												print(f"""min={var.min():9.2e} max={var.max():9.2e}"""	)

						if 0:
												print(f"""min={var.min():9.2e} max={var.max():9.2e} var={var.var():9.2e} mean={var.mean():9.2e} ({ctx})"""	)

						return detected







class     __snake_case			(    _a	):
		a_          =					"underflow_overflow"
		a_          =					"tpu_metrics_debug"






'''simple docstring'''


import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
	from .tokenization_xglm import XGLMTokenizer
else:
	__SCREAMING_SNAKE_CASE						=      None


__SCREAMING_SNAKE_CASE						=      logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE						=      {"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"}

__SCREAMING_SNAKE_CASE						=      {
    "vocab_file": {
        "facebook/xglm-564M": "https://huggingface.co/facebook/xglm-564M/resolve/main/sentencepiece.bpe.model",
    },
    "tokenizer_file": {
        "facebook/xglm-564M": "https://huggingface.co/facebook/xglm-564M/resolve/main/tokenizer.json",
    },
}

__SCREAMING_SNAKE_CASE						=      {
    "facebook/xglm-564M": 2048,
}




class     __snake_case			(    _a	):
		a_          =					VOCAB_FILES_NAMES
		a_          =					PRETRAINED_VOCAB_FILES_MAP
		a_          =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		a_          =					["input_ids", "attention_mask"]
		a_          =					XGLMTokenizer




		def __init__(  self					:str			,    _SCREAMING_SNAKE_CASE					:Dict=None			,    _SCREAMING_SNAKE_CASE					:List[str]=None			,    _SCREAMING_SNAKE_CASE					:Dict="<s>"			,    _SCREAMING_SNAKE_CASE					:Dict="</s>"			,    _SCREAMING_SNAKE_CASE					:List[Any]="</s>"			,    _SCREAMING_SNAKE_CASE					:Tuple="<s>"			,    _SCREAMING_SNAKE_CASE					:int="<unk>"			,    _SCREAMING_SNAKE_CASE					:List[str]="<pad>"			,    **_SCREAMING_SNAKE_CASE					:Optional[int]			,    )					->			Optional[int]:






								'''simple docstring'''






								UpperCamelCase_     :	Tuple					     =     7
								UpperCamelCase_     :	int					     =     [F"""<madeupword{i}>""" for i in range(self.num_madeup_words)]

								UpperCamelCase_     :	Optional[Any]					     =     kwargs.get("""additional_special_tokens"""			,    [])
								kwargs["additional_special_tokens"] += [
								    word for word in madeup_words if word not in kwargs["additional_special_tokens"]
								]

								super().__init__(
								    _SCREAMING_SNAKE_CASE			,    tokenizer_file=_SCREAMING_SNAKE_CASE			,    bos_token=_SCREAMING_SNAKE_CASE			,    eos_token=_SCREAMING_SNAKE_CASE			,    sep_token=_SCREAMING_SNAKE_CASE			,    cls_token=_SCREAMING_SNAKE_CASE			,    unk_token=_SCREAMING_SNAKE_CASE			,    pad_token=_SCREAMING_SNAKE_CASE			,    **_SCREAMING_SNAKE_CASE			,    )

								UpperCamelCase_     :	int					     =     vocab_file
								UpperCamelCase_     :	Any					     =     False if not self.vocab_file else True




		def 	_snake_case	(  self					:Dict			,    _SCREAMING_SNAKE_CASE					:List[int]			,    _SCREAMING_SNAKE_CASE					:Optional[List[int]] = None)					->			List[int]:






								'''simple docstring'''






								if token_ids_a is None:
														return [self.sep_token_id] + token_ids_a
								UpperCamelCase_     :	str					     =     [self.sep_token_id]
								return sep + token_ids_a + sep + sep + token_ids_a




		def 	_snake_case	(  self					:int			,    _SCREAMING_SNAKE_CASE					:List[int]			,    _SCREAMING_SNAKE_CASE					:Optional[List[int]] = None)					->			List[int]:






								'''simple docstring'''






								UpperCamelCase_     :	Optional[Any]					     =     [self.sep_token_id]

								if token_ids_a is None:
														return len(sep + token_ids_a) * [0]
								return len(sep + token_ids_a + sep + sep + token_ids_a) * [0]





		def 	_snake_case	(  self					:List[Any]			,    _SCREAMING_SNAKE_CASE					:str			,    _SCREAMING_SNAKE_CASE					:Optional[str] = None)					->			Tuple[str]:






								'''simple docstring'''






								if not self.can_save_slow_tokenizer:
														raise ValueError(
														    """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """
														    """tokenizer.""")

								if not os.path.isdir(_SCREAMING_SNAKE_CASE):
														logger.error(F"""Vocabulary path ({save_directory}) should be a directory.""")
														return
								UpperCamelCase_     :	Dict					     =     os.path.join(
								    _SCREAMING_SNAKE_CASE			,    (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""])

								if os.path.abspath(self.vocab_file) != os.path.abspath(_SCREAMING_SNAKE_CASE):
														copyfile(self.vocab_file			,    _SCREAMING_SNAKE_CASE)

								return (out_vocab_file,)




