





"""simple docstring"""




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  _UpperCAmelCase  (   UpperCAmelCase__			,   unittest.TestCase		):



      '''simple docstring'''
      lowercase_      : int	    =   """hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline"""

      def 					lowerCamelCase_			(     self					,      snake_case_=0  ):

        """simple docstring"""

        A_  :       Optional[int]										=  np.random.RandomState(snake_case_  )
        A_  :       Dict										=  {
            '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 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Any										=  OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint					,      provider='CPUExecutionProvider'  )
        pipe.set_progress_bar_config(disable=snake_case_  )

        A_  :       List[str]										=  self.get_dummy_inputs()
        A_  :       Union[str, Any]										=  pipe(**snake_case_  ).images
        A_  :       Any										=  image[0, -3:, -3:, -1]

        assert image.shape == (1, 1_2_8, 1_2_8, 3)
        A_  :       List[str]										=  np.array([0.6_50_72, 0.5_84_92, 0.4_82_19, 0.5_55_21, 0.5_31_80, 0.5_59_39, 0.5_06_97, 0.3_98_00, 0.4_64_55]  )

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

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       List[Any]										=  OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint					,      provider='CPUExecutionProvider'  )
        A_  :       List[str]										=  PNDMScheduler.from_config(pipe.scheduler.config					,      skip_prk_steps=snake_case_  )
        pipe.set_progress_bar_config(disable=snake_case_  )

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

        assert image.shape == (1, 1_2_8, 1_2_8, 3)
        A_  :       Tuple										=  np.array([0.6_58_63, 0.5_94_25, 0.4_93_26, 0.5_63_13, 0.5_38_75, 0.5_66_27, 0.5_10_65, 0.3_97_77, 0.4_63_30]  )

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

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Dict										=  OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint					,      provider='CPUExecutionProvider'  )
        A_  :       Union[str, Any]										=  LMSDiscreteScheduler.from_config(pipe.scheduler.config  )
        pipe.set_progress_bar_config(disable=snake_case_  )

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

        assert image.shape == (1, 1_2_8, 1_2_8, 3)
        A_  :       Optional[int]										=  np.array([0.5_37_55, 0.6_07_86, 0.4_74_02, 0.4_94_88, 0.5_18_69, 0.4_98_19, 0.4_79_85, 0.3_89_57, 0.4_42_79]  )

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

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       List[str]										=  OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint					,      provider='CPUExecutionProvider'  )
        A_  :       Optional[int]										=  EulerDiscreteScheduler.from_config(pipe.scheduler.config  )
        pipe.set_progress_bar_config(disable=snake_case_  )

        A_  :       Tuple										=  self.get_dummy_inputs()
        A_  :       Optional[int]										=  pipe(**snake_case_  ).images
        A_  :       str										=  image[0, -3:, -3:, -1]

        assert image.shape == (1, 1_2_8, 1_2_8, 3)
        A_  :       Union[str, Any]										=  np.array([0.5_37_55, 0.6_07_86, 0.4_74_02, 0.4_94_88, 0.5_18_69, 0.4_98_19, 0.4_79_85, 0.3_89_57, 0.4_42_79]  )

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

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Optional[int]										=  OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint					,      provider='CPUExecutionProvider'  )
        A_  :       Any										=  EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config  )
        pipe.set_progress_bar_config(disable=snake_case_  )

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

        assert image.shape == (1, 1_2_8, 1_2_8, 3)
        A_  :       List[Any]										=  np.array([0.5_38_17, 0.6_08_12, 0.4_73_84, 0.4_95_30, 0.5_18_94, 0.4_98_14, 0.4_79_84, 0.3_89_58, 0.4_42_71]  )

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

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Union[str, Any]										=  OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint					,      provider='CPUExecutionProvider'  )
        A_  :       int										=  DPMSolverMultistepScheduler.from_config(pipe.scheduler.config  )
        pipe.set_progress_bar_config(disable=snake_case_  )

        A_  :       List[Any]										=  self.get_dummy_inputs()
        A_  :       Union[str, Any]										=  pipe(**snake_case_  ).images
        A_  :       Any										=  image[0, -3:, -3:, -1]

        assert image.shape == (1, 1_2_8, 1_2_8, 3)
        A_  :       Optional[int]										=  np.array([0.5_38_95, 0.6_08_08, 0.4_79_33, 0.4_96_08, 0.5_18_86, 0.4_99_50, 0.4_80_53, 0.3_89_57, 0.4_42_00]  )

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

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       List[Any]										=  OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint					,      provider='CPUExecutionProvider'  )
        pipe.set_progress_bar_config(disable=snake_case_  )

        A_  :       str										=  self.get_dummy_inputs()
        A_  :       int										=  3 * [inputs['prompt']]

        # forward
        A_  :       Any										=  pipe(**snake_case_  )
        A_  :       Any										=  output.images[0, -3:, -3:, -1]

        A_  :       Dict										=  self.get_dummy_inputs()
        A_  :       Union[str, Any]										=  3 * [inputs.pop('prompt'  )]

        A_  :       str										=  pipe.tokenizer(
            snake_case_					,      padding='max_length'					,      max_length=pipe.tokenizer.model_max_length					,      truncation=snake_case_					,      return_tensors='np'					,      )
        A_  :       Optional[int]										=  text_inputs['input_ids']

        A_  :       Dict										=  pipe.text_encoder(input_ids=text_inputs.astype(np.intaa  )  )[0]

        A_  :       Optional[Any]										=  prompt_embeds

        # forward
        A_  :       str										=  pipe(**snake_case_  )
        A_  :       Optional[Any]										=  output.images[0, -3:, -3:, -1]

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

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Union[str, Any]										=  OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint					,      provider='CPUExecutionProvider'  )
        pipe.set_progress_bar_config(disable=snake_case_  )

        A_  :       Union[str, Any]										=  self.get_dummy_inputs()
        A_  :       List[str]										=  3 * ['this is a negative prompt']
        A_  :       List[str]										=  negative_prompt
        A_  :       Tuple										=  3 * [inputs['prompt']]

        # forward
        A_  :       Union[str, Any]										=  pipe(**snake_case_  )
        A_  :       Optional[int]										=  output.images[0, -3:, -3:, -1]

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

        A_  :       Optional[int]										=  []
        for p in [prompt, negative_prompt]:
          A_  :       Dict										=  pipe.tokenizer(
              snake_case_					,      padding='max_length'					,      max_length=pipe.tokenizer.model_max_length					,      truncation=snake_case_					,      return_tensors='np'					,      )
          A_  :       Any										=  text_inputs['input_ids']

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

        A_						,       A_  :       Tuple										=  embeds

        # forward
        A_  :       Optional[int]										=  pipe(**snake_case_  )
        A_  :       Dict										=  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  _UpperCAmelCase  (   unittest.TestCase		):



      '''simple docstring'''

      @property
      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

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

      @property
      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Union[str, Any]										=  ort.SessionOptions()
        A_  :       str										=  False
        return options

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Dict										=  OnnxStableDiffusionPipeline.from_pretrained(
            'CompVis/stable-diffusion-v1-4'					,      revision='onnx'					,      safety_checker=snake_case_					,      feature_extractor=snake_case_					,      provider=self.gpu_provider					,      sess_options=self.gpu_options					,      )
        sd_pipe.set_progress_bar_config(disable=snake_case_  )

        A_  :       List[Any]										=  'A painting of a squirrel eating a burger'
        np.random.seed(0  )
        A_  :       Any										=  sd_pipe([prompt]					,      guidance_scale=6.0					,      num_inference_steps=1_0					,      output_type='np'  )
        A_  :       List[Any]										=  output.images

        A_  :       str										=  image[0, -3:, -3:, -1]

        assert image.shape == (1, 5_1_2, 5_1_2, 3)
        A_  :       Any										=  np.array([0.04_52, 0.03_90, 0.00_87, 0.03_50, 0.06_17, 0.03_64, 0.05_44, 0.05_23, 0.07_20]  )

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

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

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

        A_  :       Optional[int]										=  'open neural network exchange'
        A_  :       Any										=  np.random.RandomState(0  )
        A_  :       Any										=  sd_pipe([prompt]					,      guidance_scale=7.5					,      num_inference_steps=1_0					,      generator=snake_case_					,      output_type='np'  )
        A_  :       List[Any]										=  output.images
        A_  :       List[Any]										=  image[0, -3:, -3:, -1]

        assert image.shape == (1, 5_1_2, 5_1_2, 3)
        A_  :       Any										=  np.array([0.28_67, 0.19_74, 0.14_81, 0.72_94, 0.72_51, 0.66_67, 0.41_94, 0.56_42, 0.64_86]  )

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

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

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

        A_  :       List[str]										=  'open neural network exchange'
        A_  :       Tuple										=  np.random.RandomState(0  )
        A_  :       Optional[Any]										=  sd_pipe([prompt]					,      guidance_scale=7.5					,      num_inference_steps=1_0					,      generator=snake_case_					,      output_type='np'  )
        A_  :       Tuple										=  output.images
        A_  :       Tuple										=  image[0, -3:, -3:, -1]

        assert image.shape == (1, 5_1_2, 5_1_2, 3)
        A_  :       Union[str, Any]										=  np.array([0.23_06, 0.19_59, 0.15_93, 0.65_49, 0.63_94, 0.54_08, 0.50_65, 0.60_10, 0.61_61]  )

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

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Union[str, Any]										=  0

        def test_callback_fn(snake_case_					,      snake_case_					,      snake_case_  ) -> None:
          A_  :       Optional[Any]										=  True
          nonlocal number_of_steps
          number_of_steps += 1
          if step == 0:
            assert latents.shape == (1, 4, 6_4, 6_4)
            A_  :       Optional[int]										=  latents[0, -3:, -3:, -1]
            A_  :       Optional[Any]										=  np.array(
                [-0.67_72, -0.38_35, -1.24_56, 0.19_05, -1.09_74, 0.69_67, -1.93_53, 0.01_78, 1.01_67]  )

            assert np.abs(latents_slice.flatten() - expected_slice  ).max() < 1E-3
          elif step == 5:
            assert latents.shape == (1, 4, 6_4, 6_4)
            A_  :       Optional[Any]										=  latents[0, -3:, -3:, -1]
            A_  :       Optional[int]										=  np.array(
                [-0.33_51, 0.22_41, -0.18_37, -0.23_25, -0.65_77, 0.33_93, -0.02_41, 0.58_99, 1.38_75]  )

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

        A_  :       Any										=  False

        A_  :       List[Any]										=  OnnxStableDiffusionPipeline.from_pretrained(
            'runwayml/stable-diffusion-v1-5'					,      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_  )

        A_  :       Dict										=  'Andromeda galaxy in a bottle'

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

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       List[str]										=  OnnxStableDiffusionPipeline.from_pretrained(
            'runwayml/stable-diffusion-v1-5'					,      revision='onnx'					,      safety_checker=snake_case_					,      feature_extractor=snake_case_					,      provider=self.gpu_provider					,      sess_options=self.gpu_options					,      )
        assert isinstance(snake_case_					,      snake_case_  )
        assert pipe.safety_checker is None

        A_  :       Any										=  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(snake_case_  )
          A_  :       int										=  OnnxStableDiffusionPipeline.from_pretrained(snake_case_  )

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






"""simple docstring"""




import torch

from diffusers import DiffusionPipeline

class  _UpperCAmelCase  (   UpperCAmelCase__		):



      '''simple docstring'''

      def __init__(     self					,      snake_case_					,      snake_case_  ):

        """simple docstring"""

        super().__init__()

        self.register_modules(unet=snake_case_					,      scheduler=snake_case_  )

      def __call__(     self  ):

        """simple docstring"""

        A_  :       Optional[Any]										=  torch.randn(
            (1, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size)					,      )
        A_  :       List[str]										=  1

        A_  :       List[str]										=  self.unet(snake_case_					,      snake_case_  ).sample
        A_  :       Optional[int]										=  self.scheduler.step(snake_case_					,      snake_case_					,      snake_case_  ).prev_sample

        A_  :       List[Any]										=  scheduler_output - scheduler_output + torch.ones_like(snake_case_  )

        return result