

'''simple docstring'''

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    snake_case     (      lowercase_,						unittest.TestCase		):






				"""simple docstring"""
				_a    	=     """hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline"""



				def   a__					(    self,     _lowercase=0       )     ->							List[str]:
											SCREAMING_SNAKE_CASE_				=				floats_tensor((1, 3, 128, 128),     rng=random.Random(_lowercase       )       )
											SCREAMING_SNAKE_CASE_				=				np.random.RandomState(_lowercase       )
											SCREAMING_SNAKE_CASE_				=				{
											    '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   a__					(    self       )     ->							Optional[int]:
											SCREAMING_SNAKE_CASE_				=				OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint,     provider='CPUExecutionProvider'       )
											pipe.set_progress_bar_config(disable=_lowercase       )

											SCREAMING_SNAKE_CASE_				=				self.get_dummy_inputs()
											SCREAMING_SNAKE_CASE_				=				pipe(**_lowercase       ).images
											SCREAMING_SNAKE_CASE_				=				image[0, -3:, -3:, -1].flatten()

											assert image.shape == (1, 128, 128, 3)
											SCREAMING_SNAKE_CASE_				=				np.array([0.69_643, 0.58_484, 0.50_314, 0.58_760, 0.55_368, 0.59_643, 0.51_529, 0.41_217, 0.49_087]       )
											assert np.abs(image_slice - expected_slice       ).max() < 1E-1



				def   a__					(    self       )     ->							Union[str, Any]:
											SCREAMING_SNAKE_CASE_				=				OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint,     provider='CPUExecutionProvider'       )
											SCREAMING_SNAKE_CASE_				=				PNDMScheduler.from_config(pipe.scheduler.config,     skip_prk_steps=_lowercase       )
											pipe.set_progress_bar_config(disable=_lowercase       )

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

											assert image.shape == (1, 128, 128, 3)
											SCREAMING_SNAKE_CASE_				=				np.array([0.61_737, 0.54_642, 0.53_183, 0.54_465, 0.52_742, 0.60_525, 0.49_969, 0.40_655, 0.48_154]       )

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



				def   a__					(    self       )     ->							Any:
											SCREAMING_SNAKE_CASE_				=				OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint,     provider='CPUExecutionProvider'       )
											SCREAMING_SNAKE_CASE_				=				LMSDiscreteScheduler.from_config(pipe.scheduler.config       )
											pipe.set_progress_bar_config(disable=_lowercase       )

											# warmup pass to apply optimizations
											SCREAMING_SNAKE_CASE_				=				pipe(**self.get_dummy_inputs()       )

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

											assert image.shape == (1, 128, 128, 3)
											SCREAMING_SNAKE_CASE_				=				np.array([0.52_761, 0.59_977, 0.49_033, 0.49_619, 0.54_282, 0.50_311, 0.47_600, 0.40_918, 0.45_203]       )

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



				def   a__					(    self       )     ->							List[str]:
											SCREAMING_SNAKE_CASE_				=				OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint,     provider='CPUExecutionProvider'       )
											SCREAMING_SNAKE_CASE_				=				EulerDiscreteScheduler.from_config(pipe.scheduler.config       )
											pipe.set_progress_bar_config(disable=_lowercase       )

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

											assert image.shape == (1, 128, 128, 3)
											SCREAMING_SNAKE_CASE_				=				np.array([0.52_911, 0.60_004, 0.49_229, 0.49_805, 0.54_502, 0.50_680, 0.47_777, 0.41_028, 0.45_304]       )

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



				def   a__					(    self       )     ->							List[Any]:
											SCREAMING_SNAKE_CASE_				=				OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint,     provider='CPUExecutionProvider'       )
											SCREAMING_SNAKE_CASE_				=				EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config       )
											pipe.set_progress_bar_config(disable=_lowercase       )

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

											assert image.shape == (1, 128, 128, 3)
											SCREAMING_SNAKE_CASE_				=				np.array([0.52_911, 0.60_004, 0.49_229, 0.49_805, 0.54_502, 0.50_680, 0.47_777, 0.41_028, 0.45_304]       )

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





				def   a__					(    self       )     ->							Tuple:
											SCREAMING_SNAKE_CASE_				=				OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint,     provider='CPUExecutionProvider'       )
											SCREAMING_SNAKE_CASE_				=				DPMSolverMultistepScheduler.from_config(pipe.scheduler.config       )
											pipe.set_progress_bar_config(disable=_lowercase       )

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

											assert image.shape == (1, 128, 128, 3)
											SCREAMING_SNAKE_CASE_				=				np.array([0.65_331, 0.58_277, 0.48_204, 0.56_059, 0.53_665, 0.56_235, 0.50_969, 0.40_009, 0.46_552]       )

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






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






				"""simple docstring"""



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



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



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

											SCREAMING_SNAKE_CASE_				=				'A fantasy landscape, trending on artstation'

											SCREAMING_SNAKE_CASE_				=				np.random.RandomState(0       )
											SCREAMING_SNAKE_CASE_				=				pipe(
											    prompt=_lowercase,     image=_lowercase,     strength=0.75,     guidance_scale=7.5,     num_inference_steps=10,     generator=_lowercase,     output_type='np',     )
											SCREAMING_SNAKE_CASE_				=				output.images
											SCREAMING_SNAKE_CASE_				=				images[0, 255:258, 383:386, -1]

											assert images.shape == (1, 512, 768, 3)
											SCREAMING_SNAKE_CASE_				=				np.array([0.4_909, 0.5_059, 0.5_372, 0.4_623, 0.4_876, 0.5_049, 0.4_820, 0.4_956, 0.5_019]       )
											# TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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





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

											SCREAMING_SNAKE_CASE_				=				'A fantasy landscape, trending on artstation'

											SCREAMING_SNAKE_CASE_				=				np.random.RandomState(0       )
											SCREAMING_SNAKE_CASE_				=				pipe(
											    prompt=_lowercase,     image=_lowercase,     strength=0.75,     guidance_scale=7.5,     num_inference_steps=20,     generator=_lowercase,     output_type='np',     )
											SCREAMING_SNAKE_CASE_				=				output.images
											SCREAMING_SNAKE_CASE_				=				images[0, 255:258, 383:386, -1]

											assert images.shape == (1, 512, 768, 3)
											SCREAMING_SNAKE_CASE_				=				np.array([0.8_043, 0.926, 0.9_581, 0.8_119, 0.8_954, 0.913, 0.7_209, 0.7_463, 0.7_431]       )
											# 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'''

