

import torch

from diffusers import KDPMaAncestralDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest





class    __UpperCAmelCase	(			lowerCamelCase__		):
   UpperCamelCase  	=	(KDPMaAncestralDiscreteScheduler,)
   UpperCamelCase  	=	1_0



   def 			__magic_name__       (				self				:	Tuple,						**__A				:	Tuple		):
     UpperCAmelCase			:				Any										=					{
         '''num_train_timesteps''': 1_1_0_0,
         '''beta_start''': 0.0_0_0_1,
         '''beta_end''': 0.0_2,
         '''beta_schedule''': '''linear''',
     }

     config.update(**__A		)
     return config



   def 			__magic_name__       (				self				:	Optional[int]		):
     for timesteps in [1_0, 5_0, 1_0_0, 1_0_0_0]:
       self.check_over_configs(num_train_timesteps=__A		)



   def 			__magic_name__       (				self				:	Tuple		):
     for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1],						[0.0_0_0_2, 0.0_0_2, 0.0_2]		):
       self.check_over_configs(beta_start=__A,						beta_end=__A		)



   def 			__magic_name__       (				self				:	int		):
     for schedule in ["linear", "scaled_linear"]:
       self.check_over_configs(beta_schedule=__A		)



   def 			__magic_name__       (				self				:	List[Any]		):
     if torch_device == "mps":
       return
     UpperCAmelCase			:				Dict										=					self.scheduler_classes[0]
     UpperCAmelCase			:				List[str]										=					self.get_scheduler_config()
     UpperCAmelCase			:				Dict										=					scheduler_class(**__A		)

     scheduler.set_timesteps(self.num_inference_steps		)

     UpperCAmelCase			:				Dict										=					torch.manual_seed(0		)

     UpperCAmelCase			:				Union[str, Any]										=					self.dummy_model()
     UpperCAmelCase			:				List[str]										=					self.dummy_sample_deter * scheduler.init_noise_sigma
     UpperCAmelCase			:				Dict										=					sample.to(__A		)

     for i, t in enumerate(scheduler.timesteps		):
       UpperCAmelCase			:				int										=					scheduler.scale_model_input(__A,						__A		)

       UpperCAmelCase			:				int										=					model(__A,						__A		)

       UpperCAmelCase			:				List[Any]										=					scheduler.step(__A,						__A,						__A,						generator=__A		)
       UpperCAmelCase			:				Optional[Any]										=					output.prev_sample

     UpperCAmelCase			:				Any										=					torch.sum(torch.abs(__A		)		)
     UpperCAmelCase			:				int										=					torch.mean(torch.abs(__A		)		)

     assert abs(result_sum.item() - 1_3_8_4_9.3_8_7_7		) < 1E-2
     assert abs(result_mean.item() - 1_8.0_3_3_1		) < 5E-3



   def 			__magic_name__       (				self				:	List[str]		):
     for prediction_type in ["epsilon", "v_prediction"]:
       self.check_over_configs(prediction_type=__A		)



   def 			__magic_name__       (				self				:	Union[str, Any]		):
     if torch_device == "mps":
       return
     UpperCAmelCase			:				List[str]										=					self.scheduler_classes[0]
     UpperCAmelCase			:				List[Any]										=					self.get_scheduler_config(prediction_type='''v_prediction'''		)
     UpperCAmelCase			:				str										=					scheduler_class(**__A		)

     scheduler.set_timesteps(self.num_inference_steps		)

     UpperCAmelCase			:				Tuple										=					self.dummy_model()
     UpperCAmelCase			:				Optional[int]										=					self.dummy_sample_deter * scheduler.init_noise_sigma
     UpperCAmelCase			:				Tuple										=					sample.to(__A		)

     UpperCAmelCase			:				Dict										=					torch.manual_seed(0		)

     for i, t in enumerate(scheduler.timesteps		):
       UpperCAmelCase			:				List[Any]										=					scheduler.scale_model_input(__A,						__A		)

       UpperCAmelCase			:				Optional[Any]										=					model(__A,						__A		)

       UpperCAmelCase			:				List[Any]										=					scheduler.step(__A,						__A,						__A,						generator=__A		)
       UpperCAmelCase			:				Dict										=					output.prev_sample

     UpperCAmelCase			:				Dict										=					torch.sum(torch.abs(__A		)		)
     UpperCAmelCase			:				int										=					torch.mean(torch.abs(__A		)		)

     assert abs(result_sum.item() - 3_2_8.9_9_7_0		) < 1E-2
     assert abs(result_mean.item() - 0.4_2_8_4		) < 1E-3





   def 			__magic_name__       (				self				:	Optional[Any]		):
     if torch_device == "mps":
       return
     UpperCAmelCase			:				List[Any]										=					self.scheduler_classes[0]
     UpperCAmelCase			:				List[Any]										=					self.get_scheduler_config()
     UpperCAmelCase			:				Union[str, Any]										=					scheduler_class(**__A		)

     scheduler.set_timesteps(self.num_inference_steps,						device=__A		)
     UpperCAmelCase			:				List[Any]										=					torch.manual_seed(0		)

     UpperCAmelCase			:				Tuple										=					self.dummy_model()
     UpperCAmelCase			:				Tuple										=					self.dummy_sample_deter.to(__A		) * scheduler.init_noise_sigma

     for t in scheduler.timesteps:
       UpperCAmelCase			:				Tuple										=					scheduler.scale_model_input(__A,						__A		)

       UpperCAmelCase			:				Dict										=					model(__A,						__A		)

       UpperCAmelCase			:				Dict										=					scheduler.step(__A,						__A,						__A,						generator=__A		)
       UpperCAmelCase			:				Optional[Any]										=					output.prev_sample

     UpperCAmelCase			:				List[Any]										=					torch.sum(torch.abs(__A		)		)
     UpperCAmelCase			:				Tuple										=					torch.mean(torch.abs(__A		)		)

     assert abs(result_sum.item() - 1_3_8_4_9.3_8_1_8		) < 1E-1
     assert abs(result_mean.item() - 1_8.0_3_3_1		) < 1E-3








def      a__	(						UpperCAmelCase				:     list     )    ->		list:
  UpperCAmelCase			:				Tuple										=					len(UpperCAmelCase     )

  # Set the gap value to be decreased by a factor of 1.3
  # after each iteration
  UpperCAmelCase			:				Optional[Any]										=					1.3

  # Continue sorting until the gap is 1
  while gap > 1:
    # Decrease the gap value
    UpperCAmelCase			:				int										=					int(gap / shrink     )

    # Sort the elements using insertion sort
    for i in range(UpperCAmelCase							,					len(UpperCAmelCase     )     ):
      UpperCAmelCase			:				Union[str, Any]										=					collection[i]
      UpperCAmelCase			:				List[Any]										=					i
      while j >= gap and collection[j - gap] > temp:
        UpperCAmelCase			:				int										=					collection[j - gap]
        j -= gap
      UpperCAmelCase			:				List[Any]										=					temp

  return collection


if __name__ == "__main__":
     import doctest

     doctest.testmod()





