

import random
import unittest

import numpy as np

from diffusers import (
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    OnnxStableDiffusionImgaImgPipeline,
    PNDMScheduler,
)
from diffusers.utils import floats_tensor
from diffusers.utils.testing_utils import (
    is_onnx_available,
    load_image,
    nightly,
    require_onnxruntime,
    require_torch_gpu,
)

from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin


if is_onnx_available():
			import onnxruntime as ort






class _UpperCamelCase				(				__A				,						unittest.TestCase ):
			'''simple docstring'''







			lowerCamelCase__							     ='hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline'
			def 				__UpperCamelCase			(      self				:			Any   ,   a				:			Optional[int]=0						)				->							Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							floats_tensor((1, 3, 128, 128)   ,   rng=random.Random(a						)						)
				SCREAMING_SNAKE_CASE :   str					  =							np.random.RandomState(a						)
				SCREAMING_SNAKE_CASE :   int					  =							{
				    "prompt": "A painting of a squirrel eating a burger",
				    "image": image,
				    "generator": generator,
				    "num_inference_steps": 3,
				    "strength": 0.75,
				    "guidance_scale": 7.5,
				    "output_type": "numpy",
				}
				return inputs
			def 				__UpperCamelCase			(      self				:			Optional[Any]						)				->							Any:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   List[str]					  =							OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint   ,   provider="CPUExecutionProvider"						)
				pipe.set_progress_bar_config(disable=a						)

				SCREAMING_SNAKE_CASE :   List[str]					  =							self.get_dummy_inputs()
				SCREAMING_SNAKE_CASE :   Tuple					  =							pipe(**a						).images
				SCREAMING_SNAKE_CASE :   List[Any]					  =							image[0, -3:, -3:, -1].flatten()

				assert image.shape == (1, 128, 128, 3)
				SCREAMING_SNAKE_CASE :   str					  =							np.array([0.6_9643, 0.5_8484, 0.5_0314, 0.5_8760, 0.5_5368, 0.5_9643, 0.5_1529, 0.4_1217, 0.4_9087]						)
				assert np.abs(image_slice - expected_slice						).max() < 1e-1
			def 				__UpperCamelCase			(      self				:			Tuple						)				->							List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   str					  =							OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint   ,   provider="CPUExecutionProvider"						)
				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							PNDMScheduler.from_config(pipe.scheduler.config   ,   skip_prk_steps=a						)
				pipe.set_progress_bar_config(disable=a						)

				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							self.get_dummy_inputs()
				SCREAMING_SNAKE_CASE :   List[Any]					  =							pipe(**a						).images
				SCREAMING_SNAKE_CASE :   Dict					  =							image[0, -3:, -3:, -1]

				assert image.shape == (1, 128, 128, 3)
				SCREAMING_SNAKE_CASE :   List[str]					  =							np.array([0.6_1737, 0.5_4642, 0.5_3183, 0.5_4465, 0.5_2742, 0.6_0525, 0.4_9969, 0.4_0655, 0.4_8154]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-1
			def 				__UpperCamelCase			(      self				:			List[Any]						)				->							Union[str, Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint   ,   provider="CPUExecutionProvider"						)
				SCREAMING_SNAKE_CASE :   Dict					  =							LMSDiscreteScheduler.from_config(pipe.scheduler.config						)
				pipe.set_progress_bar_config(disable=a						)

				# warmup pass to apply optimizations
				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							pipe(**self.get_dummy_inputs()						)

				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							self.get_dummy_inputs()
				SCREAMING_SNAKE_CASE :   List[Any]					  =							pipe(**a						).images
				SCREAMING_SNAKE_CASE :   List[str]					  =							image[0, -3:, -3:, -1]

				assert image.shape == (1, 128, 128, 3)
				SCREAMING_SNAKE_CASE :   Any					  =							np.array([0.5_2761, 0.5_9977, 0.4_9033, 0.4_9619, 0.5_4282, 0.5_0311, 0.4_7600, 0.4_0918, 0.4_5203]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-1
			def 				__UpperCamelCase			(      self				:			Any						)				->							List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint   ,   provider="CPUExecutionProvider"						)
				SCREAMING_SNAKE_CASE :   List[str]					  =							EulerDiscreteScheduler.from_config(pipe.scheduler.config						)
				pipe.set_progress_bar_config(disable=a						)

				SCREAMING_SNAKE_CASE :   List[str]					  =							self.get_dummy_inputs()
				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							pipe(**a						).images
				SCREAMING_SNAKE_CASE :   str					  =							image[0, -3:, -3:, -1]

				assert image.shape == (1, 128, 128, 3)
				SCREAMING_SNAKE_CASE :   Dict					  =							np.array([0.5_2911, 0.6_0004, 0.4_9229, 0.4_9805, 0.5_4502, 0.5_0680, 0.4_7777, 0.4_1028, 0.4_5304]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-1
			def 				__UpperCamelCase			(      self				:			str						)				->							Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Tuple					  =							OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint   ,   provider="CPUExecutionProvider"						)
				SCREAMING_SNAKE_CASE :   Optional[int]					  =							EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config						)
				pipe.set_progress_bar_config(disable=a						)

				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							self.get_dummy_inputs()
				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							pipe(**a						).images
				SCREAMING_SNAKE_CASE :   Tuple					  =							image[0, -3:, -3:, -1]

				assert image.shape == (1, 128, 128, 3)
				SCREAMING_SNAKE_CASE :   Tuple					  =							np.array([0.5_2911, 0.6_0004, 0.4_9229, 0.4_9805, 0.5_4502, 0.5_0680, 0.4_7777, 0.4_1028, 0.4_5304]						)

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






			def 				__UpperCamelCase			(      self				:			Tuple						)				->							List[str]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   int					  =							OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint   ,   provider="CPUExecutionProvider"						)
				SCREAMING_SNAKE_CASE :   str					  =							DPMSolverMultistepScheduler.from_config(pipe.scheduler.config						)
				pipe.set_progress_bar_config(disable=a						)

				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							self.get_dummy_inputs()
				SCREAMING_SNAKE_CASE :   List[str]					  =							pipe(**a						).images
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							image[0, -3:, -3:, -1]

				assert image.shape == (1, 128, 128, 3)
				SCREAMING_SNAKE_CASE :   Optional[int]					  =							np.array([0.6_5331, 0.5_8277, 0.4_8204, 0.5_6059, 0.5_3665, 0.5_6235, 0.5_0969, 0.4_0009, 0.4_6552]						)

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






