





import collections

from .utils import ExplicitEnum, is_torch_available, logging


if is_torch_available():
			import torch


snake_case__									=				logging.get_logger(__name__)






class  __A   :

			'''simple docstring'''







			def __init__(   self	,  __lowerCAmelCase	,  __lowerCAmelCase=2_1	,  __lowerCAmelCase=[]	,  __lowerCAmelCase=None							):



				'''simple docstring'''







				lowerCamelCase__        =		model
				lowerCamelCase__        =		trace_batch_nums
				lowerCamelCase__        =		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
				lowerCamelCase__        =		collections.deque([]	,  __lowerCAmelCase							)
				lowerCamelCase__        =		[]
				lowerCamelCase__        =		0
				lowerCamelCase__        =		0
				lowerCamelCase__        =		False
				lowerCamelCase__        =		'''                 '''

				self.analyse_model()

				self.register_forward_hook()





			def 		__lowerCamelCase    (   self	,  __lowerCAmelCase=None							):



				'''simple docstring'''







				if frame is not None:
					self.expand_frame(__lowerCAmelCase							)
				self.frames.append('''\n'''.join(self.frame							)							)
				lowerCamelCase__        =		[]  # start a new frame





			def 		__lowerCamelCase    (   self	,  __lowerCAmelCase							):



				'''simple docstring'''







				self.frame.append(__lowerCAmelCase							)





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







				print('''\n'''.join(self.frames							)							)
				lowerCamelCase__        =		[]





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







				lowerCamelCase__        =		[]





			def 		__lowerCamelCase    (   self							):



				'''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'''							)
				lowerCamelCase__        =		[]





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







				lowerCamelCase__        =		{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 		__lowerCamelCase    (   self	,  __lowerCAmelCase	,  __lowerCAmelCase							):



				'''simple docstring'''







				if torch.is_tensor(__lowerCAmelCase							):
					self.expand_frame(get_abs_min_max(__lowerCAmelCase	,  __lowerCAmelCase							)							)
					if detect_overflow(__lowerCAmelCase	,  __lowerCAmelCase							):
						lowerCamelCase__        =		True
				elif var is None:
					self.expand_frame(F'{"None":>17} {ctx}'							)
				else:
					self.expand_frame(F'{"not a tensor":>17} {ctx}'							)





			def 		__lowerCamelCase    (   self							):



				'''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 		__lowerCamelCase    (   self							):



				'''simple docstring'''







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





			def 		__lowerCamelCase    (   self	,  __lowerCAmelCase	,  __lowerCAmelCase	,  __lowerCAmelCase							):



				'''simple docstring'''







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

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

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

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

				self.save_frame()





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







				self.model.apply(self._register_forward_hook							)





			def 		__lowerCamelCase    (   self	,  __lowerCAmelCase							):



				'''simple docstring'''







				module.register_forward_hook(self.forward_hook							)





			def 		__lowerCamelCase    (   self	,  __lowerCAmelCase	,  __lowerCAmelCase	,  __lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__        =		False

				lowerCamelCase__        =		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
					lowerCamelCase__        =		True

				self.create_frame(__lowerCAmelCase	,  __lowerCAmelCase	,  __lowerCAmelCase							)

				# 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 lowerCAmelCase__(__snake_case	,__snake_case							)    ->				Dict:







	'''simple docstring'''







	lowerCamelCase__        =		var.abs()
	return F'{abs_var.min():8.2e} {abs_var.max():8.2e} {ctx}'





def lowerCAmelCase__(__snake_case	,__snake_case							)    ->				str:







	'''simple docstring'''







	lowerCamelCase__        =		False
	if torch.isnan(__snake_case							).any().item():
		lowerCamelCase__        =		True
		print(F'{ctx} has nans'							)
	if torch.isinf(__snake_case							).any().item():
		lowerCamelCase__        =		True
		print(F'{ctx} has infs'							)

	# if needed to monitor large elements can enable the following
	if 0:  # and detected:
		lowerCamelCase__        =		var[torch.ge(var.abs()	,100							)]
		if naaa.numel() > 0:
			print(F'{ctx}:  n100={naaa.numel()}'							)
		lowerCamelCase__        =		var[torch.ge(var.abs()	,1000							)]
		if naaaa.numel() > 0:
			print(F'{ctx}: n1000={naaaa.numel()}'							)
		lowerCamelCase__        =		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  __A   (   lowerCAmelCase			):

			'''simple docstring'''







			lowerCAmelCase_												=						"""underflow_overflow"""
			lowerCAmelCase_												=						"""tpu_metrics_debug"""










from ...configuration_utils import PretrainedConfig
from ...utils import logging


_a									=				logging.get_logger(__name__)

_a									=				{
    "google/pegasus-x-base": "https://huggingface.co/google/pegasus-x-base/resolve/main/config.json",
    "google/pegasus-x-large": "https://huggingface.co/google/pegasus-x-large/resolve/main/config.json",
    # See all PEGASUS-X models at https://huggingface.co/models?filter=pegasus-x
}






class  __A   (   lowerCAmelCase			):

			'''simple docstring'''







			lowerCAmelCase_												=						"""pegasus_x"""
			lowerCAmelCase_												=						["""past_key_values"""]
			lowerCAmelCase_												=						{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}





			def __init__(   self	,  __lowerCAmelCase=9_6_1_0_3	,  __lowerCAmelCase=1_6_3_8_4	,  __lowerCAmelCase=1_6	,  __lowerCAmelCase=4_0_9_6	,  __lowerCAmelCase=1_6	,  __lowerCAmelCase=1_6	,  __lowerCAmelCase=4_0_9_6	,  __lowerCAmelCase=1_6	,  __lowerCAmelCase=0.0	,  __lowerCAmelCase=0.0	,  __lowerCAmelCase=True	,  __lowerCAmelCase=True	,  __lowerCAmelCase="gelu"	,  __lowerCAmelCase=1_0_2_4	,  __lowerCAmelCase=0.1	,  __lowerCAmelCase=0.0	,  __lowerCAmelCase=0.0	,  __lowerCAmelCase=0.02	,  __lowerCAmelCase=0	,  __lowerCAmelCase=True	,  __lowerCAmelCase=0	,  __lowerCAmelCase=1	,  __lowerCAmelCase=1	,  __lowerCAmelCase=3_2	,  __lowerCAmelCase=5_1_2	,  __lowerCAmelCase=True	,  **__lowerCAmelCase	,  ):



				'''simple docstring'''







				lowerCamelCase__        =		vocab_size
				lowerCamelCase__        =		max_position_embeddings
				lowerCamelCase__        =		d_model
				lowerCamelCase__        =		encoder_ffn_dim
				lowerCamelCase__        =		encoder_layers
				lowerCamelCase__        =		encoder_attention_heads
				lowerCamelCase__        =		decoder_ffn_dim
				lowerCamelCase__        =		decoder_layers
				lowerCamelCase__        =		decoder_attention_heads
				lowerCamelCase__        =		dropout
				lowerCamelCase__        =		attention_dropout
				lowerCamelCase__        =		activation_dropout
				lowerCamelCase__        =		activation_function
				lowerCamelCase__        =		init_std
				lowerCamelCase__        =		encoder_layerdrop
				lowerCamelCase__        =		decoder_layerdrop
				lowerCamelCase__        =		use_cache
				lowerCamelCase__        =		encoder_layers
				lowerCamelCase__        =		scale_embedding  # scale factor will be sqrt(d_model) if True

				lowerCamelCase__        =		num_global_tokens
				lowerCamelCase__        =		block_size
				lowerCamelCase__        =		stagger_local_blocks

				super().__init__(
				    pad_token_id=__lowerCAmelCase	,  eos_token_id=__lowerCAmelCase	,  is_encoder_decoder=__lowerCAmelCase	,  decoder_start_token_id=__lowerCAmelCase	,  forced_eos_token_id=__lowerCAmelCase	,  **__lowerCAmelCase	,  )





			@property
			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







				return self.encoder_attention_heads





			@property
			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







				return self.d_model



