


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




			def __init__(		self	:  List[Any]						,	lowerCamelCase__	:  Union[str, Any]						,	lowerCamelCase__	:  Tuple=13						,	lowerCamelCase__	:  Tuple=32						,	lowerCamelCase__	:  Any=3						,	lowerCamelCase__	:  List[str]=4						,	lowerCamelCase__	:  int=[10, 20, 30, 40]						,	lowerCamelCase__	:  Any=[2, 2, 3, 2]						,	lowerCamelCase__	:  Union[str, Any]=True						,	lowerCamelCase__	:  List[Any]=True						,	lowerCamelCase__	:  Optional[Any]=37						,	lowerCamelCase__	:  Optional[int]="gelu"						,	lowerCamelCase__	:  List[Any]=10						,	lowerCamelCase__	:  Tuple=0.0_2						,	lowerCamelCase__	:  Union[str, Any]=3						,	lowerCamelCase__	:  int=None						,	) ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase		:							Tuple             =   parent
					_UpperCAmelCase		:							int             =   batch_size
					_UpperCAmelCase		:							Any             =   image_size
					_UpperCAmelCase		:							Dict             =   num_channels
					_UpperCAmelCase		:							int             =   num_stages
					_UpperCAmelCase		:							Dict             =   hidden_sizes
					_UpperCAmelCase		:							int             =   depths
					_UpperCAmelCase		:							Union[str, Any]             =   is_training
					_UpperCAmelCase		:							str             =   use_labels
					_UpperCAmelCase		:							List[Any]             =   intermediate_size
					_UpperCAmelCase		:							str             =   hidden_act
					_UpperCAmelCase		:							str             =   type_sequence_label_size
					_UpperCAmelCase		:							Dict             =   initializer_range
					_UpperCAmelCase		:							Optional[int]             =   scope




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



					'''simple docstring'''







					_UpperCAmelCase		:							Optional[Any]             =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

					_UpperCAmelCase		:							Union[str, Any]             =   None
					if self.use_labels:
							_UpperCAmelCase		:							Dict             =   ids_tensor([self.batch_size]						,	self.type_sequence_label_size			)

					_UpperCAmelCase		:							Optional[Any]             =   self.get_config()

					return config, pixel_values, labels




			def 				lowerCAmelCase__     (		self	:  str			) ->Any:



					'''simple docstring'''







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



					'''simple docstring'''







					_UpperCAmelCase		:							int             =   TFConvNextModel(config=lowerCamelCase__			)
					_UpperCAmelCase		:							Union[str, Any]             =   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 // 32, self.image_size // 32)						,	)




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



					'''simple docstring'''







					_UpperCAmelCase		:							List[str]             =   self.type_sequence_label_size
					_UpperCAmelCase		:							Optional[Any]             =   TFConvNextForImageClassification(lowerCamelCase__			)
					_UpperCAmelCase		:							str             =   model(lowerCamelCase__						,	labels=lowerCamelCase__						,	training=lowerCamelCase__			)
					self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.type_sequence_label_size)			)






			def 				lowerCAmelCase__     (		self	:  Union[str, Any]			) ->Union[str, Any]:



					'''simple docstring'''







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






