



import collections

from .utils import ExplicitEnum, is_torch_available, logging


if is_torch_available():
		import torch


__magic_name__       =     logging.get_logger(__name__)


class 	a__       :


					"""simple docstring"""



					def __init__(		self     :Dict      ,  lowercase__     :List[str]      ,  lowercase__     :Optional[int]=21      ,  lowercase__     :Any=[]      ,  lowercase__     :Optional[Any]=None    ):
										lowercase						   =       model
										lowercase						   =       trace_batch_nums
										lowercase						   =       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
										lowercase						   =       collections.deque([]      ,  lowercase__    )
										lowercase						   =       []
										lowercase						   =       0
										lowercase						   =       0
										lowercase						   =       False
										lowercase						   =       '                 '

										self.analyse_model()

										self.register_forward_hook()

					def 				__UpperCAmelCase		(		self     :Any      ,  lowercase__     :List[str]=None    ):
										if frame is not None:
															self.expand_frame(lowercase__    )
										self.frames.append('\n'.join(self.frame    )    )
										lowercase						   =       []  # start a new frame

					def 				__UpperCAmelCase		(		self     :Optional[int]      ,  lowercase__     :Any    ):
										self.frame.append(lowercase__    )

					def 				__UpperCAmelCase		(		self     :Union[str, Any]    ):
										print('\n'.join(self.frames    )    )
										lowercase						   =       []

					def 				__UpperCAmelCase		(		self     :Union[str, Any]    ):
										lowercase						   =       []

					def 				__UpperCAmelCase		(		self     :Any    ):
										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'    )
										lowercase						   =       []

					def 				__UpperCAmelCase		(		self     :List[str]    ):
										# 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
										lowercase						   =       {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     :Optional[Any]      ,  lowercase__     :Dict      ,  lowercase__     :List[str]    ):
										if torch.is_tensor(lowercase__    ):
															self.expand_frame(get_abs_min_max(lowercase__      ,  lowercase__    )    )
															if detect_overflow(lowercase__      ,  lowercase__    ):
																				lowercase						   =       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]    ):
										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     :int    ):
										self.expand_frame(F"""{self.prefix} *** Finished batch number={self.batch_number-1} ***\n\n"""    )

					def 				__UpperCAmelCase		(		self     :List[str]      ,  lowercase__     :Optional[Any]      ,  lowercase__     :Any      ,  lowercase__     :List[Any]    ):
										self.expand_frame(F"""{self.prefix} {self.module_names[module]} {module.__class__.__name__}"""    )

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

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

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

										self.save_frame()

					def 				__UpperCAmelCase		(		self     :List[str]    ):
										self.model.apply(self._register_forward_hook    )

					def 				__UpperCAmelCase		(		self     :List[Any]      ,  lowercase__     :Tuple    ):
										module.register_forward_hook(self.forward_hook    )



					def 				__UpperCAmelCase		(		self     :Optional[int]      ,  lowercase__     :int      ,  lowercase__     :List[str]      ,  lowercase__     :Optional[Any]    ):
										# - input is a tuple of packed inputs (could be non-Tensors)
										# - output could be a Tensor or a tuple of Tensors and non-Tensors

										lowercase						   =       False

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

										self.create_frame(lowercase__      ,  lowercase__      ,  lowercase__    )

										# 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 		__snake_case						(    _UpperCAmelCase       ,			_UpperCAmelCase				):
					"""simple docstring"""






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





def 		__snake_case						(    _UpperCAmelCase       ,			_UpperCAmelCase				):
					"""simple docstring"""






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

					# if needed to monitor large elements can enable the following
					if 0:  # and detected:
										lowercase						   =       var[torch.ge(var.abs()       ,			1_00				)]
										if naaa.numel() > 0:
															print(f"""{ctx}:  n100={naaa.numel()}"""				)
										lowercase						   =       var[torch.ge(var.abs()       ,			10_00				)]
										if naaaa.numel() > 0:
															print(f"""{ctx}: n1000={naaaa.numel()}"""				)
										lowercase						   =       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 	a__       (       _snake_case       ):


					"""simple docstring"""



					A__  :				int					 =				'''underflow_overflow'''
					A__  :				Optional[Any]					 =				'''tpu_metrics_debug'''











import os

from packaging import version

from .. import __version__
from .accelerate_utils import apply_forward_hook
from .constants import (
    CONFIG_NAME,
    DEPRECATED_REVISION_ARGS,
    DIFFUSERS_CACHE,
    DIFFUSERS_DYNAMIC_MODULE_NAME,
    FLAX_WEIGHTS_NAME,
    HF_MODULES_CACHE,
    HUGGINGFACE_CO_RESOLVE_ENDPOINT,
    ONNX_EXTERNAL_WEIGHTS_NAME,
    ONNX_WEIGHTS_NAME,
    SAFETENSORS_WEIGHTS_NAME,
    TEXT_ENCODER_ATTN_MODULE,
    WEIGHTS_NAME,
)
from .deprecation_utils import deprecate
from .doc_utils import replace_example_docstring
from .dynamic_modules_utils import get_class_from_dynamic_module
from .hub_utils import (
    HF_HUB_OFFLINE,
    _add_variant,
    _get_model_file,
    extract_commit_hash,
    http_user_agent,
)
from .import_utils import (
    BACKENDS_MAPPING,
    ENV_VARS_TRUE_AND_AUTO_VALUES,
    ENV_VARS_TRUE_VALUES,
    USE_JAX,
    USE_TF,
    USE_TORCH,
    DummyObject,
    OptionalDependencyNotAvailable,
    is_accelerate_available,
    is_accelerate_version,
    is_bsa_available,
    is_flax_available,
    is_ftfy_available,
    is_inflect_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_librosa_available,
    is_note_seq_available,
    is_omegaconf_available,
    is_onnx_available,
    is_safetensors_available,
    is_scipy_available,
    is_tensorboard_available,
    is_tf_available,
    is_torch_available,
    is_torch_version,
    is_torchsde_available,
    is_transformers_available,
    is_transformers_version,
    is_unidecode_available,
    is_wandb_available,
    is_xformers_available,
    requires_backends,
)
from .logging import get_logger
from .outputs import BaseOutput
from .pil_utils import PIL_INTERPOLATION, numpy_to_pil, pt_to_pil
from .torch_utils import is_compiled_module, randn_tensor


if is_torch_available():
		from .testing_utils import (
		    floats_tensor,
		    load_hf_numpy,
		    load_image,
		    load_numpy,
		    load_pt,
		    nightly,
		    parse_flag_from_env,
		    print_tensor_test,
		    require_torch_a,
		    require_torch_gpu,
		    skip_mps,
		    slow,
		    torch_all_close,
		    torch_device,
		)
		from .torch_utils import maybe_allow_in_graph

from .testing_utils import export_to_gif, export_to_video


__magic_name__       =     get_logger(__name__)





def 		__snake_case						(    _UpperCAmelCase				):
					"""simple docstring"""






					if version.parse(_UpperCAmelCase				) < version.parse(_UpperCAmelCase				):
										if "dev" in min_version:
															lowercase						   =       (
															    'This example requires a source install from HuggingFace diffusers (see '
															    '`https://huggingface.co/docs/diffusers/installation#install-from-source`),'
															)
										else:
															lowercase						   =       f"""This example requires a minimum version of {min_version},"""
										error_message += f""" but the version found is {__version__}.\n"""
										raise ImportError(_UpperCAmelCase				)






