code: 
  
  
  
  
  
  import tempfile
  
  import torch
  
  from diffusers import (
      DEISMultistepScheduler,
      DPMSolverMultistepScheduler,
      DPMSolverSinglestepScheduler,
      UniPCMultistepScheduler,
  )
  
  from .test_schedulers import SchedulerCommonTest
  
  
  
  
  class   _UpperCAmelCase	(   _lowercase					):
       __A          =   (DPMSolverMultistepScheduler,)
       __A          =   (("num_inference_steps", 25),)
  
  
  
  
       def 	snake_case_		(						self    ,      **__lowerCAmelCase       )						->  int:
         UpperCamelCase__         =	{
             """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""",
             """lower_order_final""": False,
             """lambda_min_clipped""": -float("""inf"""       ),
             """variance_type""": None,
         }
  
         config.update(**__lowerCAmelCase       )
         return config
  
  
  
  
       def 	snake_case_		(						self    ,      __lowerCAmelCase=0    ,      **__lowerCAmelCase       )						->  Tuple:
         UpperCamelCase__         =	dict(self.forward_default_kwargs       )
         UpperCamelCase__         =	kwargs.pop("""num_inference_steps"""    ,      __lowerCAmelCase       )
         UpperCamelCase__         =	self.dummy_sample
         UpperCamelCase__         =	0.1 * sample
         UpperCamelCase__         =	[residual + 0.2, residual + 0.1_5, residual + 0.1_0]
  
         for scheduler_class in self.scheduler_classes:
           UpperCamelCase__         =	self.get_scheduler_config(**__lowerCAmelCase       )
           UpperCamelCase__         =	scheduler_class(**__lowerCAmelCase       )
           scheduler.set_timesteps(__lowerCAmelCase       )
           # copy over dummy past residuals
           UpperCamelCase__         =	dummy_past_residuals[: scheduler.config.solver_order]
  
           with tempfile.TemporaryDirectory() as tmpdirname:
             scheduler.save_config(__lowerCAmelCase       )
             UpperCamelCase__         =	scheduler_class.from_pretrained(__lowerCAmelCase       )
             new_scheduler.set_timesteps(__lowerCAmelCase       )
             # copy over dummy past residuals
             UpperCamelCase__         =	dummy_past_residuals[: new_scheduler.config.solver_order]
  
           UpperCamelCase__  ,			UpperCamelCase__         =	sample, sample
           for t in range(__lowerCAmelCase    ,      time_step + scheduler.config.solver_order + 1       ):
             UpperCamelCase__         =	scheduler.step(__lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      **__lowerCAmelCase       ).prev_sample
             UpperCamelCase__         =	new_scheduler.step(__lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      **__lowerCAmelCase       ).prev_sample
  
             assert torch.sum(torch.abs(output - new_output       )       ) < 1E-5, "Scheduler outputs are not identical"
  
  
  
  
       def 	snake_case_		(						self       )						->  List[Any]:
         pass
  
  
  
  
       def 	snake_case_		(						self    ,      __lowerCAmelCase=0    ,      **__lowerCAmelCase       )						->  Any:
         UpperCamelCase__         =	dict(self.forward_default_kwargs       )
         UpperCamelCase__         =	kwargs.pop("""num_inference_steps"""    ,      __lowerCAmelCase       )
         UpperCamelCase__         =	self.dummy_sample
         UpperCamelCase__         =	0.1 * sample
         UpperCamelCase__         =	[residual + 0.2, residual + 0.1_5, residual + 0.1_0]
  
         for scheduler_class in self.scheduler_classes:
           UpperCamelCase__         =	self.get_scheduler_config()
           UpperCamelCase__         =	scheduler_class(**__lowerCAmelCase       )
           scheduler.set_timesteps(__lowerCAmelCase       )
  
           # copy over dummy past residuals (must be after setting timesteps)
           UpperCamelCase__         =	dummy_past_residuals[: scheduler.config.solver_order]
  
           with tempfile.TemporaryDirectory() as tmpdirname:
             scheduler.save_config(__lowerCAmelCase       )
             UpperCamelCase__         =	scheduler_class.from_pretrained(__lowerCAmelCase       )
             # copy over dummy past residuals
             new_scheduler.set_timesteps(__lowerCAmelCase       )
  
             # copy over dummy past residual (must be after setting timesteps)
             UpperCamelCase__         =	dummy_past_residuals[: new_scheduler.config.solver_order]
  
           UpperCamelCase__         =	scheduler.step(__lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      **__lowerCAmelCase       ).prev_sample
           UpperCamelCase__         =	new_scheduler.step(__lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      **__lowerCAmelCase       ).prev_sample
  
           assert torch.sum(torch.abs(output - new_output       )       ) < 1E-5, "Scheduler outputs are not identical"
  
  
  
  
       def 	snake_case_		(						self    ,      __lowerCAmelCase=None    ,      **__lowerCAmelCase       )						->  Union[str, Any]:
         if scheduler is None:
           UpperCamelCase__         =	self.scheduler_classes[0]
           UpperCamelCase__         =	self.get_scheduler_config(**__lowerCAmelCase       )
           UpperCamelCase__         =	scheduler_class(**__lowerCAmelCase       )
  
         UpperCamelCase__         =	10
         UpperCamelCase__         =	self.dummy_model()
         UpperCamelCase__         =	self.dummy_sample_deter
         scheduler.set_timesteps(__lowerCAmelCase       )
  
         for i, t in enumerate(scheduler.timesteps       ):
           UpperCamelCase__         =	model(__lowerCAmelCase    ,      __lowerCAmelCase       )
           UpperCamelCase__         =	scheduler.step(__lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase       ).prev_sample
  
         return sample
  
  
  
  
       def 	snake_case_		(						self       )						->  Tuple:
         UpperCamelCase__         =	dict(self.forward_default_kwargs       )
  
         UpperCamelCase__         =	kwargs.pop("""num_inference_steps"""    ,      __lowerCAmelCase       )
  
         for scheduler_class in self.scheduler_classes:
           UpperCamelCase__         =	self.get_scheduler_config()
           UpperCamelCase__         =	scheduler_class(**__lowerCAmelCase       )
  
           UpperCamelCase__         =	self.dummy_sample
           UpperCamelCase__         =	0.1 * sample
  
           if num_inference_steps is not None and hasattr(__lowerCAmelCase    ,      """set_timesteps"""       ):
             scheduler.set_timesteps(__lowerCAmelCase       )
           elif num_inference_steps is not None and not hasattr(__lowerCAmelCase    ,      """set_timesteps"""       ):
             UpperCamelCase__         =	num_inference_steps
  
           # copy over dummy past residuals (must be done after set_timesteps)
           UpperCamelCase__         =	[residual + 0.2, residual + 0.1_5, residual + 0.1_0]
           UpperCamelCase__         =	dummy_past_residuals[: scheduler.config.solver_order]
  
           UpperCamelCase__         =	scheduler.timesteps[5]
           UpperCamelCase__         =	scheduler.timesteps[6]
  
           UpperCamelCase__         =	scheduler.step(__lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      **__lowerCAmelCase       ).prev_sample
           UpperCamelCase__         =	scheduler.step(__lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      **__lowerCAmelCase       ).prev_sample
  
           self.assertEqual(output_a.shape    ,      sample.shape       )
           self.assertEqual(output_a.shape    ,      output_a.shape       )
  
  
  
  
       def 	snake_case_		(						self       )						->  Optional[int]:
         for timesteps in [25, 50, 100, 999, 1_000]:
           self.check_over_configs(num_train_timesteps=__lowerCAmelCase       )
  
  
  
  
       def 	snake_case_		(						self       )						->  int:
         self.check_over_configs(thresholding=__lowerCAmelCase       )
         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=__lowerCAmelCase    ,      prediction_type=__lowerCAmelCase    ,      sample_max_value=__lowerCAmelCase    ,      algorithm_type="""dpmsolver++"""    ,      solver_order=__lowerCAmelCase    ,      solver_type=__lowerCAmelCase    ,      )
  
  
  
  
       def 	snake_case_		(						self       )						->  List[str]:
         for prediction_type in ["epsilon", "v_prediction"]:
           self.check_over_configs(prediction_type=__lowerCAmelCase       )
  
  
  
  
       def 	snake_case_		(						self       )						->  int:
         for algorithm_type in ["dpmsolver", "dpmsolver++", "sde-dpmsolver", "sde-dpmsolver++"]:
           for solver_type in ["midpoint", "heun"]:
             for order in [1, 2, 3]:
               for prediction_type in ["epsilon", "sample"]:
                 if algorithm_type in ["sde-dpmsolver", "sde-dpmsolver++"]:
                   if order == 3:
                     continue
                 else:
                   self.check_over_configs(
                       solver_order=__lowerCAmelCase    ,      solver_type=__lowerCAmelCase    ,      prediction_type=__lowerCAmelCase    ,      algorithm_type=__lowerCAmelCase    ,      )
                 UpperCamelCase__         =	self.full_loop(
                     solver_order=__lowerCAmelCase    ,      solver_type=__lowerCAmelCase    ,      prediction_type=__lowerCAmelCase    ,      algorithm_type=__lowerCAmelCase    ,      )
                 assert not torch.isnan(__lowerCAmelCase       ).any(), "Samples have nan numbers"
  
  
  
  
       def 	snake_case_		(						self       )						->  Dict:
         self.check_over_configs(lower_order_final=__lowerCAmelCase       )
         self.check_over_configs(lower_order_final=__lowerCAmelCase       )
  
  
  
  
       def 	snake_case_		(						self       )						->  Dict:
         self.check_over_configs(lambda_min_clipped=-float("""inf"""       )       )
         self.check_over_configs(lambda_min_clipped=-5.1       )
  
  
  
  
       def 	snake_case_		(						self       )						->  Tuple:
         self.check_over_configs(variance_type=__lowerCAmelCase       )
         self.check_over_configs(variance_type="""learned_range"""       )
  
  
  
  
       def 	snake_case_		(						self       )						->  int:
         for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1_000]:
           self.check_over_forward(num_inference_steps=__lowerCAmelCase    ,      time_step=0       )
  
  
  
  
       def 	snake_case_		(						self       )						->  Optional[Any]:
         UpperCamelCase__         =	self.full_loop()
         UpperCamelCase__         =	torch.mean(torch.abs(__lowerCAmelCase       )       )
  
         assert abs(result_mean.item() - 0.3_3_0_1       ) < 1E-3
  
  
  
  
       def 	snake_case_		(						self       )						->  Optional[Any]:
         UpperCamelCase__         =	self.full_loop(thresholding=__lowerCAmelCase    ,      dynamic_thresholding_ratio=0.8_7    ,      sample_max_value=0.5       )
         UpperCamelCase__         =	torch.mean(torch.abs(__lowerCAmelCase       )       )
  
         assert abs(result_mean.item() - 1.1_3_6_4       ) < 1E-3
  
  
  
  
       def 	snake_case_		(						self       )						->  str:
         UpperCamelCase__         =	self.full_loop(prediction_type="""v_prediction"""       )
         UpperCamelCase__         =	torch.mean(torch.abs(__lowerCAmelCase       )       )
  
         assert abs(result_mean.item() - 0.2_2_5_1       ) < 1E-3
  
  
  
  
       def 	snake_case_		(						self       )						->  Dict:
         UpperCamelCase__         =	self.full_loop(prediction_type="""v_prediction"""    ,      use_karras_sigmas=__lowerCAmelCase       )
         UpperCamelCase__         =	torch.mean(torch.abs(__lowerCAmelCase       )       )
  
         assert abs(result_mean.item() - 0.2_0_9_6       ) < 1E-3
  
  
  
  
       def 	snake_case_		(						self       )						->  Tuple:
         # make sure that iterating over schedulers with same config names gives same results
         # for defaults
         UpperCamelCase__         =	DPMSolverMultistepScheduler(**self.get_scheduler_config()       )
         UpperCamelCase__         =	self.full_loop(scheduler=__lowerCAmelCase       )
         UpperCamelCase__         =	torch.mean(torch.abs(__lowerCAmelCase       )       )
  
         assert abs(result_mean.item() - 0.3_3_0_1       ) < 1E-3
  
         UpperCamelCase__         =	DPMSolverSinglestepScheduler.from_config(scheduler.config       )
         UpperCamelCase__         =	UniPCMultistepScheduler.from_config(scheduler.config       )
         UpperCamelCase__         =	DEISMultistepScheduler.from_config(scheduler.config       )
         UpperCamelCase__         =	DPMSolverMultistepScheduler.from_config(scheduler.config       )
  
         UpperCamelCase__         =	self.full_loop(scheduler=__lowerCAmelCase       )
         UpperCamelCase__         =	torch.mean(torch.abs(__lowerCAmelCase       )       )
  
         assert abs(result_mean.item() - 0.3_3_0_1       ) < 1E-3
  
  
  
  
       def 	snake_case_		(						self       )						->  List[Any]:
         UpperCamelCase__         =	self.scheduler_classes[0]
         UpperCamelCase__         =	self.get_scheduler_config(thresholding=__lowerCAmelCase    ,      dynamic_thresholding_ratio=0       )
         UpperCamelCase__         =	scheduler_class(**__lowerCAmelCase       )
  
         UpperCamelCase__         =	10
         UpperCamelCase__         =	self.dummy_model()
         UpperCamelCase__         =	self.dummy_sample_deter.half()
         scheduler.set_timesteps(__lowerCAmelCase       )
  
         for i, t in enumerate(scheduler.timesteps       ):
           UpperCamelCase__         =	model(__lowerCAmelCase    ,      __lowerCAmelCase       )
           UpperCamelCase__         =	scheduler.step(__lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase       ).prev_sample
  
         assert sample.dtype == torch.floataa
  
  
  
  
       def 	snake_case_		(						self    ,      **__lowerCAmelCase       )						->  Any:
         for scheduler_class in self.scheduler_classes:
           UpperCamelCase__         =	self.get_scheduler_config(**__lowerCAmelCase       )
           UpperCamelCase__         =	scheduler_class(**__lowerCAmelCase       )
  
           scheduler.set_timesteps(scheduler.config.num_train_timesteps       )
           assert len(scheduler.timesteps.unique()       ) == scheduler.num_inference_steps
  
  
  
  
  
  
