"""simple docstring"""







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_speech_available,
    is_tf_available,
    is_torch_available,
)


A    =			{
    'configuration_speech_to_text': ['SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Speech2TextConfig'],
    'processing_speech_to_text': ['Speech2TextProcessor'],
}

try:
							if not is_sentencepiece_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							A    =			['Speech2TextTokenizer']

try:
							if not is_speech_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							A    =			['Speech2TextFeatureExtractor']

try:
							if not is_tf_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							A    =			[
							    'TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST',
							    'TFSpeech2TextForConditionalGeneration',
							    'TFSpeech2TextModel',
							    'TFSpeech2TextPreTrainedModel',
							]

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							A    =			[
							    'SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST',
							    'Speech2TextForConditionalGeneration',
							    'Speech2TextModel',
							    'Speech2TextPreTrainedModel',
							]


if TYPE_CHECKING:
							from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig
							from .processing_speech_to_text import SpeechaTextProcessor

							try:
														if not is_sentencepiece_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .tokenization_speech_to_text import SpeechaTextTokenizer

							try:
														if not is_speech_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor

							try:
														if not is_tf_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_tf_speech_to_text import (
														    TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    TFSpeechaTextForConditionalGeneration,
														    TFSpeechaTextModel,
														    TFSpeechaTextPreTrainedModel,
														)

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_speech_to_text import (
														    SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    SpeechaTextForConditionalGeneration,
														    SpeechaTextModel,
														    SpeechaTextPreTrainedModel,
														)

else:
							import sys

							A    =			_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)








"""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



