

import unittest

from diffusers.models.unet_ad_blocks import *  # noqa F403
from diffusers.utils import torch_device

from .test_unet_blocks_common import UNetBlockTesterMixin


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:Tuple     =						DownBlockaD  # noqa F405
						_UpperCamelCase:Dict     =						"""down"""







						def     _snake_case							(       self				)->				Union[str, Any]:
													lowerCamelCase_				     =[-0.0_2_3_2, -0.9_8_6_9, 0.8_0_5_4, -0.0_6_3_7, -0.1_6_8_8, -1.4_2_6_4, 0.4_4_7_0, -1.3_3_9_4, 0.0_9_0_4]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:int     =						ResnetDownsampleBlockaD  # noqa F405
						_UpperCamelCase:str     =						"""down"""







						def     _snake_case							(       self				)->				List[str]:
													lowerCamelCase_				     =[0.0_7_1_0, 0.2_4_1_0, -0.7_3_2_0, -1.0_7_5_7, -1.1_3_4_3, 0.3_5_4_0, -0.0_1_3_3, -0.2_5_7_6, 0.0_9_4_8]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:Optional[int]     =						AttnDownBlockaD  # noqa F405
						_UpperCamelCase:Optional[int]     =						"""down"""







						def     _snake_case							(       self				)->				List[str]:
													lowerCamelCase_				     =[0.0_6_3_6, 0.8_9_6_4, -0.6_2_3_4, -1.0_1_3_1, 0.0_8_4_4, 0.4_9_3_5, 0.3_4_3_7, 0.0_9_1_1, -0.2_9_5_7]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:Dict     =						CrossAttnDownBlockaD  # noqa F405
						_UpperCamelCase:Any     =						"""down"""







						def     _snake_case							(       self				)->				Any:
													lowerCamelCase_				     =super().prepare_init_args_and_inputs_for_common()
													lowerCamelCase_				     =32
													return init_dict, inputs_dict







						def     _snake_case							(       self				)->				List[str]:
													lowerCamelCase_				     =[0.2_2_3_8, -0.7_3_9_6, -0.2_2_5_5, -0.3_8_2_9, 0.1_9_2_5, 1.1_6_6_5, 0.0_6_0_3, -0.7_2_9_5, 0.1_9_8_3]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:Optional[int]     =						SimpleCrossAttnDownBlockaD  # noqa F405
						_UpperCamelCase:int     =						"""down"""







						@property
						def     _snake_case							(       self				)->				List[str]:
													return super().get_dummy_input(include_encoder_hidden_states=lowerCAmelCase__				)







						def     _snake_case							(       self				)->				Any:
													lowerCamelCase_				     =super().prepare_init_args_and_inputs_for_common()
													lowerCamelCase_				     =32
													return init_dict, inputs_dict







						@unittest.skipIf(torch_device == """mps"""			,		"""MPS result is not consistent"""				)
						def     _snake_case							(       self				)->				Dict:
													lowerCamelCase_				     =[0.7_9_2_1, -0.0_9_9_2, -0.1_9_6_2, -0.7_6_9_5, -0.4_2_4_2, 0.7_8_0_4, 0.4_7_3_7, 0.2_7_6_5, 0.3_3_3_8]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:int     =						SkipDownBlockaD  # noqa F405
						_UpperCamelCase:Optional[int]     =						"""down"""







						@property
						def     _snake_case							(       self				)->				Any:
													return super().get_dummy_input(include_skip_sample=lowerCAmelCase__				)







						def     _snake_case							(       self				)->				Dict:
													lowerCamelCase_				     =[-0.0_8_4_5, -0.2_0_8_7, -0.2_4_6_5, 0.0_9_7_1, 0.1_9_0_0, -0.0_4_8_4, 0.2_6_6_4, 0.4_1_7_9, 0.5_0_6_9]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:List[Any]     =						AttnSkipDownBlockaD  # noqa F405
						_UpperCamelCase:Tuple     =						"""down"""







						@property
						def     _snake_case							(       self				)->				Union[str, Any]:
													return super().get_dummy_input(include_skip_sample=lowerCAmelCase__				)







						def     _snake_case							(       self				)->				Optional[Any]:
													lowerCamelCase_				     =[0.5_5_3_9, 0.1_6_0_9, 0.4_9_2_4, 0.0_5_3_7, -0.1_9_9_5, 0.4_0_5_0, 0.0_9_7_9, -0.2_7_2_1, -0.0_6_4_2]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:List[str]     =						DownEncoderBlockaD  # noqa F405
						_UpperCamelCase:Optional[Any]     =						"""down"""







						@property
						def     _snake_case							(       self				)->				int:
													return super().get_dummy_input(include_temb=lowerCAmelCase__				)







						def     _snake_case							(       self				)->				Union[str, Any]:
													lowerCamelCase_				     ={
													    "in_channels": 32,
													    "out_channels": 32,
													}
													lowerCamelCase_				     =self.dummy_input
													return init_dict, inputs_dict







						def     _snake_case							(       self				)->				List[str]:
													lowerCamelCase_				     =[1.1_1_0_2, 0.5_3_0_2, 0.4_8_7_2, -0.0_0_2_3, -0.8_0_4_2, 0.0_4_8_3, -0.3_4_8_9, -0.5_6_3_2, 0.7_6_2_6]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:List[Any]     =						AttnDownEncoderBlockaD  # noqa F405
						_UpperCamelCase:Optional[Any]     =						"""down"""







						@property
						def     _snake_case							(       self				)->				Any:
													return super().get_dummy_input(include_temb=lowerCAmelCase__				)







						def     _snake_case							(       self				)->				Optional[int]:
													lowerCamelCase_				     ={
													    "in_channels": 32,
													    "out_channels": 32,
													}
													lowerCamelCase_				     =self.dummy_input
													return init_dict, inputs_dict







						def     _snake_case							(       self				)->				str:
													lowerCamelCase_				     =[0.8_9_6_6, -0.1_4_8_6, 0.8_5_6_8, 0.8_1_4_1, -0.9_0_4_6, -0.1_3_4_2, -0.0_9_7_2, -0.7_4_1_7, 0.1_5_3_8]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:Dict     =						UNetMidBlockaD  # noqa F405
						_UpperCamelCase:Tuple     =						"""mid"""







						def     _snake_case							(       self				)->				str:
													lowerCamelCase_				     ={
													    "in_channels": 32,
													    "temb_channels": 128,
													}
													lowerCamelCase_				     =self.dummy_input
													return init_dict, inputs_dict







						def     _snake_case							(       self				)->				int:
													lowerCamelCase_				     =[-0.1_0_6_2, 1.7_2_4_8, 0.3_4_9_4, 1.4_5_6_9, -0.0_9_1_0, -1.2_4_2_1, -0.9_9_8_4, 0.6_7_3_6, 1.0_0_2_8]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:Optional[int]     =						UNetMidBlockaDCrossAttn  # noqa F405
						_UpperCamelCase:Optional[int]     =						"""mid"""







						def     _snake_case							(       self				)->				Union[str, Any]:
													lowerCamelCase_				     =super().prepare_init_args_and_inputs_for_common()
													lowerCamelCase_				     =32
													return init_dict, inputs_dict







						def     _snake_case							(       self				)->				Optional[Any]:
													lowerCamelCase_				     =[0.0_1_8_7, 2.4_2_2_0, 0.4_4_8_4, 1.1_2_0_3, -0.6_1_2_1, -1.5_1_2_2, -0.8_2_7_0, 0.7_8_5_1, 1.8_3_3_5]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:Optional[int]     =						UNetMidBlockaDSimpleCrossAttn  # noqa F405
						_UpperCamelCase:str     =						"""mid"""







						@property
						def     _snake_case							(       self				)->				int:
													return super().get_dummy_input(include_encoder_hidden_states=lowerCAmelCase__				)







						def     _snake_case							(       self				)->				Dict:
													lowerCamelCase_				     =super().prepare_init_args_and_inputs_for_common()
													lowerCamelCase_				     =32
													return init_dict, inputs_dict







						def     _snake_case							(       self				)->				Union[str, Any]:
													lowerCamelCase_				     =[0.7_1_4_3, 1.9_9_7_4, 0.5_4_4_8, 1.3_9_7_7, 0.1_2_8_2, -1.1_2_3_7, -1.4_2_3_8, 0.5_5_3_0, 0.8_8_8_0]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:Tuple     =						UpBlockaD  # noqa F405
						_UpperCamelCase:int     =						"""up"""







						@property
						def     _snake_case							(       self				)->				List[str]:
													return super().get_dummy_input(include_res_hidden_states_tuple=lowerCAmelCase__				)







						def     _snake_case							(       self				)->				str:
													lowerCamelCase_				     =[-0.2_0_4_1, -0.4_1_6_5, -0.3_0_2_2, 0.0_0_4_1, -0.6_6_2_8, -0.7_0_5_3, 0.1_9_2_8, -0.0_3_2_5, 0.0_5_2_3]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:Dict     =						ResnetUpsampleBlockaD  # noqa F405
						_UpperCamelCase:Any     =						"""up"""







						@property
						def     _snake_case							(       self				)->				Any:
													return super().get_dummy_input(include_res_hidden_states_tuple=lowerCAmelCase__				)







						def     _snake_case							(       self				)->				Dict:
													lowerCamelCase_				     =[0.2_2_8_7, 0.3_5_4_9, -0.1_3_4_6, 0.4_7_9_7, -0.1_7_1_5, -0.9_6_4_9, 0.7_3_0_5, -0.5_8_6_4, -0.6_2_4_4]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:int     =						CrossAttnUpBlockaD  # noqa F405
						_UpperCamelCase:Optional[int]     =						"""up"""







						@property
						def     _snake_case							(       self				)->				Any:
													return super().get_dummy_input(include_res_hidden_states_tuple=lowerCAmelCase__				)







						def     _snake_case							(       self				)->				List[Any]:
													lowerCamelCase_				     =super().prepare_init_args_and_inputs_for_common()
													lowerCamelCase_				     =32
													return init_dict, inputs_dict







						def     _snake_case							(       self				)->				int:
													lowerCamelCase_				     =[-0.1_4_0_3, -0.3_5_1_5, -0.0_4_2_0, -0.1_4_2_5, 0.3_1_6_7, 0.5_0_9_4, -0.2_1_8_1, 0.5_9_3_1, 0.5_5_8_2]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:Any     =						SimpleCrossAttnUpBlockaD  # noqa F405
						_UpperCamelCase:Any     =						"""up"""







						@property
						def     _snake_case							(       self				)->				str:
													return super().get_dummy_input(include_res_hidden_states_tuple=lowerCAmelCase__			,		include_encoder_hidden_states=lowerCAmelCase__				)







						def     _snake_case							(       self				)->				Dict:
													lowerCamelCase_				     =super().prepare_init_args_and_inputs_for_common()
													lowerCamelCase_				     =32
													return init_dict, inputs_dict







						def     _snake_case							(       self				)->				Optional[Any]:
													lowerCamelCase_				     =[0.2_6_4_5, 0.1_4_8_0, 0.0_9_0_9, 0.8_0_4_4, -0.9_7_5_8, -0.9_0_8_3, 0.0_9_9_4, -1.1_4_5_3, -0.7_4_0_2]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:Any     =						AttnUpBlockaD  # noqa F405
						_UpperCamelCase:int     =						"""up"""







						@property
						def     _snake_case							(       self				)->				Dict:
													return super().get_dummy_input(include_res_hidden_states_tuple=lowerCAmelCase__				)







						@unittest.skipIf(torch_device == """mps"""			,		"""MPS result is not consistent"""				)
						def     _snake_case							(       self				)->				Optional[Any]:
													lowerCamelCase_				     =[0.0_9_7_9, 0.1_3_2_6, 0.0_0_2_1, 0.0_6_5_9, 0.2_2_4_9, 0.0_0_5_9, 0.1_1_3_2, 0.5_9_5_2, 0.1_0_3_3]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:Union[str, Any]     =						SkipUpBlockaD  # noqa F405
						_UpperCamelCase:int     =						"""up"""







						@property
						def     _snake_case							(       self				)->				List[Any]:
													return super().get_dummy_input(include_res_hidden_states_tuple=lowerCAmelCase__				)







						def     _snake_case							(       self				)->				Optional[Any]:
													lowerCamelCase_				     =[-0.0_8_9_3, -0.1_2_3_4, -0.1_5_0_6, -0.0_3_3_2, 0.0_1_2_3, -0.0_2_1_1, 0.0_5_6_6, 0.0_1_4_3, 0.0_3_6_2]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:List[Any]     =						AttnSkipUpBlockaD  # noqa F405
						_UpperCamelCase:Dict     =						"""up"""







						@property
						def     _snake_case							(       self				)->				str:
													return super().get_dummy_input(include_res_hidden_states_tuple=lowerCAmelCase__				)







						def     _snake_case							(       self				)->				Dict:
													lowerCamelCase_				     =[0.0_3_6_1, 0.0_6_1_7, 0.2_7_8_7, -0.0_3_5_0, 0.0_3_4_2, 0.3_4_2_1, -0.0_8_4_3, 0.0_9_1_3, 0.3_0_1_5]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:Any     =						UpDecoderBlockaD  # noqa F405
						_UpperCamelCase:Optional[int]     =						"""up"""







						@property
						def     _snake_case							(       self				)->				List[str]:
													return super().get_dummy_input(include_temb=lowerCAmelCase__				)







						def     _snake_case							(       self				)->				List[str]:
													lowerCamelCase_				     ={"in_channels": 32, "out_channels": 32}

													lowerCamelCase_				     =self.dummy_input
													return init_dict, inputs_dict







						def     _snake_case							(       self				)->				Union[str, Any]:
													lowerCamelCase_				     =[0.4_4_0_4, 0.1_9_9_8, -0.9_8_8_6, -0.3_3_2_0, -0.3_1_2_8, -0.7_0_3_4, -0.6_9_5_5, -0.2_3_3_8, -0.3_1_3_7]
													super().test_output(lowerCAmelCase__				)


