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


 def __init__(					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_=13					,  SCREAMING_SNAKE_CASE_=30					,  SCREAMING_SNAKE_CASE_=2					,  SCREAMING_SNAKE_CASE_=3					,  SCREAMING_SNAKE_CASE_=True					,  SCREAMING_SNAKE_CASE_=True					,  SCREAMING_SNAKE_CASE_=32					,  SCREAMING_SNAKE_CASE_=5					,  SCREAMING_SNAKE_CASE_=4					,  SCREAMING_SNAKE_CASE_=37					,  SCREAMING_SNAKE_CASE_="gelu"					,  SCREAMING_SNAKE_CASE_=0.1					,  SCREAMING_SNAKE_CASE_=0.1					,  SCREAMING_SNAKE_CASE_=10					,  SCREAMING_SNAKE_CASE_=0.0_2					,  SCREAMING_SNAKE_CASE_=None					,  ):


   '''simple docstring'''




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

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


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   List[str]    		=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

   lowercase__  :   Tuple    		=      self.get_config()

   return config, pixel_values, labels


 def      lowercase__     (					self):


   '''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					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   lowercase__  :   Optional[int]    		=      ViTMSNModel(config=SCREAMING_SNAKE_CASE_)
   model.to(SCREAMING_SNAKE_CASE_)
   model.eval()
   lowercase__  :   Dict    		=      model(SCREAMING_SNAKE_CASE_)
   self.parent.assertEqual(result.last_hidden_state.shape					,  (self.batch_size, self.seq_length, self.hidden_size))


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   lowercase__  :   Tuple    		=      self.type_sequence_label_size
   lowercase__  :   Optional[Any]    		=      ViTMSNForImageClassification(SCREAMING_SNAKE_CASE_)
   model.to(SCREAMING_SNAKE_CASE_)
   model.eval()
   lowercase__  :   Optional[Any]    		=      model(SCREAMING_SNAKE_CASE_					,  labels=SCREAMING_SNAKE_CASE_)
   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
   lowercase__  :   List[str]    		=      1
   lowercase__  :   Any    		=      ViTMSNForImageClassification(SCREAMING_SNAKE_CASE_)
   model.to(SCREAMING_SNAKE_CASE_)
   model.eval()

   lowercase__  :   int    		=      floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
   lowercase__  :   Dict    		=      model(SCREAMING_SNAKE_CASE_)
   self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.type_sequence_label_size))



 def      lowercase__     (					self):


   '''simple docstring'''




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



@require_torch
class       _snake_case   (	UpperCAmelCase_  ,						UpperCAmelCase_  ,						unittest.TestCase							):
 __lowerCAmelCase  :     Union[str, Any]        =			(ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else ()
 __lowerCAmelCase  :     Optional[Any]        =			(
     {'feature-extraction': ViTMSNModel, 'image-classification': ViTMSNForImageClassification}
     if is_torch_available()
     else {}
 )

 __lowerCAmelCase  :     Optional[int]        =			False
 __lowerCAmelCase  :     List[Any]        =			False
 __lowerCAmelCase  :     str        =			False
 __lowerCAmelCase  :     Tuple        =			False


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Any    		=      ViTMSNModelTester(self)
   lowercase__  :   str    		=      ConfigTester(self					,  config_class=SCREAMING_SNAKE_CASE_					,  has_text_modality=SCREAMING_SNAKE_CASE_					,  hidden_size=37)


 def      lowercase__     (					self):


   '''simple docstring'''




   self.config_tester.run_common_tests()


 @unittest.skip(reason="""ViTMSN does not use inputs_embeds""")
 def      lowercase__     (					self):


   '''simple docstring'''




   pass


 def      lowercase__     (					self):


   '''simple docstring'''




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

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


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__		,    lowercase__  :   Tuple    		=      self.model_tester.prepare_config_and_inputs_for_common()

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

     lowercase__  :   Any    		=      ["""pixel_values"""]
     self.assertListEqual(arg_names[:1]					,  SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   int    		=      self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Tuple    		=      self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_)



 @slow
 def      lowercase__     (					self):


   '''simple docstring'''




   for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     lowercase__  :   int    		=      ViTMSNModel.from_pretrained(SCREAMING_SNAKE_CASE_)
     self.assertIsNotNone(SCREAMING_SNAKE_CASE_)

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


  '''simple docstring'''






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



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


 @cached_property
 def      lowercase__     (					self):


   '''simple docstring'''




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



 @slow
 def      lowercase__     (					self):


   '''simple docstring'''




   torch.manual_seed(2)
   lowercase__  :   List[Any]    		=      ViTMSNForImageClassification.from_pretrained("""facebook/vit-msn-small""").to(SCREAMING_SNAKE_CASE_)

   lowercase__  :   List[Any]    		=      self.default_image_processor
   lowercase__  :   Optional[Any]    		=      prepare_img()
   lowercase__  :   Optional[int]    		=      image_processor(images=SCREAMING_SNAKE_CASE_					,  return_tensors="""pt""").to(SCREAMING_SNAKE_CASE_)

   # forward pass
   with torch.no_grad():
     lowercase__  :   List[Any]    		=      model(**SCREAMING_SNAKE_CASE_)

   # verify the logits
   lowercase__  :   Optional[int]    		=      torch.Size((1, 10_00))
   self.assertEqual(outputs.logits.shape					,  SCREAMING_SNAKE_CASE_)

   lowercase__  :   Optional[int]    		=      torch.tensor([-0.0_8_0_3, -0.4_4_5_4, -0.2_3_7_5]).to(SCREAMING_SNAKE_CASE_)

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








from abc import ABC, abstractmethod
from argparse import ArgumentParser



class       _snake_case   (	UpperCAmelCase_							):


 @staticmethod
 @abstractmethod
 def      lowercase__     (					SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   raise NotImplementedError()



 @abstractmethod
 def      lowercase__     (					self):


   '''simple docstring'''




   raise NotImplementedError()






