
'''simple docstring'''
import gc
import random
import unittest

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

from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableDiffusionUpscalePipeline, UNetaDConditionModel
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()






class 	snake_case__       (      unittest.TestCase					):







		"""simple docstring"""

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






				"""simple docstring"""







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



		@property
		def       lowerCAmelCase				(      self				:			Tuple      )						->			Union[str, Any]:






				"""simple docstring"""







				snake_case  :       Union[str, Any]     			=						1
				snake_case  :       Dict     			=						3
				snake_case  :       Any     			=						(32, 32)

				snake_case  :       Any     			=						floats_tensor((batch_size, num_channels) + sizes  ,	rng=random.Random(0      )      ).to(UpperCamelCase__      )
				return image



		@property
		def       lowerCAmelCase				(      self				:			Any      )						->			List[str]:






				"""simple docstring"""







				torch.manual_seed(0      )
				snake_case  :       Optional[int]     			=						UNetaDConditionModel(
				    block_out_channels=(32, 32, 64)  ,	layers_per_block=2  ,	sample_size=32  ,	in_channels=7  ,	out_channels=4  ,	down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''', '''CrossAttnDownBlock2D''')  ,	up_block_types=('''CrossAttnUpBlock2D''', '''CrossAttnUpBlock2D''', '''UpBlock2D''')  ,	cross_attention_dim=32  ,	attention_head_dim=8  ,	use_linear_projection=UpperCamelCase__  ,	only_cross_attention=(True, True, False)  ,	num_class_embeds=100  ,	)
				return model



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






				"""simple docstring"""







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



		@property
		def       lowerCAmelCase				(      self				:			Any      )						->			int:






				"""simple docstring"""







				torch.manual_seed(0      )
				snake_case  :       Union[str, Any]     			=						CLIPTextConfig(
				    bos_token_id=0  ,	eos_token_id=2  ,	hidden_size=32  ,	intermediate_size=37  ,	layer_norm_eps=1e-0_5  ,	num_attention_heads=4  ,	num_hidden_layers=5  ,	pad_token_id=1  ,	vocab_size=1000  ,	hidden_act='''gelu'''  ,	projection_dim=512  ,	)
				return CLIPTextModel(UpperCamelCase__      )



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






				"""simple docstring"""







				snake_case  :       Dict     			=						'''cpu'''  # ensure determinism for the device-dependent torch.Generator
				snake_case  :       int     			=						self.dummy_cond_unet_upscale
				snake_case  :       Dict     			=						DDPMScheduler()
				snake_case  :       Any     			=						DDIMScheduler(prediction_type='''v_prediction'''      )
				snake_case  :       Any     			=						self.dummy_vae
				snake_case  :       Tuple     			=						self.dummy_text_encoder
				snake_case  :       List[str]     			=						CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''      )

				snake_case  :       str     			=						self.dummy_image.cpu().permute(0  ,	2  ,	3  ,	1      )[0]
				snake_case  :       Optional[Any]     			=						Image.fromarray(np.uinta(UpperCamelCase__      )      ).convert('''RGB'''      ).resize((64, 64)      )

				# make sure here that pndm scheduler skips prk
				snake_case  :       int     			=						StableDiffusionUpscalePipeline(
				    unet=UpperCamelCase__  ,	low_res_scheduler=UpperCamelCase__  ,	scheduler=UpperCamelCase__  ,	vae=UpperCamelCase__  ,	text_encoder=UpperCamelCase__  ,	tokenizer=UpperCamelCase__  ,	max_noise_level=350  ,	)
				snake_case  :       List[Any]     			=						sd_pipe.to(UpperCamelCase__      )
				sd_pipe.set_progress_bar_config(disable=UpperCamelCase__      )

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

				snake_case  :       int     			=						output.images

				snake_case  :       Optional[int]     			=						torch.Generator(device=UpperCamelCase__      ).manual_seed(0      )
				snake_case  :       Dict     			=						sd_pipe(
				    [prompt]  ,	image=UpperCamelCase__  ,	generator=UpperCamelCase__  ,	guidance_scale=6.0  ,	noise_level=20  ,	num_inference_steps=2  ,	output_type='''np'''  ,	return_dict=UpperCamelCase__  ,	)[0]

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

				snake_case  :       int     			=						low_res_image.size[0] * 4
				assert image.shape == (1, expected_height_width, expected_height_width, 3)
				snake_case  :       str     			=						np.array([0.3_113, 0.3_910, 0.4_272, 0.4_859, 0.5_061, 0.4_652, 0.5_362, 0.5_715, 0.5_661]      )

				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       lowerCAmelCase				(      self				:			List[str]      )						->			List[str]:






				"""simple docstring"""







				snake_case  :       Union[str, Any]     			=						'''cpu'''  # ensure determinism for the device-dependent torch.Generator
				snake_case  :       int     			=						self.dummy_cond_unet_upscale
				snake_case  :       List[str]     			=						DDPMScheduler()
				snake_case  :       Tuple     			=						DDIMScheduler(prediction_type='''v_prediction'''      )
				snake_case  :       Optional[Any]     			=						self.dummy_vae
				snake_case  :       List[str]     			=						self.dummy_text_encoder
				snake_case  :       Tuple     			=						CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''      )

				snake_case  :       Optional[int]     			=						self.dummy_image.cpu().permute(0  ,	2  ,	3  ,	1      )[0]
				snake_case  :       int     			=						Image.fromarray(np.uinta(UpperCamelCase__      )      ).convert('''RGB'''      ).resize((64, 64)      )

				# make sure here that pndm scheduler skips prk
				snake_case  :       Dict     			=						StableDiffusionUpscalePipeline(
				    unet=UpperCamelCase__  ,	low_res_scheduler=UpperCamelCase__  ,	scheduler=UpperCamelCase__  ,	vae=UpperCamelCase__  ,	text_encoder=UpperCamelCase__  ,	tokenizer=UpperCamelCase__  ,	max_noise_level=350  ,	)
				snake_case  :       List[str]     			=						sd_pipe.to(UpperCamelCase__      )
				sd_pipe.set_progress_bar_config(disable=UpperCamelCase__      )

				snake_case  :       Optional[int]     			=						'''A painting of a squirrel eating a burger'''
				snake_case  :       Tuple     			=						sd_pipe(
				    2 * [prompt]  ,	image=2 * [low_res_image]  ,	guidance_scale=6.0  ,	noise_level=20  ,	num_inference_steps=2  ,	output_type='''np'''  ,	)
				snake_case  :       List[str]     			=						output.images
				assert image.shape[0] == 2

				snake_case  :       Tuple     			=						torch.Generator(device=UpperCamelCase__      ).manual_seed(0      )
				snake_case  :       Optional[int]     			=						sd_pipe(
				    [prompt]  ,	image=UpperCamelCase__  ,	generator=UpperCamelCase__  ,	num_images_per_prompt=2  ,	guidance_scale=6.0  ,	noise_level=20  ,	num_inference_steps=2  ,	output_type='''np'''  ,	)
				snake_case  :       Any     			=						output.images
				assert image.shape[0] == 2



		@unittest.skipIf(torch_device != '''cuda'''  ,	'''This test requires a GPU'''      )
		def       lowerCAmelCase				(      self				:			Dict      )						->			List[Any]:






				"""simple docstring"""







				snake_case  :       List[Any]     			=						self.dummy_cond_unet_upscale
				snake_case  :       str     			=						DDPMScheduler()
				snake_case  :       Tuple     			=						DDIMScheduler(prediction_type='''v_prediction'''      )
				snake_case  :       List[str]     			=						self.dummy_vae
				snake_case  :       str     			=						self.dummy_text_encoder
				snake_case  :       Dict     			=						CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''      )

				snake_case  :       List[str]     			=						self.dummy_image.cpu().permute(0  ,	2  ,	3  ,	1      )[0]
				snake_case  :       Any     			=						Image.fromarray(np.uinta(UpperCamelCase__      )      ).convert('''RGB'''      ).resize((64, 64)      )

				# put models in fp16, except vae as it overflows in fp16
				snake_case  :       Dict     			=						unet.half()
				snake_case  :       Tuple     			=						text_encoder.half()

				# make sure here that pndm scheduler skips prk
				snake_case  :       Optional[Any]     			=						StableDiffusionUpscalePipeline(
				    unet=UpperCamelCase__  ,	low_res_scheduler=UpperCamelCase__  ,	scheduler=UpperCamelCase__  ,	vae=UpperCamelCase__  ,	text_encoder=UpperCamelCase__  ,	tokenizer=UpperCamelCase__  ,	max_noise_level=350  ,	)
				snake_case  :       str     			=						sd_pipe.to(UpperCamelCase__      )
				sd_pipe.set_progress_bar_config(disable=UpperCamelCase__      )

				snake_case  :       Tuple     			=						'''A painting of a squirrel eating a burger'''
				snake_case  :       str     			=						torch.manual_seed(0      )
				snake_case  :       int     			=						sd_pipe(
				    [prompt]  ,	image=UpperCamelCase__  ,	generator=UpperCamelCase__  ,	num_inference_steps=2  ,	output_type='''np'''  ,	).images

				snake_case  :       Dict     			=						low_res_image.size[0] * 4
				assert image.shape == (1, expected_height_width, expected_height_width, 3)







@slow
@require_torch_gpu
class 	snake_case__       (      unittest.TestCase					):







		"""simple docstring"""

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






				"""simple docstring"""







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



		def       lowerCAmelCase				(      self				:			Optional[int]      )						->			Dict:






				"""simple docstring"""







				snake_case  :       str     			=						load_image(
				    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
				    '''/sd2-upscale/low_res_cat.png'''      )
				snake_case  :       Any     			=						load_numpy(
				    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale'''
				    '''/upsampled_cat.npy'''      )

				snake_case  :       List[str]     			=						'''stabilityai/stable-diffusion-x4-upscaler'''
				snake_case  :       Optional[int]     			=						StableDiffusionUpscalePipeline.from_pretrained(UpperCamelCase__      )
				pipe.to(UpperCamelCase__      )
				pipe.set_progress_bar_config(disable=UpperCamelCase__      )
				pipe.enable_attention_slicing()

				snake_case  :       Tuple     			=						'''a cat sitting on a park bench'''

				snake_case  :       str     			=						torch.manual_seed(0      )
				snake_case  :       Dict     			=						pipe(
				    prompt=UpperCamelCase__  ,	image=UpperCamelCase__  ,	generator=UpperCamelCase__  ,	output_type='''np'''  ,	)
				snake_case  :       Union[str, Any]     			=						output.images[0]

				assert image.shape == (512, 512, 3)
				assert np.abs(expected_image - image      ).max() < 1e-3



		def       lowerCAmelCase				(      self				:			Tuple      )						->			Optional[Any]:






				"""simple docstring"""







				snake_case  :       Tuple     			=						load_image(
				    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
				    '''/sd2-upscale/low_res_cat.png'''      )
				snake_case  :       str     			=						load_numpy(
				    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale'''
				    '''/upsampled_cat_fp16.npy'''      )

				snake_case  :       Optional[Any]     			=						'''stabilityai/stable-diffusion-x4-upscaler'''
				snake_case  :       Tuple     			=						StableDiffusionUpscalePipeline.from_pretrained(
				    UpperCamelCase__  ,	torch_dtype=torch.floataa  ,	)
				pipe.to(UpperCamelCase__      )
				pipe.set_progress_bar_config(disable=UpperCamelCase__      )
				pipe.enable_attention_slicing()

				snake_case  :       Optional[int]     			=						'''a cat sitting on a park bench'''

				snake_case  :       Optional[Any]     			=						torch.manual_seed(0      )
				snake_case  :       Any     			=						pipe(
				    prompt=UpperCamelCase__  ,	image=UpperCamelCase__  ,	generator=UpperCamelCase__  ,	output_type='''np'''  ,	)
				snake_case  :       int     			=						output.images[0]

				assert image.shape == (512, 512, 3)
				assert np.abs(expected_image - image      ).max() < 5e-1



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






				"""simple docstring"""







				torch.cuda.empty_cache()
				torch.cuda.reset_max_memory_allocated()
				torch.cuda.reset_peak_memory_stats()

				snake_case  :       Optional[int]     			=						load_image(
				    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
				    '''/sd2-upscale/low_res_cat.png'''      )

				snake_case  :       List[Any]     			=						'''stabilityai/stable-diffusion-x4-upscaler'''
				snake_case  :       Union[str, Any]     			=						StableDiffusionUpscalePipeline.from_pretrained(
				    UpperCamelCase__  ,	torch_dtype=torch.floataa  ,	)
				pipe.to(UpperCamelCase__      )
				pipe.set_progress_bar_config(disable=UpperCamelCase__      )
				pipe.enable_attention_slicing(1      )
				pipe.enable_sequential_cpu_offload()

				snake_case  :       Any     			=						'''a cat sitting on a park bench'''

				snake_case  :       int     			=						torch.manual_seed(0      )
				snake_case  :       List[Any]     			=						pipe(
				    prompt=UpperCamelCase__  ,	image=UpperCamelCase__  ,	generator=UpperCamelCase__  ,	num_inference_steps=5  ,	output_type='''np'''  ,	)

				snake_case  :       str     			=						torch.cuda.max_memory_allocated()
				# make sure that less than 2.9 GB is allocated
				assert mem_bytes < 2.9 * 10**9




'''simple docstring'''
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


lowercase__						       =      logging.get_logger(__name__)

lowercase__						       =      {
    "microsoft/swin-tiny-patch4-window7-224": (
        "https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json"
    ),
    # See all Swin models at https://huggingface.co/models?filter=swin
}






class 	snake_case__       (      __SCREAMING_SNAKE_CASE    ,					__SCREAMING_SNAKE_CASE					):







		"""simple docstring"""

		lowerCamelCase     							=						"""swin"""

		lowerCamelCase     							=						{
		    """num_attention_heads""": """num_heads""",
		    """num_hidden_layers""": """num_layers""",
		}



		def __init__(      self				:			Tuple  ,	UpperCamelCase__				:			int=224  ,	UpperCamelCase__				:			Optional[int]=4  ,	UpperCamelCase__				:			str=3  ,	UpperCamelCase__				:			str=96  ,	UpperCamelCase__				:			List[Any]=[2, 2, 6, 2]  ,	UpperCamelCase__				:			Optional[Any]=[3, 6, 12, 24]  ,	UpperCamelCase__				:			Optional[Any]=7  ,	UpperCamelCase__				:			Tuple=4.0  ,	UpperCamelCase__				:			Tuple=True  ,	UpperCamelCase__				:			str=0.0  ,	UpperCamelCase__				:			Optional[Any]=0.0  ,	UpperCamelCase__				:			List[str]=0.1  ,	UpperCamelCase__				:			Optional[Any]="gelu"  ,	UpperCamelCase__				:			Optional[Any]=False  ,	UpperCamelCase__				:			Tuple=0.02  ,	UpperCamelCase__				:			List[Any]=1e-5  ,	UpperCamelCase__				:			Any=32  ,	UpperCamelCase__				:			List[str]=None  ,	UpperCamelCase__				:			int=None  ,	**UpperCamelCase__				:			Optional[Any]  ,	)						->			Optional[int]:






				"""simple docstring"""







				super().__init__(**UpperCamelCase__      )

				snake_case  :       Any     			=						image_size
				snake_case  :       Optional[int]     			=						patch_size
				snake_case  :       List[Any]     			=						num_channels
				snake_case  :       Union[str, Any]     			=						embed_dim
				snake_case  :       str     			=						depths
				snake_case  :       str     			=						len(UpperCamelCase__      )
				snake_case  :       List[Any]     			=						num_heads
				snake_case  :       List[Any]     			=						window_size
				snake_case  :       Optional[int]     			=						mlp_ratio
				snake_case  :       Union[str, Any]     			=						qkv_bias
				snake_case  :       Optional[int]     			=						hidden_dropout_prob
				snake_case  :       Optional[int]     			=						attention_probs_dropout_prob
				snake_case  :       Optional[int]     			=						drop_path_rate
				snake_case  :       List[Any]     			=						hidden_act
				snake_case  :       int     			=						use_absolute_embeddings
				snake_case  :       str     			=						layer_norm_eps
				snake_case  :       Optional[Any]     			=						initializer_range
				snake_case  :       Any     			=						encoder_stride
				# we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
				# this indicates the channel dimension after the last stage of the model
				snake_case  :       str     			=						int(embed_dim * 2 ** (len(UpperCamelCase__      ) - 1)      )
				snake_case  :       int     			=						['''stem'''] + [f'stage{idx}' for idx in range(1  ,	len(UpperCamelCase__      ) + 1      )]
				snake_case			,snake_case  :       List[Any]     			=						get_aligned_output_features_output_indices(
				    out_features=UpperCamelCase__  ,	out_indices=UpperCamelCase__  ,	stage_names=self.stage_names      )







class 	snake_case__       (      __SCREAMING_SNAKE_CASE					):







		"""simple docstring"""

		lowerCamelCase     							=						version.parse("""1.11"""					)



		@property
		def       lowerCAmelCase				(      self				:			List[Any]      )						->			Mapping[str, Mapping[int, str]]:






				"""simple docstring"""







				return OrderedDict(
				    [
				        ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
				    ]      )



		@property
		def       lowerCAmelCase				(      self				:			List[Any]      )						->			float:






				"""simple docstring"""







				return 1e-4


