"""simple docstring"""







import inspect
import unittest

from transformers import ConvNextConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import ConvNextBackbone, ConvNextForImageClassification, ConvNextModel
						from transformers.models.convnext.modeling_convnext import CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import AutoImageProcessor



class  _lowerCamelCase			:







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

					"""simple docstring"""






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







				def 							__UpperCAmelCase					( self    :       str						):

					"""simple docstring"""






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

					_lowercase :    int							=							None
					if self.use_labels:
						_lowercase :    Union[str, Any]							=							ids_tensor([self.batch_size]			,					self.num_labels						)

					_lowercase :    str							=							self.get_config()
					return config, pixel_values, labels







				def 							__UpperCAmelCase					( self    :       List[str]						):

					"""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			,					out_features=self.out_features			,					out_indices=self.out_indices			,					num_labels=self.num_labels			,					)







				def 							__UpperCAmelCase					( self    :       List[Any]			,					lowerCamelCase_    :       List[Any]			,					lowerCamelCase_    :       Optional[int]			,					lowerCamelCase_    :       Tuple						):

					"""simple docstring"""






					_lowercase :    Dict							=							ConvNextModel(config=lowerCamelCase_						)
					model.to(lowerCamelCase_						)
					model.eval()
					_lowercase :    Optional[int]							=							model(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 							__UpperCAmelCase					( self    :       Optional[Any]			,					lowerCamelCase_    :       List[str]			,					lowerCamelCase_    :       Optional[int]			,					lowerCamelCase_    :       Any						):

					"""simple docstring"""






					_lowercase :    str							=							ConvNextForImageClassification(lowerCamelCase_						)
					model.to(lowerCamelCase_						)
					model.eval()
					_lowercase :    Any							=							model(lowerCamelCase_			,					labels=lowerCamelCase_						)
					self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.num_labels)						)







				def 							__UpperCAmelCase					( self    :       Tuple			,					lowerCamelCase_    :       Optional[Any]			,					lowerCamelCase_    :       List[str]			,					lowerCamelCase_    :       Tuple						):

					"""simple docstring"""






					_lowercase :    Union[str, Any]							=							ConvNextBackbone(config=lowerCamelCase_						)
					model.to(lowerCamelCase_						)
					model.eval()
					_lowercase :    Tuple							=							model(lowerCamelCase_						)

					# verify hidden states
					self.parent.assertEqual(len(result.feature_maps						)			,					len(config.out_features						)						)
					self.parent.assertListEqual(list(result.feature_maps[0].shape						)			,					[self.batch_size, self.hidden_sizes[1], 4, 4]						)

					# verify channels
					self.parent.assertEqual(len(model.channels						)			,					len(config.out_features						)						)
					self.parent.assertListEqual(model.channels			,					config.hidden_sizes[1:]						)

					# verify backbone works with out_features=None
					_lowercase :    Dict							=							None
					_lowercase :    Optional[Any]							=							ConvNextBackbone(config=lowerCamelCase_						)
					model.to(lowerCamelCase_						)
					model.eval()
					_lowercase :    List[Any]							=							model(lowerCamelCase_						)

					# verify feature maps
					self.parent.assertEqual(len(result.feature_maps						)			,					1						)
					self.parent.assertListEqual(list(result.feature_maps[0].shape						)			,					[self.batch_size, self.hidden_sizes[-1], 1, 1]						)

					# verify channels
					self.parent.assertEqual(len(model.channels						)			,					1						)
					self.parent.assertListEqual(model.channels			,					[config.hidden_sizes[-1]]						)







				def 							__UpperCAmelCase					( self    :       List[Any]						):

					"""simple docstring"""






					_lowercase :    Any							=							self.prepare_config_and_inputs()
					_lowercase				,      _lowercase				,      _lowercase :    List[str]							=							config_and_inputs
					_lowercase :    List[str]							=							{'pixel_values': pixel_values}
					return config, inputs_dict



@require_torch
class  _lowerCamelCase			(__lowerCamelCase						,							__lowerCamelCase						,							unittest.TestCase     ):
				_snake_case     =  (
				    (
				        ConvNextModel,
				        ConvNextForImageClassification,
				        ConvNextBackbone,
				    )
				    if is_torch_available()
				    else ()
				)
				_snake_case     =  (
				    {"feature-extraction": ConvNextModel, "image-classification": ConvNextForImageClassification}
				    if is_torch_available()
				    else {}
				)

				_snake_case     =  True
				_snake_case     =  False
				_snake_case     =  False
				_snake_case     =  False
				_snake_case     =  False







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

					"""simple docstring"""






					_lowercase :    str							=							ConvNextModelTester(self						)
					_lowercase :    List[Any]							=							ConfigTester(self			,					config_class=lowerCamelCase_			,					has_text_modality=lowerCamelCase_			,					hidden_size=3_7						)







				def 							__UpperCAmelCase					( self    :       List[Any]						):

					"""simple docstring"""






					self.create_and_test_config_common_properties()
					self.config_tester.create_and_test_config_to_json_string()
					self.config_tester.create_and_test_config_to_json_file()
					self.config_tester.create_and_test_config_from_and_save_pretrained()
					self.config_tester.create_and_test_config_with_num_labels()
					self.config_tester.check_config_can_be_init_without_params()
					self.config_tester.check_config_arguments_init()







				def 							__UpperCAmelCase					( self    :       Dict						):

					"""simple docstring"""






					return







				@unittest.skip(reason='ConvNext does not use inputs_embeds'						)
				def 							__UpperCAmelCase					( self    :       int						):

					"""simple docstring"""






					pass







				@unittest.skip(reason='ConvNext does not support input and output embeddings'						)
				def 							__UpperCAmelCase					( self    :       List[Any]						):

					"""simple docstring"""






					pass







				@unittest.skip(reason='ConvNext does not use feedforward chunking'						)
				def 							__UpperCAmelCase					( self    :       Tuple						):

					"""simple docstring"""






					pass







				def 							__UpperCAmelCase					( self    :       Optional[int]						):

					"""simple docstring"""






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

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

						_lowercase :    Union[str, Any]							=							['pixel_values']
						self.assertListEqual(arg_names[:1]			,					lowerCamelCase_						)







				def 							__UpperCAmelCase					( self    :       Optional[int]						):

					"""simple docstring"""






					_lowercase :    Dict							=							self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*lowerCamelCase_						)







				def 							__UpperCAmelCase					( self    :       List[Any]						):

					"""simple docstring"""






					_lowercase :    List[Any]							=							self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_backbone(*lowerCamelCase_						)







				def 							__UpperCAmelCase					( self    :       List[Any]						):

					"""simple docstring"""






					def check_hidden_states_output(lowerCamelCase_    :       Tuple			,					lowerCamelCase_    :       Tuple			,					lowerCamelCase_    :       Tuple						):
						_lowercase :    List[str]							=							model_class(lowerCamelCase_						)
						model.to(lowerCamelCase_						)
						model.eval()

						with torch.no_grad():
							_lowercase :    Optional[int]							=							model(**self._prepare_for_class(lowerCamelCase_			,					lowerCamelCase_						)						)

						_lowercase :    str							=							outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

						_lowercase :    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]			,					)

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

					for model_class in self.all_model_classes:
						_lowercase :    Union[str, Any]							=							True
						check_hidden_states_output(lowerCamelCase_			,					lowerCamelCase_			,					lowerCamelCase_						)

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

						check_hidden_states_output(lowerCamelCase_			,					lowerCamelCase_			,					lowerCamelCase_						)







				def 							__UpperCAmelCase					( self    :       Tuple						):

					"""simple docstring"""






					_lowercase :    Optional[Any]							=							self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_						)







				@slow
				def 							__UpperCAmelCase					( self    :       Optional[Any]						):

					"""simple docstring"""






					for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						_lowercase :    Optional[Any]							=							ConvNextModel.from_pretrained(lowerCamelCase_						)
						self.assertIsNotNone(lowerCamelCase_						)



def      __lowerCAmelCase(		):






	"""simple docstring"""





	_lowercase :    List[Any]							=							Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
	return image



@require_torch
@require_vision
class  _lowerCamelCase			(unittest.TestCase     ):







				@cached_property
				def 							__UpperCAmelCase					( self    :       Optional[Any]						):

					"""simple docstring"""






					return AutoImageProcessor.from_pretrained('facebook/convnext-tiny-224'						) if is_vision_available() else None







				@slow
				def 							__UpperCAmelCase					( self    :       Optional[int]						):

					"""simple docstring"""






					_lowercase :    Dict							=							ConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224'						).to(lowerCamelCase_						)

					_lowercase :    str							=							self.default_image_processor
					_lowercase :    Any							=							prepare_img()
					_lowercase :    Optional[Any]							=							image_processor(images=lowerCamelCase_			,					return_tensors='pt'						).to(lowerCamelCase_						)

					# forward pass
					with torch.no_grad():
						_lowercase :    Tuple							=							model(**lowerCamelCase_						)

					# verify the logits
					_lowercase :    Dict							=							torch.Size((1, 1_0_0_0)						)
					self.assertEqual(outputs.logits.shape			,					lowerCamelCase_						)

					_lowercase :    Any							=							torch.tensor([-0.0260, -0.4739, 0.1911]						).to(lowerCamelCase_						)

					self.assertTrue(torch.allclose(outputs.logits[0, :3]			,					lowerCamelCase_			,					atol=1E-4						)						)



@require_torch
class  _lowerCamelCase			(unittest.TestCase						,							__lowerCamelCase     ):
				_snake_case     =  (ConvNextBackbone,) if is_torch_available() else ()
				_snake_case     =  ConvNextConfig

				_snake_case     =  False







				def 							__UpperCAmelCase					( self    :       Tuple						):

					"""simple docstring"""






					_lowercase :    Tuple							=							ConvNextModelTester(self						)




"""simple docstring"""







from typing import Any


def      __lowerCAmelCase(		__UpperCAmelCase			):






	"""simple docstring"""





	if not input_list:
		return []
	_lowercase :    List[str]							=							[input_list.count(__UpperCAmelCase			) for value in input_list]
	_lowercase :    Optional[int]							=							max(__UpperCAmelCase			)  # Gets the maximum count in the input list.
	# Gets values of modes
	return sorted({input_list[i] for i, value in enumerate(__UpperCAmelCase			) if value == y}			)


if __name__ == "__main__":
						import doctest

						doctest.testmod()



