

import random
import unittest

import torch

from diffusers import IFSuperResolutionPipeline
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_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin

@skip_mps
class    a__  (   snake_case							,		snake_case							,		unittest.TestCase     ):
			"""simple docstring"""







			__lowerCamelCase	    =    IFSuperResolutionPipeline
			__lowerCamelCase	    =    TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'width', 'height'}
			__lowerCamelCase	    =    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
			__lowerCamelCase	    =    PipelineTesterMixin.required_optional_params - {'latents'}


			def        UpperCamelCase						(      self    )					->  List[str]:
							'''simple docstring'''






							return self._get_superresolution_dummy_components()


			def        UpperCamelCase						(      self , lowercase , lowercase=0    )					->  Dict:
							'''simple docstring'''






							if str(lowercase    ).startswith("mps"    ):
											A__            =     torch.manual_seed(lowercase    )
							else:
											A__            =     torch.Generator(device=lowercase    ).manual_seed(lowercase    )

							A__            =     floats_tensor((1, 3, 32, 32) , rng=random.Random(lowercase    )    ).to(lowercase    )

							A__            =     {
							    "prompt": "A painting of a squirrel eating a burger",
							    "image": 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    )					->  str:
							'''simple docstring'''






							self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3    )


			def        UpperCamelCase						(      self    )					->  List[Any]:
							'''simple docstring'''






							self._test_save_load_optional_components()


			@unittest.skipIf(torch_device != "cuda" , reason="float16 requires CUDA"    )
			def        UpperCamelCase						(      self    )					->  Tuple:
							'''simple docstring'''






							super().test_save_load_floataa(expected_max_diff=1e-1    )


			def        UpperCamelCase						(      self    )					->  Any:
							'''simple docstring'''






							self._test_attention_slicing_forward_pass(expected_max_diff=1e-2    )


			def        UpperCamelCase						(      self    )					->  Union[str, Any]:
							'''simple docstring'''






							self._test_save_load_local()


			def        UpperCamelCase						(      self    )					->  Any:
							'''simple docstring'''






							self._test_inference_batch_single_identical(
							    expected_max_diff=1e-2 , )




import torch

from diffusers import DDIMScheduler

from .test_schedulers import SchedulerCommonTest

