import tempfile

import torch

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

from .test_schedulers import SchedulerCommonTest





class 							lowercase						(						__lowerCamelCase ):
   lowerCamelCase_    					=(DEISMultistepScheduler,)
   lowerCamelCase_    					=(('num_inference_steps', 2_5),)


   def __UpperCAmelCase       (   self				:	Optional[Any]   ,		**__lowerCAmelCase				:	Optional[Any])      ->					str:
         lowercase_     =      {
             "num_train_timesteps": 1000,
             "beta_start": 0.0001,
             "beta_end": 0.02,
             "beta_schedule": "linear",
             "solver_order": 2,
         }

         config.update(**__lowerCAmelCase)
         return config


   def __UpperCAmelCase       (   self				:	Optional[Any]   ,		__lowerCAmelCase				:	str=0   ,		**__lowerCAmelCase				:	Optional[int])      ->					Optional[Any]:
         lowercase_     =      dict(self.forward_default_kwargs)
         lowercase_     =      kwargs.pop("num_inference_steps"   ,		__lowerCAmelCase)
         lowercase_     =      self.dummy_sample
         lowercase_     =      0.1 * sample
         lowercase_     =      [residual + 0.2, residual + 0.15, residual + 0.10]

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

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

               lowercase_ ,							lowercase_     =      sample, sample
               for t in range(__lowerCAmelCase   ,		time_step + scheduler.config.solver_order + 1):
                     lowercase_     =      scheduler.step(__lowerCAmelCase   ,		__lowerCAmelCase   ,		__lowerCAmelCase   ,		**__lowerCAmelCase).prev_sample
                     lowercase_     =      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)      ->					int:
         pass


   def __UpperCAmelCase       (   self				:	Tuple   ,		__lowerCAmelCase				:	Dict=0   ,		**__lowerCAmelCase				:	Tuple)      ->					Dict:
         lowercase_     =      dict(self.forward_default_kwargs)
         lowercase_     =      kwargs.pop("num_inference_steps"   ,		__lowerCAmelCase)
         lowercase_     =      self.dummy_sample
         lowercase_     =      0.1 * sample
         lowercase_     =      [residual + 0.2, residual + 0.15, residual + 0.10]

         for scheduler_class in self.scheduler_classes:
               lowercase_     =      self.get_scheduler_config()
               lowercase_     =      scheduler_class(**__lowerCAmelCase)
               scheduler.set_timesteps(__lowerCAmelCase)

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

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

               lowercase_     =      scheduler.step(__lowerCAmelCase   ,		__lowerCAmelCase   ,		__lowerCAmelCase   ,		**__lowerCAmelCase).prev_sample
               lowercase_     =      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				:	Dict   ,		__lowerCAmelCase				:	Tuple=None   ,		**__lowerCAmelCase				:	int)      ->					Optional[Any]:
         if scheduler is None:
               lowercase_     =      self.scheduler_classes[0]
               lowercase_     =      self.get_scheduler_config(**__lowerCAmelCase)
               lowercase_     =      scheduler_class(**__lowerCAmelCase)

         lowercase_     =      self.scheduler_classes[0]
         lowercase_     =      self.get_scheduler_config(**__lowerCAmelCase)
         lowercase_     =      scheduler_class(**__lowerCAmelCase)

         lowercase_     =      10
         lowercase_     =      self.dummy_model()
         lowercase_     =      self.dummy_sample_deter
         scheduler.set_timesteps(__lowerCAmelCase)

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

         return sample


   def __UpperCAmelCase       (   self				:	str)      ->					List[str]:
         lowercase_     =      dict(self.forward_default_kwargs)

         lowercase_     =      kwargs.pop("num_inference_steps"   ,		__lowerCAmelCase)

         for scheduler_class in self.scheduler_classes:
               lowercase_     =      self.get_scheduler_config()
               lowercase_     =      scheduler_class(**__lowerCAmelCase)

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

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

               lowercase_     =      scheduler.timesteps[5]
               lowercase_     =      scheduler.timesteps[6]

               lowercase_     =      scheduler.step(__lowerCAmelCase   ,		__lowerCAmelCase   ,		__lowerCAmelCase   ,		**__lowerCAmelCase).prev_sample
               lowercase_     =      scheduler.step(__lowerCAmelCase   ,		__lowerCAmelCase   ,		__lowerCAmelCase   ,		**__lowerCAmelCase).prev_sample

               self.assertEqual(output_a.shape   ,		sample.shape)
               self.assertEqual(output_a.shape   ,		output_a.shape)


   def __UpperCAmelCase       (   self				:	Dict)      ->					int:
         # make sure that iterating over schedulers with same config names gives same results
         # for defaults
         lowercase_     =      DEISMultistepScheduler(**self.get_scheduler_config())
         lowercase_     =      self.full_loop(scheduler=__lowerCAmelCase)
         lowercase_     =      torch.mean(torch.abs(__lowerCAmelCase))

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

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

         lowercase_     =      self.full_loop(scheduler=__lowerCAmelCase)
         lowercase_     =      torch.mean(torch.abs(__lowerCAmelCase))

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


   def __UpperCAmelCase       (   self				:	Tuple)      ->					str:
         for timesteps in [25, 50, 100, 999, 1000]:
               self.check_over_configs(num_train_timesteps=__lowerCAmelCase)


   def __UpperCAmelCase       (   self				:	Optional[Any])      ->					Union[str, Any]:
         self.check_over_configs(thresholding=__lowerCAmelCase)
         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=__lowerCAmelCase   ,		prediction_type=__lowerCAmelCase   ,		sample_max_value=__lowerCAmelCase   ,		algorithm_type="deis"   ,		solver_order=__lowerCAmelCase   ,		solver_type=__lowerCAmelCase   ,		)


   def __UpperCAmelCase       (   self				:	Dict)      ->					Any:
         for prediction_type in ["epsilon", "v_prediction"]:
               self.check_over_configs(prediction_type=__lowerCAmelCase)


   def __UpperCAmelCase       (   self				:	List[Any])      ->					List[Any]:
         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=__lowerCAmelCase   ,		solver_type=__lowerCAmelCase   ,		prediction_type=__lowerCAmelCase   ,		algorithm_type=__lowerCAmelCase   ,		)
                                 lowercase_     =      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				:	Any)      ->					Tuple:
         self.check_over_configs(lower_order_final=__lowerCAmelCase)
         self.check_over_configs(lower_order_final=__lowerCAmelCase)


   def __UpperCAmelCase       (   self				:	List[Any])      ->					Optional[Any]:
         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				:	str)      ->					Any:
         lowercase_     =      self.full_loop()
         lowercase_     =      torch.mean(torch.abs(__lowerCAmelCase))

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


   def __UpperCAmelCase       (   self				:	Dict)      ->					Optional[Any]:
         lowercase_     =      self.full_loop(prediction_type="v_prediction")
         lowercase_     =      torch.mean(torch.abs(__lowerCAmelCase))

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


   def __UpperCAmelCase       (   self				:	Tuple)      ->					Dict:
         lowercase_     =      self.scheduler_classes[0]
         lowercase_     =      self.get_scheduler_config(thresholding=__lowerCAmelCase   ,		dynamic_thresholding_ratio=0)
         lowercase_     =      scheduler_class(**__lowerCAmelCase)

         lowercase_     =      10
         lowercase_     =      self.dummy_model()
         lowercase_     =      self.dummy_sample_deter.half()
         scheduler.set_timesteps(__lowerCAmelCase)

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

         assert sample.dtype == torch.floataa







'''simple docstring'''

from __future__ import annotations


def __a (	__lowerCamelCase			:						int | str				)	->							bool:
      '''simple docstring'''


      lowercase_     =      str(__lowerCamelCase				)
      return n == n[::-1]


def __a (	__lowerCamelCase			:						int = 1_000_000				)	->							Optional[int]:
      '''simple docstring'''


      lowercase_     =      0

      for i in range(1 ,		__lowerCamelCase				):
            if is_palindrome(__lowerCamelCase				) and is_palindrome(bin(__lowerCamelCase				).split("b"				)[1]				):
                  total += i
      return total


if __name__ == "__main__":
       print(solution(int(str(input().strip()))))






