




import tempfile
import unittest

import numpy as np

from diffusers import (
    DDIMScheduler,
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    OnnxStableDiffusionPipeline,
    PNDMScheduler,
)
from diffusers.utils.testing_utils import is_onnx_available, nightly, require_onnxruntime, require_torch_gpu

from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin


if is_onnx_available():
	import onnxruntime as ort




class 		__A      (			a__					,						unittest.TestCase			):
						UpperCamelCase     						=					"hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline"

						def     A__						(		self   :List[Any]    , __snake_case   :Tuple=0 ):
										'''simple docstring'''

										__magic_name__	:	int								=np.random.RandomState(lowerCamelCase_ )
										__magic_name__	:	Optional[Any]								={
										    """prompt""": """A painting of a squirrel eating a burger""",
										    """generator""": generator,
										    """num_inference_steps""": 2,
										    """guidance_scale""": 7.5,
										    """output_type""": """numpy""",
										}
										return inputs

						def     A__						(		self   :Optional[Any] ):
										'''simple docstring'''

										__magic_name__	:	List[str]								=OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint    , provider="""CPUExecutionProvider""" )
										pipe.set_progress_bar_config(disable=lowerCamelCase_ )

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

										assert image.shape == (1, 1_28, 1_28, 3)
										__magic_name__	:	Any								=np.array([0.65072, 0.58492, 0.48219, 0.55521, 0.53180, 0.55939, 0.50697, 0.39800, 0.46455] )

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

						def     A__						(		self   :Any ):
										'''simple docstring'''

										__magic_name__	:	str								=OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint    , provider="""CPUExecutionProvider""" )
										__magic_name__	:	Tuple								=PNDMScheduler.from_config(pipe.scheduler.config    , skip_prk_steps=lowerCamelCase_ )
										pipe.set_progress_bar_config(disable=lowerCamelCase_ )

										__magic_name__	:	List[Any]								=self.get_dummy_inputs()
										__magic_name__	:	List[Any]								=pipe(**lowerCamelCase_ ).images
										__magic_name__	:	Optional[Any]								=image[0, -3:, -3:, -1]

										assert image.shape == (1, 1_28, 1_28, 3)
										__magic_name__	:	List[str]								=np.array([0.65863, 0.59425, 0.49326, 0.56313, 0.53875, 0.56627, 0.51065, 0.39777, 0.46330] )

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

						def     A__						(		self   :Tuple ):
										'''simple docstring'''

										__magic_name__	:	Tuple								=OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint    , provider="""CPUExecutionProvider""" )
										__magic_name__	:	List[str]								=LMSDiscreteScheduler.from_config(pipe.scheduler.config )
										pipe.set_progress_bar_config(disable=lowerCamelCase_ )

										__magic_name__	:	Optional[Any]								=self.get_dummy_inputs()
										__magic_name__	:	List[str]								=pipe(**lowerCamelCase_ ).images
										__magic_name__	:	Tuple								=image[0, -3:, -3:, -1]

										assert image.shape == (1, 1_28, 1_28, 3)
										__magic_name__	:	Dict								=np.array([0.53755, 0.60786, 0.47402, 0.49488, 0.51869, 0.49819, 0.47985, 0.38957, 0.44279] )

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

						def     A__						(		self   :Optional[Any] ):
										'''simple docstring'''

										__magic_name__	:	Optional[int]								=OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint    , provider="""CPUExecutionProvider""" )
										__magic_name__	:	Optional[int]								=EulerDiscreteScheduler.from_config(pipe.scheduler.config )
										pipe.set_progress_bar_config(disable=lowerCamelCase_ )

										__magic_name__	:	Union[str, Any]								=self.get_dummy_inputs()
										__magic_name__	:	Optional[Any]								=pipe(**lowerCamelCase_ ).images
										__magic_name__	:	int								=image[0, -3:, -3:, -1]

										assert image.shape == (1, 1_28, 1_28, 3)
										__magic_name__	:	Union[str, Any]								=np.array([0.53755, 0.60786, 0.47402, 0.49488, 0.51869, 0.49819, 0.47985, 0.38957, 0.44279] )

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

						def     A__						(		self   :Optional[Any] ):
										'''simple docstring'''

										__magic_name__	:	Optional[Any]								=OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint    , provider="""CPUExecutionProvider""" )
										__magic_name__	:	str								=EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config )
										pipe.set_progress_bar_config(disable=lowerCamelCase_ )

										__magic_name__	:	Dict								=self.get_dummy_inputs()
										__magic_name__	:	List[str]								=pipe(**lowerCamelCase_ ).images
										__magic_name__	:	Dict								=image[0, -3:, -3:, -1]

										assert image.shape == (1, 1_28, 1_28, 3)
										__magic_name__	:	int								=np.array([0.53817, 0.60812, 0.47384, 0.49530, 0.51894, 0.49814, 0.47984, 0.38958, 0.44271] )

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

						def     A__						(		self   :str ):
										'''simple docstring'''

										__magic_name__	:	Tuple								=OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint    , provider="""CPUExecutionProvider""" )
										__magic_name__	:	int								=DPMSolverMultistepScheduler.from_config(pipe.scheduler.config )
										pipe.set_progress_bar_config(disable=lowerCamelCase_ )

										__magic_name__	:	Union[str, Any]								=self.get_dummy_inputs()
										__magic_name__	:	int								=pipe(**lowerCamelCase_ ).images
										__magic_name__	:	Optional[Any]								=image[0, -3:, -3:, -1]

										assert image.shape == (1, 1_28, 1_28, 3)
										__magic_name__	:	List[Any]								=np.array([0.53895, 0.60808, 0.47933, 0.49608, 0.51886, 0.49950, 0.48053, 0.38957, 0.44200] )

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

						def     A__						(		self   :Optional[int] ):
										'''simple docstring'''

										__magic_name__	:	Tuple								=OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint    , provider="""CPUExecutionProvider""" )
										pipe.set_progress_bar_config(disable=lowerCamelCase_ )

										__magic_name__	:	List[Any]								=self.get_dummy_inputs()
										__magic_name__	:	Tuple								=3 * [inputs["""prompt"""]]

										# forward
										__magic_name__	:	Any								=pipe(**lowerCamelCase_ )
										__magic_name__	:	Optional[int]								=output.images[0, -3:, -3:, -1]

										__magic_name__	:	Any								=self.get_dummy_inputs()
										__magic_name__	:	Any								=3 * [inputs.pop("""prompt""" )]

										__magic_name__	:	List[Any]								=pipe.tokenizer(
										    lowerCamelCase_    , padding="""max_length"""    , max_length=pipe.tokenizer.model_max_length    , truncation=lowerCamelCase_    , return_tensors="""np"""    , )
										__magic_name__	:	Dict								=text_inputs["""input_ids"""]

										__magic_name__	:	Optional[int]								=pipe.text_encoder(input_ids=text_inputs.astype(np.intaa ) )[0]

										__magic_name__	:	Optional[int]								=prompt_embeds

										# forward
										__magic_name__	:	List[str]								=pipe(**lowerCamelCase_ )
										__magic_name__	:	List[str]								=output.images[0, -3:, -3:, -1]

										assert np.abs(image_slice_a.flatten() - image_slice_a.flatten() ).max() < 1E-4


						def     A__						(		self   :Optional[Any] ):
										'''simple docstring'''

										__magic_name__	:	Union[str, Any]								=OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint    , provider="""CPUExecutionProvider""" )
										pipe.set_progress_bar_config(disable=lowerCamelCase_ )

										__magic_name__	:	List[str]								=self.get_dummy_inputs()
										__magic_name__	:	Optional[Any]								=3 * ["""this is a negative prompt"""]
										__magic_name__	:	str								=negative_prompt
										__magic_name__	:	Optional[Any]								=3 * [inputs["""prompt"""]]

										# forward
										__magic_name__	:	int								=pipe(**lowerCamelCase_ )
										__magic_name__	:	int								=output.images[0, -3:, -3:, -1]

										__magic_name__	:	int								=self.get_dummy_inputs()
										__magic_name__	:	List[str]								=3 * [inputs.pop("""prompt""" )]

										__magic_name__	:	Union[str, Any]								=[]
										for p in [prompt, negative_prompt]:
														__magic_name__	:	Union[str, Any]								=pipe.tokenizer(
														    lowerCamelCase_    , padding="""max_length"""    , max_length=pipe.tokenizer.model_max_length    , truncation=lowerCamelCase_    , return_tensors="""np"""    , )
														__magic_name__	:	Optional[int]								=text_inputs["""input_ids"""]

														embeds.append(pipe.text_encoder(input_ids=text_inputs.astype(np.intaa ) )[0] )

										__magic_name__				, __magic_name__	:	Union[str, Any]								=embeds

										# forward
										__magic_name__	:	Dict								=pipe(**lowerCamelCase_ )
										__magic_name__	:	List[Any]								=output.images[0, -3:, -3:, -1]

										assert np.abs(image_slice_a.flatten() - image_slice_a.flatten() ).max() < 1E-4




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

						@property
						def     A__						(		self   :Tuple ):
										'''simple docstring'''

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

						@property
						def     A__						(		self   :Union[str, Any] ):
										'''simple docstring'''

										__magic_name__	:	List[Any]								=ort.SessionOptions()
										__magic_name__	:	Any								=False
										return options

						def     A__						(		self   :Optional[int] ):
										'''simple docstring'''

										__magic_name__	:	int								=OnnxStableDiffusionPipeline.from_pretrained(
										    """CompVis/stable-diffusion-v1-4"""    , revision="""onnx"""    , safety_checker=lowerCamelCase_    , feature_extractor=lowerCamelCase_    , provider=self.gpu_provider    , sess_options=self.gpu_options    , )
										sd_pipe.set_progress_bar_config(disable=lowerCamelCase_ )

										__magic_name__	:	Union[str, Any]								="""A painting of a squirrel eating a burger"""
										np.random.seed(0 )
										__magic_name__	:	Tuple								=sd_pipe([prompt]    , guidance_scale=6.0    , num_inference_steps=10    , output_type="""np""" )
										__magic_name__	:	Optional[Any]								=output.images

										__magic_name__	:	int								=image[0, -3:, -3:, -1]

										assert image.shape == (1, 5_12, 5_12, 3)
										__magic_name__	:	Optional[Any]								=np.array([0.0452, 0.0390, 0.0087, 0.0350, 0.0617, 0.0364, 0.0544, 0.0523, 0.0720] )

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

						def     A__						(		self   :Union[str, Any] ):
										'''simple docstring'''

										__magic_name__	:	Any								=DDIMScheduler.from_pretrained(
										    """runwayml/stable-diffusion-v1-5"""    , subfolder="""scheduler"""    , revision="""onnx""" )
										__magic_name__	:	Optional[int]								=OnnxStableDiffusionPipeline.from_pretrained(
										    """runwayml/stable-diffusion-v1-5"""    , revision="""onnx"""    , scheduler=lowerCamelCase_    , safety_checker=lowerCamelCase_    , feature_extractor=lowerCamelCase_    , provider=self.gpu_provider    , sess_options=self.gpu_options    , )
										sd_pipe.set_progress_bar_config(disable=lowerCamelCase_ )

										__magic_name__	:	str								="""open neural network exchange"""
										__magic_name__	:	Tuple								=np.random.RandomState(0 )
										__magic_name__	:	List[Any]								=sd_pipe([prompt]    , guidance_scale=7.5    , num_inference_steps=10    , generator=lowerCamelCase_    , output_type="""np""" )
										__magic_name__	:	Optional[int]								=output.images
										__magic_name__	:	List[str]								=image[0, -3:, -3:, -1]

										assert image.shape == (1, 5_12, 5_12, 3)
										__magic_name__	:	Optional[Any]								=np.array([0.2867, 0.1974, 0.1481, 0.7294, 0.7251, 0.6667, 0.4194, 0.5642, 0.6486] )

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

						def     A__						(		self   :Union[str, Any] ):
										'''simple docstring'''

										__magic_name__	:	str								=LMSDiscreteScheduler.from_pretrained(
										    """runwayml/stable-diffusion-v1-5"""    , subfolder="""scheduler"""    , revision="""onnx""" )
										__magic_name__	:	Optional[Any]								=OnnxStableDiffusionPipeline.from_pretrained(
										    """runwayml/stable-diffusion-v1-5"""    , revision="""onnx"""    , scheduler=lowerCamelCase_    , safety_checker=lowerCamelCase_    , feature_extractor=lowerCamelCase_    , provider=self.gpu_provider    , sess_options=self.gpu_options    , )
										sd_pipe.set_progress_bar_config(disable=lowerCamelCase_ )

										__magic_name__	:	Optional[int]								="""open neural network exchange"""
										__magic_name__	:	str								=np.random.RandomState(0 )
										__magic_name__	:	Optional[Any]								=sd_pipe([prompt]    , guidance_scale=7.5    , num_inference_steps=10    , generator=lowerCamelCase_    , output_type="""np""" )
										__magic_name__	:	Dict								=output.images
										__magic_name__	:	Any								=image[0, -3:, -3:, -1]

										assert image.shape == (1, 5_12, 5_12, 3)
										__magic_name__	:	Dict								=np.array([0.2306, 0.1959, 0.1593, 0.6549, 0.6394, 0.5408, 0.5065, 0.6010, 0.6161] )

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

						def     A__						(		self   :List[str] ):
										'''simple docstring'''

										__magic_name__	:	str								=0

										def test_callback_fn(__snake_case   :Optional[int]    , __snake_case   :Dict    , __snake_case   :Union[str, Any] ) -> None:
														__magic_name__	:	Any								=True
														nonlocal number_of_steps
														number_of_steps += 1
														if step == 0:
																		assert latents.shape == (1, 4, 64, 64)
																		__magic_name__	:	str								=latents[0, -3:, -3:, -1]
																		__magic_name__	:	List[Any]								=np.array(
																		    [-0.6772, -0.3835, -1.2456, 0.1905, -1.0974, 0.6967, -1.9353, 0.0178, 1.0167] )

																		assert np.abs(latents_slice.flatten() - expected_slice ).max() < 1E-3
														elif step == 5:
																		assert latents.shape == (1, 4, 64, 64)
																		__magic_name__	:	Tuple								=latents[0, -3:, -3:, -1]
																		__magic_name__	:	Tuple								=np.array(
																		    [-0.3351, 0.2241, -0.1837, -0.2325, -0.6577, 0.3393, -0.0241, 0.5899, 1.3875] )

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

										__magic_name__	:	Union[str, Any]								=False

										__magic_name__	:	Any								=OnnxStableDiffusionPipeline.from_pretrained(
										    """runwayml/stable-diffusion-v1-5"""    , revision="""onnx"""    , safety_checker=lowerCamelCase_    , feature_extractor=lowerCamelCase_    , provider=self.gpu_provider    , sess_options=self.gpu_options    , )
										pipe.set_progress_bar_config(disable=lowerCamelCase_ )

										__magic_name__	:	List[Any]								="""Andromeda galaxy in a bottle"""

										__magic_name__	:	List[Any]								=np.random.RandomState(0 )
										pipe(
										    prompt=lowerCamelCase_    , num_inference_steps=5    , guidance_scale=7.5    , generator=lowerCamelCase_    , callback=lowerCamelCase_    , callback_steps=1    , )
										assert test_callback_fn.has_been_called
										assert number_of_steps == 6


						def     A__						(		self   :Optional[Any] ):
										'''simple docstring'''

										__magic_name__	:	int								=OnnxStableDiffusionPipeline.from_pretrained(
										    """runwayml/stable-diffusion-v1-5"""    , revision="""onnx"""    , safety_checker=lowerCamelCase_    , feature_extractor=lowerCamelCase_    , provider=self.gpu_provider    , sess_options=self.gpu_options    , )
										assert isinstance(lowerCamelCase_    , lowerCamelCase_ )
										assert pipe.safety_checker is None

										__magic_name__	:	Tuple								=pipe("""example prompt"""    , num_inference_steps=2 ).images[0]
										assert image is not None

										# check that there's no error when saving a pipeline with one of the models being None
										with tempfile.TemporaryDirectory() as tmpdirname:
														pipe.save_pretrained(lowerCamelCase_ )
														__magic_name__	:	Union[str, Any]								=OnnxStableDiffusionPipeline.from_pretrained(lowerCamelCase_ )

										# sanity check that the pipeline still works
										assert pipe.safety_checker is None
										__magic_name__	:	int								=pipe("""example prompt"""    , num_inference_steps=2 ).images[0]
										assert image is not None