class        _SCREAMING_SNAKE_CASE	(   a__      ,  unittest.TestCase):
						_UpperCamelCase:List[str]     =						AttnUpDecoderBlockaD  # noqa F405
						_UpperCamelCase:List[Any]     =						"""up"""







						@property
						def     _snake_case							(       self				)->				Optional[int]:
													return super().get_dummy_input(include_temb=lowerCAmelCase__				)







						def     _snake_case							(       self				)->				Tuple:
													lowerCamelCase_				     ={"in_channels": 32, "out_channels": 32}

													lowerCamelCase_				     =self.dummy_input
													return init_dict, inputs_dict







						def     _snake_case							(       self				)->				Optional[Any]:
													lowerCamelCase_				     =[0.6_7_3_8, 0.4_4_9_1, 0.1_0_5_5, 1.0_7_1_0, 0.7_3_1_6, 0.3_3_3_9, 0.3_3_5_2, 0.1_0_2_3, 0.3_5_6_8]
													super().test_output(lowerCAmelCase__				)







"""simple docstring"""




import inspect
from typing import List, Optional, Tuple, Union

import numpy as np
import PIL
import torch
import torch.utils.checkpoint

from ...models import UNetaDModel, VQModel
from ...schedulers import (
    DDIMScheduler,
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
)
from ...utils import PIL_INTERPOLATION, randn_tensor
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput






