"""simple docstring"""



def      _lowerCamelCase(     a							):






       if not isinstance(__a					,  __a							):
              raise ValueError("Input must be an integer"							)
       if input_num <= 0:
              raise ValueError("Input must be positive"							)
       return sum(
           divisor for divisor in range(1					,  input_num // 2 + 1							) if input_num % divisor == 0							)


if __name__ == "__main__":
   import doctest

   doctest.testmod()








"""simple docstring"""






import tempfile

import torch

from diffusers import PNDMScheduler

from .test_schedulers import SchedulerCommonTest




class 							lowerCAmelCase__				(			UpperCAmelCase__ ):






      '''simple docstring'''

      __UpperCamelCase	 =  (PNDMScheduler,)
      __UpperCamelCase	 =  (("num_inference_steps", 5_0),)
      def _SCREAMING_SNAKE_CASE (						self     :  Any						,	**lowercase_     :  Optional[Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Any    						=		{
              '''num_train_timesteps''': 1000,
              '''beta_start''': 0.00_01,
              '''beta_end''': 0.02,
              '''beta_schedule''': '''linear''',
          }

          config.update(**lowercase_)
          return config
      def _SCREAMING_SNAKE_CASE (						self     :  Optional[Any]						,	lowercase_     :  List[str]=0						,	**lowercase_     :  Union[str, Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  List[Any]    						=		dict(self.forward_default_kwargs)
          SCREAMING_SNAKE_CASE_      :  List[str]    						=		kwargs.pop('''num_inference_steps'''						,	lowercase_)
          SCREAMING_SNAKE_CASE_      :  Tuple    						=		self.dummy_sample
          SCREAMING_SNAKE_CASE_      :  List[Any]    						=		0.1 * sample
          SCREAMING_SNAKE_CASE_      :  Dict    						=		[residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]

          for scheduler_class in self.scheduler_classes:
              SCREAMING_SNAKE_CASE_      :  Tuple    						=		self.get_scheduler_config(**lowercase_)
              SCREAMING_SNAKE_CASE_      :  Optional[Any]    						=		scheduler_class(**lowercase_)
              scheduler.set_timesteps(lowercase_)
              # copy over dummy past residuals
              SCREAMING_SNAKE_CASE_      :  Dict    						=		dummy_past_residuals[:]

              with tempfile.TemporaryDirectory() as tmpdirname:
                  scheduler.save_config(lowercase_)
                  SCREAMING_SNAKE_CASE_      :  Union[str, Any]    						=		scheduler_class.from_pretrained(lowercase_)
                  new_scheduler.set_timesteps(lowercase_)
                  # copy over dummy past residuals
                  SCREAMING_SNAKE_CASE_      :  Optional[Any]    						=		dummy_past_residuals[:]

              SCREAMING_SNAKE_CASE_      :  Optional[Any]    						=		scheduler.step_prk(lowercase_						,	lowercase_						,	lowercase_						,	**lowercase_).prev_sample
              SCREAMING_SNAKE_CASE_      :  Optional[Any]    						=		new_scheduler.step_prk(lowercase_						,	lowercase_						,	lowercase_						,	**lowercase_).prev_sample

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

              SCREAMING_SNAKE_CASE_      :  List[str]    						=		scheduler.step_plms(lowercase_						,	lowercase_						,	lowercase_						,	**lowercase_).prev_sample
              SCREAMING_SNAKE_CASE_      :  Dict    						=		new_scheduler.step_plms(lowercase_						,	lowercase_						,	lowercase_						,	**lowercase_).prev_sample

              assert torch.sum(torch.abs(output - new_output)) < 1e-5, "Scheduler outputs are not identical"
      def _SCREAMING_SNAKE_CASE (						self     :  Any):




          '''simple docstring'''


          pass
      def _SCREAMING_SNAKE_CASE (						self     :  List[Any]						,	lowercase_     :  List[str]=0						,	**lowercase_     :  List[str]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Optional[int]    						=		dict(self.forward_default_kwargs)
          SCREAMING_SNAKE_CASE_      :  Optional[Any]    						=		kwargs.pop('''num_inference_steps'''						,	lowercase_)
          SCREAMING_SNAKE_CASE_      :  List[str]    						=		self.dummy_sample
          SCREAMING_SNAKE_CASE_      :  Optional[Any]    						=		0.1 * sample
          SCREAMING_SNAKE_CASE_      :  int    						=		[residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]

          for scheduler_class in self.scheduler_classes:
              SCREAMING_SNAKE_CASE_      :  Dict    						=		self.get_scheduler_config()
              SCREAMING_SNAKE_CASE_      :  Union[str, Any]    						=		scheduler_class(**lowercase_)
              scheduler.set_timesteps(lowercase_)

              # copy over dummy past residuals (must be after setting timesteps)
              SCREAMING_SNAKE_CASE_      :  Dict    						=		dummy_past_residuals[:]

              with tempfile.TemporaryDirectory() as tmpdirname:
                  scheduler.save_config(lowercase_)
                  SCREAMING_SNAKE_CASE_      :  str    						=		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_      :  Any    						=		dummy_past_residuals[:]

              SCREAMING_SNAKE_CASE_      :  Optional[int]    						=		scheduler.step_prk(lowercase_						,	lowercase_						,	lowercase_						,	**lowercase_).prev_sample
              SCREAMING_SNAKE_CASE_      :  Optional[Any]    						=		new_scheduler.step_prk(lowercase_						,	lowercase_						,	lowercase_						,	**lowercase_).prev_sample

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

              SCREAMING_SNAKE_CASE_      :  List[str]    						=		scheduler.step_plms(lowercase_						,	lowercase_						,	lowercase_						,	**lowercase_).prev_sample
              SCREAMING_SNAKE_CASE_      :  Tuple    						=		new_scheduler.step_plms(lowercase_						,	lowercase_						,	lowercase_						,	**lowercase_).prev_sample

              assert torch.sum(torch.abs(output - new_output)) < 1e-5, "Scheduler outputs are not identical"
      def _SCREAMING_SNAKE_CASE (						self     :  str						,	**lowercase_     :  str):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Tuple    						=		self.scheduler_classes[0]
          SCREAMING_SNAKE_CASE_      :  Optional[int]    						=		self.get_scheduler_config(**lowercase_)
          SCREAMING_SNAKE_CASE_      :  List[Any]    						=		scheduler_class(**lowercase_)

          SCREAMING_SNAKE_CASE_      :  Dict    						=		10
          SCREAMING_SNAKE_CASE_      :  List[Any]    						=		self.dummy_model()
          SCREAMING_SNAKE_CASE_      :  str    						=		self.dummy_sample_deter
          scheduler.set_timesteps(lowercase_)

          for i, t in enumerate(scheduler.prk_timesteps):
              SCREAMING_SNAKE_CASE_      :  Optional[Any]    						=		model(lowercase_						,	lowercase_)
              SCREAMING_SNAKE_CASE_      :  str    						=		scheduler.step_prk(lowercase_						,	lowercase_						,	lowercase_).prev_sample

          for i, t in enumerate(scheduler.plms_timesteps):
              SCREAMING_SNAKE_CASE_      :  List[Any]    						=		model(lowercase_						,	lowercase_)
              SCREAMING_SNAKE_CASE_      :  List[str]    						=		scheduler.step_plms(lowercase_						,	lowercase_						,	lowercase_).prev_sample

          return sample
      def _SCREAMING_SNAKE_CASE (						self     :  Dict):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Any    						=		dict(self.forward_default_kwargs)

          SCREAMING_SNAKE_CASE_      :  Dict    						=		kwargs.pop('''num_inference_steps'''						,	lowercase_)

          for scheduler_class in self.scheduler_classes:
              SCREAMING_SNAKE_CASE_      :  Tuple    						=		self.get_scheduler_config()
              SCREAMING_SNAKE_CASE_      :  Optional[Any]    						=		scheduler_class(**lowercase_)

              SCREAMING_SNAKE_CASE_      :  Optional[int]    						=		self.dummy_sample
              SCREAMING_SNAKE_CASE_      :  Any    						=		0.1 * sample

              if num_inference_steps is not None and hasattr(lowercase_						,	'''set_timesteps'''):
                  scheduler.set_timesteps(lowercase_)
              elif num_inference_steps is not None and not hasattr(lowercase_						,	'''set_timesteps'''):
                  SCREAMING_SNAKE_CASE_      :  Optional[Any]    						=		num_inference_steps

              # copy over dummy past residuals (must be done after set_timesteps)
              SCREAMING_SNAKE_CASE_      :  str    						=		[residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]
              SCREAMING_SNAKE_CASE_      :  Optional[int]    						=		dummy_past_residuals[:]

              SCREAMING_SNAKE_CASE_      :  Dict    						=		scheduler.step_prk(lowercase_						,	0						,	lowercase_						,	**lowercase_).prev_sample
              SCREAMING_SNAKE_CASE_      :  List[Any]    						=		scheduler.step_prk(lowercase_						,	1						,	lowercase_						,	**lowercase_).prev_sample

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

              SCREAMING_SNAKE_CASE_      :  Optional[int]    						=		scheduler.step_plms(lowercase_						,	0						,	lowercase_						,	**lowercase_).prev_sample
              SCREAMING_SNAKE_CASE_      :  Any    						=		scheduler.step_plms(lowercase_						,	1						,	lowercase_						,	**lowercase_).prev_sample

              self.assertEqual(output_a.shape						,	sample.shape)
              self.assertEqual(output_a.shape						,	output_a.shape)
      def _SCREAMING_SNAKE_CASE (						self     :  str):




          '''simple docstring'''


          for timesteps in [100, 1000]:
              self.check_over_configs(num_train_timesteps=lowercase_)
      def _SCREAMING_SNAKE_CASE (						self     :  Dict):




          '''simple docstring'''


          for steps_offset in [0, 1]:
              self.check_over_configs(steps_offset=lowercase_)

          SCREAMING_SNAKE_CASE_      :  Dict    						=		self.scheduler_classes[0]
          SCREAMING_SNAKE_CASE_      :  List[str]    						=		self.get_scheduler_config(steps_offset=1)
          SCREAMING_SNAKE_CASE_      :  Tuple    						=		scheduler_class(**lowercase_)
          scheduler.set_timesteps(10)
          assert torch.equal(
              scheduler.timesteps						,	torch.LongTensor(
                  [901, 851, 851, 801, 801, 751, 751, 701, 701, 651, 651, 601, 601, 501, 401, 301, 201, 101, 1])						,	)
      def _SCREAMING_SNAKE_CASE (						self     :  Any):




          '''simple docstring'''


          for beta_start, beta_end in zip([0.00_01, 0.0_01]						,	[0.0_02, 0.02]):
              self.check_over_configs(beta_start=lowercase_						,	beta_end=lowercase_)
      def _SCREAMING_SNAKE_CASE (						self     :  List[Any]):




          '''simple docstring'''


          for schedule in ["linear", "squaredcos_cap_v2"]:
              self.check_over_configs(beta_schedule=lowercase_)
      def _SCREAMING_SNAKE_CASE (						self     :  Any):




          '''simple docstring'''


          for prediction_type in ["epsilon", "v_prediction"]:
              self.check_over_configs(prediction_type=lowercase_)
      def _SCREAMING_SNAKE_CASE (						self     :  int):




          '''simple docstring'''


          for t in [1, 5, 10]:
              self.check_over_forward(time_step=lowercase_)
      def _SCREAMING_SNAKE_CASE (						self     :  Optional[Any]):




          '''simple docstring'''


          for t, num_inference_steps in zip([1, 5, 10]						,	[10, 50, 100]):
              self.check_over_forward(num_inference_steps=lowercase_)
      def _SCREAMING_SNAKE_CASE (						self     :  Any):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Union[str, Any]    						=		27

          for scheduler_class in self.scheduler_classes:
              SCREAMING_SNAKE_CASE_      :  List[Any]    						=		self.dummy_sample
              SCREAMING_SNAKE_CASE_      :  str    						=		0.1 * sample

              SCREAMING_SNAKE_CASE_      :  Union[str, Any]    						=		self.get_scheduler_config()
              SCREAMING_SNAKE_CASE_      :  Optional[int]    						=		scheduler_class(**lowercase_)

              scheduler.set_timesteps(lowercase_)

              # before power of 3 fix, would error on first step, so we only need to do two
              for i, t in enumerate(scheduler.prk_timesteps[:2]):
                  SCREAMING_SNAKE_CASE_      :  int    						=		scheduler.step_prk(lowercase_						,	lowercase_						,	lowercase_).prev_sample
      def _SCREAMING_SNAKE_CASE (						self     :  List[Any]):




          '''simple docstring'''


          with self.assertRaises(lowercase_):
              SCREAMING_SNAKE_CASE_      :  int    						=		self.scheduler_classes[0]
              SCREAMING_SNAKE_CASE_      :  List[str]    						=		self.get_scheduler_config()
              SCREAMING_SNAKE_CASE_      :  Dict    						=		scheduler_class(**lowercase_)

              scheduler.step_plms(self.dummy_sample						,	1						,	self.dummy_sample).prev_sample
      def _SCREAMING_SNAKE_CASE (						self     :  Optional[int]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Any    						=		self.full_loop()
          SCREAMING_SNAKE_CASE_      :  List[Any]    						=		torch.sum(torch.abs(lowercase_))
          SCREAMING_SNAKE_CASE_      :  Union[str, Any]    						=		torch.mean(torch.abs(lowercase_))

          assert abs(result_sum.item() - 1_98.13_18) < 1e-2
          assert abs(result_mean.item() - 0.25_80) < 1e-3
      def _SCREAMING_SNAKE_CASE (						self     :  Optional[int]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  str    						=		self.full_loop(prediction_type='''v_prediction''')
          SCREAMING_SNAKE_CASE_      :  str    						=		torch.sum(torch.abs(lowercase_))
          SCREAMING_SNAKE_CASE_      :  Any    						=		torch.mean(torch.abs(lowercase_))

          assert abs(result_sum.item() - 67.39_86) < 1e-2
          assert abs(result_mean.item() - 0.08_78) < 1e-3
      def _SCREAMING_SNAKE_CASE (						self     :  Optional[Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Tuple    						=		self.full_loop(set_alpha_to_one=lowercase_						,	beta_start=0.01)
          SCREAMING_SNAKE_CASE_      :  Optional[Any]    						=		torch.sum(torch.abs(lowercase_))
          SCREAMING_SNAKE_CASE_      :  Any    						=		torch.mean(torch.abs(lowercase_))

          assert abs(result_sum.item() - 2_30.03_99) < 1e-2
          assert abs(result_mean.item() - 0.29_95) < 1e-3



      def _SCREAMING_SNAKE_CASE (						self     :  Optional[Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Union[str, Any]    						=		self.full_loop(set_alpha_to_one=lowercase_						,	beta_start=0.01)
          SCREAMING_SNAKE_CASE_      :  int    						=		torch.sum(torch.abs(lowercase_))
          SCREAMING_SNAKE_CASE_      :  List[str]    						=		torch.mean(torch.abs(lowercase_))

          assert abs(result_sum.item() - 1_86.94_82) < 1e-2
          assert abs(result_mean.item() - 0.24_34) < 1e-3


