






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							,       ):
								"""simple docstring"""
								__magic_name__		:Tuple   =   parent
								__magic_name__		:Dict   =   batch_size
								__magic_name__		:Optional[int]   =   image_size
								__magic_name__		:Optional[Any]   =   patch_size
								__magic_name__		:int   =   num_channels
								__magic_name__		:Union[str, Any]   =   is_training
								__magic_name__		:Optional[Any]   =   use_labels
								__magic_name__		:str   =   hidden_size
								__magic_name__		:Optional[int]   =   num_hidden_layers
								__magic_name__		:Optional[Any]   =   num_attention_heads
								__magic_name__		:Optional[Any]   =   intermediate_size
								__magic_name__		:str   =   hidden_act
								__magic_name__		:Tuple   =   hidden_dropout_prob
								__magic_name__		:Optional[int]   =   attention_probs_dropout_prob
								__magic_name__		:int   =   type_sequence_label_size
								__magic_name__		:Optional[int]   =   initializer_range
								__magic_name__		:Any   =   scope

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







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Union[str, Any]   =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

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

								__magic_name__		:str   =   self.get_config()

								return config, pixel_values, labels







				def       A						(    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       A						(    self							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase  ):
								"""simple docstring"""
								__magic_name__		:Optional[Any]   =   ViTMSNModel(config=__lowerCAmelCase  )
								model.to(__lowerCAmelCase  )
								model.eval()
								__magic_name__		:Union[str, Any]   =   model(__lowerCAmelCase  )
								self.parent.assertEqual(result.last_hidden_state.shape							,       (self.batch_size, self.seq_length, self.hidden_size)  )







				def       A						(    self							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase  ):
								"""simple docstring"""
								__magic_name__		:Tuple   =   self.type_sequence_label_size
								__magic_name__		:List[str]   =   ViTMSNForImageClassification(__lowerCAmelCase  )
								model.to(__lowerCAmelCase  )
								model.eval()
								__magic_name__		:Union[str, Any]   =   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
								__magic_name__		:Optional[int]   =   1
								__magic_name__		:Any   =   ViTMSNForImageClassification(__lowerCAmelCase  )
								model.to(__lowerCAmelCase  )
								model.eval()

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







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:int   =   self.prepare_config_and_inputs()
								__magic_name__  ,					__magic_name__  ,					__magic_name__		:Union[str, Any]   =   config_and_inputs
								__magic_name__		:int   =   {'''pixel_values''': pixel_values}
								return config, inputs_dict







@require_torch
class     lowerCamelCase_					(   lowerCamelCase      ,					lowerCamelCase      ,					unittest.TestCase			):
				a__       							=	(ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else ()
				a__       							=	(
				    {'''feature-extraction''': ViTMSNModel, '''image-classification''': ViTMSNForImageClassification}
				    if is_torch_available()
				    else {}
				)

				a__       							=	False
				a__       							=	False
				a__       							=	False
				a__       							=	False







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Tuple   =   ViTMSNModelTester(self  )
								__magic_name__		:Any   =   ConfigTester(self							,       config_class=__lowerCAmelCase							,       has_text_modality=__lowerCAmelCase							,       hidden_size=3_7  )







				def       A						(    self  ):
								"""simple docstring"""
								self.config_tester.run_common_tests()







				@unittest.skip(reason='''ViTMSN does not use inputs_embeds'''  )
				def       A						(    self  ):
								"""simple docstring"""
								pass







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__  ,					__magic_name__		:Union[str, Any]   =   self.model_tester.prepare_config_and_inputs_for_common()

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







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__  ,					__magic_name__		:Union[str, Any]   =   self.model_tester.prepare_config_and_inputs_for_common()

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

												__magic_name__		:str   =   ['''pixel_values''']
												self.assertListEqual(arg_names[:1]							,       __lowerCAmelCase  )







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:List[str]   =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__lowerCAmelCase  )







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Any   =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase  )







				@slow
				def       A						(    self  ):
								"""simple docstring"""
								for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												__magic_name__		:str   =   ViTMSNModel.from_pretrained(__lowerCAmelCase  )
												self.assertIsNotNone(__lowerCAmelCase  )







