






import math
import warnings
from dataclasses import dataclass
from typing import List, Optional, Tuple, Union

import numpy as np
import torch
from scipy import integrate

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin


@dataclass
# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->LMSDiscrete
class        lowercase__       (     _UpperCAmelCase		):
	a_             =42
	a_             =None





def     _a					(       UpperCamelCase_   :			Dict      ,				UpperCamelCase_   :			int=0.999      ,				UpperCamelCase_   :			str="cosine"      ,				)	->				Any:
						"""simple docstring"""







						if alpha_transform_type == "cosine":

												def alpha_bar_fn(UpperCamelCase_   :			Dict				):
																		return math.cos((t + 0.008) / 1.008 * math.pi / 2				) ** 2

						elif alpha_transform_type == "exp":

												def alpha_bar_fn(UpperCamelCase_   :			Union[str, Any]				):
																		return math.exp(t * -12.0				)

						else:
												raise ValueError(F"Unsupported alpha_tranform_type: {alpha_transform_type}"				)

						lowerCAmelCase__	 =  []
						for i in range(UpperCamelCase_				):
												lowerCAmelCase__	 =  i / num_diffusion_timesteps
												lowerCAmelCase__	 =  (i + 1) / num_diffusion_timesteps
												betas.append(min(1 - alpha_bar_fn(UpperCamelCase_				) / alpha_bar_fn(UpperCamelCase_				)      ,				UpperCamelCase_				)				)
						return torch.tensor(UpperCamelCase_      ,				dtype=torch.floataa				)





