




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    lowercase_		(SCREAMING_SNAKE_CASE__							,	SCREAMING_SNAKE_CASE__							,	unittest.TestCase ):
						lowerCAmelCase__			      =IFInpaintingSuperResolutionPipeline
						lowerCAmelCase__			      =TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"}
						lowerCAmelCase__			      =TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"original_image"} )
						lowerCAmelCase__			      =PipelineTesterMixin.required_optional_params - {"latents"}



						def 	__a					(			self	:   str  ):

								"""simple docstring"""

								return self._get_superresolution_dummy_components()



						def 	__a					(			self	:   List[Any]					,							snake_case__	:   int					,							snake_case__	:   List[Any]=0  ):

								"""simple docstring"""

								if str(snake_case__  ).startswith('mps'  ):
										SCREAMING_SNAKE_CASE_					      =							torch.manual_seed(snake_case__  )
								else:
										SCREAMING_SNAKE_CASE_					      =							torch.Generator(device=snake_case__  ).manual_seed(snake_case__  )

								SCREAMING_SNAKE_CASE_					      =							floats_tensor((1, 3, 16, 16)					,							rng=random.Random(snake_case__  )  ).to(snake_case__  )
								SCREAMING_SNAKE_CASE_					      =							floats_tensor((1, 3, 32, 32)					,							rng=random.Random(snake_case__  )  ).to(snake_case__  )
								SCREAMING_SNAKE_CASE_					      =							floats_tensor((1, 3, 32, 32)					,							rng=random.Random(snake_case__  )  ).to(snake_case__  )

								SCREAMING_SNAKE_CASE_					      =							{
								    '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 	__a					(			self	:   List[Any]  ):

								"""simple docstring"""

								self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3  )



						def 	__a					(			self	:   int  ):

								"""simple docstring"""

								self._test_save_load_optional_components()



						@unittest.skipIf(torch_device != 'cuda'					,							reason='float16 requires CUDA'  )
						def 	__a					(			self	:   Optional[int]  ):

								"""simple docstring"""

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



						def 	__a					(			self	:   str  ):

								"""simple docstring"""

								self._test_attention_slicing_forward_pass(expected_max_diff=1e-2  )



						def 	__a					(			self	:   List[Any]  ):

								"""simple docstring"""

								self._test_save_load_local()






						def 	__a					(			self	:   Optional[Any]  ):

								"""simple docstring"""

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





import torch

from diffusers import KDPMaDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest



