code: 
  
  
  '''simple docstring'''
  
  
  
  import collections
  
  from .utils import ExplicitEnum, is_torch_available, logging
  
  
  if is_torch_available():
        import torch
  
  
  A__	:      Any				=						logging.get_logger(__name__)
  
  
  
  
  
  class      UpperCAmelCase_						:
  
  
  
  
  
      """simple docstring"""
  
  
  
  
  
  
      def __init__(							self    ,  SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_=21    ,  SCREAMING_SNAKE_CASE_=[]    ,  SCREAMING_SNAKE_CASE_=None			)       ->  List[Any]:
           __lowerCamelCase     :			Union[str, Any]              =	model
           __lowerCamelCase     :			Union[str, Any]              =	trace_batch_nums
           __lowerCamelCase     :			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
           __lowerCamelCase     :			int              =	collections.deque([]    ,  _lowerCAmelCase			)
           __lowerCamelCase     :			str              =	[]
           __lowerCamelCase     :			Optional[int]              =	0
           __lowerCamelCase     :			int              =	0
           __lowerCamelCase     :			int              =	False
           __lowerCamelCase     :			List[str]              =	"""                 """
  
           self.analyse_model()
  
           self.register_forward_hook()
  
  
  
  
  
  
  
      def    lowercase_       (							self    ,  SCREAMING_SNAKE_CASE_=None			)       ->  List[str]:
           if frame is not None:
                self.expand_frame(_lowerCAmelCase			)
           self.frames.append('\n'.join(self.frame			)			)
           __lowerCamelCase     :			str              =	[]  # start a new frame
  
  
  
  
  
  
  
      def    lowercase_       (							self    ,  SCREAMING_SNAKE_CASE_			)       ->  Optional[Any]:
           self.frame.append(_lowerCAmelCase			)
  
  
  
  
  
  
  
      def    lowercase_       (							self			)       ->  str:
           print('\n'.join(self.frames			)			)
           __lowerCamelCase     :			List[Any]              =	[]
  
  
  
  
  
  
  
      def    lowercase_       (							self			)       ->  List[Any]:
           __lowerCamelCase     :			Optional[int]              =	[]
  
  
  
  
  
  
  
      def    lowercase_       (							self			)       ->  Optional[int]:
           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     :			Any              =	[]
  
  
  
  
  
  
  
      def    lowercase_       (							self			)       ->  Tuple:
           # 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
           __lowerCamelCase     :			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    lowercase_       (							self    ,  SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_			)       ->  Union[str, Any]:
           if torch.is_tensor(_lowerCAmelCase			):
                self.expand_frame(get_abs_min_max(_lowerCAmelCase    ,  _lowerCAmelCase			)			)
                if detect_overflow(_lowerCAmelCase    ,  _lowerCAmelCase			):
                     __lowerCamelCase     :			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    lowercase_       (							self			)       ->  Tuple:
           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    lowercase_       (							self			)       ->  Dict:
           self.expand_frame(f'{self.prefix} *** Finished batch number={self.batch_number-1} ***\n\n'			)
  
  
  
  
  
  
  
      def    lowercase_       (							self    ,  SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_			)       ->  Optional[Any]:
           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    lowercase_       (							self			)       ->  int:
           self.model.apply(self._register_forward_hook			)
  
  
  
  
  
  
  
      def    lowercase_       (							self    ,  SCREAMING_SNAKE_CASE_			)       ->  int:
           module.register_forward_hook(self.forward_hook			)
  
  
  
  
  
  
  
      def    lowercase_       (							self    ,  SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_			)       ->  List[str]:
           # - input is a tuple of packed inputs (could be non-Tensors)
           # - output could be a Tensor or a tuple of Tensors and non-Tensors
  
           __lowerCamelCase     :			Union[str, Any]              =	False
  
           __lowerCamelCase     :			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
                __lowerCamelCase     :			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     UpperCAmelCase__      (			UpperCAmelCase_					:       str   ,					UpperCAmelCase_					:       Union[str, Any]							)  ->	Optional[Any]:
       __lowerCamelCase     :			Dict              =	var.abs()
       return F'{abs_var.min():8.2e} {abs_var.max():8.2e} {ctx}'
  
  
  
  def     UpperCAmelCase__      (			UpperCAmelCase_					:       List[Any]   ,					UpperCAmelCase_					:       Any							)  ->	int:
       __lowerCamelCase     :			str              =	False
       if torch.isnan(__lowerCamelCase							).any().item():
            __lowerCamelCase     :			List[Any]              =	True
            print(F'{ctx} has nans'							)
       if torch.isinf(__lowerCamelCase							).any().item():
            __lowerCamelCase     :			List[str]              =	True
            print(F'{ctx} has infs'							)
  
       # if needed to monitor large elements can enable the following
       if 0:  # and detected:
            __lowerCamelCase     :			Dict              =	var[torch.ge(var.abs()   ,					1_00							)]
            if naaa.numel() > 0:
                 print(F'{ctx}:  n100={naaa.numel()}'							)
            __lowerCamelCase     :			Tuple              =	var[torch.ge(var.abs()   ,					10_00							)]
            if naaaa.numel() > 0:
                 print(F'{ctx}: n1000={naaaa.numel()}'							)
            __lowerCamelCase     :			Optional[int]              =	var[torch.ge(var.abs()   ,					1_00_00							)]
            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						):
  
  
  
  
  
      """simple docstring"""
  
  
  
  
  
  
      lowerCamelCase						:      Dict							  =    'underflow_overflow'
      lowerCamelCase						:      str							  =    'tpu_metrics_debug'
