



'''simple docstring'''

from dataclasses import dataclass
from typing import Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps
from .modeling_utils import ModelMixin
from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block

@dataclass
class __snake_case(      _lowerCAmelCase					):


						'''simple docstring'''



						UpperCAmelCase							:    torch.FloatTensor







class __snake_case(      _lowerCAmelCase		,							_lowerCAmelCase					):


						'''simple docstring'''



						@register_to_config
						def __init__(					self	,       A_ = None	,       A_ = 3	,       A_ = 3	,       A_ = False	,       A_ = "positional"	,       A_ = 0	,       A_ = True	,       A_ = ("DownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D")	,       A_ = ("AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "UpBlock2D")	,       A_ = (224, 448, 672, 896)	,       A_ = 2	,       A_ = 1	,       A_ = 1	,       A_ = "conv"	,       A_ = "conv"	,       A_ = "silu"	,       A_ = 8	,       A_ = 32	,       A_ = 1e-5	,       A_ = "default"	,       A_ = True	,       A_ = None	,       A_ = None	,       )	->	int:
							super().__init__()

							lowerCAmelCase    =	sample_size
							lowerCAmelCase    =	block_out_channels[0] * 4

							# Check inputs
							if len(A_      ) != len(A_      ):
								raise ValueError(
								    f'Must provide the same number of `down_block_types` as `up_block_types`. `down_block_types`: {down_block_types}. `up_block_types`: {up_block_types}.'      )

							if len(A_      ) != len(A_      ):
								raise ValueError(
								    f'Must provide the same number of `block_out_channels` as `down_block_types`. `block_out_channels`: {block_out_channels}. `down_block_types`: {down_block_types}.'      )

							# input
							lowerCAmelCase    =	nn.Convad(A_	,       block_out_channels[0]	,       kernel_size=3	,       padding=(1, 1)      )

							# time
							if time_embedding_type == "fourier":
								lowerCAmelCase    =	GaussianFourierProjection(embedding_size=block_out_channels[0]	,       scale=16      )
								lowerCAmelCase    =	2 * block_out_channels[0]
							elif time_embedding_type == "positional":
								lowerCAmelCase    =	Timesteps(block_out_channels[0]	,       A_	,       A_      )
								lowerCAmelCase    =	block_out_channels[0]

							lowerCAmelCase    =	TimestepEmbedding(A_	,       A_      )

							# class embedding
							if class_embed_type is None and num_class_embeds is not None:
								lowerCAmelCase    =	nn.Embedding(A_	,       A_      )
							elif class_embed_type == "timestep":
								lowerCAmelCase    =	TimestepEmbedding(A_	,       A_      )
							elif class_embed_type == "identity":
								lowerCAmelCase    =	nn.Identity(A_	,       A_      )
							else:
								lowerCAmelCase    =	None

							lowerCAmelCase    =	nn.ModuleList([]      )
							lowerCAmelCase    =	None
							lowerCAmelCase    =	nn.ModuleList([]      )

							# down
							lowerCAmelCase    =	block_out_channels[0]
							for i, down_block_type in enumerate(A_      ):
								lowerCAmelCase    =	output_channel
								lowerCAmelCase    =	block_out_channels[i]
								lowerCAmelCase    =	i == len(A_      ) - 1

								lowerCAmelCase    =	get_down_block(
								    A_	,       num_layers=A_	,       in_channels=A_	,       out_channels=A_	,       temb_channels=A_	,       add_downsample=not is_final_block	,       resnet_eps=A_	,       resnet_act_fn=A_	,       resnet_groups=A_	,       attention_head_dim=attention_head_dim if attention_head_dim is not None else output_channel	,       downsample_padding=A_	,       resnet_time_scale_shift=A_	,       downsample_type=A_	,       )
								self.down_blocks.append(A_      )

							# mid
							lowerCAmelCase    =	UNetMidBlockaD(
							    in_channels=block_out_channels[-1]	,       temb_channels=A_	,       resnet_eps=A_	,       resnet_act_fn=A_	,       output_scale_factor=A_	,       resnet_time_scale_shift=A_	,       attention_head_dim=attention_head_dim if attention_head_dim is not None else block_out_channels[-1]	,       resnet_groups=A_	,       add_attention=A_	,       )

							# up
							lowerCAmelCase    =	list(reversed(A_      )      )
							lowerCAmelCase    =	reversed_block_out_channels[0]
							for i, up_block_type in enumerate(A_      ):
								lowerCAmelCase    =	output_channel
								lowerCAmelCase    =	reversed_block_out_channels[i]
								lowerCAmelCase    =	reversed_block_out_channels[min(i + 1	,       len(A_      ) - 1      )]

								lowerCAmelCase    =	i == len(A_      ) - 1

								lowerCAmelCase    =	get_up_block(
								    A_	,       num_layers=layers_per_block + 1	,       in_channels=A_	,       out_channels=A_	,       prev_output_channel=A_	,       temb_channels=A_	,       add_upsample=not is_final_block	,       resnet_eps=A_	,       resnet_act_fn=A_	,       resnet_groups=A_	,       attention_head_dim=attention_head_dim if attention_head_dim is not None else output_channel	,       resnet_time_scale_shift=A_	,       upsample_type=A_	,       )
								self.up_blocks.append(A_      )
								lowerCAmelCase    =	output_channel

							# out
							lowerCAmelCase    =	norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4	,       32      )
							lowerCAmelCase    =	nn.GroupNorm(num_channels=block_out_channels[0]	,       num_groups=A_	,       eps=A_      )
							lowerCAmelCase    =	nn.SiLU()
							lowerCAmelCase    =	nn.Convad(block_out_channels[0]	,       A_	,       kernel_size=3	,       padding=1      )




						def 				__snake_case    (					self	,       A_	,       A_	,       A_ = None	,       A_ = True	,       )	->	Union[UNetaDOutput, Tuple]:
							# 0. center input if necessary
							if self.config.center_input_sample:
								lowerCAmelCase    =	2 * sample - 1.0

							# 1. time
							lowerCAmelCase    =	timestep
							if not torch.is_tensor(A_      ):
								lowerCAmelCase    =	torch.tensor([timesteps]	,       dtype=torch.long	,       device=sample.device      )
							elif torch.is_tensor(A_      ) and len(timesteps.shape      ) == 0:
								lowerCAmelCase    =	timesteps[None].to(sample.device      )

							# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
							lowerCAmelCase    =	timesteps * torch.ones(sample.shape[0]	,       dtype=timesteps.dtype	,       device=timesteps.device      )

							lowerCAmelCase    =	self.time_proj(A_      )

							# timesteps does not contain any weights and will always return f32 tensors
							# but time_embedding might actually be running in fp16. so we need to cast here.
							# there might be better ways to encapsulate this.
							lowerCAmelCase    =	t_emb.to(dtype=self.dtype      )
							lowerCAmelCase    =	self.time_embedding(A_      )

							if self.class_embedding is not None:
								if class_labels is None:
									raise ValueError("""class_labels should be provided when doing class conditioning"""      )

								if self.config.class_embed_type == "timestep":
									lowerCAmelCase    =	self.time_proj(A_      )

								lowerCAmelCase    =	self.class_embedding(A_      ).to(dtype=self.dtype      )
								lowerCAmelCase    =	emb + class_emb

							# 2. pre-process
							lowerCAmelCase    =	sample
							lowerCAmelCase    =	self.conv_in(A_      )

							# 3. down
							lowerCAmelCase    =	(sample,)
							for downsample_block in self.down_blocks:
								if hasattr(A_	,       """skip_conv"""      ):
									lowerCAmelCase,   lowerCAmelCase,   lowerCAmelCase    =	downsample_block(
									    hidden_states=A_	,       temb=A_	,       skip_sample=A_      )
								else:
									lowerCAmelCase,   lowerCAmelCase    =	downsample_block(hidden_states=A_	,       temb=A_      )

								down_block_res_samples += res_samples

							# 4. mid
							lowerCAmelCase    =	self.mid_block(A_	,       A_      )

							# 5. up
							lowerCAmelCase    =	None
							for upsample_block in self.up_blocks:
								lowerCAmelCase    =	down_block_res_samples[-len(upsample_block.resnets      ) :]
								lowerCAmelCase    =	down_block_res_samples[: -len(upsample_block.resnets      )]

								if hasattr(A_	,       """skip_conv"""      ):
									lowerCAmelCase,   lowerCAmelCase    =	upsample_block(A_	,       A_	,       A_	,       A_      )
								else:
									lowerCAmelCase    =	upsample_block(A_	,       A_	,       A_      )

        # 6. post-process
							lowerCAmelCase    =	self.conv_norm_out(A_      )
							lowerCAmelCase    =	self.conv_act(A_      )
							lowerCAmelCase    =	self.conv_out(A_      )

							if skip_sample is not None:
								sample += skip_sample

							if self.config.time_embedding_type == "fourier":
								lowerCAmelCase    =	timesteps.reshape((sample.shape[0], *([1] * len(sample.shape[1:]      )))      )
								lowerCAmelCase    =	sample / timesteps

							if not return_dict:
								return (sample,)

							return UNetaDOutput(sample=A_      )




'''simple docstring'''

import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionModelEditingPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()

@skip_mps
class __snake_case(      _lowerCAmelCase		,							_lowerCAmelCase		,							_lowerCAmelCase		,							unittest.TestCase					):


						'''simple docstring'''



						UpperCAmelCase							:    Optional[Any]           =		StableDiffusionModelEditingPipeline
						UpperCAmelCase							:    List[str]           =		TEXT_TO_IMAGE_PARAMS
						UpperCAmelCase							:    Optional[Any]           =		TEXT_TO_IMAGE_BATCH_PARAMS
						UpperCAmelCase							:    Optional[int]           =		TEXT_TO_IMAGE_IMAGE_PARAMS
						UpperCAmelCase							:    Optional[int]           =		TEXT_TO_IMAGE_IMAGE_PARAMS


						def 				__snake_case    (					self      )	->	Optional[Any]:
							torch.manual_seed(0      )
							lowerCAmelCase    =	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	,       )
							lowerCAmelCase    =	DDIMScheduler()
							torch.manual_seed(0      )
							lowerCAmelCase    =	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	,       )
							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	,       )
							lowerCAmelCase    =	CLIPTextModel(A_      )
							lowerCAmelCase    =	CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""      )

							lowerCAmelCase    =	{
							    """unet""": unet,
							    """scheduler""": scheduler,
							    """vae""": vae,
							    """text_encoder""": text_encoder,
							    """tokenizer""": tokenizer,
							    """safety_checker""": None,
							    """feature_extractor""": None,
							}
							return components


						def 				__snake_case    (					self	,       A_	,       A_=0      )	->	Optional[int]:
							lowerCAmelCase    =	torch.manual_seed(A_      )
							lowerCAmelCase    =	{
							    """prompt""": """A field of roses""",
							    """generator""": generator,
							    # Setting height and width to None to prevent OOMs on CPU.
							    """height""": None,
							    """width""": None,
							    """num_inference_steps""": 2,
							    """guidance_scale""": 6.0,
							    """output_type""": """numpy""",
							}
							return inputs


						def 				__snake_case    (					self      )	->	List[str]:
							lowerCAmelCase    =	"""cpu"""  # ensure determinism for the device-dependent torch.Generator
							lowerCAmelCase    =	self.get_dummy_components()
							lowerCAmelCase    =	StableDiffusionModelEditingPipeline(**A_      )
							lowerCAmelCase    =	sd_pipe.to(A_      )
							sd_pipe.set_progress_bar_config(disable=A_      )

							lowerCAmelCase    =	self.get_dummy_inputs(A_      )
							lowerCAmelCase    =	sd_pipe(**A_      ).images
							lowerCAmelCase    =	image[0, -3:, -3:, -1]
							assert image.shape == (1, 64, 64, 3)

							lowerCAmelCase    =	np.array([0.4_7_5_5, 0.5_1_3_2, 0.4_9_7_6, 0.3_9_0_4, 0.3_5_5_4, 0.4_7_6_5, 0.5_1_3_9, 0.5_1_5_8, 0.4_8_8_9]      )

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


						def 				__snake_case    (					self      )	->	str:
							lowerCAmelCase    =	"""cpu"""  # ensure determinism for the device-dependent torch.Generator
							lowerCAmelCase    =	self.get_dummy_components()
							lowerCAmelCase    =	StableDiffusionModelEditingPipeline(**A_      )
							lowerCAmelCase    =	sd_pipe.to(A_      )
							sd_pipe.set_progress_bar_config(disable=A_      )

							lowerCAmelCase    =	self.get_dummy_inputs(A_      )
							lowerCAmelCase    =	"""french fries"""
							lowerCAmelCase    =	sd_pipe(**A_	,       negative_prompt=A_      )
							lowerCAmelCase    =	output.images
							lowerCAmelCase    =	image[0, -3:, -3:, -1]

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

							lowerCAmelCase    =	np.array([0.4_9_9_2, 0.5_1_0_1, 0.5_0_0_4, 0.3_9_4_9, 0.3_6_0_4, 0.4_7_3_5, 0.5_2_1_6, 0.5_2_0_4, 0.4_9_1_3]      )

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


						def 				__snake_case    (					self      )	->	Optional[int]:
							lowerCAmelCase    =	"""cpu"""  # ensure determinism for the device-dependent torch.Generator
							lowerCAmelCase    =	self.get_dummy_components()
							lowerCAmelCase    =	EulerAncestralDiscreteScheduler(
							    beta_start=0.0_0_0_8_5	,       beta_end=0.0_1_2	,       beta_schedule="""scaled_linear"""      )
							lowerCAmelCase    =	StableDiffusionModelEditingPipeline(**A_      )
							lowerCAmelCase    =	sd_pipe.to(A_      )
							sd_pipe.set_progress_bar_config(disable=A_      )

							lowerCAmelCase    =	self.get_dummy_inputs(A_      )
							lowerCAmelCase    =	sd_pipe(**A_      ).images
							lowerCAmelCase    =	image[0, -3:, -3:, -1]

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

							lowerCAmelCase    =	np.array([0.4_7_4_7, 0.5_3_7_2, 0.4_7_7_9, 0.4_9_8_2, 0.5_5_4_3, 0.4_8_1_6, 0.5_2_3_8, 0.4_9_0_4, 0.5_0_2_7]      )

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


						def 				__snake_case    (					self      )	->	Optional[Any]:
							lowerCAmelCase    =	"""cpu"""  # ensure determinism for the device-dependent torch.Generator
							lowerCAmelCase    =	self.get_dummy_components()
							lowerCAmelCase    =	PNDMScheduler()
							lowerCAmelCase    =	StableDiffusionModelEditingPipeline(**A_      )
							lowerCAmelCase    =	sd_pipe.to(A_      )
							sd_pipe.set_progress_bar_config(disable=A_      )

							lowerCAmelCase    =	self.get_dummy_inputs(A_      )
							# the pipeline does not expect pndm so test if it raises error.
							with self.assertRaises(A_      ):
								lowerCAmelCase    =	sd_pipe(**A_      ).images


						def 				__snake_case    (					self      )	->	str:
							super().test_inference_batch_single_identical(expected_max_diff=5e-3      )




						def 				__snake_case    (					self      )	->	Any:
							super().test_attention_slicing_forward_pass(expected_max_diff=5e-3      )







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


						'''simple docstring'''



						def 				__snake_case    (					self      )	->	int:
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()


						def 				__snake_case    (					self	,       A_=0      )	->	Dict:
							lowerCAmelCase    =	torch.manual_seed(A_      )
							lowerCAmelCase    =	{
							    """prompt""": """A field of roses""",
							    """generator""": generator,
							    """num_inference_steps""": 3,
							    """guidance_scale""": 7.5,
							    """output_type""": """numpy""",
							}
							return inputs


						def 				__snake_case    (					self      )	->	int:
							lowerCAmelCase    =	"""CompVis/stable-diffusion-v1-4"""
							lowerCAmelCase    =	StableDiffusionModelEditingPipeline.from_pretrained(A_	,       safety_checker=A_      )
							pipe.to(A_      )
							pipe.set_progress_bar_config(disable=A_      )
							pipe.enable_attention_slicing()

							lowerCAmelCase    =	self.get_inputs()
							lowerCAmelCase    =	pipe(**A_      ).images
							lowerCAmelCase    =	image[0, -3:, -3:, -1].flatten()

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

							lowerCAmelCase    =	np.array(
							    [0.6_7_4_9_4_9_6, 0.6_3_8_6_4_5_3, 0.5_1_4_4_3_2_6_7, 0.6_6_0_9_4_9_0_5, 0.6_1_9_2_1_2_1_5, 0.5_4_9_1_3_3_2, 0.5_7_4_4_4_1_7, 0.5_8_0_7_5_1_0_6, 0.5_1_7_4_6_5_8]      )

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

							# make sure image changes after editing
							pipe.edit_model("""A pack of roses"""	,       """A pack of blue roses"""      )

							lowerCAmelCase    =	pipe(**A_      ).images
							lowerCAmelCase    =	image[0, -3:, -3:, -1].flatten()

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

							assert np.abs(expected_slice - image_slice      ).max() > 1e-1




						def 				__snake_case    (					self      )	->	str:
							torch.cuda.empty_cache()
							torch.cuda.reset_max_memory_allocated()
							torch.cuda.reset_peak_memory_stats()

							lowerCAmelCase    =	"""CompVis/stable-diffusion-v1-4"""
							lowerCAmelCase    =	DDIMScheduler.from_pretrained(A_	,       subfolder="""scheduler"""      )
							lowerCAmelCase    =	StableDiffusionModelEditingPipeline.from_pretrained(
							    A_	,       scheduler=A_	,       safety_checker=A_      )
							lowerCAmelCase    =	pipe.to(A_      )
							pipe.set_progress_bar_config(disable=A_      )
							pipe.enable_attention_slicing(1      )
							pipe.enable_sequential_cpu_offload()

							lowerCAmelCase    =	self.get_inputs()
							lowerCAmelCase    =	pipe(**A_      )

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