def       _SCREAMING_SNAKE_CASE		(	_lowercase						:    Union[str, Any]					) ->str:






	'''simple docstring'''






	a,       a      :		str	=				image.size
	a,       a      :		Tuple	=				(x - x % 32 for x in (w, h))  # resize to integer multiple of 32
	a      :		Union[str, Any]	=				image.resize((w, h)    ,       resample=PIL_INTERPOLATION["lanczos"]					)
	a      :		int	=				np.array(_lowercase					).astype(np.floataa					) / 255.0
	a      :		List[str]	=				image[None].transpose(0    ,       3    ,       1    ,       2					)
	a      :		Dict	=				torch.from_numpy(_lowercase					)
	return 2.0 * image - 1.0







class        __UpperCamelCase				(						a__						):






							def __init__(   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					)	-> str:
								super().__init__()
								self.register_modules(vqvae=lowerCAmelCase__      ,					unet=lowerCAmelCase__      ,					scheduler=lowerCAmelCase__      )






							@torch.no_grad()
							def __call__(   self      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = 1      ,					lowerCAmelCase__ = 100      ,					lowerCAmelCase__ = 0.0      ,					lowerCAmelCase__ = None      ,					lowerCAmelCase__ = "pil"      ,					lowerCAmelCase__ = True      ,					)	-> Union[Tuple, ImagePipelineOutput]:
								if isinstance(lowerCAmelCase__      ,					PIL.Image.Image      ):
									a      :		int	=				1
								elif isinstance(lowerCAmelCase__      ,					torch.Tensor      ):
									a      :		str	=				image.shape[0]
								else:
									raise ValueError(f"""`image` has to be of type `PIL.Image.Image` or `torch.Tensor` but is {type(lowerCAmelCase__      )}"""      )

								if isinstance(lowerCAmelCase__      ,					PIL.Image.Image      ):
									a      :		Tuple	=				preprocess(lowerCAmelCase__      )

								a,       a      :		Optional[Any]	=				image.shape[-2:]

								# in_channels should be 6: 3 for latents, 3 for low resolution image
								a      :		Tuple	=				(batch_size, self.unet.config.in_channels // 2, height, width)
								a      :		List[str]	=				next(self.unet.parameters()      ).dtype

								a      :		Any	=				randn_tensor(lowerCAmelCase__      ,					generator=lowerCAmelCase__      ,					device=self.device      ,					dtype=lowerCAmelCase__      )

								a      :		Union[str, Any]	=				image.to(device=self.device      ,					dtype=lowerCAmelCase__      )

								# set timesteps and move to the correct device
								self.scheduler.set_timesteps(lowerCAmelCase__      ,					device=self.device      )
								a      :		int	=				self.scheduler.timesteps

								# scale the initial noise by the standard deviation required by the scheduler
								a      :		str	=				latents * self.scheduler.init_noise_sigma

								# prepare extra kwargs for the scheduler step, since not all schedulers have the same signature.
								# eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
								# eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
								# and should be between [0, 1]
								a      :		str	=				"eta" in set(inspect.signature(self.scheduler.step      ).parameters.keys()      )
								a      :		List[Any]	=				{}
								if accepts_eta:
									a      :		Any	=				eta

								for t in self.progress_bar(lowerCAmelCase__      ):
									# concat latents and low resolution image in the channel dimension.
									a      :		str	=				torch.cat([latents, image]      ,					dim=1      )
									a      :		int	=				self.scheduler.scale_model_input(lowerCAmelCase__      ,					lowerCAmelCase__      )
									# predict the noise residual
									a      :		Union[str, Any]	=				self.unet(lowerCAmelCase__      ,					lowerCAmelCase__      ).sample
									# compute the previous noisy sample x_t -> x_t-1
									a      :		Tuple	=				self.scheduler.step(lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					**lowerCAmelCase__      ).prev_sample

								# decode the image latents with the VQVAE
								a      :		List[Any]	=				self.vqvae.decode(lowerCAmelCase__      ).sample
								a      :		int	=				torch.clamp(lowerCAmelCase__      ,					-1.0      ,					1.0      )
								a      :		Optional[int]	=				image / 2 + 0.5
								a      :		Optional[int]	=				image.cpu().permute(0      ,					2      ,					3      ,					1      ).numpy()

								if output_type == "pil":
									a      :		Union[str, Any]	=				self.numpy_to_pil(lowerCAmelCase__      )

								if not return_dict:
									return (image,)

								return ImagePipelineOutput(images=lowerCAmelCase__      )


