




"""simple docstring"""

a							      =     '''0.18.2'''

from .configuration_utils import ConfigMixin
from .utils import (
    OptionalDependencyNotAvailable,
    is_flax_available,
    is_inflect_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_librosa_available,
    is_note_seq_available,
    is_onnx_available,
    is_scipy_available,
    is_torch_available,
    is_torchsde_available,
    is_transformers_available,
    is_transformers_version,
    is_unidecode_available,
    logging,
)


try:
       if not is_onnx_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       from .utils.dummy_onnx_objects import *  # noqa F403
else:
       from .pipelines import OnnxRuntimeModel

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       from .utils.dummy_pt_objects import *  # noqa F403
else:
       from .models import (
           AutoencoderKL,
           ControlNetModel,
           ModelMixin,
           PriorTransformer,
           TaFilmDecoder,
           TransformeraDModel,
           UNetaDModel,
           UNetaDConditionModel,
           UNetaDModel,
           UNetaDConditionModel,
           VQModel,
       )
       from .optimization import (
           get_constant_schedule,
           get_constant_schedule_with_warmup,
           get_cosine_schedule_with_warmup,
           get_cosine_with_hard_restarts_schedule_with_warmup,
           get_linear_schedule_with_warmup,
           get_polynomial_decay_schedule_with_warmup,
           get_scheduler,
       )
       from .pipelines import (
           AudioPipelineOutput,
           ConsistencyModelPipeline,
           DanceDiffusionPipeline,
           DDIMPipeline,
           DDPMPipeline,
           DiffusionPipeline,
           DiTPipeline,
           ImagePipelineOutput,
           KarrasVePipeline,
           LDMPipeline,
           LDMSuperResolutionPipeline,
           PNDMPipeline,
           RePaintPipeline,
           ScoreSdeVePipeline,
       )
       from .schedulers import (
           CMStochasticIterativeScheduler,
           DDIMInverseScheduler,
           DDIMParallelScheduler,
           DDIMScheduler,
           DDPMParallelScheduler,
           DDPMScheduler,
           DEISMultistepScheduler,
           DPMSolverMultistepInverseScheduler,
           DPMSolverMultistepScheduler,
           DPMSolverSinglestepScheduler,
           EulerAncestralDiscreteScheduler,
           EulerDiscreteScheduler,
           HeunDiscreteScheduler,
           IPNDMScheduler,
           KarrasVeScheduler,
           KDPMaAncestralDiscreteScheduler,
           KDPMaDiscreteScheduler,
           PNDMScheduler,
           RePaintScheduler,
           SchedulerMixin,
           ScoreSdeVeScheduler,
           UnCLIPScheduler,
           UniPCMultistepScheduler,
           VQDiffusionScheduler,
       )
       from .training_utils import EMAModel

try:
       if not (is_torch_available() and is_scipy_available()):
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       from .utils.dummy_torch_and_scipy_objects import *  # noqa F403
else:
       from .schedulers import LMSDiscreteScheduler

try:
       if not (is_torch_available() and is_torchsde_available()):
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       from .utils.dummy_torch_and_torchsde_objects import *  # noqa F403
else:
       from .schedulers import DPMSolverSDEScheduler

try:
       if not (is_torch_available() and is_transformers_available()):
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       from .utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
       from .pipelines import (
           AltDiffusionImgaImgPipeline,
           AltDiffusionPipeline,
           AudioLDMPipeline,
           CycleDiffusionPipeline,
           IFImgaImgPipeline,
           IFImgaImgSuperResolutionPipeline,
           IFInpaintingPipeline,
           IFInpaintingSuperResolutionPipeline,
           IFPipeline,
           IFSuperResolutionPipeline,
           ImageTextPipelineOutput,
           KandinskyImgaImgPipeline,
           KandinskyInpaintPipeline,
           KandinskyPipeline,
           KandinskyPriorPipeline,
           KandinskyVaaControlnetImgaImgPipeline,
           KandinskyVaaControlnetPipeline,
           KandinskyVaaImgaImgPipeline,
           KandinskyVaaInpaintPipeline,
           KandinskyVaaPipeline,
           KandinskyVaaPriorEmbaEmbPipeline,
           KandinskyVaaPriorPipeline,
           LDMTextToImagePipeline,
           PaintByExamplePipeline,
           SemanticStableDiffusionPipeline,
           ShapEImgaImgPipeline,
           ShapEPipeline,
           StableDiffusionAttendAndExcitePipeline,
           StableDiffusionControlNetImgaImgPipeline,
           StableDiffusionControlNetInpaintPipeline,
           StableDiffusionControlNetPipeline,
           StableDiffusionDepthaImgPipeline,
           StableDiffusionDiffEditPipeline,
           StableDiffusionImageVariationPipeline,
           StableDiffusionImgaImgPipeline,
           StableDiffusionInpaintPipeline,
           StableDiffusionInpaintPipelineLegacy,
           StableDiffusionInstructPixaPixPipeline,
           StableDiffusionLatentUpscalePipeline,
           StableDiffusionLDMaDPipeline,
           StableDiffusionModelEditingPipeline,
           StableDiffusionPanoramaPipeline,
           StableDiffusionParadigmsPipeline,
           StableDiffusionPipeline,
           StableDiffusionPipelineSafe,
           StableDiffusionPixaPixZeroPipeline,
           StableDiffusionSAGPipeline,
           StableDiffusionUpscalePipeline,
           StableUnCLIPImgaImgPipeline,
           StableUnCLIPPipeline,
           TextToVideoSDPipeline,
           TextToVideoZeroPipeline,
           UnCLIPImageVariationPipeline,
           UnCLIPPipeline,
           UniDiffuserModel,
           UniDiffuserPipeline,
           UniDiffuserTextDecoder,
           VersatileDiffusionDualGuidedPipeline,
           VersatileDiffusionImageVariationPipeline,
           VersatileDiffusionPipeline,
           VersatileDiffusionTextToImagePipeline,
           VideoToVideoSDPipeline,
           VQDiffusionPipeline,
       )

