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






							def __init__(       self    :      int    ,			snake_case__    :      int    ,			snake_case__    :      str=1_3    ,			snake_case__    :      List[Any]=3_2    ,			snake_case__    :      List[Any]=3    ,			snake_case__    :      Optional[int]=4    ,			snake_case__    :      List[Any]=[1_0, 2_0, 3_0, 4_0]    ,			snake_case__    :      Union[str, Any]=[2, 2, 3, 2]    ,			snake_case__    :      List[Any]=True    ,			snake_case__    :      Any=True    ,			snake_case__    :      Optional[Any]=3_7    ,			snake_case__    :      int="gelu"    ,			snake_case__    :      Any=1_0    ,			snake_case__    :      Dict=0.02    ,			snake_case__    :      Optional[int]=3    ,			snake_case__    :      Optional[int]=None    ,			):






												"""simple docstring"""



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






							def 				__lowercase	(       self    :      Optional[int]     ):






												"""simple docstring"""



												__a:    int		     =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

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

												__a:    Dict		     =       self.get_config()

												return config, pixel_values, labels






							def 				__lowercase	(       self    :      Tuple     ):






												"""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=snake_case__    ,			initializer_range=self.initializer_range    ,			)






							def 				__lowercase	(       self    :      str    ,			snake_case__    :      List[str]    ,			snake_case__    :      Optional[Any]    ,			snake_case__    :      Optional[int]     ):






												"""simple docstring"""



												__a:    Optional[Any]		     =       TFConvNextModel(config=snake_case__     )
												__a:    List[str]		     =       model(snake_case__    ,			training=snake_case__     )
												# 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 				__lowercase	(       self    :      Dict    ,			snake_case__    :      Dict    ,			snake_case__    :      str    ,			snake_case__    :      Union[str, Any]     ):






												"""simple docstring"""



												__a:    Any		     =       self.type_sequence_label_size
												__a:    Optional[int]		     =       TFConvNextForImageClassification(snake_case__     )
												__a:    Optional[int]		     =       model(snake_case__    ,			labels=snake_case__    ,			training=snake_case__     )
												self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.type_sequence_label_size)     )






							def 				__lowercase	(       self    :      Any     ):






												"""simple docstring"""



												__a:    Tuple		     =       self.prepare_config_and_inputs()
												__a     ,					__a     ,					__a:    int		     =       config_and_inputs
												__a:    Dict		     =       {"""pixel_values""": pixel_values}
												return config, inputs_dict






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

							SCREAMING_SNAKE_CASE		  =    False
							SCREAMING_SNAKE_CASE		  =    False
							SCREAMING_SNAKE_CASE		  =    False
							SCREAMING_SNAKE_CASE		  =    False
							SCREAMING_SNAKE_CASE		  =    False






							def 				__lowercase	(       self    :      Tuple     ):






												"""simple docstring"""



												__a:    int		     =       TFConvNextModelTester(self     )
												__a:    Optional[int]		     =       ConfigTester(
												    self    ,			config_class=snake_case__    ,			has_text_modality=snake_case__    ,			hidden_size=3_7    ,			)






							@unittest.skip(reason="""ConvNext does not use inputs_embeds"""     )
							def 				__lowercase	(       self    :      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 				__lowercase	(       self    :      Optional[Any]     ):






												"""simple docstring"""



												super().test_keras_fit()






							@unittest.skip(reason="""ConvNext does not support input and output embeddings"""     )
							def 				__lowercase	(       self    :      Dict     ):






												"""simple docstring"""



												pass






							def 				__lowercase	(       self    :      Tuple     ):






												"""simple docstring"""



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

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

																	__a:    Optional[int]		     =       ["""pixel_values"""]
																	self.assertListEqual(arg_names[:1]    ,			snake_case__     )






							def 				__lowercase	(       self    :      Dict     ):






												"""simple docstring"""



												__a:    Tuple		     =       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*snake_case__     )






							@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 				__lowercase	(       self    :      List[Any]     ):






												"""simple docstring"""



												super().test_dataset_conversion()






							def 				__lowercase	(       self    :      Union[str, Any]     ):






												"""simple docstring"""



												def check_hidden_states_output(snake_case__    :      Dict    ,			snake_case__    :      Tuple    ,			snake_case__    :      List[str]     ):
																	__a:    int		     =       model_class(snake_case__     )

																	__a:    str		     =       model(**self._prepare_for_class(snake_case__    ,			snake_case__     )     )
																	__a:    Tuple		     =       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																	__a:    Any		     =       self.model_tester.num_stages
																	self.assertEqual(len(snake_case__     )    ,			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]    ,			)

												__a     ,					__a:    str		     =       self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																	__a:    Optional[Any]		     =       True
																	check_hidden_states_output(snake_case__    ,			snake_case__    ,			snake_case__     )

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

																	check_hidden_states_output(snake_case__    ,			snake_case__    ,			snake_case__     )






							def 				__lowercase	(       self    :      List[Any]     ):






												"""simple docstring"""



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

												def check_equivalence(snake_case__    :      Optional[int]    ,			snake_case__    :      int    ,			snake_case__    :      Dict    ,			snake_case__    :      List[str]={}     ):
																	__a:    Dict		     =       model(snake_case__    ,			return_dict=snake_case__    ,			**snake_case__     )
																	__a:    Dict		     =       model(snake_case__    ,			return_dict=snake_case__    ,			**snake_case__     ).to_tuple()

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

																						recursive_check(snake_case__    ,			snake_case__     )

												for model_class in self.all_model_classes:
																	__a:    str		     =       model_class(snake_case__     )

																	__a:    Union[str, Any]		     =       self._prepare_for_class(snake_case__    ,			snake_case__     )
																	__a:    Optional[Any]		     =       self._prepare_for_class(snake_case__    ,			snake_case__     )
																	check_equivalence(snake_case__    ,			snake_case__    ,			snake_case__     )

																	__a:    str		     =       self._prepare_for_class(snake_case__    ,			snake_case__    ,			return_labels=snake_case__     )
																	__a:    Optional[int]		     =       self._prepare_for_class(snake_case__    ,			snake_case__    ,			return_labels=snake_case__     )
																	check_equivalence(snake_case__    ,			snake_case__    ,			snake_case__     )

																	__a:    Optional[Any]		     =       self._prepare_for_class(snake_case__    ,			snake_case__     )
																	__a:    int		     =       self._prepare_for_class(snake_case__    ,			snake_case__     )
																	check_equivalence(snake_case__    ,			snake_case__    ,			snake_case__    ,			{"""output_hidden_states""": True}     )

																	__a:    int		     =       self._prepare_for_class(snake_case__    ,			snake_case__    ,			return_labels=snake_case__     )
																	__a:    Any		     =       self._prepare_for_class(snake_case__    ,			snake_case__    ,			return_labels=snake_case__     )
																	check_equivalence(snake_case__    ,			snake_case__    ,			snake_case__    ,			{"""output_hidden_states""": True}     )






							def 				__lowercase	(       self    :      List[Any]     ):






												"""simple docstring"""



												__a:    Union[str, Any]		     =       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*snake_case__     )






							@slow
							def 				__lowercase	(       self    :      List[Any]     ):






												"""simple docstring"""



												__a:    List[str]		     =       TFConvNextModel.from_pretrained("""facebook/convnext-tiny-224"""     )
												self.assertIsNotNone(snake_case__     )






def      lowerCamelCase_     (						)  ->      int:


					'''simple docstring'''
					__a:    Union[str, Any]		     =       Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
					return image




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






							@cached_property
							def 				__lowercase	(       self    :      Any     ):






												"""simple docstring"""



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






							@slow
							def 				__lowercase	(       self    :      Any     ):






												"""simple docstring"""



												__a:    Union[str, Any]		     =       TFConvNextForImageClassification.from_pretrained("""facebook/convnext-tiny-224"""     )

												__a:    Optional[int]		     =       self.default_image_processor
												__a:    Tuple		     =       prepare_img()
												__a:    List[Any]		     =       image_processor(images=snake_case__    ,			return_tensors="""tf"""     )

												# forward pass
												__a:    Any		     =       model(**snake_case__     )

												# verify the logits
												__a:    List[Any]		     =       tf.TensorShape((1, 1_0_0_0)     )
												self.assertEqual(outputs.logits.shape    ,			snake_case__     )

												__a:    Optional[int]		     =       tf.constant([-0.0_260, -0.4_739, 0.1_911]     )

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


"""simple docstring"""
import unittest
from dataclasses import dataclass
from typing import Optional

from transformers.utils import ModelOutput




@dataclass
class  _snake_case	(    _lowercase							):
							SCREAMING_SNAKE_CASE		  =    42
							SCREAMING_SNAKE_CASE		  =    None
							SCREAMING_SNAKE_CASE		  =    None






class  _snake_case	(    unittest.TestCase							):






							def 				__lowercase	(       self    :      Optional[Any]     ):






												"""simple docstring"""



												__a:    Dict		     =       ModelOutputTest(a=3_0     )
												self.assertEqual(x.a    ,			3_0     )
												self.assertIsNone(x.b     )
												self.assertIsNone(x.c     )
												with self.assertRaises(snake_case__     ):
																	__a:    int		     =       x.d






							def 				__lowercase	(       self    :      int     ):






												"""simple docstring"""



												__a:    Tuple		     =       ModelOutputTest(a=3_0    ,			b=1_0     )
												self.assertEqual(x[0]    ,			3_0     )
												self.assertEqual(x[1]    ,			1_0     )
												self.assertEqual(x[:2]    ,			(3_0, 1_0)     )
												self.assertEqual(x[:]    ,			(3_0, 1_0)     )

												__a:    Tuple		     =       ModelOutputTest(a=3_0    ,			c=1_0     )
												self.assertEqual(x[0]    ,			3_0     )
												self.assertEqual(x[1]    ,			1_0     )
												self.assertEqual(x[:2]    ,			(3_0, 1_0)     )
												self.assertEqual(x[:]    ,			(3_0, 1_0)     )






							def 				__lowercase	(       self    :      Tuple     ):






												"""simple docstring"""



												__a:    List[str]		     =       ModelOutputTest(a=3_0    ,			b=1_0     )
												self.assertEqual(x["""a"""]    ,			3_0     )
												self.assertEqual(x["""b"""]    ,			1_0     )
												with self.assertRaises(snake_case__     ):
																	__a:    Union[str, Any]		     =       x["""c"""]

												__a:    str		     =       ModelOutputTest(a=3_0    ,			c=1_0     )
												self.assertEqual(x["""a"""]    ,			3_0     )
												self.assertEqual(x["""c"""]    ,			1_0     )
												with self.assertRaises(snake_case__     ):
																	__a:    List[str]		     =       x["""b"""]






							def 				__lowercase	(       self    :      List[str]     ):






												"""simple docstring"""



												__a:    str		     =       ModelOutputTest(a=3_0     )
												self.assertEqual(list(x.keys()     )    ,			["""a"""]     )
												self.assertEqual(list(x.values()     )    ,			[3_0]     )
												self.assertEqual(list(x.items()     )    ,			[("""a""", 3_0)]     )
												self.assertEqual(list(snake_case__     )    ,			["""a"""]     )

												__a:    Optional[Any]		     =       ModelOutputTest(a=3_0    ,			b=1_0     )
												self.assertEqual(list(x.keys()     )    ,			["""a""", """b"""]     )
												self.assertEqual(list(x.values()     )    ,			[3_0, 1_0]     )
												self.assertEqual(list(x.items()     )    ,			[("""a""", 3_0), ("""b""", 1_0)]     )
												self.assertEqual(list(snake_case__     )    ,			["""a""", """b"""]     )

												__a:    Union[str, Any]		     =       ModelOutputTest(a=3_0    ,			c=1_0     )
												self.assertEqual(list(x.keys()     )    ,			["""a""", """c"""]     )
												self.assertEqual(list(x.values()     )    ,			[3_0, 1_0]     )
												self.assertEqual(list(x.items()     )    ,			[("""a""", 3_0), ("""c""", 1_0)]     )
												self.assertEqual(list(snake_case__     )    ,			["""a""", """c"""]     )

												with self.assertRaises(snake_case__     ):
																	__a:    List[Any]		     =       x.update({"""d""": 2_0}     )
												with self.assertRaises(snake_case__     ):
																	del x["a"]
												with self.assertRaises(snake_case__     ):
																	__a:    Union[str, Any]		     =       x.pop("""a"""     )
												with self.assertRaises(snake_case__     ):
																	__a:    List[Any]		     =       x.setdefault("""d"""    ,			3_2     )






							def 				__lowercase	(       self    :      Any     ):






												"""simple docstring"""



												__a:    List[str]		     =       ModelOutputTest(a=3_0     )
												__a:    str		     =       1_0
												self.assertEqual(x.a    ,			1_0     )
												self.assertEqual(x["""a"""]    ,			1_0     )






							def 				__lowercase	(       self    :      Dict     ):






												"""simple docstring"""



												__a:    List[str]		     =       ModelOutputTest(a=3_0     )
												__a:    Any		     =       1_0
												self.assertEqual(x.a    ,			1_0     )
												self.assertEqual(x["""a"""]    ,			1_0     )






							def 				__lowercase	(       self    :      int     ):






												"""simple docstring"""



												__a:    List[Any]		     =       ModelOutputTest({"""a""": 3_0, """b""": 1_0}     )
												self.assertEqual(list(x.keys()     )    ,			["""a""", """b"""]     )
												self.assertEqual(x.a    ,			3_0     )
												self.assertEqual(x.b    ,			1_0     )






							def 				__lowercase	(       self    :      List[str]     ):






												"""simple docstring"""



												__a:    Union[str, Any]		     =       ModelOutputTest([("""a""", 3_0), ("""b""", 1_0)]     )
												self.assertEqual(list(x.keys()     )    ,			["""a""", """b"""]     )
												self.assertEqual(x.a    ,			3_0     )
												self.assertEqual(x.b    ,			1_0     )

												with self.assertRaises(snake_case__     ):
																	__a:    str		     =       ModelOutputTest([("""a""", 3_0), (1_0, 1_0)]     )

												__a:    str		     =       ModelOutputTest(a=(3_0, 3_0)     )
												self.assertEqual(list(x.keys()     )    ,			["""a"""]     )
												self.assertEqual(x.a    ,			(3_0, 3_0)     )
