






'''simple docstring'''



import gc
import unittest

import numpy as np
import torch
import torch.nn.functional as F
from transformers import (
    ClapTextConfig,
    ClapTextModelWithProjection,
    RobertaTokenizer,
    SpeechTaHifiGan,
    SpeechTaHifiGanConfig,
)

from diffusers import (
    AudioLDMPipeline,
    AutoencoderKL,
    DDIMScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.utils import is_xformers_available, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from ..pipeline_params import TEXT_TO_AUDIO_BATCH_PARAMS, TEXT_TO_AUDIO_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()
class UpperCAmelCase_       (     __A					,							unittest.TestCase					):




      """simple docstring"""




      UpperCamelCase_             =      AudioLDMPipeline
      UpperCamelCase_             =      TEXT_TO_AUDIO_PARAMS
      UpperCamelCase_             =      TEXT_TO_AUDIO_BATCH_PARAMS
      UpperCamelCase_             =      frozenset(
          [
              '''num_inference_steps''',
              '''num_waveforms_per_prompt''',
              '''generator''',
              '''latents''',
              '''output_type''',
              '''return_dict''',
              '''callback''',
              '''callback_steps''',
          ]					)






      def 					A__   (			self      :		Optional[int]		)	->	Union[str, Any]:


       '''simple docstring'''

       torch.manual_seed(0		)
       lowercase       : str						=UNetaDConditionModel(
           block_out_channels=(32, 64)  ,   layers_per_block=2  ,   sample_size=32  ,   in_channels=4  ,   out_channels=4  ,   down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')  ,   up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')  ,   cross_attention_dim=(32, 64)  ,   class_embed_type='''simple_projection'''  ,   projection_class_embeddings_input_dim=32  ,   class_embeddings_concat=UpperCAmelCase  ,   )
       lowercase       : Dict						=DDIMScheduler(
           beta_start=0.0_0_0_8_5  ,   beta_end=0.0_1_2  ,   beta_schedule='''scaled_linear'''  ,   clip_sample=UpperCAmelCase  ,   set_alpha_to_one=UpperCAmelCase  ,   )
       torch.manual_seed(0		)
       lowercase       : str						=AutoencoderKL(
           block_out_channels=[32, 64]  ,   in_channels=1  ,   out_channels=1  ,   down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']  ,   up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']  ,   latent_channels=4  ,   )
       torch.manual_seed(0		)
       lowercase       : Optional[int]						=ClapTextConfig(
           bos_token_id=0  ,   eos_token_id=2  ,   hidden_size=32  ,   intermediate_size=37  ,   layer_norm_eps=1e-05  ,   num_attention_heads=4  ,   num_hidden_layers=5  ,   pad_token_id=1  ,   vocab_size=1000  ,   projection_dim=32  ,   )
       lowercase       : Dict						=ClapTextModelWithProjection(UpperCAmelCase		)
       lowercase       : List[str]						=RobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-random-roberta'''  ,   model_max_length=77		)

       lowercase       : Any						=SpeechTaHifiGanConfig(
           model_in_dim=8  ,   sampling_rate=1_6000  ,   upsample_initial_channel=16  ,   upsample_rates=[2, 2]  ,   upsample_kernel_sizes=[4, 4]  ,   resblock_kernel_sizes=[3, 7]  ,   resblock_dilation_sizes=[[1, 3, 5], [1, 3, 5]]  ,   normalize_before=UpperCAmelCase  ,   )

       lowercase       : Dict						=SpeechTaHifiGan(UpperCAmelCase		)

       lowercase       : int						={
           '''unet''': unet,
           '''scheduler''': scheduler,
           '''vae''': vae,
           '''text_encoder''': text_encoder,
           '''tokenizer''': tokenizer,
           '''vocoder''': vocoder,
       }
       return components






      def 					A__   (			self      :		List[Any]  ,   UpperCAmelCase      :		Any  ,   UpperCAmelCase      :		Optional[Any]=0		)	->	Union[str, Any]:


       '''simple docstring'''

       if str(UpperCAmelCase		).startswith('''mps'''		):
        lowercase       : int						=torch.manual_seed(UpperCAmelCase		)
       else:
        lowercase       : Tuple						=torch.Generator(device=UpperCAmelCase		).manual_seed(UpperCAmelCase		)
       lowercase       : Optional[Any]						={
           '''prompt''': '''A hammer hitting a wooden surface''',
           '''generator''': generator,
           '''num_inference_steps''': 2,
           '''guidance_scale''': 6.0,
       }
       return inputs






      def 					A__   (			self      :		str		)	->	Dict:


       '''simple docstring'''

       lowercase       : str						='''cpu'''  # ensure determinism for the device-dependent torch.Generator

       lowercase       : Any						=self.get_dummy_components()
       lowercase       : str						=AudioLDMPipeline(**UpperCAmelCase		)
       lowercase       : Tuple						=audioldm_pipe.to(UpperCAmelCase		)
       audioldm_pipe.set_progress_bar_config(disable=UpperCAmelCase		)

       lowercase       : List[str]						=self.get_dummy_inputs(UpperCAmelCase		)
       lowercase       : int						=audioldm_pipe(**UpperCAmelCase		)
       lowercase       : Dict						=output.audios[0]

       assert audio.ndim == 1
       assert len(UpperCAmelCase		) == 256

       lowercase       : int						=audio[:10]
       lowercase       : Union[str, Any]						=np.array(
           [-0.0_0_5_0, 0.0_0_5_0, -0.0_0_6_0, 0.0_0_3_3, -0.0_0_2_6, 0.0_0_3_3, -0.0_0_2_7, 0.0_0_3_3, -0.0_0_2_8, 0.0_0_3_3]		)

       assert np.abs(audio_slice - expected_slice		).max() < 1e-2






      def 					A__   (			self      :		Tuple		)	->	Optional[int]:


       '''simple docstring'''

       lowercase       : Dict						=self.get_dummy_components()
       lowercase       : List[str]						=AudioLDMPipeline(**UpperCAmelCase		)
       lowercase       : Optional[int]						=audioldm_pipe.to(UpperCAmelCase		)
       lowercase       : Any						=audioldm_pipe.to(UpperCAmelCase		)
       audioldm_pipe.set_progress_bar_config(disable=UpperCAmelCase		)

       lowercase       : Union[str, Any]						=self.get_dummy_inputs(UpperCAmelCase		)
       lowercase       : Optional[Any]						=3 * [inputs['''prompt''']]

       # forward
       lowercase       : Optional[int]						=audioldm_pipe(**UpperCAmelCase		)
       lowercase       : Dict						=output.audios[0]

       lowercase       : str						=self.get_dummy_inputs(UpperCAmelCase		)
       lowercase       : int						=3 * [inputs.pop('''prompt'''		)]

       lowercase       : List[Any]						=audioldm_pipe.tokenizer(
           UpperCAmelCase  ,   padding='''max_length'''  ,   max_length=audioldm_pipe.tokenizer.model_max_length  ,   truncation=UpperCAmelCase  ,   return_tensors='''pt'''  ,   )
       lowercase       : int						=text_inputs['''input_ids'''].to(UpperCAmelCase		)

       lowercase       : Optional[int]						=audioldm_pipe.text_encoder(
           UpperCAmelCase  ,   )
       lowercase       : Any						=prompt_embeds.text_embeds
       # additional L_2 normalization over each hidden-state
       lowercase       : int						=F.normalize(UpperCAmelCase  ,   dim=-1		)

       lowercase       : Union[str, Any]						=prompt_embeds

       # forward
       lowercase       : int						=audioldm_pipe(**UpperCAmelCase		)
       lowercase       : Optional[Any]						=output.audios[0]

       assert np.abs(audio_a - audio_a		).max() < 1e-2






      def 					A__   (			self      :		Optional[Any]		)	->	Dict:


       '''simple docstring'''

       lowercase       : List[Any]						=self.get_dummy_components()
       lowercase       : Any						=AudioLDMPipeline(**UpperCAmelCase		)
       lowercase       : str						=audioldm_pipe.to(UpperCAmelCase		)
       lowercase       : List[str]						=audioldm_pipe.to(UpperCAmelCase		)
       audioldm_pipe.set_progress_bar_config(disable=UpperCAmelCase		)

       lowercase       : int						=self.get_dummy_inputs(UpperCAmelCase		)
       lowercase       : List[str]						=3 * ['''this is a negative prompt''']
       lowercase       : List[str]						=negative_prompt
       lowercase       : List[Any]						=3 * [inputs['''prompt''']]

       # forward
       lowercase       : List[Any]						=audioldm_pipe(**UpperCAmelCase		)
       lowercase       : Optional[int]						=output.audios[0]

       lowercase       : str						=self.get_dummy_inputs(UpperCAmelCase		)
       lowercase       : List[Any]						=3 * [inputs.pop('''prompt'''		)]

       lowercase       : str						=[]
       for p in [prompt, negative_prompt]:
        lowercase       : Tuple						=audioldm_pipe.tokenizer(
            UpperCAmelCase  ,   padding='''max_length'''  ,   max_length=audioldm_pipe.tokenizer.model_max_length  ,   truncation=UpperCAmelCase  ,   return_tensors='''pt'''  ,   )
        lowercase       : Optional[int]						=text_inputs['''input_ids'''].to(UpperCAmelCase		)

        lowercase       : List[str]						=audioldm_pipe.text_encoder(
            UpperCAmelCase  ,   )
        lowercase       : Tuple						=text_embeds.text_embeds
        # additional L_2 normalization over each hidden-state
        lowercase       : Dict						=F.normalize(UpperCAmelCase  ,   dim=-1		)

        embeds.append(UpperCAmelCase		)

       lowercase	,		lowercase       : Optional[Any]						=embeds

       # forward
       lowercase       : List[Any]						=audioldm_pipe(**UpperCAmelCase		)
       lowercase       : Optional[int]						=output.audios[0]

       assert np.abs(audio_a - audio_a		).max() < 1e-2






      def 					A__   (			self      :		Union[str, Any]		)	->	List[Any]:


       '''simple docstring'''

       lowercase       : Union[str, Any]						='''cpu'''  # ensure determinism for the device-dependent torch.Generator
       lowercase       : Dict						=self.get_dummy_components()
       lowercase       : Optional[int]						=PNDMScheduler(skip_prk_steps=UpperCAmelCase		)
       lowercase       : Tuple						=AudioLDMPipeline(**UpperCAmelCase		)
       lowercase       : List[str]						=audioldm_pipe.to(UpperCAmelCase		)
       audioldm_pipe.set_progress_bar_config(disable=UpperCAmelCase		)

       lowercase       : Any						=self.get_dummy_inputs(UpperCAmelCase		)
       lowercase       : Union[str, Any]						='''egg cracking'''
       lowercase       : Tuple						=audioldm_pipe(**UpperCAmelCase  ,   negative_prompt=UpperCAmelCase		)
       lowercase       : Tuple						=output.audios[0]

       assert audio.ndim == 1
       assert len(UpperCAmelCase		) == 256

       lowercase       : Optional[int]						=audio[:10]
       lowercase       : Union[str, Any]						=np.array(
           [-0.0_0_5_1, 0.0_0_5_0, -0.0_0_6_0, 0.0_0_3_4, -0.0_0_2_6, 0.0_0_3_3, -0.0_0_2_7, 0.0_0_3_3, -0.0_0_2_8, 0.0_0_3_2]		)

       assert np.abs(audio_slice - expected_slice		).max() < 1e-2






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


       '''simple docstring'''

       lowercase       : List[str]						='''cpu'''  # ensure determinism for the device-dependent torch.Generator
       lowercase       : Tuple						=self.get_dummy_components()
       lowercase       : Optional[int]						=PNDMScheduler(skip_prk_steps=UpperCAmelCase		)
       lowercase       : Optional[int]						=AudioLDMPipeline(**UpperCAmelCase		)
       lowercase       : List[Any]						=audioldm_pipe.to(UpperCAmelCase		)
       audioldm_pipe.set_progress_bar_config(disable=UpperCAmelCase		)

       lowercase       : Dict						='''A hammer hitting a wooden surface'''

       # test num_waveforms_per_prompt=1 (default)
       lowercase       : Optional[int]						=audioldm_pipe(UpperCAmelCase  ,   num_inference_steps=2		).audios

       assert audios.shape == (1, 256)

       # test num_waveforms_per_prompt=1 (default) for batch of prompts
       lowercase       : List[Any]						=2
       lowercase       : Union[str, Any]						=audioldm_pipe([prompt] * batch_size  ,   num_inference_steps=2		).audios

       assert audios.shape == (batch_size, 256)

       # test num_waveforms_per_prompt for single prompt
       lowercase       : Dict						=2
       lowercase       : Optional[int]						=audioldm_pipe(UpperCAmelCase  ,   num_inference_steps=2  ,   num_waveforms_per_prompt=UpperCAmelCase		).audios

       assert audios.shape == (num_waveforms_per_prompt, 256)

       # test num_waveforms_per_prompt for batch of prompts
       lowercase       : Optional[int]						=2
       lowercase       : Optional[int]						=audioldm_pipe(
           [prompt] * batch_size  ,   num_inference_steps=2  ,   num_waveforms_per_prompt=UpperCAmelCase		).audios

       assert audios.shape == (batch_size * num_waveforms_per_prompt, 256)






      def 					A__   (			self      :		Tuple		)	->	str:


       '''simple docstring'''

       lowercase       : str						='''cpu'''  # ensure determinism for the device-dependent torch.Generator
       lowercase       : Optional[Any]						=self.get_dummy_components()
       lowercase       : Dict						=AudioLDMPipeline(**UpperCAmelCase		)
       lowercase       : List[str]						=audioldm_pipe.to(UpperCAmelCase		)
       audioldm_pipe.set_progress_bar_config(disable=UpperCAmelCase		)
       lowercase       : Dict						=audioldm_pipe.vocoder.config.sampling_rate

       lowercase       : Optional[Any]						=self.get_dummy_inputs(UpperCAmelCase		)
       lowercase       : Optional[int]						=audioldm_pipe(audio_length_in_s=0.0_1_6  ,   **UpperCAmelCase		)
       lowercase       : Union[str, Any]						=output.audios[0]

       assert audio.ndim == 1
       assert len(UpperCAmelCase		) / vocoder_sampling_rate == 0.0_1_6

       lowercase       : int						=audioldm_pipe(audio_length_in_s=0.0_3_2  ,   **UpperCAmelCase		)
       lowercase       : Any						=output.audios[0]

       assert audio.ndim == 1
       assert len(UpperCAmelCase		) / vocoder_sampling_rate == 0.0_3_2






      def 					A__   (			self      :		str		)	->	str:


       '''simple docstring'''

       lowercase       : Any						=self.get_dummy_components()
       lowercase       : Union[str, Any]						=AudioLDMPipeline(**UpperCAmelCase		)
       lowercase       : List[str]						=audioldm_pipe.to(UpperCAmelCase		)
       audioldm_pipe.set_progress_bar_config(disable=UpperCAmelCase		)

       lowercase       : Optional[Any]						=['''hey''']

       lowercase       : Any						=audioldm_pipe(UpperCAmelCase  ,   num_inference_steps=1		)
       lowercase       : int						=output.audios.shape
       assert audio_shape == (1, 256)

       lowercase       : int						=audioldm_pipe.vocoder.config
       config.model_in_dim *= 2
       lowercase       : str						=SpeechTaHifiGan(UpperCAmelCase		).to(UpperCAmelCase		)
       lowercase       : Any						=audioldm_pipe(UpperCAmelCase  ,   num_inference_steps=1		)
       lowercase       : int						=output.audios.shape
       # waveform shape is unchanged, we just have 2x the number of mel channels in the spectrogram
       assert audio_shape == (1, 256)






      def 					A__   (			self      :		List[Any]		)	->	str:


       '''simple docstring'''

       self._test_attention_slicing_forward_pass(test_mean_pixel_difference=UpperCAmelCase		)






      def 					A__   (			self      :		Union[str, Any]		)	->	Dict:


       '''simple docstring'''

       self._test_inference_batch_single_identical(test_mean_pixel_difference=UpperCAmelCase		)






      @unittest.skipIf(
          torch_device != '''cuda''' or not is_xformers_available()  ,   reason='''XFormers attention is only available with CUDA and `xformers` installed'''  ,   )
      def 					A__   (			self      :		List[str]		)	->	List[Any]:


       '''simple docstring'''

       self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=UpperCAmelCase		)





@slow
class UpperCAmelCase_       (     unittest.TestCase					):




      """simple docstring"""




      def 					A__   (			self      :		Any		)	->	List[Any]:


       '''simple docstring'''

       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()






      def 					A__   (			self      :		List[str]  ,   UpperCAmelCase      :		List[Any]  ,   UpperCAmelCase      :		List[Any]="cpu"  ,   UpperCAmelCase      :		Union[str, Any]=torch.floataa  ,   UpperCAmelCase      :		List[str]=0		)	->	str:


       '''simple docstring'''

       lowercase       : str						=torch.Generator(device=UpperCAmelCase		).manual_seed(UpperCAmelCase		)
       lowercase       : List[str]						=np.random.RandomState(UpperCAmelCase		).standard_normal((1, 8, 128, 16)		)
       lowercase       : Tuple						=torch.from_numpy(UpperCAmelCase		).to(device=UpperCAmelCase  ,   dtype=UpperCAmelCase		)
       lowercase       : Any						={
           '''prompt''': '''A hammer hitting a wooden surface''',
           '''latents''': latents,
           '''generator''': generator,
           '''num_inference_steps''': 3,
           '''guidance_scale''': 2.5,
       }
       return inputs






      def 					A__   (			self      :		Union[str, Any]		)	->	Optional[Any]:


       '''simple docstring'''

       lowercase       : Any						=AudioLDMPipeline.from_pretrained('''cvssp/audioldm'''		)
       lowercase       : int						=audioldm_pipe.to(UpperCAmelCase		)
       audioldm_pipe.set_progress_bar_config(disable=UpperCAmelCase		)

       lowercase       : Optional[int]						=self.get_inputs(UpperCAmelCase		)
       lowercase       : Tuple						=25
       lowercase       : Optional[Any]						=audioldm_pipe(**UpperCAmelCase		).audios[0]

       assert audio.ndim == 1
       assert len(UpperCAmelCase		) == 8_1920

       lowercase       : List[Any]						=audio[7_7230:7_7240]
       lowercase       : str						=np.array(
           [-0.4_8_8_4, -0.4_6_0_7, 0.0_0_2_3, 0.5_0_0_7, 0.5_8_9_6, 0.5_1_5_1, 0.3_8_1_3, -0.0_2_0_8, -0.3_6_8_7, -0.4_3_1_5]		)
       lowercase       : List[str]						=np.abs(expected_slice - audio_slice		).max()
       assert max_diff < 1e-2






      def 					A__   (			self      :		Optional[Any]		)	->	Dict:


       '''simple docstring'''

       lowercase       : List[str]						=AudioLDMPipeline.from_pretrained('''cvssp/audioldm'''		)
       lowercase       : Union[str, Any]						=LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config		)
       lowercase       : Optional[int]						=audioldm_pipe.to(UpperCAmelCase		)
       audioldm_pipe.set_progress_bar_config(disable=UpperCAmelCase		)

       lowercase       : List[Any]						=self.get_inputs(UpperCAmelCase		)
       lowercase       : int						=audioldm_pipe(**UpperCAmelCase		).audios[0]

       assert audio.ndim == 1
       assert len(UpperCAmelCase		) == 8_1920

       lowercase       : List[str]						=audio[2_7780:2_7790]
       lowercase       : Any						=np.array([-0.2_1_3_1, -0.0_8_7_3, -0.0_1_2_4, -0.0_1_8_9, 0.0_5_6_9, 0.1_3_7_3, 0.1_8_8_3, 0.2_8_8_6, 0.3_2_9_7, 0.2_2_1_2]		)
       lowercase       : Optional[Any]						=np.abs(expected_slice - audio_slice		).max()
       assert max_diff < 3e-2








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







			"""simple docstring"""



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

						# in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
						_lowerCamelCase					:					Dict	       =    (image_size // patch_size) ** 2
						_lowerCamelCase					:					Any	       =    num_patches + 1
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					Any	       =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

						_lowerCamelCase					:					Dict	       =    self.get_config()

						return config, pixel_values, labels
			def    _lowerCAmelCase		(	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    _lowerCAmelCase		(	self				,   A				,   A				,   A							):
						_lowerCamelCase					:					int	       =    ViTMSNModel(config=A							)
						model.to(A							)
						model.eval()
						_lowerCamelCase					:					List[Any]	       =    model(A							)
						self.parent.assertEqual(result.last_hidden_state.shape				,   (self.batch_size, self.seq_length, self.hidden_size)							)
			def    _lowerCAmelCase		(	self				,   A				,   A				,   A							):
						_lowerCamelCase					:					List[Any]	       =    self.type_sequence_label_size
						_lowerCamelCase					:					List[str]	       =    ViTMSNForImageClassification(A							)
						model.to(A							)
						model.eval()
						_lowerCamelCase					:					Optional[Any]	       =    model(A				,   labels=A							)
						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					:					Optional[Any]	       =    1
						_lowerCamelCase					:					List[Any]	       =    ViTMSNForImageClassification(A							)
						model.to(A							)
						model.eval()

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

			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					int	       =    self.prepare_config_and_inputs()
						_lowerCamelCase       ,							_lowerCamelCase       ,							_lowerCamelCase					:					Tuple	       =    config_and_inputs
						_lowerCamelCase					:					List[str]	       =    {'pixel_values': pixel_values}
						return config, inputs_dict







@require_torch
class 					A_(SCREAMING_SNAKE_CASE_   ,   SCREAMING_SNAKE_CASE_   ,   unittest.TestCase      ):







			"""simple docstring"""



			a_       :			Union[str, Any]												=			(ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else ()
			a_       :			Union[str, Any]												=			(
			    {"""feature-extraction""": ViTMSNModel, """image-classification""": ViTMSNForImageClassification}
			    if is_torch_available()
			    else {}
			)

			a_       :			Tuple												=			False
			a_       :			Union[str, Any]												=			False
			a_       :			Tuple												=			False
			a_       :			List[str]												=			False
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					List[str]	       =    ViTMSNModelTester(self							)
						_lowerCamelCase					:					List[str]	       =    ConfigTester(self				,   config_class=A				,   has_text_modality=A				,   hidden_size=37							)
			def    _lowerCAmelCase		(	self							):
						self.config_tester.run_common_tests()
			@unittest.skip(reason='ViTMSN does not use inputs_embeds'							)
			def    _lowerCAmelCase		(	self							):
						pass
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase       ,							_lowerCamelCase					:					Optional[Any]	       =    self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
									_lowerCamelCase					:					List[str]	       =    model_class(A							)
									self.assertIsInstance(model.get_input_embeddings()				,   (nn.Module)							)
									_lowerCamelCase					:					Dict	       =    model.get_output_embeddings()
									self.assertTrue(x is None or isinstance(A				,   nn.Linear							)							)
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase       ,							_lowerCamelCase					:					List[str]	       =    self.model_tester.prepare_config_and_inputs_for_common()

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

									_lowerCamelCase					:					Union[str, Any]	       =    ['pixel_values']
									self.assertListEqual(arg_names[:1]				,   A							)
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					List[Any]	       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*A							)
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					List[Any]	       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*A							)

			@slow
			def    _lowerCAmelCase		(	self							):
						for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									_lowerCamelCase					:					Any	       =    ViTMSNModel.from_pretrained(A							)
									self.assertIsNotNone(A							)






def     UpperCAmelCase_					(				):




			'''simple docstring'''



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







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







			"""simple docstring"""



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

			@slow
			def    _lowerCAmelCase		(	self							):
						torch.manual_seed(2							)
						_lowerCamelCase					:					Tuple	       =    ViTMSNForImageClassification.from_pretrained('facebook/vit-msn-small'							).to(A							)

						_lowerCamelCase					:					int	       =    self.default_image_processor
						_lowerCamelCase					:					Optional[int]	       =    prepare_img()
						_lowerCamelCase					:					int	       =    image_processor(images=A				,   return_tensors='pt'							).to(A							)

						# forward pass
						with torch.no_grad():
									_lowerCamelCase					:					str	       =    model(**A							)

						# verify the logits
						_lowerCamelCase					:					Optional[Any]	       =    torch.Size((1, 1000)							)
						self.assertEqual(outputs.logits.shape				,   A							)

						_lowerCamelCase					:					Any	       =    torch.tensor([-0.0_8_0_3, -0.4_4_5_4, -0.2_3_7_5]							).to(A							)

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





