






'''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:       Optional[int]    ,							UpperCamelCase__:       Optional[int]    ,							UpperCamelCase__:       str=13    ,							UpperCamelCase__:       List[str]=32    ,							UpperCamelCase__:       Dict=3    ,							UpperCamelCase__:       str=4    ,							UpperCamelCase__:       Optional[Any]=[10, 20, 30, 40]    ,							UpperCamelCase__:       Optional[Any]=[2, 2, 3, 2]    ,							UpperCamelCase__:       Optional[Any]=True    ,							UpperCamelCase__:       Optional[Any]=True    ,							UpperCamelCase__:       Optional[int]=37    ,							UpperCamelCase__:       Optional[int]="gelu"    ,							UpperCamelCase__:       Tuple=10    ,							UpperCamelCase__:       Union[str, Any]=0.02    ,							UpperCamelCase__:       List[Any]=["stage2", "stage3", "stage4"]    ,							UpperCamelCase__:       Any=[2, 3, 4]    ,							UpperCamelCase__:       Optional[Any]=None    ,							):
														lowerCamelCase__      :			int 						= parent
														lowerCamelCase__      :			Union[str, Any] 						= batch_size
														lowerCamelCase__      :			Any 						= image_size
														lowerCamelCase__      :			str 						= num_channels
														lowerCamelCase__      :			Any 						= num_stages
														lowerCamelCase__      :			List[Any] 						= hidden_sizes
														lowerCamelCase__      :			Union[str, Any] 						= depths
														lowerCamelCase__      :			Dict 						= is_training
														lowerCamelCase__      :			Any 						= use_labels
														lowerCamelCase__      :			int 						= intermediate_size
														lowerCamelCase__      :			Optional[Any] 						= hidden_act
														lowerCamelCase__      :			Dict 						= num_labels
														lowerCamelCase__      :			Dict 						= initializer_range
														lowerCamelCase__      :			List[Any] 						= out_features
														lowerCamelCase__      :			Dict 						= out_indices
														lowerCamelCase__      :			int 						= scope




							def 	lowerCamelCase_       (							self:       List[str]  ):
														lowerCamelCase__      :			str 						= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

														lowerCamelCase__      :			int 						= None
														if self.use_labels:
																					lowerCamelCase__      :			List[str] 						= ids_tensor([self.batch_size]    ,							self.num_labels  )

														lowerCamelCase__      :			List[Any] 						= self.get_config()
														return config, pixel_values, labels




							def 	lowerCamelCase_       (							self:       str  ):
														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=UpperCamelCase__    ,							initializer_range=self.initializer_range    ,							out_features=self.out_features    ,							out_indices=self.out_indices    ,							num_labels=self.num_labels    ,							)




							def 	lowerCamelCase_       (							self:       Optional[Any]    ,							UpperCamelCase__:       List[str]    ,							UpperCamelCase__:       List[str]    ,							UpperCamelCase__:       int  ):
														lowerCamelCase__      :			List[str] 						= ConvNextModel(config=UpperCamelCase__  )
														model.to(UpperCamelCase__  )
														model.eval()
														lowerCamelCase__      :			Union[str, Any] 						= model(UpperCamelCase__  )
														# 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:       str    ,							UpperCamelCase__:       Optional[int]    ,							UpperCamelCase__:       Any    ,							UpperCamelCase__:       Union[str, Any]  ):
														lowerCamelCase__      :			Union[str, Any] 						= ConvNextForImageClassification(UpperCamelCase__  )
														model.to(UpperCamelCase__  )
														model.eval()
														lowerCamelCase__      :			Optional[int] 						= model(UpperCamelCase__    ,							labels=UpperCamelCase__  )
														self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.num_labels)  )




							def 	lowerCamelCase_       (							self:       Optional[Any]    ,							UpperCamelCase__:       List[str]    ,							UpperCamelCase__:       int    ,							UpperCamelCase__:       int  ):
														lowerCamelCase__      :			Tuple 						= ConvNextBackbone(config=UpperCamelCase__  )
														model.to(UpperCamelCase__  )
														model.eval()
														lowerCamelCase__      :			str 						= model(UpperCamelCase__  )

														# 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
														lowerCamelCase__      :			Optional[Any] 						= None
														lowerCamelCase__      :			Optional[Any] 						= ConvNextBackbone(config=UpperCamelCase__  )
														model.to(UpperCamelCase__  )
														model.eval()
														lowerCamelCase__      :			List[Any] 						= model(UpperCamelCase__  )

														# 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 	lowerCamelCase_       (							self:       str  ):
														lowerCamelCase__      :			Any 						= self.prepare_config_and_inputs()
														lowerCamelCase__      :			Dict 						= config_and_inputs
														lowerCamelCase__      :			List[str] 						= {"""pixel_values""": pixel_values}
														return config, inputs_dict



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

							a          = True
							a          = False
							a          = False
							a          = False
							a          = False




							def 	lowerCamelCase_       (							self:       Union[str, Any]  ):
														lowerCamelCase__      :			int 						= ConvNextModelTester(self  )
														lowerCamelCase__      :			List[Any] 						= ConfigTester(self    ,							config_class=UpperCamelCase__    ,							has_text_modality=UpperCamelCase__    ,							hidden_size=37  )




							def 	lowerCamelCase_       (							self:       Union[str, Any]  ):
														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 	lowerCamelCase_       (							self:       Tuple  ):
														return




							@unittest.skip(reason="""ConvNext does not use inputs_embeds"""  )
							def 	lowerCamelCase_       (							self:       List[str]  ):
														pass




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




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




							def 	lowerCamelCase_       (							self:       Dict  ):
														lowerCamelCase__      :			str 						= self.model_tester.prepare_config_and_inputs_for_common()

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

																					lowerCamelCase__      :			Tuple 						= ["""pixel_values"""]
																					self.assertListEqual(arg_names[:1]    ,							UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       Dict  ):
														lowerCamelCase__      :			List[str] 						= self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       Any  ):
														lowerCamelCase__      :			Union[str, Any] 						= self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_backbone(*UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       Optional[int]  ):
														def check_hidden_states_output(UpperCamelCase__:       Union[str, Any]    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       Optional[int]  ):
																					lowerCamelCase__      :			List[str] 						= model_class(UpperCamelCase__  )
																					model.to(UpperCamelCase__  )
																					model.eval()

																					with torch.no_grad():
																												lowerCamelCase__      :			Dict 						= model(**self._prepare_for_class(UpperCamelCase__    ,							UpperCamelCase__  )  )

																					lowerCamelCase__      :			List[Any] 						= outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																					lowerCamelCase__      :			List[str] 						= self.model_tester.num_stages
																					self.assertEqual(len(UpperCamelCase__  )    ,							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__      :			Tuple 						= self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					lowerCamelCase__      :			List[str] 						= True
																					check_hidden_states_output(UpperCamelCase__    ,							UpperCamelCase__    ,							UpperCamelCase__  )

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

																					check_hidden_states_output(UpperCamelCase__    ,							UpperCamelCase__    ,							UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       Optional[Any]  ):
														lowerCamelCase__      :			Any 						= self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__  )




							@slow
							def 	lowerCamelCase_       (							self:       List[Any]  ):
														for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCamelCase__      :			List[str] 						= ConvNextModel.from_pretrained(UpperCamelCase__  )
																					self.assertIsNotNone(UpperCamelCase__  )





def 				SCREAMING_SNAKE_CASE_ ()   -> List[str]:
							lowerCamelCase__      :			str 						= Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""   )
							return image

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




							@cached_property
							def 	lowerCamelCase_       (							self:       List[str]  ):
														return AutoImageProcessor.from_pretrained("""facebook/convnext-tiny-224"""  ) if is_vision_available() else None




							@slow
							def 	lowerCamelCase_       (							self:       Optional[Any]  ):
														lowerCamelCase__      :			Union[str, Any] 						= ConvNextForImageClassification.from_pretrained("""facebook/convnext-tiny-224"""  ).to(UpperCamelCase__  )

														lowerCamelCase__      :			Dict 						= self.default_image_processor
														lowerCamelCase__      :			int 						= prepare_img()
														lowerCamelCase__      :			Dict 						= image_processor(images=UpperCamelCase__    ,							return_tensors="""pt"""  ).to(UpperCamelCase__  )

														# forward pass
														with torch.no_grad():
																					lowerCamelCase__      :			List[str] 						= model(**UpperCamelCase__  )

														# verify the logits
														lowerCamelCase__      :			List[Any] 						= torch.Size((1, 1_000)  )
														self.assertEqual(outputs.logits.shape    ,							UpperCamelCase__  )

														lowerCamelCase__      :			Dict 						= torch.tensor([-0.0_260, -0.4_739, 0.1_911]  ).to(UpperCamelCase__  )

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



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

							a          = False




							def 	lowerCamelCase_       (							self:       Optional[Any]  ):
														lowerCamelCase__      :			Any 						= ConvNextModelTester(self  )










'''simple docstring'''







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_A			:    Dict					  ={
    '''configuration_git''': ['''GIT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GitConfig''', '''GitVisionConfig'''],
    '''processing_git''': ['''GitProcessor'''],
}

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			_A			:    Union[str, Any]					  =[
			    '''GIT_PRETRAINED_MODEL_ARCHIVE_LIST''',
			    '''GitForCausalLM''',
			    '''GitModel''',
			    '''GitPreTrainedModel''',
			    '''GitVisionModel''',
			]

if TYPE_CHECKING:
			from .configuration_git import GIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GitConfig, GitVisionConfig
			from .processing_git import GitProcessor

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_git import (
						    GIT_PRETRAINED_MODEL_ARCHIVE_LIST,
						    GitForCausalLM,
						    GitModel,
						    GitPreTrainedModel,
						    GitVisionModel,
						)

else:
			import sys

			_A			:    Dict					  =_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

