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






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

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

             # started as 128, should now be 512
             assert image.shape == (1, 512, 512, 3)
             __lowerCamelCase					:		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 - expected_slice    ).max() < 1E-1






      def       lowerCamelCase__     (       self      :			Any    ):
             __lowerCamelCase					:		Union[str, Any]		   =     OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint							,					provider="CPUExecutionProvider"    )
             __lowerCamelCase					:		Optional[int]		   =     PNDMScheduler.from_config(pipe.scheduler.config							,					skip_prk_steps=UpperCAmelCase    )
             pipe.set_progress_bar_config(disable=UpperCAmelCase    )

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

             assert image.shape == (1, 512, 512, 3)
             __lowerCamelCase					:		int		   =     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       lowerCamelCase__     (       self      :			Optional[int]    ):
             __lowerCamelCase					:		Optional[Any]		   =     OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint							,					provider="CPUExecutionProvider"    )
             __lowerCamelCase					:		Dict		   =     DPMSolverMultistepScheduler.from_config(pipe.scheduler.config    )
             pipe.set_progress_bar_config(disable=UpperCAmelCase    )

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

             assert image.shape == (1, 512, 512, 3)
             __lowerCamelCase					:		Optional[Any]		   =     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       lowerCamelCase__     (       self      :			Tuple    ):
             __lowerCamelCase					:		Dict		   =     OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint							,					provider="CPUExecutionProvider"    )
             __lowerCamelCase					:		Any		   =     EulerDiscreteScheduler.from_config(pipe.scheduler.config    )
             pipe.set_progress_bar_config(disable=UpperCAmelCase    )

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

             assert image.shape == (1, 512, 512, 3)
             __lowerCamelCase					:		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       lowerCamelCase__     (       self      :			Optional[int]    ):
             __lowerCamelCase					:		Optional[Any]		   =     OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint							,					provider="CPUExecutionProvider"    )
             __lowerCamelCase					:		Optional[int]		   =     EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config    )
             pipe.set_progress_bar_config(disable=UpperCAmelCase    )

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

             assert image.shape == (1, 512, 512, 3)
             __lowerCamelCase					:		Union[str, Any]		   =     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  _snake_case     (					unittest.TestCase		):






      @property
      def       lowerCamelCase__     (       self      :			Optional[int]    ):
             return (
                 "CUDAExecutionProvider",
                 {
                     "gpu_mem_limit": "15000000000",  # 15GB
                     "arena_extend_strategy": "kSameAsRequested",
                 },
             )






      @property
      def       lowerCamelCase__     (       self      :			List[Any]    ):
             __lowerCamelCase					:		Optional[Any]		   =     ort.SessionOptions()
             __lowerCamelCase					:		int		   =     False
             return options






      def       lowerCamelCase__     (       self      :			Any    ):
             __lowerCamelCase					:		Optional[Any]		   =     load_image(
                 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
                 "/img2img/sketch-mountains-input.jpg"    )
             __lowerCamelCase					:		Union[str, Any]		   =     init_image.resize((128, 128)    )
             # using the PNDM scheduler by default
             __lowerCamelCase					:		List[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    )

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

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

             assert images.shape == (1, 512, 512, 3)
             __lowerCamelCase					:		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       lowerCamelCase__     (       self      :			Optional[int]    ):
             __lowerCamelCase					:		Optional[int]		   =     load_image(
                 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
                 "/img2img/sketch-mountains-input.jpg"    )
             __lowerCamelCase					:		int		   =     init_image.resize((128, 128)    )
             __lowerCamelCase					:		Tuple		   =     LMSDiscreteScheduler.from_pretrained(
                 "ssube/stable-diffusion-x4-upscaler-onnx"							,					subfolder="scheduler"    )
             __lowerCamelCase					:		int		   =     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    )

             __lowerCamelCase					:		Any		   =     "A fantasy landscape, trending on artstation"

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

             assert images.shape == (1, 512, 512, 3)
             __lowerCamelCase					:		Optional[Any]		   =     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"""





from functools import lru_cache


def    lowercase_   (   _lowerCamelCase:	int      ) ->      set:



							'''simple docstring'''

							__lowerCamelCase					:		Optional[Any]		   =     2
							__lowerCamelCase					:		Tuple		   =     set()
							while i * i <= n:
														if n % i:
																					i += 1
														else:
																					n //= i
																					factors.add(_lowerCamelCase      )
							if n > 1:
														factors.add(_lowerCamelCase      )
							return factors


@lru_cache
def    lowercase_   (   _lowerCamelCase:	int      ) ->      int:



							'''simple docstring'''

							return len(unique_prime_factors(_lowerCamelCase      )      )


def    lowercase_   (   _lowerCamelCase:	list      ) ->      bool:



							'''simple docstring'''

							return len(set(_lowerCamelCase      )      ) in (0, 1)


def    lowercase_   (   _lowerCamelCase:	int      ) ->      list:



							'''simple docstring'''

							__lowerCamelCase					:		str		   =     2

							while True:
														# Increment each value of a generated range
														__lowerCamelCase					:		int		   =     [base + i for i in range(_lowerCamelCase      )]

														# Run elements through out unique_prime_factors function
														# Append our target number to the end.
														__lowerCamelCase					:		Dict		   =     [upf_len(_lowerCamelCase      ) for x in group]
														checker.append(_lowerCamelCase      )

														# If all numbers in the list are equal, return the group variable.
														if equality(_lowerCamelCase      ):
																					return group

														# Increment our base variable by 1
														base += 1


def    lowercase_   (   _lowerCamelCase:	int = 4      ) ->      int:



							'''simple docstring'''

							__lowerCamelCase					:		Any		   =     run(_lowerCamelCase      )
							return results[0] if len(_lowerCamelCase      ) else None


if __name__ == "__main__":
				print(solution())