




import torch

from diffusers import DPMSolverSDEScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import require_torchsde

from .test_schedulers import SchedulerCommonTest
@require_torchsde
class 			_UpperCamelCase      (				lowerCAmelCase		):
      UpperCAmelCase_        =		(DPMSolverSDEScheduler,)
      UpperCAmelCase_        =		10
      def 	UpperCAmelCase_						(			self   :Union[str, Any] ,      **lowerCamelCase   :List[str]      )     ->			List[Any]:
       UpperCAmelCase__							 =						{
           "num_train_timesteps": 1100,
           "beta_start": 0.00_01,
           "beta_end": 0.02,
           "beta_schedule": "linear",
           "noise_sampler_seed": 0,
       }

       config.update(**lowerCamelCase      )
       return config
      def 	UpperCAmelCase_						(			self   :str      )     ->			str:
       for timesteps in [10, 50, 100, 1000]:
        self.check_over_configs(num_train_timesteps=lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :Any      )     ->			Optional[int]:
       for beta_start, beta_end in zip([0.0_00_01, 0.00_01, 0.0_01] ,      [0.00_02, 0.0_02, 0.02]      ):
        self.check_over_configs(beta_start=lowerCamelCase ,      beta_end=lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :Optional[int]      )     ->			List[Any]:
       for schedule in ["linear", "scaled_linear"]:
        self.check_over_configs(beta_schedule=lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :str      )     ->			int:
       for prediction_type in ["epsilon", "v_prediction"]:
        self.check_over_configs(prediction_type=lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :int      )     ->			Tuple:
       UpperCAmelCase__							 =						self.scheduler_classes[0]
       UpperCAmelCase__							 =						self.get_scheduler_config()
       UpperCAmelCase__							 =						scheduler_class(**lowerCamelCase      )

       scheduler.set_timesteps(self.num_inference_steps      )

       UpperCAmelCase__							 =						self.dummy_model()
       UpperCAmelCase__							 =						self.dummy_sample_deter * scheduler.init_noise_sigma
       UpperCAmelCase__							 =						sample.to(lowerCamelCase      )

       for i, t in enumerate(scheduler.timesteps      ):
        UpperCAmelCase__							 =						scheduler.scale_model_input(lowerCamelCase ,      lowerCamelCase      )

        UpperCAmelCase__							 =						model(lowerCamelCase ,      lowerCamelCase      )

        UpperCAmelCase__							 =						scheduler.step(lowerCamelCase ,      lowerCamelCase ,      lowerCamelCase      )
        UpperCAmelCase__							 =						output.prev_sample

       UpperCAmelCase__							 =						torch.sum(torch.abs(lowerCamelCase      )      )
       UpperCAmelCase__							 =						torch.mean(torch.abs(lowerCamelCase      )      )

       if torch_device in ["mps"]:
        assert abs(result_sum.item() - 1_67.47_82_10_44_92_18_75      ) < 1e-2
        assert abs(result_mean.item() - 0.21_78_70_59_64_56_52_77      ) < 1e-3
       elif torch_device in ["cuda"]:
        assert abs(result_sum.item() - 1_71.59_35_21_11_81_64_06      ) < 1e-2
        assert abs(result_mean.item() - 0.2_23_42_90_68_92_29_96_52      ) < 1e-3
       else:
        assert abs(result_sum.item() - 1_62.52_38_34_22_85_15_62      ) < 1e-2
        assert abs(result_mean.item() - 0.2_11_61_95_70_85_13_26      ) < 1e-3
      def 	UpperCAmelCase_						(			self   :List[str]      )     ->			Optional[Any]:
       UpperCAmelCase__							 =						self.scheduler_classes[0]
       UpperCAmelCase__							 =						self.get_scheduler_config(prediction_type="v_prediction"      )
       UpperCAmelCase__							 =						scheduler_class(**lowerCamelCase      )

       scheduler.set_timesteps(self.num_inference_steps      )

       UpperCAmelCase__							 =						self.dummy_model()
       UpperCAmelCase__							 =						self.dummy_sample_deter * scheduler.init_noise_sigma
       UpperCAmelCase__							 =						sample.to(lowerCamelCase      )

       for i, t in enumerate(scheduler.timesteps      ):
        UpperCAmelCase__							 =						scheduler.scale_model_input(lowerCamelCase ,      lowerCamelCase      )

        UpperCAmelCase__							 =						model(lowerCamelCase ,      lowerCamelCase      )

        UpperCAmelCase__							 =						scheduler.step(lowerCamelCase ,      lowerCamelCase ,      lowerCamelCase      )
        UpperCAmelCase__							 =						output.prev_sample

       UpperCAmelCase__							 =						torch.sum(torch.abs(lowerCamelCase      )      )
       UpperCAmelCase__							 =						torch.mean(torch.abs(lowerCamelCase      )      )

       if torch_device in ["mps"]:
        assert abs(result_sum.item() - 1_24.77_14_92_00_43_94_53      ) < 1e-2
        assert abs(result_mean.item() - 0.1_62_26_28_90_14_81_62_84      ) < 1e-3
       elif torch_device in ["cuda"]:
        assert abs(result_sum.item() - 1_28.1_66_33_60_59_57_03      ) < 1e-2
        assert abs(result_mean.item() - 0.1_66_88_32_60_01_16_72_97      ) < 1e-3
       else:
        assert abs(result_sum.item() - 1_19.8_48_75_48_82_81_25      ) < 1e-2
        assert abs(result_mean.item() - 0.15_60_53_06_62_53_66_21      ) < 1e-3
      def 	UpperCAmelCase_						(			self   :List[str]      )     ->			List[str]:
       UpperCAmelCase__							 =						self.scheduler_classes[0]
       UpperCAmelCase__							 =						self.get_scheduler_config()
       UpperCAmelCase__							 =						scheduler_class(**lowerCamelCase      )

       scheduler.set_timesteps(self.num_inference_steps ,      device=lowerCamelCase      )

       UpperCAmelCase__							 =						self.dummy_model()
       UpperCAmelCase__							 =						self.dummy_sample_deter.to(lowerCamelCase      ) * scheduler.init_noise_sigma

       for t in scheduler.timesteps:
        UpperCAmelCase__							 =						scheduler.scale_model_input(lowerCamelCase ,      lowerCamelCase      )

        UpperCAmelCase__							 =						model(lowerCamelCase ,      lowerCamelCase      )

        UpperCAmelCase__							 =						scheduler.step(lowerCamelCase ,      lowerCamelCase ,      lowerCamelCase      )
        UpperCAmelCase__							 =						output.prev_sample

       UpperCAmelCase__							 =						torch.sum(torch.abs(lowerCamelCase      )      )
       UpperCAmelCase__							 =						torch.mean(torch.abs(lowerCamelCase      )      )

       if torch_device in ["mps"]:
        assert abs(result_sum.item() - 1_67.46_95_73_97_46_09_38      ) < 1e-2
        assert abs(result_mean.item() - 0.2_18_05_93_46_07_98_26_35      ) < 1e-3
       elif torch_device in ["cuda"]:
        assert abs(result_sum.item() - 1_71.59_35_36_37_69_53_12      ) < 1e-2
        assert abs(result_mean.item() - 0.2_23_42_90_83_82_41_57_71      ) < 1e-3
       else:
        assert abs(result_sum.item() - 1_62.52_38_34_22_85_15_62      ) < 1e-2
        assert abs(result_mean.item() - 0.2_11_61_95_70_85_13_26      ) < 1e-3







      def 	UpperCAmelCase_						(			self   :Optional[int]      )     ->			Any:
       UpperCAmelCase__							 =						self.scheduler_classes[0]
       UpperCAmelCase__							 =						self.get_scheduler_config()
       UpperCAmelCase__							 =						scheduler_class(**lowerCamelCase ,      use_karras_sigmas=lowerCamelCase      )

       scheduler.set_timesteps(self.num_inference_steps ,      device=lowerCamelCase      )

       UpperCAmelCase__							 =						self.dummy_model()
       UpperCAmelCase__							 =						self.dummy_sample_deter.to(lowerCamelCase      ) * scheduler.init_noise_sigma
       UpperCAmelCase__							 =						sample.to(lowerCamelCase      )

       for t in scheduler.timesteps:
        UpperCAmelCase__							 =						scheduler.scale_model_input(lowerCamelCase ,      lowerCamelCase      )

        UpperCAmelCase__							 =						model(lowerCamelCase ,      lowerCamelCase      )

        UpperCAmelCase__							 =						scheduler.step(lowerCamelCase ,      lowerCamelCase ,      lowerCamelCase      )
        UpperCAmelCase__							 =						output.prev_sample

       UpperCAmelCase__							 =						torch.sum(torch.abs(lowerCamelCase      )      )
       UpperCAmelCase__							 =						torch.mean(torch.abs(lowerCamelCase      )      )

       if torch_device in ["mps"]:
        assert abs(result_sum.item() - 1_76.66_97_41_35_74_21_88      ) < 1e-2
        assert abs(result_mean.item() - 0.2_30_03_87_27_30_98_18_11      ) < 1e-2
       elif torch_device in ["cuda"]:
        assert abs(result_sum.item() - 1_77.63_65_35_64_45_31_25      ) < 1e-2
        assert abs(result_mean.item() - 0.2_30_03_87_27_30_98_18_11      ) < 1e-2
       else:
        assert abs(result_sum.item() - 1_70.3_13_52_23_38_86_72      ) < 1e-2
        assert abs(result_mean.item() - 0.2_30_03_87_27_30_98_18_11      ) < 1e-2