@nightly
@require_onnxruntime
@require_torch_gpu
class _UpperCamelCase				(				unittest.TestCase ):
			'''simple docstring'''







			@property
			def 				__UpperCamelCase			(      self				:			Tuple						)				->							Optional[int]:

				"""simple docstring"""

				return (
				    "CUDAExecutionProvider",
				    {
				        "gpu_mem_limit": "15000000000",  # 15GB
				        "arena_extend_strategy": "kSameAsRequested",
				    },
				)
			@property
			def 				__UpperCamelCase			(      self				:			Any						)				->							Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Any					  =							ort.SessionOptions()
				SCREAMING_SNAKE_CASE :   str					  =							False
				return options
			def 				__UpperCamelCase			(      self				:			List[Any]						)				->							Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   str					  =							load_image(
				    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
				    "/img2img/sketch-mountains-input.jpg"						)
				SCREAMING_SNAKE_CASE :   Optional[int]					  =							init_image.resize((768, 512)						)
				# using the PNDM scheduler by default
				SCREAMING_SNAKE_CASE :   str					  =							OnnxStableDiffusionImgaImgPipeline.from_pretrained(
				    "CompVis/stable-diffusion-v1-4"   ,   revision="onnx"   ,   safety_checker=a   ,   feature_extractor=a   ,   provider=self.gpu_provider   ,   sess_options=self.gpu_options   ,   )
				pipe.set_progress_bar_config(disable=a						)

				SCREAMING_SNAKE_CASE :   Tuple					  =							"A fantasy landscape, trending on artstation"

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							np.random.RandomState(0						)
				SCREAMING_SNAKE_CASE :   Dict					  =							pipe(
				    prompt=a   ,   image=a   ,   strength=0.75   ,   guidance_scale=7.5   ,   num_inference_steps=10   ,   generator=a   ,   output_type="np"   ,   )
				SCREAMING_SNAKE_CASE :   Any					  =							output.images
				SCREAMING_SNAKE_CASE :   str					  =							images[0, 255:258, 383:386, -1]

				assert images.shape == (1, 512, 768, 3)
				SCREAMING_SNAKE_CASE :   Tuple					  =							np.array([0.4909, 0.5059, 0.5372, 0.4623, 0.4876, 0.5049, 0.4820, 0.4956, 0.5019]						)
				# TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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






			def 				__UpperCamelCase			(      self				:			Optional[int]						)				->							List[str]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							load_image(
				    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
				    "/img2img/sketch-mountains-input.jpg"						)
				SCREAMING_SNAKE_CASE :   Dict					  =							init_image.resize((768, 512)						)
				SCREAMING_SNAKE_CASE :   Any					  =							LMSDiscreteScheduler.from_pretrained(
				    "runwayml/stable-diffusion-v1-5"   ,   subfolder="scheduler"   ,   revision="onnx"						)
				SCREAMING_SNAKE_CASE :   List[str]					  =							OnnxStableDiffusionImgaImgPipeline.from_pretrained(
				    "runwayml/stable-diffusion-v1-5"   ,   revision="onnx"   ,   scheduler=a   ,   safety_checker=a   ,   feature_extractor=a   ,   provider=self.gpu_provider   ,   sess_options=self.gpu_options   ,   )
				pipe.set_progress_bar_config(disable=a						)

				SCREAMING_SNAKE_CASE :   List[str]					  =							"A fantasy landscape, trending on artstation"

				SCREAMING_SNAKE_CASE :   List[Any]					  =							np.random.RandomState(0						)
				SCREAMING_SNAKE_CASE :   Dict					  =							pipe(
				    prompt=a   ,   image=a   ,   strength=0.75   ,   guidance_scale=7.5   ,   num_inference_steps=20   ,   generator=a   ,   output_type="np"   ,   )
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							output.images
				SCREAMING_SNAKE_CASE :   Any					  =							images[0, 255:258, 383:386, -1]

				assert images.shape == (1, 512, 768, 3)
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							np.array([0.8043, 0.926, 0.9581, 0.8119, 0.8954, 0.913, 0.7209, 0.7463, 0.7431]						)
				# TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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