def 							_UpperCamelCase				(   lowerCAmelCase__:					int		,lowerCAmelCase__:					bool = False   )      -> bool:
							if n == 2:
														return True
							if not n % 2 or n < 2:
														return False
							if n > 5 and n % 10 not in (1, 3, 7, 9):  # can quickly check last digit
														return False
							if n > 3_3170_4406_4679_8873_8596_1981 and not allow_probable:
														raise ValueError(
														    'Warning: upper bound of deterministic test is exceeded. '
														    'Pass allow_probable=True to allow probabilistic test. '
														    'A return value of True indicates a probable prime.'   )
							# array bounds provided by analysis
							SCREAMING_SNAKE_CASE_				=				[
							    2047,
							    137_3653,
							    2532_6001,
							    32_1503_1751,
							    2_1523_0289_8747,
							    3_4747_4966_0383,
							    341_5500_7172_8321,
							    1,
							    382_5123_0565_4641_3051,
							    1,
							    1,
							    3186_6585_7834_0311_5116_7461,
							    3_3170_4406_4679_8873_8596_1981,
							]

							SCREAMING_SNAKE_CASE_				=				[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]
							for idx, _p in enumerate(lowerCAmelCase__		,1   ):
														if n < _p:
																					# then we have our last prime to check
																					SCREAMING_SNAKE_CASE_				=				primes[:idx]
																					break
							SCREAMING_SNAKE_CASE_				,    SCREAMING_SNAKE_CASE_				=				n - 1, 0
							# break up n -1 into a power of 2 (s) and
							# remaining odd component
							# essentially, solve for d * 2 ** s == n - 1
							while d % 2 == 0:
														d //= 2
														s += 1
							for prime in plist:
														SCREAMING_SNAKE_CASE_				=				False
														for r in range(lowerCAmelCase__   ):
																					SCREAMING_SNAKE_CASE_				=				pow(lowerCAmelCase__		,d * 2**r		,lowerCAmelCase__   )
																					# see article for analysis explanation for m
																					if (r == 0 and m == 1) or ((m + 1) % n == 0):
																												SCREAMING_SNAKE_CASE_				=				True
																												# this loop will not determine compositeness
																												break
														if pr:
																					continue
														# if pr is False, then the above loop never evaluated to true,
														# and the n MUST be composite
														return False
							return True




def 							_UpperCamelCase				(   )      -> None:
							assert not miller_rabin(561   )
							assert miller_rabin(563   )
							# 2047

							assert not miller_rabin(83_8201   )
							assert miller_rabin(83_8207   )
							# 1_373_653

							assert not miller_rabin(1731_6001   )
							assert miller_rabin(1731_6017   )
							# 25_326_001

							assert not miller_rabin(30_7838_6641   )
							assert miller_rabin(30_7838_6653   )
							# 3_215_031_751

							assert not miller_rabin(1_7130_4557_4801   )
							assert miller_rabin(1_7130_4557_4819   )
							# 2_152_302_898_747

							assert not miller_rabin(2_7797_9972_8307   )
							assert miller_rabin(2_7797_9972_8327   )
							# 3_474_749_660_383

							assert not miller_rabin(113_8500_2390_9441   )
							assert miller_rabin(113_8500_2390_9527   )
							# 341_550_071_728_321

							assert not miller_rabin(127_5041_0188_4880_4351   )
							assert miller_rabin(127_5041_0188_4880_4391   )
							# 3_825_123_056_546_413_051

							assert not miller_rabin(796_6646_4458_5077_8779_1867   )
							assert miller_rabin(796_6646_4458_5077_8779_1951   )
							# 318_665_857_834_031_151_167_461

							assert not miller_rabin(5528_4067_7446_6478_9766_0333   )
							assert miller_rabin(5528_4067_7446_6478_9766_0359   )
							# 3_317_044_064_679_887_385_961_981
							# upper limit for probabilistic test


if __name__ == "__main__":
							test_miller_rabin()





