





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


   def __init__( self    ,   _UpperCAmelCase    ,   _UpperCAmelCase=13    ,   _UpperCAmelCase=30    ,   _UpperCAmelCase=2    ,   _UpperCAmelCase=3    ,   _UpperCAmelCase=True    ,   _UpperCAmelCase=True    ,   _UpperCAmelCase=32    ,   _UpperCAmelCase=5    ,   _UpperCAmelCase=4    ,   _UpperCAmelCase=37    ,   _UpperCAmelCase="gelu"    ,   _UpperCAmelCase=0.1    ,   _UpperCAmelCase=0.1    ,   _UpperCAmelCase=10    ,   _UpperCAmelCase=0.02    ,   _UpperCAmelCase=None    ,   ):
       A       :       Optional[int]							=						parent
       A       :       Optional[Any]							=						batch_size
       A       :       int							=						image_size
       A       :       List[Any]							=						patch_size
       A       :       Optional[Any]							=						num_channels
       A       :       int							=						is_training
       A       :       Optional[Any]							=						use_labels
       A       :       int							=						hidden_size
       A       :       Union[str, Any]							=						num_hidden_layers
       A       :       Tuple							=						num_attention_heads
       A       :       Any							=						intermediate_size
       A       :       Union[str, Any]							=						hidden_act
       A       :       List[Any]							=						hidden_dropout_prob
       A       :       Optional[Any]							=						attention_probs_dropout_prob
       A       :       str							=						type_sequence_label_size
       A       :       Optional[Any]							=						initializer_range
       A       :       List[Any]							=						scope

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


   def 					snake_case				( self					):
       A       :       Optional[Any]							=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

       A       :       Dict							=						self.get_config()

       return config, pixel_values, labels


   def 					snake_case				( self					):
       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 					snake_case				( self    ,   _UpperCAmelCase    ,   _UpperCAmelCase    ,   _UpperCAmelCase					):
       A       :       Optional[Any]							=						ViTMSNModel(config=_UpperCAmelCase					)
       model.to(_UpperCAmelCase					)
       model.eval()
       A       :       Optional[int]							=						model(_UpperCAmelCase					)
       self.parent.assertEqual(result.last_hidden_state.shape    ,   (self.batch_size, self.seq_length, self.hidden_size)					)


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

       A       :       Union[str, Any]							=						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]					)
       A       :       int							=						model(_UpperCAmelCase					)
       self.parent.assertEqual(result.logits.shape    ,   (self.batch_size, self.type_sequence_label_size)					)


   def 					snake_case				( self					):
       A       :       Optional[int]							=						self.prepare_config_and_inputs()
       A       :       Union[str, Any]							=						config_and_inputs
       A       :       Tuple							=						{'pixel_values': pixel_values}
       return config, inputs_dict







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

   _UpperCamelCase       		=   False
   _UpperCamelCase       		=   False
   _UpperCamelCase       		=   False
   _UpperCamelCase       		=   False


   def 					snake_case				( self					):
       A       :       Tuple							=						ViTMSNModelTester(self					)
       A       :       int							=						ConfigTester(self    ,   config_class=_UpperCAmelCase    ,   has_text_modality=_UpperCAmelCase    ,   hidden_size=37					)


   def 					snake_case				( self					):
       self.config_tester.run_common_tests()


   @unittest.skip(reason='''ViTMSN does not use inputs_embeds'''					)
   def 					snake_case				( self					):
       pass


   def 					snake_case				( self					):
       A       :       Optional[int]							=						self.model_tester.prepare_config_and_inputs_for_common()

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


   def 					snake_case				( self					):
       A       :       str							=						self.model_tester.prepare_config_and_inputs_for_common()

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

           A       :       Tuple							=						['pixel_values']
           self.assertListEqual(arg_names[:1]    ,   _UpperCAmelCase					)


   def 					snake_case				( self					):
       A       :       int							=						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_UpperCAmelCase					)


   def 					snake_case				( self					):
       A       :       Optional[Any]							=						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*_UpperCAmelCase					)


   @slow
   def 					snake_case				( self					):







       for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           A       :       List[Any]							=						ViTMSNModel.from_pretrained(_UpperCAmelCase					)
           self.assertIsNotNone(_UpperCAmelCase					)







