
import numpy as np






def 		_UpperCAmelCase   (	UpperCamelCase:     np.array  ):






	"""simple docstring"""


	return (2 / (1 + np.exp(-2 * vector  ))) - 1


if __name__ == "__main__":
					import doctest

					doctest.testmod()








import tempfile

import torch

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

from .test_schedulers import SchedulerCommonTest

class       a		(   __UpperCAmelCase					):
							lowercase_		:						Dict   							=  (DPMSolverSinglestepScheduler,)
							lowercase_		:						Optional[Any]   							=  (('num_inference_steps', 25),)







							def       UpperCAmelCase__					(						self  :  Optional[Any]      ,				**snake_case__  :  Union[str, Any] ):
								"""simple docstring"""







								__lowerCAmelCase         =							{
								    "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(**snake_case__ )
								return config







							def       UpperCAmelCase__					(						self  :  Any      ,				snake_case__  :  Tuple=0      ,				**snake_case__  :  Optional[Any] ):
								"""simple docstring"""







								__lowerCAmelCase         =							dict(self.forward_default_kwargs )
								__lowerCAmelCase         =							kwargs.pop("num_inference_steps"      ,				snake_case__ )
								__lowerCAmelCase         =							self.dummy_sample
								__lowerCAmelCase         =							0.1 * sample
								__lowerCAmelCase         =							[residual + 0.2, residual + 0.1_5, residual + 0.1_0]

								for scheduler_class in self.scheduler_classes:
									__lowerCAmelCase         =							self.get_scheduler_config(**snake_case__ )
									__lowerCAmelCase         =							scheduler_class(**snake_case__ )
									scheduler.set_timesteps(snake_case__ )
									# copy over dummy past residuals
									__lowerCAmelCase         =							dummy_past_residuals[: scheduler.config.solver_order]

									with tempfile.TemporaryDirectory() as tmpdirname:
										scheduler.save_config(snake_case__ )
										__lowerCAmelCase         =							scheduler_class.from_pretrained(snake_case__ )
										new_scheduler.set_timesteps(snake_case__ )
										# copy over dummy past residuals
										__lowerCAmelCase         =							dummy_past_residuals[: new_scheduler.config.solver_order]

									__lowerCAmelCase   ,						__lowerCAmelCase         =							sample, sample
									for t in range(snake_case__      ,				time_step + scheduler.config.solver_order + 1 ):
										__lowerCAmelCase         =							scheduler.step(snake_case__      ,				snake_case__      ,				snake_case__      ,				**snake_case__ ).prev_sample
										__lowerCAmelCase         =							new_scheduler.step(snake_case__      ,				snake_case__      ,				snake_case__      ,				**snake_case__ ).prev_sample

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







							def       UpperCAmelCase__					(						self  :  List[str] ):
								"""simple docstring"""







								pass







							def       UpperCAmelCase__					(						self  :  str      ,				snake_case__  :  Tuple=0      ,				**snake_case__  :  Tuple ):
								"""simple docstring"""







								__lowerCAmelCase         =							dict(self.forward_default_kwargs )
								__lowerCAmelCase         =							kwargs.pop("num_inference_steps"      ,				snake_case__ )
								__lowerCAmelCase         =							self.dummy_sample
								__lowerCAmelCase         =							0.1 * sample
								__lowerCAmelCase         =							[residual + 0.2, residual + 0.1_5, residual + 0.1_0]

								for scheduler_class in self.scheduler_classes:
									__lowerCAmelCase         =							self.get_scheduler_config()
									__lowerCAmelCase         =							scheduler_class(**snake_case__ )
									scheduler.set_timesteps(snake_case__ )

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

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

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

									__lowerCAmelCase         =							scheduler.step(snake_case__      ,				snake_case__      ,				snake_case__      ,				**snake_case__ ).prev_sample
									__lowerCAmelCase         =							new_scheduler.step(snake_case__      ,				snake_case__      ,				snake_case__      ,				**snake_case__ ).prev_sample

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







							def       UpperCAmelCase__					(						self  :  Optional[Any]      ,				snake_case__  :  Optional[Any]=None      ,				**snake_case__  :  Optional[int] ):
								"""simple docstring"""







								if scheduler is None:
									__lowerCAmelCase         =							self.scheduler_classes[0]
									__lowerCAmelCase         =							self.get_scheduler_config(**snake_case__ )
									__lowerCAmelCase         =							scheduler_class(**snake_case__ )

								__lowerCAmelCase         =							self.scheduler_classes[0]
								__lowerCAmelCase         =							self.get_scheduler_config(**snake_case__ )
								__lowerCAmelCase         =							scheduler_class(**snake_case__ )

								__lowerCAmelCase         =							10
								__lowerCAmelCase         =							self.dummy_model()
								__lowerCAmelCase         =							self.dummy_sample_deter
								scheduler.set_timesteps(snake_case__ )

								for i, t in enumerate(scheduler.timesteps ):
									__lowerCAmelCase         =							model(snake_case__      ,				snake_case__ )
									__lowerCAmelCase         =							scheduler.step(snake_case__      ,				snake_case__      ,				snake_case__ ).prev_sample

								return sample







							def       UpperCAmelCase__					(						self  :  Union[str, Any] ):
								"""simple docstring"""







								__lowerCAmelCase         =							DPMSolverSinglestepScheduler(**self.get_scheduler_config() )
								__lowerCAmelCase         =							50
								__lowerCAmelCase         =							self.dummy_model()
								__lowerCAmelCase         =							self.dummy_sample_deter
								scheduler.set_timesteps(snake_case__ )

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

								__lowerCAmelCase         =							torch.mean(torch.abs(snake_case__ ) )

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







							def       UpperCAmelCase__					(						self  :  List[str] ):
								"""simple docstring"""







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







							def       UpperCAmelCase__					(						self  :  Optional[int] ):
								"""simple docstring"""







								__lowerCAmelCase         =							DPMSolverSinglestepScheduler(**self.get_scheduler_config() )
								__lowerCAmelCase         =							self.full_loop(scheduler=snake_case__ )
								__lowerCAmelCase         =							torch.mean(torch.abs(snake_case__ ) )

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

								__lowerCAmelCase         =							DEISMultistepScheduler.from_config(scheduler.config )
								__lowerCAmelCase         =							DPMSolverMultistepScheduler.from_config(scheduler.config )
								__lowerCAmelCase         =							UniPCMultistepScheduler.from_config(scheduler.config )
								__lowerCAmelCase         =							DPMSolverSinglestepScheduler.from_config(scheduler.config )

								__lowerCAmelCase         =							self.full_loop(scheduler=snake_case__ )
								__lowerCAmelCase         =							torch.mean(torch.abs(snake_case__ ) )

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







							def       UpperCAmelCase__					(						self  :  str ):
								"""simple docstring"""







								self.check_over_configs(thresholding=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=snake_case__      ,				prediction_type=snake_case__      ,				sample_max_value=snake_case__      ,				algorithm_type="dpmsolver++"      ,				solver_order=snake_case__      ,				solver_type=snake_case__      ,				)







							def       UpperCAmelCase__					(						self  :  Union[str, Any] ):
								"""simple docstring"""







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







							def       UpperCAmelCase__					(						self  :  str ):
								"""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=snake_case__      ,				solver_type=snake_case__      ,				prediction_type=snake_case__      ,				algorithm_type=snake_case__      ,				)
												__lowerCAmelCase         =							self.full_loop(
												    solver_order=snake_case__      ,				solver_type=snake_case__      ,				prediction_type=snake_case__      ,				algorithm_type=snake_case__      ,				)
												assert not torch.isnan(snake_case__ ).any(), "Samples have nan numbers"







							def       UpperCAmelCase__					(						self  :  List[str] ):
								"""simple docstring"""







								self.check_over_configs(lower_order_final=snake_case__ )
								self.check_over_configs(lower_order_final=snake_case__ )







							def       UpperCAmelCase__					(						self  :  Dict ):
								"""simple docstring"""







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







							def       UpperCAmelCase__					(						self  :  Any ):
								"""simple docstring"""







								self.check_over_configs(variance_type=snake_case__ )
								self.check_over_configs(variance_type="learned_range" )







							def       UpperCAmelCase__					(						self  :  List[Any] ):
								"""simple docstring"""







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







							def       UpperCAmelCase__					(						self  :  List[Any] ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.full_loop()
								__lowerCAmelCase         =							torch.mean(torch.abs(snake_case__ ) )

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







							def       UpperCAmelCase__					(						self  :  List[str] ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.full_loop(use_karras_sigmas=snake_case__ )
								__lowerCAmelCase         =							torch.mean(torch.abs(snake_case__ ) )

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







							def       UpperCAmelCase__					(						self  :  List[str] ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.full_loop(prediction_type="v_prediction" )
								__lowerCAmelCase         =							torch.mean(torch.abs(snake_case__ ) )

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







							def       UpperCAmelCase__					(						self  :  Optional[Any] ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.full_loop(prediction_type="v_prediction"      ,				use_karras_sigmas=snake_case__ )
								__lowerCAmelCase         =							torch.mean(torch.abs(snake_case__ ) )

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







							def       UpperCAmelCase__					(						self  :  int ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.scheduler_classes[0]
								__lowerCAmelCase         =							self.get_scheduler_config(thresholding=snake_case__      ,				dynamic_thresholding_ratio=0 )
								__lowerCAmelCase         =							scheduler_class(**snake_case__ )

								__lowerCAmelCase         =							10
								__lowerCAmelCase         =							self.dummy_model()
								__lowerCAmelCase         =							self.dummy_sample_deter.half()
								scheduler.set_timesteps(snake_case__ )

								for i, t in enumerate(scheduler.timesteps ):
									__lowerCAmelCase         =							model(snake_case__      ,				snake_case__ )
									__lowerCAmelCase         =							scheduler.step(snake_case__      ,				snake_case__      ,				snake_case__ ).prev_sample

								assert sample.dtype == torch.floataa