style_context: 
  
  
  
  
  
  __lowerCAmelCase     =			{
      "0": 1,
      "1": 1,
      "2": 2,
      "3": 6,
      "4": 24,
      "5": 120,
      "6": 720,
      "7": 5_040,
      "8": 40_320,
      "9": 362_880,
  }
  
  __lowerCAmelCase     =			{145: 145}
  
  __lowerCAmelCase     =			{
      145: 0,
      169: 3,
      36_301: 3,
      1_454: 3,
      871: 2,
      45_361: 2,
      872: 2,
  }
  
  def     a						(      _lowercase   :     int       )     ->int:
  		if n in CACHE_SUM_DIGIT_FACTORIALS:
  				return CACHE_SUM_DIGIT_FACTORIALS[n]
  		UpperCamelCase__         =	sum(DIGIT_FACTORIALS[let] for let in str(_lowercase       )       )
  		UpperCamelCase__         =	ret
  		return ret
  
  def     a						(      _lowercase   :     int ,						_lowercase   :     set | None = None       )     ->int:
  		UpperCamelCase__         =	previous or set()
  		if n in CHAIN_LENGTH_CACHE:
  				return CHAIN_LENGTH_CACHE[n]
  		UpperCamelCase__         =	sum_digit_factorials(_lowercase       )
  		if next_number in previous:
  				UpperCamelCase__         =	0
  				return 0
  		else:
  				previous.add(_lowercase       )
  				UpperCamelCase__         =	1 + chain_length(_lowercase ,						_lowercase       )
  				UpperCamelCase__         =	ret
  				return ret
  
  def     a						(      _lowercase   :     int = 6_0 ,						_lowercase   :     int = 1_0_0_0_0_0_0       )     ->int:
  		return sum(1 for i in range(1 ,						_lowercase       ) if chain_length(_lowercase       ) == num_terms       )
  
  
  if __name__ == "__main__":
  				print(f"""{solution() = }""")
  
  
  
  
  
  