





'''simple docstring'''






import tempfile

import torch

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

from .test_schedulers import SchedulerCommonTest
class 		lowerCamelCase	(   _A							):
	snake_case_   = (UniPCMultistepScheduler,)
	snake_case_   = (("num_inference_steps", 25),)



	def 	_lowerCamelCase     ( self		,   **a_						):
			lowerCAmelCase  :     Any       						= {
			    "num_train_timesteps": 1_000,
			    "beta_start": 0.0001,
			    "beta_end": 0.02,
			    "beta_schedule": "linear",
			    "solver_order": 2,
			    "solver_type": "bh2",
			}

			config.update(**a_						)
			return config



	def 	_lowerCamelCase     ( self		,   a_=0		,   **a_						):
			lowerCAmelCase  :     int       						= dict(self.forward_default_kwargs						)
			lowerCAmelCase  :     List[Any]       						= kwargs.pop("num_inference_steps"		,   a_						)
			lowerCAmelCase  :     Dict       						= self.dummy_sample
			lowerCAmelCase  :     Optional[int]       						= 0.1 * sample
			lowerCAmelCase  :     List[str]       						= [residual + 0.2, residual + 0.15, residual + 0.10]

			for scheduler_class in self.scheduler_classes:
					lowerCAmelCase  :     List[str]       						= self.get_scheduler_config(**a_						)
					lowerCAmelCase  :     Dict       						= scheduler_class(**a_						)
					scheduler.set_timesteps(a_						)
					# copy over dummy past residuals
					lowerCAmelCase  :     List[Any]       						= dummy_past_residuals[: scheduler.config.solver_order]

					with tempfile.TemporaryDirectory() as tmpdirname:
							scheduler.save_config(a_						)
							lowerCAmelCase  :     List[Any]       						= scheduler_class.from_pretrained(a_						)
							new_scheduler.set_timesteps(a_						)
							# copy over dummy past residuals
							lowerCAmelCase  :     int       						= dummy_past_residuals[: new_scheduler.config.solver_order]

					lowerCAmelCase						,		lowerCAmelCase  :     Dict       						= sample, sample
					for t in range(a_		,   time_step + scheduler.config.solver_order + 1						):
							lowerCAmelCase  :     Dict       						= scheduler.step(a_		,   a_		,   a_		,   **a_						).prev_sample
							lowerCAmelCase  :     List[Any]       						= new_scheduler.step(a_		,   a_		,   a_		,   **a_						).prev_sample

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



	def 	_lowerCamelCase     ( self		,   a_=0		,   **a_						):
			lowerCAmelCase  :     Optional[Any]       						= dict(self.forward_default_kwargs						)
			lowerCAmelCase  :     Any       						= kwargs.pop("num_inference_steps"		,   a_						)
			lowerCAmelCase  :     Optional[int]       						= self.dummy_sample
			lowerCAmelCase  :     Tuple       						= 0.1 * sample
			lowerCAmelCase  :     str       						= [residual + 0.2, residual + 0.15, residual + 0.10]

			for scheduler_class in self.scheduler_classes:
					lowerCAmelCase  :     str       						= self.get_scheduler_config()
					lowerCAmelCase  :     Union[str, Any]       						= scheduler_class(**a_						)
					scheduler.set_timesteps(a_						)

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

					with tempfile.TemporaryDirectory() as tmpdirname:
							scheduler.save_config(a_						)
							lowerCAmelCase  :     Optional[int]       						= scheduler_class.from_pretrained(a_						)
							# copy over dummy past residuals
							new_scheduler.set_timesteps(a_						)

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

					lowerCAmelCase  :     List[str]       						= scheduler.step(a_		,   a_		,   a_		,   **a_						).prev_sample
					lowerCAmelCase  :     Optional[Any]       						= new_scheduler.step(a_		,   a_		,   a_		,   **a_						).prev_sample

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



	def 	_lowerCamelCase     ( self		,   a_=None		,   **a_						):
			if scheduler is None:
					lowerCAmelCase  :     Dict       						= self.scheduler_classes[0]
					lowerCAmelCase  :     Dict       						= self.get_scheduler_config(**a_						)
					lowerCAmelCase  :     List[Any]       						= scheduler_class(**a_						)

			lowerCAmelCase  :     Any       						= self.scheduler_classes[0]
			lowerCAmelCase  :     int       						= self.get_scheduler_config(**a_						)
			lowerCAmelCase  :     Dict       						= scheduler_class(**a_						)

			lowerCAmelCase  :     Tuple       						= 10
			lowerCAmelCase  :     Tuple       						= self.dummy_model()
			lowerCAmelCase  :     Optional[int]       						= self.dummy_sample_deter
			scheduler.set_timesteps(a_						)

			for i, t in enumerate(scheduler.timesteps						):
					lowerCAmelCase  :     Dict       						= model(a_		,   a_						)
					lowerCAmelCase  :     Dict       						= scheduler.step(a_		,   a_		,   a_						).prev_sample

			return sample



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     List[str]       						= dict(self.forward_default_kwargs						)

			lowerCAmelCase  :     List[str]       						= kwargs.pop("num_inference_steps"		,   a_						)

			for scheduler_class in self.scheduler_classes:
					lowerCAmelCase  :     Optional[Any]       						= self.get_scheduler_config()
					lowerCAmelCase  :     Any       						= scheduler_class(**a_						)

					lowerCAmelCase  :     Any       						= self.dummy_sample
					lowerCAmelCase  :     str       						= 0.1 * sample

					if num_inference_steps is not None and hasattr(a_		,   "set_timesteps"						):
							scheduler.set_timesteps(a_						)
					elif num_inference_steps is not None and not hasattr(a_		,   "set_timesteps"						):
							lowerCAmelCase  :     Optional[Any]       						= num_inference_steps

					# copy over dummy past residuals (must be done after set_timesteps)
					lowerCAmelCase  :     Any       						= [residual + 0.2, residual + 0.15, residual + 0.10]
					lowerCAmelCase  :     Tuple       						= dummy_past_residuals[: scheduler.config.solver_order]

					lowerCAmelCase  :     Union[str, Any]       						= scheduler.timesteps[5]
					lowerCAmelCase  :     int       						= scheduler.timesteps[6]

					lowerCAmelCase  :     Union[str, Any]       						= scheduler.step(a_		,   a_		,   a_		,   **a_						).prev_sample
					lowerCAmelCase  :     Union[str, Any]       						= scheduler.step(a_		,   a_		,   a_		,   **a_						).prev_sample

					self.assertEqual(output_a.shape		,   sample.shape						)
					self.assertEqual(output_a.shape		,   output_a.shape						)



	def 	_lowerCamelCase     ( self						):
			# make sure that iterating over schedulers with same config names gives same results
			# for defaults
			lowerCAmelCase  :     Tuple       						= UniPCMultistepScheduler(**self.get_scheduler_config()						)
			lowerCAmelCase  :     Optional[int]       						= self.full_loop(scheduler=a_						)
			lowerCAmelCase  :     Dict       						= torch.mean(torch.abs(a_						)						)

			assert abs(result_mean.item() - 0.2464						) < 1e-3

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

			lowerCAmelCase  :     Union[str, Any]       						= self.full_loop(scheduler=a_						)
			lowerCAmelCase  :     Optional[Any]       						= torch.mean(torch.abs(a_						)						)

			assert abs(result_mean.item() - 0.2464						) < 1e-3



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



	def 	_lowerCamelCase     ( self						):
			self.check_over_configs(thresholding=a_						)
			for order in [1, 2, 3]:
					for solver_type in ["bh1", "bh2"]:
							for threshold in [0.5, 1.0, 2.0]:
									for prediction_type in ["epsilon", "sample"]:
											self.check_over_configs(
											    thresholding=a_		,   prediction_type=a_		,   sample_max_value=a_		,   solver_order=a_		,   solver_type=a_		,   )



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



	def 	_lowerCamelCase     ( self						):
			for solver_type in ["bh1", "bh2"]:
					for order in [1, 2, 3]:
							for prediction_type in ["epsilon", "sample"]:
									self.check_over_configs(
									    solver_order=a_		,   solver_type=a_		,   prediction_type=a_		,   )
									lowerCAmelCase  :     Union[str, Any]       						= self.full_loop(
									    solver_order=a_		,   solver_type=a_		,   prediction_type=a_		,   )
									assert not torch.isnan(a_						).any(), "Samples have nan numbers"



	def 	_lowerCamelCase     ( self						):
			self.check_over_configs(lower_order_final=a_						)
			self.check_over_configs(lower_order_final=a_						)



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



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     int       						= self.full_loop()
			lowerCAmelCase  :     Union[str, Any]       						= torch.mean(torch.abs(a_						)						)

			assert abs(result_mean.item() - 0.2464						) < 1e-3



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     Optional[int]       						= self.full_loop(prediction_type="v_prediction"						)
			lowerCAmelCase  :     List[str]       						= torch.mean(torch.abs(a_						)						)

			assert abs(result_mean.item() - 0.1014						) < 1e-3



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     List[Any]       						= self.scheduler_classes[0]
			lowerCAmelCase  :     Optional[Any]       						= self.get_scheduler_config(thresholding=a_		,   dynamic_thresholding_ratio=0						)
			lowerCAmelCase  :     Union[str, Any]       						= scheduler_class(**a_						)

			lowerCAmelCase  :     str       						= 10
			lowerCAmelCase  :     Union[str, Any]       						= self.dummy_model()
			lowerCAmelCase  :     Optional[Any]       						= self.dummy_sample_deter.half()
			scheduler.set_timesteps(a_						)

			for i, t in enumerate(scheduler.timesteps						):
					lowerCAmelCase  :     List[str]       						= model(a_		,   a_						)
					lowerCAmelCase  :     Union[str, Any]       						= scheduler.step(a_		,   a_		,   a_						).prev_sample

			assert sample.dtype == torch.floataa





	def 	_lowerCamelCase     ( self		,   **a_						):
			for scheduler_class in self.scheduler_classes:
					lowerCAmelCase  :     List[str]       						= self.get_scheduler_config(**a_						)
					lowerCAmelCase  :     Dict       						= scheduler_class(**a_						)

					scheduler.set_timesteps(scheduler.config.num_train_timesteps						)
					assert len(scheduler.timesteps.unique()						) == scheduler.num_inference_steps










