





import inspect
import unittest

from transformers import ConvNextVaConfig
from transformers.models.auto import get_values
from transformers.models.auto.modeling_auto import MODEL_FOR_BACKBONE_MAPPING_NAMES, MODEL_MAPPING_NAMES
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_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 ConvNextVaBackbone, ConvNextVaForImageClassification, ConvNextVaModel
					from transformers.models.convnextva.modeling_convnextva import CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
					from PIL import Image

					from transformers import AutoImageProcessor



class      _SCREAMING_SNAKE_CASE		:







	'''simple docstring'''

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



			'''simple docstring'''



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

	def 							SCREAMING_SNAKE_CASE_      (self       :     List[str]) ->Any:



			'''simple docstring'''



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

			lowerCamelCase__:   Any    						=None
			if self.use_labels:
					lowerCamelCase__:   Dict    						=ids_tensor([self.batch_size] ,				self.num_labels)

			lowerCamelCase__:   Optional[Any]    						=self.get_config()

			return config, pixel_values, labels

	def 							SCREAMING_SNAKE_CASE_      (self       :     Tuple) ->Dict:



			'''simple docstring'''



			return ConvNextVaConfig(
			    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=a_ ,				initializer_range=self.initializer_range ,				out_features=self.out_features ,				out_indices=self.out_indices ,				num_labels=self.num_labels ,				)

	def 							SCREAMING_SNAKE_CASE_      (self       :     int ,				UpperCAmelCase_       :     Tuple ,				UpperCAmelCase_       :     Any ,				UpperCAmelCase_       :     Dict) ->str:



			'''simple docstring'''



			lowerCamelCase__:   int    						=ConvNextVaModel(config=a_)
			model.to(a_)
			model.eval()
			lowerCamelCase__:   Any    						=model(a_)
			# 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 							SCREAMING_SNAKE_CASE_      (self       :     Optional[int] ,				UpperCAmelCase_       :     List[str] ,				UpperCAmelCase_       :     Tuple ,				UpperCAmelCase_       :     Union[str, Any]) ->str:



			'''simple docstring'''



			lowerCamelCase__:   Optional[int]    						=ConvNextVaForImageClassification(a_)
			model.to(a_)
			model.eval()
			lowerCamelCase__:   Optional[int]    						=model(a_ ,				labels=a_)
			self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_labels))

	def 							SCREAMING_SNAKE_CASE_      (self       :     Union[str, Any] ,				UpperCAmelCase_       :     Tuple ,				UpperCAmelCase_       :     Tuple ,				UpperCAmelCase_       :     Tuple) ->int:



			'''simple docstring'''



			lowerCamelCase__:   List[str]    						=ConvNextVaBackbone(config=a_)
			model.to(a_)
			model.eval()
			lowerCamelCase__:   int    						=model(a_)

			# 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__:   Tuple    						=None
			lowerCamelCase__:   Tuple    						=ConvNextVaBackbone(config=a_)
			model.to(a_)
			model.eval()
			lowerCamelCase__:   List[Any]    						=model(a_)

			# 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 							SCREAMING_SNAKE_CASE_      (self       :     Dict) ->Union[str, Any]:



			'''simple docstring'''



			lowerCamelCase__:   Dict    						=self.prepare_config_and_inputs()
			lowerCamelCase__:   Any    						=config_and_inputs
			lowerCamelCase__:   str    						={"""pixel_values""": pixel_values}
			return config, inputs_dict







	def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[int]) ->str:



			'''simple docstring'''



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



