



import gc
import random
import unittest

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

import diffusers
from diffusers import (
    AutoencoderKL,
    EulerDiscreteScheduler,
    StableDiffusionLatentUpscalePipeline,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.schedulers import KarrasDiffusionSchedulers
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

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()




def 	__UpperCamelCase	(  _lowerCAmelCase		)       ->  Optional[int]:



    """simple docstring"""






    A :       Any          =   [tensor.shape for tensor in tensor_list]
    return all(shape == shapes[0] for shape in shapes[1:]		)






class SCREAMING_SNAKE_CASE__     (		SCREAMING_SNAKE_CASE__ ,						SCREAMING_SNAKE_CASE__ ,						SCREAMING_SNAKE_CASE__ ,						unittest.TestCase    ):




    '''simple docstring'''






    __lowerCamelCase			: str					=      StableDiffusionLatentUpscalePipeline
    __lowerCamelCase			: Dict					=      TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {
        "height",
        "width",
        "cross_attention_kwargs",
        "negative_prompt_embeds",
        "prompt_embeds",
    }
    __lowerCamelCase			: int					=      PipelineTesterMixin.required_optional_params - {"num_images_per_prompt"}
    __lowerCamelCase			: Optional[int]					=      TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
    __lowerCamelCase			: Optional[Any]					=      frozenset(
        []    )  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
    __lowerCamelCase			: List[Any]					=      frozenset([]    )

    __lowerCamelCase			: Tuple					=      True






    @property
    def 			_lowerCAmelCase					(       self		):
        A :       str          =   1
        A :       Dict          =   4
        A :       Union[str, Any]          =   (16, 16)

        A :       List[str]          =   floats_tensor((batch_size, num_channels) + sizes,		rng=random.Random(0		)		).to(lowerCamelCase__		)
        return image






    def 			_lowerCAmelCase					(       self		):
        torch.manual_seed(0		)
        A :       List[Any]          =   UNetaDConditionModel(
            act_fn="""gelu""",		attention_head_dim=8,		norm_num_groups=lowerCamelCase__,		block_out_channels=[32, 32, 64, 64],		time_cond_proj_dim=160,		conv_in_kernel=1,		conv_out_kernel=1,		cross_attention_dim=32,		down_block_types=(
                """KDownBlock2D""",
                """KCrossAttnDownBlock2D""",
                """KCrossAttnDownBlock2D""",
                """KCrossAttnDownBlock2D""",
            ),		in_channels=8,		mid_block_type=lowerCamelCase__,		only_cross_attention=lowerCamelCase__,		out_channels=5,		resnet_time_scale_shift="""scale_shift""",		time_embedding_type="""fourier""",		timestep_post_act="""gelu""",		up_block_types=("""KCrossAttnUpBlock2D""", """KCrossAttnUpBlock2D""", """KCrossAttnUpBlock2D""", """KUpBlock2D"""),		)
        A :       Union[str, Any]          =   AutoencoderKL(
            block_out_channels=[32, 32, 64, 64],		in_channels=3,		out_channels=3,		down_block_types=[
                """DownEncoderBlock2D""",
                """DownEncoderBlock2D""",
                """DownEncoderBlock2D""",
                """DownEncoderBlock2D""",
            ],		up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D""", """UpDecoderBlock2D""", """UpDecoderBlock2D"""],		latent_channels=4,		)
        A :       List[str]          =   EulerDiscreteScheduler(prediction_type="""sample"""		)
        A :       Union[str, Any]          =   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="""quick_gelu""",		projection_dim=512,		)
        A :       int          =   CLIPTextModel(lowerCamelCase__		)
        A :       List[Any]          =   CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""		)

        A :       Dict          =   {
            """unet""": model.eval(),
            """vae""": vae.eval(),
            """scheduler""": scheduler,
            """text_encoder""": text_encoder,
            """tokenizer""": tokenizer,
        }

        return components






    def 			_lowerCAmelCase					(       self,		lowerCamelCase__,		lowerCamelCase__=0		):
        if str(lowerCamelCase__		).startswith("""mps"""		):
            A :       Union[str, Any]          =   torch.manual_seed(lowerCamelCase__		)
        else:
            A :       Dict          =   torch.Generator(device=lowerCamelCase__		).manual_seed(lowerCamelCase__		)
        A :       List[Any]          =   {
            """prompt""": """A painting of a squirrel eating a burger""",
            """image""": self.dummy_image.cpu(),
            """generator""": generator,
            """num_inference_steps""": 2,
            """output_type""": """numpy""",
        }
        return inputs






    def 			_lowerCAmelCase					(       self		):
        A :       Tuple          =   """cpu"""

        A :       Any          =   self.get_dummy_components()
        A :       str          =   self.pipeline_class(**lowerCamelCase__		)
        pipe.to(lowerCamelCase__		)
        pipe.set_progress_bar_config(disable=lowerCamelCase__		)

        A :       List[Any]          =   self.get_dummy_inputs(lowerCamelCase__		)
        A :       Any          =   pipe(**lowerCamelCase__		).images
        A :       List[str]          =   image[0, -3:, -3:, -1]

        self.assertEqual(image.shape,		(1, 256, 256, 3)		)
        A :       Dict          =   np.array(
            [0.4722_2412, 0.4192_1633, 0.4471_7434, 0.4687_4192, 0.4258_8258, 0.4615_0726, 0.467_7534, 0.4558_3832, 0.4857_9055]		)
        A :       List[str]          =   np.abs(image_slice.flatten() - expected_slice		).max()
        self.assertLessEqual(lowerCamelCase__,		1e-3		)






    def 			_lowerCAmelCase					(       self		):
        super().test_attention_slicing_forward_pass(expected_max_diff=7e-3		)






    def 			_lowerCAmelCase					(       self		):
        super().test_cpu_offload_forward_pass(expected_max_diff=3e-3		)






    def 			_lowerCAmelCase					(       self		):
        super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3		)






    def 			_lowerCAmelCase					(       self		):
        super().test_inference_batch_single_identical(expected_max_diff=7e-3		)






    def 			_lowerCAmelCase					(       self		):
        super().test_pt_np_pil_outputs_equivalent(expected_max_diff=3e-3		)






    def 			_lowerCAmelCase					(       self		):
        super().test_save_load_local(expected_max_difference=3e-3		)






    def 			_lowerCAmelCase					(       self		):
        super().test_save_load_optional_components(expected_max_difference=3e-3		)






    def 			_lowerCAmelCase					(       self		):
        A :       Union[str, Any]          =   [
            """DDIMScheduler""",
            """DDPMScheduler""",
            """PNDMScheduler""",
            """HeunDiscreteScheduler""",
            """EulerAncestralDiscreteScheduler""",
            """KDPM2DiscreteScheduler""",
            """KDPM2AncestralDiscreteScheduler""",
            """DPMSolverSDEScheduler""",
        ]
        A :       int          =   self.get_dummy_components()
        A :       Any          =   self.pipeline_class(**lowerCamelCase__		)

        # make sure that PNDM does not need warm-up
        pipe.scheduler.register_to_config(skip_prk_steps=lowerCamelCase__		)

        pipe.to(lowerCamelCase__		)
        pipe.set_progress_bar_config(disable=lowerCamelCase__		)
        A :       Union[str, Any]          =   self.get_dummy_inputs(lowerCamelCase__		)
        A :       str          =   2

        A :       Tuple          =   []
        for scheduler_enum in KarrasDiffusionSchedulers:
            if scheduler_enum.name in skip_schedulers:
                # no sigma schedulers are not supported
                # no schedulers
                continue

            A :       Dict          =   getattr(lowerCamelCase__,		scheduler_enum.name		)
            A :       List[str]          =   scheduler_cls.from_config(pipe.scheduler.config		)
            A :       Tuple          =   pipe(**lowerCamelCase__		)[0]
            outputs.append(lowerCamelCase__		)

        assert check_same_shape(lowerCamelCase__		)






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




    '''simple docstring'''






    def 			_lowerCAmelCase					(       self		):
        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()






    def 			_lowerCAmelCase					(       self		):
        A :       Dict          =   torch.manual_seed(33		)

        A :       int          =   StableDiffusionPipeline.from_pretrained("""CompVis/stable-diffusion-v1-4""",		torch_dtype=torch.floataa		)
        pipe.to("""cuda"""		)

        A :       str          =   StableDiffusionLatentUpscalePipeline.from_pretrained(
            """stabilityai/sd-x2-latent-upscaler""",		torch_dtype=torch.floataa		)
        upscaler.to("""cuda"""		)

        A :       str          =   """a photo of an astronaut high resolution, unreal engine, ultra realistic"""

        A :       Union[str, Any]          =   pipe(lowerCamelCase__,		generator=lowerCamelCase__,		output_type="""latent"""		).images

        A :       Union[str, Any]          =   upscaler(
            prompt=lowerCamelCase__,		image=lowerCamelCase__,		num_inference_steps=20,		guidance_scale=0,		generator=lowerCamelCase__,		output_type="""np""",		).images[0]

        A :       Optional[Any]          =   load_numpy(
            """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/astronaut_1024.npy"""		)
        assert np.abs((expected_image - image).mean()		) < 5e-2






    def 			_lowerCAmelCase					(       self		):
        A :       str          =   torch.manual_seed(33		)

        A :       Union[str, Any]          =   StableDiffusionLatentUpscalePipeline.from_pretrained(
            """stabilityai/sd-x2-latent-upscaler""",		torch_dtype=torch.floataa		)
        upscaler.to("""cuda"""		)

        A :       List[str]          =   """the temple of fire by Ross Tran and Gerardo Dottori, oil on canvas"""

        A :       List[Any]          =   load_image(
            """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_512.png"""		)

        A :       Optional[Any]          =   upscaler(
            prompt=lowerCamelCase__,		image=lowerCamelCase__,		num_inference_steps=20,		guidance_scale=0,		generator=lowerCamelCase__,		output_type="""np""",		).images[0]

        A :       str          =   load_numpy(
            """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_1024.npy"""		)
        assert np.abs((expected_image - image).max()		) < 5e-2







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

import torch
import torch.nn.functional as F
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .attention import BasicTransformerBlock
from .attention_processor import AttentionProcessor, AttnProcessor
from .embeddings import TimestepEmbedding, Timesteps
from .modeling_utils import ModelMixin






@dataclass
class SCREAMING_SNAKE_CASE__     (		SCREAMING_SNAKE_CASE__    ):




    '''simple docstring'''






    __lowerCamelCase			: torch.FloatTensor






class SCREAMING_SNAKE_CASE__     (		SCREAMING_SNAKE_CASE__ ,						SCREAMING_SNAKE_CASE__    ):




    '''simple docstring'''






    @register_to_config
    def __init__(       self,		lowerCamelCase__ = 32,		lowerCamelCase__ = 64,		lowerCamelCase__ = 20,		lowerCamelCase__ = 768,		lowerCamelCase__=77,		lowerCamelCase__=4,		lowerCamelCase__ = 0.0,		lowerCamelCase__ = "silu",		lowerCamelCase__ = None,		lowerCamelCase__ = None,		lowerCamelCase__ = "linear",		lowerCamelCase__ = "prd",		lowerCamelCase__ = None,		lowerCamelCase__ = None,		lowerCamelCase__ = None,		):
        super().__init__()
        A :       int          =   num_attention_heads
        A :       Tuple          =   attention_head_dim
        A :       Optional[int]          =   num_attention_heads * attention_head_dim
        A :       List[str]          =   additional_embeddings

        A :       int          =   time_embed_dim or inner_dim
        A :       Tuple          =   embedding_proj_dim or embedding_dim
        A :       Dict          =   clip_embed_dim or embedding_dim

        A :       List[str]          =   Timesteps(lowerCamelCase__,		lowerCamelCase__,		0		)
        A :       Any          =   TimestepEmbedding(lowerCamelCase__,		lowerCamelCase__,		out_dim=lowerCamelCase__,		act_fn=lowerCamelCase__		)

        A :       List[Any]          =   nn.Linear(lowerCamelCase__,		lowerCamelCase__		)

        if embedding_proj_norm_type is None:
            A :       Union[str, Any]          =   None
        elif embedding_proj_norm_type == "layer":
            A :       str          =   nn.LayerNorm(lowerCamelCase__		)
        else:
            raise ValueError(f'''unsupported embedding_proj_norm_type: {embedding_proj_norm_type}'''		)

        A :       Optional[Any]          =   nn.Linear(lowerCamelCase__,		lowerCamelCase__		)

        if encoder_hid_proj_type is None:
            A :       Dict          =   None
        elif encoder_hid_proj_type == "linear":
            A :       Union[str, Any]          =   nn.Linear(lowerCamelCase__,		lowerCamelCase__		)
        else:
            raise ValueError(f'''unsupported encoder_hid_proj_type: {encoder_hid_proj_type}'''		)

        A :       List[str]          =   nn.Parameter(torch.zeros(1,		num_embeddings + additional_embeddings,		lowerCamelCase__		)		)

        if added_emb_type == "prd":
            A :       Union[str, Any]          =   nn.Parameter(torch.zeros(1,		1,		lowerCamelCase__		)		)
        elif added_emb_type is None:
            A :       int          =   None
        else:
            raise ValueError(
                f'''`added_emb_type`: {added_emb_type} is not supported. Make sure to choose one of `\'prd\'` or `None`.'''		)

        A :       int          =   nn.ModuleList(
            [
                BasicTransformerBlock(
                    lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		dropout=lowerCamelCase__,		activation_fn="""gelu""",		attention_bias=lowerCamelCase__,		)
                for d in range(lowerCamelCase__		)
            ]		)

        if norm_in_type == "layer":
            A :       int          =   nn.LayerNorm(lowerCamelCase__		)
        elif norm_in_type is None:
            A :       Tuple          =   None
        else:
            raise ValueError(f'''Unsupported norm_in_type: {norm_in_type}.'''		)

        A :       Optional[Any]          =   nn.LayerNorm(lowerCamelCase__		)

        A :       str          =   nn.Linear(lowerCamelCase__,		lowerCamelCase__		)

        A :       Union[str, Any]          =   torch.full(
            [num_embeddings + additional_embeddings, num_embeddings + additional_embeddings],		-1_0000.0		)
        causal_attention_mask.triu_(1		)
        A :       Optional[Any]          =   causal_attention_mask[None, ...]
        self.register_buffer("""causal_attention_mask""",		lowerCamelCase__,		persistent=lowerCamelCase__		)

        A :       Dict          =   nn.Parameter(torch.zeros(1,		lowerCamelCase__		)		)
        A :       Tuple          =   nn.Parameter(torch.zeros(1,		lowerCamelCase__		)		)






    @property
    # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
    def 			_lowerCAmelCase					(       self		):
        A :       Optional[int]          =   {}

        def fn_recursive_add_processors(lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__		):
            if hasattr(lowerCamelCase__,		"""set_processor"""		):
                A :       Tuple          =   module.processor

            for sub_name, child in module.named_children():
                fn_recursive_add_processors(f'''{name}.{sub_name}''',		lowerCamelCase__,		lowerCamelCase__		)

            return processors

        for name, module in self.named_children():
            fn_recursive_add_processors(lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__		)

        return processors






    def 			_lowerCAmelCase					(       self,		lowerCamelCase__		):
        A :       Optional[Any]          =   len(self.attn_processors.keys()		)

        if isinstance(lowerCamelCase__,		lowerCamelCase__		) and len(lowerCamelCase__		) != count:
            raise ValueError(
                f'''A dict of processors was passed, but the number of processors {len(lowerCamelCase__		)} does not match the'''
                f''' number of attention layers: {count}. Please make sure to pass {count} processor classes.'''		)

        def fn_recursive_attn_processor(lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__		):
            if hasattr(lowerCamelCase__,		"""set_processor"""		):
                if not isinstance(lowerCamelCase__,		lowerCamelCase__		):
                    module.set_processor(lowerCamelCase__		)
                else:
                    module.set_processor(processor.pop(f'''{name}.processor'''		)		)

            for sub_name, child in module.named_children():
                fn_recursive_attn_processor(f'''{name}.{sub_name}''',		lowerCamelCase__,		lowerCamelCase__		)

        for name, module in self.named_children():
            fn_recursive_attn_processor(lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__		)






    def 			_lowerCAmelCase					(       self		):
        self.set_attn_processor(AttnProcessor()		)






    def 			_lowerCAmelCase					(       self,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__ = None,		lowerCamelCase__ = None,		lowerCamelCase__ = True,		):
        A :       Optional[int]          =   hidden_states.shape[0]

        A :       Tuple          =   timestep
        if not torch.is_tensor(lowerCamelCase__		):
            A :       List[Any]          =   torch.tensor([timesteps],		dtype=torch.long,		device=hidden_states.device		)
        elif torch.is_tensor(lowerCamelCase__		) and len(timesteps.shape		) == 0:
            A :       Optional[int]          =   timesteps[None].to(hidden_states.device		)

        # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
        A :       Optional[Any]          =   timesteps * torch.ones(lowerCamelCase__,		dtype=timesteps.dtype,		device=timesteps.device		)

        A :       str          =   self.time_proj(lowerCamelCase__		)

        # timesteps does not contain any weights and will always return f32 tensors
        # but time_embedding might be fp16, so we need to cast here.
        A :       Union[str, Any]          =   timesteps_projected.to(dtype=self.dtype		)
        A :       Tuple          =   self.time_embedding(lowerCamelCase__		)

        if self.embedding_proj_norm is not None:
            A :       Optional[Any]          =   self.embedding_proj_norm(lowerCamelCase__		)

        A :       Any          =   self.embedding_proj(lowerCamelCase__		)
        if self.encoder_hidden_states_proj is not None and encoder_hidden_states is not None:
            A :       List[str]          =   self.encoder_hidden_states_proj(lowerCamelCase__		)
        elif self.encoder_hidden_states_proj is not None and encoder_hidden_states is None:
            raise ValueError("""`encoder_hidden_states_proj` requires `encoder_hidden_states` to be set"""		)

        A :       Tuple          =   self.proj_in(lowerCamelCase__		)

        A :       Dict          =   self.positional_embedding.to(hidden_states.dtype		)

        A :       List[str]          =   []
        A :       Optional[Any]          =   0

        if encoder_hidden_states is not None:
            additional_embeds.append(lowerCamelCase__		)
            additional_embeddings_len += encoder_hidden_states.shape[1]

        if len(proj_embeddings.shape		) == 2:
            A :       Optional[Any]          =   proj_embeddings[:, None, :]

        if len(hidden_states.shape		) == 2:
            A :       List[str]          =   hidden_states[:, None, :]

        A :       Dict          =   additional_embeds + [
            proj_embeddings,
            time_embeddings[:, None, :],
            hidden_states,
        ]

        if self.prd_embedding is not None:
            A :       List[Any]          =   self.prd_embedding.to(hidden_states.dtype		).expand(lowerCamelCase__,		-1,		-1		)
            additional_embeds.append(lowerCamelCase__		)

        A :       Union[str, Any]          =   torch.cat(
            lowerCamelCase__,		dim=1,		)

        # Allow positional_embedding to not include the `addtional_embeddings` and instead pad it with zeros for these additional tokens
        A :       Dict          =   additional_embeddings_len + proj_embeddings.shape[1] + 1
        if positional_embeddings.shape[1] < hidden_states.shape[1]:
            A :       Any          =   F.pad(
                lowerCamelCase__,		(
                    0,
                    0,
                    additional_embeddings_len,
                    self.prd_embedding.shape[1] if self.prd_embedding is not None else 0,
                ),		value=0.0,		)

        A :       Union[str, Any]          =   hidden_states + positional_embeddings

        if attention_mask is not None:
            A :       List[Any]          =   (1 - attention_mask.to(hidden_states.dtype		)) * -1_0000.0
            A :       Tuple          =   F.pad(lowerCamelCase__,		(0, self.additional_embeddings),		value=0.0		)
            A :       List[Any]          =   (attention_mask[:, None, :] + self.causal_attention_mask).to(hidden_states.dtype		)
            A :       Any          =   attention_mask.repeat_interleave(self.config.num_attention_heads,		dim=0		)

        if self.norm_in is not None:
            A :       str          =   self.norm_in(lowerCamelCase__		)

        for block in self.transformer_blocks:
            A :       Optional[Any]          =   block(lowerCamelCase__,		attention_mask=lowerCamelCase__		)

        A :       Optional[int]          =   self.norm_out(lowerCamelCase__		)

        if self.prd_embedding is not None:
            A :       Dict          =   hidden_states[:, -1]
        else:
            A :       Optional[int]          =   hidden_states[:, additional_embeddings_len:]

        A :       Optional[Any]          =   self.proj_to_clip_embeddings(lowerCamelCase__		)

        if not return_dict:
            return (predicted_image_embedding,)

        return PriorTransformerOutput(predicted_image_embedding=lowerCamelCase__		)






    def 			_lowerCAmelCase					(       self,		lowerCamelCase__		):
        A :       str          =   (prior_latents * self.clip_std) + self.clip_mean
        return prior_latents


