code: 
  
  
  
  
  
  
  from __future__ import annotations
  
  import copy
  import os
  import tempfile
  from importlib import import_module
  from math import isnan
  
  from transformers import is_tf_available
  from transformers.models.auto import get_values
  from transformers.testing_utils import _tf_gpu_memory_limit, require_tf, slow
  
  from ..test_modeling_tf_common import ids_tensor
  
  
  if is_tf_available():
        import numpy as np
        import tensorflow as tf
  
        from transformers import (
            TF_MODEL_FOR_CAUSAL_LM_MAPPING,
            TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
            TF_MODEL_FOR_MASKED_LM_MAPPING,
            TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
            TF_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING,
            TF_MODEL_FOR_PRETRAINING_MAPPING,
            TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
            TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
            TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
            TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
            TFSharedEmbeddings,
        )
  
        if _tf_gpu_memory_limit is not None:
              SCREAMING_SNAKE_CASE__		:       Union[str, Any]   							=							tf.config.list_physical_devices("""GPU""")
              for gpu in gpus:
                    # Restrict TensorFlow to only allocate x GB of memory on the GPUs
                    try:
                          tf.config.set_logical_device_configuration(
                              gpu, [tf.config.LogicalDeviceConfiguration(memory_limit=_tf_gpu_memory_limit)]
                          )
                          SCREAMING_SNAKE_CASE__		:       int   							=							tf.config.list_logical_devices("""GPU""")
                          print("""Logical GPUs""", logical_gpus)
                    except RuntimeError as e:
                          # Virtual devices must be set before GPUs have been initialized
                          print(e)
  
  
  
  
  
  
  
  @require_tf
  class 							UpperCAmelCase_     :
       __lowerCamelCase										=     None
       __lowerCamelCase										=     ()
       __lowerCamelCase										=     ()
       __lowerCamelCase										=     True
       __lowerCamelCase										=     True
       __lowerCamelCase										=     True
       __lowerCamelCase										=     False
  
  
  
  
       def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase  ,	_lowerCAmelCase=False		):
            UpperCAmelCase__			:     List[Any]										= copy.deepcopy(_lowerCAmelCase		)
  
            if model_class in get_values(_lowerCAmelCase		):
                 UpperCAmelCase__			:     Any										= {
                     k: tf.tile(tf.expand_dims(_lowerCAmelCase  ,	1		)  ,	(1, self.model_tester.num_choices) + (1,) * (v.ndim - 1)		)
                     if isinstance(_lowerCAmelCase  ,	tf.Tensor		) and v.ndim > 0
                     else v
                     for k, v in inputs_dict.items()
                 }
  
            if return_labels:
                 if model_class in get_values(_lowerCAmelCase		):
                      UpperCAmelCase__			:     Dict										= tf.ones(self.model_tester.batch_size  ,	dtype=tf.intaa		)
                 elif model_class in get_values(_lowerCAmelCase		):
                      UpperCAmelCase__			:     Union[str, Any]										= tf.zeros(self.model_tester.batch_size  ,	dtype=tf.intaa		)
                      UpperCAmelCase__			:     Tuple										= tf.zeros(self.model_tester.batch_size  ,	dtype=tf.intaa		)
                 elif model_class in [
                     *get_values(_lowerCAmelCase		),
                     *get_values(_lowerCAmelCase		),
                 ]:
                      UpperCAmelCase__			:     List[str]										= tf.zeros(self.model_tester.batch_size  ,	dtype=tf.intaa		)
                 elif model_class in get_values(_lowerCAmelCase		):
                      UpperCAmelCase__			:     Union[str, Any]										= tf.zeros(self.model_tester.batch_size  ,	dtype=tf.intaa		)
                 elif model_class in [
                     *get_values(_lowerCAmelCase		),
                     *get_values(_lowerCAmelCase		),
                     *get_values(_lowerCAmelCase		),
                     *get_values(_lowerCAmelCase		),
                     *get_values(_lowerCAmelCase		),
                 ]:
                      UpperCAmelCase__			:     List[str]										= tf.zeros(
                          (self.model_tester.batch_size, self.model_tester.seq_length)  ,	dtype=tf.intaa		)
            return inputs_dict
  
  
  
  
       @slow
       def      __UpperCAmelCase     (							self		):
            UpperCAmelCase__			,		UpperCAmelCase__			:     Optional[int]										= self.model_tester.prepare_config_and_inputs_for_common()
            for model_class in self.all_model_classes[:2]:
                 UpperCAmelCase__			:     Optional[Any]										= self._prepare_for_class(_lowerCAmelCase  ,	_lowerCAmelCase		)
                 UpperCAmelCase__			:     Optional[int]										= model_class(_lowerCAmelCase		)
  
                 @tf.function
                 def run_in_graph_mode():
                      return model(_lowerCAmelCase		)
  
                 UpperCAmelCase__			:     Dict										= run_in_graph_mode()
                 self.assertIsNotNone(_lowerCAmelCase		)
  
  
  
  
       @slow
       def      __UpperCAmelCase     (							self		):
            UpperCAmelCase__			,		UpperCAmelCase__			:     Optional[Any]										= self.model_tester.prepare_config_and_inputs_for_common()
            for model_class in self.all_model_classes[:2]:
                 UpperCAmelCase__			:     Optional[int]										= self._prepare_for_class(_lowerCAmelCase  ,	_lowerCAmelCase		)
                 UpperCAmelCase__			:     Tuple										= model_class(_lowerCAmelCase		)
  
                 @tf.function(experimental_compile=_lowerCAmelCase		)
                 def run_in_graph_mode():
                      return model(_lowerCAmelCase		)
  
                 UpperCAmelCase__			:     int										= run_in_graph_mode()
                 self.assertIsNotNone(_lowerCAmelCase		)
  
  
  
  
       @slow
       def      __UpperCAmelCase     (							self		):
            # This is a copy of the test_keras_fit method, but we use XLA compilation instead of eager
            UpperCAmelCase__			,		UpperCAmelCase__			:     Dict										= self.model_tester.prepare_config_and_inputs_for_common()
            for model_class in self.all_model_classes[:2]:
                 UpperCAmelCase__			:     Optional[Any]										= model_class(_lowerCAmelCase		)
                 if getattr(_lowerCAmelCase  ,	"""hf_compute_loss"""  ,	_lowerCAmelCase		):
                      # Test that model correctly compute the loss with kwargs
                      UpperCAmelCase__			:     Any										= self._prepare_for_class(inputs_dict.copy()  ,	_lowerCAmelCase  ,	return_labels=_lowerCAmelCase		)
                      # Is there a better way to remove these decoder inputs?
                      UpperCAmelCase__			:     Optional[Any]										= {
                          key: val
                          for key, val in prepared_for_class.items()
                          if key not in ("""head_mask""", """decoder_head_mask""", """cross_attn_head_mask""", """decoder_input_ids""")
                      }
  
                      UpperCAmelCase__			:     Optional[int]										= {
                          """labels""",
                          """label""",
                          """label_ids""",
                          """start_positions""",
                          """start_position""",
                          """end_positions""",
                          """end_position""",
                          """next_sentence_label""",
                      }
                      UpperCAmelCase__			:     Tuple										= possible_label_cols.intersection(set(_lowerCAmelCase		)		)
                      self.assertGreater(len(_lowerCAmelCase		)  ,	0  ,	msg="""No matching label names found!"""		)
                      UpperCAmelCase__			:     Any										= {key: val for key, val in prepared_for_class.items() if key in label_names}
                      UpperCAmelCase__			:     Dict										= {key: val for key, val in prepared_for_class.items() if key not in label_names}
                      self.assertGreater(len(_lowerCAmelCase		)  ,	0		)
  
                      # Make sure it works with XLA!
                      model.compile(optimizer=tf.keras.optimizers.SGD(0.0		)  ,	jit_compile=_lowerCAmelCase		)
                      # Make sure the model fits without crashing regardless of where we pass the labels
                      UpperCAmelCase__			:     Tuple										= model.fit(
                          _lowerCAmelCase  ,	validation_data=_lowerCAmelCase  ,	steps_per_epoch=1  ,	validation_steps=1  ,	shuffle=_lowerCAmelCase  ,	verbose=0  ,	)
                      UpperCAmelCase__			:     Any										= history.history["""loss"""][0]
                      self.assertTrue(not isnan(_lowerCAmelCase		)		)
                      UpperCAmelCase__			:     Tuple										= history.history["""val_loss"""][0]
                      self.assertTrue(not isnan(_lowerCAmelCase		)		)
  
                      # Now test it with separate labels, to make sure that path works in XLA too.
                      UpperCAmelCase__			:     Tuple										= model_class(_lowerCAmelCase		)
                      model.compile(optimizer=tf.keras.optimizers.SGD(0.0		)  ,	jit_compile=_lowerCAmelCase		)
                      UpperCAmelCase__			:     Optional[int]										= model.fit(
                          _lowerCAmelCase  ,	_lowerCAmelCase  ,	validation_data=(inputs_minus_labels, labels)  ,	steps_per_epoch=1  ,	validation_steps=1  ,	shuffle=_lowerCAmelCase  ,	verbose=0  ,	)
  
                      UpperCAmelCase__			:     List[Any]										= history.history["""loss"""][0]
                      self.assertTrue(not isnan(_lowerCAmelCase		)		)
                      UpperCAmelCase__			:     Any										= history.history["""val_loss"""][0]
                      self.assertTrue(not isnan(_lowerCAmelCase		)		)
  
  
  
  
       @slow
       def      __UpperCAmelCase     (							self		):
            UpperCAmelCase__			,		UpperCAmelCase__			:     Optional[int]										= self.model_tester.prepare_config_and_inputs_for_common()
            UpperCAmelCase__			:     Union[str, Any]										= True
            UpperCAmelCase__			:     Any										= True
  
            if hasattr(_lowerCAmelCase  ,	"""use_cache"""		):
                 UpperCAmelCase__			:     Optional[Any]										= True
  
            UpperCAmelCase__			:     Optional[int]										= getattr(self.model_tester  ,	"""encoder_seq_length"""  ,	self.model_tester.seq_length		)
            UpperCAmelCase__			:     List[str]										= getattr(self.model_tester  ,	"""key_length"""  ,	_lowerCAmelCase		)
  
            for model_class in self.all_model_classes[:2]:
                 UpperCAmelCase__			:     Union[str, Any]										= self._prepare_for_class(_lowerCAmelCase  ,	_lowerCAmelCase		)
                 UpperCAmelCase__			:     Union[str, Any]										= model_class(_lowerCAmelCase		)
                 model.build()
                 UpperCAmelCase__			:     Any										= len(model(_lowerCAmelCase		)		)
  
                 for key in list(class_inputs_dict.keys()		):
                      # Remove keys not in the serving signature, as the SavedModel will not be compiled to deal with them
                      if key not in model.input_signature:
                           del class_inputs_dict[key]
                      # Check it's a tensor, in case the inputs dict has some bools in it too
                      elif isinstance(class_inputs_dict[key]  ,	tf.Tensor		) and class_inputs_dict[key].dtype.is_integer:
                           UpperCAmelCase__			:     List[str]										= tf.cast(class_inputs_dict[key]  ,	tf.intaa		)
  
                 if set(class_inputs_dict.keys()		) != set(model.input_signature.keys()		):
                      continue  # Some models have inputs that the preparation functions don't create, we skip those
  
                 with tempfile.TemporaryDirectory() as tmpdirname:
                      model.save_pretrained(_lowerCAmelCase  ,	saved_model=_lowerCAmelCase		)
                      UpperCAmelCase__			:     Optional[int]										= os.path.join(_lowerCAmelCase  ,	"""saved_model"""  ,	"""1"""		)
                      UpperCAmelCase__			:     List[str]										= tf.keras.models.load_model(_lowerCAmelCase		)
                      UpperCAmelCase__			:     List[str]										= model(_lowerCAmelCase		)
  
                      if self.is_encoder_decoder:
                           UpperCAmelCase__			:     Union[str, Any]										= outputs["""encoder_hidden_states"""]
                           UpperCAmelCase__			:     Union[str, Any]										= outputs["""encoder_attentions"""]
                      else:
                           UpperCAmelCase__			:     Union[str, Any]										= outputs["""hidden_states"""]
                           UpperCAmelCase__			:     Dict										= outputs["""attentions"""]
  
                      self.assertEqual(len(_lowerCAmelCase		)  ,	_lowerCAmelCase		)
  
                      UpperCAmelCase__			:     List[Any]										= getattr(
                          self.model_tester  ,	"""expected_num_hidden_layers"""  ,	self.model_tester.num_hidden_layers + 1		)
  
                      self.assertEqual(len(_lowerCAmelCase		)  ,	_lowerCAmelCase		)
                      self.assertListEqual(
                          list(output_hidden_states[0].shape[-2:]		)  ,	[self.model_tester.seq_length, self.model_tester.hidden_size]  ,	)
  
                      self.assertEqual(len(_lowerCAmelCase		)  ,	self.model_tester.num_hidden_layers		)
                      self.assertListEqual(
                          list(output_attentions[0].shape[-3:]		)  ,	[self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length]  ,	)
  
  
  
  
       @slow
       def      __UpperCAmelCase     (							self		):
            tf.keras.mixed_precision.set_global_policy("""mixed_float16"""		)
  
            # try/finally block to ensure subsequent tests run in float32
            try:
                 UpperCAmelCase__			,		UpperCAmelCase__			:     Optional[int]										= self.model_tester.prepare_config_and_inputs_for_common()
                 for model_class in self.all_model_classes[:2]:
                      UpperCAmelCase__			:     Optional[int]										= self._prepare_for_class(_lowerCAmelCase  ,	_lowerCAmelCase		)
                      UpperCAmelCase__			:     Dict										= model_class(_lowerCAmelCase		)
                      UpperCAmelCase__			:     Optional[int]										= model(_lowerCAmelCase		)
  
                      self.assertIsNotNone(_lowerCAmelCase		)
            finally:
                 tf.keras.mixed_precision.set_global_policy("""float32"""		)
  
  
  
  
       @slow
       def      __UpperCAmelCase     (							self		):
            UpperCAmelCase__			,		UpperCAmelCase__			:     Optional[int]										= self.model_tester.prepare_config_and_inputs_for_common()
            # head_mask and decoder_head_mask has different shapes than other input args
            if "head_mask" in inputs_dict:
                 del inputs_dict["head_mask"]
            if "decoder_head_mask" in inputs_dict:
                 del inputs_dict["decoder_head_mask"]
            if "cross_attn_head_mask" in inputs_dict:
                 del inputs_dict["cross_attn_head_mask"]
            UpperCAmelCase__			:     str										= {
                module_member
                for model_class in self.all_model_classes
                for module in (import_module(model_class.__module__		),)
                for module_member_name in dir(_lowerCAmelCase		)
                if module_member_name.endswith("""MainLayer"""		)
                for module_member in (getattr(_lowerCAmelCase  ,	_lowerCAmelCase		),)
                if isinstance(_lowerCAmelCase  ,	_lowerCAmelCase		)
                and tf.keras.layers.Layer in module_member.__bases__
                and getattr(_lowerCAmelCase  ,	"""_keras_serializable"""  ,	_lowerCAmelCase		)
            }
  
            for main_layer_class in tf_main_layer_classes:
                 # T5MainLayer needs an embed_tokens parameter when called without the inputs_embeds parameter
                 if "T5" in main_layer_class.__name__:
                      # Take the same values than in TFT5ModelTester for this shared layer
                      UpperCAmelCase__			:     Tuple										= TFSharedEmbeddings(self.model_tester.vocab_size  ,	self.model_tester.hidden_size  ,	name="""shared"""		)
                      UpperCAmelCase__			:     List[str]										= False
                      UpperCAmelCase__			:     str										= main_layer_class(_lowerCAmelCase  ,	embed_tokens=_lowerCAmelCase		)
                 else:
                      UpperCAmelCase__			:     Dict										= main_layer_class(_lowerCAmelCase		)
  
                 UpperCAmelCase__			:     Tuple										= {
                     name: tf.keras.Input(tensor.shape[1:]  ,	dtype=tensor.dtype		) for name, tensor in inputs_dict.items()
                 }
  
                 if hasattr(self.model_tester  ,	"""num_labels"""		):
                      UpperCAmelCase__			:     Optional[int]										= self.model_tester.num_labels
                 else:
                      UpperCAmelCase__			:     Tuple										= 2
  
                 UpperCAmelCase__			:     Optional[int]										= tf.data.Dataset.from_tensor_slices(
                     (inputs_dict, np.ones((self.model_tester.batch_size, self.model_tester.seq_length, num_labels, 1)		))		).batch(1		)
  
                 UpperCAmelCase__			:     List[Any]										= main_layer(_lowerCAmelCase		)[0]
                 UpperCAmelCase__			:     Optional[int]										= tf.keras.layers.Dense(_lowerCAmelCase  ,	activation="""softmax"""  ,	name="""outputs"""		)(_lowerCAmelCase		)
                 UpperCAmelCase__			:     List[str]										= tf.keras.models.Model(inputs=_lowerCAmelCase  ,	outputs=[outputs]		)
  
                 model.compile(loss="""binary_crossentropy"""  ,	optimizer="""adam"""  ,	metrics=["""binary_accuracy"""]		)
                 model.fit(_lowerCAmelCase  ,	epochs=1		)
  
                 with tempfile.TemporaryDirectory() as tmpdirname:
                      UpperCAmelCase__			:     str										= os.path.join(_lowerCAmelCase  ,	"""keras_model.h5"""		)
                      model.save(_lowerCAmelCase		)
                      if "T5" in main_layer_class.__name__:
                           UpperCAmelCase__			:     List[Any]										= tf.keras.models.load_model(
                               _lowerCAmelCase  ,	custom_objects={
                                   main_layer_class.__name__: main_layer_class,
                                   """TFSharedEmbeddings""": TFSharedEmbeddings,
                               }  ,	)
                      else:
                           UpperCAmelCase__			:     Any										= tf.keras.models.load_model(
                               _lowerCAmelCase  ,	custom_objects={main_layer_class.__name__: main_layer_class}		)
                      assert isinstance(_lowerCAmelCase  ,	tf.keras.Model		)
                      model(_lowerCAmelCase		)
  
  
  
  
       @slow
       def      __UpperCAmelCase     (							self		):
            UpperCAmelCase__			,		UpperCAmelCase__			:     Optional[Any]										= self.model_tester.prepare_config_and_inputs_for_common()
  
            for model_class in self.all_model_classes[:2]:
                 UpperCAmelCase__			:     Tuple										= model_class(_lowerCAmelCase		)
  
                 UpperCAmelCase__			:     List[str]										= copy.deepcopy(_lowerCAmelCase		)
  
                 if not self.is_encoder_decoder:
                      UpperCAmelCase__			:     List[Any]										= inputs["""input_ids"""]
                      del inputs["input_ids"]
                 else:
                      UpperCAmelCase__			:     str										= inputs["""input_ids"""]
                      UpperCAmelCase__			:     Optional[Any]										= inputs.get("""decoder_input_ids"""  ,	_lowerCAmelCase		)
                      del inputs["input_ids"]
                      inputs.pop("""decoder_input_ids"""  ,	_lowerCAmelCase		)
  
                 if not self.is_encoder_decoder:
                      UpperCAmelCase__			:     Any										= model.get_input_embeddings()(_lowerCAmelCase		)
                 else:
                      UpperCAmelCase__			:     int										= model.get_input_embeddings()(_lowerCAmelCase		)
                      UpperCAmelCase__			:     Dict										= model.get_input_embeddings()(_lowerCAmelCase		)
  
                 UpperCAmelCase__			:     Dict										= self._prepare_for_class(_lowerCAmelCase  ,	_lowerCAmelCase		)
  
                 @tf.function
                 def run_in_graph_mode():
                      return model(_lowerCAmelCase		)
  
                 UpperCAmelCase__			:     Union[str, Any]										= run_in_graph_mode()
                 self.assertIsNotNone(_lowerCAmelCase		)
  
  
  
  
       def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase		):
            # special tokens cannot be bad tokens
            UpperCAmelCase__			:     str										= []
            if model.config.bos_token_id is not None:
                 special_tokens.append(model.config.bos_token_id		)
            if model.config.pad_token_id is not None:
                 special_tokens.append(model.config.pad_token_id		)
            if model.config.eos_token_id is not None:
                 special_tokens.append(model.config.eos_token_id		)
  
            # create random bad tokens that are not special tokens
            UpperCAmelCase__			:     Tuple										= []
            while len(_lowerCAmelCase		) < num_bad_tokens:
                 UpperCAmelCase__			:     List[Any]										= tf.squeeze(ids_tensor((1, 1)  ,	self.model_tester.vocab_size		)  ,	0		).numpy()[0]
                 if token not in special_tokens:
                      bad_tokens.append(_lowerCAmelCase		)
            return bad_tokens
  
  
  
  
       def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase		):
            for token_id in output_ids[0].numpy().tolist():
                 self.assertGreaterEqual(_lowerCAmelCase  ,	0		)
                 self.assertLess(_lowerCAmelCase  ,	self.model_tester.vocab_size		)
  
  
  
  
       def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase		):
            # for all bad word tokens
            for bad_word_ids in bad_words_ids:
                 # for all slices in batch
                 for generated_ids_slice in generated_ids:
                      # for all word idx
                      for i in range(len(_lowerCAmelCase		)  ,	len(_lowerCAmelCase		)		):
                           # if tokens match
                           if generated_ids_slice[i - len(_lowerCAmelCase		) : i] == bad_word_ids:
                                return True
            return False
  
  
  
  