@require_torch
class      _SCREAMING_SNAKE_CASE		(			__a					,							__a					,							unittest.TestCase					):







	'''simple docstring'''
	lowercase_	=    (
	    (
	        ConvNextVaModel,
	        ConvNextVaForImageClassification,
	        ConvNextVaBackbone,
	    )
	    if is_torch_available()
	    else ()
	)
	lowercase_	=    (
	    {"feature-extraction": ConvNextVaModel, "image-classification": ConvNextVaForImageClassification}
	    if is_torch_available()
	    else {}
	)

	lowercase_	=    False
	lowercase_	=    False
	lowercase_	=    False
	lowercase_	=    False
	lowercase_	=    False

	def 							SCREAMING_SNAKE_CASE_      (self       :     Tuple) ->Optional[Any]:



			'''simple docstring'''



			lowerCamelCase__:   Tuple    						=ConvNextVaModelTester(self)
			lowerCamelCase__:   int    						=ConfigTester(self ,				config_class=a_ ,				has_text_modality=a_ ,				hidden_size=37)

	def 							SCREAMING_SNAKE_CASE_      (self       :     List[str]) ->Optional[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 							SCREAMING_SNAKE_CASE_      (self       :     Tuple) ->List[str]:



			'''simple docstring'''



			return

	@unittest.skip(reason="ConvNextV2 does not use inputs_embeds")
	def 							SCREAMING_SNAKE_CASE_      (self       :     int) ->Dict:



			'''simple docstring'''



			pass

	@unittest.skip(reason="ConvNextV2 does not support input and output embeddings")
	def 							SCREAMING_SNAKE_CASE_      (self       :     Any) ->Union[str, Any]:



			'''simple docstring'''



			pass

	@unittest.skip(reason="ConvNextV2 does not use feedforward chunking")
	def 							SCREAMING_SNAKE_CASE_      (self       :     str) ->List[Any]:



			'''simple docstring'''



			pass

	def 							SCREAMING_SNAKE_CASE_      (self       :     int) ->Optional[int]:



			'''simple docstring'''



			if not self.model_tester.is_training:
					return

			for model_class in self.all_model_classes:
					lowerCamelCase__:   Union[str, Any]    						=self.model_tester.prepare_config_and_inputs_with_labels()
					lowerCamelCase__:   List[Any]    						=True

					if model_class.__name__ in [
					    *get_values(a_),
					    *get_values(a_),
					]:
							continue

					lowerCamelCase__:   Tuple    						=model_class(a_)
					model.to(a_)
					model.train()
					lowerCamelCase__:   Optional[Any]    						=self._prepare_for_class(a_ ,				a_ ,				return_labels=a_)
					lowerCamelCase__:   Any    						=model(**a_).loss
					loss.backward()

	def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[int]) ->Optional[int]:



			'''simple docstring'''



			if not self.model_tester.is_training:
					return

			for model_class in self.all_model_classes:
					lowerCamelCase__:   str    						=self.model_tester.prepare_config_and_inputs_with_labels()
					lowerCamelCase__:   Any    						=False
					lowerCamelCase__:   List[Any]    						=True

					if (
					    model_class.__name__
					    in [*get_values(a_), *get_values(a_)]
					    or not model_class.supports_gradient_checkpointing
					):
							continue

					lowerCamelCase__:   Dict    						=model_class(a_)
					model.to(a_)
					model.gradient_checkpointing_enable()
					model.train()
					lowerCamelCase__:   str    						=self._prepare_for_class(a_ ,				a_ ,				return_labels=a_)
					lowerCamelCase__:   Optional[int]    						=model(**a_).loss
					loss.backward()

	def 							SCREAMING_SNAKE_CASE_      (self       :     str) ->Union[str, Any]:



			'''simple docstring'''



			lowerCamelCase__:   List[Any]    						=self.model_tester.prepare_config_and_inputs_for_common()

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

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

	def 							SCREAMING_SNAKE_CASE_      (self       :     int) ->List[Any]:



			'''simple docstring'''



			lowerCamelCase__:   Tuple    						=self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*a_)

	def 							SCREAMING_SNAKE_CASE_      (self       :     Union[str, Any]) ->int:



			'''simple docstring'''



			def check_hidden_states_output(UpperCAmelCase_       :     str ,				UpperCAmelCase_       :     Tuple ,				UpperCAmelCase_       :     Tuple):
					lowerCamelCase__:   Optional[Any]    						=model_class(a_)
					model.to(a_)
					model.eval()

					with torch.no_grad():
							lowerCamelCase__:   Any    						=model(**self._prepare_for_class(a_ ,				a_))

					lowerCamelCase__:   Optional[int]    						=outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

					lowerCamelCase__:   Optional[int]    						=self.model_tester.num_stages
					self.assertEqual(len(a_) ,				expected_num_stages + 1)

					# ConvNextV2'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__:   Optional[Any]    						=self.model_tester.prepare_config_and_inputs_for_common()

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

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

					check_hidden_states_output(a_ ,				a_ ,				a_)

	def 							SCREAMING_SNAKE_CASE_      (self       :     Dict) ->Any:



			'''simple docstring'''



			lowerCamelCase__:   List[str]    						=self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_image_classification(*a_)







	@slow
	def 							SCREAMING_SNAKE_CASE_      (self       :     Dict) ->str:



			'''simple docstring'''



			for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					lowerCamelCase__:   str    						=ConvNextVaModel.from_pretrained(a_)
					self.assertIsNotNone(a_)





def   lowerCAmelCase_				( )  ->	int:







		"""simple docstring"""





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






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







	'''simple docstring'''

	@cached_property
	def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[Any]) ->List[str]:



			'''simple docstring'''



			return AutoImageProcessor.from_pretrained("facebook/convnextv2-tiny-1k-224") if is_vision_available() else None







	@slow
	def 							SCREAMING_SNAKE_CASE_      (self       :     int) ->Any:



			'''simple docstring'''



			lowerCamelCase__:   Optional[Any]    						=ConvNextVaForImageClassification.from_pretrained("facebook/convnextv2-tiny-1k-224").to(a_)

			lowerCamelCase__:   Union[str, Any]    						=self.default_image_processor
			lowerCamelCase__:   List[Any]    						=prepare_img()
			lowerCamelCase__:   Optional[int]    						=preprocessor(images=a_ ,				return_tensors="pt").to(a_)

			# forward pass
			with torch.no_grad():
					lowerCamelCase__:   Optional[int]    						=model(**a_)

			# verify the logits
			lowerCamelCase__:   Optional[int]    						=torch.Size((1, 1_000))
			self.assertEqual(outputs.logits.shape ,				a_)

			lowerCamelCase__:   Optional[int]    						=torch.tensor([0.9996, 0.1966, -0.4386]).to(a_)
			self.assertTrue(torch.allclose(outputs.logits[0, :3] ,				a_ ,				atol=1E-4))








