





import tempfile

import torch

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

from .test_schedulers import SchedulerCommonTest



class       A__     ( UpperCamelCase_    ):
      _UpperCAmelCase			:List[str]							=     (DEISMultistepScheduler,)
      _UpperCAmelCase			:Any							=     (('num_inference_steps', 2_5),)





      def  __UpperCamelCase(				self ,							**A_     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[int] 			=     {
           'num_train_timesteps': 1000,
           'beta_start': 0.00_01,
           'beta_end': 0.02,
           'beta_schedule': 'linear',
           'solver_order': 2,
       }

       config.update(**__a     )
       return config





      def  __UpperCamelCase(				self ,							A_=0 ,							**A_     ):
       '''simple docstring'''




       UpperCamelCase			:      Union[str, Any] 			=     dict(self.forward_default_kwargs     )
       UpperCamelCase			:      Optional[int] 			=     kwargs.pop("num_inference_steps" ,							__a     )
       UpperCamelCase			:      List[Any] 			=     self.dummy_sample
       UpperCamelCase			:      Optional[int] 			=     0.1 * sample
       UpperCamelCase			:      Tuple 			=     [residual + 0.2, residual + 0.15, residual + 0.10]

       for scheduler_class in self.scheduler_classes:
        UpperCamelCase			:      Optional[int] 			=     self.get_scheduler_config(**__a     )
        UpperCamelCase			:      int 			=     scheduler_class(**__a     )
        scheduler.set_timesteps(__a     )
        # copy over dummy past residuals
        UpperCamelCase			:      Optional[Any] 			=     dummy_past_residuals[: scheduler.config.solver_order]

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

        UpperCamelCase			:      int 			=     sample, sample
        for t in range(__a ,							time_step + scheduler.config.solver_order + 1     ):
         UpperCamelCase			:      Tuple 			=     scheduler.step(__a ,							__a ,							__a ,							**__a     ).prev_sample
         UpperCamelCase			:      List[str] 			=     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  __UpperCamelCase(				self     ):
       '''simple docstring'''




       pass





      def  __UpperCamelCase(				self ,							A_=0 ,							**A_     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[Any] 			=     dict(self.forward_default_kwargs     )
       UpperCamelCase			:      str 			=     kwargs.pop("num_inference_steps" ,							__a     )
       UpperCamelCase			:      Any 			=     self.dummy_sample
       UpperCamelCase			:      List[Any] 			=     0.1 * sample
       UpperCamelCase			:      Optional[Any] 			=     [residual + 0.2, residual + 0.15, residual + 0.10]

       for scheduler_class in self.scheduler_classes:
        UpperCamelCase			:      Tuple 			=     self.get_scheduler_config()
        UpperCamelCase			:      Any 			=     scheduler_class(**__a     )
        scheduler.set_timesteps(__a     )

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

        with tempfile.TemporaryDirectory() as tmpdirname:
         scheduler.save_config(__a     )
         UpperCamelCase			:      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)
         UpperCamelCase			:      Tuple 			=     dummy_past_residuals[: new_scheduler.config.solver_order]

        UpperCamelCase			:      Dict 			=     scheduler.step(__a ,							__a ,							__a ,							**__a     ).prev_sample
        UpperCamelCase			:      Tuple 			=     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  __UpperCamelCase(				self ,							A_=None ,							**A_     ):
       '''simple docstring'''




       if scheduler is None:
        UpperCamelCase			:      Dict 			=     self.scheduler_classes[0]
        UpperCamelCase			:      Tuple 			=     self.get_scheduler_config(**__a     )
        UpperCamelCase			:      Optional[int] 			=     scheduler_class(**__a     )

       UpperCamelCase			:      Optional[Any] 			=     self.scheduler_classes[0]
       UpperCamelCase			:      Optional[Any] 			=     self.get_scheduler_config(**__a     )
       UpperCamelCase			:      int 			=     scheduler_class(**__a     )

       UpperCamelCase			:      Any 			=     10
       UpperCamelCase			:      Optional[Any] 			=     self.dummy_model()
       UpperCamelCase			:      str 			=     self.dummy_sample_deter
       scheduler.set_timesteps(__a     )

       for i, t in enumerate(scheduler.timesteps     ):
        UpperCamelCase			:      Tuple 			=     model(__a ,							__a     )
        UpperCamelCase			:      Optional[Any] 			=     scheduler.step(__a ,							__a ,							__a     ).prev_sample

       return sample





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      List[Any] 			=     dict(self.forward_default_kwargs     )

       UpperCamelCase			:      str 			=     kwargs.pop("num_inference_steps" ,							__a     )

       for scheduler_class in self.scheduler_classes:
        UpperCamelCase			:      Any 			=     self.get_scheduler_config()
        UpperCamelCase			:      Optional[int] 			=     scheduler_class(**__a     )

        UpperCamelCase			:      Dict 			=     self.dummy_sample
        UpperCamelCase			:      Dict 			=     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"     ):
         UpperCamelCase			:      str 			=     num_inference_steps

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

        UpperCamelCase			:      Union[str, Any] 			=     scheduler.timesteps[5]
        UpperCamelCase			:      List[str] 			=     scheduler.timesteps[6]

        UpperCamelCase			:      Optional[Any] 			=     scheduler.step(__a ,							__a ,							__a ,							**__a     ).prev_sample
        UpperCamelCase			:      List[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  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[int] 			=     DEISMultistepScheduler(**self.get_scheduler_config()     )
       UpperCamelCase			:      Dict 			=     self.full_loop(scheduler=__a     )
       UpperCamelCase			:      Any 			=     torch.mean(torch.abs(__a     )     )

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

       UpperCamelCase			:      Dict 			=     DPMSolverSinglestepScheduler.from_config(scheduler.config     )
       UpperCamelCase			:      Union[str, Any] 			=     DPMSolverMultistepScheduler.from_config(scheduler.config     )
       UpperCamelCase			:      Tuple 			=     UniPCMultistepScheduler.from_config(scheduler.config     )
       UpperCamelCase			:      Dict 			=     DEISMultistepScheduler.from_config(scheduler.config     )

       UpperCamelCase			:      Dict 			=     self.full_loop(scheduler=__a     )
       UpperCamelCase			:      List[str] 			=     torch.mean(torch.abs(__a     )     )

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





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       for timesteps in [25, 50, 100, 999, 1000]:
        self.check_over_configs(num_train_timesteps=__a     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       self.check_over_configs(thresholding=__a     )
       for order in [1, 2, 3]:
        for solver_type in ["logrho"]:
         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 ,							algorithm_type="deis" ,							solver_order=__a ,							solver_type=__a ,							)





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




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





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       for algorithm_type in ["deis"]:
        for solver_type in ["logrho"]:
         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 ,							algorithm_type=__a ,							)
           UpperCamelCase			:      Dict 			=     self.full_loop(
               solver_order=__a ,							solver_type=__a ,							prediction_type=__a ,							algorithm_type=__a ,							)
           assert not torch.isnan(__a     ).any(), "Samples have nan numbers"





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       self.check_over_configs(lower_order_final=__a     )
       self.check_over_configs(lower_order_final=__a     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]:
        self.check_over_forward(num_inference_steps=__a ,							time_step=0     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      List[Any] 			=     self.full_loop()
       UpperCamelCase			:      Optional[Any] 			=     torch.mean(torch.abs(__a     )     )

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





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      List[str] 			=     self.full_loop(prediction_type="v_prediction"     )
       UpperCamelCase			:      Tuple 			=     torch.mean(torch.abs(__a     )     )

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





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      List[str] 			=     self.scheduler_classes[0]
       UpperCamelCase			:      int 			=     self.get_scheduler_config(thresholding=__a ,							dynamic_thresholding_ratio=0     )
       UpperCamelCase			:      Union[str, Any] 			=     scheduler_class(**__a     )

       UpperCamelCase			:      List[str] 			=     10
       UpperCamelCase			:      Dict 			=     self.dummy_model()
       UpperCamelCase			:      str 			=     self.dummy_sample_deter.half()
       scheduler.set_timesteps(__a     )

       for i, t in enumerate(scheduler.timesteps     ):
        UpperCamelCase			:      List[Any] 			=     model(__a ,							__a     )
        UpperCamelCase			:      Dict 			=     scheduler.step(__a ,							__a ,							__a     ).prev_sample

       assert sample.dtype == torch.floataa







from __future__ import annotations



def 	A_       (  _lowerCAmelCase  )		->				list[int]:
 UpperCamelCase			:      Optional[Any] 			=     [True] * limit
 UpperCamelCase			:      Optional[Any] 			=     False
 UpperCamelCase			:      List[str] 			=     False
 UpperCamelCase			:      Tuple 			=     True

 for i in range(3							,     int(limit**0.5 + 1  )							,     2  ):
  UpperCamelCase			:      Optional[Any] 			=     i * 2
  while index < limit:
   UpperCamelCase			:      int 			=     False
   UpperCamelCase			:      Optional[int] 			=     index + i

 UpperCamelCase			:      Any 			=     [2]

 for i in range(3							,     _lowerCAmelCase							,     2  ):
  if is_prime[i]:
   primes.append(_lowerCAmelCase  )

 return primes



def 	A_       (  _lowerCAmelCase = 100_0000  )		->				int:
 UpperCamelCase			:      Union[str, Any] 			=     prime_sieve(_lowerCAmelCase  )
 UpperCamelCase			:      List[str] 			=     0
 UpperCamelCase			:      Union[str, Any] 			=     0

 for i in range(len(_lowerCAmelCase  )  ):
  for j in range(i + length							,     len(_lowerCAmelCase  )  ):
   UpperCamelCase			:      Dict 			=     sum(primes[i:j]  )
   if sol >= ceiling:
    break

   if sol in primes:
    UpperCamelCase			:      int 			=     j - i
    UpperCamelCase			:      Dict 			=     sol

 return largest


if __name__ == "__main__":
    print(f"""{solution() = }""")
