"""simple docstring"""







from collections import deque
from math import floor
from random import random
from time import time






class      UpperCAmelCase__		:



		def __init__( self  :    Dict				)			->						int:






									'''simple docstring'''

									A         =					{}



		def 					A_		( self  :    Any      ,							snake_case  :    Union[str, Any]      ,							snake_case  :    Any      ,							snake_case  :    Optional[Any]=1				)			->						int:






									'''simple docstring'''

									if self.graph.get(A_				):
																if self.graph[u].count([w, v]				) == 0:
																							self.graph[u].append([w, v]				)
									else:
																A         =					[[w, v]]
									if not self.graph.get(A_				):
																A         =					[]



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






									'''simple docstring'''

									return list(self.graph				)



		def 					A_		( self  :    Union[str, Any]      ,							snake_case  :    Union[str, Any]      ,							snake_case  :    Dict				)			->						Optional[Any]:






									'''simple docstring'''

									if self.graph.get(A_				):
																for _ in self.graph[u]:
																							if _[1] == v:
																														self.graph[u].remove(A_				)



		def 					A_		( self  :    Any      ,							snake_case  :    int=-2      ,							snake_case  :    Dict=-1				)			->						str:






									'''simple docstring'''

									if s == d:
																return []
									A         =					[]
									A         =					[]
									if s == -2:
																A         =					list(self.graph				)[0]
									stack.append(A_				)
									visited.append(A_				)
									A         =					s

									while True:
																# check if there is any non isolated nodes
																if len(self.graph[s]				) != 0:
																							A         =					s
																							for node in self.graph[s]:
																														if visited.count(node[1]				) < 1:
																																					if node[1] == d:
																																												visited.append(A_				)
																																												return visited
																																					else:
																																												stack.append(node[1]				)
																																												visited.append(node[1]				)
																																												A         =					node[1]
																																												break

            # check if all the children are visited
																if s == ss:
																							stack.pop()
																							if len(A_				) != 0:
																														A         =					stack[len(A_				) - 1]
																else:
																							A         =					ss

																# check if se have reached the starting point
																if len(A_				) == 0:
																							return visited



		def 					A_		( self  :    Optional[Any]      ,							snake_case  :    Any=-1				)			->						int:






									'''simple docstring'''

									if c == -1:
																A         =					floor(random() * 10_000				) + 10
									for i in range(A_				):
																# every vertex has max 100 edges
																for _ in range(floor(random() * 102				) + 1				):
																							A         =					floor(random() * c				) + 1
																							if n != i:
																														self.add_pair(A_      ,							A_      ,							1				)



		def 					A_		( self  :    Optional[Any]      ,							snake_case  :    Union[str, Any]=-2				)			->						Optional[Any]:






									'''simple docstring'''

									A         =					deque()
									A         =					[]
									if s == -2:
																A         =					list(self.graph				)[0]
									d.append(A_				)
									visited.append(A_				)
									while d:
																A         =					d.popleft()
																if len(self.graph[s]				) != 0:
																							for node in self.graph[s]:
																														if visited.count(node[1]				) < 1:
																																					d.append(node[1]				)
																																					visited.append(node[1]				)
									return visited



		def 					A_		( self  :    Union[str, Any]      ,							snake_case  :    Tuple				)			->						Any:






									'''simple docstring'''

									A         =					0
									for x in self.graph:
																for y in self.graph[x]:
																							if y[1] == u:
																														count += 1
									return count



		def 					A_		( self  :    Dict      ,							snake_case  :    Union[str, Any]				)			->						str:






									'''simple docstring'''

									return len(self.graph[u]				)



		def 					A_		( self  :    Optional[int]      ,							snake_case  :    Union[str, Any]=-2				)			->						Any:






									'''simple docstring'''

									A         =					[]
									A         =					[]
									if s == -2:
																A         =					list(self.graph				)[0]
									stack.append(A_				)
									visited.append(A_				)
									A         =					s
									A         =					[]

									while True:
																# check if there is any non isolated nodes
																if len(self.graph[s]				) != 0:
																							A         =					s
																							for node in self.graph[s]:
																														if visited.count(node[1]				) < 1:
																																					stack.append(node[1]				)
																																					visited.append(node[1]				)
																																					A         =					node[1]
																																					break

            # check if all the children are visited
																if s == ss:
																							sorted_nodes.append(stack.pop()				)
																							if len(A_				) != 0:
																														A         =					stack[len(A_				) - 1]
																else:
																							A         =					ss

																# check if se have reached the starting point
																if len(A_				) == 0:
																							return sorted_nodes



		def 					A_		( self  :    Optional[Any]				)			->						Tuple:






									'''simple docstring'''

									A         =					[]
									A         =					[]
									A         =					list(self.graph				)[0]
									stack.append(A_				)
									visited.append(A_				)
									A         =					-2
									A         =					[]
									A         =					s
									A         =					False
									A         =					set()

									while True:
																# check if there is any non isolated nodes
																if len(self.graph[s]				) != 0:
																							A         =					s
																							for node in self.graph[s]:
																														if (
																														    visited.count(node[1]				) > 0
																														    and node[1] != parent
																														    and indirect_parents.count(node[1]				) > 0
																														    and not on_the_way_back
																														):
																																					A         =					len(A_				) - 1
																																					while len_stack >= 0:
																																												if stack[len_stack] == node[1]:
																																																			anticipating_nodes.add(node[1]				)
																																																			break
																																												else:
																																																			anticipating_nodes.add(stack[len_stack]				)
																																																			len_stack -= 1
																														if visited.count(node[1]				) < 1:
																																					stack.append(node[1]				)
																																					visited.append(node[1]				)
																																					A         =					node[1]
																																					break

            # check if all the children are visited
																if s == ss:
																							stack.pop()
																							A         =					True
																							if len(A_				) != 0:
																														A         =					stack[len(A_				) - 1]
																else:
																							A         =					False
																							indirect_parents.append(A_				)
																							A         =					s
																							A         =					ss

																# check if se have reached the starting point
																if len(A_				) == 0:
																							return list(A_				)



		def 					A_		( self  :    int				)			->						Optional[int]:






									'''simple docstring'''

									A         =					[]
									A         =					[]
									A         =					list(self.graph				)[0]
									stack.append(A_				)
									visited.append(A_				)
									A         =					-2
									A         =					[]
									A         =					s
									A         =					False
									A         =					set()

									while True:
																# check if there is any non isolated nodes
																if len(self.graph[s]				) != 0:
																							A         =					s
																							for node in self.graph[s]:
																														if (
																														    visited.count(node[1]				) > 0
																														    and node[1] != parent
																														    and indirect_parents.count(node[1]				) > 0
																														    and not on_the_way_back
																														):
																																					A         =					len(A_				) - 1
																																					while len_stack_minus_one >= 0:
																																												if stack[len_stack_minus_one] == node[1]:
																																																			anticipating_nodes.add(node[1]				)
																																																			break
																																												else:
																																																			return True
																														if visited.count(node[1]				) < 1:
																																					stack.append(node[1]				)
																																					visited.append(node[1]				)
																																					A         =					node[1]
																																					break

            # check if all the children are visited
																if s == ss:
																							stack.pop()
																							A         =					True
																							if len(A_				) != 0:
																														A         =					stack[len(A_				) - 1]
																else:
																							A         =					False
																							indirect_parents.append(A_				)
																							A         =					s
																							A         =					ss

																# check if se have reached the starting point
																if len(A_				) == 0:
																							return False



		def 					A_		( self  :    Dict      ,							snake_case  :    Tuple=-2      ,							snake_case  :    List[str]=-1				)			->						str:






									'''simple docstring'''

									A         =					time()
									self.dfs(A_      ,							A_				)
									A         =					time()
									return end - begin



		def 					A_		( self  :    List[str]      ,							snake_case  :    Union[str, Any]=-2				)			->						Dict:






									'''simple docstring'''

									A         =					time()
									self.bfs(A_				)
									A         =					time()
									return end - begin






