


"""simple docstring"""







import torch

from diffusers import KDPMaDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest






class 						_A  (  a__  ):







    """simple docstring"""




    UpperCAmelCase	:							Union[str, Any]     =	(KDPMaDiscreteScheduler,)
    UpperCAmelCase	:							Union[str, Any]     =	1_0






    def      __snake_case							( self    :   Tuple    ,			**__UpperCAmelCase    :   Dict):
      a						:		Any											=      {
          '''num_train_timesteps''': 1100,
          '''beta_start''': 0.0_001,
          '''beta_end''': 0.02,
          '''beta_schedule''': '''linear''',
      }

      config.update(**_lowerCamelCase)
      return config






    def      __snake_case							( self    :   Tuple):







      for timesteps in [10, 50, 100, 1000]:
        self.check_over_configs(num_train_timesteps=_lowerCamelCase)






    def      __snake_case							( self    :   List[Any]):







      for beta_start, beta_end in zip([0.00_001, 0.0_001, 0.001]    ,			[0.0_002, 0.002, 0.02]):
        self.check_over_configs(beta_start=_lowerCamelCase    ,			beta_end=_lowerCamelCase)






    def      __snake_case							( self    :   int):







      for schedule in ["linear", "scaled_linear"]:
        self.check_over_configs(beta_schedule=_lowerCamelCase)






    def      __snake_case							( self    :   Union[str, Any]):







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






    def      __snake_case							( self    :   Any):
      a						:		Optional[int]											=      self.scheduler_classes[0]
      a						:		Optional[int]											=      self.get_scheduler_config(prediction_type="v_prediction")
      a						:		Any											=      scheduler_class(**_lowerCamelCase)

      scheduler.set_timesteps(self.num_inference_steps)

      a						:		Dict											=      self.dummy_model()
      a						:		int											=      self.dummy_sample_deter * scheduler.init_noise_sigma
      a						:		Optional[Any]											=      sample.to(_lowerCamelCase)

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

        a						:		Tuple											=      model(_lowerCamelCase    ,			_lowerCamelCase)

        a						:		List[str]											=      scheduler.step(_lowerCamelCase    ,			_lowerCamelCase    ,			_lowerCamelCase)
        a						:		str											=      output.prev_sample

      a						:		int											=      torch.sum(torch.abs(_lowerCamelCase))
      a						:		Any											=      torch.mean(torch.abs(_lowerCamelCase))

      if torch_device in ["cpu", "mps"]:
        assert abs(result_sum.item() - 4.6934e-07) < 1e-2
        assert abs(result_mean.item() - 6.1112e-10) < 1e-3
      else:
        # CUDA
        assert abs(result_sum.item() - 4.693428650170972e-07) < 1e-2
        assert abs(result_mean.item() - 0.0_002) < 1e-3






    def      __snake_case							( self    :   List[Any]):







      if torch_device == "mps":
        return
      a						:		Dict											=      self.scheduler_classes[0]
      a						:		List[Any]											=      self.get_scheduler_config()
      a						:		str											=      scheduler_class(**_lowerCamelCase)

      scheduler.set_timesteps(self.num_inference_steps)

      a						:		Any											=      self.dummy_model()
      a						:		Union[str, Any]											=      self.dummy_sample_deter * scheduler.init_noise_sigma
      a						:		Any											=      sample.to(_lowerCamelCase)

      for i, t in enumerate(scheduler.timesteps):
        a						:		str											=      scheduler.scale_model_input(_lowerCamelCase    ,			_lowerCamelCase)

        a						:		Dict											=      model(_lowerCamelCase    ,			_lowerCamelCase)

        a						:		Optional[Any]											=      scheduler.step(_lowerCamelCase    ,			_lowerCamelCase    ,			_lowerCamelCase)
        a						:		Any											=      output.prev_sample

      a						:		Optional[Any]											=      torch.sum(torch.abs(_lowerCamelCase))
      a						:		Optional[Any]											=      torch.mean(torch.abs(_lowerCamelCase))

      if torch_device in ["cpu", "mps"]:
        assert abs(result_sum.item() - 20.4_125) < 1e-2
        assert abs(result_mean.item() - 0.0_266) < 1e-3
      else:
        # CUDA
        assert abs(result_sum.item() - 20.4_125) < 1e-2
        assert abs(result_mean.item() - 0.0_266) < 1e-3






    def      __snake_case							( self    :   List[Any]):







      if torch_device == "mps":
        return
      a						:		Optional[Any]											=      self.scheduler_classes[0]
      a						:		Optional[int]											=      self.get_scheduler_config()
      a						:		List[str]											=      scheduler_class(**_lowerCamelCase)

      scheduler.set_timesteps(self.num_inference_steps    ,			device=_lowerCamelCase)

      a						:		List[str]											=      self.dummy_model()
      a						:		List[Any]											=      self.dummy_sample_deter.to(_lowerCamelCase) * scheduler.init_noise_sigma

      for t in scheduler.timesteps:
        a						:		Any											=      scheduler.scale_model_input(_lowerCamelCase    ,			_lowerCamelCase)

        a						:		Optional[Any]											=      model(_lowerCamelCase    ,			_lowerCamelCase)

        a						:		int											=      scheduler.step(_lowerCamelCase    ,			_lowerCamelCase    ,			_lowerCamelCase)
        a						:		str											=      output.prev_sample

      a						:		Optional[int]											=      torch.sum(torch.abs(_lowerCamelCase))
      a						:		Dict											=      torch.mean(torch.abs(_lowerCamelCase))

      if str(_lowerCamelCase).startswith("cpu"):
        # The following sum varies between 148 and 156 on mps. Why?
        assert abs(result_sum.item() - 20.4_125) < 1e-2
        assert abs(result_mean.item() - 0.0_266) < 1e-3
      else:
        # CUDA
        assert abs(result_sum.item() - 20.4_125) < 1e-2
        assert abs(result_mean.item() - 0.0_266) < 1e-3





