




import tempfile
import unittest

import numpy as np

from diffusers import (
    DDIMScheduler,
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    OnnxStableDiffusionPipeline,
    PNDMScheduler,
)
from diffusers.utils.testing_utils import is_onnx_available, nightly, require_onnxruntime, require_torch_gpu

from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin


if is_onnx_available():
  import onnxruntime as ort
class 			_UpperCamelCase      (				lowerCAmelCase	,       unittest.TestCase		):
      UpperCAmelCase_        =		"""hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline"""
      def 	UpperCAmelCase_						(			self   :Optional[Any] ,      lowerCamelCase   :Tuple=0      )     ->			Optional[Any]:
       UpperCAmelCase__							 =						np.random.RandomState(lowerCamelCase      )
       UpperCAmelCase__							 =						{
           "prompt": "A painting of a squirrel eating a burger",
           "generator": generator,
           "num_inference_steps": 2,
           "guidance_scale": 7.5,
           "output_type": "numpy",
       }
       return inputs
      def 	UpperCAmelCase_						(			self   :List[Any]      )     ->			List[Any]:
       UpperCAmelCase__							 =						OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint ,      provider="CPUExecutionProvider"      )
       pipe.set_progress_bar_config(disable=lowerCamelCase      )

       UpperCAmelCase__							 =						self.get_dummy_inputs()
       UpperCAmelCase__							 =						pipe(**lowerCamelCase      ).images
       UpperCAmelCase__							 =						image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       UpperCAmelCase__							 =						np.array([0.6_50_72, 0.5_84_92, 0.4_82_19, 0.5_55_21, 0.5_31_80, 0.5_59_39, 0.5_06_97, 0.3_98_00, 0.4_64_55]      )

       assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1e-2
      def 	UpperCAmelCase_						(			self   :str      )     ->			Union[str, Any]:
       UpperCAmelCase__							 =						OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint ,      provider="CPUExecutionProvider"      )
       UpperCAmelCase__							 =						PNDMScheduler.from_config(pipe.scheduler.config ,      skip_prk_steps=lowerCamelCase      )
       pipe.set_progress_bar_config(disable=lowerCamelCase      )

       UpperCAmelCase__							 =						self.get_dummy_inputs()
       UpperCAmelCase__							 =						pipe(**lowerCamelCase      ).images
       UpperCAmelCase__							 =						image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       UpperCAmelCase__							 =						np.array([0.6_58_63, 0.5_94_25, 0.4_93_26, 0.5_63_13, 0.5_38_75, 0.5_66_27, 0.5_10_65, 0.3_97_77, 0.4_63_30]      )

       assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1e-2
      def 	UpperCAmelCase_						(			self   :Tuple      )     ->			Tuple:
       UpperCAmelCase__							 =						OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint ,      provider="CPUExecutionProvider"      )
       UpperCAmelCase__							 =						LMSDiscreteScheduler.from_config(pipe.scheduler.config      )
       pipe.set_progress_bar_config(disable=lowerCamelCase      )

       UpperCAmelCase__							 =						self.get_dummy_inputs()
       UpperCAmelCase__							 =						pipe(**lowerCamelCase      ).images
       UpperCAmelCase__							 =						image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       UpperCAmelCase__							 =						np.array([0.5_37_55, 0.6_07_86, 0.4_74_02, 0.4_94_88, 0.5_18_69, 0.4_98_19, 0.4_79_85, 0.3_89_57, 0.4_42_79]      )

       assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1e-2
      def 	UpperCAmelCase_						(			self   :int      )     ->			str:
       UpperCAmelCase__							 =						OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint ,      provider="CPUExecutionProvider"      )
       UpperCAmelCase__							 =						EulerDiscreteScheduler.from_config(pipe.scheduler.config      )
       pipe.set_progress_bar_config(disable=lowerCamelCase      )

       UpperCAmelCase__							 =						self.get_dummy_inputs()
       UpperCAmelCase__							 =						pipe(**lowerCamelCase      ).images
       UpperCAmelCase__							 =						image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       UpperCAmelCase__							 =						np.array([0.5_37_55, 0.6_07_86, 0.4_74_02, 0.4_94_88, 0.5_18_69, 0.4_98_19, 0.4_79_85, 0.3_89_57, 0.4_42_79]      )

       assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1e-2
      def 	UpperCAmelCase_						(			self   :List[str]      )     ->			List[str]:
       UpperCAmelCase__							 =						OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint ,      provider="CPUExecutionProvider"      )
       UpperCAmelCase__							 =						EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config      )
       pipe.set_progress_bar_config(disable=lowerCamelCase      )

       UpperCAmelCase__							 =						self.get_dummy_inputs()
       UpperCAmelCase__							 =						pipe(**lowerCamelCase      ).images
       UpperCAmelCase__							 =						image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       UpperCAmelCase__							 =						np.array([0.5_38_17, 0.6_08_12, 0.4_73_84, 0.4_95_30, 0.5_18_94, 0.4_98_14, 0.4_79_84, 0.3_89_58, 0.4_42_71]      )

       assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1e-2
      def 	UpperCAmelCase_						(			self   :str      )     ->			Optional[int]:
       UpperCAmelCase__							 =						OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint ,      provider="CPUExecutionProvider"      )
       UpperCAmelCase__							 =						DPMSolverMultistepScheduler.from_config(pipe.scheduler.config      )
       pipe.set_progress_bar_config(disable=lowerCamelCase      )

       UpperCAmelCase__							 =						self.get_dummy_inputs()
       UpperCAmelCase__							 =						pipe(**lowerCamelCase      ).images
       UpperCAmelCase__							 =						image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       UpperCAmelCase__							 =						np.array([0.5_38_95, 0.6_08_08, 0.4_79_33, 0.4_96_08, 0.5_18_86, 0.4_99_50, 0.4_80_53, 0.3_89_57, 0.4_42_00]      )

       assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1e-2
      def 	UpperCAmelCase_						(			self   :Optional[int]      )     ->			int:
       UpperCAmelCase__							 =						OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint ,      provider="CPUExecutionProvider"      )
       pipe.set_progress_bar_config(disable=lowerCamelCase      )

       UpperCAmelCase__							 =						self.get_dummy_inputs()
       UpperCAmelCase__							 =						3 * [inputs["prompt"]]

       # forward
       UpperCAmelCase__							 =						pipe(**lowerCamelCase      )
       UpperCAmelCase__							 =						output.images[0, -3:, -3:, -1]

       UpperCAmelCase__							 =						self.get_dummy_inputs()
       UpperCAmelCase__							 =						3 * [inputs.pop("prompt"      )]

       UpperCAmelCase__							 =						pipe.tokenizer(
           lowerCamelCase ,      padding="max_length" ,      max_length=pipe.tokenizer.model_max_length ,      truncation=lowerCamelCase ,      return_tensors="np" ,      )
       UpperCAmelCase__							 =						text_inputs["input_ids"]

       UpperCAmelCase__							 =						pipe.text_encoder(input_ids=text_inputs.astype(np.intaa      )      )[0]

       UpperCAmelCase__							 =						prompt_embeds

       # forward
       UpperCAmelCase__							 =						pipe(**lowerCamelCase      )
       UpperCAmelCase__							 =						output.images[0, -3:, -3:, -1]

       assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()      ).max() < 1e-4







      def 	UpperCAmelCase_						(			self   :Optional[int]      )     ->			Dict:
       UpperCAmelCase__							 =						OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint ,      provider="CPUExecutionProvider"      )
       pipe.set_progress_bar_config(disable=lowerCamelCase      )

       UpperCAmelCase__							 =						self.get_dummy_inputs()
       UpperCAmelCase__							 =						3 * ["this is a negative prompt"]
       UpperCAmelCase__							 =						negative_prompt
       UpperCAmelCase__							 =						3 * [inputs["prompt"]]

       # forward
       UpperCAmelCase__							 =						pipe(**lowerCamelCase      )
       UpperCAmelCase__							 =						output.images[0, -3:, -3:, -1]

       UpperCAmelCase__							 =						self.get_dummy_inputs()
       UpperCAmelCase__							 =						3 * [inputs.pop("prompt"      )]

       UpperCAmelCase__							 =						[]
       for p in [prompt, negative_prompt]:
        UpperCAmelCase__							 =						pipe.tokenizer(
            lowerCamelCase ,      padding="max_length" ,      max_length=pipe.tokenizer.model_max_length ,      truncation=lowerCamelCase ,      return_tensors="np" ,      )
        UpperCAmelCase__							 =						text_inputs["input_ids"]

        embeds.append(pipe.text_encoder(input_ids=text_inputs.astype(np.intaa      )      )[0]      )

       UpperCAmelCase__ , UpperCAmelCase__							 =						embeds

       # forward
       UpperCAmelCase__							 =						pipe(**lowerCamelCase      )
       UpperCAmelCase__							 =						output.images[0, -3:, -3:, -1]

       assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()      ).max() < 1e-4



