


import collections

from .utils import ExplicitEnum, is_torch_available, logging


if is_torch_available():
   import torch


A__:   Dict    =	logging.get_logger(__name__)







class A__	:






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




             '''simple docstring'''
             _a   :			str					 =model
             _a   :			str					 =trace_batch_nums
             _a   :			List[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
             _a   :			List[str]					 =collections.deque([]					,						SCREAMING_SNAKE_CASE     )
             _a   :			Union[str, Any]					 =[]
             _a   :			List[str]					 =0
             _a   :			List[str]					 =0
             _a   :			Any					 =False
             _a   :			Dict					 ="""                 """

             self.analyse_model()

             self.register_forward_hook()






      def __UpperCAmelCase	(			self      :Optional[Any]					,						SCREAMING_SNAKE_CASE      :Optional[int]=None     )	-> Any:




             '''simple docstring'''
             if frame is not None:
                    self.expand_frame(SCREAMING_SNAKE_CASE     )
             self.frames.append("""\n""".join(self.frame     )     )
             _a   :			str					 =[]  # start a new frame






      def __UpperCAmelCase	(			self      :int					,						SCREAMING_SNAKE_CASE      :str     )	-> str:




             '''simple docstring'''
             self.frame.append(SCREAMING_SNAKE_CASE     )






      def __UpperCAmelCase	(			self      :Dict     )	-> Dict:




             '''simple docstring'''
             print("""\n""".join(self.frames     )     )
             _a   :			Any					 =[]






      def __UpperCAmelCase	(			self      :List[Any]     )	-> List[Any]:




             '''simple docstring'''
             _a   :			Optional[int]					 =[]






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




             '''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"""     )
             _a   :			Tuple					 =[]






      def __UpperCAmelCase	(			self      :Optional[int]     )	-> Dict:




             '''simple docstring'''
             _a   :			Optional[Any]					 ={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      :Any					,						SCREAMING_SNAKE_CASE      :str					,						SCREAMING_SNAKE_CASE      :Tuple     )	-> Dict:




             '''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     ):
                           _a   :			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 __UpperCAmelCase	(			self      :List[Any]     )	-> 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 __UpperCAmelCase	(			self      :List[Any]     )	-> Optional[int]:




             '''simple docstring'''
             self.expand_frame(f"{self.prefix} *** Finished batch number={self.batch_number-1} ***\n\n"     )






      def __UpperCAmelCase	(			self      :Tuple					,						SCREAMING_SNAKE_CASE      :List[str]					,						SCREAMING_SNAKE_CASE      :Optional[int]					,						SCREAMING_SNAKE_CASE      :List[Any]     )	-> Dict:




             '''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 __UpperCAmelCase	(			self      :List[Any]     )	-> Optional[Any]:




             '''simple docstring'''
             self.model.apply(self._register_forward_hook     )






      def __UpperCAmelCase	(			self      :Optional[int]					,						SCREAMING_SNAKE_CASE      :List[Any]     )	-> int:




             '''simple docstring'''
             module.register_forward_hook(self.forward_hook     )






      def __UpperCAmelCase	(			self      :int					,						SCREAMING_SNAKE_CASE      :int					,						SCREAMING_SNAKE_CASE      :Optional[int]					,						SCREAMING_SNAKE_CASE      :int     )	-> Tuple:




             '''simple docstring'''
             _a   :			Optional[Any]					 =False

             _a   :			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
                    _a   :			Dict					 =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      SCREAMING_SNAKE_CASE_ (  _UpperCAmelCase     :     int   ,_UpperCAmelCase     :     Any  )  ->      Any:
       _a   :			str					 =var.abs()
       return F"{abs_var.min():8.2e} {abs_var.max():8.2e} {ctx}"


def      SCREAMING_SNAKE_CASE_ (  _UpperCAmelCase     :     Optional[int]   ,_UpperCAmelCase     :     Tuple  )  ->      Union[str, Any]:
       _a   :			Tuple					 =False
       if torch.isnan(_UpperCAmelCase  ).any().item():
              _a   :			Any					 =True
              print(F"{ctx} has nans"  )
       if torch.isinf(_UpperCAmelCase  ).any().item():
              _a   :			List[Any]					 =True
              print(F"{ctx} has infs"  )

       # if needed to monitor large elements can enable the following
       if 0:  # and detected:
              _a   :			List[str]					 =var[torch.ge(var.abs()   ,100  )]
              if naaa.numel() > 0:
                     print(F"{ctx}:  n100={naaa.numel()}"  )
              _a   :			Dict					 =var[torch.ge(var.abs()   ,1000  )]
              if naaaa.numel() > 0:
                     print(F"{ctx}: n1000={naaaa.numel()}"  )
              _a   :			Any					 =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__	(						UpperCAmelCase__				):
      __UpperCamelCase	:  Dict     	=							"underflow_overflow"
      __UpperCamelCase	:  Tuple     	=							"tpu_metrics_debug"






'''simple docstring'''







from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding







class A__	(						UpperCAmelCase__				):
      __UpperCamelCase	:  Optional[Any]     	=							["image_processor", "tokenizer"]
      __UpperCamelCase	:  Optional[Any]     	=							"EfficientNetImageProcessor"
      __UpperCamelCase	:  Union[str, Any]     	=							("BertTokenizer", "BertTokenizerFast")






      def __init__(			self      :Union[str, Any]					,						SCREAMING_SNAKE_CASE      :int					,						SCREAMING_SNAKE_CASE      :Optional[int]     )	-> List[str]:




             '''simple docstring'''
             super().__init__(SCREAMING_SNAKE_CASE					,						SCREAMING_SNAKE_CASE     )






      def __call__(			self      :str					,						SCREAMING_SNAKE_CASE      :List[Any]=None					,						SCREAMING_SNAKE_CASE      :Dict=None					,						SCREAMING_SNAKE_CASE      :Union[str, Any]="max_length"					,						SCREAMING_SNAKE_CASE      :Any=6_4					,						SCREAMING_SNAKE_CASE      :List[str]=None					,						**SCREAMING_SNAKE_CASE      :Union[str, Any]     )	-> Optional[int]:




             '''simple docstring'''
             if text is None and images is None:
                    raise ValueError("""You have to specify either text or images. Both cannot be none."""     )

             if text is not None:
                    _a   :			Dict					 =self.tokenizer(
                        SCREAMING_SNAKE_CASE					,						padding=SCREAMING_SNAKE_CASE					,						max_length=SCREAMING_SNAKE_CASE					,						return_tensors=SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )

             if images is not None:
                    _a   :			Any					 =self.image_processor(SCREAMING_SNAKE_CASE					,						return_tensors=SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )

             if text is not None and images is not None:
                    _a   :			Dict					 =image_features.pixel_values
                    return encoding
             elif text is not None:
                    return encoding
             else:
                    return BatchEncoding(data=dict(**SCREAMING_SNAKE_CASE     )					,						tensor_type=SCREAMING_SNAKE_CASE     )






      def __UpperCAmelCase	(			self      :Any					,						*SCREAMING_SNAKE_CASE      :Optional[int]					,						**SCREAMING_SNAKE_CASE      :str     )	-> Dict:




             '''simple docstring'''
             return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )






      def __UpperCAmelCase	(			self      :int					,						*SCREAMING_SNAKE_CASE      :List[str]					,						**SCREAMING_SNAKE_CASE      :Union[str, Any]     )	-> Union[str, Any]:




             '''simple docstring'''
             return self.tokenizer.decode(*SCREAMING_SNAKE_CASE					,						**SCREAMING_SNAKE_CASE     )






      @property
      def __UpperCAmelCase	(			self      :Tuple     )	-> Tuple:




             '''simple docstring'''
             _a   :			int					 =self.tokenizer.model_input_names
             _a   :			Union[str, Any]					 =self.image_processor.model_input_names
             return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names     )     )


