





import collections

from .utils import ExplicitEnum, is_torch_available, logging


if is_torch_available():
	import torch


a__		    =    logging.get_logger(__name__)


class       SCREAMING_SNAKE_CASE_							:



	"""simple docstring"""


	def __init__(				self		:	Optional[int]				,    lowerCAmelCase		:	Dict				,    lowerCAmelCase		:	Dict=21				,    lowerCAmelCase		:	List[str]=[]				,    lowerCAmelCase		:	Any=None  )  ->					Dict:







						"""simple docstring"""







						__UpperCamelCase				:  Optional[Any]								=			model
						__UpperCamelCase				:  Union[str, Any]								=			trace_batch_nums
						__UpperCamelCase				:  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				:  str								=			collections.deque([]				,    lowerCamelCase__  )
						__UpperCamelCase				:  List[Any]								=			[]
						__UpperCamelCase				:  Optional[int]								=			0
						__UpperCamelCase				:  Any								=			0
						__UpperCamelCase				:  List[str]								=			False
						__UpperCamelCase				:  Any								=			"                 "

						self.analyse_model()

						self.register_forward_hook()






	def 				lowerCamelCase__    (				self		:	List[str]				,    lowerCAmelCase		:	Dict=None  )  ->					int:







						"""simple docstring"""







						if frame is not None:
											self.expand_frame(lowerCamelCase__  )
						self.frames.append("""\n""".join(self.frame  )  )
						__UpperCamelCase				:  List[str]								=			[]  # start a new frame






	def 				lowerCamelCase__    (				self		:	Dict				,    lowerCAmelCase		:	Union[str, Any]  )  ->					str:







						"""simple docstring"""







						self.frame.append(lowerCamelCase__  )






	def 				lowerCamelCase__    (				self		:	Any  )  ->					Optional[Any]:







						"""simple docstring"""







						print("""\n""".join(self.frames  )  )
						__UpperCamelCase				:  str								=			[]






	def 				lowerCamelCase__    (				self		:	List[Any]  )  ->					Tuple:







						"""simple docstring"""







						__UpperCamelCase				:  str								=			[]






	def 				lowerCamelCase__    (				self		:	int  )  ->					List[str]:







						"""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"""  )
						__UpperCamelCase				:  List[str]								=			[]






	def 				lowerCamelCase__    (				self		:	Optional[int]  )  ->					Optional[Any]:







						"""simple docstring"""







						__UpperCamelCase				:  Union[str, 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 				lowerCamelCase__    (				self		:	Optional[int]				,    lowerCAmelCase		:	str				,    lowerCAmelCase		:	Optional[int]  )  ->					Optional[Any]:







						"""simple docstring"""







						if torch.is_tensor(lowerCamelCase__  ):
											self.expand_frame(get_abs_min_max(lowerCamelCase__				,    lowerCamelCase__  )  )
											if detect_overflow(lowerCamelCase__				,    lowerCamelCase__  ):
																__UpperCamelCase				:  Dict								=			True
						elif var is None:
											self.expand_frame(F'''{"None":>17} {ctx}'''  )
						else:
											self.expand_frame(F'''{"not a tensor":>17} {ctx}'''  )






	def 				lowerCamelCase__    (				self		:	List[str]  )  ->					str:







						"""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 				lowerCamelCase__    (				self		:	str  )  ->					Dict:







						"""simple docstring"""







						self.expand_frame(F'''{self.prefix} *** Finished batch number={self.batch_number-1} ***\n\n'''  )






	def 				lowerCamelCase__    (				self		:	Tuple				,    lowerCAmelCase		:	List[Any]				,    lowerCAmelCase		:	List[Any]				,    lowerCAmelCase		:	int  )  ->					Optional[Any]:







						"""simple docstring"""







						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 				lowerCamelCase__    (				self		:	str  )  ->					Optional[Any]:







						"""simple docstring"""







						self.model.apply(self._register_forward_hook  )






	def 				lowerCamelCase__    (				self		:	Optional[Any]				,    lowerCAmelCase		:	int  )  ->					Optional[int]:







						"""simple docstring"""







						module.register_forward_hook(self.forward_hook  )






	def 				lowerCamelCase__    (				self		:	Tuple				,    lowerCAmelCase		:	Any				,    lowerCAmelCase		:	Optional[Any]				,    lowerCAmelCase		:	Optional[int]  )  ->					Optional[Any]:







						"""simple docstring"""







						__UpperCamelCase				:  Optional[int]								=			False

						__UpperCamelCase				:  int								=			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				:  Any								=			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 							A__   (snake_case      :		Optional[int]		,  snake_case      :		Dict							)    ->	Union[str, Any]:
					__UpperCamelCase				:  Union[str, Any]								=			var.abs()
					return F'''{abs_var.min():8.2e} {abs_var.max():8.2e} {ctx}'''





def 							A__   (snake_case      :		Optional[Any]		,  snake_case      :		List[str]							)    ->	List[str]:
					__UpperCamelCase				:  Union[str, Any]								=			False
					if torch.isnan(snake_case							).any().item():
										__UpperCamelCase				:  Optional[int]								=			True
										print(F'''{ctx} has nans'''							)
					if torch.isinf(snake_case							).any().item():
										__UpperCamelCase				:  str								=			True
										print(F'''{ctx} has infs'''							)

					# if needed to monitor large elements can enable the following
					if 0:  # and detected:
										__UpperCamelCase				:  Any								=			var[torch.ge(var.abs()		,  1_00							)]
										if naaa.numel() > 0:
															print(F'''{ctx}:  n100={naaa.numel()}'''							)
										__UpperCamelCase				:  Dict								=			var[torch.ge(var.abs()		,  10_00							)]
										if naaaa.numel() > 0:
															print(F'''{ctx}: n1000={naaaa.numel()}'''							)
										__UpperCamelCase				:  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       SCREAMING_SNAKE_CASE_							(					UpperCAmelCase__      ):



	"""simple docstring"""


	__magic_name__      :					str			  =		'underflow_overflow'
	__magic_name__      :					Tuple			  =		'tpu_metrics_debug'












import inspect
import unittest

from transformers import EfficientNetConfig
from transformers.testing_utils import is_pipeline_test, require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import EfficientNetForImageClassification, EfficientNetModel
	from transformers.models.efficientnet.modeling_efficientnet import EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import AutoImageProcessor


class       SCREAMING_SNAKE_CASE_							:



	"""simple docstring"""


	def __init__(				self		:	Optional[int]				,    lowerCAmelCase		:	List[Any]				,    lowerCAmelCase		:	int=13				,    lowerCAmelCase		:	Union[str, Any]=32				,    lowerCAmelCase		:	List[Any]=3				,    lowerCAmelCase		:	Tuple=[3, 3, 5]				,    lowerCAmelCase		:	str=[32, 16, 24]				,    lowerCAmelCase		:	Dict=[16, 24, 40]				,    lowerCAmelCase		:	Optional[Any]=[1, 1, 2]				,    lowerCAmelCase		:	int=[1, 1, 2]				,    lowerCAmelCase		:	Any=[1, 6, 6]				,    lowerCAmelCase		:	str=True				,    lowerCAmelCase		:	Tuple=True				,    lowerCAmelCase		:	Dict=37				,    lowerCAmelCase		:	Any="gelu"				,    lowerCAmelCase		:	List[str]=10				,    )  ->					Optional[Any]:







						"""simple docstring"""







						__UpperCamelCase				:  List[Any]								=			parent
						__UpperCamelCase				:  Any								=			batch_size
						__UpperCamelCase				:  List[str]								=			image_size
						__UpperCamelCase				:  int								=			num_channels
						__UpperCamelCase				:  Tuple								=			kernel_sizes
						__UpperCamelCase				:  Union[str, Any]								=			in_channels
						__UpperCamelCase				:  int								=			out_channels
						__UpperCamelCase				:  List[str]								=			strides
						__UpperCamelCase				:  Optional[Any]								=			num_block_repeats
						__UpperCamelCase				:  Tuple								=			expand_ratios
						__UpperCamelCase				:  Optional[int]								=			is_training
						__UpperCamelCase				:  Dict								=			hidden_act
						__UpperCamelCase				:  Optional[int]								=			num_labels
						__UpperCamelCase				:  List[Any]								=			use_labels






	def 				lowerCamelCase__    (				self		:	Optional[Any]  )  ->					Tuple:







						"""simple docstring"""







						__UpperCamelCase				:  Tuple								=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

						__UpperCamelCase				:  int								=			None
						if self.use_labels:
											__UpperCamelCase				:  str								=			ids_tensor([self.batch_size]				,    self.num_labels  )

						__UpperCamelCase				:  int								=			self.get_config()
						return config, pixel_values, labels






	def 				lowerCamelCase__    (				self		:	Tuple  )  ->					Any:







						"""simple docstring"""







						return EfficientNetConfig(
						    num_channels=self.num_channels				,    kernel_sizes=self.kernel_sizes				,    in_channels=self.in_channels				,    out_channels=self.out_channels				,    strides=self.strides				,    num_block_repeats=self.num_block_repeats				,    expand_ratios=self.expand_ratios				,    hidden_act=self.hidden_act				,    num_labels=self.num_labels				,    )






	def 				lowerCamelCase__    (				self		:	Optional[int]				,    lowerCAmelCase		:	int				,    lowerCAmelCase		:	Tuple				,    lowerCAmelCase		:	Optional[Any]  )  ->					Union[str, Any]:







						"""simple docstring"""







						__UpperCamelCase				:  int								=			EfficientNetModel(config=lowerCAmelCase  )
						model.to(lowerCAmelCase  )
						model.eval()
						__UpperCamelCase				:  Union[str, Any]								=			model(lowerCAmelCase  )
						# expected last hidden states: B, C, H // 4, W // 4
						self.parent.assertEqual(
						    result.last_hidden_state.shape				,    (self.batch_size, config.hidden_dim, self.image_size // 4, self.image_size // 4)				,    )






	def 				lowerCamelCase__    (				self		:	int				,    lowerCAmelCase		:	Union[str, Any]				,    lowerCAmelCase		:	Optional[Any]				,    lowerCAmelCase		:	str  )  ->					Optional[int]:







						"""simple docstring"""







						__UpperCamelCase				:  List[Any]								=			EfficientNetForImageClassification(lowerCAmelCase  )
						model.to(lowerCAmelCase  )
						model.eval()
						__UpperCamelCase				:  Optional[int]								=			model(lowerCAmelCase				,    labels=lowerCAmelCase  )
						self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_labels)  )






	def 				lowerCamelCase__    (				self		:	Optional[Any]  )  ->					Optional[int]:







						"""simple docstring"""







						__UpperCamelCase				:  Optional[int]								=			self.prepare_config_and_inputs()
						__UpperCamelCase			, __UpperCamelCase			, __UpperCamelCase				:  str								=			config_and_inputs
						__UpperCamelCase				:  Optional[Any]								=			{"""pixel_values""": pixel_values}
						return config, inputs_dict






@require_torch
class       SCREAMING_SNAKE_CASE_							(					_UpperCamelCase  ,     _UpperCamelCase  ,     unittest.TestCase      ):



	"""simple docstring"""


	__magic_name__      :					List[Any]			  =		(EfficientNetModel, EfficientNetForImageClassification) if is_torch_available() else ()
	__magic_name__      :					Tuple			  =		(
	    {'feature-extraction': EfficientNetModel, 'image-classification': EfficientNetForImageClassification}
	    if is_torch_available()
	    else {}
	)

	__magic_name__      :					Union[str, Any]			  =		False
	__magic_name__      :					Dict			  =		False
	__magic_name__      :					str			  =		False
	__magic_name__      :					Union[str, Any]			  =		False
	__magic_name__      :					int			  =		False






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







						"""simple docstring"""







						__UpperCamelCase				:  List[str]								=			EfficientNetModelTester(self  )
						__UpperCamelCase				:  Optional[Any]								=			ConfigTester(
						    self				,    config_class=lowerCAmelCase				,    has_text_modality=lowerCAmelCase				,    hidden_size=37  )






	def 				lowerCamelCase__    (				self		:	List[str]  )  ->					int:







						"""simple docstring"""







						self.create_and_test_config_common_properties()
						self.config_tester.create_and_test_config_to_json_string()
						self.config_tester.create_and_test_config_to_json_file()
						self.config_tester.create_and_test_config_from_and_save_pretrained()
						self.config_tester.create_and_test_config_with_num_labels()
						self.config_tester.check_config_can_be_init_without_params()
						self.config_tester.check_config_arguments_init()






	def 				lowerCamelCase__    (				self		:	Optional[Any]  )  ->					Dict:







						"""simple docstring"""







						return






	@unittest.skip(reason="""EfficientNet does not use inputs_embeds"""  )
	def 				lowerCamelCase__    (				self		:	str  )  ->					List[str]:







						"""simple docstring"""







						pass






	@unittest.skip(reason="""EfficientNet does not support input and output embeddings"""  )
	def 				lowerCamelCase__    (				self		:	Optional[Any]  )  ->					Any:







						"""simple docstring"""







						pass






	@unittest.skip(reason="""EfficientNet does not use feedforward chunking"""  )
	def 				lowerCamelCase__    (				self		:	Tuple  )  ->					Optional[int]:







						"""simple docstring"""







						pass






	def 				lowerCamelCase__    (				self		:	Tuple  )  ->					List[str]:







						"""simple docstring"""







						__UpperCamelCase			, __UpperCamelCase				:  Dict								=			self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
											__UpperCamelCase				:  List[str]								=			model_class(lowerCAmelCase  )
											__UpperCamelCase				:  str								=			inspect.signature(model.forward  )
											# signature.parameters is an OrderedDict => so arg_names order is deterministic
											__UpperCamelCase				:  Tuple								=			[*signature.parameters.keys()]

											__UpperCamelCase				:  Union[str, Any]								=			["""pixel_values"""]
											self.assertListEqual(arg_names[:1]				,    lowerCAmelCase  )






	def 				lowerCamelCase__    (				self		:	Any  )  ->					Optional[int]:







						"""simple docstring"""







						__UpperCamelCase				:  Optional[int]								=			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowerCAmelCase  )






	def 				lowerCamelCase__    (				self		:	Tuple  )  ->					List[Any]:







						"""simple docstring"""







						def check_hidden_states_output(lowerCAmelCase		:	Optional[Any]				,    lowerCAmelCase		:	str				,    lowerCAmelCase		:	Any  ):
											__UpperCamelCase				:  int								=			model_class(lowerCAmelCase  )
											model.to(lowerCAmelCase  )
											model.eval()

											with torch.no_grad():
																__UpperCamelCase				:  Tuple								=			model(**self._prepare_for_class(lowerCAmelCase				,    lowerCAmelCase  )  )

											__UpperCamelCase				:  Any								=			outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states
											__UpperCamelCase				:  Union[str, Any]								=			sum(config.num_block_repeats  ) * 4
											self.assertEqual(len(lowerCAmelCase  )				,    lowerCAmelCase  )

											# EfficientNet's feature maps are of shape (batch_size, num_channels, height, width)
											self.assertListEqual(
											    list(hidden_states[0].shape[-2:]  )				,    [self.model_tester.image_size // 2, self.model_tester.image_size // 2]				,    )

						__UpperCamelCase			, __UpperCamelCase				:  Union[str, Any]								=			self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
											__UpperCamelCase				:  Union[str, Any]								=			True
											check_hidden_states_output(lowerCAmelCase				,    lowerCAmelCase				,    lowerCAmelCase  )

											# check that output_hidden_states also work using config
											del inputs_dict["output_hidden_states"]
											__UpperCamelCase				:  Optional[Any]								=			True

											check_hidden_states_output(lowerCAmelCase				,    lowerCAmelCase				,    lowerCAmelCase  )






	def 				lowerCamelCase__    (				self		:	List[str]  )  ->					List[Any]:







						"""simple docstring"""







						__UpperCamelCase				:  Optional[Any]								=			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase  )






	@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""  )
	def 				lowerCamelCase__    (				self		:	Optional[int]  )  ->					Dict:







						"""simple docstring"""







						pass






	@slow
	def 				lowerCamelCase__    (				self		:	str  )  ->					Any:







						"""simple docstring"""







						for model_name in EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__UpperCamelCase				:  Union[str, Any]								=			EfficientNetModel.from_pretrained(lowerCAmelCase  )
											self.assertIsNotNone(lowerCAmelCase  )






	@is_pipeline_test
	@require_vision
	@slow
	def 				lowerCamelCase__    (				self		:	List[str]  )  ->					Union[str, Any]:







						"""simple docstring"""







						super().test_pipeline_image_classification()






def 							A__   ()    ->	Union[str, Any]:
					__UpperCamelCase				:  List[str]								=			Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""							)
					return image







@require_torch
@require_vision
class       SCREAMING_SNAKE_CASE_							(					unittest.TestCase      ):



	"""simple docstring"""


	@cached_property
	def 				lowerCamelCase__    (				self		:	List[str]  )  ->					Union[str, Any]:







						"""simple docstring"""







						return AutoImageProcessor.from_pretrained("""google/efficientnet-b7"""  ) if is_vision_available() else None






	@slow
	def 				lowerCamelCase__    (				self		:	List[str]  )  ->					Dict:







						"""simple docstring"""







						__UpperCamelCase				:  Optional[Any]								=			EfficientNetForImageClassification.from_pretrained("""google/efficientnet-b7"""  ).to(lowerCAmelCase  )

						__UpperCamelCase				:  Any								=			self.default_image_processor
						__UpperCamelCase				:  Any								=			prepare_img()
						__UpperCamelCase				:  Optional[Any]								=			image_processor(images=lowerCAmelCase				,    return_tensors="""pt"""  ).to(lowerCAmelCase  )

						# forward pass
						with torch.no_grad():
											__UpperCamelCase				:  Tuple								=			model(**lowerCAmelCase  )

						# verify the logits
						__UpperCamelCase				:  Optional[int]								=			torch.Size((1, 1000)  )
						self.assertEqual(outputs.logits.shape				,    lowerCAmelCase  )

						__UpperCamelCase				:  int								=			torch.tensor([0.00_01, 0.00_02, 0.00_02]  ).to(lowerCAmelCase  )
						self.assertTrue(torch.allclose(outputs.logits[0, :3]				,    lowerCAmelCase				,    atol=1E-4  )  )






