




import tempfile

import torch

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

from .test_schedulers import SchedulerCommonTest
class 	_UpperCamelCase					( UpperCamelCase__						):
      UpperCAmelCase_						=						(DPMSolverSinglestepScheduler,)
      UpperCAmelCase_						=						(("""num_inference_steps""", 25),)
      def 		UpperCAmelCase_  (	self      :List[str]       ,					**lowerCamelCase      :Any					)   ->	Tuple:
       UpperCAmelCase__							       =  {
           "num_train_timesteps": 1000,
           "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(**__lowerCamelCase					)
       return config
      def 		UpperCAmelCase_  (	self      :List[Any]       ,					lowerCamelCase      :Optional[Any]=0       ,					**lowerCamelCase      :List[str]					)   ->	int:
       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.15, residual + 0.10]

       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 		UpperCAmelCase_  (	self      :str					)   ->	Tuple:
       pass
      def 		UpperCAmelCase_  (	self      :Tuple       ,					lowerCamelCase      :List[Any]=0       ,					**lowerCamelCase      :List[Any]					)   ->	Union[str, 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.15, residual + 0.10]

       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 		UpperCAmelCase_  (	self      :int       ,					lowerCamelCase      :Optional[int]=None       ,					**lowerCamelCase      :List[Any]					)   ->	Dict:
       if scheduler is None:
        UpperCAmelCase__							       =  self.scheduler_classes[0]
        UpperCAmelCase__							       =  self.get_scheduler_config(**__lowerCamelCase					)
        UpperCAmelCase__							       =  scheduler_class(**__lowerCamelCase					)

       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 		UpperCAmelCase_  (	self      :Union[str, Any]					)   ->	Dict:
       UpperCAmelCase__							       =  DPMSolverSinglestepScheduler(**self.get_scheduler_config()					)
       UpperCAmelCase__							       =  50
       UpperCAmelCase__							       =  self.dummy_model()
       UpperCAmelCase__							       =  self.dummy_sample_deter
       scheduler.set_timesteps(__lowerCamelCase					)

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

       UpperCAmelCase__							       =  torch.mean(torch.abs(__lowerCamelCase					)					)

       assert abs(result_mean.item() - 0.25_74					) < 1e-3
      def 		UpperCAmelCase_  (	self      :Dict					)   ->	Union[str, Any]:
       for timesteps in [25, 50, 100, 999, 1000]:
        self.check_over_configs(num_train_timesteps=__lowerCamelCase					)
      def 		UpperCAmelCase_  (	self      :List[str]					)   ->	Dict:
       # make sure that iterating over schedulers with same config names gives same results
       # for defaults
       UpperCAmelCase__							       =  DPMSolverSinglestepScheduler(**self.get_scheduler_config()					)
       UpperCAmelCase__							       =  self.full_loop(scheduler=__lowerCamelCase					)
       UpperCAmelCase__							       =  torch.mean(torch.abs(__lowerCamelCase					)					)

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

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

       UpperCAmelCase__							       =  self.full_loop(scheduler=__lowerCamelCase					)
       UpperCAmelCase__							       =  torch.mean(torch.abs(__lowerCamelCase					)					)

       assert abs(result_mean.item() - 0.27_91					) < 1e-3
      def 		UpperCAmelCase_  (	self      :Optional[int]					)   ->	List[str]:
       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 		UpperCAmelCase_  (	self      :List[str]					)   ->	Optional[int]:
       for prediction_type in ["epsilon", "v_prediction"]:
        self.check_over_configs(prediction_type=__lowerCamelCase					)
      def 		UpperCAmelCase_  (	self      :List[Any]					)   ->	Tuple:
       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=__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 		UpperCAmelCase_  (	self      :Optional[int]					)   ->	Optional[int]:
       self.check_over_configs(lower_order_final=__lowerCamelCase					)
       self.check_over_configs(lower_order_final=__lowerCamelCase					)
      def 		UpperCAmelCase_  (	self      :Tuple					)   ->	Dict:
       self.check_over_configs(lambda_min_clipped=-float("inf"					)					)
       self.check_over_configs(lambda_min_clipped=-5.1					)
      def 		UpperCAmelCase_  (	self      :Tuple					)   ->	List[Any]:
       self.check_over_configs(variance_type=__lowerCamelCase					)
       self.check_over_configs(variance_type="learned_range"					)
      def 		UpperCAmelCase_  (	self      :str					)   ->	str:
       for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]:
        self.check_over_forward(num_inference_steps=__lowerCamelCase       ,					time_step=0					)
      def 		UpperCAmelCase_  (	self      :Any					)   ->	str:
       UpperCAmelCase__							       =  self.full_loop()
       UpperCAmelCase__							       =  torch.mean(torch.abs(__lowerCamelCase					)					)

       assert abs(result_mean.item() - 0.27_91					) < 1e-3
      def 		UpperCAmelCase_  (	self      :List[Any]					)   ->	Union[str, Any]:
       UpperCAmelCase__							       =  self.full_loop(use_karras_sigmas=__lowerCamelCase					)
       UpperCAmelCase__							       =  torch.mean(torch.abs(__lowerCamelCase					)					)

       assert abs(result_mean.item() - 0.22_48					) < 1e-3
      def 		UpperCAmelCase_  (	self      :Union[str, Any]					)   ->	Tuple:
       UpperCAmelCase__							       =  self.full_loop(prediction_type="v_prediction"					)
       UpperCAmelCase__							       =  torch.mean(torch.abs(__lowerCamelCase					)					)

       assert abs(result_mean.item() - 0.14_53					) < 1e-3
      def 		UpperCAmelCase_  (	self      :Any					)   ->	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.06_49					) < 1e-3







      def 		UpperCAmelCase_  (	self      :Optional[Any]					)   ->	str:
       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