import random
import unittest

import torch

from diffusers import IFInpaintingSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin
@skip_mps
class 			_UpperCamelCase      (				lowerCAmelCase	,       lowerCAmelCase	,       unittest.TestCase		):
      UpperCAmelCase_        =		IFInpaintingSuperResolutionPipeline
      UpperCAmelCase_        =		TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"""width""", """height"""}
      UpperCAmelCase_        =		TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"""original_image"""}		)
      UpperCAmelCase_        =		PipelineTesterMixin.required_optional_params - {"""latents"""}
      def 	UpperCAmelCase_						(			self   :str      )     ->			Optional[Any]:
       return self._get_superresolution_dummy_components()
      def 	UpperCAmelCase_						(			self   :List[str] ,      lowerCamelCase   :Dict ,      lowerCamelCase   :Optional[Any]=0      )     ->			int:
       if str(lowerCamelCase      ).startswith("mps"      ):
        UpperCAmelCase__							 =						torch.manual_seed(lowerCamelCase      )
       else:
        UpperCAmelCase__							 =						torch.Generator(device=lowerCamelCase      ).manual_seed(lowerCamelCase      )

       UpperCAmelCase__							 =						floats_tensor((1, 3, 16, 16) ,      rng=random.Random(lowerCamelCase      )      ).to(lowerCamelCase      )
       UpperCAmelCase__							 =						floats_tensor((1, 3, 32, 32) ,      rng=random.Random(lowerCamelCase      )      ).to(lowerCamelCase      )
       UpperCAmelCase__							 =						floats_tensor((1, 3, 32, 32) ,      rng=random.Random(lowerCamelCase      )      ).to(lowerCamelCase      )

       UpperCAmelCase__							 =						{
           "prompt": "A painting of a squirrel eating a burger",
           "image": image,
           "original_image": original_image,
           "mask_image": mask_image,
           "generator": generator,
           "num_inference_steps": 2,
           "output_type": "numpy",
       }

       return inputs
      @unittest.skipIf(
          torch_device != "cuda" or not is_xformers_available() ,      reason="XFormers attention is only available with CUDA and `xformers` installed" ,      )
      def 	UpperCAmelCase_						(			self   :Union[str, Any]      )     ->			Dict:
       self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3      )
      def 	UpperCAmelCase_						(			self   :List[str]      )     ->			Dict:
       self._test_save_load_optional_components()
      @unittest.skipIf(torch_device != "cuda" ,      reason="float16 requires CUDA"      )
      def 	UpperCAmelCase_						(			self   :Tuple      )     ->			Dict:
       # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
       super().test_save_load_floataa(expected_max_diff=1e-1      )
      def 	UpperCAmelCase_						(			self   :Dict      )     ->			str:
       self._test_attention_slicing_forward_pass(expected_max_diff=1e-2      )
      def 	UpperCAmelCase_						(			self   :Dict      )     ->			str:
       self._test_save_load_local()







      def 	UpperCAmelCase_						(			self   :List[str]      )     ->			List[str]:
       self._test_inference_batch_single_identical(
           expected_max_diff=1e-2 ,      )





