import random
import unittest

import numpy as np
import torch

from diffusers import (
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    OnnxStableDiffusionUpscalePipeline,
    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        lowercase_   (					__SCREAMING_SNAKE_CASE						,			unittest.TestCase		):
							_lowerCamelCase						=		'ssube/stable-diffusion-x4-upscaler-onnx'



							def 	UpperCamelCase	(						self				,    lowercase_=0    ):
													_snake_case						:					Dict    =   floats_tensor((1, 3, 128, 128)				,    rng=random.Random(UpperCamelCase__    )    )
													_snake_case						:					Any    =   torch.manual_seed(UpperCamelCase__    )
													_snake_case						:					int    =   {
													    '''prompt''': '''A painting of a squirrel eating a burger''',
													    '''image''': image,
													    '''generator''': generator,
													    '''num_inference_steps''': 3,
													    '''guidance_scale''': 7.5,
													    '''output_type''': '''numpy''',
													}
													return inputs



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Optional[Any]    =   OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint				,    provider="CPUExecutionProvider"    )
													pipe.set_progress_bar_config(disable=UpperCamelCase__    )

													_snake_case						:					int    =   self.get_dummy_inputs()
													_snake_case						:					List[str]    =   pipe(**UpperCamelCase__    ).images
													_snake_case						:					List[str]    =   image[0, -3:, -3:, -1].flatten()

													# started as 128, should now be 512
													assert image.shape == (1, 512, 512, 3)
													_snake_case						:					int    =   np.array(
													    [0.6_974_782, 0.68_902_093, 0.70_135_885, 0.7_583_618, 0.7_804_545, 0.7_854_912, 0.78_667_426, 0.78_743_863, 0.78_070_223]    )
													assert np.abs(image_slice - expected_slice    ).max() < 1e-1



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Any    =   OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint				,    provider="CPUExecutionProvider"    )
													_snake_case						:					List[str]    =   PNDMScheduler.from_config(pipe.scheduler.config				,    skip_prk_steps=UpperCamelCase__    )
													pipe.set_progress_bar_config(disable=UpperCamelCase__    )

													_snake_case						:					Optional[int]    =   self.get_dummy_inputs()
													_snake_case						:					Union[str, Any]    =   pipe(**UpperCamelCase__    ).images
													_snake_case						:					Optional[Any]    =   image[0, -3:, -3:, -1]

													assert image.shape == (1, 512, 512, 3)
													_snake_case						:					Any    =   np.array(
													    [0.6_898_892, 0.59_240_556, 0.52_499_527, 0.58_866_215, 0.52_258_235, 0.52_572_715, 0.62_414_473, 0.6_174_387, 0.6_214_964]    )
													assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-1



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Optional[int]    =   OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint				,    provider="CPUExecutionProvider"    )
													_snake_case						:					List[Any]    =   DPMSolverMultistepScheduler.from_config(pipe.scheduler.config    )
													pipe.set_progress_bar_config(disable=UpperCamelCase__    )

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

													assert image.shape == (1, 512, 512, 3)
													_snake_case						:					Optional[Any]    =   np.array(
													    [0.7_659_278, 0.76_437_664, 0.75_579_107, 0.7_691_116, 0.77_666_986, 0.7_727_672, 0.7_758_664, 0.7_812_226, 0.76_942_515]    )

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



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					str    =   OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint				,    provider="CPUExecutionProvider"    )
													_snake_case						:					Optional[int]    =   EulerDiscreteScheduler.from_config(pipe.scheduler.config    )
													pipe.set_progress_bar_config(disable=UpperCamelCase__    )

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

													assert image.shape == (1, 512, 512, 3)
													_snake_case						:					Optional[Any]    =   np.array(
													    [0.6_974_782, 0.68_902_093, 0.70_135_885, 0.7_583_618, 0.7_804_545, 0.7_854_912, 0.78_667_426, 0.78_743_863, 0.78_070_223]    )
													assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-1



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					List[Any]    =   OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint				,    provider="CPUExecutionProvider"    )
													_snake_case						:					Optional[Any]    =   EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config    )
													pipe.set_progress_bar_config(disable=UpperCamelCase__    )

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

													assert image.shape == (1, 512, 512, 3)
													_snake_case						:					Any    =   np.array(
													    [0.77_424_496, 0.773_601, 0.7_645_288, 0.7_769_598, 0.7_772_739, 0.7_738_688, 0.78_187_233, 0.77_879_584, 0.767_043]    )

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