@nightly
@require_onnxruntime
@require_torch_gpu
class 			_UpperCamelCase      (				unittest.TestCase		):
      @property
      def 	UpperCAmelCase_						(			self   :Optional[int]      )     ->			int:
       return (
           "CUDAExecutionProvider",
           {
               "gpu_mem_limit": "15000000000",  # 15GB
               "arena_extend_strategy": "kSameAsRequested",
           },
       )
      @property
      def 	UpperCAmelCase_						(			self   :List[Any]      )     ->			Union[str, Any]:
       UpperCAmelCase__							 =						ort.SessionOptions()
       UpperCAmelCase__							 =						False
       return options
      def 	UpperCAmelCase_						(			self   :Optional[int]      )     ->			Optional[int]:
       # using the PNDM scheduler by default
       UpperCAmelCase__							 =						OnnxStableDiffusionPipeline.from_pretrained(
           "CompVis/stable-diffusion-v1-4" ,      revision="onnx" ,      safety_checker=lowerCamelCase ,      feature_extractor=lowerCamelCase ,      provider=self.gpu_provider ,      sess_options=self.gpu_options ,      )
       sd_pipe.set_progress_bar_config(disable=lowerCamelCase      )

       UpperCAmelCase__							 =						"A painting of a squirrel eating a burger"
       np.random.seed(0      )
       UpperCAmelCase__							 =						sd_pipe([prompt] ,      guidance_scale=6.0 ,      num_inference_steps=10 ,      output_type="np"      )
       UpperCAmelCase__							 =						output.images

       UpperCAmelCase__							 =						image[0, -3:, -3:, -1]

       assert image.shape == (1, 512, 512, 3)
       UpperCAmelCase__							 =						np.array([0.04_52, 0.03_90, 0.00_87, 0.03_50, 0.06_17, 0.03_64, 0.05_44, 0.05_23, 0.07_20]      )

       assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1e-3
      def 	UpperCAmelCase_						(			self   :Optional[Any]      )     ->			Dict:
       UpperCAmelCase__							 =						DDIMScheduler.from_pretrained(
           "runwayml/stable-diffusion-v1-5" ,      subfolder="scheduler" ,      revision="onnx"      )
       UpperCAmelCase__							 =						OnnxStableDiffusionPipeline.from_pretrained(
           "runwayml/stable-diffusion-v1-5" ,      revision="onnx" ,      scheduler=lowerCamelCase ,      safety_checker=lowerCamelCase ,      feature_extractor=lowerCamelCase ,      provider=self.gpu_provider ,      sess_options=self.gpu_options ,      )
       sd_pipe.set_progress_bar_config(disable=lowerCamelCase      )

       UpperCAmelCase__							 =						"open neural network exchange"
       UpperCAmelCase__							 =						np.random.RandomState(0      )
       UpperCAmelCase__							 =						sd_pipe([prompt] ,      guidance_scale=7.5 ,      num_inference_steps=10 ,      generator=lowerCamelCase ,      output_type="np"      )
       UpperCAmelCase__							 =						output.images
       UpperCAmelCase__							 =						image[0, -3:, -3:, -1]

       assert image.shape == (1, 512, 512, 3)
       UpperCAmelCase__							 =						np.array([0.28_67, 0.19_74, 0.14_81, 0.72_94, 0.72_51, 0.66_67, 0.41_94, 0.56_42, 0.64_86]      )

       assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1e-3
      def 	UpperCAmelCase_						(			self   :List[Any]      )     ->			List[Any]:
       UpperCAmelCase__							 =						LMSDiscreteScheduler.from_pretrained(
           "runwayml/stable-diffusion-v1-5" ,      subfolder="scheduler" ,      revision="onnx"      )
       UpperCAmelCase__							 =						OnnxStableDiffusionPipeline.from_pretrained(
           "runwayml/stable-diffusion-v1-5" ,      revision="onnx" ,      scheduler=lowerCamelCase ,      safety_checker=lowerCamelCase ,      feature_extractor=lowerCamelCase ,      provider=self.gpu_provider ,      sess_options=self.gpu_options ,      )
       sd_pipe.set_progress_bar_config(disable=lowerCamelCase      )

       UpperCAmelCase__							 =						"open neural network exchange"
       UpperCAmelCase__							 =						np.random.RandomState(0      )
       UpperCAmelCase__							 =						sd_pipe([prompt] ,      guidance_scale=7.5 ,      num_inference_steps=10 ,      generator=lowerCamelCase ,      output_type="np"      )
       UpperCAmelCase__							 =						output.images
       UpperCAmelCase__							 =						image[0, -3:, -3:, -1]

       assert image.shape == (1, 512, 512, 3)
       UpperCAmelCase__							 =						np.array([0.23_06, 0.19_59, 0.15_93, 0.65_49, 0.63_94, 0.54_08, 0.50_65, 0.60_10, 0.61_61]      )

       assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1e-3
      def 	UpperCAmelCase_						(			self   :List[Any]      )     ->			str:
       UpperCAmelCase__							 =						0

       def test_callback_fn(lowerCamelCase   :int ,      lowerCamelCase   :int ,      lowerCamelCase   :np.ndarray      ) -> None:
        UpperCAmelCase__							 =						True
        nonlocal number_of_steps
        number_of_steps += 1
        if step == 0:
         assert latents.shape == (1, 4, 64, 64)
         UpperCAmelCase__							 =						latents[0, -3:, -3:, -1]
         UpperCAmelCase__							 =						np.array(
             [-0.67_72, -0.38_35, -1.24_56, 0.19_05, -1.09_74, 0.69_67, -1.93_53, 0.01_78, 1.01_67]      )

         assert np.abs(latents_slice.flatten() - expected_slice      ).max() < 1e-3
        elif step == 5:
         assert latents.shape == (1, 4, 64, 64)
         UpperCAmelCase__							 =						latents[0, -3:, -3:, -1]
         UpperCAmelCase__							 =						np.array(
             [-0.33_51, 0.22_41, -0.18_37, -0.23_25, -0.65_77, 0.33_93, -0.02_41, 0.58_99, 1.38_75]      )

         assert np.abs(latents_slice.flatten() - expected_slice      ).max() < 1e-3

       UpperCAmelCase__							 =						False

       UpperCAmelCase__							 =						OnnxStableDiffusionPipeline.from_pretrained(
           "runwayml/stable-diffusion-v1-5" ,      revision="onnx" ,      safety_checker=lowerCamelCase ,      feature_extractor=lowerCamelCase ,      provider=self.gpu_provider ,      sess_options=self.gpu_options ,      )
       pipe.set_progress_bar_config(disable=lowerCamelCase      )

       UpperCAmelCase__							 =						"Andromeda galaxy in a bottle"

       UpperCAmelCase__							 =						np.random.RandomState(0      )
       pipe(
           prompt=lowerCamelCase ,      num_inference_steps=5 ,      guidance_scale=7.5 ,      generator=lowerCamelCase ,      callback=lowerCamelCase ,      callback_steps=1 ,      )
       assert test_callback_fn.has_been_called
       assert number_of_steps == 6







      def 	UpperCAmelCase_						(			self   :Optional[int]      )     ->			Any:
       UpperCAmelCase__							 =						OnnxStableDiffusionPipeline.from_pretrained(
           "runwayml/stable-diffusion-v1-5" ,      revision="onnx" ,      safety_checker=lowerCamelCase ,      feature_extractor=lowerCamelCase ,      provider=self.gpu_provider ,      sess_options=self.gpu_options ,      )
       assert isinstance(lowerCamelCase ,      lowerCamelCase      )
       assert pipe.safety_checker is None

       UpperCAmelCase__							 =						pipe("example prompt" ,      num_inference_steps=2      ).images[0]
       assert image is not None

       # check that there's no error when saving a pipeline with one of the models being None
       with tempfile.TemporaryDirectory() as tmpdirname:
        pipe.save_pretrained(lowerCamelCase      )
        UpperCAmelCase__							 =						OnnxStableDiffusionPipeline.from_pretrained(lowerCamelCase      )

       # sanity check that the pipeline still works
       assert pipe.safety_checker is None
       UpperCAmelCase__							 =						pipe("example prompt" ,      num_inference_steps=2      ).images[0]
       assert image is not None











