import tempfile

import torch

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

from .test_schedulers import SchedulerCommonTest







class __snake_case       (		lowerCamelCase_	):
   lowerCAmelCase_														=							(DPMSolverSinglestepScheduler,)
   lowerCAmelCase_														=							(("num_inference_steps", 25),)




   def 			__a		(					self    :       List[str] ,			**_lowercase    :       Tuple   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   {
         """num_train_timesteps""": 10_00,
         """beta_start""": 0.00_01,
         """beta_end""": 0.02,
         """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(**_lowercase   )
     return config




   def 			__a		(					self    :       List[Any] ,			_lowercase    :       Dict=0 ,			**_lowercase    :       Dict   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   dict(self.forward_default_kwargs   )
     SCREAMING_SNAKE_CASE__    							=   kwargs.pop("""num_inference_steps""" ,			_lowercase   )
     SCREAMING_SNAKE_CASE__    							=   self.dummy_sample
     SCREAMING_SNAKE_CASE__    							=   0.1 * sample
     SCREAMING_SNAKE_CASE__    							=   [residual + 0.2, residual + 0.15, residual + 0.10]

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

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

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

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




   def 			__a		(					self    :       Union[str, Any]   ):



     """simple docstring"""






     pass




   def 			__a		(					self    :       int ,			_lowercase    :       str=0 ,			**_lowercase    :       List[Any]   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   dict(self.forward_default_kwargs   )
     SCREAMING_SNAKE_CASE__    							=   kwargs.pop("""num_inference_steps""" ,			_lowercase   )
     SCREAMING_SNAKE_CASE__    							=   self.dummy_sample
     SCREAMING_SNAKE_CASE__    							=   0.1 * sample
     SCREAMING_SNAKE_CASE__    							=   [residual + 0.2, residual + 0.15, residual + 0.10]

     for scheduler_class in self.scheduler_classes:
       SCREAMING_SNAKE_CASE__    							=   self.get_scheduler_config()
       SCREAMING_SNAKE_CASE__    							=   scheduler_class(**_lowercase   )
       scheduler.set_timesteps(_lowercase   )

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

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

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

       SCREAMING_SNAKE_CASE__    							=   scheduler.step(_lowercase ,			_lowercase ,			_lowercase ,			**_lowercase   ).prev_sample
       SCREAMING_SNAKE_CASE__    							=   new_scheduler.step(_lowercase ,			_lowercase ,			_lowercase ,			**_lowercase   ).prev_sample

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




   def 			__a		(					self    :       Optional[int] ,			_lowercase    :       List[Any]=None ,			**_lowercase    :       Dict   ):



     """simple docstring"""






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

     SCREAMING_SNAKE_CASE__    							=   self.scheduler_classes[0]
     SCREAMING_SNAKE_CASE__    							=   self.get_scheduler_config(**_lowercase   )
     SCREAMING_SNAKE_CASE__    							=   scheduler_class(**_lowercase   )

     SCREAMING_SNAKE_CASE__    							=   10
     SCREAMING_SNAKE_CASE__    							=   self.dummy_model()
     SCREAMING_SNAKE_CASE__    							=   self.dummy_sample_deter
     scheduler.set_timesteps(_lowercase   )

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

     return sample




   def 			__a		(					self    :       str   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   DPMSolverSinglestepScheduler(**self.get_scheduler_config()   )
     SCREAMING_SNAKE_CASE__    							=   50
     SCREAMING_SNAKE_CASE__    							=   self.dummy_model()
     SCREAMING_SNAKE_CASE__    							=   self.dummy_sample_deter
     scheduler.set_timesteps(_lowercase   )

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

     SCREAMING_SNAKE_CASE__    							=   torch.mean(torch.abs(_lowercase   )   )

     assert abs(result_mean.item() - 0.25_74   ) < 1E-3




   def 			__a		(					self    :       List[str]   ):



     """simple docstring"""






     for timesteps in [25, 50, 1_00, 9_99, 10_00]:
       self.check_over_configs(num_train_timesteps=_lowercase   )




   def 			__a		(					self    :       Optional[Any]   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   DPMSolverSinglestepScheduler(**self.get_scheduler_config()   )
     SCREAMING_SNAKE_CASE__    							=   self.full_loop(scheduler=_lowercase   )
     SCREAMING_SNAKE_CASE__    							=   torch.mean(torch.abs(_lowercase   )   )

     assert abs(result_mean.item() - 0.27_91   ) < 1E-3

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

     SCREAMING_SNAKE_CASE__    							=   self.full_loop(scheduler=_lowercase   )
     SCREAMING_SNAKE_CASE__    							=   torch.mean(torch.abs(_lowercase   )   )

     assert abs(result_mean.item() - 0.27_91   ) < 1E-3




   def 			__a		(					self    :       Optional[Any]   ):



     """simple docstring"""






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




   def 			__a		(					self    :       Optional[Any]   ):



     """simple docstring"""






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




   def 			__a		(					self    :       Any   ):



     """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=_lowercase ,			solver_type=_lowercase ,			prediction_type=_lowercase ,			algorithm_type=_lowercase ,			)
             SCREAMING_SNAKE_CASE__    							=   self.full_loop(
                 solver_order=_lowercase ,			solver_type=_lowercase ,			prediction_type=_lowercase ,			algorithm_type=_lowercase ,			)
             assert not torch.isnan(_lowercase   ).any(), "Samples have nan numbers"




   def 			__a		(					self    :       Optional[int]   ):



     """simple docstring"""






     self.check_over_configs(lower_order_final=_lowercase   )
     self.check_over_configs(lower_order_final=_lowercase   )




   def 			__a		(					self    :       str   ):



     """simple docstring"""






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




   def 			__a		(					self    :       Optional[Any]   ):



     """simple docstring"""






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




   def 			__a		(					self    :       List[str]   ):



     """simple docstring"""






     for num_inference_steps in [1, 2, 3, 5, 10, 50, 1_00, 9_99, 10_00]:
       self.check_over_forward(num_inference_steps=_lowercase ,			time_step=0   )




   def 			__a		(					self    :       List[str]   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   self.full_loop()
     SCREAMING_SNAKE_CASE__    							=   torch.mean(torch.abs(_lowercase   )   )

     assert abs(result_mean.item() - 0.27_91   ) < 1E-3




   def 			__a		(					self    :       int   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   self.full_loop(use_karras_sigmas=_lowercase   )
     SCREAMING_SNAKE_CASE__    							=   torch.mean(torch.abs(_lowercase   )   )

     assert abs(result_mean.item() - 0.22_48   ) < 1E-3




   def 			__a		(					self    :       Any   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   self.full_loop(prediction_type="""v_prediction"""   )
     SCREAMING_SNAKE_CASE__    							=   torch.mean(torch.abs(_lowercase   )   )

     assert abs(result_mean.item() - 0.14_53   ) < 1E-3




   def 			__a		(					self    :       Any   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   self.full_loop(prediction_type="""v_prediction""" ,			use_karras_sigmas=_lowercase   )
     SCREAMING_SNAKE_CASE__    							=   torch.mean(torch.abs(_lowercase   )   )

     assert abs(result_mean.item() - 0.06_49   ) < 1E-3




   def 			__a		(					self    :       Dict   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   self.scheduler_classes[0]
     SCREAMING_SNAKE_CASE__    							=   self.get_scheduler_config(thresholding=_lowercase ,			dynamic_thresholding_ratio=0   )
     SCREAMING_SNAKE_CASE__    							=   scheduler_class(**_lowercase   )

     SCREAMING_SNAKE_CASE__    							=   10
     SCREAMING_SNAKE_CASE__    							=   self.dummy_model()
     SCREAMING_SNAKE_CASE__    							=   self.dummy_sample_deter.half()
     scheduler.set_timesteps(_lowercase   )

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

     assert sample.dtype == torch.floataa

from math import log

from scipy.constants import Boltzmann, physical_constants

__lowerCamelCase    :				int							     =	300  # TEMPERATURE (unit = K)



def 			__SCREAMING_SNAKE_CASE						(      __UpperCamelCase					: float				,  __UpperCamelCase					: float				,  __UpperCamelCase					: float				,  )							->      float:







  """simple docstring"""





  if donor_conc <= 0:
    raise ValueError("""Donor concentration should be positive"""			)
  elif acceptor_conc <= 0:
    raise ValueError("""Acceptor concentration should be positive"""			)
  elif intrinsic_conc <= 0:
    raise ValueError("""Intrinsic concentration should be positive"""			)
  elif donor_conc <= intrinsic_conc:
    raise ValueError(
        """Donor concentration should be greater than intrinsic concentration"""			)
  elif acceptor_conc <= intrinsic_conc:
    raise ValueError(
        """Acceptor concentration should be greater than intrinsic concentration"""			)
  else:
    return (
        Boltzmann
        * T
        * log((donor_conc * acceptor_conc) / intrinsic_conc**2			)
        / physical_constants["electron volt"][0]
    )


if __name__ == "__main__":
   import doctest

   doctest.testmod()