from __future__ import annotations

import math


def 	UpperCamelCase__(       UpperCamelCase__			:       int  )->bool:

       if 1 < number < 4:
              # 2 and 3 are primes
              return True
       elif number < 2 or number % 2 == 0 or number % 3 == 0:
              # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
              return False

       # All primes number are in format of 6k +/- 1
       for i in range(5 ,     int(math.sqrt(UpperCamelCase__  ) + 1  ) ,     6  ):
              if number % i == 0 or number % (i + 2) == 0:
                     return False
       return True


def 	UpperCamelCase__(       UpperCamelCase__			:       int  )->list[int]:
       A__											=						str(UpperCamelCase__  )
       A__											=						[n]
       for i in range(1 ,     len(UpperCamelCase__  )  ):
              list_nums.append(int(str_num[i:]  )  )
              list_nums.append(int(str_num[:-i]  )  )
       return list_nums


def 	UpperCamelCase__(       UpperCamelCase__			:       int  )->bool:
       if len(str(UpperCamelCase__  )  ) > 3:
              if not is_prime(int(str(UpperCamelCase__  )[-3:]  )  ) or not is_prime(int(str(UpperCamelCase__  )[:3]  )  ):
                     return False
       return True


def 	UpperCamelCase__(       UpperCamelCase__			:       int = 11  )->list[int]:
       A__											=						[]
       A__											=						13
       while len(UpperCamelCase__  ) != count:
              if validate(UpperCamelCase__  ):
                     A__											=						list_truncated_nums(UpperCamelCase__  )
                     if all(is_prime(UpperCamelCase__  ) for i in list_nums  ):
                            list_truncated_primes.append(UpperCamelCase__  )
              num += 2
       return list_truncated_primes


def 	UpperCamelCase__(       )->int:
       return sum(compute_truncated_primes(11  )  )


if __name__ == "__main__":
       print(F"{sum(compute_truncated_primes(11)) = }")





