



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





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







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





	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[Any]												=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

							_UpperCamelCase   :						Tuple												=  None
							if self.use_labels:
													_UpperCamelCase   :						int												=  ids_tensor([self.batch_size]					,self.num_labels					)

							_UpperCamelCase   :						Dict												=  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   :							Dict					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							Tuple					,lowerCamelCase__   :							Tuple					):







							'''simple docstring'''
							_UpperCamelCase   :						Tuple												=  ConvNextModel(config=lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						List[Any]												=  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 // 32, self.image_size // 32)					,)





	def       UpperCamelCase_    (					self   :							Tuple					,lowerCamelCase__   :							Union[str, Any]					,lowerCamelCase__   :							Optional[Any]					,lowerCamelCase__   :							List[str]					):







							'''simple docstring'''
							_UpperCamelCase   :						List[str]												=  ConvNextForImageClassification(lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						int												=  model(lowerCamelCase__					,labels=lowerCamelCase__					)
							self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.num_labels)					)





	def       UpperCamelCase_    (					self   :							int					,lowerCamelCase__   :							Tuple					,lowerCamelCase__   :							List[Any]					,lowerCamelCase__   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[int]												=  ConvNextBackbone(config=lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						Union[str, Any]												=  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
							_UpperCamelCase   :						Optional[int]												=  None
							_UpperCamelCase   :						List[str]												=  ConvNextBackbone(config=lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						int												=  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'''
							_UpperCamelCase   :						Tuple												=  self.prepare_config_and_inputs()
							_UpperCamelCase							,     _UpperCamelCase							,     _UpperCamelCase   :						int												=  config_and_inputs
							_UpperCamelCase   :						List[str]												=  {'pixel_values': pixel_values}
							return config, inputs_dict






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

	lowercase__    							=     True
	lowercase__    							=     False
	lowercase__    							=     False
	lowercase__    							=     False
	lowercase__    							=     False





	def       UpperCamelCase_    (					self   :							Any					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[int]												=  ConvNextModelTester(self					)
							_UpperCamelCase   :						Union[str, Any]												=  ConfigTester(self					,config_class=lowerCamelCase__					,has_text_modality=lowerCamelCase__					,hidden_size=37					)





	def       UpperCamelCase_    (					self   :							int					):







							'''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   :							int					):







							'''simple docstring'''
							return





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







							'''simple docstring'''
							pass





	@unittest.skip(reason='ConvNext does not support input and output embeddings'					)
	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							pass





	@unittest.skip(reason='ConvNext does not use feedforward chunking'					)
	def       UpperCamelCase_    (					self   :							Optional[Any]					):







							'''simple docstring'''
							pass





	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							_UpperCamelCase							,     _UpperCamelCase   :						int												=  self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													_UpperCamelCase   :						Dict												=  model_class(lowerCamelCase__					)
													_UpperCamelCase   :						int												=  inspect.signature(model.forward					)
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													_UpperCamelCase   :						Any												=  [*signature.parameters.keys()]

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





	def       UpperCamelCase_    (					self   :							str					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[Any]												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase   :						int												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_backbone(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							Any					):







							'''simple docstring'''
							def check_hidden_states_output(lowerCamelCase__   :							Optional[int]					,lowerCamelCase__   :							Any					,lowerCamelCase__   :							int					):
													_UpperCamelCase   :						Tuple												=  model_class(lowerCamelCase__					)
													model.to(lowerCamelCase__					)
													model.eval()

													with torch.no_grad():
																			_UpperCamelCase   :						Tuple												=  model(**self._prepare_for_class(lowerCamelCase__					,lowerCamelCase__					)					)

													_UpperCamelCase   :						List[str]												=  outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

													_UpperCamelCase   :						str												=  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   :						int												=  self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													_UpperCamelCase   :						List[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   :						Optional[Any]												=  True

													check_hidden_states_output(lowerCamelCase__					,lowerCamelCase__					,lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							str					):







							'''simple docstring'''
							_UpperCamelCase   :						str												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__					)





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







							'''simple docstring'''
							for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_UpperCamelCase   :						str												=  ConvNextModel.from_pretrained(lowerCamelCase__					)
													self.assertIsNotNone(lowerCamelCase__					)






def A__       (	):
						_UpperCamelCase   :						Any												=  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
						return image






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





	@cached_property
	def       UpperCamelCase_    (					self   :							str					):







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





	@slow
	def       UpperCamelCase_    (					self   :							Tuple					):







							'''simple docstring'''
							_UpperCamelCase   :						List[Any]												=  ConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224'					).to(lowerCamelCase__					)

							_UpperCamelCase   :						str												=  self.default_image_processor
							_UpperCamelCase   :						int												=  prepare_img()
							_UpperCamelCase   :						int												=  image_processor(images=lowerCamelCase__					,return_tensors='pt'					).to(lowerCamelCase__					)

							# forward pass
							with torch.no_grad():
													_UpperCamelCase   :						Tuple												=  model(**lowerCamelCase__					)

							# verify the logits
							_UpperCamelCase   :						Optional[Any]												=  torch.Size((1, 1000)					)
							self.assertEqual(outputs.logits.shape					,lowerCamelCase__					)

							_UpperCamelCase   :						Tuple												=  torch.tensor([-0.0_2_6_0, -0.4_7_3_9, 0.1_9_1_1]					).to(lowerCamelCase__					)

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






@require_torch
class 				lowercase__      (				unittest.TestCase		,   lowercase  ):
	lowercase__    							=     (ConvNextBackbone,) if is_torch_available() else ()
	lowercase__    							=     ConvNextConfig

	lowercase__    							=     False





	def       UpperCamelCase_    (					self   :							Optional[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Tuple												=  ConvNextModelTester(self					)







'''simple docstring'''
def A__       (	UpperCAmelCase_ = 1_0_0_0				):
						_UpperCamelCase   :						List[str]												=  3
						_UpperCamelCase   :						Any												=  0
						while a < n:
												if a % 3 == 0 or a % 5 == 0:
																		result += a
												elif a % 1_5 == 0:
																		result -= a
												a += 1
						return result


if __name__ == "__main__":
					print(F"""{solution() = }""")


