
'''simple docstring'''







import collections

from .utils import ExplicitEnum, is_torch_available, logging


if is_torch_available():
					import torch


__SCREAMING_SNAKE_CASE		:  Dict        =      logging.get_logger(__name__)
class  SCREAMING_SNAKE_CASE__       :
	def __init__(				self  ,    __UpperCamelCase  ,    __UpperCamelCase=21  ,    __UpperCamelCase=[]  ,    __UpperCamelCase=None			):



					'''simple docstring'''






					__a       :     Union[str, Any]								=	model
					__a       :     Any								=	trace_batch_nums
					__a       :     Optional[int]								=	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([]  ,    __lowerCamelCase			)
					__a       :     Optional[Any]								=	[]
					__a       :     str								=	0
					__a       :     List[Any]								=	0
					__a       :     Tuple								=	False
					__a       :     List[str]								=	"""                 """

					self.analyse_model()

					self.register_forward_hook()
	def __lowerCamelCase    (				self  ,    __UpperCamelCase=None			):



					'''simple docstring'''






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



					'''simple docstring'''






					self.frame.append(__lowerCamelCase			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					print("""\n""".join(self.frames			)			)
					__a       :     List[str]								=	[]
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     Any								=	[]
	def __lowerCamelCase    (				self			):



					'''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 __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     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 __lowerCamelCase    (				self  ,    __UpperCamelCase  ,    __UpperCamelCase			):



					'''simple docstring'''






					if torch.is_tensor(__lowerCamelCase			):
									self.expand_frame(get_abs_min_max(__lowerCamelCase  ,    __lowerCamelCase			)			)
									if detect_overflow(__lowerCamelCase  ,    __lowerCamelCase			):
													__a       :     Optional[Any]								=	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			):



					'''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			):



					'''simple docstring'''






					self.expand_frame(f"""{self.prefix} *** Finished batch number={self.batch_number-1} ***\n\n"""			)
	def __lowerCamelCase    (				self  ,    __UpperCamelCase  ,    __UpperCamelCase  ,    __UpperCamelCase			):



					'''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			):



					'''simple docstring'''






					self.model.apply(self._register_forward_hook			)
	def __lowerCamelCase    (				self  ,    __UpperCamelCase			):



					'''simple docstring'''






					module.register_forward_hook(self.forward_hook			)

	def __lowerCamelCase    (				self  ,    __UpperCamelCase  ,    __UpperCamelCase  ,    __UpperCamelCase			):



					'''simple docstring'''






					__a       :     Optional[int]								=	False

					__a       :     Tuple								=	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       :     Optional[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 				_snake_case (		lowercase			,    lowercase						)       ->       Any:
				__a       :     Optional[int]								=	var.abs()
				return F"""{abs_var.min():8.2e} {abs_var.max():8.2e} {ctx}"""

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

				# if needed to monitor large elements can enable the following
				if 0:  # and detected:
								__a       :     Any								=	var[torch.ge(var.abs()			,    1_0_0						)]
								if naaa.numel() > 0:
												print(F"""{ctx}:  n100={naaa.numel()}"""						)
								__a       :     Tuple								=	var[torch.ge(var.abs()			,    1_0_0_0						)]
								if naaaa.numel() > 0:
												print(F"""{ctx}: n1000={naaaa.numel()}"""						)
								__a       :     Any								=	var[torch.ge(var.abs()			,    1_0_0_0_0						)]
								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__       (  snake_case_   ):
	lowercase__	    =							"underflow_overflow"
	lowercase__	    =							"tpu_metrics_debug"







"""simple docstring"""

from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import ConvNextConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
			import tensorflow as tf

			from transformers import TFConvNextForImageClassification, TFConvNextModel


if is_vision_available():
			from PIL import Image

			from transformers import ConvNextImageProcessor

class     A_				:







				def __init__(				self				:    Optional[int]    ,     __lowerCamelCase				:    Tuple    ,     __lowerCamelCase				:    int=1_3    ,     __lowerCamelCase				:    Any=3_2    ,     __lowerCamelCase				:    Dict=3    ,     __lowerCamelCase				:    List[Any]=4    ,     __lowerCamelCase				:    Union[str, Any]=[1_0, 2_0, 3_0, 4_0]    ,     __lowerCamelCase				:    int=[2, 2, 3, 2]    ,     __lowerCamelCase				:    str=True    ,     __lowerCamelCase				:    List[str]=True    ,     __lowerCamelCase				:    List[Any]=3_7    ,     __lowerCamelCase				:    Optional[Any]="gelu"    ,     __lowerCamelCase				:    str=1_0    ,     __lowerCamelCase				:    int=0.02    ,     __lowerCamelCase				:    Optional[int]=3    ,     __lowerCamelCase				:    Any=None    ,     )					->       List[Any]:
										__magic_name__      =  parent
										__magic_name__      =  batch_size
										__magic_name__      =  image_size
										__magic_name__      =  num_channels
										__magic_name__      =  num_stages
										__magic_name__      =  hidden_sizes
										__magic_name__      =  depths
										__magic_name__      =  is_training
										__magic_name__      =  use_labels
										__magic_name__      =  intermediate_size
										__magic_name__      =  hidden_act
										__magic_name__      =  type_sequence_label_size
										__magic_name__      =  initializer_range
										__magic_name__      =  scope







				def      _snake_case       (				self				:    Optional[Any]						)					->       int:
										__magic_name__      =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

										__magic_name__      =  None
										if self.use_labels:
																__magic_name__      =  ids_tensor([self.batch_size]    ,     self.type_sequence_label_size						)

										__magic_name__      =  self.get_config()

										return config, pixel_values, labels







				def      _snake_case       (				self				:    Optional[int]						)					->       Union[str, Any]:
										return ConvNextConfig(
										    num_channels=self.num_channels    ,     hidden_sizes=self.hidden_sizes    ,     depths=self.depths    ,     num_stages=self.num_stages    ,     hidden_act=self.hidden_act    ,     is_decoder=__lowerCamelCase    ,     initializer_range=self.initializer_range    ,     )







				def      _snake_case       (				self				:    List[Any]    ,     __lowerCamelCase				:    List[str]    ,     __lowerCamelCase				:    Union[str, Any]    ,     __lowerCamelCase				:    Optional[Any]						)					->       List[str]:
										__magic_name__      =  TFConvNextModel(config=__lowerCamelCase						)
										__magic_name__      =  model(__lowerCamelCase    ,     training=__lowerCamelCase						)
										# expected last hidden states: B, C, H // 32, W // 32
										self.parent.assertEqual(
										    result.last_hidden_state.shape    ,     (self.batch_size, self.hidden_sizes[-1], self.image_size // 3_2, self.image_size // 3_2)    ,     )







				def      _snake_case       (				self				:    List[Any]    ,     __lowerCamelCase				:    Union[str, Any]    ,     __lowerCamelCase				:    Tuple    ,     __lowerCamelCase				:    List[str]						)					->       int:
										__magic_name__      =  self.type_sequence_label_size
										__magic_name__      =  TFConvNextForImageClassification(__lowerCamelCase						)
										__magic_name__      =  model(__lowerCamelCase    ,     labels=__lowerCamelCase    ,     training=__lowerCamelCase						)
										self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.type_sequence_label_size)						)







				def      _snake_case       (				self				:    List[str]						)					->       str:
										__magic_name__      =  self.prepare_config_and_inputs()
										__magic_name__   ,     __magic_name__   ,     __magic_name__      =  config_and_inputs
										__magic_name__      =  {"pixel_values": pixel_values}
										return config, inputs_dict





@require_tf
class     A_				(   snake_case_       ,				snake_case_       ,				unittest.TestCase	):
				UpperCAmelCase__										=					(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
				UpperCAmelCase__										=					(
				    {'''feature-extraction''': TFConvNextModel, '''image-classification''': TFConvNextForImageClassification}
				    if is_tf_available()
				    else {}
				)

				UpperCAmelCase__										=					False
				UpperCAmelCase__										=					False
				UpperCAmelCase__										=					False
				UpperCAmelCase__										=					False
				UpperCAmelCase__										=					False







				def      _snake_case       (				self				:    str						)					->       str:
										__magic_name__      =  TFConvNextModelTester(self						)
										__magic_name__      =  ConfigTester(
										    self    ,     config_class=__lowerCamelCase    ,     has_text_modality=__lowerCamelCase    ,     hidden_size=3_7    ,     )







				@unittest.skip(reason="ConvNext does not use inputs_embeds"						)
				def      _snake_case       (				self				:    Dict						)					->       Dict:
										pass







				@unittest.skipIf(
				    not is_tf_available() or len(tf.config.list_physical_devices("GPU"						)						) == 0    ,     reason="TF does not support backprop for grouped convolutions on CPU."    ,     )
				@slow
				def      _snake_case       (				self				:    List[str]						)					->       Tuple:
										super().test_keras_fit()







				@unittest.skip(reason="ConvNext does not support input and output embeddings"						)
				def      _snake_case       (				self				:    str						)					->       Tuple:
										pass







				def      _snake_case       (				self				:    Union[str, Any]						)					->       Optional[int]:
										__magic_name__   ,     __magic_name__      =  self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																__magic_name__      =  model_class(__lowerCamelCase						)
																__magic_name__      =  inspect.signature(model.call						)
																# signature.parameters is an OrderedDict => so arg_names order is deterministic
																__magic_name__      =  [*signature.parameters.keys()]

																__magic_name__      =  ["pixel_values"]
																self.assertListEqual(arg_names[:1]    ,     __lowerCamelCase						)







				def      _snake_case       (				self				:    Dict						)					->       Optional[int]:
										__magic_name__      =  self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__lowerCamelCase						)







				@unittest.skipIf(
				    not is_tf_available() or len(tf.config.list_physical_devices("GPU"						)						) == 0    ,     reason="TF does not support backprop for grouped convolutions on CPU."    ,     )
				def      _snake_case       (				self				:    Optional[int]						)					->       Tuple:
										super().test_dataset_conversion()







				def      _snake_case       (				self				:    int						)					->       List[str]:
										def check_hidden_states_output(__lowerCamelCase				:    Union[str, Any]    ,     __lowerCamelCase				:    Dict    ,     __lowerCamelCase				:    List[str]						):
																__magic_name__      =  model_class(__lowerCamelCase						)

																__magic_name__      =  model(**self._prepare_for_class(__lowerCamelCase    ,     __lowerCamelCase						)						)
																__magic_name__      =  outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																__magic_name__      =  self.model_tester.num_stages
																self.assertEqual(len(__lowerCamelCase						)    ,     expected_num_stages + 1						)

																# ConvNext'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 // 4, self.model_tester.image_size // 4]    ,     )

										__magic_name__   ,     __magic_name__      =  self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																__magic_name__      =  True
																check_hidden_states_output(__lowerCamelCase    ,     __lowerCamelCase    ,     __lowerCamelCase						)

																# check that output_hidden_states also work using config
																del inputs_dict["output_hidden_states"]
																__magic_name__      =  True

																check_hidden_states_output(__lowerCamelCase    ,     __lowerCamelCase    ,     __lowerCamelCase						)







				def      _snake_case       (				self				:    List[str]						)					->       Dict:
										__magic_name__   ,     __magic_name__      =  self.model_tester.prepare_config_and_inputs_for_common()

										def check_equivalence(__lowerCamelCase				:    Dict    ,     __lowerCamelCase				:    Any    ,     __lowerCamelCase				:    Union[str, Any]    ,     __lowerCamelCase				:    Any={}						):
																__magic_name__      =  model(__lowerCamelCase    ,     return_dict=__lowerCamelCase    ,     **__lowerCamelCase						)
																__magic_name__      =  model(__lowerCamelCase    ,     return_dict=__lowerCamelCase    ,     **__lowerCamelCase						).to_tuple()

																def recursive_check(__lowerCamelCase				:    Union[str, Any]    ,     __lowerCamelCase				:    Union[str, Any]						):
																						if isinstance(__lowerCamelCase    ,     (List, Tuple)						):
																												for tuple_iterable_value, dict_iterable_value in zip(__lowerCamelCase    ,     __lowerCamelCase						):
																																		recursive_check(__lowerCamelCase    ,     __lowerCamelCase						)
																						elif tuple_object is None:
																												return
																						else:
																												self.assertTrue(
																												    all(tf.equal(__lowerCamelCase    ,     __lowerCamelCase						)						)    ,     msg=(
																												        "Tuple and dict output are not equal. Difference:"
																												        f''' {tf.math.reduce_max(tf.abs(tuple_object - dict_object						)						)}'''
																												    )    ,     )

																						recursive_check(__lowerCamelCase    ,     __lowerCamelCase						)

										for model_class in self.all_model_classes:
																__magic_name__      =  model_class(__lowerCamelCase						)

																__magic_name__      =  self._prepare_for_class(__lowerCamelCase    ,     __lowerCamelCase						)
																__magic_name__      =  self._prepare_for_class(__lowerCamelCase    ,     __lowerCamelCase						)
																check_equivalence(__lowerCamelCase    ,     __lowerCamelCase    ,     __lowerCamelCase						)

																__magic_name__      =  self._prepare_for_class(__lowerCamelCase    ,     __lowerCamelCase    ,     return_labels=__lowerCamelCase						)
																__magic_name__      =  self._prepare_for_class(__lowerCamelCase    ,     __lowerCamelCase    ,     return_labels=__lowerCamelCase						)
																check_equivalence(__lowerCamelCase    ,     __lowerCamelCase    ,     __lowerCamelCase						)

																__magic_name__      =  self._prepare_for_class(__lowerCamelCase    ,     __lowerCamelCase						)
																__magic_name__      =  self._prepare_for_class(__lowerCamelCase    ,     __lowerCamelCase						)
																check_equivalence(__lowerCamelCase    ,     __lowerCamelCase    ,     __lowerCamelCase    ,     {"output_hidden_states": True}						)

																__magic_name__      =  self._prepare_for_class(__lowerCamelCase    ,     __lowerCamelCase    ,     return_labels=__lowerCamelCase						)
																__magic_name__      =  self._prepare_for_class(__lowerCamelCase    ,     __lowerCamelCase    ,     return_labels=__lowerCamelCase						)
																check_equivalence(__lowerCamelCase    ,     __lowerCamelCase    ,     __lowerCamelCase    ,     {"output_hidden_states": True}						)







				def      _snake_case       (				self				:    Union[str, Any]						)					->       Dict:
										__magic_name__      =  self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase						)







				@slow
				def      _snake_case       (				self				:    List[str]						)					->       Tuple:
										__magic_name__      =  TFConvNextModel.from_pretrained("facebook/convnext-tiny-224"						)
										self.assertIsNotNone(__lowerCamelCase						)






def _lowerCAmelCase						(	):

						'''simple docstring'''






						__magic_name__      =  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
						return image


@require_tf
@require_vision
class     A_				(   unittest.TestCase	):







				@cached_property
				def      _snake_case       (				self				:    Tuple						)					->       Any:
										return ConvNextImageProcessor.from_pretrained("facebook/convnext-tiny-224"						) if is_vision_available() else None







				@slow
				def      _snake_case       (				self				:    Any						)					->       str:
										__magic_name__      =  TFConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224"						)

										__magic_name__      =  self.default_image_processor
										__magic_name__      =  prepare_img()
										__magic_name__      =  image_processor(images=__lowerCamelCase    ,     return_tensors="tf"						)

										# forward pass
										__magic_name__      =  model(**__lowerCamelCase						)

										# verify the logits
										__magic_name__      =  tf.TensorShape((1, 1_0_0_0)						)
										self.assertEqual(outputs.logits.shape    ,     __lowerCamelCase						)

										__magic_name__      =  tf.constant([-0.0260, -0.4739, 0.1911]						)

										tf.debugging.assert_near(outputs.logits[0, :3]    ,     __lowerCamelCase    ,     atol=1e-4						)



