


"""simple docstring"""



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       lowerCamelCase					(a_						:int)     ->							Union[str, Any]:
      lowercase		:str       							=				[tensor.shape for tensor in tensor_list]
      return all(shape == shapes[0] for shape in shapes[1:])
class 					__magic_name__		(			__A			,						__A			,						__A			,						unittest.TestCase     ):
       __A			:      List[str]        =   StableDiffusionLatentUpscalePipeline
       __A			:      List[Any]        =   TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {
           "height",
           "width",
           "cross_attention_kwargs",
           "negative_prompt_embeds",
           "prompt_embeds",
       }
       __A			:      Optional[int]        =   PipelineTesterMixin.required_optional_params - {"num_images_per_prompt"}
       __A			:      Dict        =   TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
       __A			:      int        =   frozenset(
           []     )  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
       __A			:      Tuple        =   frozenset([]     )

       __A			:      Union[str, Any]        =   True






       @property
       def  __snake_case						(   self  :    Optional[Any]				):

             '''simple docstring'''






             lowercase		:str       							=				1
             lowercase		:List[str]       							=				4
             lowercase		:Union[str, Any]       							=				(1_6, 1_6)

             lowercase		:Dict       							=				floats_tensor((batch_size, num_channels) + sizes  ,     rng=random.Random(0				)				).to(snake_case__				)
             return image






       def  __snake_case						(   self  :    Optional[Any]				):

             '''simple docstring'''






             torch.manual_seed(0				)
             lowercase		:Optional[int]       							=				UNetaDConditionModel(
                 act_fn='''gelu'''  ,     attention_head_dim=8  ,     norm_num_groups=snake_case__  ,     block_out_channels=[3_2, 3_2, 6_4, 6_4]  ,     time_cond_proj_dim=1_6_0  ,     conv_in_kernel=1  ,     conv_out_kernel=1  ,     cross_attention_dim=3_2  ,     down_block_types=(
                     '''KDownBlock2D''',
                     '''KCrossAttnDownBlock2D''',
                     '''KCrossAttnDownBlock2D''',
                     '''KCrossAttnDownBlock2D''',
                 )  ,     in_channels=8  ,     mid_block_type=snake_case__  ,     only_cross_attention=snake_case__  ,     out_channels=5  ,     resnet_time_scale_shift='''scale_shift'''  ,     time_embedding_type='''fourier'''  ,     timestep_post_act='''gelu'''  ,     up_block_types=('''KCrossAttnUpBlock2D''', '''KCrossAttnUpBlock2D''', '''KCrossAttnUpBlock2D''', '''KUpBlock2D''')  ,     )
             lowercase		:List[str]       							=				AutoencoderKL(
                 block_out_channels=[3_2, 3_2, 6_4, 6_4]  ,     in_channels=3  ,     out_channels=3  ,     down_block_types=[
                     '''DownEncoderBlock2D''',
                     '''DownEncoderBlock2D''',
                     '''DownEncoderBlock2D''',
                     '''DownEncoderBlock2D''',
                 ]  ,     up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''', '''UpDecoderBlock2D''', '''UpDecoderBlock2D''']  ,     latent_channels=4  ,     )
             lowercase		:Union[str, Any]       							=				EulerDiscreteScheduler(prediction_type='''sample'''				)
             lowercase		:int       							=				CLIPTextConfig(
                 bos_token_id=0  ,     eos_token_id=2  ,     hidden_size=3_2  ,     intermediate_size=3_7  ,     layer_norm_eps=1e-0_5  ,     num_attention_heads=4  ,     num_hidden_layers=5  ,     pad_token_id=1  ,     vocab_size=1_0_0_0  ,     hidden_act='''quick_gelu'''  ,     projection_dim=5_1_2  ,     )
             lowercase		:Dict       							=				CLIPTextModel(snake_case__				)
             lowercase		:int       							=				CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''				)

             lowercase		:List[Any]       							=				{
                 '''unet''': model.eval(),
                 '''vae''': vae.eval(),
                 '''scheduler''': scheduler,
                 '''text_encoder''': text_encoder,
                 '''tokenizer''': tokenizer,
             }

             return components






       def  __snake_case						(   self  :    Union[str, Any]  ,     snake_case__  :    Optional[int]  ,     snake_case__  :    Optional[int]=0				):

             '''simple docstring'''






             if str(snake_case__				).startswith('''mps'''				):
                   lowercase		:int       							=				torch.manual_seed(snake_case__				)
             else:
                   lowercase		:Union[str, Any]       							=				torch.Generator(device=snake_case__				).manual_seed(snake_case__				)
             lowercase		:List[str]       							=				{
                 '''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  __snake_case						(   self  :    str				):

             '''simple docstring'''






             lowercase		:str       							=				'''cpu'''

             lowercase		:List[str]       							=				self.get_dummy_components()
             lowercase		:str       							=				self.pipeline_class(**snake_case__				)
             pipe.to(snake_case__				)
             pipe.set_progress_bar_config(disable=snake_case__				)

             lowercase		:List[str]       							=				self.get_dummy_inputs(snake_case__				)
             lowercase		:int       							=				pipe(**snake_case__				).images
             lowercase		:str       							=				image[0, -3:, -3:, -1]

             self.assertEqual(image.shape  ,     (1, 2_5_6, 2_5_6, 3)				)
             lowercase		:Dict       							=				np.array(
                 [0.47_22_24_12, 0.41_92_16_33, 0.44_71_74_34, 0.46_87_41_92, 0.42_58_82_58, 0.46_15_07_26, 0.4_67_75_34, 0.45_58_38_32, 0.48_57_90_55]				)
             lowercase		:int       							=				np.abs(image_slice.flatten() - expected_slice				).max()
             self.assertLessEqual(snake_case__  ,     1e-3				)






       def  __snake_case						(   self  :    List[Any]				):

             '''simple docstring'''






             super().test_attention_slicing_forward_pass(expected_max_diff=7e-3				)






       def  __snake_case						(   self  :    str				):

             '''simple docstring'''






             super().test_cpu_offload_forward_pass(expected_max_diff=3e-3				)






       def  __snake_case						(   self  :    str				):

             '''simple docstring'''






             super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3				)






       def  __snake_case						(   self  :    str				):

             '''simple docstring'''






             super().test_inference_batch_single_identical(expected_max_diff=7e-3				)






       def  __snake_case						(   self  :    Dict				):

             '''simple docstring'''






             super().test_pt_np_pil_outputs_equivalent(expected_max_diff=3e-3				)






       def  __snake_case						(   self  :    int				):

             '''simple docstring'''






             super().test_save_load_local(expected_max_difference=3e-3				)






       def  __snake_case						(   self  :    List[Any]				):

             '''simple docstring'''






             super().test_save_load_optional_components(expected_max_difference=3e-3				)







       def  __snake_case						(   self  :    Dict				):

             '''simple docstring'''






             lowercase		:Dict       							=				[
                 '''DDIMScheduler''',
                 '''DDPMScheduler''',
                 '''PNDMScheduler''',
                 '''HeunDiscreteScheduler''',
                 '''EulerAncestralDiscreteScheduler''',
                 '''KDPM2DiscreteScheduler''',
                 '''KDPM2AncestralDiscreteScheduler''',
                 '''DPMSolverSDEScheduler''',
             ]
             lowercase		:int       							=				self.get_dummy_components()
             lowercase		:Dict       							=				self.pipeline_class(**snake_case__				)

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

             pipe.to(snake_case__				)
             pipe.set_progress_bar_config(disable=snake_case__				)
             lowercase		:int       							=				self.get_dummy_inputs(snake_case__				)
             lowercase		:int       							=				2

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

                   lowercase		:Dict       							=				getattr(snake_case__  ,     scheduler_enum.name				)
                   lowercase		:Tuple       							=				scheduler_cls.from_config(pipe.scheduler.config				)
                   lowercase		:Dict       							=				pipe(**snake_case__				)[0]
                   outputs.append(snake_case__				)

             assert check_same_shape(snake_case__				)







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






       def  __snake_case						(   self  :    Tuple				):

             '''simple docstring'''






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






       def  __snake_case						(   self  :    Optional[Any]				):

             '''simple docstring'''






             lowercase		:Tuple       							=				torch.manual_seed(3_3				)

             lowercase		:Dict       							=				StableDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4'''  ,     torch_dtype=torch.floataa				)
             pipe.to('''cuda'''				)

             lowercase		:List[str]       							=				StableDiffusionLatentUpscalePipeline.from_pretrained(
                 '''stabilityai/sd-x2-latent-upscaler'''  ,     torch_dtype=torch.floataa				)
             upscaler.to('''cuda'''				)

             lowercase		:str       							=				'''a photo of an astronaut high resolution, unreal engine, ultra realistic'''

             lowercase		:Dict       							=				pipe(snake_case__  ,     generator=snake_case__  ,     output_type='''latent'''				).images

             lowercase		:Union[str, Any]       							=				upscaler(
                 prompt=snake_case__  ,     image=snake_case__  ,     num_inference_steps=2_0  ,     guidance_scale=0  ,     generator=snake_case__  ,     output_type='''np'''  ,     ).images[0]

             lowercase		:str       							=				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  __snake_case						(   self  :    Union[str, Any]				):

             '''simple docstring'''






             lowercase		:str       							=				torch.manual_seed(3_3				)

             lowercase		:Optional[int]       							=				StableDiffusionLatentUpscalePipeline.from_pretrained(
                 '''stabilityai/sd-x2-latent-upscaler'''  ,     torch_dtype=torch.floataa				)
             upscaler.to('''cuda'''				)

             lowercase		:Union[str, Any]       							=				'''the temple of fire by Ross Tran and Gerardo Dottori, oil on canvas'''

             lowercase		:Union[str, Any]       							=				load_image(
                 '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_512.png'''				)

             lowercase		:Optional[Any]       							=				upscaler(
                 prompt=snake_case__  ,     image=snake_case__  ,     num_inference_steps=2_0  ,     guidance_scale=0  ,     generator=snake_case__  ,     output_type='''np'''  ,     ).images[0]

             lowercase		: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