"""simple docstring"""







__lowercase    =				frozenset(
    [
        """prompt""",
        """height""",
        """width""",
        """guidance_scale""",
        """negative_prompt""",
        """prompt_embeds""",
        """negative_prompt_embeds""",
        """cross_attention_kwargs""",
    ]
)

__lowercase    =				frozenset(["""prompt""", """negative_prompt"""])

__lowercase    =				frozenset([])

__lowercase    =				frozenset(["""image"""])

__lowercase    =				frozenset(
    [
        """image""",
        """height""",
        """width""",
        """guidance_scale""",
    ]
)

__lowercase    =				frozenset(["""image"""])

__lowercase    =				frozenset(
    [
        """prompt""",
        """image""",
        """height""",
        """width""",
        """guidance_scale""",
        """negative_prompt""",
        """prompt_embeds""",
        """negative_prompt_embeds""",
    ]
)

__lowercase    =				frozenset(["""prompt""", """image""", """negative_prompt"""])

__lowercase    =				frozenset(
    [
        # Text guided image variation with an image mask
        """prompt""",
        """image""",
        """mask_image""",
        """height""",
        """width""",
        """guidance_scale""",
        """negative_prompt""",
        """prompt_embeds""",
        """negative_prompt_embeds""",
    ]
)

__lowercase    =				frozenset(["""prompt""", """image""", """mask_image""", """negative_prompt"""])

__lowercase    =				frozenset(
    [
        # image variation with an image mask
        """image""",
        """mask_image""",
        """height""",
        """width""",
        """guidance_scale""",
    ]
)

__lowercase    =				frozenset(["""image""", """mask_image"""])

__lowercase    =				frozenset(
    [
        """example_image""",
        """image""",
        """mask_image""",
        """height""",
        """width""",
        """guidance_scale""",
    ]
)

__lowercase    =				frozenset(["""example_image""", """image""", """mask_image"""])

__lowercase    =				frozenset(["""class_labels"""])

__lowercase    =				frozenset(["""class_labels"""])

__lowercase    =				frozenset(["""batch_size"""])

__lowercase    =				frozenset([])

__lowercase    =				frozenset(["""batch_size"""])

__lowercase    =				frozenset([])

__lowercase    =				frozenset(
    [
        """prompt""",
        """audio_length_in_s""",
        """guidance_scale""",
        """negative_prompt""",
        """prompt_embeds""",
        """negative_prompt_embeds""",
        """cross_attention_kwargs""",
    ]
)

__lowercase    =				frozenset(["""prompt""", """negative_prompt"""])
__lowercase    =				frozenset(["""input_tokens"""])

__lowercase    =				frozenset(["""input_tokens"""])