def 							_lowerCamelCase(  )		->  Any:
    A       :       int							=						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
    return image





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


   @cached_property
   def 					snake_case				( self					):
       return ViTImageProcessor.from_pretrained('''facebook/vit-msn-small'''					) if is_vision_available() else None


   @slow
   def 					snake_case				( self					):
       torch.manual_seed(2					)
       A       :       List[Any]							=						ViTMSNForImageClassification.from_pretrained('''facebook/vit-msn-small'''					).to(_UpperCAmelCase					)

       A       :       Optional[int]							=						self.default_image_processor
       A       :       Dict							=						prepare_img()
       A       :       Union[str, Any]							=						image_processor(images=_UpperCAmelCase    ,   return_tensors='''pt'''					).to(_UpperCAmelCase					)

       # forward pass
       with torch.no_grad():
           A       :       int							=						model(**_UpperCAmelCase					)

       # verify the logits
       A       :       Any							=						torch.Size((1, 1_000)					)
       self.assertEqual(outputs.logits.shape    ,   _UpperCAmelCase					)

       A       :       int							=						torch.tensor([-0.0803, -0.4454, -0.2375]					).to(_UpperCAmelCase					)

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







'''simple docstring'''






snake_case_     =	[
    9_99,
    8_00,
    7_99,
    6_00,
    5_99,
    5_00,
    4_00,
    3_99,
    3_77,
    3_55,
    3_33,
    3_11,
    2_88,
    2_66,
    2_44,
    2_22,
    2_00,
    1_99,
    1_77,
    1_55,
    1_33,
    1_11,
    88,
    66,
    44,
    22,
    0,
]

snake_case_     =	[
    9_99,
    9_76,
    9_52,
    9_28,
    9_05,
    8_82,
    8_58,
    8_57,
    8_10,
    7_62,
    7_15,
    7_14,
    5_72,
    4_29,
    4_28,
    2_86,
    2_85,
    2_38,
    1_90,
    1_43,
    1_42,
    1_18,
    95,
    71,
    47,
    24,
    0,
]

snake_case_     =	[
    9_99,
    9_88,
    9_77,
    9_66,
    9_55,
    9_44,
    9_33,
    9_22,
    9_11,
    9_00,
    8_99,
    8_79,
    8_59,
    8_40,
    8_20,
    8_00,
    7_99,
    7_66,
    7_33,
    7_00,
    6_99,
    6_50,
    6_00,
    5_99,
    5_00,
    4_99,
    4_00,
    3_99,
    3_50,
    3_00,
    2_99,
    2_66,
    2_33,
    2_00,
    1_99,
    1_79,
    1_59,
    1_40,
    1_20,
    1_00,
    99,
    88,
    77,
    66,
    55,
    44,
    33,
    22,
    11,
    0,
]

snake_case_     =	[
    9_99,
    9_95,
    9_92,
    9_89,
    9_85,
    9_81,
    9_78,
    9_75,
    9_71,
    9_67,
    9_64,
    9_61,
    9_57,
    9_56,
    9_51,
    9_47,
    9_42,
    9_37,
    9_33,
    9_28,
    9_23,
    9_19,
    9_14,
    9_13,
    9_08,
    9_03,
    8_97,
    8_92,
    8_87,
    8_81,
    8_76,
    8_71,
    8_70,
    8_64,
    8_58,
    8_52,
    8_46,
    8_40,
    8_34,
    8_28,
    8_27,
    8_20,
    8_13,
    8_06,
    7_99,
    7_92,
    7_85,
    7_84,
    7_77,
    7_70,
    7_63,
    7_56,
    7_49,
    7_42,
    7_41,
    7_33,
    7_24,
    7_16,
    7_07,
    6_99,
    6_98,
    6_88,
    6_77,
    6_66,
    6_56,
    6_55,
    6_45,
    6_34,
    6_23,
    6_13,
    6_12,
    5_98,
    5_84,
    5_70,
    5_69,
    5_55,
    5_41,
    5_27,
    5_26,
    5_05,
    4_84,
    4_83,
    4_62,
    4_40,
    4_39,
    3_96,
    3_95,
    3_52,
    3_51,
    3_08,
    3_07,
    2_64,
    2_63,
    2_20,
    2_19,
    1_76,
    1_32,
    88,
    44,
    0,
]