@nightly
@require_onnxruntime
@require_torch_gpu
class        lowercase_   (					unittest.TestCase		):



							@property
							def 	UpperCamelCase	(						self    ):
													return (
													    "CUDAExecutionProvider",
													    {
													        "gpu_mem_limit": "15000000000",  # 15GB
													        "arena_extend_strategy": "kSameAsRequested",
													    },
													)



							@property
							def 	UpperCamelCase	(						self    ):
													_snake_case						:					List[str]    =   ort.SessionOptions()
													_snake_case						:					List[str]    =   False
													return options



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Any    =   load_image(
													    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
													    "/img2img/sketch-mountains-input.jpg"    )
													_snake_case						:					Any    =   init_image.resize((128, 128)    )
													# using the PNDM scheduler by default
													_snake_case						:					Union[str, Any]    =   OnnxStableDiffusionUpscalePipeline.from_pretrained(
													    "ssube/stable-diffusion-x4-upscaler-onnx"				,    provider=self.gpu_provider				,    sess_options=self.gpu_options				,    )
													pipe.set_progress_bar_config(disable=UpperCamelCase__    )

													_snake_case						:					Union[str, Any]    =   '''A fantasy landscape, trending on artstation'''

													_snake_case						:					str    =   torch.manual_seed(0    )
													_snake_case						:					List[str]    =   pipe(
													    prompt=UpperCamelCase__				,    image=UpperCamelCase__				,    guidance_scale=7.5				,    num_inference_steps=10				,    generator=UpperCamelCase__				,    output_type="np"				,    )
													_snake_case						:					int    =   output.images
													_snake_case						:					str    =   images[0, 255:258, 383:386, -1]

													assert images.shape == (1, 512, 512, 3)
													_snake_case						:					Optional[int]    =   np.array([0.4_883, 0.4_947, 0.4_980, 0.4_975, 0.4_982, 0.4_980, 0.5_000, 0.5_006, 0.4_972]    )
													# 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    ):
													_snake_case						:					Tuple    =   load_image(
													    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
													    "/img2img/sketch-mountains-input.jpg"    )
													_snake_case						:					List[Any]    =   init_image.resize((128, 128)    )
													_snake_case						:					Optional[Any]    =   LMSDiscreteScheduler.from_pretrained(
													    "ssube/stable-diffusion-x4-upscaler-onnx"				,    subfolder="scheduler"    )
													_snake_case						:					Union[str, Any]    =   OnnxStableDiffusionUpscalePipeline.from_pretrained(
													    "ssube/stable-diffusion-x4-upscaler-onnx"				,    scheduler=UpperCamelCase__				,    provider=self.gpu_provider				,    sess_options=self.gpu_options				,    )
													pipe.set_progress_bar_config(disable=UpperCamelCase__    )

													_snake_case						:					Optional[Any]    =   '''A fantasy landscape, trending on artstation'''

													_snake_case						:					List[Any]    =   torch.manual_seed(0    )
													_snake_case						:					List[str]    =   pipe(
													    prompt=UpperCamelCase__				,    image=UpperCamelCase__				,    guidance_scale=7.5				,    num_inference_steps=20				,    generator=UpperCamelCase__				,    output_type="np"				,    )
													_snake_case						:					str    =   output.images
													_snake_case						:					int    =   images[0, 255:258, 383:386, -1]

													assert images.shape == (1, 512, 512, 3)
													_snake_case						:					int    =   np.array(
													    [0.50_173_753, 0.50_223_356, 0.502_039, 0.50_233_036, 0.5_023_725, 0.5_022_601, 0.5_018_758, 0.50_234_085, 0.50_241_566]    )
													# TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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

import numpy as np
import torch
from transformers import AutoTokenizer, TaEncoderModel

from diffusers import DDPMScheduler, UNetaDConditionModel
from diffusers.models.attention_processor import AttnAddedKVProcessor
from diffusers.pipelines.deepfloyd_if import IFWatermarker
from diffusers.utils.testing_utils import torch_device

from ..test_pipelines_common import to_np