class      UpperCAmelCase__		:



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






									'''simple docstring'''

									A         =					{}



		def 					A_		( self  :    Any      ,							snake_case  :    Optional[Any]      ,							snake_case  :    str      ,							snake_case  :    List[str]=1				)			->						Dict:






									'''simple docstring'''

									if self.graph.get(A_				):
																# if there already is a edge
																if self.graph[u].count([w, v]				) == 0:
																							self.graph[u].append([w, v]				)
									else:
																# if u does not exist
																A         =					[[w, v]]
									# add the other way
									if self.graph.get(A_				):
																# if there already is a edge
																if self.graph[v].count([w, u]				) == 0:
																							self.graph[v].append([w, u]				)
									else:
																# if u does not exist
																A         =					[[w, u]]



		def 					A_		( self  :    List[Any]      ,							snake_case  :    List[Any]      ,							snake_case  :    List[str]				)			->						List[Any]:






									'''simple docstring'''

									if self.graph.get(A_				):
																for _ in self.graph[u]:
																							if _[1] == v:
																														self.graph[u].remove(A_				)
        # the other way round
									if self.graph.get(A_				):
																for _ in self.graph[v]:
																							if _[1] == u:
																														self.graph[v].remove(A_				)



		def 					A_		( self  :    List[Any]      ,							snake_case  :    List[str]=-2      ,							snake_case  :    List[Any]=-1				)			->						int:






									'''simple docstring'''

									if s == d:
																return []
									A         =					[]
									A         =					[]
									if s == -2:
																A         =					list(self.graph				)[0]
									stack.append(A_				)
									visited.append(A_				)
									A         =					s

									while True:
																# check if there is any non isolated nodes
																if len(self.graph[s]				) != 0:
																							A         =					s
																							for node in self.graph[s]:
																														if visited.count(node[1]				) < 1:
																																					if node[1] == d:
																																												visited.append(A_				)
																																												return visited
																																					else:
																																												stack.append(node[1]				)
																																												visited.append(node[1]				)
																																												A         =					node[1]
																																												break

            # check if all the children are visited
																if s == ss:
																							stack.pop()
																							if len(A_				) != 0:
																														A         =					stack[len(A_				) - 1]
																else:
																							A         =					ss

																# check if se have reached the starting point
																if len(A_				) == 0:
																							return visited



		def 					A_		( self  :    Union[str, Any]      ,							snake_case  :    Optional[int]=-1				)			->						List[Any]:






									'''simple docstring'''

									if c == -1:
																A         =					floor(random() * 10_000				) + 10
									for i in range(A_				):
																# every vertex has max 100 edges
																for _ in range(floor(random() * 102				) + 1				):
																							A         =					floor(random() * c				) + 1
																							if n != i:
																														self.add_pair(A_      ,							A_      ,							1				)



		def 					A_		( self  :    Optional[Any]      ,							snake_case  :    Dict=-2				)			->						List[Any]:






									'''simple docstring'''

									A         =					deque()
									A         =					[]
									if s == -2:
																A         =					list(self.graph				)[0]
									d.append(A_				)
									visited.append(A_				)
									while d:
																A         =					d.popleft()
																if len(self.graph[s]				) != 0:
																							for node in self.graph[s]:
																														if visited.count(node[1]				) < 1:
																																					d.append(node[1]				)
																																					visited.append(node[1]				)
									return visited



		def 					A_		( self  :    Union[str, Any]      ,							snake_case  :    Optional[Any]				)			->						List[Any]:






									'''simple docstring'''

									return len(self.graph[u]				)



		def 					A_		( self  :    List[str]				)			->						List[Any]:






									'''simple docstring'''

									A         =					[]
									A         =					[]
									A         =					list(self.graph				)[0]
									stack.append(A_				)
									visited.append(A_				)
									A         =					-2
									A         =					[]
									A         =					s
									A         =					False
									A         =					set()

									while True:
																# check if there is any non isolated nodes
																if len(self.graph[s]				) != 0:
																							A         =					s
																							for node in self.graph[s]:
																														if (
																														    visited.count(node[1]				) > 0
																														    and node[1] != parent
																														    and indirect_parents.count(node[1]				) > 0
																														    and not on_the_way_back
																														):
																																					A         =					len(A_				) - 1
																																					while len_stack >= 0:
																																												if stack[len_stack] == node[1]:
																																																			anticipating_nodes.add(node[1]				)
																																																			break
																																												else:
																																																			anticipating_nodes.add(stack[len_stack]				)
																																																			len_stack -= 1
																														if visited.count(node[1]				) < 1:
																																					stack.append(node[1]				)
																																					visited.append(node[1]				)
																																					A         =					node[1]
																																					break

            # check if all the children are visited
																if s == ss:
																							stack.pop()
																							A         =					True
																							if len(A_				) != 0:
																														A         =					stack[len(A_				) - 1]
																else:
																							A         =					False
																							indirect_parents.append(A_				)
																							A         =					s
																							A         =					ss

																# check if se have reached the starting point
																if len(A_				) == 0:
																							return list(A_				)



		def 					A_		( self  :    Optional[int]				)			->						Any:






									'''simple docstring'''

									A         =					[]
									A         =					[]
									A         =					list(self.graph				)[0]
									stack.append(A_				)
									visited.append(A_				)
									A         =					-2
									A         =					[]
									A         =					s
									A         =					False
									A         =					set()

									while True:
																# check if there is any non isolated nodes
																if len(self.graph[s]				) != 0:
																							A         =					s
																							for node in self.graph[s]:
																														if (
																														    visited.count(node[1]				) > 0
																														    and node[1] != parent
																														    and indirect_parents.count(node[1]				) > 0
																														    and not on_the_way_back
																														):
																																					A         =					len(A_				) - 1
																																					while len_stack_minus_one >= 0:
																																												if stack[len_stack_minus_one] == node[1]:
																																																			anticipating_nodes.add(node[1]				)
																																																			break
																																												else:
																																																			return True
																														if visited.count(node[1]				) < 1:
																																					stack.append(node[1]				)
																																					visited.append(node[1]				)
																																					A         =					node[1]
																																					break

            # check if all the children are visited
																if s == ss:
																							stack.pop()
																							A         =					True
																							if len(A_				) != 0:
																														A         =					stack[len(A_				) - 1]
																else:
																							A         =					False
																							indirect_parents.append(A_				)
																							A         =					s
																							A         =					ss

																# check if se have reached the starting point
																if len(A_				) == 0:
																							return False



		def 					A_		( self  :    Dict				)			->						Any:






									'''simple docstring'''

									return list(self.graph				)



		def 					A_		( self  :    Optional[int]      ,							snake_case  :    Optional[Any]=-2      ,							snake_case  :    List[str]=-1				)			->						Any:






									'''simple docstring'''

									A         =					time()
									self.dfs(A_      ,							A_				)
									A         =					time()
									return end - begin



		def 					A_		( self  :    Optional[Any]      ,							snake_case  :    List[Any]=-2				)			->						Union[str, Any]:






									'''simple docstring'''

									A         =					time()
									self.bfs(A_				)
									A         =					time()
									return end - begin








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



		def 					A_		( self  :    Any      ,							snake_case  :    Union[str, Any]=0				)			->						Dict:






									'''simple docstring'''

									A         =					floats_tensor((1, 3, 128, 128)      ,							rng=random.Random(snake_case				)				)
									A         =					torch.manual_seed(snake_case				)
									A         =					{
									    '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 					A_		( self  :    str				)			->						Optional[Any]:






									'''simple docstring'''

									A         =					OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint      ,							provider='CPUExecutionProvider'				)
									pipe.set_progress_bar_config(disable=snake_case				)

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

									# started as 128, should now be 512
									assert image.shape == (1, 512, 512, 3)
									A         =					np.array(
									    [0.6974782, 0.68902093, 0.70135885, 0.7583618, 0.7804545, 0.7854912, 0.78667426, 0.78743863, 0.78070223]				)
									assert np.abs(image_slice - expected_slice				).max() < 1E-1



		def 					A_		( self  :    Union[str, Any]				)			->						Optional[int]:






									'''simple docstring'''

									A         =					OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint      ,							provider='CPUExecutionProvider'				)
									A         =					PNDMScheduler.from_config(pipe.scheduler.config      ,							skip_prk_steps=snake_case				)
									pipe.set_progress_bar_config(disable=snake_case				)

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

									assert image.shape == (1, 512, 512, 3)
									A         =					np.array(
									    [0.6898892, 0.59240556, 0.52499527, 0.58866215, 0.52258235, 0.52572715, 0.62414473, 0.6174387, 0.6214964]				)
									assert np.abs(image_slice.flatten() - expected_slice				).max() < 1E-1



		def 					A_		( self  :    List[str]				)			->						str:






									'''simple docstring'''

									A         =					OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint      ,							provider='CPUExecutionProvider'				)
									A         =					DPMSolverMultistepScheduler.from_config(pipe.scheduler.config				)
									pipe.set_progress_bar_config(disable=snake_case				)

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

									assert image.shape == (1, 512, 512, 3)
									A         =					np.array(
									    [0.7659278, 0.76437664, 0.75579107, 0.7691116, 0.77666986, 0.7727672, 0.7758664, 0.7812226, 0.76942515]				)

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



		def 					A_		( self  :    int				)			->						Optional[int]:






									'''simple docstring'''

									A         =					OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint      ,							provider='CPUExecutionProvider'				)
									A         =					EulerDiscreteScheduler.from_config(pipe.scheduler.config				)
									pipe.set_progress_bar_config(disable=snake_case				)

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

									assert image.shape == (1, 512, 512, 3)
									A         =					np.array(
									    [0.6974782, 0.68902093, 0.70135885, 0.7583618, 0.7804545, 0.7854912, 0.78667426, 0.78743863, 0.78070223]				)
									assert np.abs(image_slice.flatten() - expected_slice				).max() < 1E-1



		def 					A_		( self  :    Optional[int]				)			->						Optional[int]:






									'''simple docstring'''

									A         =					OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint      ,							provider='CPUExecutionProvider'				)
									A         =					EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config				)
									pipe.set_progress_bar_config(disable=snake_case				)

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

									assert image.shape == (1, 512, 512, 3)
									A         =					np.array(
									    [0.77424496, 0.773601, 0.7645288, 0.7769598, 0.7772739, 0.7738688, 0.78187233, 0.77879584, 0.767043]				)

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






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



		@property
		def 					A_		( self  :    Tuple				)			->						str:






									'''simple docstring'''

									return (
									    "CUDAExecutionProvider",
									    {
									        "gpu_mem_limit": "15000000000",  # 15GB
									        "arena_extend_strategy": "kSameAsRequested",
									    },
									)



		@property
		def 					A_		( self  :    Optional[int]				)			->						Any:






									'''simple docstring'''

									A         =					ort.SessionOptions()
									A         =					False
									return options



		def 					A_		( self  :    List[Any]				)			->						Any:






									'''simple docstring'''

									A         =					load_image(
									    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
									    '/img2img/sketch-mountains-input.jpg'				)
									A         =					init_image.resize((128, 128)				)
									# using the PNDM scheduler by default
									A         =					OnnxStableDiffusionUpscalePipeline.from_pretrained(
									    'ssube/stable-diffusion-x4-upscaler-onnx'      ,							provider=self.gpu_provider      ,							sess_options=self.gpu_options      ,							)
									pipe.set_progress_bar_config(disable=snake_case				)

									A         =					'A fantasy landscape, trending on artstation'

									A         =					torch.manual_seed(0				)
									A         =					pipe(
									    prompt=snake_case      ,							image=snake_case      ,							guidance_scale=7.5      ,							num_inference_steps=10      ,							generator=snake_case      ,							output_type='np'      ,							)
									A         =					output.images
									A         =					images[0, 255:258, 383:386, -1]

									assert images.shape == (1, 512, 512, 3)
									A         =					np.array([0.4883, 0.4947, 0.4980, 0.4975, 0.4982, 0.4980, 0.5000, 0.5006, 0.4972]				)
									# 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  :    str				)			->						Union[str, Any]:






									'''simple docstring'''

									A         =					load_image(
									    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
									    '/img2img/sketch-mountains-input.jpg'				)
									A         =					init_image.resize((128, 128)				)
									A         =					LMSDiscreteScheduler.from_pretrained(
									    'ssube/stable-diffusion-x4-upscaler-onnx'      ,							subfolder='scheduler'				)
									A         =					OnnxStableDiffusionUpscalePipeline.from_pretrained(
									    'ssube/stable-diffusion-x4-upscaler-onnx'      ,							scheduler=snake_case      ,							provider=self.gpu_provider      ,							sess_options=self.gpu_options      ,							)
									pipe.set_progress_bar_config(disable=snake_case				)

									A         =					'A fantasy landscape, trending on artstation'

									A         =					torch.manual_seed(0				)
									A         =					pipe(
									    prompt=snake_case      ,							image=snake_case      ,							guidance_scale=7.5      ,							num_inference_steps=20      ,							generator=snake_case      ,							output_type='np'      ,							)
									A         =					output.images
									A         =					images[0, 255:258, 383:386, -1]

									assert images.shape == (1, 512, 512, 3)
									A         =					np.array(
									    [0.50173753, 0.50223356, 0.502039, 0.50233036, 0.5023725, 0.5022601, 0.5018758, 0.50234085, 0.50241566]				)
									# TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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



