



"""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  lowercase__  (       SCREAMING_SNAKE_CASE							,   unittest.TestCase ):







       '''simple docstring'''


       UpperCamelCase		=	'''ssube/stable-diffusion-x4-upscaler-onnx'''



       def 	lowercase__							(				self     :    str						,							_UpperCAmelCase     :    int=0       )					->		int:


             '''simple docstring'''




             UpperCAmelCase_     					=				floats_tensor((1, 3, 128, 128)						,							rng=random.Random(_UpperCAmelCase       )       )
             UpperCAmelCase_     					=				torch.manual_seed(_UpperCAmelCase       )
             UpperCAmelCase_     					=				{
                 "prompt": "A painting of a squirrel eating a burger",
                 "image": image,
                 "generator": generator,
                 "num_inference_steps": 3,
                 "guidance_scale": 7.5,
                 "output_type": "numpy",
             }
             return inputs



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


             '''simple docstring'''




             UpperCAmelCase_     					=				OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						,							provider="CPUExecutionProvider"       )
             pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

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

             # started as 128, should now be 512
             assert image.shape == (1, 512, 512, 3)
             UpperCAmelCase_     					=				np.array(
                 [0.697_4782, 0.6890_2093, 0.7013_5885, 0.758_3618, 0.780_4545, 0.785_4912, 0.7866_7426, 0.7874_3863, 0.7807_0223]       )
             assert np.abs(image_slice - expected_slice       ).max() < 1e-1



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


             '''simple docstring'''




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

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

             assert image.shape == (1, 512, 512, 3)
             UpperCAmelCase_     					=				np.array(
                 [0.689_8892, 0.5924_0556, 0.5249_9527, 0.5886_6215, 0.5225_8235, 0.5257_2715, 0.6241_4473, 0.617_4387, 0.621_4964]       )
             assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-1



       def 	lowercase__							(				self     :    Any       )					->		Dict:


             '''simple docstring'''




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

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

             assert image.shape == (1, 512, 512, 3)
             UpperCAmelCase_     					=				np.array(
                 [0.765_9278, 0.7643_7664, 0.7557_9107, 0.769_1116, 0.7766_6986, 0.772_7672, 0.775_8664, 0.781_2226, 0.7694_2515]       )

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



       def 	lowercase__							(				self     :    int       )					->		Dict:


             '''simple docstring'''




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

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

             assert image.shape == (1, 512, 512, 3)
             UpperCAmelCase_     					=				np.array(
                 [0.697_4782, 0.6890_2093, 0.7013_5885, 0.758_3618, 0.780_4545, 0.785_4912, 0.7866_7426, 0.7874_3863, 0.7807_0223]       )
             assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-1







       def 	lowercase__							(				self     :    Dict       )					->		Any:


             '''simple docstring'''




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

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

             assert image.shape == (1, 512, 512, 3)
             UpperCAmelCase_     					=				np.array(
                 [0.7742_4496, 0.77_3601, 0.764_5288, 0.776_9598, 0.777_2739, 0.773_8688, 0.7818_7233, 0.7787_9584, 0.76_7043]       )

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



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







       '''simple docstring'''


       @property
       def 	lowercase__							(				self     :    List[str]       )					->		Dict:


             '''simple docstring'''




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



       @property
       def 	lowercase__							(				self     :    Union[str, Any]       )					->		Dict:


             '''simple docstring'''




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



       def 	lowercase__							(				self     :    Optional[Any]       )					->		Any:


             '''simple docstring'''




             UpperCAmelCase_     					=				load_image(
                 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
                 "/img2img/sketch-mountains-input.jpg"       )
             UpperCAmelCase_     					=				init_image.resize((128, 128)       )
             # using the PNDM scheduler by default
             UpperCAmelCase_     					=				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       )

             UpperCAmelCase_     					=				"A fantasy landscape, trending on artstation"

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

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


             '''simple docstring'''




             UpperCAmelCase_     					=				load_image(
                 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
                 "/img2img/sketch-mountains-input.jpg"       )
             UpperCAmelCase_     					=				init_image.resize((128, 128)       )
             UpperCAmelCase_     					=				LMSDiscreteScheduler.from_pretrained(
                 "ssube/stable-diffusion-x4-upscaler-onnx"						,							subfolder="scheduler"       )
             UpperCAmelCase_     					=				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       )

             UpperCAmelCase_     					=				"A fantasy landscape, trending on artstation"

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

             assert images.shape == (1, 512, 512, 3)
             UpperCAmelCase_     					=				np.array(
                 [0.5017_3753, 0.5022_3356, 0.50_2039, 0.5023_3036, 0.502_3725, 0.502_2601, 0.501_8758, 0.5023_4085, 0.5024_1566]       )
             # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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








import gc
import inspect
import unittest

import torch
from parameterized import parameterized

from diffusers import PriorTransformer
from diffusers.utils import floats_tensor, slow, torch_all_close, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin


enable_full_determinism()






class      UpperCamelCase(	_a  ,				unittest.TestCase ):
					snake_case_		:					Any    					=						PriorTransformer
					snake_case_		:					List[str]    					=						"""hidden_states"""


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




											'''simple docstring'''

											__snake_case				=		4
											__snake_case				=		8
											__snake_case				=		7

											__snake_case				=		floats_tensor((batch_size, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )

											__snake_case				=		floats_tensor((batch_size, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )
											__snake_case				=		floats_tensor((batch_size, num_embeddings, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )

											return {
											    "hidden_states": hidden_states,
											    "timestep": 2,
											    "proj_embedding": proj_embedding,
											    "encoder_hidden_states": encoder_hidden_states,
											}


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




											'''simple docstring'''

											torch.manual_seed(SCREAMING_SNAKE_CASE    )
											__snake_case				=		4
											__snake_case				=		8
											__snake_case				=		7

											__snake_case				=		torch.randn((batch_size, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )

											__snake_case				=		torch.randn((batch_size, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )
											__snake_case				=		torch.randn((batch_size, num_embeddings, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )

											return {
											    "hidden_states": hidden_states,
											    "timestep": 2,
											    "proj_embedding": proj_embedding,
											    "encoder_hidden_states": encoder_hidden_states,
											}


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




											'''simple docstring'''

											return (4, 8)


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




											'''simple docstring'''

											return (4, 8)


					def 				SCREAMING_SNAKE_CASE_	(    self						:		str    )      ->						Dict:




											'''simple docstring'''

											__snake_case				=		{
											    "num_attention_heads": 2,
											    "attention_head_dim": 4,
											    "num_layers": 2,
											    "embedding_dim": 8,
											    "num_embeddings": 7,
											    "additional_embeddings": 4,
											}
											__snake_case				=		self.dummy_input
											return init_dict, inputs_dict


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Any    )      ->						Dict:




											'''simple docstring'''

											__snake_case		,       __snake_case				=		PriorTransformer.from_pretrained(
											    "hf-internal-testing/prior-dummy"  ,     output_loading_info=SCREAMING_SNAKE_CASE    )
											self.assertIsNotNone(SCREAMING_SNAKE_CASE    )
											self.assertEqual(len(loading_info["missing_keys"]    )  ,     0    )

											model.to(SCREAMING_SNAKE_CASE    )
											__snake_case				=		model(**self.dummy_input    )[0]

											assert hidden_states is not None, "Make sure output is not None"


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




											'''simple docstring'''

											__snake_case		,       __snake_case				=		self.prepare_init_args_and_inputs_for_common()

											__snake_case				=		self.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				=		["hidden_states", "timestep"]
											self.assertListEqual(arg_names[:2]  ,     SCREAMING_SNAKE_CASE    )






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




											'''simple docstring'''

											__snake_case				=		PriorTransformer.from_pretrained("hf-internal-testing/prior-dummy"    )
											__snake_case				=		model.to(SCREAMING_SNAKE_CASE    )

											if hasattr(SCREAMING_SNAKE_CASE  ,     "set_default_attn_processor"    ):
																	model.set_default_attn_processor()

											__snake_case				=		self.get_dummy_seed_input()

											with torch.no_grad():
																	__snake_case				=		model(**SCREAMING_SNAKE_CASE    )[0]

											__snake_case				=		output[0, :5].flatten().cpu()
											print(SCREAMING_SNAKE_CASE    )

											# Since the VAE Gaussian prior's generator is seeded on the appropriate device,
											# the expected output slices are not the same for CPU and GPU.
											__snake_case				=		torch.tensor([-1.3436, -0.2870, 0.7538, 0.4368, -0.0239]    )
											self.assertTrue(torch_all_close(SCREAMING_SNAKE_CASE  ,     SCREAMING_SNAKE_CASE  ,     rtol=1e-2    )    )






@slow
class      UpperCamelCase(	unittest.TestCase ):


					def 				SCREAMING_SNAKE_CASE_	(    self						:		List[Any]  ,     SCREAMING_SNAKE_CASE						:		int=1  ,     SCREAMING_SNAKE_CASE						:		str=7_6_8  ,     SCREAMING_SNAKE_CASE						:		Dict=7_7  ,     SCREAMING_SNAKE_CASE						:		int=0    )      ->						List[str]:




											'''simple docstring'''

											torch.manual_seed(SCREAMING_SNAKE_CASE    )
											__snake_case				=		batch_size
											__snake_case				=		embedding_dim
											__snake_case				=		num_embeddings

											__snake_case				=		torch.randn((batch_size, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )

											__snake_case				=		torch.randn((batch_size, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )
											__snake_case				=		torch.randn((batch_size, num_embeddings, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )

											return {
											    "hidden_states": hidden_states,
											    "timestep": 2,
											    "proj_embedding": proj_embedding,
											    "encoder_hidden_states": encoder_hidden_states,
											}


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




											'''simple docstring'''

											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()






					@parameterized.expand(
					    [
					        # fmt: off
					        [1_3, [-0.5861,  0.1283, -0.0931,  0.0882,  0.4476,  0.1329, -0.0498,  0.0640]],
					        [3_7, [-0.4913,  0.0110, -0.0483,  0.0541,  0.4954, -0.0170,  0.0354,  0.1651]],
					        # fmt: on
					    ]    )
					def 				SCREAMING_SNAKE_CASE_	(    self						:		Union[str, Any]  ,     SCREAMING_SNAKE_CASE						:		int  ,     SCREAMING_SNAKE_CASE						:		Tuple    )      ->						List[str]:




											'''simple docstring'''

											__snake_case				=		PriorTransformer.from_pretrained("kandinsky-community/kandinsky-2-1-prior"  ,     subfolder="prior"    )
											model.to(SCREAMING_SNAKE_CASE    )
											__snake_case				=		self.get_dummy_seed_input(seed=SCREAMING_SNAKE_CASE    )

											with torch.no_grad():
																	__snake_case				=		model(**SCREAMING_SNAKE_CASE    )[0]

											assert list(sample.shape    ) == [1, 7_6_8]

											__snake_case				=		sample[0, :8].flatten().cpu()
											print(SCREAMING_SNAKE_CASE    )
											__snake_case				=		torch.tensor(SCREAMING_SNAKE_CASE    )

											assert torch_all_close(SCREAMING_SNAKE_CASE  ,     SCREAMING_SNAKE_CASE  ,     atol=1e-3    )