try:
       if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()):
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       from .utils.dummy_torch_and_transformers_and_invisible_watermark_objects import *  # noqa F403
else:
       from .pipelines import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline

try:
       if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()):
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       from .utils.dummy_torch_and_transformers_and_k_diffusion_objects import *  # noqa F403
else:
       from .pipelines import StableDiffusionKDiffusionPipeline

try:
       if not (is_torch_available() and is_transformers_available() and is_onnx_available()):
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       from .utils.dummy_torch_and_transformers_and_onnx_objects import *  # noqa F403
else:
       from .pipelines import (
           OnnxStableDiffusionImgaImgPipeline,
           OnnxStableDiffusionInpaintPipeline,
           OnnxStableDiffusionInpaintPipelineLegacy,
           OnnxStableDiffusionPipeline,
           OnnxStableDiffusionUpscalePipeline,
           StableDiffusionOnnxPipeline,
       )

try:
       if not (is_torch_available() and is_librosa_available()):
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       from .utils.dummy_torch_and_librosa_objects import *  # noqa F403
else:
       from .pipelines import AudioDiffusionPipeline, Mel

try:
       if not (is_transformers_available() and is_torch_available() and is_note_seq_available()):
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       from .utils.dummy_transformers_and_torch_and_note_seq_objects import *  # noqa F403
else:
       from .pipelines import SpectrogramDiffusionPipeline

try:
       if not is_flax_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       from .utils.dummy_flax_objects import *  # noqa F403
else:
       from .models.controlnet_flax import FlaxControlNetModel
       from .models.modeling_flax_utils import FlaxModelMixin
       from .models.unet_ad_condition_flax import FlaxUNetaDConditionModel
       from .models.vae_flax import FlaxAutoencoderKL
       from .pipelines import FlaxDiffusionPipeline
       from .schedulers import (
           FlaxDDIMScheduler,
           FlaxDDPMScheduler,
           FlaxDPMSolverMultistepScheduler,
           FlaxKarrasVeScheduler,
           FlaxLMSDiscreteScheduler,
           FlaxPNDMScheduler,
           FlaxSchedulerMixin,
           FlaxScoreSdeVeScheduler,
       )


try:
       if not (is_flax_available() and is_transformers_available()):
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       from .utils.dummy_flax_and_transformers_objects import *  # noqa F403
else:
       from .pipelines import (
           FlaxStableDiffusionControlNetPipeline,
           FlaxStableDiffusionImgaImgPipeline,
           FlaxStableDiffusionInpaintPipeline,
           FlaxStableDiffusionPipeline,
       )

try:
       if not (is_note_seq_available()):
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       from .utils.dummy_note_seq_objects import *  # noqa F403
else:
       from .pipelines import MidiProcessor







"""simple docstring"""

import torch

from diffusers import KDPMaDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest





