





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


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



         """simple docstring"""



         SCREAMING_SNAKE_CASE__			=      parent
         SCREAMING_SNAKE_CASE__			=      batch_size
         SCREAMING_SNAKE_CASE__			=      image_size
         SCREAMING_SNAKE_CASE__			=      num_channels
         SCREAMING_SNAKE_CASE__			=      num_stages
         SCREAMING_SNAKE_CASE__			=      hidden_sizes
         SCREAMING_SNAKE_CASE__			=      depths
         SCREAMING_SNAKE_CASE__			=      is_training
         SCREAMING_SNAKE_CASE__			=      use_labels
         SCREAMING_SNAKE_CASE__			=      intermediate_size
         SCREAMING_SNAKE_CASE__			=      hidden_act
         SCREAMING_SNAKE_CASE__			=      num_labels
         SCREAMING_SNAKE_CASE__			=      initializer_range
         SCREAMING_SNAKE_CASE__			=      out_features
         SCREAMING_SNAKE_CASE__			=      out_indices
         SCREAMING_SNAKE_CASE__			=      scope


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



         """simple docstring"""



         SCREAMING_SNAKE_CASE__			=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

         SCREAMING_SNAKE_CASE__			=      None
         if self.use_labels:
           SCREAMING_SNAKE_CASE__			=      ids_tensor([self.batch_size]							,				self.num_labels		)

         SCREAMING_SNAKE_CASE__			=      self.get_config()

         return config, pixel_values, labels


       def      _snake_case  (							self    :Any		)   ->   List[Any]:



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


       def      _snake_case  (							self    :List[Any]							,				__A    :Union[str, Any]							,				__A    :List[Any]							,				__A    :Any		)   ->   Optional[Any]:



         """simple docstring"""



         SCREAMING_SNAKE_CASE__			=      ConvNextVaModel(config=_SCREAMING_SNAKE_CASE		)
         model.to(_SCREAMING_SNAKE_CASE		)
         model.eval()
         SCREAMING_SNAKE_CASE__			=      model(_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      _snake_case  (							self    :str							,				__A    :int							,				__A    :List[Any]							,				__A    :Optional[Any]		)   ->   List[Any]:



         """simple docstring"""



         SCREAMING_SNAKE_CASE__			=      ConvNextVaForImageClassification(_SCREAMING_SNAKE_CASE		)
         model.to(_SCREAMING_SNAKE_CASE		)
         model.eval()
         SCREAMING_SNAKE_CASE__			=      model(_SCREAMING_SNAKE_CASE							,				labels=_SCREAMING_SNAKE_CASE		)
         self.parent.assertEqual(result.logits.shape							,				(self.batch_size, self.num_labels)		)


       def      _snake_case  (							self    :Union[str, Any]							,				__A    :int							,				__A    :List[str]							,				__A    :Union[str, Any]		)   ->   Optional[Any]:



         """simple docstring"""



         SCREAMING_SNAKE_CASE__			=      ConvNextVaBackbone(config=_SCREAMING_SNAKE_CASE		)
         model.to(_SCREAMING_SNAKE_CASE		)
         model.eval()
         SCREAMING_SNAKE_CASE__			=      model(_SCREAMING_SNAKE_CASE		)

         # 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
         SCREAMING_SNAKE_CASE__			=      None
         SCREAMING_SNAKE_CASE__			=      ConvNextVaBackbone(config=_SCREAMING_SNAKE_CASE		)
         model.to(_SCREAMING_SNAKE_CASE		)
         model.eval()
         SCREAMING_SNAKE_CASE__			=      model(_SCREAMING_SNAKE_CASE		)

         # 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      _snake_case  (							self    :Optional[int]		)   ->   Optional[Any]:



         """simple docstring"""



         SCREAMING_SNAKE_CASE__			=      self.prepare_config_and_inputs()
         SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      config_and_inputs
         SCREAMING_SNAKE_CASE__			=      {"""pixel_values""": pixel_values}
         return config, inputs_dict


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



         """simple docstring"""



         SCREAMING_SNAKE_CASE__			=      self.prepare_config_and_inputs()
         SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      config_and_inputs
         SCREAMING_SNAKE_CASE__			=      {"""pixel_values""": pixel_values, """labels""": labels}
         return config, inputs_dict




@require_torch
class   UpperCamelCase_       (     snake_case__				,  snake_case__				,  unittest.TestCase    ):
       lowerCamelCase_           =					(
           (
               ConvNextVaModel,
               ConvNextVaForImageClassification,
               ConvNextVaBackbone,
           )
           if is_torch_available()
           else ()
       )
       lowerCamelCase_           =					(
           {"feature-extraction": ConvNextVaModel, "image-classification": ConvNextVaForImageClassification}
           if is_torch_available()
           else {}
       )

       lowerCamelCase_           =					False
       lowerCamelCase_           =					False
       lowerCamelCase_           =					False
       lowerCamelCase_           =					False
       lowerCamelCase_           =					False


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



         """simple docstring"""



         SCREAMING_SNAKE_CASE__			=      ConvNextVaModelTester(self		)
         SCREAMING_SNAKE_CASE__			=      ConfigTester(self							,				config_class=_SCREAMING_SNAKE_CASE							,				has_text_modality=_SCREAMING_SNAKE_CASE							,				hidden_size=37		)


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



         """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      _snake_case  (							self    :Union[str, Any]		)   ->   Union[str, Any]:



         """simple docstring"""



         return


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



         """simple docstring"""



         pass


       @unittest.skip(reason="""ConvNextV2 does not support input and output embeddings"""		)
       def      _snake_case  (							self    :str		)   ->   Dict:



         """simple docstring"""



         pass


       @unittest.skip(reason="""ConvNextV2 does not use feedforward chunking"""		)
       def      _snake_case  (							self    :Dict		)   ->   Any:



         """simple docstring"""



         pass


       def      _snake_case  (							self    :List[str]		)   ->   int:



         """simple docstring"""



         if not self.model_tester.is_training:
           return

         for model_class in self.all_model_classes:
           SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs_with_labels()
           SCREAMING_SNAKE_CASE__			=      True

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

           SCREAMING_SNAKE_CASE__			=      model_class(_SCREAMING_SNAKE_CASE		)
           model.to(_SCREAMING_SNAKE_CASE		)
           model.train()
           SCREAMING_SNAKE_CASE__			=      self._prepare_for_class(_SCREAMING_SNAKE_CASE							,				_SCREAMING_SNAKE_CASE							,				return_labels=_SCREAMING_SNAKE_CASE		)
           SCREAMING_SNAKE_CASE__			=      model(**_SCREAMING_SNAKE_CASE		).loss
           loss.backward()


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



         """simple docstring"""



         if not self.model_tester.is_training:
           return

         for model_class in self.all_model_classes:
           SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs_with_labels()
           SCREAMING_SNAKE_CASE__			=      False
           SCREAMING_SNAKE_CASE__			=      True

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

           SCREAMING_SNAKE_CASE__			=      model_class(_SCREAMING_SNAKE_CASE		)
           model.to(_SCREAMING_SNAKE_CASE		)
           model.gradient_checkpointing_enable()
           model.train()
           SCREAMING_SNAKE_CASE__			=      self._prepare_for_class(_SCREAMING_SNAKE_CASE							,				_SCREAMING_SNAKE_CASE							,				return_labels=_SCREAMING_SNAKE_CASE		)
           SCREAMING_SNAKE_CASE__			=      model(**_SCREAMING_SNAKE_CASE		).loss
           loss.backward()


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



         """simple docstring"""



         SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs_for_common()

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

           SCREAMING_SNAKE_CASE__			=      ["""pixel_values"""]
           self.assertListEqual(arg_names[:1]							,				_SCREAMING_SNAKE_CASE		)


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



         """simple docstring"""



         SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE		)


       def      _snake_case  (							self    :Dict		)   ->   Dict:



         """simple docstring"""



         def check_hidden_states_output(__A    :int							,				__A    :List[Any]							,				__A    :Tuple		):
           SCREAMING_SNAKE_CASE__			=      model_class(_SCREAMING_SNAKE_CASE		)
           model.to(_SCREAMING_SNAKE_CASE		)
           model.eval()

           with torch.no_grad():
             SCREAMING_SNAKE_CASE__			=      model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE							,				_SCREAMING_SNAKE_CASE		)		)

           SCREAMING_SNAKE_CASE__			=      outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

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

         SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           SCREAMING_SNAKE_CASE__			=      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"]
           SCREAMING_SNAKE_CASE__			=      True

           check_hidden_states_output(_SCREAMING_SNAKE_CASE							,				_SCREAMING_SNAKE_CASE							,				_SCREAMING_SNAKE_CASE		)


       def      _snake_case  (							self    :Tuple		)   ->   List[Any]:



         """simple docstring"""



         SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE		)


       @slow
       def      _snake_case  (							self    :Union[str, Any]		)   ->   Optional[int]:



         """simple docstring"""



         for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           SCREAMING_SNAKE_CASE__			=      ConvNextVaModel.from_pretrained(_SCREAMING_SNAKE_CASE		)
           self.assertIsNotNone(_SCREAMING_SNAKE_CASE		)






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




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


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



         """simple docstring"""



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


       @slow
       def      _snake_case  (							self    :Optional[Any]		)   ->   Tuple:



         """simple docstring"""



         SCREAMING_SNAKE_CASE__			=      ConvNextVaForImageClassification.from_pretrained("""facebook/convnextv2-tiny-1k-224"""		).to(_SCREAMING_SNAKE_CASE		)

         SCREAMING_SNAKE_CASE__			=      self.default_image_processor
         SCREAMING_SNAKE_CASE__			=      prepare_img()
         SCREAMING_SNAKE_CASE__			=      preprocessor(images=_SCREAMING_SNAKE_CASE							,				return_tensors="""pt"""		).to(_SCREAMING_SNAKE_CASE		)

         # forward pass
         with torch.no_grad():
           SCREAMING_SNAKE_CASE__			=      model(**_SCREAMING_SNAKE_CASE		)

         # verify the logits
         SCREAMING_SNAKE_CASE__			=      torch.Size((1, 1000)		)
         self.assertEqual(outputs.logits.shape							,				_SCREAMING_SNAKE_CASE		)

         SCREAMING_SNAKE_CASE__			=      torch.tensor([0.9_9_9_6, 0.1_9_6_6, -0.4_3_8_6]		).to(_SCREAMING_SNAKE_CASE		)
         self.assertTrue(torch.allclose(outputs.logits[0, :3]							,				_SCREAMING_SNAKE_CASE							,				atol=1E-4		)		)






from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowerCamelCase   				=			logging.get_logger(__name__)




class   UpperCamelCase_       (     UpperCamelCase__    ):
							lowerCamelCase_           =					"timm_backbone"


							def __init__(							self    :Union[str, Any]							,				__A    :str=None							,				__A    :Union[str, Any]=3							,				__A    :str=True							,				__A    :Any=True							,				__A    :Optional[Any]=None							,				**__A    :List[str]							,				)   ->   Tuple:



									"""simple docstring"""



									super().__init__(**__A		)
									SCREAMING_SNAKE_CASE__			=      backbone
									SCREAMING_SNAKE_CASE__			=      num_channels
									SCREAMING_SNAKE_CASE__			=      features_only
									SCREAMING_SNAKE_CASE__			=      use_pretrained_backbone
									SCREAMING_SNAKE_CASE__			=      True
									SCREAMING_SNAKE_CASE__			=      out_indices if out_indices is not None else (-1,)