





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







	'''simple docstring'''

	def   _UpperCAmelCase       (self	)							->							Any:







				'''simple docstring'''





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


	@property
	def   _UpperCAmelCase       (self	)							->							Any:







				'''simple docstring'''





				__lowercase			  =							1
				__lowercase			  =							3
				__lowercase			  =							(32, 32)

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


	@property
	def   _UpperCAmelCase       (self	)							->							str:







				'''simple docstring'''





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


	@property
	def   _UpperCAmelCase       (self	)							->							Tuple:







				'''simple docstring'''





				torch.manual_seed(0	)
				__lowercase			  =							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   _UpperCAmelCase       (self	)							->							Any:







				'''simple docstring'''





				torch.manual_seed(0	)
				__lowercase			  =							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(_lowerCamelCase	)


	def   _UpperCAmelCase       (self	)							->							str:







				'''simple docstring'''





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

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

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

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

				__lowercase			  =							output.images

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

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

				__lowercase			  =							low_res_image.size[0] * 4
				assert image.shape == (1, expected_height_width, expected_height_width, 3)
				__lowercase			  =							np.array([0.3_1_1_3, 0.3_9_1_0, 0.4_2_7_2, 0.4_8_5_9, 0.5_0_6_1, 0.4_6_5_2, 0.5_3_6_2, 0.5_7_1_5, 0.5_6_6_1]	)

				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	)							->							Union[str, Any]:







				'''simple docstring'''





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

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

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

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

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


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







				'''simple docstring'''





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

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

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

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

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

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






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







	'''simple docstring'''

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







				'''simple docstring'''





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


	def   _UpperCAmelCase       (self	)							->							List[str]:







				'''simple docstring'''





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

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

				__lowercase			  =							'''a cat sitting on a park bench'''

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

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


	def   _UpperCAmelCase       (self	)							->							int:







				'''simple docstring'''





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

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

				__lowercase			  =							'''a cat sitting on a park bench'''

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

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


	def   _UpperCAmelCase       (self	)							->							Dict:







				'''simple docstring'''





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

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

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

				__lowercase			  =							'''a cat sitting on a park bench'''

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

				__lowercase			  =							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 typing import Dict, List, Optional, Tuple, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_torch_available, is_torch_tensor, logging


if is_torch_available():
	import torch


_SCREAMING_SNAKE_CASE   =      logging.get_logger(__name__)