from ... import PretrainedConfig


UpperCAmelCase_		:				List[str]									=     {
    "sijunhe/nezha-cn-base": "https://huggingface.co/sijunhe/nezha-cn-base/resolve/main/config.json",
}




class 		__A      (			UpperCamelCase__			):
						UpperCamelCase     						=					NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP
						UpperCamelCase     						=					"""nezha"""

						def __init__(		self   :List[Any]    , __snake_case   :Optional[int]=2_11_28    , __snake_case   :Dict=7_68    , __snake_case   :str=12    , __snake_case   :List[Any]=12    , __snake_case   :Optional[int]=30_72    , __snake_case   :Any="gelu"    , __snake_case   :List[str]=0.1    , __snake_case   :Optional[int]=0.1    , __snake_case   :Dict=5_12    , __snake_case   :Optional[int]=64    , __snake_case   :Any=2    , __snake_case   :List[Any]=0.02    , __snake_case   :List[str]=1E-12    , __snake_case   :Any=0.1    , __snake_case   :str=0    , __snake_case   :int=2    , __snake_case   :str=3    , __snake_case   :Any=True    , **__snake_case   :Any    , ):
										'''simple docstring'''

										super().__init__(pad_token_id=__snake_case    , bos_token_id=__snake_case    , eos_token_id=__snake_case    , **__snake_case )

										__magic_name__	:	Tuple								=vocab_size
										__magic_name__	:	str								=hidden_size
										__magic_name__	:	Dict								=num_hidden_layers
										__magic_name__	:	str								=num_attention_heads
										__magic_name__	:	int								=hidden_act
										__magic_name__	:	Optional[int]								=intermediate_size
										__magic_name__	:	Union[str, Any]								=hidden_dropout_prob
										__magic_name__	:	Any								=attention_probs_dropout_prob
										__magic_name__	:	Union[str, Any]								=max_position_embeddings
										__magic_name__	:	str								=max_relative_position
										__magic_name__	:	Tuple								=type_vocab_size
										__magic_name__	:	str								=initializer_range
										__magic_name__	:	Tuple								=layer_norm_eps
										__magic_name__	:	Optional[int]								=classifier_dropout
										__magic_name__	:	List[str]								=use_cache