def   __lowercase       (			):







				"""simple docstring"""






				__magic_name__		:Optional[int]   =   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
				return image

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







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







				@slow
				def       A						(    self  ):
								"""simple docstring"""
								torch.manual_seed(2  )
								__magic_name__		:int   =   ViTMSNForImageClassification.from_pretrained('''facebook/vit-msn-small'''  ).to(__lowerCAmelCase  )

								__magic_name__		:Any   =   self.default_image_processor
								__magic_name__		:Dict   =   prepare_img()
								__magic_name__		:Any   =   image_processor(images=__lowerCAmelCase							,       return_tensors='''pt'''  ).to(__lowerCAmelCase  )

								# forward pass
								with torch.no_grad():
												__magic_name__		:Tuple   =   model(**__lowerCAmelCase  )

								# verify the logits
								__magic_name__		:Union[str, Any]   =   torch.Size((1, 1_0_0_0)  )
								self.assertEqual(outputs.logits.shape							,       __lowerCAmelCase  )

								__magic_name__		:Tuple   =   torch.tensor([-0.0803, -0.4454, -0.2375]  ).to(__lowerCAmelCase  )

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














import unittest

import numpy as np

from transformers.testing_utils import require_flax, require_tf, require_torch
from transformers.utils import (
    expand_dims,
    flatten_dict,
    is_flax_available,
    is_tf_available,
    is_torch_available,
    reshape,
    squeeze,
    transpose,
)


if is_flax_available():
					import jax.numpy as jnp

if is_tf_available():
					import tensorflow as tf

if is_torch_available():
					import torch
