
"""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"""





from typing import Dict, List, Optional, Tuple, Union

import torch

from ...models import AutoencoderKL, TransformeraDModel
from ...schedulers import KarrasDiffusionSchedulers
from ...utils import randn_tensor
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput






class    _UpperCAmelCase(   lowerCamelCase     ):



		def __init__( self    ,		__a    ,		__a    ,		__a    ,		__a = None    ,		)	->	List[str]:






							'''simple docstring'''
							super().__init__()
							self.register_modules(transformer=__a    ,		vae=__a    ,		scheduler=__a)

							# create a imagenet -> id dictionary for easier use
							_UpperCamelCase										=  {}
							if idalabel is not None:
												for key, value in idalabel.items():
																	for label in value.split(''','''):
																						_UpperCamelCase										=  int(__a)
												_UpperCamelCase										=  dict(sorted(self.labels.items()))



		def 						UpperCAmelCase							( self    ,		__a)	->	List[int]:






							'''simple docstring'''

							if not isinstance(__a    ,		__a):
												_UpperCamelCase										=  list(__a)

							for l in label:
												if l not in self.labels:
																	raise ValueError(
																	    F'''{l} does not exist. Please make sure to select one of the following labels: \n {self.labels}.''')

							return [self.labels[l] for l in label]




		@torch.no_grad()
		def __call__( self    ,		__a    ,		__a = 4.0    ,		__a = None    ,		__a = 50    ,		__a = "pil"    ,		__a = True    ,		)	->	Union[ImagePipelineOutput, Tuple]:






							'''simple docstring'''
							_UpperCamelCase										=  len(__a)
							_UpperCamelCase										=  self.transformer.config.sample_size
							_UpperCamelCase										=  self.transformer.config.in_channels

							_UpperCamelCase										=  randn_tensor(
							    shape=(batch_size, latent_channels, latent_size, latent_size)    ,		generator=__a    ,		device=self.device    ,		dtype=self.transformer.dtype    ,		)
							_UpperCamelCase										=  torch.cat([latents] * 2) if guidance_scale > 1 else latents

							_UpperCamelCase										=  torch.tensor(__a    ,		device=self.device).reshape(-1)
							_UpperCamelCase										=  torch.tensor([10_00] * batch_size    ,		device=self.device)
							_UpperCamelCase										=  torch.cat([class_labels, class_null]    ,		0) if guidance_scale > 1 else class_labels

							# set step values
							self.scheduler.set_timesteps(__a)

							for t in self.progress_bar(self.scheduler.timesteps):
												if guidance_scale > 1:
																	_UpperCamelCase										=  latent_model_input[: len(__a) // 2]
																	_UpperCamelCase										=  torch.cat([half, half]    ,		dim=0)
												_UpperCamelCase										=  self.scheduler.scale_model_input(__a    ,		__a)

												_UpperCamelCase										=  t
												if not torch.is_tensor(__a):
																	# TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can
																	# This would be a good case for the `match` statement (Python 3.10+)
																	_UpperCamelCase										=  latent_model_input.device.type == '''mps'''
																	if isinstance(__a    ,		__a):
																						_UpperCamelCase										=  torch.floataa if is_mps else torch.floataa
																	else:
																						_UpperCamelCase										=  torch.intaa if is_mps else torch.intaa
																	_UpperCamelCase										=  torch.tensor([timesteps]    ,		dtype=__a    ,		device=latent_model_input.device)
												elif len(timesteps.shape) == 0:
																	_UpperCamelCase										=  timesteps[None].to(latent_model_input.device)
												# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
												_UpperCamelCase										=  timesteps.expand(latent_model_input.shape[0])
												# predict noise model_output
												_UpperCamelCase										=  self.transformer(
												    __a    ,		timestep=__a    ,		class_labels=__a).sample

												# perform guidance
												if guidance_scale > 1:
																	_UpperCamelCase			,  _UpperCamelCase										=  noise_pred[:, :latent_channels], noise_pred[:, latent_channels:]
																	_UpperCamelCase			,  _UpperCamelCase										=  torch.split(__a    ,		len(__a) // 2    ,		dim=0)

																	_UpperCamelCase										=  uncond_eps + guidance_scale * (cond_eps - uncond_eps)
																	_UpperCamelCase										=  torch.cat([half_eps, half_eps]    ,		dim=0)

																	_UpperCamelCase										=  torch.cat([eps, rest]    ,		dim=1)

												# learned sigma
												if self.transformer.config.out_channels // 2 == latent_channels:
																	_UpperCamelCase			,  _UpperCamelCase										=  torch.split(__a    ,		__a    ,		dim=1)
												else:
																	_UpperCamelCase										=  noise_pred

												# compute previous image: x_t -> x_t-1
												_UpperCamelCase										=  self.scheduler.step(__a    ,		__a    ,		__a).prev_sample

							if guidance_scale > 1:
												_UpperCamelCase			,  _UpperCamelCase										=  latent_model_input.chunk(2    ,		dim=0)
							else:
												_UpperCamelCase										=  latent_model_input

							_UpperCamelCase										=  1 / self.vae.config.scaling_factor * latents
							_UpperCamelCase										=  self.vae.decode(__a).sample

							_UpperCamelCase										=  (samples / 2 + 0.5).clamp(0    ,		1)

							# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
							_UpperCamelCase										=  samples.cpu().permute(0    ,		2    ,		3    ,		1).float().numpy()

							if output_type == "pil":
												_UpperCamelCase										=  self.numpy_to_pil(__a)

							if not return_dict:
												return (samples,)

							return ImagePipelineOutput(images=__a)






