


"""simple docstring"""


from typing import Any

import numpy as np


def _lowerCamelCase      (						_UpperCamelCase ):

			'''simple docstring'''




			return np.array_equal(_UpperCamelCase       ,					matrix.conjugate().T )


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			__lowerCAmelCase							       =     v.conjugate().T
			__lowerCAmelCase							       =     v_star.dot(_UpperCamelCase )
			assert isinstance(_UpperCamelCase       ,					np.ndarray )
			return (v_star_dot.dot(_UpperCamelCase )) / (v_star.dot(_UpperCamelCase ))


def _lowerCamelCase      (						):

			'''simple docstring'''




			__lowerCAmelCase							       =     np.array([[2, 2 + 1J, 4], [2 - 1J, 3, 1J], [4, -1J, 1]] )
			__lowerCAmelCase							       =     np.array([[1], [2], [3]] )
			assert is_hermitian(_UpperCamelCase ), f"{a} is not hermitian."
			print(rayleigh_quotient(_UpperCamelCase       ,					_UpperCamelCase ) )

			__lowerCAmelCase							       =     np.array([[1, 2, 4], [2, 3, -1], [4, -1, 1]] )
			assert is_hermitian(_UpperCamelCase ), f"{a} is not hermitian."
			assert rayleigh_quotient(_UpperCamelCase       ,					_UpperCamelCase ) == float(3 )


if __name__ == "__main__":
	import doctest

	doctest.testmod()
	tests()








"""simple docstring"""


import torch

from diffusers import KDPMaDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest
class     _UpperCamelCase    (  lowerCAmelCase__						):
							'''simple docstring'''







							__UpperCAmelCase		:       Tuple							 =(KDPMaDiscreteScheduler,)
							__UpperCAmelCase		:       Optional[Any]							 =1_0

							def      snake_case							(	self       ,  **__a      ):
										__lowerCAmelCase							       =     {
										    "num_train_timesteps": 11_00,
										    "beta_start": 0.0_0_0_1,
										    "beta_end": 0.0_2,
										    "beta_schedule": "linear",
										}

										config.update(**__a      )
										return config

							def      snake_case							(	self      ):
										for timesteps in [10, 50, 1_00, 10_00]:
													self.check_over_configs(num_train_timesteps=__a      )

							def      snake_case							(	self      ):
										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=__a       ,  beta_end=__a      )

							def      snake_case							(	self      ):
										for schedule in ["linear", "scaled_linear"]:
													self.check_over_configs(beta_schedule=__a      )

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

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     self.scheduler_classes[0]
										__lowerCAmelCase							       =     self.get_scheduler_config(prediction_type="v_prediction"      )
										__lowerCAmelCase							       =     scheduler_class(**__a      )

										scheduler.set_timesteps(self.num_inference_steps      )

										__lowerCAmelCase							       =     self.dummy_model()
										__lowerCAmelCase							       =     self.dummy_sample_deter * scheduler.init_noise_sigma
										__lowerCAmelCase							       =     sample.to(__a      )

										for i, t in enumerate(scheduler.timesteps      ):
													__lowerCAmelCase							       =     scheduler.scale_model_input(__a       ,  __a      )

													__lowerCAmelCase							       =     model(__a       ,  __a      )

													__lowerCAmelCase							       =     scheduler.step(__a       ,  __a       ,  __a      )
													__lowerCAmelCase							       =     output.prev_sample

										__lowerCAmelCase							       =     torch.sum(torch.abs(__a      )      )
										__lowerCAmelCase							       =     torch.mean(torch.abs(__a      )      )

										if torch_device in ["cpu", "mps"]:
													assert abs(result_sum.item() - 4.6_9_3_4e-0_7      ) < 1e-2
													assert abs(result_mean.item() - 6.1_1_1_2e-1_0      ) < 1e-3
										else:
													# CUDA
													assert abs(result_sum.item() - 4.6_9_3_4_2_8_6_5_0_1_7_0_9_7_2e-0_7      ) < 1e-2
													assert abs(result_mean.item() - 0.0_0_0_2      ) < 1e-3

							def      snake_case							(	self      ):
										if torch_device == "mps":
													return
										__lowerCAmelCase							       =     self.scheduler_classes[0]
										__lowerCAmelCase							       =     self.get_scheduler_config()
										__lowerCAmelCase							       =     scheduler_class(**__a      )

										scheduler.set_timesteps(self.num_inference_steps      )

										__lowerCAmelCase							       =     self.dummy_model()
										__lowerCAmelCase							       =     self.dummy_sample_deter * scheduler.init_noise_sigma
										__lowerCAmelCase							       =     sample.to(__a      )

										for i, t in enumerate(scheduler.timesteps      ):
													__lowerCAmelCase							       =     scheduler.scale_model_input(__a       ,  __a      )

													__lowerCAmelCase							       =     model(__a       ,  __a      )

													__lowerCAmelCase							       =     scheduler.step(__a       ,  __a       ,  __a      )
													__lowerCAmelCase							       =     output.prev_sample

										__lowerCAmelCase							       =     torch.sum(torch.abs(__a      )      )
										__lowerCAmelCase							       =     torch.mean(torch.abs(__a      )      )

										if torch_device in ["cpu", "mps"]:
													assert abs(result_sum.item() - 2_0.4_1_2_5      ) < 1e-2
													assert abs(result_mean.item() - 0.0_2_6_6      ) < 1e-3
										else:
													# CUDA
													assert abs(result_sum.item() - 2_0.4_1_2_5      ) < 1e-2
													assert abs(result_mean.item() - 0.0_2_6_6      ) < 1e-3



							def      snake_case							(	self      ):
										if torch_device == "mps":
													return
										__lowerCAmelCase							       =     self.scheduler_classes[0]
										__lowerCAmelCase							       =     self.get_scheduler_config()
										__lowerCAmelCase							       =     scheduler_class(**__a      )

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

										__lowerCAmelCase							       =     self.dummy_model()
										__lowerCAmelCase							       =     self.dummy_sample_deter.to(__a      ) * scheduler.init_noise_sigma

										for t in scheduler.timesteps:
													__lowerCAmelCase							       =     scheduler.scale_model_input(__a       ,  __a      )

													__lowerCAmelCase							       =     model(__a       ,  __a      )

													__lowerCAmelCase							       =     scheduler.step(__a       ,  __a       ,  __a      )
													__lowerCAmelCase							       =     output.prev_sample

										__lowerCAmelCase							       =     torch.sum(torch.abs(__a      )      )
										__lowerCAmelCase							       =     torch.mean(torch.abs(__a      )      )

										if str(__a      ).startswith("cpu"      ):
													# The following sum varies between 148 and 156 on mps. Why?
													assert abs(result_sum.item() - 2_0.4_1_2_5      ) < 1e-2
													assert abs(result_mean.item() - 0.0_2_6_6      ) < 1e-3
										else:
													# CUDA
													assert abs(result_sum.item() - 2_0.4_1_2_5      ) < 1e-2
													assert abs(result_mean.item() - 0.0_2_6_6      ) < 1e-3