class    a__  (   snake_case     ):
			"""simple docstring"""







			__lowerCamelCase	    =    (DDIMScheduler,)
			__lowerCamelCase	    =    (('eta', 0.0), ('num_inference_steps', 50))


			def        UpperCamelCase						(      self , **lowercase    )					->  Optional[Any]:
							'''simple docstring'''






							A__            =     {
							    "num_train_timesteps": 1000,
							    "beta_start": 0.0001,
							    "beta_end": 0.02,
							    "beta_schedule": "linear",
							    "clip_sample": True,
							}

							config.update(**lowercase    )
							return config


			def        UpperCamelCase						(      self , **lowercase    )					->  List[Any]:
							'''simple docstring'''






							A__            =     self.scheduler_classes[0]
							A__            =     self.get_scheduler_config(**lowercase    )
							A__            =     scheduler_class(**lowercase    )

							A__      ,			A__            =     10, 0.0

							A__            =     self.dummy_model()
							A__            =     self.dummy_sample_deter

							scheduler.set_timesteps(lowercase    )

							for t in scheduler.timesteps:
											A__            =     model(lowercase , lowercase    )
											A__            =     scheduler.step(lowercase , lowercase , lowercase , lowercase    ).prev_sample

							return sample


			def        UpperCamelCase						(      self    )					->  Optional[Any]:
							'''simple docstring'''






							for timesteps in [100, 500, 1000]:
											self.check_over_configs(num_train_timesteps=lowercase    )


			def        UpperCamelCase						(      self    )					->  Union[str, Any]:
							'''simple docstring'''






							for steps_offset in [0, 1]:
											self.check_over_configs(steps_offset=lowercase    )

							A__            =     self.scheduler_classes[0]
							A__            =     self.get_scheduler_config(steps_offset=1    )
							A__            =     scheduler_class(**lowercase    )
							scheduler.set_timesteps(5    )
							assert torch.equal(scheduler.timesteps , torch.LongTensor([801, 601, 401, 201, 1]    )    )


			def        UpperCamelCase						(      self    )					->  Dict:
							'''simple docstring'''






							for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1] , [0.002, 0.02, 0.2, 2]    ):
											self.check_over_configs(beta_start=lowercase , beta_end=lowercase    )


			def        UpperCamelCase						(      self    )					->  Optional[int]:
							'''simple docstring'''






							for schedule in ["linear", "squaredcos_cap_v2"]:
											self.check_over_configs(beta_schedule=lowercase    )


			def        UpperCamelCase						(      self    )					->  Dict:
							'''simple docstring'''






							for prediction_type in ["epsilon", "v_prediction"]:
											self.check_over_configs(prediction_type=lowercase    )


			def        UpperCamelCase						(      self    )					->  Dict:
							'''simple docstring'''






							for clip_sample in [True, False]:
											self.check_over_configs(clip_sample=lowercase    )


			def        UpperCamelCase						(      self    )					->  int:
							'''simple docstring'''






							for timestep_spacing in ["trailing", "leading"]:
											self.check_over_configs(timestep_spacing=lowercase    )


			def        UpperCamelCase						(      self    )					->  int:
							'''simple docstring'''






							for rescale_betas_zero_snr in [True, False]:
											self.check_over_configs(rescale_betas_zero_snr=lowercase    )


			def        UpperCamelCase						(      self    )					->  Union[str, Any]:
							'''simple docstring'''






							self.check_over_configs(thresholding=lowercase    )
							for threshold in [0.5, 1.0, 2.0]:
											for prediction_type in ["epsilon", "v_prediction"]:
															self.check_over_configs(
															    thresholding=lowercase , prediction_type=lowercase , sample_max_value=lowercase , )


			def        UpperCamelCase						(      self    )					->  Dict:
							'''simple docstring'''






							for t in [1, 10, 49]:
											self.check_over_forward(time_step=lowercase    )


			def        UpperCamelCase						(      self    )					->  int:
							'''simple docstring'''






							for t, num_inference_steps in zip([1, 10, 50] , [10, 50, 500]    ):
											self.check_over_forward(time_step=lowercase , num_inference_steps=lowercase    )


			def        UpperCamelCase						(      self    )					->  List[str]:
							'''simple docstring'''






							for t, eta in zip([1, 10, 49] , [0.0, 0.5, 1.0]    ):
											self.check_over_forward(time_step=lowercase , eta=lowercase    )


			def        UpperCamelCase						(      self    )					->  int:
							'''simple docstring'''






							A__            =     self.scheduler_classes[0]
							A__            =     self.get_scheduler_config()
							A__            =     scheduler_class(**lowercase    )

							assert torch.sum(torch.abs(scheduler._get_variance(0 , 0    ) - 0.0    )    ) < 1e-5
							assert torch.sum(torch.abs(scheduler._get_variance(420 , 400    ) - 0.1_4771    )    ) < 1e-5
							assert torch.sum(torch.abs(scheduler._get_variance(980 , 960    ) - 0.3_2460    )    ) < 1e-5
							assert torch.sum(torch.abs(scheduler._get_variance(0 , 0    ) - 0.0    )    ) < 1e-5
							assert torch.sum(torch.abs(scheduler._get_variance(487 , 486    ) - 0.0_0979    )    ) < 1e-5
							assert torch.sum(torch.abs(scheduler._get_variance(999 , 998    ) - 0.02    )    ) < 1e-5


			def        UpperCamelCase						(      self    )					->  str:
							'''simple docstring'''






							A__            =     self.full_loop()

							A__            =     torch.sum(torch.abs(lowercase    )    )
							A__            =     torch.mean(torch.abs(lowercase    )    )

							assert abs(result_sum.item() - 172.0067    ) < 1e-2
							assert abs(result_mean.item() - 0.22_3967    ) < 1e-3


			def        UpperCamelCase						(      self    )					->  Dict:
							'''simple docstring'''






							A__            =     self.full_loop(prediction_type="v_prediction"    )

							A__            =     torch.sum(torch.abs(lowercase    )    )
							A__            =     torch.mean(torch.abs(lowercase    )    )

							assert abs(result_sum.item() - 52.5302    ) < 1e-2
							assert abs(result_mean.item() - 0.0684    ) < 1e-3


			def        UpperCamelCase						(      self    )					->  List[Any]:
							'''simple docstring'''






							A__            =     self.full_loop(set_alpha_to_one=lowercase , beta_start=0.01    )
							A__            =     torch.sum(torch.abs(lowercase    )    )
							A__            =     torch.mean(torch.abs(lowercase    )    )

							assert abs(result_sum.item() - 149.8295    ) < 1e-2
							assert abs(result_mean.item() - 0.1951    ) < 1e-3


			def        UpperCamelCase						(      self    )					->  Tuple:
							'''simple docstring'''






							A__            =     self.full_loop(set_alpha_to_one=lowercase , beta_start=0.01    )
							A__            =     torch.sum(torch.abs(lowercase    )    )
							A__            =     torch.mean(torch.abs(lowercase    )    )

							assert abs(result_sum.item() - 149.0784    ) < 1e-2
							assert abs(result_mean.item() - 0.1941    ) < 1e-3

