
"""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 			_lowercase  :




	def __init__(					self						,					UpperCAmelCase_						,					UpperCAmelCase_=13						,					UpperCAmelCase_=32						,					UpperCAmelCase_=3						,					UpperCAmelCase_=4						,					UpperCAmelCase_=[10, 20, 30, 40]						,					UpperCAmelCase_=[2, 2, 3, 2]						,					UpperCAmelCase_=True						,					UpperCAmelCase_=True						,					UpperCAmelCase_=37						,					UpperCAmelCase_="gelu"						,					UpperCAmelCase_=10						,					UpperCAmelCase_=0.02						,					UpperCAmelCase_=3						,					UpperCAmelCase_=None						,					)						->				str:
			lowerCamelCase	:     Any            =       parent
			lowerCamelCase	:     Tuple            =       batch_size
			lowerCamelCase	:     Optional[int]            =       image_size
			lowerCamelCase	:     Optional[int]            =       num_channels
			lowerCamelCase	:     Optional[int]            =       num_stages
			lowerCamelCase	:     int            =       hidden_sizes
			lowerCamelCase	:     List[Any]            =       depths
			lowerCamelCase	:     Optional[Any]            =       is_training
			lowerCamelCase	:     Any            =       use_labels
			lowerCamelCase	:     List[Any]            =       intermediate_size
			lowerCamelCase	:     str            =       hidden_act
			lowerCamelCase	:     Dict            =       type_sequence_label_size
			lowerCamelCase	:     Optional[Any]            =       initializer_range
			lowerCamelCase	:     str            =       scope




	def      _UpperCamelCase       (					self   )						->				List[Any]:
			lowerCamelCase	:     List[str]            =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

			lowerCamelCase	:     Optional[Any]            =       None
			if self.use_labels:
					lowerCamelCase	:     List[Any]            =       ids_tensor([self.batch_size]						,					self.type_sequence_label_size   )

			lowerCamelCase	:     Tuple            =       self.get_config()

			return config, pixel_values, labels




	def      _UpperCamelCase       (					self   )						->				Dict:
			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=UpperCAmelCase_						,					initializer_range=self.initializer_range						,					)




	def      _UpperCamelCase       (					self						,					UpperCAmelCase_						,					UpperCAmelCase_						,					UpperCAmelCase_   )						->				Union[str, Any]:
			lowerCamelCase	:     Optional[Any]            =       TFConvNextModel(config=UpperCAmelCase_   )
			lowerCamelCase	:     Optional[Any]            =       model(UpperCAmelCase_						,					training=UpperCAmelCase_   )
			# 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 // 32, self.image_size // 32)						,					)




	def      _UpperCamelCase       (					self						,					UpperCAmelCase_						,					UpperCAmelCase_						,					UpperCAmelCase_   )						->				Optional[int]:
			lowerCamelCase	:     Optional[int]            =       self.type_sequence_label_size
			lowerCamelCase	:     Tuple            =       TFConvNextForImageClassification(UpperCAmelCase_   )
			lowerCamelCase	:     List[str]            =       model(UpperCAmelCase_						,					labels=UpperCAmelCase_						,					training=UpperCAmelCase_   )
			self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.type_sequence_label_size)   )




	def      _UpperCamelCase       (					self   )						->				Any:
			lowerCamelCase	:     int            =       self.prepare_config_and_inputs()
			lowerCamelCase      ,       lowerCamelCase      ,       lowerCamelCase	:     Union[str, Any]            =       config_and_inputs
			lowerCamelCase	:     Dict            =       {'pixel_values': pixel_values}
			return config, inputs_dict






@require_tf
class 			_lowercase  (							__UpperCAmelCase							,			__UpperCAmelCase							,			unittest.TestCase						):
	lowercase_								=      (TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
	lowercase_								=      (
	    {"""feature-extraction""": TFConvNextModel, """image-classification""": TFConvNextForImageClassification}
	    if is_tf_available()
	    else {}
	)

	lowercase_								=      False
	lowercase_								=      False
	lowercase_								=      False
	lowercase_								=      False
	lowercase_								=      False




	def      _UpperCamelCase       (					self   )						->				Optional[int]:
			lowerCamelCase	:     List[Any]            =       TFConvNextModelTester(self   )
			lowerCamelCase	:     str            =       ConfigTester(
			    self						,					config_class=UpperCAmelCase_						,					has_text_modality=UpperCAmelCase_						,					hidden_size=37						,					)




	@unittest.skip(reason='ConvNext does not use inputs_embeds'   )
	def      _UpperCamelCase       (					self   )						->				int:
			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      _UpperCamelCase       (					self   )						->				str:
			super().test_keras_fit()




	@unittest.skip(reason='ConvNext does not support input and output embeddings'   )
	def      _UpperCamelCase       (					self   )						->				Optional[Any]:
			pass




	def      _UpperCamelCase       (					self   )						->				List[str]:
			lowerCamelCase      ,       lowerCamelCase	:     Union[str, Any]            =       self.model_tester.prepare_config_and_inputs_for_common()

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

					lowerCamelCase	:     Any            =       ['pixel_values']
					self.assertListEqual(arg_names[:1]						,					UpperCAmelCase_   )




	def      _UpperCamelCase       (					self   )						->				Optional[int]:
			lowerCamelCase	:     Union[str, Any]            =       self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*UpperCAmelCase_   )




	@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      _UpperCamelCase       (					self   )						->				int:
			super().test_dataset_conversion()




	def      _UpperCamelCase       (					self   )						->				Any:
			def check_hidden_states_output(UpperCAmelCase_						,					UpperCAmelCase_						,					UpperCAmelCase_   ):
					lowerCamelCase	:     Tuple            =       model_class(UpperCAmelCase_   )

					lowerCamelCase	:     Dict            =       model(**self._prepare_for_class(UpperCAmelCase_						,					UpperCAmelCase_   )   )
					lowerCamelCase	:     int            =       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

					lowerCamelCase	:     int            =       self.model_tester.num_stages
					self.assertEqual(len(UpperCAmelCase_   )						,					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]						,					)

			lowerCamelCase      ,       lowerCamelCase	:     List[str]            =       self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
					lowerCamelCase	:     Tuple            =       True
					check_hidden_states_output(UpperCAmelCase_						,					UpperCAmelCase_						,					UpperCAmelCase_   )

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

					check_hidden_states_output(UpperCAmelCase_						,					UpperCAmelCase_						,					UpperCAmelCase_   )




	def      _UpperCamelCase       (					self   )						->				Union[str, Any]:
			lowerCamelCase      ,       lowerCamelCase	:     str            =       self.model_tester.prepare_config_and_inputs_for_common()

			def check_equivalence(UpperCAmelCase_						,					UpperCAmelCase_						,					UpperCAmelCase_						,					UpperCAmelCase_={}   ):
					lowerCamelCase	:     Tuple            =       model(UpperCAmelCase_						,					return_dict=UpperCAmelCase_						,					**UpperCAmelCase_   )
					lowerCamelCase	:     Optional[Any]            =       model(UpperCAmelCase_						,					return_dict=UpperCAmelCase_						,					**UpperCAmelCase_   ).to_tuple()

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

							recursive_check(UpperCAmelCase_						,					UpperCAmelCase_   )

			for model_class in self.all_model_classes:
					lowerCamelCase	:     Dict            =       model_class(UpperCAmelCase_   )

					lowerCamelCase	:     List[str]            =       self._prepare_for_class(UpperCAmelCase_						,					UpperCAmelCase_   )
					lowerCamelCase	:     Optional[Any]            =       self._prepare_for_class(UpperCAmelCase_						,					UpperCAmelCase_   )
					check_equivalence(UpperCAmelCase_						,					UpperCAmelCase_						,					UpperCAmelCase_   )

					lowerCamelCase	:     Optional[int]            =       self._prepare_for_class(UpperCAmelCase_						,					UpperCAmelCase_						,					return_labels=UpperCAmelCase_   )
					lowerCamelCase	:     Dict            =       self._prepare_for_class(UpperCAmelCase_						,					UpperCAmelCase_						,					return_labels=UpperCAmelCase_   )
					check_equivalence(UpperCAmelCase_						,					UpperCAmelCase_						,					UpperCAmelCase_   )

					lowerCamelCase	:     List[Any]            =       self._prepare_for_class(UpperCAmelCase_						,					UpperCAmelCase_   )
					lowerCamelCase	:     Union[str, Any]            =       self._prepare_for_class(UpperCAmelCase_						,					UpperCAmelCase_   )
					check_equivalence(UpperCAmelCase_						,					UpperCAmelCase_						,					UpperCAmelCase_						,					{'output_hidden_states': True}   )

					lowerCamelCase	:     List[str]            =       self._prepare_for_class(UpperCAmelCase_						,					UpperCAmelCase_						,					return_labels=UpperCAmelCase_   )
					lowerCamelCase	:     int            =       self._prepare_for_class(UpperCAmelCase_						,					UpperCAmelCase_						,					return_labels=UpperCAmelCase_   )
					check_equivalence(UpperCAmelCase_						,					UpperCAmelCase_						,					UpperCAmelCase_						,					{'output_hidden_states': True}   )




	def      _UpperCamelCase       (					self   )						->				Optional[int]:
			lowerCamelCase	:     Optional[Any]            =       self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_   )




	@slow
	def      _UpperCamelCase       (					self   )						->				int:
			lowerCamelCase	:     Union[str, Any]            =       TFConvNextModel.from_pretrained('facebook/convnext-tiny-224'   )
			self.assertIsNotNone(UpperCAmelCase_   )


def 						UpperCAmelCase   (						):





		'''simple docstring'''


		lowerCamelCase	:     int            =       Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'     )
		return image






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




	@cached_property
	def      _UpperCamelCase       (					self   )						->				Union[str, Any]:
			return ConvNextImageProcessor.from_pretrained('facebook/convnext-tiny-224'   ) if is_vision_available() else None




	@slow
	def      _UpperCamelCase       (					self   )						->				int:
			lowerCamelCase	:     Tuple            =       TFConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224'   )

			lowerCamelCase	:     str            =       self.default_image_processor
			lowerCamelCase	:     Union[str, Any]            =       prepare_img()
			lowerCamelCase	:     Tuple            =       image_processor(images=UpperCAmelCase_						,					return_tensors='tf'   )

			# forward pass
			lowerCamelCase	:     Tuple            =       model(**UpperCAmelCase_   )

			# verify the logits
			lowerCamelCase	:     Union[str, Any]            =       tf.TensorShape((1, 1000)   )
			self.assertEqual(outputs.logits.shape						,					UpperCAmelCase_   )

			lowerCamelCase	:     Tuple            =       tf.constant([-0.0260, -0.4739, 0.1911]   )

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












import collections

from .utils import ExplicitEnum, is_torch_available, logging


if is_torch_available():
		import torch


UpperCAmelCase_    =logging.get_logger(__name__)


class 				__UpperCamelCase      :





				'''simple docstring'''



				def __init__(			self							,			UpperCAmelCase_							,			UpperCAmelCase_=21							,			UpperCAmelCase_=[]							,			UpperCAmelCase_=None    ):
											lowerCAmelCase =		model
											lowerCAmelCase =		trace_batch_nums
											lowerCAmelCase =		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 =		collections.deque([]							,			UpperCAmelCase_    )
											lowerCAmelCase =		[]
											lowerCAmelCase =		0
											lowerCAmelCase =		0
											lowerCAmelCase =		False
											lowerCAmelCase =		'''                 '''

											self.analyse_model()

											self.register_forward_hook()



				def    __snake_case		(			self							,			UpperCAmelCase_=None    ):
											if frame is not None:
																		self.expand_frame(UpperCAmelCase_    )
											self.frames.append('''\n'''.join(self.frame    )    )
											lowerCAmelCase =		[]  # start a new frame



				def    __snake_case		(			self							,			UpperCAmelCase_    ):
											self.frame.append(UpperCAmelCase_    )



				def    __snake_case		(			self    ):
											print('''\n'''.join(self.frames    )    )
											lowerCAmelCase =		[]



				def    __snake_case		(			self    ):
											lowerCAmelCase =		[]



				def    __snake_case		(			self    ):
											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 =		[]



				def    __snake_case		(			self    ):
											# 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 =		{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    __snake_case		(			self							,			UpperCAmelCase_							,			UpperCAmelCase_    ):
											if torch.is_tensor(UpperCAmelCase_    ):
																		self.expand_frame(get_abs_min_max(UpperCAmelCase_							,			UpperCAmelCase_    )    )
																		if detect_overflow(UpperCAmelCase_							,			UpperCAmelCase_    ):
																									lowerCAmelCase =		True
											elif var is None:
																		self.expand_frame(F"""{"None":>17} {ctx}"""    )
											else:
																		self.expand_frame(F"""{"not a tensor":>17} {ctx}"""    )



				def    __snake_case		(			self    ):
											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    __snake_case		(			self    ):
											self.expand_frame(F"""{self.prefix} *** Finished batch number={self.batch_number-1} ***\n\n"""    )



				def    __snake_case		(			self							,			UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    ):
											self.expand_frame(F"""{self.prefix} {self.module_names[module]} {module.__class__.__name__}"""    )

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

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

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

											self.save_frame()



				def    __snake_case		(			self    ):
											self.model.apply(self._register_forward_hook    )



				def    __snake_case		(			self							,			UpperCAmelCase_    ):
											module.register_forward_hook(self.forward_hook    )






				def    __snake_case		(			self							,			UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    ):
											# - 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 =		False

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

											self.create_frame(UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    )

											# 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							(  _snake_case    ,			_snake_case				):
							lowerCAmelCase =		var.abs()
							return F"""{abs_var.min():8.2e} {abs_var.max():8.2e} {ctx}"""



def    UpperCAmelCase							(  _snake_case    ,			_snake_case				):
							lowerCAmelCase =		False
							if torch.isnan(_snake_case				).any().item():
														lowerCAmelCase =		True
														print(F"""{ctx} has nans"""				)
							if torch.isinf(_snake_case				).any().item():
														lowerCAmelCase =		True
														print(F"""{ctx} has infs"""				)

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





				'''simple docstring'''



				__a				:			Tuple					    ="""underflow_overflow"""
				__a				:			Dict					    ="""tpu_metrics_debug"""