import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()






@slow
@require_torch_gpu
class _UpperCamelCase				(				unittest.TestCase ):
			'''simple docstring'''







			def 				__UpperCamelCase			(      self				:			Dict						)				->							Tuple:

				"""simple docstring"""

				super().tearDown()
				gc.collect()
				torch.cuda.empty_cache()
			def 				__UpperCamelCase			(      self				:			Optional[int]						)				->							int:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							StableDiffusionKDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4"						)
				SCREAMING_SNAKE_CASE :   str					  =							sd_pipe.to(a						)
				sd_pipe.set_progress_bar_config(disable=a						)

				sd_pipe.set_scheduler("sample_euler"						)

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							"A painting of a squirrel eating a burger"
				SCREAMING_SNAKE_CASE :   Any					  =							torch.manual_seed(0						)
				SCREAMING_SNAKE_CASE :   List[Any]					  =							sd_pipe([prompt]   ,   generator=a   ,   guidance_scale=9.0   ,   num_inference_steps=20   ,   output_type="np"						)

				SCREAMING_SNAKE_CASE :   Tuple					  =							output.images

				SCREAMING_SNAKE_CASE :   Any					  =							image[0, -3:, -3:, -1]

				assert image.shape == (1, 512, 512, 3)
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2
			def 				__UpperCamelCase			(      self				:			List[Any]						)				->							Optional[int]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   int					  =							StableDiffusionKDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base"						)
				SCREAMING_SNAKE_CASE :   Tuple					  =							sd_pipe.to(a						)
				sd_pipe.set_progress_bar_config(disable=a						)

				sd_pipe.set_scheduler("sample_euler"						)

				SCREAMING_SNAKE_CASE :   List[str]					  =							"A painting of a squirrel eating a burger"
				SCREAMING_SNAKE_CASE :   List[str]					  =							torch.manual_seed(0						)
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							sd_pipe([prompt]   ,   generator=a   ,   guidance_scale=9.0   ,   num_inference_steps=20   ,   output_type="np"						)

				SCREAMING_SNAKE_CASE :   List[Any]					  =							output.images

				SCREAMING_SNAKE_CASE :   Tuple					  =							image[0, -3:, -3:, -1]

				assert image.shape == (1, 512, 512, 3)
				SCREAMING_SNAKE_CASE :   int					  =							np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112]						)

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






			def 				__UpperCamelCase			(      self				:			Tuple						)				->							List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Dict					  =							StableDiffusionKDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base"						)
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							sd_pipe.to(a						)
				sd_pipe.set_progress_bar_config(disable=a						)

				sd_pipe.set_scheduler("sample_dpmpp_2m"						)

				SCREAMING_SNAKE_CASE :   str					  =							"A painting of a squirrel eating a burger"
				SCREAMING_SNAKE_CASE :   Any					  =							torch.manual_seed(0						)
				SCREAMING_SNAKE_CASE :   str					  =							sd_pipe(
				    [prompt]   ,   generator=a   ,   guidance_scale=7.5   ,   num_inference_steps=15   ,   output_type="np"   ,   use_karras_sigmas=a   ,   )

				SCREAMING_SNAKE_CASE :   str					  =							output.images

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							image[0, -3:, -3:, -1]

				assert image.shape == (1, 512, 512, 3)
				SCREAMING_SNAKE_CASE :   int					  =							np.array(
				    [0.1138_1689, 0.1211_2921, 0.138_9457, 0.1254_9606, 0.124_4964, 0.1083_1517, 0.1156_2866, 0.1086_7816, 0.1049_9048]						)

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