class    lowercase_		(SCREAMING_SNAKE_CASE__ ):
						lowerCAmelCase__			      =(KDPMaDiscreteScheduler,)
						lowerCAmelCase__			      =10



						def 	__a					(			self	:   Dict					,							**snake_case__	:   str  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							{
								    'num_train_timesteps': 11_00,
								    'beta_start': 0.00_01,
								    'beta_end': 0.02,
								    'beta_schedule': 'linear',
								}

								config.update(**snake_case__  )
								return config



						def 	__a					(			self	:   str  ):

								"""simple docstring"""

								for timesteps in [10, 50, 1_00, 10_00]:
										self.check_over_configs(num_train_timesteps=snake_case__  )



						def 	__a					(			self	:   List[Any]  ):

								"""simple docstring"""

								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=snake_case__					,							beta_end=snake_case__  )



						def 	__a					(			self	:   Optional[int]  ):

								"""simple docstring"""

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



						def 	__a					(			self	:   Optional[Any]  ):

								"""simple docstring"""

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



						def 	__a					(			self	:   Optional[Any]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							self.scheduler_classes[0]
								SCREAMING_SNAKE_CASE_					      =							self.get_scheduler_config(prediction_type='v_prediction'  )
								SCREAMING_SNAKE_CASE_					      =							scheduler_class(**snake_case__  )

								scheduler.set_timesteps(self.num_inference_steps  )

								SCREAMING_SNAKE_CASE_					      =							self.dummy_model()
								SCREAMING_SNAKE_CASE_					      =							self.dummy_sample_deter * scheduler.init_noise_sigma
								SCREAMING_SNAKE_CASE_					      =							sample.to(snake_case__  )

								for i, t in enumerate(scheduler.timesteps  ):
										SCREAMING_SNAKE_CASE_					      =							scheduler.scale_model_input(snake_case__					,							snake_case__  )

										SCREAMING_SNAKE_CASE_					      =							model(snake_case__					,							snake_case__  )

										SCREAMING_SNAKE_CASE_					      =							scheduler.step(snake_case__					,							snake_case__					,							snake_case__  )
										SCREAMING_SNAKE_CASE_					      =							output.prev_sample

								SCREAMING_SNAKE_CASE_					      =							torch.sum(torch.abs(snake_case__  )  )
								SCREAMING_SNAKE_CASE_					      =							torch.mean(torch.abs(snake_case__  )  )

								if torch_device in ["cpu", "mps"]:
										assert abs(result_sum.item() - 4.69_34e-07  ) < 1e-2
										assert abs(result_mean.item() - 6.11_12e-10  ) < 1e-3
								else:
										# CUDA
										assert abs(result_sum.item() - 4.6_93_42_86_50_17_09_72e-07  ) < 1e-2
										assert abs(result_mean.item() - 0.00_02  ) < 1e-3



						def 	__a					(			self	:   Union[str, Any]  ):

								"""simple docstring"""

								if torch_device == "mps":
										return
								SCREAMING_SNAKE_CASE_					      =							self.scheduler_classes[0]
								SCREAMING_SNAKE_CASE_					      =							self.get_scheduler_config()
								SCREAMING_SNAKE_CASE_					      =							scheduler_class(**snake_case__  )

								scheduler.set_timesteps(self.num_inference_steps  )

								SCREAMING_SNAKE_CASE_					      =							self.dummy_model()
								SCREAMING_SNAKE_CASE_					      =							self.dummy_sample_deter * scheduler.init_noise_sigma
								SCREAMING_SNAKE_CASE_					      =							sample.to(snake_case__  )

								for i, t in enumerate(scheduler.timesteps  ):
										SCREAMING_SNAKE_CASE_					      =							scheduler.scale_model_input(snake_case__					,							snake_case__  )

										SCREAMING_SNAKE_CASE_					      =							model(snake_case__					,							snake_case__  )

										SCREAMING_SNAKE_CASE_					      =							scheduler.step(snake_case__					,							snake_case__					,							snake_case__  )
										SCREAMING_SNAKE_CASE_					      =							output.prev_sample

								SCREAMING_SNAKE_CASE_					      =							torch.sum(torch.abs(snake_case__  )  )
								SCREAMING_SNAKE_CASE_					      =							torch.mean(torch.abs(snake_case__  )  )

								if torch_device in ["cpu", "mps"]:
										assert abs(result_sum.item() - 20.41_25  ) < 1e-2
										assert abs(result_mean.item() - 0.02_66  ) < 1e-3
								else:
										# CUDA
										assert abs(result_sum.item() - 20.41_25  ) < 1e-2
										assert abs(result_mean.item() - 0.02_66  ) < 1e-3






						def 	__a					(			self	:   Optional[int]  ):

								"""simple docstring"""

								if torch_device == "mps":
										return
								SCREAMING_SNAKE_CASE_					      =							self.scheduler_classes[0]
								SCREAMING_SNAKE_CASE_					      =							self.get_scheduler_config()
								SCREAMING_SNAKE_CASE_					      =							scheduler_class(**snake_case__  )

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

								SCREAMING_SNAKE_CASE_					      =							self.dummy_model()
								SCREAMING_SNAKE_CASE_					      =							self.dummy_sample_deter.to(snake_case__  ) * scheduler.init_noise_sigma

								for t in scheduler.timesteps:
										SCREAMING_SNAKE_CASE_					      =							scheduler.scale_model_input(snake_case__					,							snake_case__  )

										SCREAMING_SNAKE_CASE_					      =							model(snake_case__					,							snake_case__  )

										SCREAMING_SNAKE_CASE_					      =							scheduler.step(snake_case__					,							snake_case__					,							snake_case__  )
										SCREAMING_SNAKE_CASE_					      =							output.prev_sample

								SCREAMING_SNAKE_CASE_					      =							torch.sum(torch.abs(snake_case__  )  )
								SCREAMING_SNAKE_CASE_					      =							torch.mean(torch.abs(snake_case__  )  )

								if str(snake_case__  ).startswith('cpu'  ):
										# The following sum varies between 148 and 156 on mps. Why?
										assert abs(result_sum.item() - 20.41_25  ) < 1e-2
										assert abs(result_mean.item() - 0.02_66  ) < 1e-3
								else:
										# CUDA
										assert abs(result_sum.item() - 20.41_25  ) < 1e-2
										assert abs(result_mean.item() - 0.02_66  ) < 1e-3