"""simple docstring"""



import gc
import inspect
import unittest

import torch
from parameterized import parameterized

from diffusers import PriorTransformer
from diffusers.utils import floats_tensor, slow, torch_all_close, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin


enable_full_determinism()
class 					__magic_name__		(			__UpperCAmelCase			,						unittest.TestCase     ):
       __A			:      Tuple        =   PriorTransformer
       __A			:      Any        =   "hidden_states"






       @property
       def  __snake_case						(   self  :    str				):

              '''simple docstring'''






              lowercase		:Union[str, Any]       							=				4
              lowercase		:List[Any]       							=				8
              lowercase		:Any       							=				7

              lowercase		:Optional[Any]       							=				floats_tensor((batch_size, embedding_dim)				).to(snake_case__				)

              lowercase		:List[Any]       							=				floats_tensor((batch_size, embedding_dim)				).to(snake_case__				)
              lowercase		:Union[str, Any]       							=				floats_tensor((batch_size, num_embeddings, embedding_dim)				).to(snake_case__				)

              return {
                  "hidden_states": hidden_states,
                  "timestep": 2,
                  "proj_embedding": proj_embedding,
                  "encoder_hidden_states": encoder_hidden_states,
              }






       def  __snake_case						(   self  :    Dict  ,     snake_case__  :    Optional[Any]=0				):

              '''simple docstring'''






              torch.manual_seed(snake_case__				)
              lowercase		:Union[str, Any]       							=				4
              lowercase		:Optional[int]       							=				8
              lowercase		:str       							=				7

              lowercase		:Dict       							=				torch.randn((batch_size, embedding_dim)				).to(snake_case__				)

              lowercase		:Union[str, Any]       							=				torch.randn((batch_size, embedding_dim)				).to(snake_case__				)
              lowercase		:Tuple       							=				torch.randn((batch_size, num_embeddings, embedding_dim)				).to(snake_case__				)

              return {
                  "hidden_states": hidden_states,
                  "timestep": 2,
                  "proj_embedding": proj_embedding,
                  "encoder_hidden_states": encoder_hidden_states,
              }






       @property
       def  __snake_case						(   self  :    Union[str, Any]				):

              '''simple docstring'''






              return (4, 8)






       @property
       def  __snake_case						(   self  :    Dict				):

              '''simple docstring'''






              return (4, 8)






       def  __snake_case						(   self  :    Optional[Any]				):

              '''simple docstring'''






              lowercase		:Union[str, Any]       							=				{
                  '''num_attention_heads''': 2,
                  '''attention_head_dim''': 4,
                  '''num_layers''': 2,
                  '''embedding_dim''': 8,
                  '''num_embeddings''': 7,
                  '''additional_embeddings''': 4,
              }
              lowercase		:List[str]       							=				self.dummy_input
              return init_dict, inputs_dict






       def  __snake_case						(   self  :    List[str]				):

              '''simple docstring'''






              lowercase							,  lowercase		:Optional[int]       							=				PriorTransformer.from_pretrained(
                  '''hf-internal-testing/prior-dummy'''  ,     output_loading_info=snake_case__				)
              self.assertIsNotNone(snake_case__				)
              self.assertEqual(len(loading_info['''missing_keys''']				)  ,     0				)

              model.to(snake_case__				)
              lowercase		:List[Any]       							=				model(**self.dummy_input				)[0]

              assert hidden_states is not None, "Make sure output is not None"






       def  __snake_case						(   self  :    Union[str, Any]				):

              '''simple docstring'''






              lowercase							,  lowercase		:List[Any]       							=				self.prepare_init_args_and_inputs_for_common()

              lowercase		:str       							=				self.model_class(**snake_case__				)
              lowercase		:Optional[Any]       							=				inspect.signature(model.forward				)
              # signature.parameters is an OrderedDict => so arg_names order is deterministic
              lowercase		:List[Any]       							=				[*signature.parameters.keys()]

              lowercase		:Optional[Any]       							=				['''hidden_states''', '''timestep''']
              self.assertListEqual(arg_names[:2]  ,     snake_case__				)







       def  __snake_case						(   self  :    Optional[int]				):

              '''simple docstring'''






              lowercase		:Optional[int]       							=				PriorTransformer.from_pretrained('''hf-internal-testing/prior-dummy'''				)
              lowercase		:List[Any]       							=				model.to(snake_case__				)

              if hasattr(snake_case__  ,     '''set_default_attn_processor'''				):
                     model.set_default_attn_processor()

              lowercase		:List[str]       							=				self.get_dummy_seed_input()

              with torch.no_grad():
                     lowercase		:List[Any]       							=				model(**snake_case__				)[0]

              lowercase		:Dict       							=				output[0, :5].flatten().cpu()
              print(snake_case__				)

              # Since the VAE Gaussian prior's generator is seeded on the appropriate device,
              # the expected output slices are not the same for CPU and GPU.
              lowercase		:str       							=				torch.tensor([-1.34_36, -0.28_70, 0.75_38, 0.43_68, -0.02_39]				)
              self.assertTrue(torch_all_close(snake_case__  ,     snake_case__  ,     rtol=1e-2				)				)







