


"""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 					__magic_name__		:






							def __init__(   self  :    Any  ,     snake_case__  :    Optional[Any]  ,     snake_case__  :    Union[str, Any]=1_3  ,     snake_case__  :    Tuple=3_0  ,     snake_case__  :    str=2  ,     snake_case__  :    Union[str, Any]=3  ,     snake_case__  :    Union[str, Any]=True  ,     snake_case__  :    Union[str, Any]=True  ,     snake_case__  :    Optional[Any]=3_2  ,     snake_case__  :    Any=5  ,     snake_case__  :    int=4  ,     snake_case__  :    Dict=3_7  ,     snake_case__  :    Dict="gelu"  ,     snake_case__  :    Union[str, Any]=0.1  ,     snake_case__  :    Union[str, Any]=0.1  ,     snake_case__  :    List[Any]=1_0  ,     snake_case__  :    int=0.02  ,     snake_case__  :    List[str]=None  ,     ):

														'''simple docstring'''






														lowercase		:List[str]       							=				parent
														lowercase		:Optional[Any]       							=				batch_size
														lowercase		:int       							=				image_size
														lowercase		:Union[str, Any]       							=				patch_size
														lowercase		:Dict       							=				num_channels
														lowercase		:int       							=				is_training
														lowercase		:List[Any]       							=				use_labels
														lowercase		:int       							=				hidden_size
														lowercase		:Tuple       							=				num_hidden_layers
														lowercase		:Tuple       							=				num_attention_heads
														lowercase		:Optional[Any]       							=				intermediate_size
														lowercase		:List[Any]       							=				hidden_act
														lowercase		:str       							=				hidden_dropout_prob
														lowercase		:Any       							=				attention_probs_dropout_prob
														lowercase		:Dict       							=				type_sequence_label_size
														lowercase		:Dict       							=				initializer_range
														lowercase		:int       							=				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		:int       							=				num_patches + 1






							def  __snake_case						(   self  :    Tuple				):

														'''simple docstring'''






														lowercase		:List[Any]       							=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

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

														lowercase		:Union[str, Any]       							=				self.get_config()

														return config, pixel_values, labels






							def  __snake_case						(   self  :    Dict				):

														'''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  __snake_case						(   self  :    Optional[Any]  ,     snake_case__  :    Tuple  ,     snake_case__  :    Any  ,     snake_case__  :    str				):

														'''simple docstring'''






														lowercase		:int       							=				ViTMSNModel(config=snake_case__				)
														model.to(snake_case__				)
														model.eval()
														lowercase		:List[Any]       							=				model(snake_case__				)
														self.parent.assertEqual(result.last_hidden_state.shape  ,     (self.batch_size, self.seq_length, self.hidden_size)				)






							def  __snake_case						(   self  :    int  ,     snake_case__  :    Dict  ,     snake_case__  :    Dict  ,     snake_case__  :    Tuple				):

														'''simple docstring'''






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

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







							def  __snake_case						(   self  :    List[Any]				):

														'''simple docstring'''






														lowercase		:int       							=				self.prepare_config_and_inputs()
														lowercase		:Union[str, Any]       							=				config_and_inputs
														lowercase		:int       							=				{'''pixel_values''': pixel_values}
														return config, inputs_dict







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

							__A			:      List[str]        =   False
							__A			:      Dict        =   False
							__A			:      Any        =   False
							__A			:      Optional[Any]        =   False






							def  __snake_case						(   self  :    Dict				):

														'''simple docstring'''






														lowercase		:Any       							=				ViTMSNModelTester(self				)
														lowercase		:Tuple       							=				ConfigTester(self  ,     config_class=snake_case__  ,     has_text_modality=snake_case__  ,     hidden_size=3_7				)






							def  __snake_case						(   self  :    Union[str, Any]				):

														'''simple docstring'''






														self.config_tester.run_common_tests()






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

														'''simple docstring'''






														pass






							def  __snake_case						(   self  :    List[str]				):

														'''simple docstring'''






														lowercase		:Dict       							=				self.model_tester.prepare_config_and_inputs_for_common()

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






							def  __snake_case						(   self  :    Optional[int]				):

														'''simple docstring'''






														lowercase		:int       							=				self.model_tester.prepare_config_and_inputs_for_common()

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

																					lowercase		:str       							=				['''pixel_values''']
																					self.assertListEqual(arg_names[:1]  ,     snake_case__				)






							def  __snake_case						(   self  :    int				):

														'''simple docstring'''






														lowercase		:Union[str, Any]       							=				self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*snake_case__				)






							def  __snake_case						(   self  :    str				):

														'''simple docstring'''






														lowercase		:Optional[Any]       							=				self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*snake_case__				)







							@slow
							def  __snake_case						(   self  :    Optional[Any]				):

														'''simple docstring'''






														for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowercase		:List[str]       							=				ViTMSNModel.from_pretrained(snake_case__				)
																					self.assertIsNotNone(snake_case__				)







def       lowerCamelCase					()     ->							Optional[int]:
							lowercase		:str       							=				Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''')
							return image




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






							@cached_property
							def  __snake_case						(   self  :    List[str]				):

														'''simple docstring'''






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







							@slow
							def  __snake_case						(   self  :    int				):

														'''simple docstring'''






														torch.manual_seed(2				)
														lowercase		:Optional[int]       							=				ViTMSNForImageClassification.from_pretrained('''facebook/vit-msn-small'''				).to(snake_case__				)

														lowercase		:str       							=				self.default_image_processor
														lowercase		:str       							=				prepare_img()
														lowercase		:Any       							=				image_processor(images=snake_case__  ,     return_tensors='''pt'''				).to(snake_case__				)

														# forward pass
														with torch.no_grad():
																					lowercase		:Dict       							=				model(**snake_case__				)

														# verify the logits
														lowercase		:List[Any]       							=				torch.Size((1, 1_0_0_0)				)
														self.assertEqual(outputs.logits.shape  ,     snake_case__				)

														lowercase		:Dict       							=				torch.tensor([-0.08_03, -0.44_54, -0.23_75]				).to(snake_case__				)

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





"""simple docstring"""



import unittest

import torch

from diffusers import VQModel
from diffusers.utils import floats_tensor, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()
class 					__magic_name__		(			__UpperCAmelCase			,						__UpperCAmelCase			,						unittest.TestCase     ):
       __A			:      List[str]        =   VQModel
       __A			:      Any        =   "sample"






       @property
       def  __snake_case						(   self  :    int  ,     snake_case__  :    int=(3_2, 3_2)				):

              '''simple docstring'''






              lowercase		:Optional[int]       							=				4
              lowercase		:Tuple       							=				3

              lowercase		:List[str]       							=				floats_tensor((batch_size, num_channels) + sizes				).to(snake_case__				)

              return {"sample": image}






       @property
       def  __snake_case						(   self  :    Union[str, Any]				):

              '''simple docstring'''






              return (3, 3_2, 3_2)






       @property
       def  __snake_case						(   self  :    List[str]				):

              '''simple docstring'''






              return (3, 3_2, 3_2)






       def  __snake_case						(   self  :    Dict				):

              '''simple docstring'''






              lowercase		:Optional[int]       							=				{
                  '''block_out_channels''': [3_2, 6_4],
                  '''in_channels''': 3,
                  '''out_channels''': 3,
                  '''down_block_types''': ['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''],
                  '''up_block_types''': ['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''],
                  '''latent_channels''': 3,
              }
              lowercase		:Dict       							=				self.dummy_input
              return init_dict, inputs_dict






       def  __snake_case						(   self  :    Optional[int]				):

              '''simple docstring'''






              pass






       def  __snake_case						(   self  :    Union[str, Any]				):

              '''simple docstring'''






              pass






       def  __snake_case						(   self  :    Dict				):

              '''simple docstring'''






              lowercase							,  lowercase		:Optional[int]       							=				VQModel.from_pretrained('''fusing/vqgan-dummy'''  ,     output_loading_info=snake_case__				)
              self.assertIsNotNone(snake_case__				)
              self.assertEqual(len(loading_info['''missing_keys''']				)  ,     0				)

              model.to(snake_case__				)
              lowercase		:List[str]       							=				model(**self.dummy_input				)

              assert image is not None, "Make sure output is not None"







       def  __snake_case						(   self  :    Optional[int]				):

              '''simple docstring'''






              lowercase		:Optional[int]       							=				VQModel.from_pretrained('''fusing/vqgan-dummy'''				)
              model.to(snake_case__				).eval()

              torch.manual_seed(0				)
              if torch.cuda.is_available():
                     torch.cuda.manual_seed_all(0				)

              lowercase		:List[Any]       							=				torch.randn(1  ,     model.config.in_channels  ,     model.config.sample_size  ,     model.config.sample_size				)
              lowercase		:int       							=				image.to(snake_case__				)
              with torch.no_grad():
                     lowercase		:str       							=				model(snake_case__				).sample

              lowercase		:List[Any]       							=				output[0, -1, -3:, -3:].flatten().cpu()
              # fmt: off
              lowercase		:Tuple       							=				torch.tensor([-0.01_53, -0.40_44, -0.18_80, -0.51_61, -0.24_18, -0.40_72, -0.16_12, -0.06_33, -0.01_43]				)
              # fmt: on
              self.assertTrue(torch.allclose(snake_case__  ,     snake_case__  ,     atol=1e-3				)				)