snake_case_     =	[
    9_99,
    9_97,
    9_95,
    9_92,
    9_90,
    9_88,
    9_86,
    9_84,
    9_81,
    9_79,
    9_77,
    9_75,
    9_72,
    9_70,
    9_68,
    9_66,
    9_64,
    9_61,
    9_59,
    9_57,
    9_56,
    9_54,
    9_51,
    9_49,
    9_46,
    9_44,
    9_41,
    9_39,
    9_36,
    9_34,
    9_31,
    9_29,
    9_26,
    9_24,
    9_21,
    9_19,
    9_16,
    9_14,
    9_13,
    9_10,
    9_07,
    9_05,
    9_02,
    8_99,
    8_96,
    8_93,
    8_91,
    8_88,
    8_85,
    8_82,
    8_79,
    8_77,
    8_74,
    8_71,
    8_70,
    8_67,
    8_64,
    8_61,
    8_58,
    8_55,
    8_52,
    8_49,
    8_46,
    8_43,
    8_40,
    8_37,
    8_34,
    8_31,
    8_28,
    8_27,
    8_24,
    8_21,
    8_17,
    8_14,
    8_11,
    8_08,
    8_04,
    8_01,
    7_98,
    7_95,
    7_91,
    7_88,
    7_85,
    7_84,
    7_80,
    7_77,
    7_74,
    7_70,
    7_66,
    7_63,
    7_60,
    7_56,
    7_52,
    7_49,
    7_46,
    7_42,
    7_41,
    7_37,
    7_33,
    7_30,
    7_26,
    7_22,
    7_18,
    7_14,
    7_10,
    7_07,
    7_03,
    6_99,
    6_98,
    6_94,
    6_90,
    6_85,
    6_81,
    6_77,
    6_73,
    6_69,
    6_64,
    6_60,
    6_56,
    6_55,
    6_50,
    6_46,
    6_41,
    6_36,
    6_32,
    6_27,
    6_22,
    6_18,
    6_13,
    6_12,
    6_07,
    6_02,
    5_96,
    5_91,
    5_86,
    5_80,
    5_75,
    5_70,
    5_69,
    5_63,
    5_57,
    5_51,
    5_45,
    5_39,
    5_33,
    5_27,
    5_26,
    5_19,
    5_12,
    5_05,
    4_98,
    4_91,
    4_84,
    4_83,
    4_74,
    4_66,
    4_57,
    4_49,
    4_40,
    4_39,
    4_28,
    4_18,
    4_07,
    3_96,
    3_95,
    3_81,
    3_66,
    3_52,
    3_51,
    3_30,
    3_08,
    3_07,
    2_86,
    2_64,
    2_63,
    2_42,
    2_20,
    2_19,
    1_76,
    1_75,
    1_32,
    1_31,
    88,
    44,
    0,
]

snake_case_     =	[
    9_99,
    9_91,
    9_82,
    9_74,
    9_66,
    9_58,
    9_50,
    9_41,
    9_33,
    9_25,
    9_16,
    9_08,
    9_00,
    8_99,
    8_74,
    8_50,
    8_25,
    8_00,
    7_99,
    7_00,
    6_00,
    5_00,
    4_00,
    3_00,
    2_00,
    1_00,
    0,
]

snake_case_     =	[
    9_99,
    9_92,
    9_85,
    9_78,
    9_71,
    9_64,
    9_57,
    9_49,
    9_42,
    9_35,
    9_28,
    9_21,
    9_14,
    9_07,
    9_00,
    8_99,
    8_79,
    8_59,
    8_40,
    8_20,
    8_00,
    7_99,
    7_66,
    7_33,
    7_00,
    6_99,
    6_50,
    6_00,
    5_99,
    5_00,
    4_99,
    4_00,
    3_99,
    3_00,
    2_99,
    2_00,
    1_99,
    1_00,
    99,
    0,
]

snake_case_     =	[
    9_99,
    9_96,
    9_92,
    9_89,
    9_85,
    9_82,
    9_79,
    9_75,
    9_72,
    9_68,
    9_65,
    9_61,
    9_58,
    9_55,
    9_51,
    9_48,
    9_44,
    9_41,
    9_38,
    9_34,
    9_31,
    9_27,
    9_24,
    9_20,
    9_17,
    9_14,
    9_10,
    9_07,
    9_03,
    9_00,
    8_99,
    8_91,
    8_84,
    8_76,
    8_69,
    8_61,
    8_53,
    8_46,
    8_38,
    8_30,
    8_23,
    8_15,
    8_08,
    8_00,
    7_99,
    7_88,
    7_77,
    7_66,
    7_55,
    7_44,
    7_33,
    7_22,
    7_11,
    7_00,
    6_99,
    6_88,
    6_77,
    6_66,
    6_55,
    6_44,
    6_33,
    6_22,
    6_11,
    6_00,
    5_99,
    5_85,
    5_71,
    5_57,
    5_42,
    5_28,
    5_14,
    5_00,
    4_99,
    4_85,
    4_71,
    4_57,
    4_42,
    4_28,
    4_14,
    4_00,
    3_99,
    3_79,
    3_59,
    3_40,
    3_20,
    3_00,
    2_99,
    2_79,
    2_59,
    2_40,
    2_20,
    2_00,
    1_99,
    1_66,
    1_33,
    1_00,
    99,
    66,
    33,
    0,
]
