



import gc
import random
import unittest

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

from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel
from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow

from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()





class  lowerCamelCase__	(    UpperCAmelCase_							,     UpperCAmelCase_							,     UpperCAmelCase_							,     unittest.TestCase):
     """simple docstring"""



     _A      =      StableDiffusionInpaintPipeline
     _A      =      TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
     _A      =      TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
     _A      =      frozenset(
         [])  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
     _A      =      frozenset([])







     def     _a   (self     ):






           '''simple docstring'''



           torch.manual_seed(0     )
           lowerCamelCase             =		UNetaDConditionModel(
               block_out_channels=(32, 64)					,      layers_per_block=2					,      sample_size=32					,      in_channels=9					,      out_channels=4					,      down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")					,      up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")					,      cross_attention_dim=32					,      attention_head_dim=(2, 4)					,      use_linear_projection=__a					,      )
           lowerCamelCase             =		PNDMScheduler(skip_prk_steps=__a     )
           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					,      sample_size=1_28					,      )
           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=10_00					,      hidden_act="gelu"					,      projection_dim=5_12					,      )
           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     _a   (self					,      __a					,      __a=0     ):






           '''simple docstring'''



           lowerCamelCase             =		floats_tensor((1, 3, 32, 32)					,      rng=random.Random(__a     )     ).to(__a     )
           lowerCamelCase             =		image.cpu().permute(0					,      2					,      3					,      1     )[0]
           lowerCamelCase             =		Image.fromarray(np.uinta(__a     )     ).convert("RGB"     ).resize((64, 64)     )
           lowerCamelCase             =		Image.fromarray(np.uinta(image + 4     )     ).convert("RGB"     ).resize((64, 64)     )
           if str(__a     ).startswith("mps"     ):
                 lowerCamelCase             =		torch.manual_seed(__a     )
           else:
                 lowerCamelCase             =		torch.Generator(device=__a     ).manual_seed(__a     )
           lowerCamelCase             =		{
               "prompt": "A painting of a squirrel eating a burger",
               "image": init_image,
               "mask_image": mask_image,
               "generator": generator,
               "num_inference_steps": 2,
               "guidance_scale": 6.0,
               "output_type": "numpy",
           }
           return inputs







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		"cpu"  # ensure determinism for the device-dependent torch.Generator
           lowerCamelCase             =		self.get_dummy_components()
           lowerCamelCase             =		StableDiffusionInpaintPipeline(**__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.4727, 0.5735, 0.3941, 0.5446, 0.5926, 0.4394, 0.5062, 0.4654, 0.4476]     )

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







     def     _a   (self     ):






           '''simple docstring'''



           super().test_inference_batch_single_identical(expected_max_diff=3E-3     )





