





import warnings

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase_    =				logging.get_logger(__name__)

lowerCAmelCase_    =				{
    "RUCAIBox/mvp": "https://huggingface.co/RUCAIBox/mvp/resolve/main/config.json",
}



class 					__lowerCAmelCase       (			_a     ):
			lowerCamelCase_     :     Dict       			=      '''mvp'''
			lowerCamelCase_     :     Tuple       			=      ['''past_key_values''']
			lowerCamelCase_     :     Optional[Any]       			=      {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}






			def __init__(self					,							__magic_name__=5_0267					,							__magic_name__=1024					,							__magic_name__=12					,							__magic_name__=4096					,							__magic_name__=16					,							__magic_name__=12					,							__magic_name__=4096					,							__magic_name__=16					,							__magic_name__=0.0					,							__magic_name__=0.0					,							__magic_name__="gelu"					,							__magic_name__=1024					,							__magic_name__=0.1					,							__magic_name__=0.0					,							__magic_name__=0.0					,							__magic_name__=0.02					,							__magic_name__=0.0					,							__magic_name__=False					,							__magic_name__=True					,							__magic_name__=1					,							__magic_name__=0					,							__magic_name__=2					,							__magic_name__=True					,							__magic_name__=2					,							__magic_name__=2					,							__magic_name__=False					,							__magic_name__=100					,							__magic_name__=800					,							**__magic_name__					,							)     ->   str:


							'''simple docstring'''



							snake_case_						:    List[Any]				=  vocab_size
							snake_case_						:    Optional[Any]				=  max_position_embeddings
							snake_case_						:    List[Any]				=  d_model
							snake_case_						:    Tuple				=  encoder_ffn_dim
							snake_case_						:    Optional[Any]				=  encoder_layers
							snake_case_						:    Optional[int]				=  encoder_attention_heads
							snake_case_						:    Any				=  decoder_ffn_dim
							snake_case_						:    Tuple				=  decoder_layers
							snake_case_						:    str				=  decoder_attention_heads
							snake_case_						:    List[Any]				=  dropout
							snake_case_						:    Optional[Any]				=  attention_dropout
							snake_case_						:    Tuple				=  activation_dropout
							snake_case_						:    Optional[Any]				=  activation_function
							snake_case_						:    Any				=  init_std
							snake_case_						:    Any				=  encoder_layerdrop
							snake_case_						:    str				=  decoder_layerdrop
							snake_case_						:    Tuple				=  classifier_dropout
							snake_case_						:    Optional[int]				=  use_cache
							snake_case_						:    List[Any]				=  encoder_layers
							snake_case_						:    Dict				=  scale_embedding  # scale factor will be sqrt(d_model) if True
							snake_case_						:    Dict				=  use_prompt
							snake_case_						:    int				=  prompt_length
							snake_case_						:    Union[str, Any]				=  prompt_mid_dim

							super().__init__(
							    pad_token_id=A_					,							bos_token_id=A_					,							eos_token_id=A_					,							is_encoder_decoder=A_					,							decoder_start_token_id=A_					,							forced_eos_token_id=A_					,							**A_					,							)

							if self.forced_bos_token_id is None and kwargs.get('''force_bos_token_to_be_generated'''					,							A_    ):
											snake_case_						:    Optional[Any]				=  self.bos_token_id
											warnings.warn(
											    F'''Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. '''
											    '''The config can simply be saved and uploaded again to be fixed.'''    )






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   A(						unittest.TestCase   ):



 '''simple docstring'''


 def 				a__ (				self					:  Optional[Any]  )   ->	Optional[Any]:

       """simple docstring"""






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

 @property
 def 				a__ (				self					:  int  )   ->	int:

       """simple docstring"""






       lowerCamelCase_		     =    1
       lowerCamelCase_		     =    3
       lowerCamelCase_		     =    (32, 32)

       lowerCamelCase_		     =    floats_tensor((batch_size, num_channels) + sizes						,							rng=random.Random(0  )  ).to(A_  )
       return image

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

       """simple docstring"""






       torch.manual_seed(0  )
       lowerCamelCase_		     =    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=A_						,							only_cross_attention=(True, True, False)						,							num_class_embeds=100						,							)
       return model

 @property
 def 				a__ (				self					:  Optional[int]  )   ->	Optional[int]:

       """simple docstring"""






       torch.manual_seed(0  )
       lowerCamelCase_		     =    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 				a__ (				self					:  Union[str, Any]  )   ->	Union[str, Any]:

       """simple docstring"""






       torch.manual_seed(0  )
       lowerCamelCase_		     =    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=1000						,							hidden_act='gelu'						,							projection_dim=512						,							)
       return CLIPTextModel(A_  )

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

       """simple docstring"""






       lowerCamelCase_		     =    'cpu'  # ensure determinism for the device-dependent torch.Generator
       lowerCamelCase_		     =    self.dummy_cond_unet_upscale
       lowerCamelCase_		     =    DDPMScheduler()
       lowerCamelCase_		     =    DDIMScheduler(prediction_type='v_prediction'  )
       lowerCamelCase_		     =    self.dummy_vae
       lowerCamelCase_		     =    self.dummy_text_encoder
       lowerCamelCase_		     =    CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'  )

       lowerCamelCase_		     =    self.dummy_image.cpu().permute(0						,							2						,							3						,							1  )[0]
       lowerCamelCase_		     =    Image.fromarray(np.uinta(A_  )  ).convert('RGB'  ).resize((64, 64)  )

       # make sure here that pndm scheduler skips prk
       lowerCamelCase_		     =    StableDiffusionUpscalePipeline(
           unet=A_						,							low_res_scheduler=A_						,							scheduler=A_						,							vae=A_						,							text_encoder=A_						,							tokenizer=A_						,							max_noise_level=350						,							)
       lowerCamelCase_		     =    sd_pipe.to(A_  )
       sd_pipe.set_progress_bar_config(disable=A_  )

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

       lowerCamelCase_		     =    output.images

       lowerCamelCase_		     =    torch.Generator(device=A_  ).manual_seed(0  )
       lowerCamelCase_		     =    sd_pipe(
           [prompt]						,							image=A_						,							generator=A_						,							guidance_scale=6.0						,							noise_level=20						,							num_inference_steps=2						,							output_type='np'						,							return_dict=A_						,							)[0]

       lowerCamelCase_		     =    image[0, -3:, -3:, -1]
       lowerCamelCase_		     =    image_from_tuple[0, -3:, -3:, -1]

       lowerCamelCase_		     =    low_res_image.size[0] * 4
       assert image.shape == (1, expected_height_width, expected_height_width, 3)
       lowerCamelCase_		     =    np.array([0.3113, 0.3910, 0.4272, 0.4859, 0.5061, 0.4652, 0.5362, 0.5715, 0.5661]  )

       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 				a__ (				self					:  List[Any]  )   ->	int:

       """simple docstring"""






       lowerCamelCase_		     =    'cpu'  # ensure determinism for the device-dependent torch.Generator
       lowerCamelCase_		     =    self.dummy_cond_unet_upscale
       lowerCamelCase_		     =    DDPMScheduler()
       lowerCamelCase_		     =    DDIMScheduler(prediction_type='v_prediction'  )
       lowerCamelCase_		     =    self.dummy_vae
       lowerCamelCase_		     =    self.dummy_text_encoder
       lowerCamelCase_		     =    CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'  )

       lowerCamelCase_		     =    self.dummy_image.cpu().permute(0						,							2						,							3						,							1  )[0]
       lowerCamelCase_		     =    Image.fromarray(np.uinta(A_  )  ).convert('RGB'  ).resize((64, 64)  )

       # make sure here that pndm scheduler skips prk
       lowerCamelCase_		     =    StableDiffusionUpscalePipeline(
           unet=A_						,							low_res_scheduler=A_						,							scheduler=A_						,							vae=A_						,							text_encoder=A_						,							tokenizer=A_						,							max_noise_level=350						,							)
       lowerCamelCase_		     =    sd_pipe.to(A_  )
       sd_pipe.set_progress_bar_config(disable=A_  )

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

       lowerCamelCase_		     =    torch.Generator(device=A_  ).manual_seed(0  )
       lowerCamelCase_		     =    sd_pipe(
           [prompt]						,							image=A_						,							generator=A_						,							num_images_per_prompt=2						,							guidance_scale=6.0						,							noise_level=20						,							num_inference_steps=2						,							output_type='np'						,							)
       lowerCamelCase_		     =    output.images
       assert image.shape[0] == 2





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

       """simple docstring"""






       lowerCamelCase_		     =    self.dummy_cond_unet_upscale
       lowerCamelCase_		     =    DDPMScheduler()
       lowerCamelCase_		     =    DDIMScheduler(prediction_type='v_prediction'  )
       lowerCamelCase_		     =    self.dummy_vae
       lowerCamelCase_		     =    self.dummy_text_encoder
       lowerCamelCase_		     =    CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'  )

       lowerCamelCase_		     =    self.dummy_image.cpu().permute(0						,							2						,							3						,							1  )[0]
       lowerCamelCase_		     =    Image.fromarray(np.uinta(A_  )  ).convert('RGB'  ).resize((64, 64)  )

       # put models in fp16, except vae as it overflows in fp16
       lowerCamelCase_		     =    unet.half()
       lowerCamelCase_		     =    text_encoder.half()

       # make sure here that pndm scheduler skips prk
       lowerCamelCase_		     =    StableDiffusionUpscalePipeline(
           unet=A_						,							low_res_scheduler=A_						,							scheduler=A_						,							vae=A_						,							text_encoder=A_						,							tokenizer=A_						,							max_noise_level=350						,							)
       lowerCamelCase_		     =    sd_pipe.to(A_  )
       sd_pipe.set_progress_bar_config(disable=A_  )

       lowerCamelCase_		     =    'A painting of a squirrel eating a burger'
       lowerCamelCase_		     =    torch.manual_seed(0  )
       lowerCamelCase_		     =    sd_pipe(
           [prompt]						,							image=A_						,							generator=A_						,							num_inference_steps=2						,							output_type='np'						,							).images

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







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



 '''simple docstring'''


 def 				a__ (				self					:  Tuple  )   ->	str:

       """simple docstring"""






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

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

       """simple docstring"""






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

       lowerCamelCase_		     =    'stabilityai/stable-diffusion-x4-upscaler'
       lowerCamelCase_		     =    StableDiffusionUpscalePipeline.from_pretrained(A_  )
       pipe.to(A_  )
       pipe.set_progress_bar_config(disable=A_  )
       pipe.enable_attention_slicing()

       lowerCamelCase_		     =    'a cat sitting on a park bench'

       lowerCamelCase_		     =    torch.manual_seed(0  )
       lowerCamelCase_		     =    pipe(
           prompt=A_						,							image=A_						,							generator=A_						,							output_type='np'						,							)
       lowerCamelCase_		     =    output.images[0]

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

 def 				a__ (				self					:  Dict  )   ->	List[str]:

       """simple docstring"""






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

       lowerCamelCase_		     =    'stabilityai/stable-diffusion-x4-upscaler'
       lowerCamelCase_		     =    StableDiffusionUpscalePipeline.from_pretrained(
           A_						,							torch_dtype=torch.floataa						,							)
       pipe.to(A_  )
       pipe.set_progress_bar_config(disable=A_  )
       pipe.enable_attention_slicing()

       lowerCamelCase_		     =    'a cat sitting on a park bench'

       lowerCamelCase_		     =    torch.manual_seed(0  )
       lowerCamelCase_		     =    pipe(
           prompt=A_						,							image=A_						,							generator=A_						,							output_type='np'						,							)
       lowerCamelCase_		     =    output.images[0]

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





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

       """simple docstring"""






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

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

       lowerCamelCase_		     =    'stabilityai/stable-diffusion-x4-upscaler'
       lowerCamelCase_		     =    StableDiffusionUpscalePipeline.from_pretrained(
           A_						,							torch_dtype=torch.floataa						,							)
       pipe.to(A_  )
       pipe.set_progress_bar_config(disable=A_  )
       pipe.enable_attention_slicing(1  )
       pipe.enable_sequential_cpu_offload()

       lowerCamelCase_		     =    'a cat sitting on a park bench'

       lowerCamelCase_		     =    torch.manual_seed(0  )
       lowerCamelCase_		     =    pipe(
           prompt=A_						,							image=A_						,							generator=A_						,							num_inference_steps=5						,							output_type='np'						,							)

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



