

"""simple docstring"""




import random
import unittest

import numpy as np

from diffusers import (
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    OnnxStableDiffusionImgaImgPipeline,
    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			(    __lowerCamelCase	,			unittest.TestCase					):
		snake_case_				       =       """hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline"""

		def    __lowercase       (  self		:			Union[str, Any]					,A		:			Tuple=0    ):






			'''simple docstring'''
			UpperCAmelCase__					:  Union[str, Any]					   =					floats_tensor((1, 3, 128, 128)					,rng=random.Random(A    )    )
			UpperCAmelCase__					:  Tuple					   =					np.random.RandomState(A    )
			UpperCAmelCase__					:  List[Any]					   =					{
			    """prompt""": """A painting of a squirrel eating a burger""",
			    """image""": image,
			    """generator""": generator,
			    """num_inference_steps""": 3,
			    """strength""": 0.7_5,
			    """guidance_scale""": 7.5,
			    """output_type""": """numpy""",
			}
			return inputs

		def    __lowercase       (  self		:			Any    ):






			'''simple docstring'''
			UpperCAmelCase__					:  Dict					   =					OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint					,provider="""CPUExecutionProvider"""    )
			pipe.set_progress_bar_config(disable=A    )

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

			assert image.shape == (1, 128, 128, 3)
			UpperCAmelCase__					:  Optional[Any]					   =					np.array([0.6_9_6_4_3, 0.5_8_4_8_4, 0.5_0_3_1_4, 0.5_8_7_6_0, 0.5_5_3_6_8, 0.5_9_6_4_3, 0.5_1_5_2_9, 0.4_1_2_1_7, 0.4_9_0_8_7]    )
			assert np.abs(image_slice - expected_slice    ).max() < 1e-1

		def    __lowercase       (  self		:			Optional[int]    ):






			'''simple docstring'''
			UpperCAmelCase__					:  Dict					   =					OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint					,provider="""CPUExecutionProvider"""    )
			UpperCAmelCase__					:  Tuple					   =					PNDMScheduler.from_config(pipe.scheduler.config					,skip_prk_steps=A    )
			pipe.set_progress_bar_config(disable=A    )

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

			assert image.shape == (1, 128, 128, 3)
			UpperCAmelCase__					:  str					   =					np.array([0.6_1_7_3_7, 0.5_4_6_4_2, 0.5_3_1_8_3, 0.5_4_4_6_5, 0.5_2_7_4_2, 0.6_0_5_2_5, 0.4_9_9_6_9, 0.4_0_6_5_5, 0.4_8_1_5_4]    )

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

		def    __lowercase       (  self		:			List[Any]    ):






			'''simple docstring'''
			UpperCAmelCase__					:  Dict					   =					OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint					,provider="""CPUExecutionProvider"""    )
			UpperCAmelCase__					:  List[str]					   =					LMSDiscreteScheduler.from_config(pipe.scheduler.config    )
			pipe.set_progress_bar_config(disable=A    )

			# warmup pass to apply optimizations
			UpperCAmelCase__					:  List[str]					   =					pipe(**self.get_dummy_inputs()    )

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

			assert image.shape == (1, 128, 128, 3)
			UpperCAmelCase__					:  List[str]					   =					np.array([0.5_2_7_6_1, 0.5_9_9_7_7, 0.4_9_0_3_3, 0.4_9_6_1_9, 0.5_4_2_8_2, 0.5_0_3_1_1, 0.4_7_6_0_0, 0.4_0_9_1_8, 0.4_5_2_0_3]    )

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

		def    __lowercase       (  self		:			List[str]    ):






			'''simple docstring'''
			UpperCAmelCase__					:  Union[str, Any]					   =					OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint					,provider="""CPUExecutionProvider"""    )
			UpperCAmelCase__					:  List[Any]					   =					EulerDiscreteScheduler.from_config(pipe.scheduler.config    )
			pipe.set_progress_bar_config(disable=A    )

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

			assert image.shape == (1, 128, 128, 3)
			UpperCAmelCase__					:  str					   =					np.array([0.5_2_9_1_1, 0.6_0_0_0_4, 0.4_9_2_2_9, 0.4_9_8_0_5, 0.5_4_5_0_2, 0.5_0_6_8_0, 0.4_7_7_7_7, 0.4_1_0_2_8, 0.4_5_3_0_4]    )

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

		def    __lowercase       (  self		:			Dict    ):






			'''simple docstring'''
			UpperCAmelCase__					:  Any					   =					OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint					,provider="""CPUExecutionProvider"""    )
			UpperCAmelCase__					:  Optional[int]					   =					EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config    )
			pipe.set_progress_bar_config(disable=A    )

			UpperCAmelCase__					:  Tuple					   =					self.get_dummy_inputs()
			UpperCAmelCase__					:  int					   =					pipe(**A    ).images
			UpperCAmelCase__					:  Union[str, Any]					   =					image[0, -3:, -3:, -1]

			assert image.shape == (1, 128, 128, 3)
			UpperCAmelCase__					:  Union[str, Any]					   =					np.array([0.5_2_9_1_1, 0.6_0_0_0_4, 0.4_9_2_2_9, 0.4_9_8_0_5, 0.5_4_5_0_2, 0.5_0_6_8_0, 0.4_7_7_7_7, 0.4_1_0_2_8, 0.4_5_3_0_4]    )

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




		def    __lowercase       (  self		:			str    ):






			'''simple docstring'''
			UpperCAmelCase__					:  Union[str, Any]					   =					OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint					,provider="""CPUExecutionProvider"""    )
			UpperCAmelCase__					:  Optional[Any]					   =					DPMSolverMultistepScheduler.from_config(pipe.scheduler.config    )
			pipe.set_progress_bar_config(disable=A    )

			UpperCAmelCase__					:  Optional[Any]					   =					self.get_dummy_inputs()
			UpperCAmelCase__					:  Optional[Any]					   =					pipe(**A    ).images
			UpperCAmelCase__					:  Dict					   =					image[0, -3:, -3:, -1]

			assert image.shape == (1, 128, 128, 3)
			UpperCAmelCase__					:  Optional[Any]					   =					np.array([0.6_5_3_3_1, 0.5_8_2_7_7, 0.4_8_2_0_4, 0.5_6_0_5_9, 0.5_3_6_6_5, 0.5_6_2_3_5, 0.5_0_9_6_9, 0.4_0_0_0_9, 0.4_6_5_5_2]    )

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







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

		@property
		def    __lowercase       (  self		:			Dict    ):






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

		@property
		def    __lowercase       (  self		:			List[str]    ):






			'''simple docstring'''
			UpperCAmelCase__					:  str					   =					ort.SessionOptions()
			UpperCAmelCase__					:  Tuple					   =					False
			return options

		def    __lowercase       (  self		:			Any    ):






			'''simple docstring'''
			UpperCAmelCase__					:  int					   =					load_image(
			    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
			    """/img2img/sketch-mountains-input.jpg"""    )
			UpperCAmelCase__					:  List[str]					   =					init_image.resize((768, 512)    )
			# using the PNDM scheduler by default
			UpperCAmelCase__					:  List[str]					   =					OnnxStableDiffusionImgaImgPipeline.from_pretrained(
			    """CompVis/stable-diffusion-v1-4"""					,revision="""onnx"""					,safety_checker=A					,feature_extractor=A					,provider=self.gpu_provider					,sess_options=self.gpu_options					,)
			pipe.set_progress_bar_config(disable=A    )

			UpperCAmelCase__					:  Optional[Any]					   =					"""A fantasy landscape, trending on artstation"""

			UpperCAmelCase__					:  Optional[Any]					   =					np.random.RandomState(0    )
			UpperCAmelCase__					:  int					   =					pipe(
			    prompt=A					,image=A					,strength=0.7_5					,guidance_scale=7.5					,num_inference_steps=10					,generator=A					,output_type="""np"""					,)
			UpperCAmelCase__					:  List[Any]					   =					output.images
			UpperCAmelCase__					:  Union[str, Any]					   =					images[0, 255:258, 383:386, -1]

			assert images.shape == (1, 512, 768, 3)
			UpperCAmelCase__					:  Optional[Any]					   =					np.array([0.4_9_0_9, 0.5_0_5_9, 0.5_3_7_2, 0.4_6_2_3, 0.4_8_7_6, 0.5_0_4_9, 0.4_8_2_0, 0.4_9_5_6, 0.5_0_1_9]    )
			# 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		:			Optional[Any]    ):






			'''simple docstring'''
			UpperCAmelCase__					:  List[str]					   =					load_image(
			    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
			    """/img2img/sketch-mountains-input.jpg"""    )
			UpperCAmelCase__					:  Tuple					   =					init_image.resize((768, 512)    )
			UpperCAmelCase__					:  List[Any]					   =					LMSDiscreteScheduler.from_pretrained(
			    """runwayml/stable-diffusion-v1-5"""					,subfolder="""scheduler"""					,revision="""onnx"""    )
			UpperCAmelCase__					:  Tuple					   =					OnnxStableDiffusionImgaImgPipeline.from_pretrained(
			    """runwayml/stable-diffusion-v1-5"""					,revision="""onnx"""					,scheduler=A					,safety_checker=A					,feature_extractor=A					,provider=self.gpu_provider					,sess_options=self.gpu_options					,)
			pipe.set_progress_bar_config(disable=A    )

			UpperCAmelCase__					:  List[str]					   =					"""A fantasy landscape, trending on artstation"""

			UpperCAmelCase__					:  str					   =					np.random.RandomState(0    )
			UpperCAmelCase__					:  Union[str, Any]					   =					pipe(
			    prompt=A					,image=A					,strength=0.7_5					,guidance_scale=7.5					,num_inference_steps=20					,generator=A					,output_type="""np"""					,)
			UpperCAmelCase__					:  Optional[Any]					   =					output.images
			UpperCAmelCase__					:  str					   =					images[0, 255:258, 383:386, -1]

			assert images.shape == (1, 512, 768, 3)
			UpperCAmelCase__					:  List[Any]					   =					np.array([0.8_0_4_3, 0.9_2_6, 0.9_5_8_1, 0.8_1_1_9, 0.8_9_5_4, 0.9_1_3, 0.7_2_0_9, 0.7_4_6_3, 0.7_4_3_1]    )
			# TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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