@slow
@require_torch_gpu
class  lowerCamelCase__	(    unittest.TestCase):
     """simple docstring"""



     def     _a   (self     ):






           '''simple docstring'''



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







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		load_image(
               "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
               "/sd2-inpaint/init_image.png"     )
           lowerCamelCase             =		load_image(
               "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png"     )
           lowerCamelCase             =		load_numpy(
               "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint"
               "/yellow_cat_sitting_on_a_park_bench.npy"     )

           lowerCamelCase             =		"stabilityai/stable-diffusion-2-inpainting"
           lowerCamelCase             =		StableDiffusionInpaintPipeline.from_pretrained(__a					,      safety_checker=__a     )
           pipe.to(__a     )
           pipe.set_progress_bar_config(disable=__a     )
           pipe.enable_attention_slicing()

           lowerCamelCase             =		"Face of a yellow cat, high resolution, sitting on a park bench"

           lowerCamelCase             =		torch.manual_seed(0     )
           lowerCamelCase             =		pipe(
               prompt=__a					,      image=__a					,      mask_image=__a					,      generator=__a					,      output_type="np"					,      )
           lowerCamelCase             =		output.images[0]

           assert image.shape == (5_12, 5_12, 3)
           assert np.abs(expected_image - image     ).max() < 9E-3







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		load_image(
               "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
               "/sd2-inpaint/init_image.png"     )
           lowerCamelCase             =		load_image(
               "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png"     )
           lowerCamelCase             =		load_numpy(
               "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint"
               "/yellow_cat_sitting_on_a_park_bench_fp16.npy"     )

           lowerCamelCase             =		"stabilityai/stable-diffusion-2-inpainting"
           lowerCamelCase             =		StableDiffusionInpaintPipeline.from_pretrained(
               __a					,      torch_dtype=torch.floataa					,      safety_checker=__a					,      )
           pipe.to(__a     )
           pipe.set_progress_bar_config(disable=__a     )
           pipe.enable_attention_slicing()

           lowerCamelCase             =		"Face of a yellow cat, high resolution, sitting on a park bench"

           lowerCamelCase             =		torch.manual_seed(0     )
           lowerCamelCase             =		pipe(
               prompt=__a					,      image=__a					,      mask_image=__a					,      generator=__a					,      output_type="np"					,      )
           lowerCamelCase             =		output.images[0]

           assert image.shape == (5_12, 5_12, 3)
           assert np.abs(expected_image - image     ).max() < 5E-1







     def     _a   (self     ):






           '''simple docstring'''



           torch.cuda.empty_cache()
           torch.cuda.reset_max_memory_allocated()
           torch.cuda.reset_peak_memory_stats()

           lowerCamelCase             =		load_image(
               "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
               "/sd2-inpaint/init_image.png"     )
           lowerCamelCase             =		load_image(
               "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png"     )

           lowerCamelCase             =		"stabilityai/stable-diffusion-2-inpainting"
           lowerCamelCase             =		PNDMScheduler.from_pretrained(__a					,      subfolder="scheduler"     )
           lowerCamelCase             =		StableDiffusionInpaintPipeline.from_pretrained(
               __a					,      safety_checker=__a					,      scheduler=__a					,      torch_dtype=torch.floataa					,      )
           pipe.to(__a     )
           pipe.set_progress_bar_config(disable=__a     )
           pipe.enable_attention_slicing(1     )
           pipe.enable_sequential_cpu_offload()

           lowerCamelCase             =		"Face of a yellow cat, high resolution, sitting on a park bench"

           lowerCamelCase             =		torch.manual_seed(0     )
           lowerCamelCase             =		pipe(
               prompt=__a					,      image=__a					,      mask_image=__a					,      generator=__a					,      num_inference_steps=2					,      output_type="np"					,      )

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




import tempfile

import torch

from diffusers import PNDMScheduler

from .test_schedulers import SchedulerCommonTest





