



"""simple docstring"""







import gc
import random
import tempfile
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.stable_diffusion_safe import StableDiffusionPipelineSafe as StableDiffusionPipeline
from diffusers.utils import floats_tensor, nightly, torch_device
from diffusers.utils.testing_utils import require_torch_gpu
class        __A       (	unittest.TestCase    ):







		'''simple docstring'''





		def 					UpperCAmelCase    (	self						:  List[Any]						)					->		Dict:






				"""simple docstring"""



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







		@property
		def 					UpperCAmelCase    (	self						:  Optional[Any]						)					->		List[Any]:






				"""simple docstring"""



				lowercase__   :    Dict				=			1
				lowercase__   :    Tuple				=			3
				lowercase__   :    str				=			(32, 32)

				lowercase__   :    Tuple				=			floats_tensor((batch_size, num_channels) + sizes			,rng=random.Random(0						)						).to(_snake_case						)
				return image







		@property
		def 					UpperCAmelCase    (	self						:  int						)					->		Dict:






				"""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			,)
				return model







		@property
		def 					UpperCAmelCase    (	self						:  Dict						)					->		Union[str, Any]:






				"""simple docstring"""



				torch.manual_seed(0						)
				lowercase__   :    int				=			AutoencoderKL(
				    block_out_channels=[32, 64]			,in_channels=3			,out_channels=3			,down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']			,up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']			,latent_channels=4			,)
				return model







		@property
		def 					UpperCAmelCase    (	self						:  int						)					->		Union[str, Any]:






				"""simple docstring"""



				torch.manual_seed(0						)
				lowercase__   :    int				=			CLIPTextConfig(
				    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=1_000			,)
				return CLIPTextModel(_snake_case						)







		@property
		def 					UpperCAmelCase    (	self						:  List[Any]						)					->		int:






				"""simple docstring"""



				def extract(*_snake_case						:  int			,**_snake_case						:  int						):
						class        __A       :







								'''simple docstring'''





								def __init__(	self						:  int						)					->		List[str]:






										"""simple docstring"""



										lowercase__   :    Union[str, Any]				=			torch.ones([0]						)







								def 					UpperCAmelCase    (	self						:  Optional[Any]			,_snake_case						:  List[Any]						)					->		Any:






										"""simple docstring"""



										self.pixel_values.to(_snake_case						)
										return self





						return Out()

				return extract







		def 					UpperCAmelCase    (	self						:  Dict						)					->		str:






				"""simple docstring"""



				lowercase__   :    Tuple				=			'''cpu'''  # ensure determinism for the device-dependent torch.Generator
				lowercase__   :    List[Any]				=			self.dummy_cond_unet
				lowercase__   :    Optional[Any]				=			DDIMScheduler(
				    beta_start=0.0_0085			,beta_end=0.012			,beta_schedule='''scaled_linear'''			,clip_sample=_snake_case			,set_alpha_to_one=_snake_case			,)

				lowercase__   :    str				=			self.dummy_vae
				lowercase__   :    int				=			self.dummy_text_encoder
				lowercase__   :    Optional[int]				=			CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''						)

				# make sure here that pndm scheduler skips prk
				lowercase__   :    str				=			StableDiffusionPipeline(
				    unet=_snake_case			,scheduler=_snake_case			,vae=_snake_case			,text_encoder=_snake_case			,tokenizer=_snake_case			,safety_checker=_snake_case			,feature_extractor=self.dummy_extractor			,)
				lowercase__   :    Dict				=			sd_pipe.to(_snake_case						)
				sd_pipe.set_progress_bar_config(disable=_snake_case						)

				lowercase__   :    Dict				=			'''A painting of a squirrel eating a burger'''

				lowercase__   :    List[Any]				=			torch.Generator(device=_snake_case						).manual_seed(0						)
				lowercase__   :    int				=			sd_pipe([prompt]			,generator=_snake_case			,guidance_scale=6.0			,num_inference_steps=2			,output_type='''np'''						)
				lowercase__   :    Any				=			output.images

				lowercase__   :    Union[str, Any]				=			torch.Generator(device=_snake_case						).manual_seed(0						)
				lowercase__   :    int				=			sd_pipe(
				    [prompt]			,generator=_snake_case			,guidance_scale=6.0			,num_inference_steps=2			,output_type='''np'''			,return_dict=_snake_case			,)[0]

				lowercase__   :    Union[str, Any]				=			image[0, -3:, -3:, -1]
				lowercase__   :    List[Any]				=			image_from_tuple[0, -3:, -3:, -1]

				assert image.shape == (1, 64, 64, 3)
				lowercase__   :    Tuple				=			np.array([0.5756, 0.6118, 0.5005, 0.5041, 0.5471, 0.4726, 0.4976, 0.4865, 0.4864]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2
				assert np.abs(image_from_tuple_slice.flatten() - expected_slice						).max() < 1e-2







		def 					UpperCAmelCase    (	self						:  Optional[Any]						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    Tuple				=			'''cpu'''  # ensure determinism for the device-dependent torch.Generator
				lowercase__   :    Union[str, Any]				=			self.dummy_cond_unet
				lowercase__   :    Union[str, Any]				=			PNDMScheduler(skip_prk_steps=_snake_case						)
				lowercase__   :    str				=			self.dummy_vae
				lowercase__   :    int				=			self.dummy_text_encoder
				lowercase__   :    str				=			CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''						)

				# make sure here that pndm scheduler skips prk
				lowercase__   :    Optional[int]				=			StableDiffusionPipeline(
				    unet=_snake_case			,scheduler=_snake_case			,vae=_snake_case			,text_encoder=_snake_case			,tokenizer=_snake_case			,safety_checker=_snake_case			,feature_extractor=self.dummy_extractor			,)
				lowercase__   :    Any				=			sd_pipe.to(_snake_case						)
				sd_pipe.set_progress_bar_config(disable=_snake_case						)

				lowercase__   :    int				=			'''A painting of a squirrel eating a burger'''
				lowercase__   :    str				=			torch.Generator(device=_snake_case						).manual_seed(0						)
				lowercase__   :    Optional[Any]				=			sd_pipe([prompt]			,generator=_snake_case			,guidance_scale=6.0			,num_inference_steps=2			,output_type='''np'''						)

				lowercase__   :    int				=			output.images

				lowercase__   :    Tuple				=			torch.Generator(device=_snake_case						).manual_seed(0						)
				lowercase__   :    int				=			sd_pipe(
				    [prompt]			,generator=_snake_case			,guidance_scale=6.0			,num_inference_steps=2			,output_type='''np'''			,return_dict=_snake_case			,)[0]

				lowercase__   :    List[str]				=			image[0, -3:, -3:, -1]
				lowercase__   :    Any				=			image_from_tuple[0, -3:, -3:, -1]

				assert image.shape == (1, 64, 64, 3)
				lowercase__   :    int				=			np.array([0.5125, 0.5716, 0.4828, 0.5060, 0.5650, 0.4768, 0.5185, 0.4895, 0.4993]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2
				assert np.abs(image_from_tuple_slice.flatten() - expected_slice						).max() < 1e-2







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






				"""simple docstring"""



				lowercase__   :    List[Any]				=			StableDiffusionPipeline.from_pretrained(
				    '''hf-internal-testing/tiny-stable-diffusion-lms-pipe'''			,safety_checker=_snake_case						)
				assert isinstance(_snake_case			,_snake_case						)
				assert isinstance(pipe.scheduler			,_snake_case						)
				assert pipe.safety_checker is None

				lowercase__   :    Union[str, Any]				=			pipe('''example prompt'''			,num_inference_steps=2						).images[0]
				assert image is not None

				# check that there's no error when saving a pipeline with one of the models being None
				with tempfile.TemporaryDirectory() as tmpdirname:
						pipe.save_pretrained(_snake_case						)
						lowercase__   :    Tuple				=			StableDiffusionPipeline.from_pretrained(_snake_case						)

				# sanity check that the pipeline still works
				assert pipe.safety_checker is None
				lowercase__   :    str				=			pipe('''example prompt'''			,num_inference_steps=2						).images[0]
				assert image is not None







		@unittest.skipIf(torch_device != '''cuda'''			,'''This test requires a GPU'''						)
		def 					UpperCAmelCase    (	self						:  str						)					->		Dict:






				"""simple docstring"""



				lowercase__   :    Any				=			self.dummy_cond_unet
				lowercase__   :    Optional[Any]				=			PNDMScheduler(skip_prk_steps=_snake_case						)
				lowercase__   :    int				=			self.dummy_vae
				lowercase__   :    List[str]				=			self.dummy_text_encoder
				lowercase__   :    List[Any]				=			CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''						)

				# put models in fp16
				lowercase__   :    Dict				=			unet.half()
				lowercase__   :    Any				=			vae.half()
				lowercase__   :    Tuple				=			bert.half()

				# make sure here that pndm scheduler skips prk
				lowercase__   :    Union[str, Any]				=			StableDiffusionPipeline(
				    unet=_snake_case			,scheduler=_snake_case			,vae=_snake_case			,text_encoder=_snake_case			,tokenizer=_snake_case			,safety_checker=_snake_case			,feature_extractor=self.dummy_extractor			,)
				lowercase__   :    str				=			sd_pipe.to(_snake_case						)
				sd_pipe.set_progress_bar_config(disable=_snake_case						)

				lowercase__   :    Any				=			'''A painting of a squirrel eating a burger'''
				lowercase__   :    Union[str, Any]				=			sd_pipe([prompt]			,num_inference_steps=2			,output_type='''np'''						).images

				assert image.shape == (1, 64, 64, 3)





@nightly
@require_torch_gpu
class        __A       (	unittest.TestCase    ):







		'''simple docstring'''





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






				"""simple docstring"""



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







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






				"""simple docstring"""



				lowercase__   :    int				=			StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5'''			,safety_checker=_snake_case						)
				lowercase__   :    Union[str, Any]				=			LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config						)
				lowercase__   :    str				=			sd_pipe.to(_snake_case						)
				sd_pipe.set_progress_bar_config(disable=_snake_case						)

				lowercase__   :    Optional[int]				=			(
				    '''portrait of girl with smokey eyes makeup in abandoned hotel, grange clothes, redshift, wide high angle'''
				    ''' coloured polaroid photograph with flash, kodak film, hyper real, stunning moody cinematography, with'''
				    ''' anamorphic lenses, by maripol, fallen angels by wong kar - wai, style of suspiria and neon demon and'''
				    ''' children from bahnhof zoo, detailed '''
				)
				lowercase__   :    List[Any]				=			4_003_660_346
				lowercase__   :    List[Any]				=			7

				# without safety guidance (sld_guidance_scale = 0)
				lowercase__   :    Optional[Any]				=			torch.manual_seed(_snake_case						)
				lowercase__   :    str				=			sd_pipe(
				    [prompt]			,generator=_snake_case			,guidance_scale=_snake_case			,num_inference_steps=50			,output_type='''np'''			,width=512			,height=512			,sld_guidance_scale=0			,)

				lowercase__   :    Dict				=			output.images
				lowercase__   :    Union[str, Any]				=			image[0, -3:, -3:, -1]
				lowercase__   :    int				=			[0.2278, 0.2231, 0.2249, 0.2333, 0.2303, 0.1885, 0.2273, 0.2144, 0.2176]

				assert image.shape == (1, 512, 512, 3)

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

				# without safety guidance (strong configuration)
				lowercase__   :    Optional[int]				=			torch.manual_seed(_snake_case						)
				lowercase__   :    Optional[Any]				=			sd_pipe(
				    [prompt]			,generator=_snake_case			,guidance_scale=_snake_case			,num_inference_steps=50			,output_type='''np'''			,width=512			,height=512			,sld_guidance_scale=2_000			,sld_warmup_steps=7			,sld_threshold=0.025			,sld_momentum_scale=0.5			,sld_mom_beta=0.7			,)

				lowercase__   :    List[Any]				=			output.images
				lowercase__   :    Dict				=			image[0, -3:, -3:, -1]
				lowercase__   :    Optional[Any]				=			[0.2383, 0.2276, 0.236, 0.2192, 0.2186, 0.2053, 0.1971, 0.1901, 0.1719]

				assert image.shape == (1, 512, 512, 3)

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







		def 					UpperCAmelCase    (	self						:  Any						)					->		Tuple:






				"""simple docstring"""



				lowercase__   :    Optional[Any]				=			StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5'''			,safety_checker=_snake_case						)
				lowercase__   :    Optional[Any]				=			LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config						)
				lowercase__   :    Optional[Any]				=			sd_pipe.to(_snake_case						)
				sd_pipe.set_progress_bar_config(disable=_snake_case						)

				lowercase__   :    List[Any]				=			'''padme amidala taking a bath artwork, safe for work, no nudity'''
				lowercase__   :    Tuple				=			2_734_971_755
				lowercase__   :    List[str]				=			7

				lowercase__   :    Dict				=			torch.manual_seed(_snake_case						)
				lowercase__   :    Union[str, Any]				=			sd_pipe(
				    [prompt]			,generator=_snake_case			,guidance_scale=_snake_case			,num_inference_steps=50			,output_type='''np'''			,width=512			,height=512			,sld_guidance_scale=0			,)

				lowercase__   :    str				=			output.images
				lowercase__   :    List[Any]				=			image[0, -3:, -3:, -1]
				lowercase__   :    Optional[Any]				=			[0.3502, 0.3622, 0.3396, 0.3642, 0.3478, 0.3318, 0.35, 0.3348, 0.3297]

				assert image.shape == (1, 512, 512, 3)

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

				lowercase__   :    Tuple				=			torch.manual_seed(_snake_case						)
				lowercase__   :    Union[str, Any]				=			sd_pipe(
				    [prompt]			,generator=_snake_case			,guidance_scale=_snake_case			,num_inference_steps=50			,output_type='''np'''			,width=512			,height=512			,sld_guidance_scale=2_000			,sld_warmup_steps=7			,sld_threshold=0.025			,sld_momentum_scale=0.5			,sld_mom_beta=0.7			,)

				lowercase__   :    int				=			output.images
				lowercase__   :    Dict				=			image[0, -3:, -3:, -1]
				lowercase__   :    Optional[int]				=			[0.5531, 0.5206, 0.4895, 0.5156, 0.5182, 0.4751, 0.4802, 0.4803, 0.4443]

				assert image.shape == (1, 512, 512, 3)

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







		def 					UpperCAmelCase    (	self						:  Tuple						)					->		Any:






				"""simple docstring"""



				lowercase__   :    str				=			StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5'''						)
				lowercase__   :    int				=			sd_pipe.to(_snake_case						)
				sd_pipe.set_progress_bar_config(disable=_snake_case						)

				lowercase__   :    Optional[int]				=			(
				    '''the four horsewomen of the apocalypse, painting by tom of finland, gaston bussiere, craig mullins, j. c.'''
				    ''' leyendecker'''
				)
				lowercase__   :    List[Any]				=			1_044_355_234
				lowercase__   :    Optional[int]				=			12

				lowercase__   :    List[Any]				=			torch.manual_seed(_snake_case						)
				lowercase__   :    Dict				=			sd_pipe(
				    [prompt]			,generator=_snake_case			,guidance_scale=_snake_case			,num_inference_steps=50			,output_type='''np'''			,width=512			,height=512			,sld_guidance_scale=0			,)

				lowercase__   :    str				=			output.images
				lowercase__   :    Optional[Any]				=			image[0, -3:, -3:, -1]
				lowercase__   :    List[str]				=			np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]						)

				assert image.shape == (1, 512, 512, 3)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-7

				lowercase__   :    Optional[int]				=			torch.manual_seed(_snake_case						)
				lowercase__   :    Any				=			sd_pipe(
				    [prompt]			,generator=_snake_case			,guidance_scale=_snake_case			,num_inference_steps=50			,output_type='''np'''			,width=512			,height=512			,sld_guidance_scale=2_000			,sld_warmup_steps=7			,sld_threshold=0.025			,sld_momentum_scale=0.5			,sld_mom_beta=0.7			,)

				lowercase__   :    Dict				=			output.images
				lowercase__   :    Dict				=			image[0, -3:, -3:, -1]
				lowercase__   :    Dict				=			np.array([0.5818, 0.6285, 0.6835, 0.6019, 0.625, 0.6754, 0.6096, 0.6334, 0.6561]						)
				assert image.shape == (1, 512, 512, 3)

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





"""simple docstring"""







import collections
import inspect
import unittest

from transformers import SwinvaConfig
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, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch
							from torch import nn

							from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel
							from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor
class        __A       :







		'''simple docstring'''





		def __init__(	self						:  Optional[int]			,_snake_case						:  Optional[Any]			,_snake_case						:  Union[str, Any]=13			,_snake_case						:  Any=32			,_snake_case						:  int=2			,_snake_case						:  str=3			,_snake_case						:  Optional[Any]=16			,_snake_case						:  List[Any]=[1, 2, 1]			,_snake_case						:  Dict=[2, 2, 4]			,_snake_case						:  List[Any]=2			,_snake_case						:  Any=2.0			,_snake_case						:  Optional[int]=True			,_snake_case						:  Optional[int]=0.0			,_snake_case						:  Union[str, Any]=0.0			,_snake_case						:  str=0.1			,_snake_case						:  List[Any]="gelu"			,_snake_case						:  Tuple=False			,_snake_case						:  Optional[int]=True			,_snake_case						:  str=0.02			,_snake_case						:  List[str]=1e-5			,_snake_case						:  int=True			,_snake_case						:  Dict=None			,_snake_case						:  str=True			,_snake_case						:  List[Any]=10			,_snake_case						:  Any=8			,)					->		Union[str, Any]:






				"""simple docstring"""



				lowercase__   :    Dict				=			parent
				lowercase__   :    Any				=			batch_size
				lowercase__   :    Union[str, Any]				=			image_size
				lowercase__   :    Dict				=			patch_size
				lowercase__   :    int				=			num_channels
				lowercase__   :    Any				=			embed_dim
				lowercase__   :    int				=			depths
				lowercase__   :    Dict				=			num_heads
				lowercase__   :    List[Any]				=			window_size
				lowercase__   :    int				=			mlp_ratio
				lowercase__   :    Optional[int]				=			qkv_bias
				lowercase__   :    str				=			hidden_dropout_prob
				lowercase__   :    List[Any]				=			attention_probs_dropout_prob
				lowercase__   :    Dict				=			drop_path_rate
				lowercase__   :    int				=			hidden_act
				lowercase__   :    Tuple				=			use_absolute_embeddings
				lowercase__   :    Tuple				=			patch_norm
				lowercase__   :    Tuple				=			layer_norm_eps
				lowercase__   :    Optional[Any]				=			initializer_range
				lowercase__   :    int				=			is_training
				lowercase__   :    Optional[int]				=			scope
				lowercase__   :    str				=			use_labels
				lowercase__   :    Dict				=			type_sequence_label_size
				lowercase__   :    Union[str, Any]				=			encoder_stride







		def 					UpperCAmelCase    (	self						:  Dict						)					->		Any:






				"""simple docstring"""



				lowercase__   :    str				=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

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

				return config, pixel_values, labels







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






				"""simple docstring"""



				return SwinvaConfig(
				    image_size=self.image_size			,patch_size=self.patch_size			,num_channels=self.num_channels			,embed_dim=self.embed_dim			,depths=self.depths			,num_heads=self.num_heads			,window_size=self.window_size			,mlp_ratio=self.mlp_ratio			,qkv_bias=self.qkv_bias			,hidden_dropout_prob=self.hidden_dropout_prob			,attention_probs_dropout_prob=self.attention_probs_dropout_prob			,drop_path_rate=self.drop_path_rate			,hidden_act=self.hidden_act			,use_absolute_embeddings=self.use_absolute_embeddings			,path_norm=self.patch_norm			,layer_norm_eps=self.layer_norm_eps			,initializer_range=self.initializer_range			,encoder_stride=self.encoder_stride			,)







		def 					UpperCAmelCase    (	self						:  str			,_snake_case						:  Dict			,_snake_case						:  List[str]			,_snake_case						:  Optional[int]						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    Any				=			SwinvaModel(config=_snake_case						)
				model.to(_snake_case						)
				model.eval()
				lowercase__   :    str				=			model(_snake_case						)

				lowercase__   :    List[Any]				=			((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths						) - 1))
				lowercase__   :    Tuple				=			int(config.embed_dim * 2 ** (len(config.depths						) - 1)						)

				self.parent.assertEqual(result.last_hidden_state.shape			,(self.batch_size, expected_seq_len, expected_dim)						)







		def 					UpperCAmelCase    (	self						:  Union[str, Any]			,_snake_case						:  List[str]			,_snake_case						:  Optional[Any]			,_snake_case						:  int						)					->		Any:






				"""simple docstring"""



				lowercase__   :    Union[str, Any]				=			SwinvaForMaskedImageModeling(config=_snake_case						)
				model.to(_snake_case						)
				model.eval()
				lowercase__   :    Tuple				=			model(_snake_case						)
				self.parent.assertEqual(
				    result.logits.shape			,(self.batch_size, self.num_channels, self.image_size, self.image_size)						)

				# test greyscale images
				lowercase__   :    Optional[int]				=			1
				lowercase__   :    List[Any]				=			SwinvaForMaskedImageModeling(_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__   :    str				=			model(_snake_case						)
				self.parent.assertEqual(result.logits.shape			,(self.batch_size, 1, self.image_size, self.image_size)						)







		def 					UpperCAmelCase    (	self						:  str			,_snake_case						:  str			,_snake_case						:  str			,_snake_case						:  Tuple						)					->		Any:






				"""simple docstring"""



				lowercase__   :    Tuple				=			self.type_sequence_label_size
				lowercase__   :    Dict				=			SwinvaForImageClassification(_snake_case						)
				model.to(_snake_case						)
				model.eval()
				lowercase__   :    str				=			model(_snake_case			,labels=_snake_case						)
				self.parent.assertEqual(result.logits.shape			,(self.batch_size, self.type_sequence_label_size)						)







		def 					UpperCAmelCase    (	self						:  Dict						)					->		Dict:






				"""simple docstring"""



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





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







		'''simple docstring'''





		lowerCAmelCase			:					Union[str, Any]   	=				(
		    (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
		)
		lowerCAmelCase			:					Optional[int]   	=				(
		    {"feature-extraction": SwinvaModel, "image-classification": SwinvaForImageClassification}
		    if is_torch_available()
		    else {}
		)

		lowerCAmelCase			:					List[Any]   	=				False
		lowerCAmelCase			:					Dict   	=				False
		lowerCAmelCase			:					List[Any]   	=				False
		lowerCAmelCase			:					Any   	=				False







		def 					UpperCAmelCase    (	self						:  Optional[int]						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    Optional[Any]				=			SwinvaModelTester(self						)
				lowercase__   :    List[str]				=			ConfigTester(self			,config_class=_snake_case			,embed_dim=37						)







		def 					UpperCAmelCase    (	self						:  int						)					->		Any:






				"""simple docstring"""



				self.config_tester.create_and_test_config_to_json_string()
				self.config_tester.create_and_test_config_to_json_file()
				self.config_tester.create_and_test_config_from_and_save_pretrained()
				self.config_tester.create_and_test_config_with_num_labels()
				self.config_tester.check_config_can_be_init_without_params()
				self.config_tester.check_config_arguments_init()







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






				"""simple docstring"""



				lowercase__   :    int				=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_snake_case						)







		@unittest.skip(reason='''Got `CUDA error: misaligned address` with PyTorch 2.0.0.'''						)
		def 					UpperCAmelCase    (	self						:  Optional[Any]						)					->		Optional[int]:






				"""simple docstring"""



				pass







		@unittest.skip(reason='''Swinv2 does not use inputs_embeds'''						)
		def 					UpperCAmelCase    (	self						:  List[str]						)					->		str:






				"""simple docstring"""



				pass







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






				"""simple docstring"""



				lowercase__		,			lowercase__   :    Optional[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						)
						self.assertIsInstance(model.get_input_embeddings()			,(nn.Module)						)
						lowercase__   :    str				=			model.get_output_embeddings()
						self.assertTrue(x is None or isinstance(_snake_case			,nn.Linear						)						)







		def 					UpperCAmelCase    (	self						:  int						)					->		List[Any]:






				"""simple docstring"""



				lowercase__		,			lowercase__   :    Union[str, Any]				=			self.model_tester.prepare_config_and_inputs_for_common()

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

						lowercase__   :    Tuple				=			['''pixel_values''']
						self.assertListEqual(arg_names[:1]			,_snake_case						)







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






				"""simple docstring"""



				lowercase__		,			lowercase__   :    List[str]				=			self.model_tester.prepare_config_and_inputs_for_common()
				lowercase__   :    Tuple				=			True

				for model_class in self.all_model_classes:
						lowercase__   :    Optional[int]				=			True
						lowercase__   :    str				=			False
						lowercase__   :    Union[str, Any]				=			True
						lowercase__   :    Optional[Any]				=			model_class(_snake_case						)
						model.to(_snake_case						)
						model.eval()
						with torch.no_grad():
								lowercase__   :    str				=			model(**self._prepare_for_class(_snake_case			,_snake_case						)						)
						lowercase__   :    Dict				=			outputs.attentions
						lowercase__   :    Any				=			len(self.model_tester.depths						)
						self.assertEqual(len(_snake_case						)			,_snake_case						)

						# check that output_attentions also work using config
						del inputs_dict["output_attentions"]
						lowercase__   :    List[Any]				=			True
						lowercase__   :    Optional[Any]				=			config.window_size**2
						lowercase__   :    Any				=			model_class(_snake_case						)
						model.to(_snake_case						)
						model.eval()
						with torch.no_grad():
								lowercase__   :    List[str]				=			model(**self._prepare_for_class(_snake_case			,_snake_case						)						)
						lowercase__   :    Optional[Any]				=			outputs.attentions
						self.assertEqual(len(_snake_case						)			,_snake_case						)

						self.assertListEqual(
						    list(attentions[0].shape[-3:]						)			,[self.model_tester.num_heads[0], window_size_squared, window_size_squared]			,)
						lowercase__   :    Optional[Any]				=			len(_snake_case						)

						# Check attention is always last and order is fine
						lowercase__   :    Optional[int]				=			True
						lowercase__   :    Tuple				=			True
						lowercase__   :    Optional[Any]				=			model_class(_snake_case						)
						model.to(_snake_case						)
						model.eval()
						with torch.no_grad():
								lowercase__   :    Optional[Any]				=			model(**self._prepare_for_class(_snake_case			,_snake_case						)						)

						if hasattr(self.model_tester			,'''num_hidden_states_types'''						):
								lowercase__   :    int				=			self.model_tester.num_hidden_states_types
						else:
								# also another +1 for reshaped_hidden_states
								lowercase__   :    List[str]				=			2
						self.assertEqual(out_len + added_hidden_states			,len(_snake_case						)						)

						lowercase__   :    Optional[int]				=			outputs.attentions

						self.assertEqual(len(_snake_case						)			,_snake_case						)

						self.assertListEqual(
						    list(self_attentions[0].shape[-3:]						)			,[self.model_tester.num_heads[0], window_size_squared, window_size_squared]			,)







		def 					UpperCAmelCase    (	self						:  List[str]			,_snake_case						:  int			,_snake_case						:  List[str]			,_snake_case						:  Optional[int]			,_snake_case						:  List[Any]						)					->		Union[str, Any]:






				"""simple docstring"""



				lowercase__   :    List[Any]				=			model_class(_snake_case						)
				model.to(_snake_case						)
				model.eval()

				with torch.no_grad():
						lowercase__   :    int				=			model(**self._prepare_for_class(_snake_case			,_snake_case						)						)

				lowercase__   :    Optional[int]				=			outputs.hidden_states

				lowercase__   :    List[Any]				=			getattr(
				    self.model_tester			,'''expected_num_hidden_layers'''			,len(self.model_tester.depths						) + 1						)
				self.assertEqual(len(_snake_case						)			,_snake_case						)

				# Swinv2 has a different seq_length
				lowercase__   :    Dict				=			(
				    config.patch_size
				    if isinstance(config.patch_size			,collections.abc.Iterable						)
				    else (config.patch_size, config.patch_size)
				)

				lowercase__   :    int				=			(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

				self.assertListEqual(
				    list(hidden_states[0].shape[-2:]						)			,[num_patches, self.model_tester.embed_dim]			,)

				lowercase__   :    Tuple				=			outputs.reshaped_hidden_states
				self.assertEqual(len(_snake_case						)			,_snake_case						)

				lowercase__		,			lowercase__		,			lowercase__		,			lowercase__   :    List[str]				=			reshaped_hidden_states[0].shape
				lowercase__   :    int				=			(
				    reshaped_hidden_states[0].view(_snake_case			,_snake_case			,height * width						).permute(0			,2			,1						)
				)
				self.assertListEqual(
				    list(reshaped_hidden_states.shape[-2:]						)			,[num_patches, self.model_tester.embed_dim]			,)







		def 					UpperCAmelCase    (	self						:  Tuple						)					->		int:






				"""simple docstring"""



				lowercase__		,			lowercase__   :    Optional[int]				=			self.model_tester.prepare_config_and_inputs_for_common()

				lowercase__   :    str				=			(
				    self.model_tester.image_size
				    if isinstance(self.model_tester.image_size			,collections.abc.Iterable						)
				    else (self.model_tester.image_size, self.model_tester.image_size)
				)

				for model_class in self.all_model_classes:
						lowercase__   :    List[str]				=			True
						self.check_hidden_states_output(_snake_case			,_snake_case			,_snake_case			,_snake_case						)

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						lowercase__   :    str				=			True

						self.check_hidden_states_output(_snake_case			,_snake_case			,_snake_case			,_snake_case						)







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






				"""simple docstring"""



				lowercase__		,			lowercase__   :    List[str]				=			self.model_tester.prepare_config_and_inputs_for_common()
				lowercase__   :    List[Any]				=			3

				lowercase__   :    Tuple				=			(
				    self.model_tester.image_size
				    if isinstance(self.model_tester.image_size			,collections.abc.Iterable						)
				    else (self.model_tester.image_size, self.model_tester.image_size)
				)
				lowercase__   :    Optional[int]				=			(
				    config.patch_size
				    if isinstance(config.patch_size			,collections.abc.Iterable						)
				    else (config.patch_size, config.patch_size)
				)

				lowercase__   :    Dict				=			image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
				lowercase__   :    Dict				=			image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

				for model_class in self.all_model_classes:
						lowercase__   :    str				=			True
						self.check_hidden_states_output(_snake_case			,_snake_case			,_snake_case			,(padded_height, padded_width)						)

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						lowercase__   :    Dict				=			True
						self.check_hidden_states_output(_snake_case			,_snake_case			,_snake_case			,(padded_height, padded_width)						)







		def 					UpperCAmelCase    (	self						:  Tuple						)					->		List[Any]:






				"""simple docstring"""



				lowercase__   :    Union[str, Any]				=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_masked_image_modeling(*_snake_case						)







		def 					UpperCAmelCase    (	self						:  Dict						)					->		Any:






				"""simple docstring"""



				lowercase__   :    Tuple				=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*_snake_case						)







		@slow
		def 					UpperCAmelCase    (	self						:  List[str]						)					->		Optional[Any]:






				"""simple docstring"""



				for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						lowercase__   :    Union[str, Any]				=			SwinvaModel.from_pretrained(_snake_case						)
						self.assertIsNotNone(_snake_case						)







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






				"""simple docstring"""



				lowercase__		,			lowercase__   :    str				=			self.model_tester.prepare_config_and_inputs_for_common()

				lowercase__   :    Tuple				=			_config_zero_init(_snake_case						)
				for model_class in self.all_model_classes:
						lowercase__   :    Optional[int]				=			model_class(config=_snake_case						)
						for name, param in model.named_parameters():
								if "embeddings" not in name and "logit_scale" not in name and param.requires_grad:
										self.assertIn(
										    ((param.data.mean() * 1e9).round() / 1e9).item()			,[0.0, 1.0]			,msg=f"""Parameter {name} of model {model_class} seems not properly initialized"""			,)





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







		'''simple docstring'''





		@cached_property
		def 					UpperCAmelCase    (	self						:  Optional[Any]						)					->		Tuple:






				"""simple docstring"""



				return (
				    AutoImageProcessor.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256'''						)
				    if is_vision_available()
				    else None
				)







		@slow
		def 					UpperCAmelCase    (	self						:  Any						)					->		List[str]:






				"""simple docstring"""



				lowercase__   :    str				=			SwinvaForImageClassification.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256'''						).to(
				    _snake_case						)
				lowercase__   :    Union[str, Any]				=			self.default_image_processor

				lowercase__   :    List[str]				=			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
				lowercase__   :    Dict				=			image_processor(images=_snake_case			,return_tensors='''pt'''						).to(_snake_case						)

				# forward pass
				with torch.no_grad():
						lowercase__   :    Optional[Any]				=			model(**_snake_case						)

				# verify the logits
				lowercase__   :    str				=			torch.Size((1, 1_000)						)
				self.assertEqual(outputs.logits.shape			,_snake_case						)
				lowercase__   :    Dict				=			torch.tensor([-0.3947, -0.4306, 0.0026]						).to(_snake_case						)
				self.assertTrue(torch.allclose(outputs.logits[0, :3]			,_snake_case			,atol=1e-4						)						)