class 							__lowercase    ( lowerCAmelCase__  ):







	'''simple docstring'''

	a      :			int     =    ["pixel_values"]


	def __init__(self       ,_lowerCamelCase = True       ,_lowerCamelCase = None       ,_lowerCamelCase = PILImageResampling.BILINEAR       ,_lowerCamelCase = True       ,_lowerCamelCase = None       ,_lowerCamelCase = True       ,_lowerCamelCase = 1 / 255       ,_lowerCamelCase = True       ,_lowerCamelCase = None       ,_lowerCamelCase = None       ,**_lowerCamelCase       ,)							->							None:







				'''simple docstring'''





				super().__init__(**_lowerCamelCase	)
				__lowercase			  =							size if size is not None else {'''shortest_edge''': 256}
				__lowercase			  =							get_size_dict(_lowerCamelCase       ,default_to_square=_lowerCamelCase	)
				__lowercase			  =							crop_size if crop_size is not None else {'''height''': 224, '''width''': 224}
				__lowercase			  =							get_size_dict(_lowerCamelCase       ,param_name='''crop_size'''	)
				__lowercase			  =							do_resize
				__lowercase			  =							size
				__lowercase			  =							resample
				__lowercase			  =							do_center_crop
				__lowercase			  =							crop_size
				__lowercase			  =							do_rescale
				__lowercase			  =							rescale_factor
				__lowercase			  =							do_normalize
				__lowercase			  =							image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
				__lowercase			  =							image_std if image_std is not None else IMAGENET_STANDARD_STD


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase = PILImageResampling.BICUBIC       ,_lowerCamelCase = None       ,**_lowerCamelCase       ,)							->							np.ndarray:







				'''simple docstring'''





				__lowercase			  =							get_size_dict(_lowerCamelCase       ,default_to_square=_lowerCamelCase	)
				if "shortest_edge" not in size:
							raise ValueError(f"The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}"	)
				__lowercase			  =							get_resize_output_image_size(_lowerCamelCase       ,size=size['''shortest_edge''']       ,default_to_square=_lowerCamelCase	)
				return resize(_lowerCamelCase       ,size=_lowerCamelCase       ,resample=_lowerCamelCase       ,data_format=_lowerCamelCase       ,**_lowerCamelCase	)


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase = None       ,**_lowerCamelCase       ,)							->							np.ndarray:







				'''simple docstring'''





				__lowercase			  =							get_size_dict(_lowerCamelCase	)
				if "height" not in size or "width" not in size:
							raise ValueError(f"The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}"	)
				return center_crop(_lowerCamelCase       ,size=(size['''height'''], size['''width'''])       ,data_format=_lowerCamelCase       ,**_lowerCamelCase	)


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase = None       ,**_lowerCamelCase	)							->							np.ndarray:







				'''simple docstring'''





				return rescale(_lowerCamelCase       ,scale=_lowerCamelCase       ,data_format=_lowerCamelCase       ,**_lowerCamelCase	)


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase = None       ,**_lowerCamelCase       ,)							->							np.ndarray:







				'''simple docstring'''





				return normalize(_lowerCamelCase       ,mean=_lowerCamelCase       ,std=_lowerCamelCase       ,data_format=_lowerCamelCase       ,**_lowerCamelCase	)


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase = None       ,_lowerCamelCase = None       ,_lowerCamelCase = None       ,_lowerCamelCase = None       ,_lowerCamelCase = None       ,_lowerCamelCase = None       ,_lowerCamelCase = None       ,_lowerCamelCase = None       ,_lowerCamelCase = None       ,_lowerCamelCase = None       ,_lowerCamelCase = None       ,_lowerCamelCase = ChannelDimension.FIRST       ,**_lowerCamelCase       ,)							->							Any:







				'''simple docstring'''





				__lowercase			  =							do_resize if do_resize is not None else self.do_resize
				__lowercase			  =							size if size is not None else self.size
				__lowercase			  =							get_size_dict(_lowerCamelCase       ,default_to_square=_lowerCamelCase	)
				__lowercase			  =							resample if resample is not None else self.resample
				__lowercase			  =							do_center_crop if do_center_crop is not None else self.do_center_crop
				__lowercase			  =							crop_size if crop_size is not None else self.crop_size
				__lowercase			  =							get_size_dict(_lowerCamelCase       ,param_name='''crop_size'''	)
				__lowercase			  =							do_rescale if do_rescale is not None else self.do_rescale
				__lowercase			  =							rescale_factor if rescale_factor is not None else self.rescale_factor
				__lowercase			  =							do_normalize if do_normalize is not None else self.do_normalize
				__lowercase			  =							image_mean if image_mean is not None else self.image_mean
				__lowercase			  =							image_std if image_std is not None else self.image_std

				__lowercase			  =							make_list_of_images(_lowerCamelCase	)

				if not valid_images(_lowerCamelCase	):
							raise ValueError(
							    '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '''
							    '''torch.Tensor, tf.Tensor or jax.ndarray.'''	)

				if do_resize and size is None:
							raise ValueError('''Size must be specified if do_resize is True.'''	)

				if do_center_crop and crop_size is None:
							raise ValueError('''Crop size must be specified if do_center_crop is True.'''	)

				if do_rescale and rescale_factor is None:
							raise ValueError('''Rescale factor must be specified if do_rescale is True.'''	)

				if do_normalize and (image_mean is None or image_std is None):
							raise ValueError('''Image mean and std must be specified if do_normalize is True.'''	)

				# All transformations expect numpy arrays.
				__lowercase			  =							[to_numpy_array(_lowerCamelCase	) for image in images]

				if do_resize:
							__lowercase			  =							[self.resize(image=_lowerCamelCase       ,size=_lowerCamelCase       ,resample=_lowerCamelCase	) for image in images]

				if do_center_crop:
							__lowercase			  =							[self.center_crop(image=_lowerCamelCase       ,size=_lowerCamelCase	) for image in images]

				if do_rescale:
							__lowercase			  =							[self.rescale(image=_lowerCamelCase       ,scale=_lowerCamelCase	) for image in images]

				if do_normalize:
							__lowercase			  =							[self.normalize(image=_lowerCamelCase       ,mean=_lowerCamelCase       ,std=_lowerCamelCase	) for image in images]

				__lowercase			  =							[to_channel_dimension_format(_lowerCamelCase       ,_lowerCamelCase	) for image in images]

				__lowercase			  =							{'''pixel_values''': images}
				return BatchFeature(data=_lowerCamelCase       ,tensor_type=_lowerCamelCase	)


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase = None	)							->							str:







				'''simple docstring'''





				__lowercase			  =							outputs.logits

				# Resize logits and compute semantic segmentation maps
				if target_sizes is not None:
							if len(_lowerCamelCase	) != len(_lowerCamelCase	):
										raise ValueError(
										    '''Make sure that you pass in as many target sizes as the batch dimension of the logits'''	)

							if is_torch_tensor(_lowerCamelCase	):
										__lowercase			  =							target_sizes.numpy()

							__lowercase			  =							[]

							for idx in range(len(_lowerCamelCase	)	):
										__lowercase			  =							torch.nn.functional.interpolate(
										    logits[idx].unsqueeze(dim=0	)       ,size=target_sizes[idx]       ,mode='''bilinear'''       ,align_corners=_lowerCamelCase	)
										__lowercase			  =							resized_logits[0].argmax(dim=0	)
										semantic_segmentation.append(_lowerCamelCase	)
				else:
							__lowercase			  =							logits.argmax(dim=1	)
							__lowercase			  =							[semantic_segmentation[i] for i in range(semantic_segmentation.shape[0]	)]

				return semantic_segmentation