'''simple docstring'''






import math

import tensorflow as tf
from packaging import version






def   __A    (				a_    :	List[Any]						):
		lowerCAmelCase  :     Any       						= tf.convert_to_tensor(a_						)
		lowerCAmelCase  :     List[Any]       						= 0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0						)   ,x.dtype						)						))

		return x * cdf






def   __A    (				a_    :	int						):
		lowerCAmelCase  :     Dict       						= tf.convert_to_tensor(a_						)
		lowerCAmelCase  :     int       						= tf.cast(math.pi   ,x.dtype						)
		lowerCAmelCase  :     Dict       						= tf.cast(0.0_4_4_7_1_5   ,x.dtype						)
		lowerCAmelCase  :     Dict       						= 0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi						) * (x + coeff * tf.pow(a_   ,3						))						))

		return x * cdf






def   __A    (				a_    :	Union[str, Any]						):
		lowerCAmelCase  :     Any       						= tf.convert_to_tensor(a_						)

		return x * tf.tanh(tf.math.softplus(a_						)						)






def   __A    (				a_    :	List[str]						):
		lowerCAmelCase  :     Dict       						= tf.convert_to_tensor(a_						)
		lowerCAmelCase  :     Any       						= tf.cast(0.0_4_4_7_1_5   ,x.dtype						)
		lowerCAmelCase  :     Optional[int]       						= tf.cast(0.7_9_7_8_8_4_5_6_0_8   ,x.dtype						)

		return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x)						))






