



"""simple docstring"""

import tempfile

import torch

from diffusers import (
    DEISMultistepScheduler,
    DPMSolverMultistepScheduler,
    DPMSolverSinglestepScheduler,
    UniPCMultistepScheduler,
)

from .test_schedulers import SchedulerCommonTest
class     lowerCAmelCase__						(	_lowerCamelCase ):


	'''simple docstring'''







	_SCREAMING_SNAKE_CASE	:		Any  						= (DPMSolverSinglestepScheduler,)
	_SCREAMING_SNAKE_CASE	:		Dict  						= (('''num_inference_steps''', 25),)





	def 			_lowerCAmelCase     (      self				:       Union[str, Any]     ,       **_SCREAMING_SNAKE_CASE				:       List[str]    )						->	Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							Optional[int]							=     {
					    'num_train_timesteps': 1_000,
					    'beta_start': 0.0_0_0_1,
					    'beta_end': 0.0_2,
					    'beta_schedule': 'linear',
					    'solver_order': 2,
					    'prediction_type': 'epsilon',
					    'thresholding': False,
					    'sample_max_value': 1.0,
					    'algorithm_type': 'dpmsolver++',
					    'solver_type': 'midpoint',
					    'lambda_min_clipped': -float('inf'    ),
					    'variance_type': None,
					}

					config.update(**_SCREAMING_SNAKE_CASE    )
					return config





	def 			_lowerCAmelCase     (      self				:       Dict     ,       _SCREAMING_SNAKE_CASE				:       Union[str, Any]=0     ,       **_SCREAMING_SNAKE_CASE				:       int    )						->	Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							List[Any]							=     dict(self.forward_default_kwargs    )
					SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     kwargs.pop('num_inference_steps'     ,       _SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							List[str]							=     self.dummy_sample
					SCREAMING_SNAKE_CASE						:							Dict							=     0.1 * sample
					SCREAMING_SNAKE_CASE						:							List[Any]							=     [residual + 0.2, residual + 0.1_5, residual + 0.1_0]

					for scheduler_class in self.scheduler_classes:
									SCREAMING_SNAKE_CASE						:							int							=     self.get_scheduler_config(**_SCREAMING_SNAKE_CASE    )
									SCREAMING_SNAKE_CASE						:							List[str]							=     scheduler_class(**_SCREAMING_SNAKE_CASE    )
									scheduler.set_timesteps(_SCREAMING_SNAKE_CASE    )
									# copy over dummy past residuals
									SCREAMING_SNAKE_CASE						:							Optional[int]							=     dummy_past_residuals[: scheduler.config.solver_order]

									with tempfile.TemporaryDirectory() as tmpdirname:
													scheduler.save_config(_SCREAMING_SNAKE_CASE    )
													SCREAMING_SNAKE_CASE						:							Optional[int]							=     scheduler_class.from_pretrained(_SCREAMING_SNAKE_CASE    )
													new_scheduler.set_timesteps(_SCREAMING_SNAKE_CASE    )
													# copy over dummy past residuals
													SCREAMING_SNAKE_CASE						:							Tuple							=     dummy_past_residuals[: new_scheduler.config.solver_order]

									SCREAMING_SNAKE_CASE				,						SCREAMING_SNAKE_CASE						:							Tuple							=     sample, sample
									for t in range(_SCREAMING_SNAKE_CASE     ,       time_step + scheduler.config.solver_order + 1    ):
													SCREAMING_SNAKE_CASE						:							List[Any]							=     scheduler.step(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       **_SCREAMING_SNAKE_CASE    ).prev_sample
													SCREAMING_SNAKE_CASE						:							str							=     new_scheduler.step(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       **_SCREAMING_SNAKE_CASE    ).prev_sample

													assert torch.sum(torch.abs(output - new_output    )    ) < 1E-5, "Scheduler outputs are not identical"





	def 			_lowerCAmelCase     (      self				:       Optional[Any]    )						->	Optional[int]:





					"""simple docstring"""


					pass





	def 			_lowerCAmelCase     (      self				:       Optional[Any]     ,       _SCREAMING_SNAKE_CASE				:       Optional[Any]=0     ,       **_SCREAMING_SNAKE_CASE				:       Optional[Any]    )						->	List[str]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							Dict							=     dict(self.forward_default_kwargs    )
					SCREAMING_SNAKE_CASE						:							Optional[int]							=     kwargs.pop('num_inference_steps'     ,       _SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							Any							=     self.dummy_sample
					SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     0.1 * sample
					SCREAMING_SNAKE_CASE						:							List[str]							=     [residual + 0.2, residual + 0.1_5, residual + 0.1_0]

					for scheduler_class in self.scheduler_classes:
									SCREAMING_SNAKE_CASE						:							Optional[int]							=     self.get_scheduler_config()
									SCREAMING_SNAKE_CASE						:							Tuple							=     scheduler_class(**_SCREAMING_SNAKE_CASE    )
									scheduler.set_timesteps(_SCREAMING_SNAKE_CASE    )

									# copy over dummy past residuals (must be after setting timesteps)
									SCREAMING_SNAKE_CASE						:							str							=     dummy_past_residuals[: scheduler.config.solver_order]

									with tempfile.TemporaryDirectory() as tmpdirname:
													scheduler.save_config(_SCREAMING_SNAKE_CASE    )
													SCREAMING_SNAKE_CASE						:							Dict							=     scheduler_class.from_pretrained(_SCREAMING_SNAKE_CASE    )
													# copy over dummy past residuals
													new_scheduler.set_timesteps(_SCREAMING_SNAKE_CASE    )

													# copy over dummy past residual (must be after setting timesteps)
													SCREAMING_SNAKE_CASE						:							List[Any]							=     dummy_past_residuals[: new_scheduler.config.solver_order]

									SCREAMING_SNAKE_CASE						:							Optional[Any]							=     scheduler.step(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       **_SCREAMING_SNAKE_CASE    ).prev_sample
									SCREAMING_SNAKE_CASE						:							Dict							=     new_scheduler.step(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       **_SCREAMING_SNAKE_CASE    ).prev_sample

									assert torch.sum(torch.abs(output - new_output    )    ) < 1E-5, "Scheduler outputs are not identical"





	def 			_lowerCAmelCase     (      self				:       Optional[int]     ,       _SCREAMING_SNAKE_CASE				:       str=None     ,       **_SCREAMING_SNAKE_CASE				:       List[Any]    )						->	Optional[Any]:





					"""simple docstring"""


					if scheduler is None:
									SCREAMING_SNAKE_CASE						:							Optional[Any]							=     self.scheduler_classes[0]
									SCREAMING_SNAKE_CASE						:							Optional[int]							=     self.get_scheduler_config(**_SCREAMING_SNAKE_CASE    )
									SCREAMING_SNAKE_CASE						:							int							=     scheduler_class(**_SCREAMING_SNAKE_CASE    )

					SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     self.scheduler_classes[0]
					SCREAMING_SNAKE_CASE						:							List[str]							=     self.get_scheduler_config(**_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							List[str]							=     scheduler_class(**_SCREAMING_SNAKE_CASE    )

					SCREAMING_SNAKE_CASE						:							Tuple							=     10
					SCREAMING_SNAKE_CASE						:							str							=     self.dummy_model()
					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     self.dummy_sample_deter
					scheduler.set_timesteps(_SCREAMING_SNAKE_CASE    )

					for i, t in enumerate(scheduler.timesteps    ):
									SCREAMING_SNAKE_CASE						:							List[Any]							=     model(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )
									SCREAMING_SNAKE_CASE						:							Optional[Any]							=     scheduler.step(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    ).prev_sample

					return sample





	def 			_lowerCAmelCase     (      self				:       List[Any]    )						->	Optional[Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     DPMSolverSinglestepScheduler(**self.get_scheduler_config()    )
					SCREAMING_SNAKE_CASE						:							Optional[int]							=     50
					SCREAMING_SNAKE_CASE						:							List[str]							=     self.dummy_model()
					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     self.dummy_sample_deter
					scheduler.set_timesteps(_SCREAMING_SNAKE_CASE    )

					# make sure that the first t is uneven
					for i, t in enumerate(scheduler.timesteps[3:]    ):
									SCREAMING_SNAKE_CASE						:							Any							=     model(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )
									SCREAMING_SNAKE_CASE						:							List[Any]							=     scheduler.step(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    ).prev_sample

					SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     torch.mean(torch.abs(_SCREAMING_SNAKE_CASE    )    )

					assert abs(result_mean.item() - 0.2_5_7_4    ) < 1E-3





	def 			_lowerCAmelCase     (      self				:       List[Any]    )						->	Any:





					"""simple docstring"""


					for timesteps in [25, 50, 100, 999, 1_000]:
									self.check_over_configs(num_train_timesteps=_SCREAMING_SNAKE_CASE    )





	def 			_lowerCAmelCase     (      self				:       Dict    )						->	Optional[int]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							List[Any]							=     DPMSolverSinglestepScheduler(**self.get_scheduler_config()    )
					SCREAMING_SNAKE_CASE						:							Tuple							=     self.full_loop(scheduler=_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							Any							=     torch.mean(torch.abs(_SCREAMING_SNAKE_CASE    )    )

					assert abs(result_mean.item() - 0.2_7_9_1    ) < 1E-3

					SCREAMING_SNAKE_CASE						:							List[Any]							=     DEISMultistepScheduler.from_config(scheduler.config    )
					SCREAMING_SNAKE_CASE						:							int							=     DPMSolverMultistepScheduler.from_config(scheduler.config    )
					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     UniPCMultistepScheduler.from_config(scheduler.config    )
					SCREAMING_SNAKE_CASE						:							Any							=     DPMSolverSinglestepScheduler.from_config(scheduler.config    )

					SCREAMING_SNAKE_CASE						:							Any							=     self.full_loop(scheduler=_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							int							=     torch.mean(torch.abs(_SCREAMING_SNAKE_CASE    )    )

					assert abs(result_mean.item() - 0.2_7_9_1    ) < 1E-3





	def 			_lowerCAmelCase     (      self				:       List[Any]    )						->	Union[str, Any]:





					"""simple docstring"""


					self.check_over_configs(thresholding=_SCREAMING_SNAKE_CASE    )
					for order in [1, 2, 3]:
									for solver_type in ["midpoint", "heun"]:
													for threshold in [0.5, 1.0, 2.0]:
																	for prediction_type in ["epsilon", "sample"]:
																					self.check_over_configs(
																					    thresholding=_SCREAMING_SNAKE_CASE     ,       prediction_type=_SCREAMING_SNAKE_CASE     ,       sample_max_value=_SCREAMING_SNAKE_CASE     ,       algorithm_type='dpmsolver++'     ,       solver_order=_SCREAMING_SNAKE_CASE     ,       solver_type=_SCREAMING_SNAKE_CASE     ,       )





	def 			_lowerCAmelCase     (      self				:       List[str]    )						->	Optional[Any]:





					"""simple docstring"""


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





	def 			_lowerCAmelCase     (      self				:       List[str]    )						->	Optional[Any]:





					"""simple docstring"""


					for algorithm_type in ["dpmsolver", "dpmsolver++"]:
									for solver_type in ["midpoint", "heun"]:
													for order in [1, 2, 3]:
																	for prediction_type in ["epsilon", "sample"]:
																					self.check_over_configs(
																					    solver_order=_SCREAMING_SNAKE_CASE     ,       solver_type=_SCREAMING_SNAKE_CASE     ,       prediction_type=_SCREAMING_SNAKE_CASE     ,       algorithm_type=_SCREAMING_SNAKE_CASE     ,       )
																					SCREAMING_SNAKE_CASE						:							Any							=     self.full_loop(
																					    solver_order=_SCREAMING_SNAKE_CASE     ,       solver_type=_SCREAMING_SNAKE_CASE     ,       prediction_type=_SCREAMING_SNAKE_CASE     ,       algorithm_type=_SCREAMING_SNAKE_CASE     ,       )
																					assert not torch.isnan(_SCREAMING_SNAKE_CASE    ).any(), "Samples have nan numbers"





	def 			_lowerCAmelCase     (      self				:       str    )						->	int:





					"""simple docstring"""


					self.check_over_configs(lower_order_final=_SCREAMING_SNAKE_CASE    )
					self.check_over_configs(lower_order_final=_SCREAMING_SNAKE_CASE    )





	def 			_lowerCAmelCase     (      self				:       Any    )						->	Any:





					"""simple docstring"""


					self.check_over_configs(lambda_min_clipped=-float('inf'    )    )
					self.check_over_configs(lambda_min_clipped=-5.1    )





	def 			_lowerCAmelCase     (      self				:       Optional[int]    )						->	Dict:





					"""simple docstring"""


					self.check_over_configs(variance_type=_SCREAMING_SNAKE_CASE    )
					self.check_over_configs(variance_type='learned_range'    )





	def 			_lowerCAmelCase     (      self				:       Optional[Any]    )						->	str:





					"""simple docstring"""


					for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1_000]:
									self.check_over_forward(num_inference_steps=_SCREAMING_SNAKE_CASE     ,       time_step=0    )





	def 			_lowerCAmelCase     (      self				:       Any    )						->	Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							Dict							=     self.full_loop()
					SCREAMING_SNAKE_CASE						:							str							=     torch.mean(torch.abs(_SCREAMING_SNAKE_CASE    )    )

					assert abs(result_mean.item() - 0.2_7_9_1    ) < 1E-3





	def 			_lowerCAmelCase     (      self				:       str    )						->	Dict:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     self.full_loop(use_karras_sigmas=_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     torch.mean(torch.abs(_SCREAMING_SNAKE_CASE    )    )

					assert abs(result_mean.item() - 0.2_2_4_8    ) < 1E-3





	def 			_lowerCAmelCase     (      self				:       Dict    )						->	Optional[Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							Optional[int]							=     self.full_loop(prediction_type='v_prediction'    )
					SCREAMING_SNAKE_CASE						:							List[Any]							=     torch.mean(torch.abs(_SCREAMING_SNAKE_CASE    )    )

					assert abs(result_mean.item() - 0.1_4_5_3    ) < 1E-3





	def 			_lowerCAmelCase     (      self				:       List[str]    )						->	Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							Dict							=     self.full_loop(prediction_type='v_prediction'     ,       use_karras_sigmas=_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							List[str]							=     torch.mean(torch.abs(_SCREAMING_SNAKE_CASE    )    )

					assert abs(result_mean.item() - 0.0_6_4_9    ) < 1E-3







	def 			_lowerCAmelCase     (      self				:       str    )						->	Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							List[str]							=     self.scheduler_classes[0]
					SCREAMING_SNAKE_CASE						:							List[Any]							=     self.get_scheduler_config(thresholding=_SCREAMING_SNAKE_CASE     ,       dynamic_thresholding_ratio=0    )
					SCREAMING_SNAKE_CASE						:							int							=     scheduler_class(**_SCREAMING_SNAKE_CASE    )

					SCREAMING_SNAKE_CASE						:							Any							=     10
					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     self.dummy_model()
					SCREAMING_SNAKE_CASE						:							Any							=     self.dummy_sample_deter.half()
					scheduler.set_timesteps(_SCREAMING_SNAKE_CASE    )

					for i, t in enumerate(scheduler.timesteps    ):
									SCREAMING_SNAKE_CASE						:							str							=     model(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )
									SCREAMING_SNAKE_CASE						:							Dict							=     scheduler.step(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    ).prev_sample

					assert sample.dtype == torch.floataa










"""simple docstring"""

import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


A_						:    List[Any]				=      argparse.ArgumentParser('Stable Diffusion script with intel optimization', add_help=False)
parser.add_argument('--dpm', action='store_true', help='Enable DPMSolver or not')
parser.add_argument('--steps', default=None, type=int, help='Num inference steps')
A_						:    Tuple				=      parser.parse_args()


A_						:    Optional[int]				=      'cpu'
A_						:    Dict				=      'a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings'

A_						:    List[str]				=      'path-to-your-trained-model'
A_						:    Any				=      StableDiffusionPipeline.from_pretrained(model_id)
if args.dpm:
							A_						:    Tuple				=      DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
A_						:    Tuple				=      pipe.to(device)

# to channels last
A_						:    List[str]				=      pipe.unet.to(memory_format=torch.channels_last)
A_						:    List[str]				=      pipe.vae.to(memory_format=torch.channels_last)
A_						:    Optional[int]				=      pipe.text_encoder.to(memory_format=torch.channels_last)
if pipe.requires_safety_checker:
							A_						:    Union[str, Any]				=      pipe.safety_checker.to(memory_format=torch.channels_last)

# optimize with ipex
A_						:    List[Any]				=      torch.randn(2, 4, 64, 64)
A_						:    int				=      torch.rand(1) * 999
A_						:    int				=      torch.randn(2, 77, 768)
A_						:    Tuple				=      (sample, timestep, encoder_hidden_status)
try:
							A_						:    Any				=      ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example)
except Exception:
							A_						:    List[str]				=      ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True)
A_						:    str				=      ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True)
A_						:    Optional[int]				=      ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True)
if pipe.requires_safety_checker:
							A_						:    List[str]				=      ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True)

# compute
A_						:    Union[str, Any]				=      666
A_						:    Optional[Any]				=      torch.Generator(device).manual_seed(seed)
A_						:    Union[str, Any]				=      {'generator': generator}
if args.steps is not None:
							A_						:    int				=      args.steps

with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa):
							A_						:    int				=      pipe(prompt, **generate_kwargs).images[0]

# save image
image.save('generated.png')





