





import inspect
import unittest

from transformers import ViTMSNConfig
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 torch import nn

     from transformers import ViTMSNForImageClassification, ViTMSNModel
     from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from PIL import Image

     from transformers import ViTImageProcessor







class 				__lowerCAmelCase       :







      def __init__(    self		,      lowerCAmelCase__		,      lowerCAmelCase__=1_3		,      lowerCAmelCase__=3_0		,      lowerCAmelCase__=2		,      lowerCAmelCase__=3		,      lowerCAmelCase__=True		,      lowerCAmelCase__=True		,      lowerCAmelCase__=3_2		,      lowerCAmelCase__=5		,      lowerCAmelCase__=4		,      lowerCAmelCase__=3_7		,      lowerCAmelCase__="gelu"		,      lowerCAmelCase__=0.1		,      lowerCAmelCase__=0.1		,      lowerCAmelCase__=1_0		,      lowerCAmelCase__=0.02		,      lowerCAmelCase__=None		,      )   ->						List[str]:



        '''simple docstring'''




        a__	:					int										=parent
        a__	:					Dict										=batch_size
        a__	:					Any										=image_size
        a__	:					int										=patch_size
        a__	:					Union[str, Any]										=num_channels
        a__	:					List[str]										=is_training
        a__	:					str										=use_labels
        a__	:					Tuple										=hidden_size
        a__	:					str										=num_hidden_layers
        a__	:					Dict										=num_attention_heads
        a__	:					Union[str, Any]										=intermediate_size
        a__	:					int										=hidden_act
        a__	:					str										=hidden_dropout_prob
        a__	:					Optional[int]										=attention_probs_dropout_prob
        a__	:					Any										=type_sequence_label_size
        a__	:					List[str]										=initializer_range
        a__	:					Tuple										=scope

        # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
        a__	:					Optional[int]										=(image_size // patch_size) ** 2
        a__	:					Optional[Any]										=num_patches + 1







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



        '''simple docstring'''




        a__	:					Tuple										=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

        a__	:					Any										=None
        if self.use_labels:
          a__	:					Optional[Any]										=ids_tensor([self.batch_size]		,      self.type_sequence_label_size							)

        a__	:					Tuple										=self.get_config()

        return config, pixel_values, labels







      def    _lowercase  (    self							)   ->						List[str]:



        '''simple docstring'''




        return ViTMSNConfig(
            image_size=self.image_size		,      patch_size=self.patch_size		,      num_channels=self.num_channels		,      hidden_size=self.hidden_size		,      num_hidden_layers=self.num_hidden_layers		,      num_attention_heads=self.num_attention_heads		,      intermediate_size=self.intermediate_size		,      hidden_act=self.hidden_act		,      hidden_dropout_prob=self.hidden_dropout_prob		,      attention_probs_dropout_prob=self.attention_probs_dropout_prob		,      initializer_range=self.initializer_range		,      )







      def    _lowercase  (    self		,      lowerCAmelCase__		,      lowerCAmelCase__		,      lowerCAmelCase__							)   ->						Tuple:



        '''simple docstring'''




        a__	:					Any										=ViTMSNModel(config=lowerCAmelCase__							)
        model.to(lowerCAmelCase__							)
        model.eval()
        a__	:					Dict										=model(lowerCAmelCase__							)
        self.parent.assertEqual(result.last_hidden_state.shape		,      (self.batch_size, self.seq_length, self.hidden_size)							)







      def    _lowercase  (    self		,      lowerCAmelCase__		,      lowerCAmelCase__		,      lowerCAmelCase__							)   ->						str:



        '''simple docstring'''




        a__	:					Optional[Any]										=self.type_sequence_label_size
        a__	:					Union[str, Any]										=ViTMSNForImageClassification(lowerCAmelCase__							)
        model.to(lowerCAmelCase__							)
        model.eval()
        a__	:					Dict										=model(lowerCAmelCase__		,      labels=lowerCAmelCase__							)
        print("Pixel and labels shape: {pixel_values.shape}, {labels.shape}"							)
        print("Labels: {labels}"							)
        self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.type_sequence_label_size)							)

        # test greyscale images
        a__	:					Tuple										=1
        a__	:					str										=ViTMSNForImageClassification(lowerCAmelCase__							)
        model.to(lowerCAmelCase__							)
        model.eval()

        a__	:					List[Any]										=floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
        a__	:					List[str]										=model(lowerCAmelCase__							)
        self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.type_sequence_label_size)							)







      def    _lowercase  (    self							)   ->						Optional[Any]:



        '''simple docstring'''




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







