





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      __magic_name__       :




					def __init__(    self     ,					_a     ,					_a=13     ,					_a=32     ,					_a=3     ,					_a=4     ,					_a=[10, 20, 30, 40]     ,					_a=[2, 2, 3, 2]     ,					_a=True     ,					_a=True     ,					_a=37     ,					_a="gelu"     ,					_a=10     ,					_a=0.0_2     ,					_a=3     ,					_a=None     ,					)       -> Dict:
							lowerCAmelCase_				=							parent
							lowerCAmelCase_				=							batch_size
							lowerCAmelCase_				=							image_size
							lowerCAmelCase_				=							num_channels
							lowerCAmelCase_				=							num_stages
							lowerCAmelCase_				=							hidden_sizes
							lowerCAmelCase_				=							depths
							lowerCAmelCase_				=							is_training
							lowerCAmelCase_				=							use_labels
							lowerCAmelCase_				=							intermediate_size
							lowerCAmelCase_				=							hidden_act
							lowerCAmelCase_				=							type_sequence_label_size
							lowerCAmelCase_				=							initializer_range
							lowerCAmelCase_				=							scope




					def  __a			(    self		)       -> List[Any]:
							lowerCAmelCase_				=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

							lowerCAmelCase_				=							self.get_config()

							return config, pixel_values, labels




					def  __a			(    self		)       -> Optional[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=__SCREAMING_SNAKE_CASE     ,					initializer_range=self.initializer_range     ,					)




					def  __a			(    self     ,					_a     ,					_a     ,					_a		)       -> Optional[Any]:
							lowerCAmelCase_				=							TFConvNextModel(config=__SCREAMING_SNAKE_CASE		)
							lowerCAmelCase_				=							model(__SCREAMING_SNAKE_CASE     ,					training=__SCREAMING_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 // 32, self.image_size // 32)     ,					)




					def  __a			(    self     ,					_a     ,					_a     ,					_a		)       -> List[Any]:
							lowerCAmelCase_				=							self.type_sequence_label_size
							lowerCAmelCase_				=							TFConvNextForImageClassification(__SCREAMING_SNAKE_CASE		)
							lowerCAmelCase_				=							model(__SCREAMING_SNAKE_CASE     ,					labels=__SCREAMING_SNAKE_CASE     ,					training=__SCREAMING_SNAKE_CASE		)
							self.parent.assertEqual(result.logits.shape     ,					(self.batch_size, self.type_sequence_label_size)		)




					def  __a			(    self		)       -> int:
							lowerCAmelCase_				=							self.prepare_config_and_inputs()
							lowerCAmelCase_		,   lowerCAmelCase_		,   lowerCAmelCase_				=							config_and_inputs
							lowerCAmelCase_				=							{"pixel_values": pixel_values}
							return config, inputs_dict





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

					lowerCamelCase__					=			False
					lowerCamelCase__					=			False
					lowerCamelCase__					=			False
					lowerCamelCase__					=			False
					lowerCamelCase__					=			False




					def  __a			(    self		)       -> Optional[int]:
							lowerCAmelCase_				=							TFConvNextModelTester(self		)
							lowerCAmelCase_				=							ConfigTester(
							    self     ,					config_class=__SCREAMING_SNAKE_CASE     ,					has_text_modality=__SCREAMING_SNAKE_CASE     ,					hidden_size=37     ,					)




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




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




					def  __a			(    self		)       -> str:
							lowerCAmelCase_		,   lowerCAmelCase_				=							self.model_tester.prepare_config_and_inputs_for_common()

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

									lowerCAmelCase_				=							["pixel_values"]
									self.assertListEqual(arg_names[:1]     ,					__SCREAMING_SNAKE_CASE		)




					def  __a			(    self		)       -> str:
							lowerCAmelCase_				=							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__SCREAMING_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  __a			(    self		)       -> str:
							super().test_dataset_conversion()




					def  __a			(    self		)       -> int:
							def check_hidden_states_output(_a     ,					_a     ,					_a		):
									lowerCAmelCase_				=							model_class(__SCREAMING_SNAKE_CASE		)

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

									lowerCAmelCase_				=							self.model_tester.num_stages
									self.assertEqual(len(__SCREAMING_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]     ,					)

							lowerCAmelCase_		,   lowerCAmelCase_				=							self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									lowerCAmelCase_				=							True
									check_hidden_states_output(__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE		)

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

									check_hidden_states_output(__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE		)




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

							def check_equivalence(_a     ,					_a     ,					_a     ,					_a={}		):
									lowerCAmelCase_				=							model(__SCREAMING_SNAKE_CASE     ,					return_dict=__SCREAMING_SNAKE_CASE     ,					**__SCREAMING_SNAKE_CASE		)
									lowerCAmelCase_				=							model(__SCREAMING_SNAKE_CASE     ,					return_dict=__SCREAMING_SNAKE_CASE     ,					**__SCREAMING_SNAKE_CASE		).to_tuple()

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

											recursive_check(__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE		)

							for model_class in self.all_model_classes:
									lowerCAmelCase_				=							model_class(__SCREAMING_SNAKE_CASE		)

									lowerCAmelCase_				=							self._prepare_for_class(__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE		)
									lowerCAmelCase_				=							self._prepare_for_class(__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE		)
									check_equivalence(__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE		)

									lowerCAmelCase_				=							self._prepare_for_class(__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE     ,					return_labels=__SCREAMING_SNAKE_CASE		)
									lowerCAmelCase_				=							self._prepare_for_class(__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE     ,					return_labels=__SCREAMING_SNAKE_CASE		)
									check_equivalence(__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE		)

									lowerCAmelCase_				=							self._prepare_for_class(__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE		)
									lowerCAmelCase_				=							self._prepare_for_class(__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE		)
									check_equivalence(__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE     ,					{"output_hidden_states": True}		)

									lowerCAmelCase_				=							self._prepare_for_class(__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE     ,					return_labels=__SCREAMING_SNAKE_CASE		)
									lowerCAmelCase_				=							self._prepare_for_class(__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE     ,					return_labels=__SCREAMING_SNAKE_CASE		)
									check_equivalence(__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE     ,					{"output_hidden_states": True}		)




					def  __a			(    self		)       -> Optional[Any]:
							lowerCAmelCase_				=							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE		)




					@slow
					def  __a			(    self		)       -> Tuple:
							lowerCAmelCase_				=							TFConvNextModel.from_pretrained("facebook/convnext-tiny-224"		)
							self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)