class        lowercase__       (     _UpperCAmelCase,   _UpperCAmelCase		):
	a_             =[e.name for e in KarrasDiffusionSchedulers]
	a_             =1

	@register_to_config
	def __init__(							self						,      __UpperCAmelCase = 1000						,      __UpperCAmelCase = 0.0_001						,      __UpperCAmelCase = 0.02						,      __UpperCAmelCase = "linear"						,      __UpperCAmelCase = None						,      __UpperCAmelCase = False						,      __UpperCAmelCase = "epsilon"						,      __UpperCAmelCase = "linspace"						,      __UpperCAmelCase = 0						,      )->   Union[str, Any]:







							'''simple docstring'''





							if trained_betas is not None:
													lowerCAmelCase__	 =  torch.tensor(__UpperCAmelCase						,      dtype=torch.floataa    )
							elif beta_schedule == "linear":
													lowerCAmelCase__	 =  torch.linspace(__UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase						,      dtype=torch.floataa    )
							elif beta_schedule == "scaled_linear":
													# this schedule is very specific to the latent diffusion model.
													lowerCAmelCase__	 =  (
													    torch.linspace(beta_start**0.5						,      beta_end**0.5						,      __UpperCAmelCase						,      dtype=torch.floataa    ) ** 2
													)
							elif beta_schedule == "squaredcos_cap_v2":
													# Glide cosine schedule
													lowerCAmelCase__	 =  betas_for_alpha_bar(__UpperCAmelCase    )
							else:
													raise NotImplementedError(F"{beta_schedule} does is not implemented for {self.__class__}"    )

							lowerCAmelCase__	 =  1.0 - self.betas
							lowerCAmelCase__	 =  torch.cumprod(self.alphas						,      dim=0    )

							lowerCAmelCase__	 =  np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5    )
							lowerCAmelCase__	 =  np.concatenate([sigmas[::-1], [0.0]]    ).astype(np.floataa    )
							lowerCAmelCase__	 =  torch.from_numpy(__UpperCAmelCase    )

							# setable values
							lowerCAmelCase__	 =  None
							lowerCAmelCase__	 =  use_karras_sigmas
							self.set_timesteps(__UpperCAmelCase						,      __UpperCAmelCase    )
							lowerCAmelCase__	 =  []
							lowerCAmelCase__	 =  False

	@property
	def 						UpperCAmelCase					(							self    )->   Tuple:







							'''simple docstring'''





							if self.config.timestep_spacing in ["linspace", "trailing"]:
													return self.sigmas.max()

							return (self.sigmas.max() ** 2 + 1) ** 0.5

	def 						UpperCAmelCase					(							self						,      __UpperCAmelCase						,      __UpperCAmelCase    )->   torch.FloatTensor:







							'''simple docstring'''





							if isinstance(__UpperCAmelCase						,      torch.Tensor    ):
													lowerCAmelCase__	 =  timestep.to(self.timesteps.device    )
							lowerCAmelCase__	 =  (self.timesteps == timestep).nonzero().item()
							lowerCAmelCase__	 =  self.sigmas[step_index]
							lowerCAmelCase__	 =  sample / ((sigma**2 + 1) ** 0.5)
							lowerCAmelCase__	 =  True
							return sample

	def 						UpperCAmelCase					(							self						,      __UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase    )->   Union[str, Any]:







							'''simple docstring'''





							def lms_derivative(__UpperCAmelCase    ):
													lowerCAmelCase__	 =  1.0
													for k in range(__UpperCAmelCase    ):
																			if current_order == k:
																									continue
																			prod *= (tau - self.sigmas[t - k]) / (self.sigmas[t - current_order] - self.sigmas[t - k])
													return prod

							lowerCAmelCase__	 =  integrate.quad(__UpperCAmelCase						,      self.sigmas[t]						,      self.sigmas[t + 1]						,      epsrel=1E-4    )[0]

							return integrated_coeff

	def 						UpperCAmelCase					(							self						,      __UpperCAmelCase						,      __UpperCAmelCase = None    )->   List[Any]:







							'''simple docstring'''





							lowerCAmelCase__	 =  num_inference_steps

							# "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891
							if self.config.timestep_spacing == "linspace":
													lowerCAmelCase__	 =  np.linspace(0						,      self.config.num_train_timesteps - 1						,      __UpperCAmelCase						,      dtype=__UpperCAmelCase    )[
													    ::-1
													].copy()
							elif self.config.timestep_spacing == "leading":
													lowerCAmelCase__	 =  self.config.num_train_timesteps // self.num_inference_steps
													# creates integer timesteps by multiplying by ratio
													# casting to int to avoid issues when num_inference_step is power of 3
													lowerCAmelCase__	 =  (np.arange(0						,      __UpperCAmelCase    ) * step_ratio).round()[::-1].copy().astype(__UpperCAmelCase    )
													timesteps += self.config.steps_offset
							elif self.config.timestep_spacing == "trailing":
													lowerCAmelCase__	 =  self.config.num_train_timesteps / self.num_inference_steps
													# creates integer timesteps by multiplying by ratio
													# casting to int to avoid issues when num_inference_step is power of 3
													lowerCAmelCase__	 =  (np.arange(self.config.num_train_timesteps						,      0						,      -step_ratio    )).round().copy().astype(__UpperCAmelCase    )
													timesteps -= 1
							else:
													raise ValueError(
													    F"{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'."    )

							lowerCAmelCase__	 =  np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5    )
							lowerCAmelCase__	 =  np.log(__UpperCAmelCase    )
							lowerCAmelCase__	 =  np.interp(__UpperCAmelCase						,      np.arange(0						,      len(__UpperCAmelCase    )    )						,      __UpperCAmelCase    )

							if self.use_karras_sigmas:
													lowerCAmelCase__	 =  self._convert_to_karras(in_sigmas=__UpperCAmelCase    )
													lowerCAmelCase__	 =  np.array([self._sigma_to_t(__UpperCAmelCase						,      __UpperCAmelCase    ) for sigma in sigmas]    )

							lowerCAmelCase__	 =  np.concatenate([sigmas, [0.0]]    ).astype(np.floataa    )

							lowerCAmelCase__	 =  torch.from_numpy(__UpperCAmelCase    ).to(device=__UpperCAmelCase    )
							if str(__UpperCAmelCase    ).startswith("mps"    ):
													# mps does not support float64
													lowerCAmelCase__	 =  torch.from_numpy(__UpperCAmelCase    ).to(__UpperCAmelCase						,      dtype=torch.floataa    )
							else:
													lowerCAmelCase__	 =  torch.from_numpy(__UpperCAmelCase    ).to(device=__UpperCAmelCase    )

							lowerCAmelCase__	 =  []

	def 						UpperCAmelCase					(							self						,      __UpperCAmelCase						,      __UpperCAmelCase    )->   Union[str, Any]:







							'''simple docstring'''





							lowerCAmelCase__	 =  np.log(__UpperCAmelCase    )

							# get distribution
							lowerCAmelCase__	 =  log_sigma - log_sigmas[:, np.newaxis]

							# get sigmas range
							lowerCAmelCase__	 =  np.cumsum((dists >= 0)						,      axis=0    ).argmax(axis=0    ).clip(max=log_sigmas.shape[0] - 2    )
							lowerCAmelCase__	 =  low_idx + 1

							lowerCAmelCase__	 =  log_sigmas[low_idx]
							lowerCAmelCase__	 =  log_sigmas[high_idx]

							# interpolate sigmas
							lowerCAmelCase__	 =  (low - log_sigma) / (low - high)
							lowerCAmelCase__	 =  np.clip(__UpperCAmelCase						,      0						,      1    )

							# transform interpolation to time range
							lowerCAmelCase__	 =  (1 - w) * low_idx + w * high_idx
							lowerCAmelCase__	 =  t.reshape(sigma.shape    )
							return t

	def 						UpperCAmelCase					(							self						,      __UpperCAmelCase    )->   torch.FloatTensor:







							'''simple docstring'''





							lowerCAmelCase__	 =  in_sigmas[-1].item()
							lowerCAmelCase__	 =  in_sigmas[0].item()

							lowerCAmelCase__	 =  7.0  # 7.0 is the value used in the paper
							lowerCAmelCase__	 =  np.linspace(0						,      1						,      self.num_inference_steps    )
							lowerCAmelCase__	 =  sigma_min ** (1 / rho)
							lowerCAmelCase__	 =  sigma_max ** (1 / rho)
							lowerCAmelCase__	 =  (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho
							return sigmas

	def 						UpperCAmelCase					(							self						,      __UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase = 4						,      __UpperCAmelCase = True						,      )->   Union[LMSDiscreteSchedulerOutput, Tuple]:







							'''simple docstring'''





							if not self.is_scale_input_called:
													warnings.warn(
													    "The `scale_model_input` function should be called before `step` to ensure correct denoising. "
													    "See `StableDiffusionPipeline` for a usage example."    )

							if isinstance(__UpperCAmelCase						,      torch.Tensor    ):
													lowerCAmelCase__	 =  timestep.to(self.timesteps.device    )
							lowerCAmelCase__	 =  (self.timesteps == timestep).nonzero().item()
							lowerCAmelCase__	 =  self.sigmas[step_index]

							# 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
							if self.config.prediction_type == "epsilon":
													lowerCAmelCase__	 =  sample - sigma * model_output
							elif self.config.prediction_type == "v_prediction":
													# * c_out + input * c_skip
													lowerCAmelCase__	 =  model_output * (-sigma / (sigma**2 + 1) ** 0.5) + (sample / (sigma**2 + 1))
							elif self.config.prediction_type == "sample":
													lowerCAmelCase__	 =  model_output
							else:
													raise ValueError(
													    F"prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`"    )

							# 2. Convert to an ODE derivative
							lowerCAmelCase__	 =  (sample - pred_original_sample) / sigma
							self.derivatives.append(__UpperCAmelCase    )
							if len(self.derivatives    ) > order:
													self.derivatives.pop(0    )

							# 3. Compute linear multistep coefficients
							lowerCAmelCase__	 =  min(step_index + 1						,      __UpperCAmelCase    )
							lowerCAmelCase__	 =  [self.get_lms_coefficient(__UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase    ) for curr_order in range(__UpperCAmelCase    )]

							# 4. Compute previous sample based on the derivatives path
							lowerCAmelCase__	 =  sample + sum(
							    coeff * derivative for coeff, derivative in zip(__UpperCAmelCase						,      reversed(self.derivatives    )    )    )

							if not return_dict:
													return (prev_sample,)

							return LMSDiscreteSchedulerOutput(prev_sample=__UpperCAmelCase						,      pred_original_sample=__UpperCAmelCase    )

	def 						UpperCAmelCase					(							self						,      __UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase						,      )->   torch.FloatTensor:







							'''simple docstring'''





							lowerCAmelCase__	 =  self.sigmas.to(device=original_samples.device						,      dtype=original_samples.dtype    )
							if original_samples.device.type == "mps" and torch.is_floating_point(__UpperCAmelCase    ):
													# mps does not support float64
													lowerCAmelCase__	 =  self.timesteps.to(original_samples.device						,      dtype=torch.floataa    )
													lowerCAmelCase__	 =  timesteps.to(original_samples.device						,      dtype=torch.floataa    )
							else:
													lowerCAmelCase__	 =  self.timesteps.to(original_samples.device    )
													lowerCAmelCase__	 =  timesteps.to(original_samples.device    )

							lowerCAmelCase__	 =  [(schedule_timesteps == t).nonzero().item() for t in timesteps]

							lowerCAmelCase__	 =  sigmas[step_indices].flatten()
							while len(sigma.shape    ) < len(original_samples.shape    ):
													lowerCAmelCase__	 =  sigma.unsqueeze(-1    )

							lowerCAmelCase__	 =  original_samples + noise * sigma
							return noisy_samples


	def __len__(							self    )->   Tuple:







							'''simple docstring'''





							return self.config.num_train_timesteps














import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


a_							=      logging.get_logger(__name__)

a_							=      {
    '''facebook/musicgen-small''': '''https://huggingface.co/facebook/musicgen-small/resolve/main/config.json''',
    # See all Musicgen models at https://huggingface.co/models?filter=musicgen
}


class        lowercase__       (     _UpperCAmelCase		):
	a_             ="""musicgen_decoder"""
	a_             =["""past_key_values"""]

	def __init__(							self						,      __UpperCAmelCase=2048						,      __UpperCAmelCase=2048						,      __UpperCAmelCase=24						,      __UpperCAmelCase=4096						,      __UpperCAmelCase=16						,      __UpperCAmelCase=0.0						,      __UpperCAmelCase=True						,      __UpperCAmelCase="gelu"						,      __UpperCAmelCase=1024						,      __UpperCAmelCase=0.1						,      __UpperCAmelCase=0.0						,      __UpperCAmelCase=0.0						,      __UpperCAmelCase=0.02						,      __UpperCAmelCase=False						,      __UpperCAmelCase=4						,      __UpperCAmelCase=2048						,      __UpperCAmelCase=2048						,      __UpperCAmelCase=None						,      __UpperCAmelCase=False						,      **__UpperCAmelCase						,      )->   Optional[Any]:







							'''simple docstring'''





							lowerCAmelCase__	 =  vocab_size
							lowerCAmelCase__	 =  max_position_embeddings
							lowerCAmelCase__	 =  hidden_size
							lowerCAmelCase__	 =  ffn_dim
							lowerCAmelCase__	 =  num_hidden_layers
							lowerCAmelCase__	 =  num_attention_heads
							lowerCAmelCase__	 =  dropout
							lowerCAmelCase__	 =  attention_dropout
							lowerCAmelCase__	 =  activation_dropout
							lowerCAmelCase__	 =  activation_function
							lowerCAmelCase__	 =  initializer_factor
							lowerCAmelCase__	 =  layerdrop
							lowerCAmelCase__	 =  use_cache
							lowerCAmelCase__	 =  scale_embedding  # scale factor will be sqrt(d_model) if True
							lowerCAmelCase__	 =  num_codebooks
							super().__init__(
							    pad_token_id=__UpperCAmelCase						,      bos_token_id=__UpperCAmelCase						,      eos_token_id=__UpperCAmelCase						,      tie_word_embeddings=__UpperCAmelCase						,      **__UpperCAmelCase						,      )





class        lowercase__       (     _UpperCAmelCase		):
	a_             ="""musicgen"""
	a_             =True

	def __init__(							self						,      **__UpperCAmelCase    )->   Any:







							'''simple docstring'''





							super().__init__(**__UpperCAmelCase    )
							if "text_encoder" not in kwargs or "audio_encoder" not in kwargs or "decoder" not in kwargs:
													raise ValueError("Config has to be initialized with text_encoder, audio_encoder and decoder config"    )

							lowerCAmelCase__	 =  kwargs.pop("text_encoder"    )
							lowerCAmelCase__	 =  text_encoder_config.pop("model_type"    )

							lowerCAmelCase__	 =  kwargs.pop("audio_encoder"    )
							lowerCAmelCase__	 =  audio_encoder_config.pop("model_type"    )

							lowerCAmelCase__	 =  kwargs.pop("decoder"    )

							lowerCAmelCase__	 =  AutoConfig.for_model(__UpperCAmelCase						,      **__UpperCAmelCase    )
							lowerCAmelCase__	 =  AutoConfig.for_model(__UpperCAmelCase						,      **__UpperCAmelCase    )
							lowerCAmelCase__	 =  MusicgenDecoderConfig(**__UpperCAmelCase    )
							lowerCAmelCase__	 =  True

	@classmethod
	def 						UpperCAmelCase					(							cls						,      __UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase						,      **__UpperCAmelCase						,      )->   Tuple:







							'''simple docstring'''





							return cls(
							    text_encoder=text_encoder_config.to_dict()						,      audio_encoder=audio_encoder_config.to_dict()						,      decoder=decoder_config.to_dict()						,      **__UpperCAmelCase						,      )


	def 						UpperCAmelCase					(							self    )->   Optional[int]:







							'''simple docstring'''





							lowerCAmelCase__	 =  copy.deepcopy(self.__dict__    )
							lowerCAmelCase__	 =  self.text_encoder.to_dict()
							lowerCAmelCase__	 =  self.audio_encoder.to_dict()
							lowerCAmelCase__	 =  self.decoder.to_dict()
							lowerCAmelCase__	 =  self.__class__.model_type
							return output






