





"""simple docstring"""






import collections

from .utils import ExplicitEnum, is_torch_available, logging


if is_torch_available():
    import torch


lowerCAmelCase_      =				logging.get_logger(__name__)



class 						lowerCAmelCase       :






       def __init__( self     ,	a__     ,	a__=21     ,	a__=[]     ,	a__=None  ):
              _UpperCAmelCase			     =  model
              _UpperCAmelCase			     =  trace_batch_nums
              _UpperCAmelCase			     =  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			     =  collections.deque([]     ,	a__  )
              _UpperCAmelCase			     =  []
              _UpperCAmelCase			     =  0
              _UpperCAmelCase			     =  0
              _UpperCAmelCase			     =  False
              _UpperCAmelCase			     =  '                 '

              self.analyse_model()

              self.register_forward_hook()






       def 				__A			( self     ,	a__=None  ):
              if frame is not None:
                     self.expand_frame(a__  )
              self.frames.append('\n'.join(self.frame  )  )
              _UpperCAmelCase			     =  []  # start a new frame






       def 				__A			( self     ,	a__  ):
              self.frame.append(a__  )






       def 				__A			( self  ):
              print('\n'.join(self.frames  )  )
              _UpperCAmelCase			     =  []






       def 				__A			( self  ):
              _UpperCAmelCase			     =  []






       def 				__A			( self  ):
              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			     =  []






       def 				__A			( self  ):
              # extract the fully qualified module names, to be able to report at run time. e.g.:
              # encoder.block.2.layer.0.SelfAttention.o
              #
              # for shared weights only the first shared module name will be registered
              _UpperCAmelCase			     =  {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 				__A			( self     ,	a__     ,	a__  ):
              if torch.is_tensor(a__  ):
                     self.expand_frame(get_abs_min_max(a__     ,	a__  )  )
                     if detect_overflow(a__     ,	a__  ):
                            _UpperCAmelCase			     =  True
              elif var is None:
                     self.expand_frame(f"""{"None":>17} {ctx}"""  )
              else:
                     self.expand_frame(f"""{"not a tensor":>17} {ctx}"""  )






       def 				__A			( self  ):
              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 				__A			( self  ):
              self.expand_frame(f"""{self.prefix} *** Finished batch number={self.batch_number-1} ***\n\n"""  )






       def 				__A			( self     ,	a__     ,	a__     ,	a__  ):
              self.expand_frame(f"""{self.prefix} {self.module_names[module]} {module.__class__.__name__}"""  )

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

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

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

              self.save_frame()






       def 				__A			( self  ):
              self.model.apply(self._register_forward_hook  )






       def 				__A			( self     ,	a__  ):
              module.register_forward_hook(self.forward_hook  )






       def 				__A			( self     ,	a__     ,	a__     ,	a__  ):
              # - input is a tuple of packed inputs (could be non-Tensors)
              # - output could be a Tensor or a tuple of Tensors and non-Tensors

              _UpperCAmelCase			     =  False

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

              self.create_frame(a__     ,	a__     ,	a__  )

              # 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     (							SCREAMING_SNAKE_CASE,SCREAMING_SNAKE_CASE				)		->							Optional[int]:






       """simple docstring"""




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

def    __lowerCamelCase     (							SCREAMING_SNAKE_CASE,SCREAMING_SNAKE_CASE				)		->							Dict:






       """simple docstring"""




       _UpperCAmelCase			     =  False
       if torch.isnan(SCREAMING_SNAKE_CASE				).any().item():
              _UpperCAmelCase			     =  True
              print(F"""{ctx} has nans"""				)
       if torch.isinf(SCREAMING_SNAKE_CASE				).any().item():
              _UpperCAmelCase			     =  True
              print(F"""{ctx} has infs"""				)

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













"""simple docstring"""






from typing import Callable, Optional, Union

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


lowerCAmelCase_      =				logging.get_logger(__name__)

lowerCAmelCase_      =				{
    '''microsoft/prophetnet-large-uncased''': (
        '''https://huggingface.co/microsoft/prophetnet-large-uncased/resolve/main/config.json'''
    ),
}



class 						lowerCAmelCase       (   snake_case				):
       lowerCAmelCase__    			=	"""prophetnet"""
       lowerCAmelCase__    			=	["""past_key_values"""]
       lowerCAmelCase__    			=	{
           """num_attention_heads""": """num_encoder_attention_heads""",
       }






       def __init__( self     ,	a__ = 0.1     ,	a__ = "gelu"     ,	a__ = 3_05_22     ,	a__ = 10_24     ,	a__ = 40_96     ,	a__ = 12     ,	a__ = 16     ,	a__ = 40_96     ,	a__ = 12     ,	a__ = 16     ,	a__ = 0.1     ,	a__ = 0.1     ,	a__ = 5_12     ,	a__ = 0.02     ,	a__ = True     ,	a__ = True     ,	a__ = 0     ,	a__ = 2     ,	a__ = 32     ,	a__ = 1_28     ,	a__ = False     ,	a__ = 0.0     ,	a__ = True     ,	a__ = 0     ,	a__ = 1     ,	a__ = 2     ,	**a__     ,	):
              _UpperCAmelCase			     =  vocab_size
              _UpperCAmelCase			     =  hidden_size
              _UpperCAmelCase			     =  encoder_ffn_dim
              _UpperCAmelCase			     =  num_encoder_layers
              _UpperCAmelCase			     =  num_encoder_attention_heads
              _UpperCAmelCase			     =  decoder_ffn_dim
              _UpperCAmelCase			     =  num_decoder_layers
              _UpperCAmelCase			     =  num_decoder_attention_heads
              _UpperCAmelCase			     =  max_position_embeddings
              _UpperCAmelCase			     =  init_std  # Normal(0, this parameter)
              _UpperCAmelCase			     =  activation_function

              # parameters for prophetnet
              _UpperCAmelCase			     =  ngram
              _UpperCAmelCase			     =  num_buckets
              _UpperCAmelCase			     =  relative_max_distance
              _UpperCAmelCase			     =  disable_ngram_loss
              _UpperCAmelCase			     =  eps

              # 3 Types of Dropout
              _UpperCAmelCase			     =  attention_dropout
              _UpperCAmelCase			     =  activation_dropout
              _UpperCAmelCase			     =  dropout

              _UpperCAmelCase			     =  use_cache

              super().__init__(
                  pad_token_id=a__     ,	bos_token_id=a__     ,	eos_token_id=a__     ,	is_encoder_decoder=a__     ,	add_cross_attention=a__     ,	decoder_start_token_id=a__     ,	**a__     ,	)






       @property
       def 				__A			( self  ):
              return self.num_encoder_layers + self.num_decoder_layers






       @num_hidden_layers.setter
       def 				__A			( self     ,	a__  ):
              raise NotImplementedError(
                  'This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and'
                  ' `num_decoder_layers`.'  )