@require_tf
class 						lowerCAmelCase__      (      UpperCAmelCase__     ,							UpperCAmelCase__     ,							unittest.TestCase						):
			lowerCAmelCase			:						Union[str, Any]							 =  (TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
			lowerCAmelCase			:						List[Any]							 =  (
			    {"feature-extraction": TFConvNextModel, "image-classification": TFConvNextForImageClassification}
			    if is_tf_available()
			    else {}
			)

			lowerCAmelCase			:						List[str]							 =  False
			lowerCAmelCase			:						Dict							 =  False
			lowerCAmelCase			:						Optional[int]							 =  False
			lowerCAmelCase			:						Dict							 =  False
			lowerCAmelCase			:						str							 =  False




			def 				lowerCAmelCase__     (		self	:  List[str]			) ->Union[str, Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							Tuple             =   TFConvNextModelTester(self			)
					_UpperCAmelCase		:							int             =   ConfigTester(
					    self						,	config_class=lowerCamelCase__						,	has_text_modality=lowerCamelCase__						,	hidden_size=37						,	)




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



					'''simple docstring'''







					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 				lowerCAmelCase__     (		self	:  Tuple			) ->int:



					'''simple docstring'''







					super().test_keras_fit()




			@unittest.skip(reason="ConvNext does not support input and output embeddings"			)
			def 				lowerCAmelCase__     (		self	:  Union[str, Any]			) ->Optional[Any]:



					'''simple docstring'''







					pass




			def 				lowerCAmelCase__     (		self	:  Optional[int]			) ->int:



					'''simple docstring'''







					_UpperCAmelCase       ,     _UpperCAmelCase		:							int             =   self.model_tester.prepare_config_and_inputs_for_common()

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

							_UpperCAmelCase		:							List[str]             =   ["pixel_values"]
							self.assertListEqual(arg_names[:1]						,	lowerCamelCase__			)




			def 				lowerCAmelCase__     (		self	:  str			) ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase		:							List[str]             =   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 				lowerCAmelCase__     (		self	:  int			) ->List[Any]:



					'''simple docstring'''







					super().test_dataset_conversion()




			def 				lowerCAmelCase__     (		self	:  int			) ->List[Any]:



					'''simple docstring'''







					def check_hidden_states_output(lowerCamelCase__	:  List[Any]						,	lowerCamelCase__	:  Dict						,	lowerCamelCase__	:  Dict			):
							_UpperCAmelCase		:							List[Any]             =   model_class(lowerCamelCase__			)

							_UpperCAmelCase		:							Union[str, Any]             =   model(**self._prepare_for_class(lowerCamelCase__						,	lowerCamelCase__			)			)
							_UpperCAmelCase		:							List[Any]             =   outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

							_UpperCAmelCase		:							int             =   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]						,	)

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

					for model_class in self.all_model_classes:
							_UpperCAmelCase		:							str             =   True
							check_hidden_states_output(lowerCamelCase__						,	lowerCamelCase__						,	lowerCamelCase__			)

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

							check_hidden_states_output(lowerCamelCase__						,	lowerCamelCase__						,	lowerCamelCase__			)




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



					'''simple docstring'''







					_UpperCAmelCase       ,     _UpperCAmelCase		:							Any             =   self.model_tester.prepare_config_and_inputs_for_common()

					def check_equivalence(lowerCamelCase__	:  Union[str, Any]						,	lowerCamelCase__	:  Union[str, Any]						,	lowerCamelCase__	:  List[str]						,	lowerCamelCase__	:  Dict={}			):
							_UpperCAmelCase		:							Optional[int]             =   model(lowerCamelCase__						,	return_dict=lowerCamelCase__						,	**lowerCamelCase__			)
							_UpperCAmelCase		:							Optional[int]             =   model(lowerCamelCase__						,	return_dict=lowerCamelCase__						,	**lowerCamelCase__			).to_tuple()

							def recursive_check(lowerCamelCase__	:  Optional[Any]						,	lowerCamelCase__	:  Optional[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:
							_UpperCAmelCase		:							List[Any]             =   model_class(lowerCamelCase__			)

							_UpperCAmelCase		:							Any             =   self._prepare_for_class(lowerCamelCase__						,	lowerCamelCase__			)
							_UpperCAmelCase		:							Optional[Any]             =   self._prepare_for_class(lowerCamelCase__						,	lowerCamelCase__			)
							check_equivalence(lowerCamelCase__						,	lowerCamelCase__						,	lowerCamelCase__			)

							_UpperCAmelCase		:							str             =   self._prepare_for_class(lowerCamelCase__						,	lowerCamelCase__						,	return_labels=lowerCamelCase__			)
							_UpperCAmelCase		:							Optional[Any]             =   self._prepare_for_class(lowerCamelCase__						,	lowerCamelCase__						,	return_labels=lowerCamelCase__			)
							check_equivalence(lowerCamelCase__						,	lowerCamelCase__						,	lowerCamelCase__			)

							_UpperCAmelCase		:							Tuple             =   self._prepare_for_class(lowerCamelCase__						,	lowerCamelCase__			)
							_UpperCAmelCase		:							List[Any]             =   self._prepare_for_class(lowerCamelCase__						,	lowerCamelCase__			)
							check_equivalence(lowerCamelCase__						,	lowerCamelCase__						,	lowerCamelCase__						,	{"output_hidden_states": True}			)

							_UpperCAmelCase		:							List[Any]             =   self._prepare_for_class(lowerCamelCase__						,	lowerCamelCase__						,	return_labels=lowerCamelCase__			)
							_UpperCAmelCase		:							List[str]             =   self._prepare_for_class(lowerCamelCase__						,	lowerCamelCase__						,	return_labels=lowerCamelCase__			)
							check_equivalence(lowerCamelCase__						,	lowerCamelCase__						,	lowerCamelCase__						,	{"output_hidden_states": True}			)




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



					'''simple docstring'''







					_UpperCAmelCase		:							Optional[Any]             =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__			)






			@slow
			def 				lowerCAmelCase__     (		self	:  str			) ->int:



					'''simple docstring'''







					_UpperCAmelCase		:							Any             =   TFConvNextModel.from_pretrained("facebook/convnext-tiny-224"			)
					self.assertIsNotNone(lowerCamelCase__			)






def 			__lowerCAmelCase			():
		_UpperCAmelCase		:							List[Any]             =   Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
		return image






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




			@cached_property
			def 				lowerCAmelCase__     (		self	:  str			) ->Dict:



					'''simple docstring'''







					return ConvNextImageProcessor.from_pretrained("facebook/convnext-tiny-224"			) if is_vision_available() else None






			@slow
			def 				lowerCAmelCase__     (		self	:  int			) ->List[Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							int             =   TFConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224"			)

					_UpperCAmelCase		:							Optional[Any]             =   self.default_image_processor
					_UpperCAmelCase		:							Dict             =   prepare_img()
					_UpperCAmelCase		:							str             =   image_processor(images=lowerCamelCase__						,	return_tensors="tf"			)

					# forward pass
					_UpperCAmelCase		:							str             =   model(**lowerCamelCase__			)

					# verify the logits
					_UpperCAmelCase		:							int             =   tf.TensorShape((1, 10_00)			)
					self.assertEqual(outputs.logits.shape						,	lowerCamelCase__			)

					_UpperCAmelCase		:							Dict             =   tf.constant([-0.0_2_6_0, -0.4_7_3_9, 0.1_9_1_1]			)

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







'''simple docstring'''


import torch
from torch import autograd






class 						lowerCAmelCase__      (      autograd.Function						):




			@staticmethod
			def 				lowerCAmelCase__     (		lowerCamelCase__	:  Optional[int]						,	lowerCamelCase__	:  torch.tensor						,	lowerCamelCase__	:  float						,	lowerCamelCase__	:  bool			) ->Optional[Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							Optional[int]             =   inputs.numel()
					_UpperCAmelCase		:							Any             =   int(0.0_0_5 * nb_elems			) + 1
					if sigmoid:
							_UpperCAmelCase		:							List[str]             =   (torch.sigmoid(lowerCamelCase__			) > threshold).type(inputs.type()			)
					else:
							_UpperCAmelCase		:							List[Any]             =   (inputs > threshold).type(inputs.type()			)
					if mask.sum() < nb_min:
							# We limit the pruning so that at least 0.5% (half a percent) of the weights are remaining
							_UpperCAmelCase		:							str             =   inputs.flatten().kthvalue(max(nb_elems - nb_min						,	1			)			).values
							_UpperCAmelCase		:							str             =   (inputs > k_threshold).type(inputs.type()			)
					return mask






			@staticmethod
			def 				lowerCAmelCase__     (		lowerCamelCase__	:  List[Any]						,	lowerCamelCase__	:  Optional[int]			) ->Dict:



					'''simple docstring'''







					return gradOutput, None, None






class 						lowerCAmelCase__      (      autograd.Function						):




			@staticmethod
			def 				lowerCAmelCase__     (		lowerCamelCase__	:  Optional[int]						,	lowerCamelCase__	:  torch.tensor						,	lowerCamelCase__	:  float			) ->List[str]:



					'''simple docstring'''







					_UpperCAmelCase		:							Optional[int]             =   inputs.clone()
					_UpperCAmelCase       ,     _UpperCAmelCase		:							Tuple             =   inputs.flatten().sort(descending=lowerCamelCase__			)
					_UpperCAmelCase		:							Optional[int]             =   int(threshold * inputs.numel()			)

					# flat_out and mask access the same memory.
					_UpperCAmelCase		:							str             =   mask.flatten()
					_UpperCAmelCase		:							Optional[int]             =   0
					_UpperCAmelCase		:							Optional[Any]             =   1
					return mask






			@staticmethod
			def 				lowerCAmelCase__     (		lowerCamelCase__	:  Optional[int]						,	lowerCamelCase__	:  Any			) ->Union[str, Any]:



					'''simple docstring'''







					return gradOutput, None






class 						lowerCAmelCase__      (      UpperCAmelCase__						):




			@staticmethod
			def 				lowerCAmelCase__     (		lowerCamelCase__	:  torch.tensor						,	lowerCamelCase__	:  float			) ->Tuple:



					'''simple docstring'''







					_UpperCAmelCase		:							List[Any]             =   inputs.clone()
					_UpperCAmelCase       ,     _UpperCAmelCase		:							Optional[Any]             =   inputs.abs().flatten().sort(descending=lowerCamelCase__			)
					_UpperCAmelCase		:							int             =   int(threshold * inputs.numel()			)

					# flat_out and mask access the same memory.
					_UpperCAmelCase		:							int             =   mask.flatten()
					_UpperCAmelCase		:							List[Any]             =   0
					_UpperCAmelCase		:							int             =   1
					return mask



