






'''simple docstring'''







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			:    Dict					  =logging.get_logger(__name__)  # pylint: disable=invalid-name

_A			:    Dict					  =256

class _lowercase			(       _lowercase	):
							a          = ["""melgan"""]




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

														# From MELGAN
														lowerCamelCase__      :			str 						= math.log(1e-5  )  # Matches MelGAN training.
														lowerCamelCase__      :			List[str] 						= 4.0  # Largest value for most examples
														lowerCamelCase__      :			Tuple 						= 128

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




							def 	lowerCamelCase_       (							self:       str    ,							UpperCamelCase__:       Union[str, Any]    ,							UpperCamelCase__:       Optional[int]=(-1.0, 1.0)    ,							UpperCamelCase__:       Tuple=False  ):
														lowerCamelCase__						,	lowerCamelCase__      :			Any 						= output_range
														if clip:
																					lowerCamelCase__      :			Union[str, Any] 						= torch.clip(UpperCamelCase__    ,							self.min_value    ,							self.max_value  )
														# Scale to [0, 1].
														lowerCamelCase__      :			int 						= (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 	lowerCamelCase_       (							self:       str    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       int=(-1.0, 1.0)    ,							UpperCamelCase__:       List[str]=False  ):
														lowerCamelCase__						,	lowerCamelCase__      :			str 						= input_range
														lowerCamelCase__      :			Dict 						= torch.clip(UpperCamelCase__    ,							UpperCamelCase__    ,							UpperCamelCase__  ) if clip else outputs
														# Scale to [0, 1].
														lowerCamelCase__      :			List[Any] 						= (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 	lowerCamelCase_       (							self:       Optional[int]    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       Dict    ,							UpperCamelCase__:       Union[str, Any]  ):
														lowerCamelCase__      :			Any 						= input_tokens > 0
														lowerCamelCase__						,	lowerCamelCase__      :			List[str] 						= self.notes_encoder(
														    encoder_input_tokens=UpperCamelCase__    ,							encoder_inputs_mask=UpperCamelCase__  )

														lowerCamelCase__						,	lowerCamelCase__      :			Any 						= self.continuous_encoder(
														    encoder_inputs=UpperCamelCase__    ,							encoder_inputs_mask=UpperCamelCase__  )

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




							def 	lowerCamelCase_       (							self:       List[str]    ,							UpperCamelCase__:       Optional[int]    ,							UpperCamelCase__:       int    ,							UpperCamelCase__:       Tuple  ):
														lowerCamelCase__      :			Optional[Any] 						= noise_time
														if not torch.is_tensor(UpperCamelCase__  ):
																					lowerCamelCase__      :			Optional[Any] 						= torch.tensor([timesteps]    ,							dtype=torch.long    ,							device=input_tokens.device  )
														elif torch.is_tensor(UpperCamelCase__  ) and len(timesteps.shape  ) == 0:
																					lowerCamelCase__      :			Tuple 						= timesteps[None].to(input_tokens.device  )

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

														lowerCamelCase__      :			Tuple 						= self.decoder(
														    encodings_and_masks=UpperCamelCase__    ,							decoder_input_tokens=UpperCamelCase__    ,							decoder_noise_time=UpperCamelCase__  )
														return logits




							@torch.no_grad()
							def __call__(							self:       List[str]    ,							UpperCamelCase__:       List[List[int]]    ,							UpperCamelCase__:       Optional[torch.Generator] = None    ,							UpperCamelCase__:       int = 100    ,							UpperCamelCase__:       bool = True    ,							UpperCamelCase__:       str = "numpy"    ,							UpperCamelCase__:       Optional[Callable[[int, int, torch.FloatTensor], None]] = None    ,							UpperCamelCase__:       int = 1    ,							):
														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__  )}.'''  )

														lowerCamelCase__      :			str 						= np.zeros([1, TARGET_FEATURE_LENGTH, self.n_dims]    ,							dtype=np.floataa  )
														lowerCamelCase__      :			Any 						= np.zeros([1, 0, self.n_dims]    ,							np.floataa  )
														lowerCamelCase__      :			List[Any] 						= torch.ones((1, TARGET_FEATURE_LENGTH)    ,							dtype=UpperCamelCase__    ,							device=self.device  )

														for i, encoder_input_tokens in enumerate(UpperCamelCase__  ):
																					if i == 0:
																												lowerCamelCase__      :			Optional[Any] 						= torch.from_numpy(pred_mel[:1].copy()  ).to(
																												    device=self.device    ,							dtype=self.decoder.dtype  )
																												# The first chunk has no previous context.
																												lowerCamelCase__      :			List[Any] 						= 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.
																												lowerCamelCase__      :			Optional[int] 						= ones

																					lowerCamelCase__      :			Optional[int] 						= self.scale_features(
																					    UpperCamelCase__    ,							output_range=[-1.0, 1.0]    ,							clip=UpperCamelCase__  )

																					lowerCamelCase__      :			Optional[Any] 						= 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
																					lowerCamelCase__      :			List[str] 						= 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  )  ):
																												lowerCamelCase__      :			str 						= 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
																												lowerCamelCase__      :			Tuple 						= self.scheduler.step(UpperCamelCase__    ,							UpperCamelCase__    ,							UpperCamelCase__    ,							generator=UpperCamelCase__  ).prev_sample

																					lowerCamelCase__      :			Tuple 						= self.scale_to_features(UpperCamelCase__    ,							input_range=[-1.0, 1.0]  )
																					lowerCamelCase__      :			Optional[Any] 						= mel[:1]
																					lowerCamelCase__      :			int 						= mel.cpu().float().numpy()

																					lowerCamelCase__      :			Optional[Any] 						= 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":
																					lowerCamelCase__      :			Union[str, Any] 						= self.melgan(input_features=full_pred_mel.astype(np.floataa  )  )
														else:
																					lowerCamelCase__      :			str 						= full_pred_mel

														if not return_dict:
																					return (output,)

														return AudioPipelineOutput(audios=UpperCamelCase__  )









'''simple docstring'''







from ...configuration_utils import PretrainedConfig
from ...utils import logging


_A			:    Union[str, Any]					  =logging.get_logger(__name__)

_A			:    List[str]					  ={
    '''MIT/ast-finetuned-audioset-10-10-0.4593''': (
        '''https://huggingface.co/MIT/ast-finetuned-audioset-10-10-0.4593/resolve/main/config.json'''
    ),
}