style_context: 
  
  
  '''simple docstring'''
  
  
  
  import torch
  
  from diffusers import KDPMaAncestralDiscreteScheduler
  from diffusers.utils import torch_device
  
  from .test_schedulers import SchedulerCommonTest
  
  
  
  
  
  class      UpperCAmelCase_						(_UpperCAmelCase						):
  
  
  
  
  
      """simple docstring"""
  
  
  
  
  
  
      lowerCamelCase						:      Optional[Any]							  =    (KDPMaAncestralDiscreteScheduler,)
      lowerCamelCase						:      Tuple							  =    1_0
  
  
  
  
  
  
  
      def    lowercase_       (							self    ,  **SCREAMING_SNAKE_CASE_			)       ->  Tuple:
           __lowerCamelCase     :			Union[str, Any]              =	{
               'num_train_timesteps': 11_00,
               'beta_start': 0.0_0_0_1,
               'beta_end': 0.0_2,
               'beta_schedule': 'linear',
           }
  
           config.update(**SCREAMING_SNAKE_CASE_			)
           return config
  
  
  
  
  
  
  
      def    lowercase_       (							self			)       ->  Union[str, Any]:
           for timesteps in [10, 50, 1_00, 10_00]:
                self.check_over_configs(num_train_timesteps=SCREAMING_SNAKE_CASE_			)
  
  
  
  
  
  
  
      def    lowercase_       (							self			)       ->  Dict:
           for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1]    ,  [0.0_0_0_2, 0.0_0_2, 0.0_2]			):
                self.check_over_configs(beta_start=SCREAMING_SNAKE_CASE_    ,  beta_end=SCREAMING_SNAKE_CASE_			)
  
  
  
  
  
  
  
      def    lowercase_       (							self			)       ->  List[str]:
           for schedule in ["linear", "scaled_linear"]:
                self.check_over_configs(beta_schedule=SCREAMING_SNAKE_CASE_			)
  
  
  
  
  
  
  
      def    lowercase_       (							self			)       ->  int:
           if torch_device == "mps":
                return
           __lowerCamelCase     :			Union[str, Any]              =	self.scheduler_classes[0]
           __lowerCamelCase     :			Optional[Any]              =	self.get_scheduler_config()
           __lowerCamelCase     :			Optional[int]              =	scheduler_class(**SCREAMING_SNAKE_CASE_			)
  
           scheduler.set_timesteps(self.num_inference_steps			)
  
           __lowerCamelCase     :			int              =	torch.manual_seed(0			)
  
           __lowerCamelCase     :			int              =	self.dummy_model()
           __lowerCamelCase     :			List[Any]              =	self.dummy_sample_deter * scheduler.init_noise_sigma
           __lowerCamelCase     :			Any              =	sample.to(SCREAMING_SNAKE_CASE_			)
  
           for i, t in enumerate(scheduler.timesteps			):
                __lowerCamelCase     :			Dict              =	scheduler.scale_model_input(SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_			)
  
                __lowerCamelCase     :			Dict              =	model(SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_			)
  
                __lowerCamelCase     :			Union[str, Any]              =	scheduler.step(SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_    ,  generator=SCREAMING_SNAKE_CASE_			)
                __lowerCamelCase     :			Any              =	output.prev_sample
  
           __lowerCamelCase     :			Tuple              =	torch.sum(torch.abs(SCREAMING_SNAKE_CASE_			)			)
           __lowerCamelCase     :			Optional[int]              =	torch.mean(torch.abs(SCREAMING_SNAKE_CASE_			)			)
  
           assert abs(result_sum.item() - 1_3_8_4_9.3_8_7_7			) < 1E-2
           assert abs(result_mean.item() - 1_8.0_3_3_1			) < 5E-3
  
  
  
  
  
  
  
      def    lowercase_       (							self			)       ->  List[str]:
           for prediction_type in ["epsilon", "v_prediction"]:
                self.check_over_configs(prediction_type=SCREAMING_SNAKE_CASE_			)
  
  
  
  
  
  
  
      def    lowercase_       (							self			)       ->  List[str]:
           if torch_device == "mps":
                return
           __lowerCamelCase     :			Union[str, Any]              =	self.scheduler_classes[0]
           __lowerCamelCase     :			Optional[Any]              =	self.get_scheduler_config(prediction_type='v_prediction'			)
           __lowerCamelCase     :			str              =	scheduler_class(**SCREAMING_SNAKE_CASE_			)
  
           scheduler.set_timesteps(self.num_inference_steps			)
  
           __lowerCamelCase     :			int              =	self.dummy_model()
           __lowerCamelCase     :			int              =	self.dummy_sample_deter * scheduler.init_noise_sigma
           __lowerCamelCase     :			Dict              =	sample.to(SCREAMING_SNAKE_CASE_			)
  
           __lowerCamelCase     :			int              =	torch.manual_seed(0			)
  
           for i, t in enumerate(scheduler.timesteps			):
                __lowerCamelCase     :			Any              =	scheduler.scale_model_input(SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_			)
  
                __lowerCamelCase     :			List[str]              =	model(SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_			)
  
                __lowerCamelCase     :			str              =	scheduler.step(SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_    ,  generator=SCREAMING_SNAKE_CASE_			)
                __lowerCamelCase     :			Tuple              =	output.prev_sample
  
           __lowerCamelCase     :			int              =	torch.sum(torch.abs(SCREAMING_SNAKE_CASE_			)			)
           __lowerCamelCase     :			int              =	torch.mean(torch.abs(SCREAMING_SNAKE_CASE_			)			)
  
           assert abs(result_sum.item() - 3_2_8.9_9_7_0			) < 1E-2
           assert abs(result_mean.item() - 0.4_2_8_4			) < 1E-3
  
  
  
  
  
  
  
      def    lowercase_       (							self			)       ->  Dict:
           if torch_device == "mps":
                return
           __lowerCamelCase     :			Optional[int]              =	self.scheduler_classes[0]
           __lowerCamelCase     :			str              =	self.get_scheduler_config()
           __lowerCamelCase     :			Union[str, Any]              =	scheduler_class(**SCREAMING_SNAKE_CASE_			)
  
           scheduler.set_timesteps(self.num_inference_steps    ,  device=SCREAMING_SNAKE_CASE_			)
           __lowerCamelCase     :			List[Any]              =	torch.manual_seed(0			)
  
           __lowerCamelCase     :			Optional[Any]              =	self.dummy_model()
           __lowerCamelCase     :			Dict              =	self.dummy_sample_deter.to(SCREAMING_SNAKE_CASE_			) * scheduler.init_noise_sigma
  
           for t in scheduler.timesteps:
                __lowerCamelCase     :			Any              =	scheduler.scale_model_input(SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_			)
  
                __lowerCamelCase     :			Optional[Any]              =	model(SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_			)
  
                __lowerCamelCase     :			Dict              =	scheduler.step(SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_    ,  generator=SCREAMING_SNAKE_CASE_			)
                __lowerCamelCase     :			Union[str, Any]              =	output.prev_sample
  
           __lowerCamelCase     :			int              =	torch.sum(torch.abs(SCREAMING_SNAKE_CASE_			)			)
           __lowerCamelCase     :			Dict              =	torch.mean(torch.abs(SCREAMING_SNAKE_CASE_			)			)
  
           assert abs(result_sum.item() - 1_3_8_4_9.3_8_1_8			) < 1E-1
           assert abs(result_mean.item() - 1_8.0_3_3_1			) < 1E-3
  
  
  
  
  
  
  