@slow
class 					__magic_name__		(			unittest.TestCase     ):






       def  __snake_case						(   self  :    Tuple  ,     snake_case__  :    int=1  ,     snake_case__  :    int=7_6_8  ,     snake_case__  :    Optional[Any]=7_7  ,     snake_case__  :    Union[str, Any]=0				):

              '''simple docstring'''






              torch.manual_seed(snake_case__				)
              lowercase		:Any       							=				batch_size
              lowercase		:Dict       							=				embedding_dim
              lowercase		:int       							=				num_embeddings

              lowercase		:int       							=				torch.randn((batch_size, embedding_dim)				).to(snake_case__				)

              lowercase		:List[str]       							=				torch.randn((batch_size, embedding_dim)				).to(snake_case__				)
              lowercase		:Optional[Any]       							=				torch.randn((batch_size, num_embeddings, embedding_dim)				).to(snake_case__				)

              return {
                  "hidden_states": hidden_states,
                  "timestep": 2,
                  "proj_embedding": proj_embedding,
                  "encoder_hidden_states": encoder_hidden_states,
              }






       def  __snake_case						(   self  :    Dict				):

              '''simple docstring'''






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







       @parameterized.expand(
           [
               # fmt: off
               [1_3, [-0.58_61,  0.12_83, -0.09_31,  0.08_82,  0.44_76,  0.13_29, -0.04_98,  0.06_40]],
               [3_7, [-0.49_13,  0.01_10, -0.04_83,  0.05_41,  0.49_54, -0.01_70,  0.03_54,  0.16_51]],
               # fmt: on
           ]				)
       def  __snake_case						(   self  :    Dict  ,     snake_case__  :    Dict  ,     snake_case__  :    Union[str, Any]				):

              '''simple docstring'''






              lowercase		:str       							=				PriorTransformer.from_pretrained('''kandinsky-community/kandinsky-2-1-prior'''  ,     subfolder='''prior'''				)
              model.to(snake_case__				)
              lowercase		:Union[str, Any]       							=				self.get_dummy_seed_input(seed=snake_case__				)

              with torch.no_grad():
                     lowercase		:int       							=				model(**snake_case__				)[0]

              assert list(sample.shape				) == [1, 7_6_8]

              lowercase		:List[Any]       							=				sample[0, :8].flatten().cpu()
              print(snake_case__				)
              lowercase		:Union[str, Any]       							=				torch.tensor(snake_case__				)

              assert torch_all_close(snake_case__  ,     snake_case__  ,     atol=1e-3				)