style_context: 
  
  
  
  
  
  
  import collections
  
  from .utils import ExplicitEnum, is_torch_available, logging
  
  
  if is_torch_available():
        import torch
  
  
  SCREAMING_SNAKE_CASE__		:       Any   							=							logging.get_logger(__name__)
  
  
  
  
  
  
  
  class 							UpperCAmelCase_     :
  
  
  
  
       def __init__(							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase=21  ,	_lowerCAmelCase=[]  ,	_lowerCAmelCase=None		):
            UpperCAmelCase__			:     Union[str, Any]										= model
            UpperCAmelCase__			:     Union[str, Any]										= trace_batch_nums
            UpperCAmelCase__			:     Optional[Any]										= 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__			:     int										= collections.deque([]  ,	_lowerCAmelCase		)
            UpperCAmelCase__			:     str										= []
            UpperCAmelCase__			:     Optional[int]										= 0
            UpperCAmelCase__			:     int										= 0
            UpperCAmelCase__			:     int										= False
            UpperCAmelCase__			:     List[str]										= """                 """
  
            self.analyse_model()
  
            self.register_forward_hook()
  
  
  
  
       def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase=None		):
            if frame is not None:
                 self.expand_frame(_lowerCAmelCase		)
            self.frames.append("""\n""".join(self.frame		)		)
            UpperCAmelCase__			:     str										= []  # start a new frame
  
  
  
  
       def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase		):
            self.frame.append(_lowerCAmelCase		)
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            print("""\n""".join(self.frames		)		)
            UpperCAmelCase__			:     List[Any]										= []
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            UpperCAmelCase__			:     Optional[int]										= []
  
  
  
  
       def      __UpperCAmelCase     (							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__			:     Any										= []
  
  
  
  
       def      __UpperCAmelCase     (							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__			:     str										= {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      __UpperCAmelCase     (							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase		):
            if torch.is_tensor(_lowerCAmelCase		):
                 self.expand_frame(get_abs_min_max(_lowerCAmelCase  ,	_lowerCAmelCase		)		)
                 if detect_overflow(_lowerCAmelCase  ,	_lowerCAmelCase		):
                      UpperCAmelCase__			:     Optional[int]										= True
            elif var is None:
                 self.expand_frame(f"{'None':>17} {ctx}"		)
            else:
                 self.expand_frame(f"{'not a tensor':>17} {ctx}"		)
  
  
  
  
       def      __UpperCAmelCase     (							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      __UpperCAmelCase     (							self		):
            self.expand_frame(f"{self.prefix} *** Finished batch number={self.batch_number-1} ***\n\n"		)
  
  
  
  
       def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase  ,	_lowerCAmelCase		):
            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      __UpperCAmelCase     (							self		):
            self.model.apply(self._register_forward_hook		)
  
  
  
  
       def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase		):
            module.register_forward_hook(self.forward_hook		)
  
  
  
  
       def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase  ,	_lowerCAmelCase		):
            # - 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__			:     Union[str, Any]										= False
  
            UpperCAmelCase__			:     List[str]										= 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__			:     Dict										= 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	(	__lowerCamelCase   ,       __lowerCamelCase					)   -> Optional[Any]:
  
       '''simple docstring'''
       UpperCAmelCase__			:     Dict										= var.abs()
       return F"{abs_var.min():8.2e} {abs_var.max():8.2e} {ctx}"
  
  
  
  
  
  def    _lowerCamelCase	(	__lowerCamelCase   ,       __lowerCamelCase					)   -> int:
  
       '''simple docstring'''
       UpperCAmelCase__			:     str										= False
       if torch.isnan(__lowerCamelCase					).any().item():
            UpperCAmelCase__			:     List[Any]										= True
            print(F"{ctx} has nans"					)
       if torch.isinf(__lowerCamelCase					).any().item():
            UpperCAmelCase__			:     List[str]										= True
            print(F"{ctx} has infs"					)
  
       # if needed to monitor large elements can enable the following
       if 0:  # and detected:
            UpperCAmelCase__			:     Dict										= var[torch.ge(var.abs()   ,       100					)]
            if naaa.numel() > 0:
                 print(F"{ctx}:  n100={naaa.numel()}"					)
            UpperCAmelCase__			:     Tuple										= var[torch.ge(var.abs()   ,       1000					)]
            if naaaa.numel() > 0:
                 print(F"{ctx}: n1000={naaaa.numel()}"					)
            UpperCAmelCase__			:     Optional[int]										= var[torch.ge(var.abs()   ,       1_0000					)]
            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 							UpperCAmelCase_     (		__lowerCamelCase		):
       __lowerCamelCase										=     'underflow_overflow'
       __lowerCamelCase										=     'tpu_metrics_debug'
  
  
  
  
  
  
  