



"""simple docstring"""


import torch

from diffusers import HeunDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest







class  A_     (	SCREAMING_SNAKE_CASE_							):



						"""simple docstring"""
						__UpperCamelCase													=				(HeunDiscreteScheduler,)
						__UpperCamelCase													=				10






						def       UpperCAmelCase__	(   self					:int  ,    **lowercase_					:str   )							->     Any:
												UpperCAmelCase			 =					{
												    'num_train_timesteps': 11_00,
												    'beta_start': 0.0001,
												    'beta_end': 0.02,
												    'beta_schedule': 'linear',
												}

												config.update(**lowercase_   )
												return config






						def       UpperCAmelCase__	(   self					:List[str]   )							->     int:
												for timesteps in [10, 50, 1_00, 10_00]:
																		self.check_over_configs(num_train_timesteps=lowercase_   )






						def       UpperCAmelCase__	(   self					:Any   )							->     Tuple:
												for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001]  ,    [0.0002, 0.002, 0.02]   ):
																		self.check_over_configs(beta_start=lowercase_  ,    beta_end=lowercase_   )






						def       UpperCAmelCase__	(   self					:List[Any]   )							->     List[str]:
												for schedule in ["linear", "scaled_linear", "exp"]:
																		self.check_over_configs(beta_schedule=lowercase_   )






						def       UpperCAmelCase__	(   self					:Tuple   )							->     str:
												for clip_sample_range in [1.0, 2.0, 3.0]:
																		self.check_over_configs(clip_sample_range=lowercase_  ,    clip_sample=lowercase_   )






						def       UpperCAmelCase__	(   self					:Tuple   )							->     Optional[Any]:
												for prediction_type in ["epsilon", "v_prediction", "sample"]:
																		self.check_over_configs(prediction_type=lowercase_   )






						def       UpperCAmelCase__	(   self					:int   )							->     Optional[Any]:
												UpperCAmelCase			 =					self.scheduler_classes[0]
												UpperCAmelCase			 =					self.get_scheduler_config()
												UpperCAmelCase			 =					scheduler_class(**lowercase_   )

												scheduler.set_timesteps(self.num_inference_steps   )

												UpperCAmelCase			 =					self.dummy_model()
												UpperCAmelCase			 =					self.dummy_sample_deter * scheduler.init_noise_sigma
												UpperCAmelCase			 =					sample.to(lowercase_   )

												for i, t in enumerate(scheduler.timesteps   ):
																		UpperCAmelCase			 =					scheduler.scale_model_input(lowercase_  ,    lowercase_   )

																		UpperCAmelCase			 =					model(lowercase_  ,    lowercase_   )

																		UpperCAmelCase			 =					scheduler.step(lowercase_  ,    lowercase_  ,    lowercase_   )
																		UpperCAmelCase			 =					output.prev_sample

												UpperCAmelCase			 =					torch.sum(torch.abs(lowercase_   )   )
												UpperCAmelCase			 =					torch.mean(torch.abs(lowercase_   )   )

												if torch_device in ["cpu", "mps"]:
																		assert abs(result_sum.item() - 0.1233   ) < 1E-2
																		assert abs(result_mean.item() - 0.0002   ) < 1E-3
												else:
																		# CUDA
																		assert abs(result_sum.item() - 0.1233   ) < 1E-2
																		assert abs(result_mean.item() - 0.0002   ) < 1E-3






						def       UpperCAmelCase__	(   self					:Tuple   )							->     int:
												UpperCAmelCase			 =					self.scheduler_classes[0]
												UpperCAmelCase			 =					self.get_scheduler_config(prediction_type='v_prediction'   )
												UpperCAmelCase			 =					scheduler_class(**lowercase_   )

												scheduler.set_timesteps(self.num_inference_steps   )

												UpperCAmelCase			 =					self.dummy_model()
												UpperCAmelCase			 =					self.dummy_sample_deter * scheduler.init_noise_sigma
												UpperCAmelCase			 =					sample.to(lowercase_   )

												for i, t in enumerate(scheduler.timesteps   ):
																		UpperCAmelCase			 =					scheduler.scale_model_input(lowercase_  ,    lowercase_   )

																		UpperCAmelCase			 =					model(lowercase_  ,    lowercase_   )

																		UpperCAmelCase			 =					scheduler.step(lowercase_  ,    lowercase_  ,    lowercase_   )
																		UpperCAmelCase			 =					output.prev_sample

												UpperCAmelCase			 =					torch.sum(torch.abs(lowercase_   )   )
												UpperCAmelCase			 =					torch.mean(torch.abs(lowercase_   )   )

												if torch_device in ["cpu", "mps"]:
																		assert abs(result_sum.item() - 4.6_934E-07   ) < 1E-2
																		assert abs(result_mean.item() - 6.1_112E-10   ) < 1E-3
												else:
																		# CUDA
																		assert abs(result_sum.item() - 4.693_428_650_170_972E-07   ) < 1E-2
																		assert abs(result_mean.item() - 0.0002   ) < 1E-3






						def       UpperCAmelCase__	(   self					:str   )							->     int:
												UpperCAmelCase			 =					self.scheduler_classes[0]
												UpperCAmelCase			 =					self.get_scheduler_config()
												UpperCAmelCase			 =					scheduler_class(**lowercase_   )

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

												UpperCAmelCase			 =					self.dummy_model()
												UpperCAmelCase			 =					self.dummy_sample_deter.to(lowercase_   ) * scheduler.init_noise_sigma

												for t in scheduler.timesteps:
																		UpperCAmelCase			 =					scheduler.scale_model_input(lowercase_  ,    lowercase_   )

																		UpperCAmelCase			 =					model(lowercase_  ,    lowercase_   )

																		UpperCAmelCase			 =					scheduler.step(lowercase_  ,    lowercase_  ,    lowercase_   )
																		UpperCAmelCase			 =					output.prev_sample

												UpperCAmelCase			 =					torch.sum(torch.abs(lowercase_   )   )
												UpperCAmelCase			 =					torch.mean(torch.abs(lowercase_   )   )

												if str(lowercase_   ).startswith('cpu'   ):
																		# The following sum varies between 148 and 156 on mps. Why?
																		assert abs(result_sum.item() - 0.1233   ) < 1E-2
																		assert abs(result_mean.item() - 0.0002   ) < 1E-3
												elif str(lowercase_   ).startswith('mps'   ):
																		# Larger tolerance on mps
																		assert abs(result_mean.item() - 0.0002   ) < 1E-2
												else:
																		# CUDA
																		assert abs(result_sum.item() - 0.1233   ) < 1E-2
																		assert abs(result_mean.item() - 0.0002   ) < 1E-3






						def       UpperCAmelCase__	(   self					:List[Any]   )							->     Optional[Any]:
												UpperCAmelCase			 =					self.scheduler_classes[0]
												UpperCAmelCase			 =					self.get_scheduler_config()
												UpperCAmelCase			 =					scheduler_class(**lowercase_  ,    use_karras_sigmas=lowercase_   )

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

												UpperCAmelCase			 =					self.dummy_model()
												UpperCAmelCase			 =					self.dummy_sample_deter.to(lowercase_   ) * scheduler.init_noise_sigma
												UpperCAmelCase			 =					sample.to(lowercase_   )

												for t in scheduler.timesteps:
																		UpperCAmelCase			 =					scheduler.scale_model_input(lowercase_  ,    lowercase_   )

																		UpperCAmelCase			 =					model(lowercase_  ,    lowercase_   )

																		UpperCAmelCase			 =					scheduler.step(lowercase_  ,    lowercase_  ,    lowercase_   )
																		UpperCAmelCase			 =					output.prev_sample

												UpperCAmelCase			 =					torch.sum(torch.abs(lowercase_   )   )
												UpperCAmelCase			 =					torch.mean(torch.abs(lowercase_   )   )

												assert abs(result_sum.item() - 0.0_0015   ) < 1E-2
												assert abs(result_mean.item() - 1.9_869_554_535_034_695E-07   ) < 1E-2










from typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
		import torch

__magic_name__       =     logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
		from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):





		def 		__snake_case						(    cls				):
							"""simple docstring"""






							return cls





def 		__snake_case						(    _UpperCAmelCase       ,			_UpperCAmelCase = None       ,			_UpperCAmelCase = None       ,			_UpperCAmelCase = None       ,			_UpperCAmelCase = None       ,			):
					"""simple docstring"""






					lowercase						   =       device
					lowercase						   =       shape[0]

					lowercase						   =       layout or torch.strided
					lowercase						   =       device or torch.device('cpu'				)

					if generator is not None:
										lowercase						   =       generator.device.type if not isinstance(_UpperCAmelCase       ,			_UpperCAmelCase				) else generator[0].device.type
										if gen_device_type != device.type and gen_device_type == "cpu":
															lowercase						   =       'cpu'
															if device != "mps":
																				logger.info(
																				    f"""The passed generator was created on 'cpu' even though a tensor on {device} was expected."""
																				    f""" Tensors will be created on 'cpu' and then moved to {device}. Note that one can probably"""
																				    f""" slighly speed up this function by passing a generator that was created on the {device} device."""				)
										elif gen_device_type != device.type and gen_device_type == "cuda":
															raise ValueError(f"""Cannot generate a {device} tensor from a generator of type {gen_device_type}."""				)

					if isinstance(_UpperCAmelCase       ,			_UpperCAmelCase				):
										lowercase						   =       (1,) + shape[1:]
										lowercase						   =       [
										    torch.randn(_UpperCAmelCase       ,			generator=generator[i]       ,			device=_UpperCAmelCase       ,			dtype=_UpperCAmelCase       ,			layout=_UpperCAmelCase				)
										    for i in range(_UpperCAmelCase				)
										]
										lowercase						   =       torch.cat(_UpperCAmelCase       ,			dim=0				).to(_UpperCAmelCase				)
					else:
										lowercase						   =       torch.randn(_UpperCAmelCase       ,			generator=_UpperCAmelCase       ,			device=_UpperCAmelCase       ,			dtype=_UpperCAmelCase       ,			layout=_UpperCAmelCase				).to(_UpperCAmelCase				)

					return latents





def 		__snake_case						(    _UpperCAmelCase				):
					"""simple docstring"""






					if is_torch_version('<'       ,			'2.0.0'				) or not hasattr(_UpperCAmelCase       ,			'_dynamo'				):
										return False
					return isinstance(_UpperCAmelCase       ,			torch._dynamo.eval_frame.OptimizedModule				)






