


import inspect
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import (
	    MODEL_MAPPING,
	    SegformerForImageClassification,
	    SegformerForSemanticSegmentation,
	    SegformerModel,
	)
	from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import SegformerImageProcessor






class     UpperCamelCase(  _a   ):


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Dict						)			->	Any:




											'''simple docstring'''

											__snake_case				=		self.config_class(**self.inputs_dict						)
											self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE     ,    "hidden_sizes"						)						)
											self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE     ,    "num_attention_heads"						)						)
											self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE     ,    "num_encoder_blocks"						)						)






class     UpperCamelCase:


					def __init__(						self	:		str     ,    SCREAMING_SNAKE_CASE	:		Dict     ,    SCREAMING_SNAKE_CASE	:		Dict=1_3     ,    SCREAMING_SNAKE_CASE	:		Any=6_4     ,    SCREAMING_SNAKE_CASE	:		Optional[Any]=3     ,    SCREAMING_SNAKE_CASE	:		Dict=4     ,    SCREAMING_SNAKE_CASE	:		Tuple=[2, 2, 2, 2]     ,    SCREAMING_SNAKE_CASE	:		Dict=[8, 4, 2, 1]     ,    SCREAMING_SNAKE_CASE	:		List[str]=[1_6, 3_2, 6_4, 1_2_8]     ,    SCREAMING_SNAKE_CASE	:		str=[1, 4, 8, 1_6]     ,    SCREAMING_SNAKE_CASE	:		List[Any]=[1, 2, 4, 8]     ,    SCREAMING_SNAKE_CASE	:		List[Any]=True     ,    SCREAMING_SNAKE_CASE	:		Union[str, Any]=True     ,    SCREAMING_SNAKE_CASE	:		int="gelu"     ,    SCREAMING_SNAKE_CASE	:		int=0.1     ,    SCREAMING_SNAKE_CASE	:		Union[str, Any]=0.1     ,    SCREAMING_SNAKE_CASE	:		Optional[int]=0.02     ,    SCREAMING_SNAKE_CASE	:		Tuple=3     ,    SCREAMING_SNAKE_CASE	:		Tuple=None     ,    )			->	str:




											'''simple docstring'''

											__snake_case				=		parent
											__snake_case				=		batch_size
											__snake_case				=		image_size
											__snake_case				=		num_channels
											__snake_case				=		num_encoder_blocks
											__snake_case				=		sr_ratios
											__snake_case				=		depths
											__snake_case				=		hidden_sizes
											__snake_case				=		downsampling_rates
											__snake_case				=		num_attention_heads
											__snake_case				=		is_training
											__snake_case				=		use_labels
											__snake_case				=		hidden_act
											__snake_case				=		hidden_dropout_prob
											__snake_case				=		attention_probs_dropout_prob
											__snake_case				=		initializer_range
											__snake_case				=		num_labels
											__snake_case				=		scope


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Optional[int]						)			->	Tuple:




											'''simple docstring'''

											__snake_case				=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

											__snake_case				=		None
											if self.use_labels:
																	__snake_case				=		ids_tensor([self.batch_size, self.image_size, self.image_size]     ,    self.num_labels						)

											__snake_case				=		self.get_config()
											return config, pixel_values, labels


					def 							SCREAMING_SNAKE_CASE_  (						self	:		str						)			->	str:




											'''simple docstring'''

											return SegformerConfig(
											    image_size=self.image_size     ,    num_channels=self.num_channels     ,    num_encoder_blocks=self.num_encoder_blocks     ,    depths=self.depths     ,    hidden_sizes=self.hidden_sizes     ,    num_attention_heads=self.num_attention_heads     ,    hidden_act=self.hidden_act     ,    hidden_dropout_prob=self.hidden_dropout_prob     ,    attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,    initializer_range=self.initializer_range     ,    )


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Tuple     ,    SCREAMING_SNAKE_CASE	:		str     ,    SCREAMING_SNAKE_CASE	:		Any     ,    SCREAMING_SNAKE_CASE	:		str						)			->	List[Any]:




											'''simple docstring'''

											__snake_case				=		SegformerModel(config=SCREAMING_SNAKE_CASE						)
											model.to(SCREAMING_SNAKE_CASE						)
											model.eval()
											__snake_case				=		model(SCREAMING_SNAKE_CASE						)
											__snake_case				=		__snake_case				=		self.image_size // (self.downsampling_rates[-1] * 2)
											self.parent.assertEqual(
											    result.last_hidden_state.shape     ,    (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		List[str]     ,    SCREAMING_SNAKE_CASE	:		Optional[int]     ,    SCREAMING_SNAKE_CASE	:		Any     ,    SCREAMING_SNAKE_CASE	:		Tuple						)			->	List[str]:




											'''simple docstring'''

											__snake_case				=		self.num_labels
											__snake_case				=		SegformerForSemanticSegmentation(SCREAMING_SNAKE_CASE						)
											model.to(SCREAMING_SNAKE_CASE						)
											model.eval()
											__snake_case				=		model(SCREAMING_SNAKE_CASE						)
											self.parent.assertEqual(
											    result.logits.shape     ,    (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)						)
											__snake_case				=		model(SCREAMING_SNAKE_CASE     ,    labels=SCREAMING_SNAKE_CASE						)
											self.parent.assertEqual(
											    result.logits.shape     ,    (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)						)
											self.parent.assertGreater(result.loss     ,    0.0						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Optional[Any]     ,    SCREAMING_SNAKE_CASE	:		List[Any]     ,    SCREAMING_SNAKE_CASE	:		Union[str, Any]     ,    SCREAMING_SNAKE_CASE	:		List[str]						)			->	Dict:




											'''simple docstring'''

											__snake_case				=		1
											__snake_case				=		SegformerForSemanticSegmentation(config=SCREAMING_SNAKE_CASE						)
											model.to(SCREAMING_SNAKE_CASE						)
											model.eval()
											__snake_case				=		torch.randint(0     ,    1     ,    (self.batch_size, self.image_size, self.image_size)						).to(SCREAMING_SNAKE_CASE						)
											__snake_case				=		model(SCREAMING_SNAKE_CASE     ,    labels=SCREAMING_SNAKE_CASE						)
											self.parent.assertGreater(result.loss     ,    0.0						)






					def 							SCREAMING_SNAKE_CASE_  (						self	:		Union[str, Any]						)			->	List[Any]:




											'''simple docstring'''

											__snake_case				=		self.prepare_config_and_inputs()
											__snake_case		,					__snake_case		,					__snake_case				=		config_and_inputs
											__snake_case				=		{"pixel_values": pixel_values}
											return config, inputs_dict






@require_torch
class     UpperCamelCase(  _a     ,							_a     ,							unittest.TestCase   ):
					snake_case_		:				Union[str, Any]										=	(
					    (
					        SegformerModel,
					        SegformerForSemanticSegmentation,
					        SegformerForImageClassification,
					    )
					    if is_torch_available()
					    else ()
					)
					snake_case_		:				Dict										=	(
					    {
					        """feature-extraction""": SegformerModel,
					        """image-classification""": SegformerForImageClassification,
					        """image-segmentation""": SegformerForSemanticSegmentation,
					    }
					    if is_torch_available()
					    else {}
					)

					snake_case_		:				Tuple										=	True
					snake_case_		:				int										=	False
					snake_case_		:				Tuple										=	False
					snake_case_		:				Optional[Any]										=	False


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Any						)			->	Tuple:




											'''simple docstring'''

											__snake_case				=		SegformerModelTester(self						)
											__snake_case				=		SegformerConfigTester(self     ,    config_class=SCREAMING_SNAKE_CASE						)


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




											'''simple docstring'''

											self.config_tester.run_common_tests()


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




											'''simple docstring'''

											__snake_case				=		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE						)


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




											'''simple docstring'''

											__snake_case				=		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_binary_image_segmentation(*SCREAMING_SNAKE_CASE						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		str						)			->	Tuple:




											'''simple docstring'''

											__snake_case				=		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_segmentation(*SCREAMING_SNAKE_CASE						)


					@unittest.skip("SegFormer does not use inputs_embeds"						)
					def 							SCREAMING_SNAKE_CASE_  (						self	:		Tuple						)			->	Tuple:




											'''simple docstring'''

											pass


					@unittest.skip("SegFormer does not have get_input_embeddings method and get_output_embeddings methods"						)
					def 							SCREAMING_SNAKE_CASE_  (						self	:		List[Any]						)			->	str:




											'''simple docstring'''

											pass


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




											'''simple docstring'''

											__snake_case		,					__snake_case				=		self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																	__snake_case				=		model_class(SCREAMING_SNAKE_CASE						)
																	__snake_case				=		inspect.signature(model.forward						)
																	# signature.parameters is an OrderedDict => so arg_names order is deterministic
																	__snake_case				=		[*signature.parameters.keys()]

																	__snake_case				=		["pixel_values"]
																	self.assertListEqual(arg_names[:1]     ,    SCREAMING_SNAKE_CASE						)


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




											'''simple docstring'''

											__snake_case		,					__snake_case				=		self.model_tester.prepare_config_and_inputs_for_common()
											__snake_case				=		True

											for model_class in self.all_model_classes:
																	__snake_case				=		True
																	__snake_case				=		False
																	__snake_case				=		True
																	__snake_case				=		model_class(SCREAMING_SNAKE_CASE						)
																	model.to(SCREAMING_SNAKE_CASE						)
																	model.eval()
																	with torch.no_grad():
																							__snake_case				=		model(**self._prepare_for_class(SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE						)						)
																	__snake_case				=		outputs.attentions

																	__snake_case				=		sum(self.model_tester.depths						)
																	self.assertEqual(len(SCREAMING_SNAKE_CASE						)     ,    SCREAMING_SNAKE_CASE						)

																	# check that output_attentions also work using config
																	del inputs_dict["output_attentions"]
																	__snake_case				=		True
																	__snake_case				=		model_class(SCREAMING_SNAKE_CASE						)
																	model.to(SCREAMING_SNAKE_CASE						)
																	model.eval()
																	with torch.no_grad():
																							__snake_case				=		model(**self._prepare_for_class(SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE						)						)
																	__snake_case				=		outputs.attentions

																	self.assertEqual(len(SCREAMING_SNAKE_CASE						)     ,    SCREAMING_SNAKE_CASE						)

																	# verify the first attentions (first block, first layer)
																	__snake_case				=		(self.model_tester.image_size // 4) ** 2
																	__snake_case				=		(self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
																	self.assertListEqual(
																	    list(attentions[0].shape[-3:]						)     ,    [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]     ,    )

																	# verify the last attentions (last block, last layer)
																	__snake_case				=		(self.model_tester.image_size // 3_2) ** 2
																	__snake_case				=		(self.model_tester.image_size // (3_2 * self.model_tester.sr_ratios[-1])) ** 2
																	self.assertListEqual(
																	    list(attentions[-1].shape[-3:]						)     ,    [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]     ,    )
																	__snake_case				=		len(SCREAMING_SNAKE_CASE						)

																	# Check attention is always last and order is fine
																	__snake_case				=		True
																	__snake_case				=		True
																	__snake_case				=		model_class(SCREAMING_SNAKE_CASE						)
																	model.to(SCREAMING_SNAKE_CASE						)
																	model.eval()
																	with torch.no_grad():
																							__snake_case				=		model(**self._prepare_for_class(SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE						)						)

																	self.assertEqual(out_len + 1     ,    len(SCREAMING_SNAKE_CASE						)						)

																	__snake_case				=		outputs.attentions

																	self.assertEqual(len(SCREAMING_SNAKE_CASE						)     ,    SCREAMING_SNAKE_CASE						)
																	# verify the first attentions (first block, first layer)
																	__snake_case				=		(self.model_tester.image_size // 4) ** 2
																	__snake_case				=		(self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
																	self.assertListEqual(
																	    list(self_attentions[0].shape[-3:]						)     ,    [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]     ,    )


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




											'''simple docstring'''

											def check_hidden_states_output(SCREAMING_SNAKE_CASE	:		List[str]     ,    SCREAMING_SNAKE_CASE	:		List[str]     ,    SCREAMING_SNAKE_CASE	:		int						):
																	__snake_case				=		model_class(SCREAMING_SNAKE_CASE						)
																	model.to(SCREAMING_SNAKE_CASE						)
																	model.eval()

																	with torch.no_grad():
																							__snake_case				=		model(**self._prepare_for_class(SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE						)						)

																	__snake_case				=		outputs.hidden_states

																	__snake_case				=		self.model_tester.num_encoder_blocks
																	self.assertEqual(len(SCREAMING_SNAKE_CASE						)     ,    SCREAMING_SNAKE_CASE						)

																	# verify the first hidden states (first block)
																	self.assertListEqual(
																	    list(hidden_states[0].shape[-3:]						)     ,    [
																	        self.model_tester.hidden_sizes[0],
																	        self.model_tester.image_size // 4,
																	        self.model_tester.image_size // 4,
																	    ]     ,    )

											__snake_case		,					__snake_case				=		self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																	__snake_case				=		True
																	check_hidden_states_output(SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE						)

																	# check that output_hidden_states also work using config
																	del inputs_dict["output_hidden_states"]
																	__snake_case				=		True

																	check_hidden_states_output(SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Any						)			->	int:




											'''simple docstring'''

											if not self.model_tester.is_training:
																	return

											__snake_case		,					__snake_case				=		self.model_tester.prepare_config_and_inputs_for_common()
											__snake_case				=		True

											for model_class in self.all_model_classes:
																	if model_class in get_values(SCREAMING_SNAKE_CASE						):
																							continue

																	__snake_case				=		model_class(SCREAMING_SNAKE_CASE						)
																	model.to(SCREAMING_SNAKE_CASE						)
																	model.train()
																	__snake_case				=		self._prepare_for_class(SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE     ,    return_labels=SCREAMING_SNAKE_CASE						)
																	__snake_case				=		model(**SCREAMING_SNAKE_CASE						).loss
																	loss.backward()


					@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."						)
					def 							SCREAMING_SNAKE_CASE_  (						self	:		Union[str, Any]						)			->	Any:




											'''simple docstring'''

											pass






					@slow
					def 							SCREAMING_SNAKE_CASE_  (						self	:		Any						)			->	int:




											'''simple docstring'''

											for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	__snake_case				=		SegformerModel.from_pretrained(SCREAMING_SNAKE_CASE						)
																	self.assertIsNotNone(SCREAMING_SNAKE_CASE						)





def 	_lowerCAmelCase       (    )						->	Optional[Any]:


						'''simple docstring'''






						__snake_case				=		Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
						return image






@require_torch
class     UpperCamelCase(  unittest.TestCase   ):


					@slow
					def 							SCREAMING_SNAKE_CASE_  (						self	:		str						)			->	int:




											'''simple docstring'''

											__snake_case				=		SegformerImageProcessor(
											    image_scale=(5_1_2, 5_1_2)     ,    keep_ratio=SCREAMING_SNAKE_CASE     ,    align=SCREAMING_SNAKE_CASE     ,    do_random_crop=SCREAMING_SNAKE_CASE						)
											__snake_case				=		SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512"						).to(
											    SCREAMING_SNAKE_CASE						)

											__snake_case				=		prepare_img()
											__snake_case				=		image_processor(images=SCREAMING_SNAKE_CASE     ,    return_tensors="pt"						)
											__snake_case				=		encoded_inputs.pixel_values.to(SCREAMING_SNAKE_CASE						)

											with torch.no_grad():
																	__snake_case				=		model(SCREAMING_SNAKE_CASE						)

											__snake_case				=		torch.Size((1, model.config.num_labels, 1_2_8, 1_2_8)						)
											self.assertEqual(outputs.logits.shape     ,    SCREAMING_SNAKE_CASE						)

											__snake_case				=		torch.tensor(
											    [
											        [[-4.6310, -5.5232, -6.2356], [-5.1921, -6.1444, -6.5996], [-5.4424, -6.2790, -6.7574]],
											        [[-12.1391, -13.3122, -13.9554], [-12.8732, -13.9352, -14.3563], [-12.9438, -13.8226, -14.2513]],
											        [[-12.5134, -13.4686, -14.4915], [-12.8669, -14.4343, -14.7758], [-13.2523, -14.5819, -15.0694]],
											    ]						).to(SCREAMING_SNAKE_CASE						)
											self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]     ,    SCREAMING_SNAKE_CASE     ,    atol=1e-4						)						)


					@slow
					def 							SCREAMING_SNAKE_CASE_  (						self	:		List[Any]						)			->	Optional[Any]:




											'''simple docstring'''

											__snake_case				=		SegformerImageProcessor(
											    image_scale=(5_1_2, 5_1_2)     ,    keep_ratio=SCREAMING_SNAKE_CASE     ,    align=SCREAMING_SNAKE_CASE     ,    do_random_crop=SCREAMING_SNAKE_CASE						)
											__snake_case				=		SegformerForSemanticSegmentation.from_pretrained(
											    "nvidia/segformer-b1-finetuned-cityscapes-1024-1024"						).to(SCREAMING_SNAKE_CASE						)

											__snake_case				=		prepare_img()
											__snake_case				=		image_processor(images=SCREAMING_SNAKE_CASE     ,    return_tensors="pt"						)
											__snake_case				=		encoded_inputs.pixel_values.to(SCREAMING_SNAKE_CASE						)

											with torch.no_grad():
																	__snake_case				=		model(SCREAMING_SNAKE_CASE						)

											__snake_case				=		torch.Size((1, model.config.num_labels, 1_2_8, 1_2_8)						)
											self.assertEqual(outputs.logits.shape     ,    SCREAMING_SNAKE_CASE						)

											__snake_case				=		torch.tensor(
											    [
											        [[-13.5748, -13.9111, -12.6500], [-14.3500, -15.3683, -14.2328], [-14.7532, -16.0424, -15.6087]],
											        [[-17.1651, -15.8725, -12.9653], [-17.2580, -17.3718, -14.8223], [-16.6058, -16.8783, -16.7452]],
											        [[-3.6456, -3.0209, -1.4203], [-3.0797, -3.1959, -2.0000], [-1.8757, -1.9217, -1.6997]],
											    ]						).to(SCREAMING_SNAKE_CASE						)
											self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]     ,    SCREAMING_SNAKE_CASE     ,    atol=1e-1						)						)






					@slow
					def 							SCREAMING_SNAKE_CASE_  (						self	:		str						)			->	List[Any]:




											'''simple docstring'''

											__snake_case				=		SegformerImageProcessor(
											    image_scale=(5_1_2, 5_1_2)     ,    keep_ratio=SCREAMING_SNAKE_CASE     ,    align=SCREAMING_SNAKE_CASE     ,    do_random_crop=SCREAMING_SNAKE_CASE						)
											__snake_case				=		SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512"						).to(
											    SCREAMING_SNAKE_CASE						)

											__snake_case				=		prepare_img()
											__snake_case				=		image_processor(images=SCREAMING_SNAKE_CASE     ,    return_tensors="pt"						)
											__snake_case				=		encoded_inputs.pixel_values.to(SCREAMING_SNAKE_CASE						)

											with torch.no_grad():
																	__snake_case				=		model(SCREAMING_SNAKE_CASE						)

											__snake_case				=		outputs.logits.detach().cpu()

											__snake_case				=		image_processor.post_process_semantic_segmentation(outputs=SCREAMING_SNAKE_CASE     ,    target_sizes=[(5_0_0, 3_0_0)]						)
											__snake_case				=		torch.Size((5_0_0, 3_0_0)						)
											self.assertEqual(segmentation[0].shape     ,    SCREAMING_SNAKE_CASE						)

											__snake_case				=		image_processor.post_process_semantic_segmentation(outputs=SCREAMING_SNAKE_CASE						)
											__snake_case				=		torch.Size((1_2_8, 1_2_8)						)
											self.assertEqual(segmentation[0].shape     ,    SCREAMING_SNAKE_CASE						)









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


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Dict     ,    SCREAMING_SNAKE_CASE	:		List[str]=0						)			->	Union[str, Any]:




											'''simple docstring'''

											__snake_case				=		floats_tensor((1, 3, 1_2_8, 1_2_8)     ,    rng=random.Random(SCREAMING_SNAKE_CASE						)						)
											__snake_case				=		torch.manual_seed(SCREAMING_SNAKE_CASE						)
											__snake_case				=		{
											    "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 							SCREAMING_SNAKE_CASE_  (						self	:		List[str]						)			->	List[str]:




											'''simple docstring'''

											__snake_case				=		OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint     ,    provider="CPUExecutionProvider"						)
											pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE						)

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

											# started as 128, should now be 512
											assert image.shape == (1, 5_1_2, 5_1_2, 3)
											__snake_case				=		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 							SCREAMING_SNAKE_CASE_  (						self	:		List[str]						)			->	Optional[Any]:




											'''simple docstring'''

											__snake_case				=		OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint     ,    provider="CPUExecutionProvider"						)
											__snake_case				=		PNDMScheduler.from_config(pipe.scheduler.config     ,    skip_prk_steps=SCREAMING_SNAKE_CASE						)
											pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE						)

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

											assert image.shape == (1, 5_1_2, 5_1_2, 3)
											__snake_case				=		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 							SCREAMING_SNAKE_CASE_  (						self	:		Any						)			->	Optional[int]:




											'''simple docstring'''

											__snake_case				=		OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint     ,    provider="CPUExecutionProvider"						)
											__snake_case				=		DPMSolverMultistepScheduler.from_config(pipe.scheduler.config						)
											pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE						)

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

											assert image.shape == (1, 5_1_2, 5_1_2, 3)
											__snake_case				=		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 							SCREAMING_SNAKE_CASE_  (						self	:		Dict						)			->	Dict:




											'''simple docstring'''

											__snake_case				=		OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint     ,    provider="CPUExecutionProvider"						)
											__snake_case				=		EulerDiscreteScheduler.from_config(pipe.scheduler.config						)
											pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE						)

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

											assert image.shape == (1, 5_1_2, 5_1_2, 3)
											__snake_case				=		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 							SCREAMING_SNAKE_CASE_  (						self	:		List[Any]						)			->	List[str]:




											'''simple docstring'''

											__snake_case				=		OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint     ,    provider="CPUExecutionProvider"						)
											__snake_case				=		EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config						)
											pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE						)

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

											assert image.shape == (1, 5_1_2, 5_1_2, 3)
											__snake_case				=		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 							SCREAMING_SNAKE_CASE_  (						self	:		str						)			->	List[Any]:




											'''simple docstring'''

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


					@property
					def 							SCREAMING_SNAKE_CASE_  (						self	:		Tuple						)			->	Tuple:




											'''simple docstring'''

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


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




											'''simple docstring'''

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

											__snake_case				=		"A fantasy landscape, trending on artstation"

											__snake_case				=		torch.manual_seed(0						)
											__snake_case				=		pipe(
											    prompt=SCREAMING_SNAKE_CASE     ,    image=SCREAMING_SNAKE_CASE     ,    guidance_scale=7.5     ,    num_inference_steps=1_0     ,    generator=SCREAMING_SNAKE_CASE     ,    output_type="np"     ,    )
											__snake_case				=		output.images
											__snake_case				=		images[0, 2_5_5:2_5_8, 3_8_3:3_8_6, -1]

											assert images.shape == (1, 5_1_2, 5_1_2, 3)
											__snake_case				=		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 							SCREAMING_SNAKE_CASE_  (						self	:		Tuple						)			->	Optional[int]:




											'''simple docstring'''

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

											__snake_case				=		"A fantasy landscape, trending on artstation"

											__snake_case				=		torch.manual_seed(0						)
											__snake_case				=		pipe(
											    prompt=SCREAMING_SNAKE_CASE     ,    image=SCREAMING_SNAKE_CASE     ,    guidance_scale=7.5     ,    num_inference_steps=2_0     ,    generator=SCREAMING_SNAKE_CASE     ,    output_type="np"     ,    )
											__snake_case				=		output.images
											__snake_case				=		images[0, 2_5_5:2_5_8, 3_8_3:3_8_6, -1]

											assert images.shape == (1, 5_1_2, 5_1_2, 3)
											__snake_case				=		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