class  lowerCamelCase__	(    UpperCAmelCase_):
     """simple docstring"""



     _A      =      (PNDMScheduler,)
     _A      =      (('num_inference_steps', 50),)







     def     _a   (self					,      **__a     ):






           '''simple docstring'''



           lowerCamelCase             =		{
               "num_train_timesteps": 10_00,
               "beta_start": 0.0001,
               "beta_end": 0.02,
               "beta_schedule": "linear",
           }

           config.update(**__a     )
           return config







     def     _a   (self					,      __a=0					,      **__a     ):






           '''simple docstring'''



           lowerCamelCase             =		dict(self.forward_default_kwargs     )
           lowerCamelCase             =		kwargs.pop("num_inference_steps"					,      __a     )
           lowerCamelCase             =		self.dummy_sample
           lowerCamelCase             =		0.1 * sample
           lowerCamelCase             =		[residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]

           for scheduler_class in self.scheduler_classes:
                 lowerCamelCase             =		self.get_scheduler_config(**__a     )
                 lowerCamelCase             =		scheduler_class(**__a     )
                 scheduler.set_timesteps(__a     )
                 # copy over dummy past residuals
                 lowerCamelCase             =		dummy_past_residuals[:]

                 with tempfile.TemporaryDirectory() as tmpdirname:
                       scheduler.save_config(__a     )
                       lowerCamelCase             =		scheduler_class.from_pretrained(__a     )
                       new_scheduler.set_timesteps(__a     )
                       # copy over dummy past residuals
                       lowerCamelCase             =		dummy_past_residuals[:]

                 lowerCamelCase             =		scheduler.step_prk(__a					,      __a					,      __a					,      **__a     ).prev_sample
                 lowerCamelCase             =		new_scheduler.step_prk(__a					,      __a					,      __a					,      **__a     ).prev_sample

                 assert torch.sum(torch.abs(output - new_output     )     ) < 1E-5, "Scheduler outputs are not identical"

                 lowerCamelCase             =		scheduler.step_plms(__a					,      __a					,      __a					,      **__a     ).prev_sample
                 lowerCamelCase             =		new_scheduler.step_plms(__a					,      __a					,      __a					,      **__a     ).prev_sample

                 assert torch.sum(torch.abs(output - new_output     )     ) < 1E-5, "Scheduler outputs are not identical"







     def     _a   (self     ):






           '''simple docstring'''



           pass







     def     _a   (self					,      __a=0					,      **__a     ):






           '''simple docstring'''



           lowerCamelCase             =		dict(self.forward_default_kwargs     )
           lowerCamelCase             =		kwargs.pop("num_inference_steps"					,      __a     )
           lowerCamelCase             =		self.dummy_sample
           lowerCamelCase             =		0.1 * sample
           lowerCamelCase             =		[residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]

           for scheduler_class in self.scheduler_classes:
                 lowerCamelCase             =		self.get_scheduler_config()
                 lowerCamelCase             =		scheduler_class(**__a     )
                 scheduler.set_timesteps(__a     )

                 # copy over dummy past residuals (must be after setting timesteps)
                 lowerCamelCase             =		dummy_past_residuals[:]

                 with tempfile.TemporaryDirectory() as tmpdirname:
                       scheduler.save_config(__a     )
                       lowerCamelCase             =		scheduler_class.from_pretrained(__a     )
                       # copy over dummy past residuals
                       new_scheduler.set_timesteps(__a     )

                       # copy over dummy past residual (must be after setting timesteps)
                       lowerCamelCase             =		dummy_past_residuals[:]

                 lowerCamelCase             =		scheduler.step_prk(__a					,      __a					,      __a					,      **__a     ).prev_sample
                 lowerCamelCase             =		new_scheduler.step_prk(__a					,      __a					,      __a					,      **__a     ).prev_sample

                 assert torch.sum(torch.abs(output - new_output     )     ) < 1E-5, "Scheduler outputs are not identical"

                 lowerCamelCase             =		scheduler.step_plms(__a					,      __a					,      __a					,      **__a     ).prev_sample
                 lowerCamelCase             =		new_scheduler.step_plms(__a					,      __a					,      __a					,      **__a     ).prev_sample

                 assert torch.sum(torch.abs(output - new_output     )     ) < 1E-5, "Scheduler outputs are not identical"







     def     _a   (self					,      **__a     ):






           '''simple docstring'''



           lowerCamelCase             =		self.scheduler_classes[0]
           lowerCamelCase             =		self.get_scheduler_config(**__a     )
           lowerCamelCase             =		scheduler_class(**__a     )

           lowerCamelCase             =		10
           lowerCamelCase             =		self.dummy_model()
           lowerCamelCase             =		self.dummy_sample_deter
           scheduler.set_timesteps(__a     )

           for i, t in enumerate(scheduler.prk_timesteps     ):
                 lowerCamelCase             =		model(__a					,      __a     )
                 lowerCamelCase             =		scheduler.step_prk(__a					,      __a					,      __a     ).prev_sample

           for i, t in enumerate(scheduler.plms_timesteps     ):
                 lowerCamelCase             =		model(__a					,      __a     )
                 lowerCamelCase             =		scheduler.step_plms(__a					,      __a					,      __a     ).prev_sample

           return sample







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		dict(self.forward_default_kwargs     )

           lowerCamelCase             =		kwargs.pop("num_inference_steps"					,      __a     )

           for scheduler_class in self.scheduler_classes:
                 lowerCamelCase             =		self.get_scheduler_config()
                 lowerCamelCase             =		scheduler_class(**__a     )

                 lowerCamelCase             =		self.dummy_sample
                 lowerCamelCase             =		0.1 * sample

                 if num_inference_steps is not None and hasattr(__a					,      "set_timesteps"     ):
                       scheduler.set_timesteps(__a     )
                 elif num_inference_steps is not None and not hasattr(__a					,      "set_timesteps"     ):
                       lowerCamelCase             =		num_inference_steps

                 # copy over dummy past residuals (must be done after set_timesteps)
                 lowerCamelCase             =		[residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]
                 lowerCamelCase             =		dummy_past_residuals[:]

                 lowerCamelCase             =		scheduler.step_prk(__a					,      0					,      __a					,      **__a     ).prev_sample
                 lowerCamelCase             =		scheduler.step_prk(__a					,      1					,      __a					,      **__a     ).prev_sample

                 self.assertEqual(output_a.shape					,      sample.shape     )
                 self.assertEqual(output_a.shape					,      output_a.shape     )

                 lowerCamelCase             =		scheduler.step_plms(__a					,      0					,      __a					,      **__a     ).prev_sample
                 lowerCamelCase             =		scheduler.step_plms(__a					,      1					,      __a					,      **__a     ).prev_sample

                 self.assertEqual(output_a.shape					,      sample.shape     )
                 self.assertEqual(output_a.shape					,      output_a.shape     )







     def     _a   (self     ):






           '''simple docstring'''



           for timesteps in [1_00, 10_00]:
                 self.check_over_configs(num_train_timesteps=__a     )







     def     _a   (self     ):






           '''simple docstring'''



           for steps_offset in [0, 1]:
                 self.check_over_configs(steps_offset=__a     )

           lowerCamelCase             =		self.scheduler_classes[0]
           lowerCamelCase             =		self.get_scheduler_config(steps_offset=1     )
           lowerCamelCase             =		scheduler_class(**__a     )
           scheduler.set_timesteps(10     )
           assert torch.equal(
               scheduler.timesteps					,      torch.LongTensor(
                   [9_01, 8_51, 8_51, 8_01, 8_01, 7_51, 7_51, 7_01, 7_01, 6_51, 6_51, 6_01, 6_01, 5_01, 4_01, 3_01, 2_01, 1_01, 1]     )					,      )







     def     _a   (self     ):






           '''simple docstring'''



           for beta_start, beta_end in zip([0.0001, 0.001]					,      [0.002, 0.02]     ):
                 self.check_over_configs(beta_start=__a					,      beta_end=__a     )







     def     _a   (self     ):






           '''simple docstring'''



           for schedule in ["linear", "squaredcos_cap_v2"]:
                 self.check_over_configs(beta_schedule=__a     )







     def     _a   (self     ):






           '''simple docstring'''



           for prediction_type in ["epsilon", "v_prediction"]:
                 self.check_over_configs(prediction_type=__a     )







     def     _a   (self     ):






           '''simple docstring'''



           for t in [1, 5, 10]:
                 self.check_over_forward(time_step=__a     )







     def     _a   (self     ):






           '''simple docstring'''



           for t, num_inference_steps in zip([1, 5, 10]					,      [10, 50, 1_00]     ):
                 self.check_over_forward(num_inference_steps=__a     )







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		27

           for scheduler_class in self.scheduler_classes:
                 lowerCamelCase             =		self.dummy_sample
                 lowerCamelCase             =		0.1 * sample

                 lowerCamelCase             =		self.get_scheduler_config()
                 lowerCamelCase             =		scheduler_class(**__a     )

                 scheduler.set_timesteps(__a     )

                 # before power of 3 fix, would error on first step, so we only need to do two
                 for i, t in enumerate(scheduler.prk_timesteps[:2]     ):
                       lowerCamelCase             =		scheduler.step_prk(__a					,      __a					,      __a     ).prev_sample







     def     _a   (self     ):






           '''simple docstring'''



           with self.assertRaises(__a     ):
                 lowerCamelCase             =		self.scheduler_classes[0]
                 lowerCamelCase             =		self.get_scheduler_config()
                 lowerCamelCase             =		scheduler_class(**__a     )

                 scheduler.step_plms(self.dummy_sample					,      1					,      self.dummy_sample     ).prev_sample







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		self.full_loop()
           lowerCamelCase             =		torch.sum(torch.abs(__a     )     )
           lowerCamelCase             =		torch.mean(torch.abs(__a     )     )

           assert abs(result_sum.item() - 198.1318     ) < 1E-2
           assert abs(result_mean.item() - 0.2580     ) < 1E-3







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		self.full_loop(prediction_type="v_prediction"     )
           lowerCamelCase             =		torch.sum(torch.abs(__a     )     )
           lowerCamelCase             =		torch.mean(torch.abs(__a     )     )

           assert abs(result_sum.item() - 67.3986     ) < 1E-2
           assert abs(result_mean.item() - 0.0878     ) < 1E-3







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		self.full_loop(set_alpha_to_one=__a					,      beta_start=0.01     )
           lowerCamelCase             =		torch.sum(torch.abs(__a     )     )
           lowerCamelCase             =		torch.mean(torch.abs(__a     )     )

           assert abs(result_sum.item() - 230.0399     ) < 1E-2
           assert abs(result_mean.item() - 0.2995     ) < 1E-3







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		self.full_loop(set_alpha_to_one=__a					,      beta_start=0.01     )
           lowerCamelCase             =		torch.sum(torch.abs(__a     )     )
           lowerCamelCase             =		torch.mean(torch.abs(__a     )     )

           assert abs(result_sum.item() - 186.9482     ) < 1E-2
           assert abs(result_mean.item() - 0.2434     ) < 1E-3