import random
import unittest

import torch

from diffusers import IFInpaintingSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin
@skip_mps
class 			_UpperCamelCase      (				lowerCAmelCase	,       lowerCAmelCase	,       unittest.TestCase		):
      UpperCAmelCase_        =		IFInpaintingSuperResolutionPipeline
      UpperCAmelCase_        =		TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"""width""", """height"""}
      UpperCAmelCase_        =		TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"""original_image"""}		)
      UpperCAmelCase_        =		PipelineTesterMixin.required_optional_params - {"""latents"""}
      def 	UpperCAmelCase_						(			self   :str      )     ->			Optional[Any]:
       return self._get_superresolution_dummy_components()
      def 	UpperCAmelCase_						(			self   :List[str] ,      lowerCamelCase   :Dict ,      lowerCamelCase   :Optional[Any]=0      )     ->			int:
       if str(lowerCamelCase      ).startswith("mps"      ):
        UpperCAmelCase__							 =						torch.manual_seed(lowerCamelCase      )
       else:
        UpperCAmelCase__							 =						torch.Generator(device=lowerCamelCase      ).manual_seed(lowerCamelCase      )

       UpperCAmelCase__							 =						floats_tensor((1, 3, 16, 16) ,      rng=random.Random(lowerCamelCase      )      ).to(lowerCamelCase      )
       UpperCAmelCase__							 =						floats_tensor((1, 3, 32, 32) ,      rng=random.Random(lowerCamelCase      )      ).to(lowerCamelCase      )
       UpperCAmelCase__							 =						floats_tensor((1, 3, 32, 32) ,      rng=random.Random(lowerCamelCase      )      ).to(lowerCamelCase      )

       UpperCAmelCase__							 =						{
           "prompt": "A painting of a squirrel eating a burger",
           "image": image,
           "original_image": original_image,
           "mask_image": mask_image,
           "generator": generator,
           "num_inference_steps": 2,
           "output_type": "numpy",
       }

       return inputs
      @unittest.skipIf(
          torch_device != "cuda" or not is_xformers_available() ,      reason="XFormers attention is only available with CUDA and `xformers` installed" ,      )
      def 	UpperCAmelCase_						(			self   :Union[str, Any]      )     ->			Dict:
       self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3      )
      def 	UpperCAmelCase_						(			self   :List[str]      )     ->			Dict:
       self._test_save_load_optional_components()
      @unittest.skipIf(torch_device != "cuda" ,      reason="float16 requires CUDA"      )
      def 	UpperCAmelCase_						(			self   :Tuple      )     ->			Dict:
       # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
       super().test_save_load_floataa(expected_max_diff=1e-1      )
      def 	UpperCAmelCase_						(			self   :Dict      )     ->			str:
       self._test_attention_slicing_forward_pass(expected_max_diff=1e-2      )
      def 	UpperCAmelCase_						(			self   :Dict      )     ->			str:
       self._test_save_load_local()







      def 	UpperCAmelCase_						(			self   :List[str]      )     ->			List[str]:
       self._test_inference_batch_single_identical(
           expected_max_diff=1e-2 ,      )