class _lowercase			(       _lowercase	):
							a          = """audio-spectrogram-transformer"""




							def __init__(							self:       str    ,							UpperCamelCase__:       Any=768    ,							UpperCamelCase__:       Union[str, Any]=12    ,							UpperCamelCase__:       List[Any]=12    ,							UpperCamelCase__:       int=3_072    ,							UpperCamelCase__:       Optional[Any]="gelu"    ,							UpperCamelCase__:       Optional[int]=0.0    ,							UpperCamelCase__:       Tuple=0.0    ,							UpperCamelCase__:       Union[str, Any]=0.02    ,							UpperCamelCase__:       Dict=1e-12    ,							UpperCamelCase__:       List[str]=16    ,							UpperCamelCase__:       List[str]=True    ,							UpperCamelCase__:       Any=10    ,							UpperCamelCase__:       List[str]=10    ,							UpperCamelCase__:       Any=1_024    ,							UpperCamelCase__:       Optional[Any]=128    ,							**UpperCamelCase__:       Union[str, Any]    ,							):
														super().__init__(**UpperCamelCase__  )

														lowerCamelCase__      :			List[Any] 						= hidden_size
														lowerCamelCase__      :			int 						= num_hidden_layers
														lowerCamelCase__      :			List[str] 						= num_attention_heads
														lowerCamelCase__      :			Optional[int] 						= intermediate_size
														lowerCamelCase__      :			List[Any] 						= hidden_act
														lowerCamelCase__      :			List[Any] 						= hidden_dropout_prob
														lowerCamelCase__      :			str 						= attention_probs_dropout_prob
														lowerCamelCase__      :			Dict 						= initializer_range
														lowerCamelCase__      :			List[str] 						= layer_norm_eps
														lowerCamelCase__      :			List[Any] 						= patch_size
														lowerCamelCase__      :			List[str] 						= qkv_bias
														lowerCamelCase__      :			Dict 						= frequency_stride
														lowerCamelCase__      :			List[Any] 						= time_stride
														lowerCamelCase__      :			str 						= max_length
														lowerCamelCase__      :			Dict 						= num_mel_bins

