






"""simple docstring"""





_SCREAMING_SNAKE_CASE   :							Optional[int]          =		[
    '''Audio''',
    '''Array2D''',
    '''Array3D''',
    '''Array4D''',
    '''Array5D''',
    '''ClassLabel''',
    '''Features''',
    '''Sequence''',
    '''Value''',
    '''Image''',
    '''Translation''',
    '''TranslationVariableLanguages''',
]
from .audio import Audio
from .features import ArrayaD, ArrayaD, ArrayaD, ArrayaD, ClassLabel, Features, Sequence, Value
from .image import Image
from .translation import Translation, TranslationVariableLanguages












"""simple docstring"""





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






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

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






    def   UpperCamelCase			(			self	:				Dict   )	->						Union[str, Any]:
      lowerCamelCase_       	=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

      lowerCamelCase_       	=		None
      if self.use_labels:
        lowerCamelCase_       	=		ids_tensor([self.batch_size]			,		self.type_sequence_label_size   )

      lowerCamelCase_       	=		self.get_config()

      return config, pixel_values, labels






    def   UpperCamelCase			(			self	:				Union[str, Any]   )	->						Union[str, Any]:
      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   UpperCamelCase			(			self	:				int			,		__SCREAMING_SNAKE_CASE	:				Any			,		__SCREAMING_SNAKE_CASE	:				Any			,		__SCREAMING_SNAKE_CASE	:				int   )	->						Any:
      lowerCamelCase_       	=		ViTMSNModel(config=__SCREAMING_SNAKE_CASE   )
      model.to(__SCREAMING_SNAKE_CASE   )
      model.eval()
      lowerCamelCase_       	=		model(__SCREAMING_SNAKE_CASE   )
      self.parent.assertEqual(result.last_hidden_state.shape			,		(self.batch_size, self.seq_length, self.hidden_size)   )






    def   UpperCamelCase			(			self	:				int			,		__SCREAMING_SNAKE_CASE	:				Dict			,		__SCREAMING_SNAKE_CASE	:				str			,		__SCREAMING_SNAKE_CASE	:				str   )	->						List[str]:
      lowerCamelCase_       	=		self.type_sequence_label_size
      lowerCamelCase_       	=		ViTMSNForImageClassification(__SCREAMING_SNAKE_CASE   )
      model.to(__SCREAMING_SNAKE_CASE   )
      model.eval()
      lowerCamelCase_       	=		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
      lowerCamelCase_       	=		1
      lowerCamelCase_       	=		ViTMSNForImageClassification(__SCREAMING_SNAKE_CASE   )
      model.to(__SCREAMING_SNAKE_CASE   )
      model.eval()

      lowerCamelCase_       	=		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
      lowerCamelCase_       	=		model(__SCREAMING_SNAKE_CASE   )
      self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.type_sequence_label_size)   )






    def   UpperCamelCase			(			self	:				List[str]   )	->						Tuple:
      lowerCamelCase_       	=		self.prepare_config_and_inputs()
      lowerCamelCase_	,      lowerCamelCase_	,      lowerCamelCase_       	=		config_and_inputs
      lowerCamelCase_       	=		{'pixel_values': pixel_values}
      return config, inputs_dict






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

    SCREAMING_SNAKE_CASE		:	Dict		=					False
    SCREAMING_SNAKE_CASE		:	Union[str, Any]		=					False
    SCREAMING_SNAKE_CASE		:	Optional[Any]		=					False
    SCREAMING_SNAKE_CASE		:	str		=					False






    def   UpperCamelCase			(			self	:				List[Any]   )	->						List[str]:
      lowerCamelCase_       	=		ViTMSNModelTester(self   )
      lowerCamelCase_       	=		ConfigTester(self			,		config_class=__SCREAMING_SNAKE_CASE			,		has_text_modality=__SCREAMING_SNAKE_CASE			,		hidden_size=37   )






    def   UpperCamelCase			(			self	:				Union[str, Any]   )	->						Optional[Any]:
      self.config_tester.run_common_tests()






    @unittest.skip(reason='ViTMSN does not use inputs_embeds'   )
    def   UpperCamelCase			(			self	:				Tuple   )	->						Union[str, Any]:
      pass






    def   UpperCamelCase			(			self	:				List[Any]   )	->						List[Any]:
      lowerCamelCase_	,      lowerCamelCase_       	=		self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        lowerCamelCase_       	=		model_class(__SCREAMING_SNAKE_CASE   )
        self.assertIsInstance(model.get_input_embeddings()			,		(nn.Module)   )
        lowerCamelCase_       	=		model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(__SCREAMING_SNAKE_CASE			,		nn.Linear   )   )






    def   UpperCamelCase			(			self	:				Optional[int]   )	->						Any:
      lowerCamelCase_	,      lowerCamelCase_       	=		self.model_tester.prepare_config_and_inputs_for_common()

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

        lowerCamelCase_       	=		['pixel_values']
        self.assertListEqual(arg_names[:1]			,		__SCREAMING_SNAKE_CASE   )






    def   UpperCamelCase			(			self	:				Optional[Any]   )	->						List[Any]:
      lowerCamelCase_       	=		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE   )






    def   UpperCamelCase			(			self	:				Union[str, Any]   )	->						Optional[int]:
      lowerCamelCase_       	=		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE   )






    @slow
    def   UpperCamelCase			(			self	:				int   )	->						List[Any]:
      for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowerCamelCase_       	=		ViTMSNModel.from_pretrained(__SCREAMING_SNAKE_CASE   )
        self.assertIsNotNone(__SCREAMING_SNAKE_CASE   )







def 	lowerCamelCase__			(				) ->							List[Any]:
  lowerCamelCase_       	=		Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'					)
  return image






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






    @cached_property
    def   UpperCamelCase			(			self	:				Optional[int]   )	->						Any:
      return ViTImageProcessor.from_pretrained('facebook/vit-msn-small'   ) if is_vision_available() else None






    @slow
    def   UpperCamelCase			(			self	:				Dict   )	->						Any:
      torch.manual_seed(2   )
      lowerCamelCase_       	=		ViTMSNForImageClassification.from_pretrained('facebook/vit-msn-small'   ).to(__SCREAMING_SNAKE_CASE   )

      lowerCamelCase_       	=		self.default_image_processor
      lowerCamelCase_       	=		prepare_img()
      lowerCamelCase_       	=		image_processor(images=__SCREAMING_SNAKE_CASE			,		return_tensors='pt'   ).to(__SCREAMING_SNAKE_CASE   )

      # forward pass
      with torch.no_grad():
        lowerCamelCase_       	=		model(**__SCREAMING_SNAKE_CASE   )

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

      lowerCamelCase_       	=		torch.tensor([-0.0_803, -0.4_454, -0.2_375]   ).to(__SCREAMING_SNAKE_CASE   )

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




