





'''simple docstring'''


import gc
import unittest

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

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

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


enable_full_determinism()



class 							__UpperCAmelCase  (       A__		,					A__		,					unittest.TestCase						):







		'''simple docstring'''







		__lowerCAmelCase 				=    StableDiffusionXLPipeline
		__lowerCAmelCase 				=    TEXT_TO_IMAGE_PARAMS
		__lowerCAmelCase 				=    TEXT_TO_IMAGE_BATCH_PARAMS
		__lowerCAmelCase 				=    TEXT_TO_IMAGE_IMAGE_PARAMS
		__lowerCAmelCase 				=    TEXT_TO_IMAGE_IMAGE_PARAMS







		def        A	(self						:      Dict      ):
			torch.manual_seed(0      )
			A							  =      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=_lowerCAmelCase      ,      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      ,      )
			A							  =      EulerDiscreteScheduler(
			    beta_start=0.00_085      ,      beta_end=0.012      ,      steps_offset=1      ,      beta_schedule="""scaled_linear"""      ,      timestep_spacing="""leading"""      ,      )
			torch.manual_seed(0      )
			A							  =      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      )
			A							  =      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      ,      )
			A							  =      CLIPTextModel(_lowerCAmelCase      )
			A							  =      CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""      ,      local_files_only=_lowerCAmelCase      )

			A							  =      CLIPTextModelWithProjection(_lowerCAmelCase      )
			A							  =      CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""      ,      local_files_only=_lowerCAmelCase      )

			A							  =      {
			    """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						:      Optional[Any]      ,      _lowerCAmelCase						:      int      ,      _lowerCAmelCase						:      List[Any]=0      ):
			if str(_lowerCAmelCase      ).startswith("""mps"""      ):
				A							  =      torch.manual_seed(_lowerCAmelCase      )
			else:
				A							  =      torch.Generator(device=_lowerCAmelCase      ).manual_seed(_lowerCAmelCase      )
			A							  =      {
			    """prompt""": """A painting of a squirrel eating a burger""",
			    """generator""": generator,
			    """num_inference_steps""": 2,
			    """guidance_scale""": 5.0,
			    """output_type""": """numpy""",
			}
			return inputs







		def        A	(self						:      Optional[Any]      ):
			A							  =      """cpu"""  # ensure determinism for the device-dependent torch.Generator
			A							  =      self.get_dummy_components()
			A							  =      StableDiffusionXLPipeline(**_lowerCAmelCase      )
			A							  =      sd_pipe.to(_lowerCAmelCase      )
			sd_pipe.set_progress_bar_config(disable=_lowerCAmelCase      )

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

			assert image.shape == (1, 64, 64, 3)
			A							  =      np.array([0.5_873, 0.6_128, 0.4_797, 0.5_122, 0.5_674, 0.4_639, 0.5_227, 0.5_149, 0.4_747]      )

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







		def        A	(self						:      List[Any]      ):
			A							  =      self.get_dummy_components()
			A							  =      StableDiffusionXLPipeline(**_lowerCAmelCase      )
			A							  =      sd_pipe.to(_lowerCAmelCase      )
			A							  =      sd_pipe.to(_lowerCAmelCase      )
			sd_pipe.set_progress_bar_config(disable=_lowerCAmelCase      )

			# forward without prompt embeds
			A							  =      self.get_dummy_inputs(_lowerCAmelCase      )
			A							  =      3 * ["""this is a negative prompt"""]
			A							  =      negative_prompt
			A							  =      3 * [inputs["""prompt"""]]

			A							  =      sd_pipe(**_lowerCAmelCase      )
			A							  =      output.images[0, -3:, -3:, -1]

			# forward with prompt embeds
			A							  =      self.get_dummy_inputs(_lowerCAmelCase      )
			A							  =      3 * ["""this is a negative prompt"""]
			A							  =      3 * [inputs.pop("""prompt"""      )]

			(
			    (
			    A    
			)  ,    (
			    A    
			)  ,    (
			    A    
			)  ,    (
			    A    
			)  ,        
			)							  =      sd_pipe.encode_prompt(_lowerCAmelCase      ,      negative_prompt=_lowerCAmelCase      )

			A							  =      sd_pipe(
			    **_lowerCAmelCase      ,      prompt_embeds=_lowerCAmelCase      ,      negative_prompt_embeds=_lowerCAmelCase      ,      pooled_prompt_embeds=_lowerCAmelCase      ,      negative_pooled_prompt_embeds=_lowerCAmelCase      ,      )
			A							  =      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







		def        A	(self						:      List[Any]      ):
			super().test_attention_slicing_forward_pass(expected_max_diff=3e-3      )







		def        A	(self						:      Tuple      ):
			super().test_inference_batch_single_identical(expected_max_diff=3e-3      )






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







		'''simple docstring'''







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







		def        A	(self						:      Optional[int]      ,      _lowerCAmelCase						:      Dict      ,      _lowerCAmelCase						:      List[str]="cpu"      ,      _lowerCAmelCase						:      Union[str, Any]=torch.floataa      ,      _lowerCAmelCase						:      Optional[int]=0      ):
			A							  =      torch.Generator(device=_lowerCAmelCase      ).manual_seed(_lowerCAmelCase      )
			A							  =      np.random.RandomState(_lowerCAmelCase      ).standard_normal((1, 4, 64, 64)      )
			A							  =      torch.from_numpy(_lowerCAmelCase      ).to(device=_lowerCAmelCase      ,      dtype=_lowerCAmelCase      )
			A							  =      {
			    """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						:      Dict      ):
			A							  =      DiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-base"""      )
			pipe.to(_lowerCAmelCase      )
			pipe.set_progress_bar_config(disable=_lowerCAmelCase      )

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

			assert image.shape == (1, 512, 512, 3)
			A							  =      np.array([0.49_493, 0.47_896, 0.40_798, 0.54_214, 0.53_212, 0.48_202, 0.47_656, 0.46_329, 0.48_506]      )
			assert np.abs(image_slice - expected_slice      ).max() < 7e-3








'''simple docstring'''


import torch

from diffusers import KDPMaAncestralDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest



class 							__UpperCAmelCase  (       A__						):







		'''simple docstring'''







		__lowerCAmelCase 				=    (KDPMaAncestralDiscreteScheduler,)
		__lowerCAmelCase 				=    10







		def        A     (self						:      Any      ,      **_lowerCAmelCase						:      List[Any]      ):
			A							  =      {
			    """num_train_timesteps""": 1100,
			    """beta_start""": 0.0_001,
			    """beta_end""": 0.02,
			    """beta_schedule""": """linear""",
			}

			config.update(**_lowerCAmelCase      )
			return config







		def        A     (self						:      Dict      ):
			for timesteps in [10, 50, 100, 1000]:
				self.check_over_configs(num_train_timesteps=_lowerCAmelCase      )







		def        A     (self						:      Optional[int]      ):
			for beta_start, beta_end in zip([0.00_001, 0.0_001, 0.001]      ,      [0.0_002, 0.002, 0.02]      ):
				self.check_over_configs(beta_start=_lowerCAmelCase      ,      beta_end=_lowerCAmelCase      )







		def        A     (self						:      Tuple      ):
			for schedule in ["linear", "scaled_linear"]:
				self.check_over_configs(beta_schedule=_lowerCAmelCase      )







		def        A     (self						:      Dict      ):
			if torch_device == "mps":
				return
			A							  =      self.scheduler_classes[0]
			A							  =      self.get_scheduler_config()
			A							  =      scheduler_class(**_lowerCAmelCase      )

			scheduler.set_timesteps(self.num_inference_steps      )

			A							  =      torch.manual_seed(0      )

			A							  =      self.dummy_model()
			A							  =      self.dummy_sample_deter * scheduler.init_noise_sigma
			A							  =      sample.to(_lowerCAmelCase      )

			for i, t in enumerate(scheduler.timesteps      ):
				A							  =      scheduler.scale_model_input(_lowerCAmelCase      ,      _lowerCAmelCase      )

				A							  =      model(_lowerCAmelCase      ,      _lowerCAmelCase      )

				A							  =      scheduler.step(_lowerCAmelCase      ,      _lowerCAmelCase      ,      _lowerCAmelCase      ,      generator=_lowerCAmelCase      )
				A							  =      output.prev_sample

			A							  =      torch.sum(torch.abs(_lowerCAmelCase      )      )
			A							  =      torch.mean(torch.abs(_lowerCAmelCase      )      )

			assert abs(result_sum.item() - 13_849.3_877      ) < 1e-2
			assert abs(result_mean.item() - 18.0_331      ) < 5e-3







		def        A     (self						:      Union[str, Any]      ):
			for prediction_type in ["epsilon", "v_prediction"]:
				self.check_over_configs(prediction_type=_lowerCAmelCase      )







		def        A     (self						:      Dict      ):
			if torch_device == "mps":
				return
			A							  =      self.scheduler_classes[0]
			A							  =      self.get_scheduler_config(prediction_type="""v_prediction"""      )
			A							  =      scheduler_class(**_lowerCAmelCase      )

			scheduler.set_timesteps(self.num_inference_steps      )

			A							  =      self.dummy_model()
			A							  =      self.dummy_sample_deter * scheduler.init_noise_sigma
			A							  =      sample.to(_lowerCAmelCase      )

			A							  =      torch.manual_seed(0      )

			for i, t in enumerate(scheduler.timesteps      ):
				A							  =      scheduler.scale_model_input(_lowerCAmelCase      ,      _lowerCAmelCase      )

				A							  =      model(_lowerCAmelCase      ,      _lowerCAmelCase      )

				A							  =      scheduler.step(_lowerCAmelCase      ,      _lowerCAmelCase      ,      _lowerCAmelCase      ,      generator=_lowerCAmelCase      )
				A							  =      output.prev_sample

			A							  =      torch.sum(torch.abs(_lowerCAmelCase      )      )
			A							  =      torch.mean(torch.abs(_lowerCAmelCase      )      )

			assert abs(result_sum.item() - 328.9_970      ) < 1e-2
			assert abs(result_mean.item() - 0.4_284      ) < 1e-3







		def        A     (self						:      Optional[int]      ):
			if torch_device == "mps":
				return
			A							  =      self.scheduler_classes[0]
			A							  =      self.get_scheduler_config()
			A							  =      scheduler_class(**_lowerCAmelCase      )

			scheduler.set_timesteps(self.num_inference_steps      ,      device=_lowerCAmelCase      )
			A							  =      torch.manual_seed(0      )

			A							  =      self.dummy_model()
			A							  =      self.dummy_sample_deter.to(_lowerCAmelCase      ) * scheduler.init_noise_sigma

			for t in scheduler.timesteps:
				A							  =      scheduler.scale_model_input(_lowerCAmelCase      ,      _lowerCAmelCase      )

				A							  =      model(_lowerCAmelCase      ,      _lowerCAmelCase      )

				A							  =      scheduler.step(_lowerCAmelCase      ,      _lowerCAmelCase      ,      _lowerCAmelCase      ,      generator=_lowerCAmelCase      )
				A							  =      output.prev_sample

			A							  =      torch.sum(torch.abs(_lowerCAmelCase      )      )
			A							  =      torch.mean(torch.abs(_lowerCAmelCase      )      )

			assert abs(result_sum.item() - 13_849.3_818      ) < 1e-1
			assert abs(result_mean.item() - 18.0_331      ) < 1e-3