class 							lowercase_			(	__lowerCAmelCase				):



 '''simple docstring'''
 UpperCAmelCase       :	Optional[int]     =      (KDPMaDiscreteScheduler,)
 UpperCAmelCase       :	Any     =      10

 def      lowerCAmelCase_				(					self			:			Dict       ,				**_UpperCAmelCase			:			Optional[Any]						):
       _A     =  {
           'num_train_timesteps': 1_100,
           'beta_start': 0.0001,
           'beta_end': 0.02,
           'beta_schedule': 'linear',
       }

       config.update(**_UpperCAmelCase						)
       return config

 def      lowerCAmelCase_				(					self			:			Any						):
       for timesteps in [10, 50, 100, 1_000]:
             self.check_over_configs(num_train_timesteps=_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Dict						):
       for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001]       ,				[0.0002, 0.002, 0.02]						):
             self.check_over_configs(beta_start=_UpperCAmelCase       ,				beta_end=_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Tuple						):
       for schedule in ["linear", "scaled_linear"]:
             self.check_over_configs(beta_schedule=_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Optional[int]						):
       for prediction_type in ["epsilon", "v_prediction"]:
             self.check_over_configs(prediction_type=_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Optional[int]						):
       _A     =  self.scheduler_classes[0]
       _A     =  self.get_scheduler_config(prediction_type='v_prediction'						)
       _A     =  scheduler_class(**_UpperCAmelCase						)

       scheduler.set_timesteps(self.num_inference_steps						)

       _A     =  self.dummy_model()
       _A     =  self.dummy_sample_deter * scheduler.init_noise_sigma
       _A     =  sample.to(_UpperCAmelCase						)

       for i, t in enumerate(scheduler.timesteps						):
             _A     =  scheduler.scale_model_input(_UpperCAmelCase       ,				_UpperCAmelCase						)

             _A     =  model(_UpperCAmelCase       ,				_UpperCAmelCase						)

             _A     =  scheduler.step(_UpperCAmelCase       ,				_UpperCAmelCase       ,				_UpperCAmelCase						)
             _A     =  output.prev_sample

       _A     =  torch.sum(torch.abs(_UpperCAmelCase						)						)
       _A     =  torch.mean(torch.abs(_UpperCAmelCase						)						)

       if torch_device in ["cpu", "mps"]:
             assert abs(result_sum.item() - 4.6_9_3_4E-0_7						) < 1E-2
             assert abs(result_mean.item() - 6.1_1_1_2E-1_0						) < 1E-3
       else:
             # CUDA
             assert abs(result_sum.item() - 4.6_9_3_4_2_8_6_5_0_1_7_0_9_7_2E-0_7						) < 1E-2
             assert abs(result_mean.item() - 0.0002						) < 1E-3

 def      lowerCAmelCase_				(					self			:			Optional[Any]						):
       if torch_device == "mps":
             return
       _A     =  self.scheduler_classes[0]
       _A     =  self.get_scheduler_config()
       _A     =  scheduler_class(**_UpperCAmelCase						)

       scheduler.set_timesteps(self.num_inference_steps						)

       _A     =  self.dummy_model()
       _A     =  self.dummy_sample_deter * scheduler.init_noise_sigma
       _A     =  sample.to(_UpperCAmelCase						)

       for i, t in enumerate(scheduler.timesteps						):
             _A     =  scheduler.scale_model_input(_UpperCAmelCase       ,				_UpperCAmelCase						)

             _A     =  model(_UpperCAmelCase       ,				_UpperCAmelCase						)

             _A     =  scheduler.step(_UpperCAmelCase       ,				_UpperCAmelCase       ,				_UpperCAmelCase						)
             _A     =  output.prev_sample

       _A     =  torch.sum(torch.abs(_UpperCAmelCase						)						)
       _A     =  torch.mean(torch.abs(_UpperCAmelCase						)						)

       if torch_device in ["cpu", "mps"]:
             assert abs(result_sum.item() - 20.4125						) < 1E-2
             assert abs(result_mean.item() - 0.0266						) < 1E-3
       else:
             # CUDA
             assert abs(result_sum.item() - 20.4125						) < 1E-2
             assert abs(result_mean.item() - 0.0266						) < 1E-3



 def      lowerCAmelCase_				(					self			:			Any						):
       if torch_device == "mps":
             return
       _A     =  self.scheduler_classes[0]
       _A     =  self.get_scheduler_config()
       _A     =  scheduler_class(**_UpperCAmelCase						)

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

       _A     =  self.dummy_model()
       _A     =  self.dummy_sample_deter.to(_UpperCAmelCase						) * scheduler.init_noise_sigma

       for t in scheduler.timesteps:
             _A     =  scheduler.scale_model_input(_UpperCAmelCase       ,				_UpperCAmelCase						)

             _A     =  model(_UpperCAmelCase       ,				_UpperCAmelCase						)

             _A     =  scheduler.step(_UpperCAmelCase       ,				_UpperCAmelCase       ,				_UpperCAmelCase						)
             _A     =  output.prev_sample

       _A     =  torch.sum(torch.abs(_UpperCAmelCase						)						)
       _A     =  torch.mean(torch.abs(_UpperCAmelCase						)						)

       if str(_UpperCAmelCase						).startswith('cpu'						):
             # The following sum varies between 148 and 156 on mps. Why?
             assert abs(result_sum.item() - 20.4125						) < 1E-2
             assert abs(result_mean.item() - 0.0266						) < 1E-3
       else:
             # CUDA
             assert abs(result_sum.item() - 20.4125						) < 1E-2
             assert abs(result_mean.item() - 0.0266						) < 1E-3