import os
import sys
import unittest


__A             =       os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, "utils"))

import get_test_info  # noqa: E402
from get_test_info import (  # noqa: E402
    get_model_to_test_mapping,
    get_model_to_tester_mapping,
    get_test_to_tester_mapping,
)


__A             =       os.path.join("tests", "models", "bert", "test_modeling_bert.py")
__A             =       os.path.join("tests", "models", "blip", "test_modeling_blip.py")



class      _SCREAMING_SNAKE_CASE		(			unittest.TestCase					):







 '''simple docstring'''

 def 							SCREAMING_SNAKE_CASE_      (self       :     List[Any]) ->str:



   '''simple docstring'''



   lowerCamelCase__:   List[str]    						=get_test_to_tester_mapping(UpperCAmelCase_)
   lowerCamelCase__:   List[str]    						=get_test_to_tester_mapping(UpperCAmelCase_)

   lowerCamelCase__:   List[Any]    						={"BertModelTest": "BertModelTester"}

   lowerCamelCase__:   Optional[int]    						={
       "BlipModelTest": "BlipModelTester",
       "BlipTextImageModelTest": "BlipTextImageModelsModelTester",
       "BlipTextModelTest": "BlipTextModelTester",
       "BlipTextRetrievalModelTest": "BlipTextRetrievalModelTester",
       "BlipVQAModelTest": "BlipVQAModelTester",
       "BlipVisionModelTest": "BlipVisionModelTester",
   }

   self.assertEqual(get_test_info.to_json(UpperCAmelCase_) ,				UpperCAmelCase_)
   self.assertEqual(get_test_info.to_json(UpperCAmelCase_) ,				UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[Any]) ->str:



   '''simple docstring'''



   lowerCamelCase__:   str    						=get_model_to_test_mapping(UpperCAmelCase_)
   lowerCamelCase__:   int    						=get_model_to_test_mapping(UpperCAmelCase_)

   lowerCamelCase__:   Tuple    						={
       "BertForMaskedLM": ["BertModelTest"],
       "BertForMultipleChoice": ["BertModelTest"],
       "BertForNextSentencePrediction": ["BertModelTest"],
       "BertForPreTraining": ["BertModelTest"],
       "BertForQuestionAnswering": ["BertModelTest"],
       "BertForSequenceClassification": ["BertModelTest"],
       "BertForTokenClassification": ["BertModelTest"],
       "BertLMHeadModel": ["BertModelTest"],
       "BertModel": ["BertModelTest"],
   }

   lowerCamelCase__:   Any    						={
       "BlipForConditionalGeneration": ["BlipTextImageModelTest"],
       "BlipForImageTextRetrieval": ["BlipTextRetrievalModelTest"],
       "BlipForQuestionAnswering": ["BlipVQAModelTest"],
       "BlipModel": ["BlipModelTest"],
       "BlipTextModel": ["BlipTextModelTest"],
       "BlipVisionModel": ["BlipVisionModelTest"],
   }

   self.assertEqual(get_test_info.to_json(UpperCAmelCase_) ,				UpperCAmelCase_)
   self.assertEqual(get_test_info.to_json(UpperCAmelCase_) ,				UpperCAmelCase_)







 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[int]) ->Any:



   '''simple docstring'''



   lowerCamelCase__:   Optional[Any]    						=get_model_to_tester_mapping(UpperCAmelCase_)
   lowerCamelCase__:   Optional[Any]    						=get_model_to_tester_mapping(UpperCAmelCase_)

   lowerCamelCase__:   Any    						={
       "BertForMaskedLM": ["BertModelTester"],
       "BertForMultipleChoice": ["BertModelTester"],
       "BertForNextSentencePrediction": ["BertModelTester"],
       "BertForPreTraining": ["BertModelTester"],
       "BertForQuestionAnswering": ["BertModelTester"],
       "BertForSequenceClassification": ["BertModelTester"],
       "BertForTokenClassification": ["BertModelTester"],
       "BertLMHeadModel": ["BertModelTester"],
       "BertModel": ["BertModelTester"],
   }

   lowerCamelCase__:   Optional[Any]    						={
       "BlipForConditionalGeneration": ["BlipTextImageModelsModelTester"],
       "BlipForImageTextRetrieval": ["BlipTextRetrievalModelTester"],
       "BlipForQuestionAnswering": ["BlipVQAModelTester"],
       "BlipModel": ["BlipModelTester"],
       "BlipTextModel": ["BlipTextModelTester"],
       "BlipVisionModel": ["BlipVisionModelTester"],
   }

   self.assertEqual(get_test_info.to_json(UpperCAmelCase_) ,				UpperCAmelCase_)
   self.assertEqual(get_test_info.to_json(UpperCAmelCase_) ,				UpperCAmelCase_)