def   __A    (				a_    :	Union[str, Any]						):
		lowerCAmelCase  :     Optional[int]       						= tf.convert_to_tensor(a_						)
		lowerCAmelCase  :     List[Any]       						= tf.cast(1.7_0_2   ,x.dtype						)
		return x * tf.math.sigmoid(coeff * x						)






def   __A    (				a_    :	List[Any]						):
		return tf.clip_by_value(_gelu(a_						)   ,-1_0   ,1_0						)






def   __A    (				a_    :	List[Any]   ,a_    :	List[Any]=-1						):
		lowerCAmelCase						,		lowerCAmelCase  :     Optional[int]       						= tf.split(a_   ,2   ,axis=a_						)
		return a * tf.math.sigmoid(a_						)


if version.parse(tf.version.VERSION) >= version.parse("""2.4"""):






					def   __A    (				a_    :	Optional[Any]						):
							return tf.keras.activations.gelu(a_   ,approximate=a_						)

					lowerCAmelCase									=   tf.keras.activations.gelu

					lowerCAmelCase									=   approximate_gelu_wrap
else:
					lowerCAmelCase									=   _gelu
					lowerCAmelCase									=   _gelu_new


lowerCAmelCase									=   {
    """gelu""": gelu,
    """gelu_10""": gelu_aa,
    """gelu_fast""": gelu_fast,
    """gelu_new""": gelu_new,
    """glu""": glu,
    """mish""": mish,
    """quick_gelu""": quick_gelu,
    """relu""": tf.keras.activations.relu,
    """sigmoid""": tf.keras.activations.sigmoid,
    """silu""": tf.keras.activations.swish,
    """swish""": tf.keras.activations.swish,
    """tanh""": tf.keras.activations.tanh,
}






def   __A    (				a_    :	int						):
		if activation_string in ACTaFN:
				return ACTaFN[activation_string]
		else:
				raise KeyError(f'''function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()						)}'''						)



