






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








import math
from typing import Any, Callable, List, Optional, Tuple, Union

import numpy as np
import torch

from ...models import TaFilmDecoder
from ...schedulers import DDPMScheduler
from ...utils import is_onnx_available, logging, randn_tensor


if is_onnx_available():
	from ..onnx_utils import OnnxRuntimeModel

from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline
from .continous_encoder import SpectrogramContEncoder
from .notes_encoder import SpectrogramNotesEncoder


a__       =     logging.get_logger(__name__)  # pylint: disable=invalid-name

a__       =     2_5_6


class    _lowerCAmelCase			(		lowercase_      ):



				"""simple docstring"""



				_lowercase				:   str      =  ['''melgan''']



				def __init__(      self    :    int			,		UpperCamelCase__    :    SpectrogramNotesEncoder			,		UpperCamelCase__    :    SpectrogramContEncoder			,		UpperCamelCase__    :    TaFilmDecoder			,		UpperCamelCase__    :    DDPMScheduler			,		UpperCamelCase__    :    OnnxRuntimeModel if is_onnx_available() else Any			,		):






											'''simple docstring'''







											super().__init__()

											# From MELGAN
											snake_case__	  =       math.log(1E-5)  # Matches MelGAN training.
											snake_case__	  =       4.0  # Largest value for most examples
											snake_case__	  =       1_2_8

											self.register_modules(
											    notes_encoder=UpperCamelCase__			,		continuous_encoder=UpperCamelCase__			,		decoder=UpperCamelCase__			,		scheduler=UpperCamelCase__			,		melgan=UpperCamelCase__			,		)



				def 					__magic_name__							(      self    :    Tuple			,		UpperCamelCase__    :    Any			,		UpperCamelCase__    :    int=(-1.0, 1.0)			,		UpperCamelCase__    :    Tuple=False):






											'''simple docstring'''







											snake_case__  , snake_case__	  =       output_range
											if clip:
																		snake_case__	  =       torch.clip(UpperCamelCase__			,		self.min_value			,		self.max_value)
											# Scale to [0, 1].
											snake_case__	  =       (features - self.min_value) / (self.max_value - self.min_value)
											# Scale to [min_out, max_out].
											return zero_one * (max_out - min_out) + min_out



				def 					__magic_name__							(      self    :    Optional[Any]			,		UpperCamelCase__    :    str			,		UpperCamelCase__    :    List[str]=(-1.0, 1.0)			,		UpperCamelCase__    :    Optional[Any]=False):






											'''simple docstring'''







											snake_case__  , snake_case__	  =       input_range
											snake_case__	  =       torch.clip(UpperCamelCase__			,		UpperCamelCase__			,		UpperCamelCase__) if clip else outputs
											# Scale to [0, 1].
											snake_case__	  =       (outputs - min_out) / (max_out - min_out)
											# Scale to [self.min_value, self.max_value].
											return zero_one * (self.max_value - self.min_value) + self.min_value



				def 					__magic_name__							(      self    :    Optional[int]			,		UpperCamelCase__    :    Union[str, Any]			,		UpperCamelCase__    :    Optional[int]			,		UpperCamelCase__    :    Any):






											'''simple docstring'''







											snake_case__	  =       input_tokens > 0
											snake_case__  , snake_case__	  =       self.notes_encoder(
											    encoder_input_tokens=UpperCamelCase__			,		encoder_inputs_mask=UpperCamelCase__)

											snake_case__  , snake_case__	  =       self.continuous_encoder(
											    encoder_inputs=UpperCamelCase__			,		encoder_inputs_mask=UpperCamelCase__)

											return [(tokens_encoded, tokens_mask), (continuous_encoded, continuous_mask)]



				def 					__magic_name__							(      self    :    List[Any]			,		UpperCamelCase__    :    Dict			,		UpperCamelCase__    :    Any			,		UpperCamelCase__    :    int):






											'''simple docstring'''







											snake_case__	  =       noise_time
											if not torch.is_tensor(UpperCamelCase__):
																		snake_case__	  =       torch.tensor([timesteps]			,		dtype=torch.long			,		device=input_tokens.device)
											elif torch.is_tensor(UpperCamelCase__) and len(timesteps.shape) == 0:
																		snake_case__	  =       timesteps[None].to(input_tokens.device)

											# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
											snake_case__	  =       timesteps * torch.ones(input_tokens.shape[0]			,		dtype=timesteps.dtype			,		device=timesteps.device)

											snake_case__	  =       self.decoder(
											    encodings_and_masks=UpperCamelCase__			,		decoder_input_tokens=UpperCamelCase__			,		decoder_noise_time=UpperCamelCase__)
											return logits



				@torch.no_grad()
				def __call__(      self    :    Dict			,		UpperCamelCase__    :    List[List[int]]			,		UpperCamelCase__    :    Optional[torch.Generator] = None			,		UpperCamelCase__    :    int = 1_0_0			,		UpperCamelCase__    :    bool = True			,		UpperCamelCase__    :    str = "numpy"			,		UpperCamelCase__    :    Optional[Callable[[int, int, torch.FloatTensor], None]] = None			,		UpperCamelCase__    :    int = 1			,		):






											'''simple docstring'''







											if (callback_steps is None) or (
											    callback_steps is not None and (not isinstance(UpperCamelCase__			,		UpperCamelCase__) or callback_steps <= 0)
											):
																		raise ValueError(
																		    F'''`callback_steps` has to be a positive integer but is {callback_steps} of type'''
																		    F''' {type(UpperCamelCase__)}.''')

											snake_case__	  =       np.zeros([1, TARGET_FEATURE_LENGTH, self.n_dims]			,		dtype=np.floataa)
											snake_case__	  =       np.zeros([1, 0, self.n_dims]			,		np.floataa)
											snake_case__	  =       torch.ones((1, TARGET_FEATURE_LENGTH)			,		dtype=UpperCamelCase__			,		device=self.device)

											for i, encoder_input_tokens in enumerate(UpperCamelCase__):
																		if i == 0:
																									snake_case__	  =       torch.from_numpy(pred_mel[:1].copy()).to(
																									    device=self.device			,		dtype=self.decoder.dtype)
																									# The first chunk has no previous context.
																									snake_case__	  =       torch.zeros((1, TARGET_FEATURE_LENGTH)			,		dtype=UpperCamelCase__			,		device=self.device)
																		else:
																									# The full song pipeline does not feed in a context feature, so the mask
																									# will be all 0s after the feature converter. Because we know we're
																									# feeding in a full context chunk from the previous prediction, set it
																									# to all 1s.
																									snake_case__	  =       ones

																		snake_case__	  =       self.scale_features(
																		    UpperCamelCase__			,		output_range=[-1.0, 1.0]			,		clip=UpperCamelCase__)

																		snake_case__	  =       self.encode(
																		    input_tokens=torch.IntTensor([encoder_input_tokens]).to(device=self.device)			,		continuous_inputs=UpperCamelCase__			,		continuous_mask=UpperCamelCase__			,		)

																		# Sample encoder_continuous_inputs shaped gaussian noise to begin loop
																		snake_case__	  =       randn_tensor(
																		    shape=encoder_continuous_inputs.shape			,		generator=UpperCamelCase__			,		device=self.device			,		dtype=self.decoder.dtype			,		)

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

																		# Denoising diffusion loop
																		for j, t in enumerate(self.progress_bar(self.scheduler.timesteps)):
																									snake_case__	  =       self.decode(
																									    encodings_and_masks=UpperCamelCase__			,		input_tokens=UpperCamelCase__			,		noise_time=t / self.scheduler.config.num_train_timesteps			,		)

																									# Compute previous output: x_t -> x_t-1
																									snake_case__	  =       self.scheduler.step(UpperCamelCase__			,		UpperCamelCase__			,		UpperCamelCase__			,		generator=UpperCamelCase__).prev_sample

																		snake_case__	  =       self.scale_to_features(UpperCamelCase__			,		input_range=[-1.0, 1.0])
																		snake_case__	  =       mel[:1]
																		snake_case__	  =       mel.cpu().float().numpy()

																		snake_case__	  =       np.concatenate([full_pred_mel, pred_mel[:1]]			,		axis=1)

																		# call the callback, if provided
																		if callback is not None and i % callback_steps == 0:
																									callback(UpperCamelCase__			,		UpperCamelCase__)

																		logger.info("""Generated segment"""			,		UpperCamelCase__)

											if output_type == "numpy" and not is_onnx_available():
																		raise ValueError(
																		    """Cannot return output in 'np' format if ONNX is not available. Make sure to have ONNX installed or set 'output_type' to 'mel'.""")
											elif output_type == "numpy" and self.melgan is None:
																		raise ValueError(
																		    """Cannot return output in 'np' format if melgan component is not defined. Make sure to define `self.melgan` or set 'output_type' to 'mel'.""")

											if output_type == "numpy":
																		snake_case__	  =       self.melgan(input_features=full_pred_mel.astype(np.floataa))
											else:
																		snake_case__	  =       full_pred_mel

											if not return_dict:
																		return (output,)

											return AudioPipelineOutput(audios=UpperCamelCase__)
