



'''simple docstring'''




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     UpperCamelCase_				(							__magic_name__   ,  unittest.TestCase    ):
	# TODO: is there an appropriate internal test set?
	lowercase    =				'ssube/stable-diffusion-x4-upscaler-onnx'





	def 				_lowercase(						self	,	A=0					)    ->    Dict:
								UpperCAmelCase  :	Union[str, Any]							  =	floats_tensor((1, 3, 128, 128)	,	rng=random.Random(A					)					)
								UpperCAmelCase  :	Optional[Any]							  =	torch.manual_seed(A					)
								UpperCAmelCase  :	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 				_lowercase(						self					)    ->    str:
								UpperCAmelCase  :	Optional[int]							  =	OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint	,	provider="""CPUExecutionProvider"""					)
								pipe.set_progress_bar_config(disable=A					)

								UpperCAmelCase  :	int							  =	self.get_dummy_inputs()
								UpperCAmelCase  :	List[str]							  =	pipe(**A					).images
								UpperCAmelCase  :	Optional[Any]							  =	image[0, -3:, -3:, -1].flatten()

								# started as 128, should now be 512
								assert image.shape == (1, 512, 512, 3)
								UpperCAmelCase  :	Dict							  =	np.array(
								    [0.6_9_7_4_7_8_2, 0.6_8_9_0_2_0_9_3, 0.7_0_1_3_5_8_8_5, 0.7_5_8_3_6_1_8, 0.7_8_0_4_5_4_5, 0.7_8_5_4_9_1_2, 0.7_8_6_6_7_4_2_6, 0.7_8_7_4_3_8_6_3, 0.7_8_0_7_0_2_2_3]					)
								assert np.abs(image_slice - expected_slice					).max() < 1e-1





	def 				_lowercase(						self					)    ->    List[str]:
								UpperCAmelCase  :	Dict							  =	OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint	,	provider="""CPUExecutionProvider"""					)
								UpperCAmelCase  :	Optional[int]							  =	PNDMScheduler.from_config(pipe.scheduler.config	,	skip_prk_steps=A					)
								pipe.set_progress_bar_config(disable=A					)

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

								assert image.shape == (1, 512, 512, 3)
								UpperCAmelCase  :	Any							  =	np.array(
								    [0.6_8_9_8_8_9_2, 0.5_9_2_4_0_5_5_6, 0.5_2_4_9_9_5_2_7, 0.5_8_8_6_6_2_1_5, 0.5_2_2_5_8_2_3_5, 0.5_2_5_7_2_7_1_5, 0.6_2_4_1_4_4_7_3, 0.6_1_7_4_3_8_7, 0.6_2_1_4_9_6_4]					)
								assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-1





	def 				_lowercase(						self					)    ->    List[Any]:
								UpperCAmelCase  :	Optional[Any]							  =	OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint	,	provider="""CPUExecutionProvider"""					)
								UpperCAmelCase  :	Optional[int]							  =	DPMSolverMultistepScheduler.from_config(pipe.scheduler.config					)
								pipe.set_progress_bar_config(disable=A					)

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

								assert image.shape == (1, 512, 512, 3)
								UpperCAmelCase  :	List[str]							  =	np.array(
								    [0.7_6_5_9_2_7_8, 0.7_6_4_3_7_6_6_4, 0.7_5_5_7_9_1_0_7, 0.7_6_9_1_1_1_6, 0.7_7_6_6_6_9_8_6, 0.7_7_2_7_6_7_2, 0.7_7_5_8_6_6_4, 0.7_8_1_2_2_2_6, 0.7_6_9_4_2_5_1_5]					)

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





	def 				_lowercase(						self					)    ->    Optional[int]:
								UpperCAmelCase  :	int							  =	OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint	,	provider="""CPUExecutionProvider"""					)
								UpperCAmelCase  :	Optional[Any]							  =	EulerDiscreteScheduler.from_config(pipe.scheduler.config					)
								pipe.set_progress_bar_config(disable=A					)

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

								assert image.shape == (1, 512, 512, 3)
								UpperCAmelCase  :	Optional[Any]							  =	np.array(
								    [0.6_9_7_4_7_8_2, 0.6_8_9_0_2_0_9_3, 0.7_0_1_3_5_8_8_5, 0.7_5_8_3_6_1_8, 0.7_8_0_4_5_4_5, 0.7_8_5_4_9_1_2, 0.7_8_6_6_7_4_2_6, 0.7_8_7_4_3_8_6_3, 0.7_8_0_7_0_2_2_3]					)
								assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-1






	def 				_lowercase(						self					)    ->    Tuple:
								UpperCAmelCase  :	Optional[Any]							  =	OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint	,	provider="""CPUExecutionProvider"""					)
								UpperCAmelCase  :	int							  =	EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config					)
								pipe.set_progress_bar_config(disable=A					)

								UpperCAmelCase  :	str							  =	self.get_dummy_inputs()
								UpperCAmelCase  :	int							  =	pipe(**A					).images
								UpperCAmelCase  :	Tuple							  =	image[0, -3:, -3:, -1]

								assert image.shape == (1, 512, 512, 3)
								UpperCAmelCase  :	int							  =	np.array(
								    [0.7_7_4_2_4_4_9_6, 0.7_7_3_6_0_1, 0.7_6_4_5_2_8_8, 0.7_7_6_9_5_9_8, 0.7_7_7_2_7_3_9, 0.7_7_3_8_6_8_8, 0.7_8_1_8_7_2_3_3, 0.7_7_8_7_9_5_8_4, 0.7_6_7_0_4_3]					)

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




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





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





	@property
	def 				_lowercase(						self					)    ->    str:
								UpperCAmelCase  :	Union[str, Any]							  =	ort.SessionOptions()
								UpperCAmelCase  :	Any							  =	False
								return options





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

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

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

								assert images.shape == (1, 512, 512, 3)
								UpperCAmelCase  :	Optional[Any]							  =	np.array([0.4_8_8_3, 0.4_9_4_7, 0.4_9_8_0, 0.4_9_7_5, 0.4_9_8_2, 0.4_9_8_0, 0.5_0_0_0, 0.5_0_0_6, 0.4_9_7_2]					)
								# TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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






	def 				_lowercase(						self					)    ->    Dict:
								UpperCAmelCase  :	Tuple							  =	load_image(
								    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
								    """/img2img/sketch-mountains-input.jpg"""					)
								UpperCAmelCase  :	Optional[Any]							  =	init_image.resize((128, 128)					)
								UpperCAmelCase  :	Any							  =	LMSDiscreteScheduler.from_pretrained(
								    """ssube/stable-diffusion-x4-upscaler-onnx"""	,	subfolder="""scheduler"""					)
								UpperCAmelCase  :	int							  =	OnnxStableDiffusionUpscalePipeline.from_pretrained(
								    """ssube/stable-diffusion-x4-upscaler-onnx"""	,	scheduler=A	,	provider=self.gpu_provider	,	sess_options=self.gpu_options	,	)
								pipe.set_progress_bar_config(disable=A					)

								UpperCAmelCase  :	List[Any]							  =	"""A fantasy landscape, trending on artstation"""

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

								assert images.shape == (1, 512, 512, 3)
								UpperCAmelCase  :	int							  =	np.array(
								    [0.5_0_1_7_3_7_5_3, 0.5_0_2_2_3_3_5_6, 0.5_0_2_0_3_9, 0.5_0_2_3_3_0_3_6, 0.5_0_2_3_7_2_5, 0.5_0_2_2_6_0_1, 0.5_0_1_8_7_5_8, 0.5_0_2_3_4_0_8_5, 0.5_0_2_4_1_5_6_6]					)
								# TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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






'''simple docstring'''




import math



def    __lowerCamelCase						(      _lowercase	)   ->    bool:
							assert isinstance(_lowercase			,       _lowercase	) and (
							    number >= 0
							), "'number' must been an int and positive"

							if 1 < number < 4:
														# 2 and 3 are primes
														return True
							elif number < 2 or not number % 2:
														# Negatives, 0, 1 and all even numbers are not primes
														return False

							UpperCAmelCase  :	str							  =	range(3			,       int(math.sqrt(_lowercase	) + 1	)			,       2	)
							return not any(not number % i for i in odd_numbers	)



def    __lowerCamelCase						(      _lowercase			,       _lowercase=1			,       **_lowercase	)   ->    Union[str, Any]:
							UpperCAmelCase  :	Optional[int]							  =	factor * value
							UpperCAmelCase  :	List[Any]							  =	value

							while not is_prime(_lowercase	):
														value += 1 if not ("desc" in kwargs and kwargs["desc"] is True) else -1

							if value == first_value_val:
														return next_prime(value + 1			,       **_lowercase	)
							return value