def 	A():
		lowerCAmelCase_				=							Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
		return image


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




					@cached_property
					def  __a			(    self		)       -> Optional[int]:
							return ConvNextImageProcessor.from_pretrained("facebook/convnext-tiny-224"		) if is_vision_available() else None




					@slow
					def  __a			(    self		)       -> List[Any]:
							lowerCAmelCase_				=							TFConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224"		)

							lowerCAmelCase_				=							self.default_image_processor
							lowerCAmelCase_				=							prepare_img()
							lowerCAmelCase_				=							image_processor(images=__SCREAMING_SNAKE_CASE     ,					return_tensors="tf"		)

							# forward pass
							lowerCAmelCase_				=							model(**__SCREAMING_SNAKE_CASE		)

							# verify the logits
							lowerCAmelCase_				=							tf.TensorShape((1, 1000)		)
							self.assertEqual(outputs.logits.shape     ,					__SCREAMING_SNAKE_CASE		)

							lowerCAmelCase_				=							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]     ,					__SCREAMING_SNAKE_CASE     ,					atol=1E-4		)




"""simple docstring"""




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__A     :    int    =		{'configuration_bit': ['BIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BitConfig', 'BitOnnxConfig']}

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__A     :    Optional[Any]    =		[
	    'BIT_PRETRAINED_MODEL_ARCHIVE_LIST',
	    'BitForImageClassification',
	    'BitModel',
	    'BitPreTrainedModel',
	    'BitBackbone',
	]


try:
	if not is_vision_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__A     :    List[Any]    =		['BitImageProcessor']


if TYPE_CHECKING:
	from .configuration_bit import BIT_PRETRAINED_CONFIG_ARCHIVE_MAP, BitConfig, BitOnnxConfig

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_bit import (
		    BIT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    BitBackbone,
		    BitForImageClassification,
		    BitModel,
		    BitPreTrainedModel,
		)

	try:
		if not is_vision_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .image_processing_bit import BitImageProcessor

else:
	import sys

	__A     :    Union[str, Any]    =		_LazyModule(__name__, globals()['__file__'], _import_structure)