class     lowerCamelCase_					(   unittest.TestCase			):







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:List[Any]   =   {
								    '''task_specific_params''': {
								        '''summarization''': {'''length_penalty''': 1.0, '''max_length''': 1_2_8, '''min_length''': 1_2, '''num_beams''': 4},
								        '''summarization_cnn''': {'''length_penalty''': 2.0, '''max_length''': 1_4_2, '''min_length''': 5_6, '''num_beams''': 4},
								        '''summarization_xsum''': {'''length_penalty''': 1.0, '''max_length''': 6_2, '''min_length''': 1_1, '''num_beams''': 6},
								    }
								}
								__magic_name__		:List[str]   =   {
								    '''task_specific_params.summarization.length_penalty''': 1.0,
								    '''task_specific_params.summarization.max_length''': 1_2_8,
								    '''task_specific_params.summarization.min_length''': 1_2,
								    '''task_specific_params.summarization.num_beams''': 4,
								    '''task_specific_params.summarization_cnn.length_penalty''': 2.0,
								    '''task_specific_params.summarization_cnn.max_length''': 1_4_2,
								    '''task_specific_params.summarization_cnn.min_length''': 5_6,
								    '''task_specific_params.summarization_cnn.num_beams''': 4,
								    '''task_specific_params.summarization_xsum.length_penalty''': 1.0,
								    '''task_specific_params.summarization_xsum.max_length''': 6_2,
								    '''task_specific_params.summarization_xsum.min_length''': 1_1,
								    '''task_specific_params.summarization_xsum.num_beams''': 6,
								}

								self.assertEqual(flatten_dict(__lowerCAmelCase  )							,       __lowerCAmelCase  )







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Optional[Any]   =   np.random.randn(3							,       4  )
								self.assertTrue(np.allclose(transpose(__lowerCAmelCase  )							,       x.transpose()  )  )

								__magic_name__		:List[Any]   =   np.random.randn(3							,       4							,       5  )
								self.assertTrue(np.allclose(transpose(__lowerCAmelCase							,       axes=(1, 2, 0)  )							,       x.transpose((1, 2, 0)  )  )  )







				@require_torch
				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Optional[Any]   =   np.random.randn(3							,       4  )
								__magic_name__		:Tuple   =   torch.tensor(__lowerCAmelCase  )
								self.assertTrue(np.allclose(transpose(__lowerCAmelCase  )							,       transpose(__lowerCAmelCase  ).numpy()  )  )

								__magic_name__		:int   =   np.random.randn(3							,       4							,       5  )
								__magic_name__		:Union[str, Any]   =   torch.tensor(__lowerCAmelCase  )
								self.assertTrue(np.allclose(transpose(__lowerCAmelCase							,       axes=(1, 2, 0)  )							,       transpose(__lowerCAmelCase							,       axes=(1, 2, 0)  ).numpy()  )  )







				@require_tf
				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:int   =   np.random.randn(3							,       4  )
								__magic_name__		:Optional[Any]   =   tf.constant(__lowerCAmelCase  )
								self.assertTrue(np.allclose(transpose(__lowerCAmelCase  )							,       transpose(__lowerCAmelCase  ).numpy()  )  )

								__magic_name__		:List[str]   =   np.random.randn(3							,       4							,       5  )
								__magic_name__		:Optional[Any]   =   tf.constant(__lowerCAmelCase  )
								self.assertTrue(np.allclose(transpose(__lowerCAmelCase							,       axes=(1, 2, 0)  )							,       transpose(__lowerCAmelCase							,       axes=(1, 2, 0)  ).numpy()  )  )







				@require_flax
				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:int   =   np.random.randn(3							,       4  )
								__magic_name__		:Dict   =   jnp.array(__lowerCAmelCase  )
								self.assertTrue(np.allclose(transpose(__lowerCAmelCase  )							,       np.asarray(transpose(__lowerCAmelCase  )  )  )  )

								__magic_name__		:Dict   =   np.random.randn(3							,       4							,       5  )
								__magic_name__		:Dict   =   jnp.array(__lowerCAmelCase  )
								self.assertTrue(np.allclose(transpose(__lowerCAmelCase							,       axes=(1, 2, 0)  )							,       np.asarray(transpose(__lowerCAmelCase							,       axes=(1, 2, 0)  )  )  )  )







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Any   =   np.random.randn(3							,       4  )
								self.assertTrue(np.allclose(reshape(__lowerCAmelCase							,       (4, 3)  )							,       np.reshape(__lowerCAmelCase							,       (4, 3)  )  )  )

								__magic_name__		:Union[str, Any]   =   np.random.randn(3							,       4							,       5  )
								self.assertTrue(np.allclose(reshape(__lowerCAmelCase							,       (1_2, 5)  )							,       np.reshape(__lowerCAmelCase							,       (1_2, 5)  )  )  )







				@require_torch
				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Dict   =   np.random.randn(3							,       4  )
								__magic_name__		:Tuple   =   torch.tensor(__lowerCAmelCase  )
								self.assertTrue(np.allclose(reshape(__lowerCAmelCase							,       (4, 3)  )							,       reshape(__lowerCAmelCase							,       (4, 3)  ).numpy()  )  )

								__magic_name__		:Union[str, Any]   =   np.random.randn(3							,       4							,       5  )
								__magic_name__		:List[str]   =   torch.tensor(__lowerCAmelCase  )
								self.assertTrue(np.allclose(reshape(__lowerCAmelCase							,       (1_2, 5)  )							,       reshape(__lowerCAmelCase							,       (1_2, 5)  ).numpy()  )  )







				@require_tf
				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Dict   =   np.random.randn(3							,       4  )
								__magic_name__		:Union[str, Any]   =   tf.constant(__lowerCAmelCase  )
								self.assertTrue(np.allclose(reshape(__lowerCAmelCase							,       (4, 3)  )							,       reshape(__lowerCAmelCase							,       (4, 3)  ).numpy()  )  )

								__magic_name__		:List[Any]   =   np.random.randn(3							,       4							,       5  )
								__magic_name__		:Optional[int]   =   tf.constant(__lowerCAmelCase  )
								self.assertTrue(np.allclose(reshape(__lowerCAmelCase							,       (1_2, 5)  )							,       reshape(__lowerCAmelCase							,       (1_2, 5)  ).numpy()  )  )







				@require_flax
				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:List[str]   =   np.random.randn(3							,       4  )
								__magic_name__		:Any   =   jnp.array(__lowerCAmelCase  )
								self.assertTrue(np.allclose(reshape(__lowerCAmelCase							,       (4, 3)  )							,       np.asarray(reshape(__lowerCAmelCase							,       (4, 3)  )  )  )  )

								__magic_name__		:List[Any]   =   np.random.randn(3							,       4							,       5  )
								__magic_name__		:List[str]   =   jnp.array(__lowerCAmelCase  )
								self.assertTrue(np.allclose(reshape(__lowerCAmelCase							,       (1_2, 5)  )							,       np.asarray(reshape(__lowerCAmelCase							,       (1_2, 5)  )  )  )  )







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:List[Any]   =   np.random.randn(1							,       3							,       4  )
								self.assertTrue(np.allclose(squeeze(__lowerCAmelCase  )							,       np.squeeze(__lowerCAmelCase  )  )  )

								__magic_name__		:Optional[Any]   =   np.random.randn(1							,       4							,       1							,       5  )
								self.assertTrue(np.allclose(squeeze(__lowerCAmelCase							,       axis=2  )							,       np.squeeze(__lowerCAmelCase							,       axis=2  )  )  )







				@require_torch
				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Dict   =   np.random.randn(1							,       3							,       4  )
								__magic_name__		:List[Any]   =   torch.tensor(__lowerCAmelCase  )
								self.assertTrue(np.allclose(squeeze(__lowerCAmelCase  )							,       squeeze(__lowerCAmelCase  ).numpy()  )  )

								__magic_name__		:List[str]   =   np.random.randn(1							,       4							,       1							,       5  )
								__magic_name__		:str   =   torch.tensor(__lowerCAmelCase  )
								self.assertTrue(np.allclose(squeeze(__lowerCAmelCase							,       axis=2  )							,       squeeze(__lowerCAmelCase							,       axis=2  ).numpy()  )  )







				@require_tf
				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:int   =   np.random.randn(1							,       3							,       4  )
								__magic_name__		:Tuple   =   tf.constant(__lowerCAmelCase  )
								self.assertTrue(np.allclose(squeeze(__lowerCAmelCase  )							,       squeeze(__lowerCAmelCase  ).numpy()  )  )

								__magic_name__		:Tuple   =   np.random.randn(1							,       4							,       1							,       5  )
								__magic_name__		:Optional[int]   =   tf.constant(__lowerCAmelCase  )
								self.assertTrue(np.allclose(squeeze(__lowerCAmelCase							,       axis=2  )							,       squeeze(__lowerCAmelCase							,       axis=2  ).numpy()  )  )







				@require_flax
				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Tuple   =   np.random.randn(1							,       3							,       4  )
								__magic_name__		:Optional[Any]   =   jnp.array(__lowerCAmelCase  )
								self.assertTrue(np.allclose(squeeze(__lowerCAmelCase  )							,       np.asarray(squeeze(__lowerCAmelCase  )  )  )  )

								__magic_name__		:List[Any]   =   np.random.randn(1							,       4							,       1							,       5  )
								__magic_name__		:Optional[Any]   =   jnp.array(__lowerCAmelCase  )
								self.assertTrue(np.allclose(squeeze(__lowerCAmelCase							,       axis=2  )							,       np.asarray(squeeze(__lowerCAmelCase							,       axis=2  )  )  )  )







				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Any   =   np.random.randn(3							,       4  )
								self.assertTrue(np.allclose(expand_dims(__lowerCAmelCase							,       axis=1  )							,       np.expand_dims(__lowerCAmelCase							,       axis=1  )  )  )







				@require_torch
				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:List[Any]   =   np.random.randn(3							,       4  )
								__magic_name__		:Any   =   torch.tensor(__lowerCAmelCase  )
								self.assertTrue(np.allclose(expand_dims(__lowerCAmelCase							,       axis=1  )							,       expand_dims(__lowerCAmelCase							,       axis=1  ).numpy()  )  )







				@require_tf
				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:Union[str, Any]   =   np.random.randn(3							,       4  )
								__magic_name__		:Union[str, Any]   =   tf.constant(__lowerCAmelCase  )
								self.assertTrue(np.allclose(expand_dims(__lowerCAmelCase							,       axis=1  )							,       expand_dims(__lowerCAmelCase							,       axis=1  ).numpy()  )  )







				@require_flax
				def       A						(    self  ):
								"""simple docstring"""
								__magic_name__		:List[str]   =   np.random.randn(3							,       4  )
								__magic_name__		:Tuple   =   jnp.array(__lowerCAmelCase  )
								self.assertTrue(np.allclose(expand_dims(__lowerCAmelCase							,       axis=1  )							,       np.asarray(expand_dims(__lowerCAmelCase							,       axis=1  )  )  )  )