class        lowercase_   :



							def 	UpperCamelCase	(						self    ):
													torch.manual_seed(0    )
													_snake_case						:					Tuple    =   TaEncoderModel.from_pretrained("hf-internal-testing/tiny-random-t5"    )

													torch.manual_seed(0    )
													_snake_case						:					List[Any]    =   AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-t5"    )

													torch.manual_seed(0    )
													_snake_case						:					Optional[int]    =   UNetaDConditionModel(
													    sample_size=32				,    layers_per_block=1				,    block_out_channels=[32, 64]				,    down_block_types=[
													        "ResnetDownsampleBlock2D",
													        "SimpleCrossAttnDownBlock2D",
													    ]				,    mid_block_type="UNetMidBlock2DSimpleCrossAttn"				,    up_block_types=["SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"]				,    in_channels=3				,    out_channels=6				,    cross_attention_dim=32				,    encoder_hid_dim=32				,    attention_head_dim=8				,    addition_embed_type="text"				,    addition_embed_type_num_heads=2				,    cross_attention_norm="group_norm"				,    resnet_time_scale_shift="scale_shift"				,    act_fn="gelu"				,    )
													unet.set_attn_processor(AttnAddedKVProcessor()    )  # For reproducibility tests

													torch.manual_seed(0    )
													_snake_case						:					str    =   DDPMScheduler(
													    num_train_timesteps=1_000				,    beta_schedule="squaredcos_cap_v2"				,    beta_start=0.0_001				,    beta_end=0.02				,    thresholding=lowercase_				,    dynamic_thresholding_ratio=0.95				,    sample_max_value=1.0				,    prediction_type="epsilon"				,    variance_type="learned_range"				,    )

													torch.manual_seed(0    )
													_snake_case						:					int    =   IFWatermarker()

													return {
													    "text_encoder": text_encoder,
													    "tokenizer": tokenizer,
													    "unet": unet,
													    "scheduler": scheduler,
													    "watermarker": watermarker,
													    "safety_checker": None,
													    "feature_extractor": None,
													}



							def 	UpperCamelCase	(						self    ):
													torch.manual_seed(0    )
													_snake_case						:					List[str]    =   TaEncoderModel.from_pretrained("hf-internal-testing/tiny-random-t5"    )

													torch.manual_seed(0    )
													_snake_case						:					Tuple    =   AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-t5"    )

													torch.manual_seed(0    )
													_snake_case						:					Any    =   UNetaDConditionModel(
													    sample_size=32				,    layers_per_block=[1, 2]				,    block_out_channels=[32, 64]				,    down_block_types=[
													        "ResnetDownsampleBlock2D",
													        "SimpleCrossAttnDownBlock2D",
													    ]				,    mid_block_type="UNetMidBlock2DSimpleCrossAttn"				,    up_block_types=["SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"]				,    in_channels=6				,    out_channels=6				,    cross_attention_dim=32				,    encoder_hid_dim=32				,    attention_head_dim=8				,    addition_embed_type="text"				,    addition_embed_type_num_heads=2				,    cross_attention_norm="group_norm"				,    resnet_time_scale_shift="scale_shift"				,    act_fn="gelu"				,    class_embed_type="timestep"				,    mid_block_scale_factor=1.414				,    time_embedding_act_fn="gelu"				,    time_embedding_dim=32				,    )
													unet.set_attn_processor(AttnAddedKVProcessor()    )  # For reproducibility tests

													torch.manual_seed(0    )
													_snake_case						:					Union[str, Any]    =   DDPMScheduler(
													    num_train_timesteps=1_000				,    beta_schedule="squaredcos_cap_v2"				,    beta_start=0.0_001				,    beta_end=0.02				,    thresholding=lowercase_				,    dynamic_thresholding_ratio=0.95				,    sample_max_value=1.0				,    prediction_type="epsilon"				,    variance_type="learned_range"				,    )

													torch.manual_seed(0    )
													_snake_case						:					List[str]    =   DDPMScheduler(
													    num_train_timesteps=1_000				,    beta_schedule="squaredcos_cap_v2"				,    beta_start=0.0_001				,    beta_end=0.02				,    )

													torch.manual_seed(0    )
													_snake_case						:					Dict    =   IFWatermarker()

													return {
													    "text_encoder": text_encoder,
													    "tokenizer": tokenizer,
													    "unet": unet,
													    "scheduler": scheduler,
													    "image_noising_scheduler": image_noising_scheduler,
													    "watermarker": watermarker,
													    "safety_checker": None,
													    "feature_extractor": None,
													}



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					List[Any]    =   self.get_dummy_components()
													_snake_case						:					List[Any]    =   self.pipeline_class(**lowercase_    )
													pipe.to(lowercase_    )
													pipe.set_progress_bar_config(disable=lowercase_    )

													_snake_case						:					Union[str, Any]    =   self.get_dummy_inputs(lowercase_    )

													_snake_case						:					Union[str, Any]    =   inputs["prompt"]
													_snake_case						:					Dict    =   inputs["generator"]
													_snake_case						:					Any    =   inputs["num_inference_steps"]
													_snake_case						:					Union[str, Any]    =   inputs["output_type"]

													if "image" in inputs:
																			_snake_case						:					int    =   inputs["image"]
													else:
																			_snake_case						:					Union[str, Any]    =   None

													if "mask_image" in inputs:
																			_snake_case						:					int    =   inputs["mask_image"]
													else:
																			_snake_case						:					List[str]    =   None

													if "original_image" in inputs:
																			_snake_case						:					Tuple    =   inputs["original_image"]
													else:
																			_snake_case						:					Any    =   None

													_snake_case    ,_snake_case						:					Optional[int]    =   pipe.encode_prompt(lowercase_    )

													# inputs with prompt converted to embeddings
													_snake_case						:					Dict    =   {
													    "prompt_embeds": prompt_embeds,
													    "negative_prompt_embeds": negative_prompt_embeds,
													    "generator": generator,
													    "num_inference_steps": num_inference_steps,
													    "output_type": output_type,
													}

													if image is not None:
																			_snake_case						:					int    =   image

													if mask_image is not None:
																			_snake_case						:					int    =   mask_image

													if original_image is not None:
																			_snake_case						:					Any    =   original_image

													# set all optional components to None
													for optional_component in pipe._optional_components:
																			setattr(lowercase_				,    lowercase_				,    lowercase_    )

													_snake_case						:					Optional[int]    =   pipe(**lowercase_    )[0]

													with tempfile.TemporaryDirectory() as tmpdir:
																			pipe.save_pretrained(lowercase_    )
																			_snake_case						:					Any    =   self.pipeline_class.from_pretrained(lowercase_    )
																			pipe_loaded.to(lowercase_    )
																			pipe_loaded.set_progress_bar_config(disable=lowercase_    )

													pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor()    )  # For reproducibility tests

													for optional_component in pipe._optional_components:
																			self.assertTrue(
																			    getattr(lowercase_				,    lowercase_    ) is None				,    f"""`{optional_component}` did not stay set to None after loading."""				,    )

													_snake_case						:					int    =   self.get_dummy_inputs(lowercase_    )

													_snake_case						:					Optional[int]    =   inputs["generator"]
													_snake_case						:					List[Any]    =   inputs["num_inference_steps"]
													_snake_case						:					Tuple    =   inputs["output_type"]

													# inputs with prompt converted to embeddings
													_snake_case						:					int    =   {
													    "prompt_embeds": prompt_embeds,
													    "negative_prompt_embeds": negative_prompt_embeds,
													    "generator": generator,
													    "num_inference_steps": num_inference_steps,
													    "output_type": output_type,
													}

													if image is not None:
																			_snake_case						:					int    =   image

													if mask_image is not None:
																			_snake_case						:					str    =   mask_image

													if original_image is not None:
																			_snake_case						:					int    =   original_image

													_snake_case						:					Optional[Any]    =   pipe_loaded(**lowercase_    )[0]

													_snake_case						:					Dict    =   np.abs(to_np(lowercase_    ) - to_np(lowercase_    )    ).max()
													self.assertLess(lowercase_				,    1e-4    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Tuple    =   self.get_dummy_components()
													_snake_case						:					Any    =   self.pipeline_class(**lowercase_    )
													pipe.to(lowercase_    )
													pipe.set_progress_bar_config(disable=lowercase_    )

													_snake_case						:					int    =   self.get_dummy_inputs(lowercase_    )
													_snake_case						:					List[str]    =   pipe(**lowercase_    )[0]

													with tempfile.TemporaryDirectory() as tmpdir:
																			pipe.save_pretrained(lowercase_    )
																			_snake_case						:					List[Any]    =   self.pipeline_class.from_pretrained(lowercase_    )
																			pipe_loaded.to(lowercase_    )
																			pipe_loaded.set_progress_bar_config(disable=lowercase_    )

													pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor()    )  # For reproducibility tests

													_snake_case						:					Optional[Any]    =   self.get_dummy_inputs(lowercase_    )
													_snake_case						:					int    =   pipe_loaded(**lowercase_    )[0]

													_snake_case						:					Tuple    =   np.abs(to_np(lowercase_    ) - to_np(lowercase_    )    ).max()
													self.assertLess(lowercase_				,    1e-4    )