


'''simple docstring'''







import unittest

import numpy as np

from transformers import is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
		import tensorflow as tf

		from transformers.activations_tf import get_tf_activation






@require_tf
class 		_snake_case    (				unittest.TestCase		):






						'''simple docstring'''







						def lowerCAmelCase__ (  self:	Any     )							->	Union[str, Any]:
										__magic_name__				:					Tuple						       =       tf.constant([-100, -1.0, -0.1, 0, 0.1, 1.0, 1_0_0.0]     )
										__magic_name__				:					Union[str, Any]						       =       get_tf_activation("gelu"     )
										__magic_name__				:					Optional[Any]						       =       get_tf_activation("gelu_10"     )

										__magic_name__				:					Optional[Any]						       =       gelu(__UpperCamelCase     )
										__magic_name__				:					Any						       =       geluaa(__UpperCamelCase     )

										__magic_name__				:					Tuple						       =       tf.where(y_gelu_aa < 1_0.0      ,							1.0      ,							0.0     )

										self.assertEqual(tf.math.reduce_max(__UpperCamelCase     ).numpy().item()      ,							1_0.0     )
										self.assertTrue(np.allclose(y_gelu * clipped_mask      ,							y_gelu_aa * clipped_mask     )     )







						def lowerCAmelCase__ (  self:	List[Any]     )							->	Optional[Any]:
										get_tf_activation("gelu"     )
										get_tf_activation("gelu_10"     )
										get_tf_activation("gelu_fast"     )
										get_tf_activation("gelu_new"     )
										get_tf_activation("glu"     )
										get_tf_activation("mish"     )
										get_tf_activation("quick_gelu"     )
										get_tf_activation("relu"     )
										get_tf_activation("sigmoid"     )
										get_tf_activation("silu"     )
										get_tf_activation("swish"     )
										get_tf_activation("tanh"     )
										with self.assertRaises(__UpperCamelCase     ):
														get_tf_activation("bogus"     )
										with self.assertRaises(__UpperCamelCase     ):
														get_tf_activation(__UpperCamelCase     )



'''simple docstring'''







import torch

from diffusers import LMSDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest






class 		_snake_case    (				snake_case_		):






						'''simple docstring'''







						__snake_case   =      (LMSDiscreteScheduler,)
						__snake_case   =      1_0







						def lowerCAmelCase__ (  self:	Union[str, Any]      ,							**__UpperCamelCase:	Any     )							->	List[str]:
										__magic_name__				:					List[str]						       =       {
										    "num_train_timesteps": 1100,
										    "beta_start": 0.0_0_0_1,
										    "beta_end": 0.0_2,
										    "beta_schedule": "linear",
										}

										config.update(**__UpperCamelCase     )
										return config







						def lowerCAmelCase__ (  self:	Any     )							->	str:
										for timesteps in [10, 50, 100, 1000]:
														self.check_over_configs(num_train_timesteps=__UpperCamelCase     )







						def lowerCAmelCase__ (  self:	str     )							->	int:
										for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1]      ,							[0.0_0_0_2, 0.0_0_2, 0.0_2]     ):
														self.check_over_configs(beta_start=__UpperCamelCase      ,							beta_end=__UpperCamelCase     )







						def lowerCAmelCase__ (  self:	str     )							->	Any:
										for schedule in ["linear", "scaled_linear"]:
														self.check_over_configs(beta_schedule=__UpperCamelCase     )







						def lowerCAmelCase__ (  self:	Dict     )							->	int:
										for prediction_type in ["epsilon", "v_prediction"]:
														self.check_over_configs(prediction_type=__UpperCamelCase     )







						def lowerCAmelCase__ (  self:	Optional[Any]     )							->	Optional[Any]:
										for t in [0, 500, 800]:
														self.check_over_forward(time_step=__UpperCamelCase     )







						def lowerCAmelCase__ (  self:	List[str]     )							->	Optional[int]:
										__magic_name__				:					Dict						       =       self.scheduler_classes[0]
										__magic_name__				:					List[str]						       =       self.get_scheduler_config()
										__magic_name__				:					Dict						       =       scheduler_class(**__UpperCamelCase     )

										scheduler.set_timesteps(self.num_inference_steps     )

										__magic_name__				:					str						       =       self.dummy_model()
										__magic_name__				:					str						       =       self.dummy_sample_deter * scheduler.init_noise_sigma

										for i, t in enumerate(scheduler.timesteps     ):
														__magic_name__				:					Dict						       =       scheduler.scale_model_input(__UpperCamelCase      ,							__UpperCamelCase     )

														__magic_name__				:					Optional[Any]						       =       model(__UpperCamelCase      ,							__UpperCamelCase     )

														__magic_name__				:					Union[str, Any]						       =       scheduler.step(__UpperCamelCase      ,							__UpperCamelCase      ,							__UpperCamelCase     )
														__magic_name__				:					Dict						       =       output.prev_sample

										__magic_name__				:					Tuple						       =       torch.sum(torch.abs(__UpperCamelCase     )     )
										__magic_name__				:					Union[str, Any]						       =       torch.mean(torch.abs(__UpperCamelCase     )     )

										assert abs(result_sum.item() - 1_0_0_6.3_8_8     ) < 1E-2
										assert abs(result_mean.item() - 1.3_1     ) < 1E-3







						def lowerCAmelCase__ (  self:	int     )							->	Tuple:
										__magic_name__				:					Optional[int]						       =       self.scheduler_classes[0]
										__magic_name__				:					Tuple						       =       self.get_scheduler_config(prediction_type="v_prediction"     )
										__magic_name__				:					List[Any]						       =       scheduler_class(**__UpperCamelCase     )

										scheduler.set_timesteps(self.num_inference_steps     )

										__magic_name__				:					Tuple						       =       self.dummy_model()
										__magic_name__				:					str						       =       self.dummy_sample_deter * scheduler.init_noise_sigma

										for i, t in enumerate(scheduler.timesteps     ):
														__magic_name__				:					int						       =       scheduler.scale_model_input(__UpperCamelCase      ,							__UpperCamelCase     )

														__magic_name__				:					int						       =       model(__UpperCamelCase      ,							__UpperCamelCase     )

														__magic_name__				:					Dict						       =       scheduler.step(__UpperCamelCase      ,							__UpperCamelCase      ,							__UpperCamelCase     )
														__magic_name__				:					Dict						       =       output.prev_sample

										__magic_name__				:					Optional[Any]						       =       torch.sum(torch.abs(__UpperCamelCase     )     )
										__magic_name__				:					List[Any]						       =       torch.mean(torch.abs(__UpperCamelCase     )     )

										assert abs(result_sum.item() - 0.0_0_1_7     ) < 1E-2
										assert abs(result_mean.item() - 2.2_676E-06     ) < 1E-3







						def lowerCAmelCase__ (  self:	Union[str, Any]     )							->	str:
										__magic_name__				:					Optional[int]						       =       self.scheduler_classes[0]
										__magic_name__				:					List[Any]						       =       self.get_scheduler_config()
										__magic_name__				:					Any						       =       scheduler_class(**__UpperCamelCase     )

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

										__magic_name__				:					Optional[Any]						       =       self.dummy_model()
										__magic_name__				:					Union[str, Any]						       =       self.dummy_sample_deter * scheduler.init_noise_sigma.cpu()
										__magic_name__				:					int						       =       sample.to(__UpperCamelCase     )

										for i, t in enumerate(scheduler.timesteps     ):
														__magic_name__				:					Optional[int]						       =       scheduler.scale_model_input(__UpperCamelCase      ,							__UpperCamelCase     )

														__magic_name__				:					Union[str, Any]						       =       model(__UpperCamelCase      ,							__UpperCamelCase     )

														__magic_name__				:					int						       =       scheduler.step(__UpperCamelCase      ,							__UpperCamelCase      ,							__UpperCamelCase     )
														__magic_name__				:					List[Any]						       =       output.prev_sample

										__magic_name__				:					Any						       =       torch.sum(torch.abs(__UpperCamelCase     )     )
										__magic_name__				:					int						       =       torch.mean(torch.abs(__UpperCamelCase     )     )

										assert abs(result_sum.item() - 1_0_0_6.3_8_8     ) < 1E-2
										assert abs(result_mean.item() - 1.3_1     ) < 1E-3







						def lowerCAmelCase__ (  self:	List[str]     )							->	str:
										__magic_name__				:					Dict						       =       self.scheduler_classes[0]
										__magic_name__				:					Optional[Any]						       =       self.get_scheduler_config()
										__magic_name__				:					Dict						       =       scheduler_class(**__UpperCamelCase      ,							use_karras_sigmas=__UpperCamelCase     )

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

										__magic_name__				:					int						       =       self.dummy_model()
										__magic_name__				:					Optional[int]						       =       self.dummy_sample_deter.to(__UpperCamelCase     ) * scheduler.init_noise_sigma
										__magic_name__				:					List[str]						       =       sample.to(__UpperCamelCase     )

										for t in scheduler.timesteps:
														__magic_name__				:					str						       =       scheduler.scale_model_input(__UpperCamelCase      ,							__UpperCamelCase     )

														__magic_name__				:					List[Any]						       =       model(__UpperCamelCase      ,							__UpperCamelCase     )

														__magic_name__				:					str						       =       scheduler.step(__UpperCamelCase      ,							__UpperCamelCase      ,							__UpperCamelCase     )
														__magic_name__				:					int						       =       output.prev_sample

										__magic_name__				:					str						       =       torch.sum(torch.abs(__UpperCamelCase     )     )
										__magic_name__				:					Dict						       =       torch.mean(torch.abs(__UpperCamelCase     )     )

										assert abs(result_sum.item() - 3_8_1_2.9_9_2_7     ) < 2E-2
										assert abs(result_mean.item() - 4.9_6_4_8     ) < 1E-3