




"""simple docstring"""






import gc
import random
import unittest

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

from diffusers import (
    AutoencoderKL,
    DiffusionPipeline,
    EulerDiscreteScheduler,
    StableDiffusionXLImgaImgPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import floats_tensor, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    IMAGE_TO_IMAGE_IMAGE_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()






class lowerCAmelCase__      (	lowercase,      lowercase,      unittest.TestCase		):






			'''simple docstring'''



			lowerCamelCase__ =     StableDiffusionXLImgaImgPipeline
			lowerCamelCase__ =     TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width"""}
			lowerCamelCase__ =     PipelineTesterMixin.required_optional_params - {"""latents"""}
			lowerCamelCase__ =     TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
			lowerCamelCase__ =     IMAGE_TO_IMAGE_IMAGE_PARAMS
			lowerCamelCase__ =     IMAGE_TO_IMAGE_IMAGE_PARAMS
			def    A_       (    self    ):
							torch.manual_seed(0    )
							_lowerCamelCase	:     Optional[int]       =       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')    , attention_head_dim=(2, 4)    , use_linear_projection=lowercase    , addition_embed_type='text_time'    , addition_time_embed_dim=8    , transformer_layers_per_block=(1, 2)    , projection_class_embeddings_input_dim=80    , cross_attention_dim=64    , )
							_lowerCamelCase	:     Dict       =       EulerDiscreteScheduler(
							    beta_start=0.0_00_85    , beta_end=0.0_12    , steps_offset=1    , beta_schedule='scaled_linear'    , timestep_spacing='leading'    , )
							torch.manual_seed(0    )
							_lowerCamelCase	:     Optional[Any]       =       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    , sample_size=128    , )
							torch.manual_seed(0    )
							_lowerCamelCase	:     Tuple       =       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='gelu'    , projection_dim=32    , )
							_lowerCamelCase	:     Optional[Any]       =       CLIPTextModel(lowercase    )
							_lowerCamelCase	:     Any       =       CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'    , local_files_only=lowercase    )

							_lowerCamelCase	:     Dict       =       CLIPTextModelWithProjection(lowercase    )
							_lowerCamelCase	:     Tuple       =       CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'    , local_files_only=lowercase    )

							_lowerCamelCase	:     Union[str, Any]       =       {
							    'unet': unet,
							    'scheduler': scheduler,
							    'vae': vae,
							    'text_encoder': text_encoder,
							    'tokenizer': tokenizer,
							    'text_encoder_2': text_encoder_a,
							    'tokenizer_2': tokenizer_a,
							    # "safety_checker": None,
							    # "feature_extractor": None,
							}
							return components
			def    A_       (    self    , lowercase    , lowercase=0    ):
							_lowerCamelCase	:     List[Any]       =       floats_tensor((1, 3, 32, 32)    , rng=random.Random(lowercase    )    ).to(lowercase    )
							_lowerCamelCase	:     Any       =       image / 2 + 0.5
							if str(lowercase    ).startswith('mps'    ):
											_lowerCamelCase	:     Any       =       torch.manual_seed(lowercase    )
							else:
											_lowerCamelCase	:     Optional[int]       =       torch.Generator(device=lowercase    ).manual_seed(lowercase    )
							_lowerCamelCase	:     List[Any]       =       {
							    'prompt': 'A painting of a squirrel eating a burger',
							    'image': image,
							    'generator': generator,
							    'num_inference_steps': 2,
							    'guidance_scale': 5.0,
							    'output_type': 'numpy',
							    'strength': 0.75,
							}
							return inputs
			def    A_       (    self    ):
							_lowerCamelCase	:     Union[str, Any]       =       'cpu'  # ensure determinism for the device-dependent torch.Generator
							_lowerCamelCase	:     Optional[int]       =       self.get_dummy_components()
							_lowerCamelCase	:     Union[str, Any]       =       StableDiffusionXLImgaImgPipeline(**lowercase    )
							_lowerCamelCase	:     Tuple       =       sd_pipe.to(lowercase    )
							sd_pipe.set_progress_bar_config(disable=lowercase    )

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

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

							_lowerCamelCase	:     Tuple       =       np.array([0.46_56, 0.48_40, 0.44_39, 0.66_98, 0.55_74, 0.45_24, 0.57_99, 0.59_43, 0.51_65]    )

							assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1E-2
			def    A_       (    self    ):
							super().test_attention_slicing_forward_pass(expected_max_diff=3E-3    )
			def    A_       (    self    ):
							super().test_inference_batch_single_identical(expected_max_diff=3E-3    )
			def    A_       (    self    ):
							pass






			def    A_       (    self    ):
							_lowerCamelCase	:     int       =       self.get_dummy_components()
							_lowerCamelCase	:     str       =       StableDiffusionXLImgaImgPipeline(**lowercase    )
							_lowerCamelCase	:     Optional[int]       =       sd_pipe.to(lowercase    )
							_lowerCamelCase	:     Any       =       sd_pipe.to(lowercase    )
							sd_pipe.set_progress_bar_config(disable=lowercase    )

							# forward without prompt embeds
							_lowerCamelCase	:     List[str]       =       self.get_dummy_inputs(lowercase    )
							_lowerCamelCase	:     Union[str, Any]       =       3 * ['this is a negative prompt']
							_lowerCamelCase	:     Optional[int]       =       negative_prompt
							_lowerCamelCase	:     List[str]       =       3 * [inputs['prompt']]

							_lowerCamelCase	:     List[str]       =       sd_pipe(**lowercase    )
							_lowerCamelCase	:     Any       =       output.images[0, -3:, -3:, -1]

							# forward with prompt embeds
							_lowerCamelCase	:     Tuple       =       self.get_dummy_inputs(lowercase    )
							_lowerCamelCase	:     List[Any]       =       3 * ['this is a negative prompt']
							_lowerCamelCase	:     Union[str, Any]       =       3 * [inputs.pop('prompt'    )]

							(
							    (
							    _lowerCamelCase
							),					(
							    _lowerCamelCase
							),					(
							    _lowerCamelCase
							),					(
							    _lowerCamelCase
							),					
							)	:     Dict       =       sd_pipe.encode_prompt(lowercase    , negative_prompt=lowercase    )

							_lowerCamelCase	:     Optional[Any]       =       sd_pipe(
							    **lowercase    , prompt_embeds=lowercase    , negative_prompt_embeds=lowercase    , pooled_prompt_embeds=lowercase    , negative_pooled_prompt_embeds=lowercase    , )
							_lowerCamelCase	:     List[Any]       =       output.images[0, -3:, -3:, -1]

							# make sure that it's equal
							assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()    ).max() < 1E-4






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






			'''simple docstring'''



			def    A_       (    self    ):
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()
			def    A_       (    self    , lowercase    , lowercase="cpu"    , lowercase=torch.floataa    , lowercase=0    ):
							_lowerCamelCase	:     List[Any]       =       torch.Generator(device=lowercase    ).manual_seed(lowercase    )
							_lowerCamelCase	:     str       =       np.random.RandomState(lowercase    ).standard_normal((1, 4, 64, 64)    )
							_lowerCamelCase	:     Dict       =       torch.from_numpy(lowercase    ).to(device=lowercase    , dtype=lowercase    )
							_lowerCamelCase	:     int       =       {
							    'prompt': 'a photograph of an astronaut riding a horse',
							    'latents': latents,
							    'generator': generator,
							    'num_inference_steps': 3,
							    'guidance_scale': 7.5,
							    'output_type': 'numpy',
							}
							return inputs






			def    A_       (    self    ):
							_lowerCamelCase	:     Union[str, Any]       =       DiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-base'    )
							pipe.to(lowercase    )
							pipe.set_progress_bar_config(disable=lowercase    )

							_lowerCamelCase	:     Any       =       self.get_inputs(lowercase    )
							_lowerCamelCase	:     Optional[Any]       =       pipe(**lowercase    ).images
							_lowerCamelCase	:     List[Any]       =       image[0, -3:, -3:, -1].flatten()

							assert image.shape == (1, 512, 512, 3)
							_lowerCamelCase	:     int       =       np.array([0.4_94_93, 0.4_78_96, 0.4_07_98, 0.5_42_14, 0.5_32_12, 0.4_82_02, 0.4_76_56, 0.4_63_29, 0.4_85_06]    )
							assert np.abs(image_slice - expected_slice    ).max() < 7E-3





"""simple docstring"""






# DISCLAIMER: This code is strongly influenced by https://github.com/pesser/pytorch_diffusion
# and https://github.com/hojonathanho/diffusion
import math
from dataclasses import dataclass
from typing import List, Optional, Tuple, Union

import numpy as np
import torch

from diffusers.configuration_utils import ConfigMixin, register_to_config
from diffusers.schedulers.scheduling_utils import SchedulerMixin
from diffusers.utils import BaseOutput, deprecate






@dataclass
# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->DDIM
class lowerCAmelCase__      (	lowercase		):






			'''simple docstring'''



			lowerCamelCase__ =     42
			lowerCamelCase__ =     None







def 	_snake_case     (							lowercase__				,       lowercase__=0.9_9_9				,       lowercase__="cosine"				,       ):
				if alpha_transform_type == "cosine":

								def alpha_bar_fn(lowercase__						):
												return math.cos((t + 0.0_0_8) / 1.0_0_8 * math.pi / 2						) ** 2

				elif alpha_transform_type == "exp":

								def alpha_bar_fn(lowercase__						):
												return math.exp(t * -1_2.0						)

				else:
								raise ValueError(f'''Unsupported alpha_tranform_type: {alpha_transform_type}'''						)

				_lowerCamelCase	:     Optional[Any]       =       []
				for i in range(lowercase__						):
								_lowerCamelCase	:     Optional[Any]       =       i / num_diffusion_timesteps
								_lowerCamelCase	:     List[str]       =       (i + 1) / num_diffusion_timesteps
								betas.append(min(1 - alpha_bar_fn(lowercase__						) / alpha_bar_fn(lowercase__						)				,       lowercase__						)						)
				return torch.tensor(lowercase__				,       dtype=torch.floataa						)






class lowerCAmelCase__      (	lowercase,      lowercase		):






			'''simple docstring'''



			lowerCamelCase__ =     1
			@register_to_config
			def __init__(    self    , lowercase = 1000    , lowercase = 0.00_01    , lowercase = 0.02    , lowercase = "linear"    , lowercase = None    , lowercase = True    , lowercase = True    , lowercase = 0    , lowercase = "epsilon"    , lowercase = 1.0    , **lowercase    , ):
							if kwargs.get('set_alpha_to_one'    , lowercase    ) is not None:
											_lowerCamelCase	:     List[str]       =       (
											    'The `set_alpha_to_one` argument is deprecated. Please use `set_alpha_to_zero` instead.'
											)
											deprecate('set_alpha_to_one'    , '1.0.0'    , lowercase    , standard_warn=lowercase    )
											_lowerCamelCase	:     Union[str, Any]       =       kwargs['set_alpha_to_one']
							if trained_betas is not None:
											_lowerCamelCase	:     Tuple       =       torch.tensor(lowercase    , dtype=torch.floataa    )
							elif beta_schedule == "linear":
											_lowerCamelCase	:     List[str]       =       torch.linspace(lowercase    , lowercase    , lowercase    , dtype=torch.floataa    )
							elif beta_schedule == "scaled_linear":
											# this schedule is very specific to the latent diffusion model.
											_lowerCamelCase	:     Any       =       (
											    torch.linspace(beta_start**0.5    , beta_end**0.5    , lowercase    , dtype=torch.floataa    ) ** 2
											)
							elif beta_schedule == "squaredcos_cap_v2":
											# Glide cosine schedule
											_lowerCamelCase	:     Tuple       =       betas_for_alpha_bar(lowercase    )
							else:
											raise NotImplementedError(F'''{beta_schedule} does is not implemented for {self.__class__}'''    )

							_lowerCamelCase	:     str       =       1.0 - self.betas
							_lowerCamelCase	:     str       =       torch.cumprod(self.alphas    , dim=0    )

							# At every step in inverted ddim, we are looking into the next alphas_cumprod
							# For the final step, there is no next alphas_cumprod, and the index is out of bounds
							# `set_alpha_to_zero` decides whether we set this parameter simply to zero
							# in this case, self.step() just output the predicted noise
							# or whether we use the final alpha of the "non-previous" one.
							_lowerCamelCase	:     int       =       torch.tensor(0.0    ) if set_alpha_to_zero else self.alphas_cumprod[-1]

							# standard deviation of the initial noise distribution
							_lowerCamelCase	:     Optional[Any]       =       1.0

							# setable values
							_lowerCamelCase	:     Dict       =       None
							_lowerCamelCase	:     int       =       torch.from_numpy(np.arange(0    , lowercase    ).copy().astype(np.intaa    )    )
			def    A_       (    self    , lowercase    , lowercase = None    ):
							return sample
			def    A_       (    self    , lowercase    , lowercase = None    ):

							if num_inference_steps > self.config.num_train_timesteps:
											raise ValueError(
											    F'''`num_inference_steps`: {num_inference_steps} cannot be larger than `self.config.train_timesteps`:'''
											    F''' {self.config.num_train_timesteps} as the unet model trained with this scheduler can only handle'''
											    F''' maximal {self.config.num_train_timesteps} timesteps.'''    )

							_lowerCamelCase	:     int       =       num_inference_steps
							_lowerCamelCase	:     Optional[int]       =       self.config.num_train_timesteps // self.num_inference_steps
							# creates integer timesteps by multiplying by ratio
							# casting to int to avoid issues when num_inference_step is power of 3
							_lowerCamelCase	:     Any       =       (np.arange(0    , lowercase    ) * step_ratio).round().copy().astype(np.intaa    )
							_lowerCamelCase	:     Any       =       torch.from_numpy(lowercase    ).to(lowercase    )
							self.timesteps += self.config.steps_offset
			def    A_       (    self    , lowercase    , lowercase    , lowercase    , lowercase = 0.0    , lowercase = False    , lowercase = None    , lowercase = True    , ):
							# 1. get previous step value (=t+1)
							_lowerCamelCase	:     int       =       timestep + self.config.num_train_timesteps // self.num_inference_steps

							# 2. compute alphas, betas
							# change original implementation to exactly match noise levels for analogous forward process
							_lowerCamelCase	:     Dict       =       self.alphas_cumprod[timestep]
							_lowerCamelCase	:     str       =       (
							    self.alphas_cumprod[prev_timestep]
							    if prev_timestep < self.config.num_train_timesteps
							    else self.final_alpha_cumprod
							)

							_lowerCamelCase	:     Any       =       1 - alpha_prod_t

							# 3. compute predicted original sample from predicted noise also called
							# "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
							if self.config.prediction_type == "epsilon":
											_lowerCamelCase	:     Optional[int]       =       (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5
											_lowerCamelCase	:     Any       =       model_output
							elif self.config.prediction_type == "sample":
											_lowerCamelCase	:     Optional[Any]       =       model_output
											_lowerCamelCase	:     Tuple       =       (sample - alpha_prod_t ** 0.5 * pred_original_sample) / beta_prod_t ** 0.5
							elif self.config.prediction_type == "v_prediction":
											_lowerCamelCase	:     Any       =       (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output
											_lowerCamelCase	:     Tuple       =       (alpha_prod_t**0.5) * model_output + (beta_prod_t**0.5) * sample
							else:
											raise ValueError(
											    F'''prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample`, or'''
											    ' `v_prediction`'    )

							# 4. Clip or threshold "predicted x_0"
							if self.config.clip_sample:
											_lowerCamelCase	:     Tuple       =       pred_original_sample.clamp(
											    -self.config.clip_sample_range    , self.config.clip_sample_range    )

							# 5. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
							_lowerCamelCase	:     Optional[int]       =       (1 - alpha_prod_t_prev) ** 0.5 * pred_epsilon

							# 6. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
							_lowerCamelCase	:     Dict       =       alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction

							if not return_dict:
											return (prev_sample, pred_original_sample)
							return DDIMSchedulerOutput(prev_sample=lowercase    , pred_original_sample=lowercase    )






			def __len__(    self    ):
							return self.config.num_train_timesteps