@require_torch
class 				__lowerCAmelCase       (    UpperCamelCase__						,				UpperCamelCase__						,				unittest.TestCase):
      _lowercase				:					Optional[int]						=			(ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else ()
      _lowercase				:					Optional[int]						=			(
          {"""feature-extraction""": ViTMSNModel, """image-classification""": ViTMSNForImageClassification}
          if is_torch_available()
          else {}
      )

      _lowercase				:					int						=			False
      _lowercase				:					Any						=			False
      _lowercase				:					Optional[int]						=			False
      _lowercase				:					List[Any]						=			False







      def    _lowercase  (    self							)   ->						str:



        '''simple docstring'''




        a__	:					int										=ViTMSNModelTester(self							)
        a__	:					Optional[Any]										=ConfigTester(self		,      config_class=lowerCAmelCase__		,      has_text_modality=lowerCAmelCase__		,      hidden_size=3_7							)







      def    _lowercase  (    self							)   ->						str:



        '''simple docstring'''




        self.config_tester.run_common_tests()







      @unittest.skip(reason="ViTMSN does not use inputs_embeds"							)
      def    _lowercase  (    self							)   ->						Optional[Any]:



        '''simple docstring'''




        pass







      def    _lowercase  (    self							)   ->						str:



        '''simple docstring'''




        a__					,							a__	:					Union[str, Any]										=self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
          a__	:					Optional[Any]										=model_class(lowerCAmelCase__							)
          self.assertIsInstance(model.get_input_embeddings()		,      (nn.Module)							)
          a__	:					List[str]										=model.get_output_embeddings()
          self.assertTrue(x is None or isinstance(lowerCAmelCase__		,      nn.Linear							)							)







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



        '''simple docstring'''




        a__					,							a__	:					Optional[int]										=self.model_tester.prepare_config_and_inputs_for_common()

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

          a__	:					str										=["pixel_values"]
          self.assertListEqual(arg_names[:1]		,      lowerCAmelCase__							)







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



        '''simple docstring'''




        a__	:					List[str]										=self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCAmelCase__							)







      def    _lowercase  (    self							)   ->						Tuple:



        '''simple docstring'''




        a__	:					List[Any]										=self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__							)







      @slow
      def    _lowercase  (    self							)   ->						List[Any]:



        '''simple docstring'''




        for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          a__	:					Optional[int]										=ViTMSNModel.from_pretrained(lowerCAmelCase__							)
          self.assertIsNotNone(lowerCAmelCase__							)
def      _A       (		):





  """simple docstring"""



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







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







      @cached_property
      def    _lowercase  (    self							)   ->						Dict:



        '''simple docstring'''




        return ViTImageProcessor.from_pretrained("facebook/vit-msn-small"							) if is_vision_available() else None







      @slow
      def    _lowercase  (    self							)   ->						Union[str, Any]:



        '''simple docstring'''




        torch.manual_seed(2							)
        a__	:					Tuple										=ViTMSNForImageClassification.from_pretrained("facebook/vit-msn-small"							).to(lowerCAmelCase__							)

        a__	:					Optional[int]										=self.default_image_processor
        a__	:					Tuple										=prepare_img()
        a__	:					Union[str, Any]										=image_processor(images=lowerCAmelCase__		,      return_tensors="pt"							).to(lowerCAmelCase__							)

        # forward pass
        with torch.no_grad():
          a__	:					Tuple										=model(**lowerCAmelCase__							)

        # verify the logits
        a__	:					Tuple										=torch.Size((1, 1_0_0_0)							)
        self.assertEqual(outputs.logits.shape		,      lowerCAmelCase__							)

        a__	:					Tuple										=torch.tensor([-0.08_03, -0.44_54, -0.23_75]							).to(lowerCAmelCase__							)

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













import numpy as np
def      _A       (		SCREAMING_SNAKE_CASE     :   np.array						):





  """simple docstring"""



  return 1 / (1 + np.exp(-vector						))


if __name__ == "__main__":
     import doctest

     doctest.testmod()






