



'''simple docstring'''





import unittest

import numpy as np

from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline
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	):
    """simple docstring"""







    pass







@nightly
@require_onnxruntime
@require_torch_gpu
class     UpperCAmelCase_       (       unittest.TestCase	):
    """simple docstring"""







    @property
    def lowerCamelCase       (    self					:     List[Any]						):
          return (
              "CUDAExecutionProvider",
              {
                  "gpu_mem_limit": "15000000000",  # 15GB
                  "arena_extend_strategy": "kSameAsRequested",
              },
          )




    @property
    def lowerCamelCase       (    self					:     List[str]						):
          snake_case__  :    List[Any]    						=      ort.SessionOptions()
          snake_case__  :    List[Any]    						=      False
          return options




    def lowerCamelCase       (    self					:     str						):
          snake_case__  :    Any    						=      load_image(
              """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
              """/in_paint/overture-creations-5sI6fQgYIuo.png"""						)
          snake_case__  :    Any    						=      load_image(
              """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
              """/in_paint/overture-creations-5sI6fQgYIuo_mask.png"""						)
          snake_case__  :    Optional[Any]    						=      OnnxStableDiffusionInpaintPipeline.from_pretrained(
              """runwayml/stable-diffusion-inpainting""" ,     revision="""onnx""" ,     safety_checker=snake_case_ ,     feature_extractor=snake_case_ ,     provider=self.gpu_provider ,     sess_options=self.gpu_options ,     )
          pipe.set_progress_bar_config(disable=snake_case_						)

          snake_case__  :    List[Any]    						=      """A red cat sitting on a park bench"""

          snake_case__  :    Optional[Any]    						=      np.random.RandomState(0						)
          snake_case__  :    Optional[int]    						=      pipe(
              prompt=snake_case_ ,     image=snake_case_ ,     mask_image=snake_case_ ,     guidance_scale=7.5 ,     num_inference_steps=10 ,     generator=snake_case_ ,     output_type="""np""" ,     )
          snake_case__  :    Optional[Any]    						=      output.images
          snake_case__  :    Dict    						=      images[0, 255:258, 255:258, -1]

          assert images.shape == (1, 512, 512, 3)
          snake_case__  :    Optional[int]    						=      np.array([0.2514, 0.3007, 0.3517, 0.1790, 0.2382, 0.3167, 0.1944, 0.2273, 0.2464]						)

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




    def lowerCamelCase       (    self					:     List[str]						):
          snake_case__  :    int    						=      load_image(
              """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
              """/in_paint/overture-creations-5sI6fQgYIuo.png"""						)
          snake_case__  :    Optional[Any]    						=      load_image(
              """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
              """/in_paint/overture-creations-5sI6fQgYIuo_mask.png"""						)
          snake_case__  :    Tuple    						=      LMSDiscreteScheduler.from_pretrained(
              """runwayml/stable-diffusion-inpainting""" ,     subfolder="""scheduler""" ,     revision="""onnx"""						)
          snake_case__  :    Optional[int]    						=      OnnxStableDiffusionInpaintPipeline.from_pretrained(
              """runwayml/stable-diffusion-inpainting""" ,     revision="""onnx""" ,     scheduler=snake_case_ ,     safety_checker=snake_case_ ,     feature_extractor=snake_case_ ,     provider=self.gpu_provider ,     sess_options=self.gpu_options ,     )
          pipe.set_progress_bar_config(disable=snake_case_						)

          snake_case__  :    List[Any]    						=      """A red cat sitting on a park bench"""

          snake_case__  :    Optional[int]    						=      np.random.RandomState(0						)
          snake_case__  :    Optional[int]    						=      pipe(
              prompt=snake_case_ ,     image=snake_case_ ,     mask_image=snake_case_ ,     guidance_scale=7.5 ,     num_inference_steps=20 ,     generator=snake_case_ ,     output_type="""np""" ,     )
          snake_case__  :    Tuple    						=      output.images
          snake_case__  :    Optional[int]    						=      images[0, 255:258, 255:258, -1]

          assert images.shape == (1, 512, 512, 3)
          snake_case__  :    Union[str, Any]    						=      np.array([0.0086, 0.0077, 0.0083, 0.0093, 0.0107, 0.0139, 0.0094, 0.0097, 0.0125]						)

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





'''simple docstring'''





import numpy as np

from transformers import Pipeline





def 						__snake_case(				_lowerCAmelCase		)		-> Optional[int]:
      snake_case__  :    Optional[Any]    						=      np.max(_lowerCAmelCase     ,   axis=-1     ,   keepdims=_lowerCAmelCase		)
      snake_case__  :    List[str]    						=      np.exp(outputs - maxes		)
      return shifted_exp / shifted_exp.sum(axis=-1     ,   keepdims=_lowerCAmelCase		)







class     UpperCAmelCase_       (       _a	):
    """simple docstring"""







    def lowerCamelCase       (    self					:     Optional[Any] ,     **snake_case_					:     int						):
          snake_case__  :    Optional[int]    						=      {}
          if "second_text" in kwargs:
                snake_case__  :    Union[str, Any]    						=      kwargs["""second_text"""]
          return preprocess_kwargs, {}, {}




    def lowerCamelCase       (    self					:     str ,     snake_case_					:     Tuple ,     snake_case_					:     Union[str, Any]=None						):
          return self.tokenizer(snake_case_ ,     text_pair=snake_case_ ,     return_tensors=self.framework						)




    def lowerCamelCase       (    self					:     List[Any] ,     snake_case_					:     Dict						):
          return self.model(**snake_case_						)




    def lowerCamelCase       (    self					:     int ,     snake_case_					:     List[Any]						):
          snake_case__  :    Union[str, Any]    						=      model_outputs.logits[0].numpy()
          snake_case__  :    List[str]    						=      softmax(snake_case_						)

          snake_case__  :    List[str]    						=      np.argmax(snake_case_						)
          snake_case__  :    List[str]    						=      self.model.config.idalabel[best_class]
          snake_case__  :    Optional[int]    						=      probabilities[best_class].item()
          snake_case__  :    str    						=      logits.tolist()
          return {"label": label, "score": score, "logits": logits}
