






a__       =     [
    "VerificationMode",
    "Version",
    "disable_progress_bar",
    "enable_progress_bar",
    "is_progress_bar_enabled",
    "experimental",
]

from .info_utils import VerificationMode
from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled
from .version import Version
from .experimental import experimental










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__)