"""simple docstring"""




from typing import Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
      from PIL import Image

      from ..image_utils import load_image

if is_torch_available():
      from ..models.auto.modeling_auto import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

__UpperCAmelCase =				logging.get_logger(__name__)







@add_end_docstrings(__lowerCamelCase					)
class      __lowercase			(    __lowerCamelCase					):

  def __init__(  self		:			List[str]					,*A		:			Optional[int]					,**A		:			List[str]    ):






   '''simple docstring'''
   super().__init__(*A					,**A    )
   self.check_model_type(A    )

  def    __lowercase       (  self		:			Optional[int]					,A		:			Dict=None					,A		:			Optional[int]=None					,A		:			Tuple=None					,**A		:			str    ):






   '''simple docstring'''
   UpperCAmelCase__   ,      UpperCAmelCase__					:  Any					   =					{}, {}
   if padding is not None:
    UpperCAmelCase__					:  Union[str, Any]					   =					padding
   if truncation is not None:
    UpperCAmelCase__					:  List[str]					   =					truncation
   if top_k is not None:
    UpperCAmelCase__					:  List[Any]					   =					top_k
   return preprocess_params, {}, postprocess_params

  def __call__(  self		:			List[Any]					,A		:			Union["Image.Image", str]					,A		:			str = None					,**A		:			Union[str, Any]    ):






   '''simple docstring'''
   if isinstance(A					,(Image.Image, str)    ) and isinstance(A					,A    ):
    UpperCAmelCase__					:  Any					   =					{"""image""": image, """question""": question}
   else:
    UpperCAmelCase__					:  Dict					   =					image
   UpperCAmelCase__					:  Dict					   =					super().__call__(A					,**A    )
   return results

  def    __lowercase       (  self		:			Optional[int]					,A		:			Optional[Any]					,A		:			Optional[Any]=False					,A		:			List[Any]=False    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[Any]					   =					load_image(inputs["""image"""]    )
   UpperCAmelCase__					:  Optional[Any]					   =					self.tokenizer(
       inputs["""question"""]					,return_tensors=self.framework					,padding=A					,truncation=A    )
   UpperCAmelCase__					:  int					   =					self.image_processor(images=A					,return_tensors=self.framework    )
   model_inputs.update(A    )
   return model_inputs

  def    __lowercase       (  self		:			Dict					,A		:			Tuple    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[Any]					   =					self.model(**A    )
   return model_outputs




  def    __lowercase       (  self		:			int					,A		:			List[Any]					,A		:			List[str]=5    ):






   '''simple docstring'''
   if top_k > self.model.config.num_labels:
    UpperCAmelCase__					:  Any					   =					self.model.config.num_labels

   if self.framework == "pt":
    UpperCAmelCase__					:  Optional[int]					   =					model_outputs.logits.sigmoid()[0]
    UpperCAmelCase__   ,      UpperCAmelCase__					:  Optional[int]					   =					probs.topk(A    )
   else:
    raise ValueError(f"Unsupported framework: {self.framework}"    )

   UpperCAmelCase__					:  List[str]					   =					scores.tolist()
   UpperCAmelCase__					:  Optional[int]					   =					ids.tolist()
   return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(A					,A    )]





