






from typing import Optional

import torch
from PIL import Image
from tqdm.auto import tqdm
from transformers import CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, DiffusionPipeline, UNetaDConditionModel
from diffusers.image_processor import VaeImageProcessor
from diffusers.utils import (
    deprecate,
)


class        lowercase__       (     _UpperCAmelCase		):

	def __init__(							self						,      __UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase = 0.93						,      __UpperCAmelCase = True						,      )->   Optional[int]:







							'''simple docstring'''





							lowerCAmelCase__	 =  mixing_coeff
							lowerCAmelCase__	 =  leapfrog_steps

							super().__init__()
							self.register_modules(
							    vae=__UpperCAmelCase						,      text_encoder=__UpperCAmelCase						,      tokenizer=__UpperCAmelCase						,      unet=__UpperCAmelCase						,      scheduler=__UpperCAmelCase						,      )

							lowerCAmelCase__	 =  2 ** (len(self.vae.config.block_out_channels    ) - 1)
							lowerCAmelCase__	 =  VaeImageProcessor(vae_scale_factor=self.vae_scale_factor    )

	def 						UpperCAmelCase					(							self						,      __UpperCAmelCase						,      __UpperCAmelCase = None						,      __UpperCAmelCase = False    )->   str:







							'''simple docstring'''





							lowerCAmelCase__	 =  self.tokenizer(
							    __UpperCAmelCase						,      padding="max_length"						,      max_length=self.tokenizer.model_max_length						,      truncation=__UpperCAmelCase						,      return_tensors="pt"						,      )

							lowerCAmelCase__	 =  self.text_encoder(text_inputs.input_ids.to(self.device    )    ).last_hidden_state

							lowerCAmelCase__	 =  prompt_embeds.to(dtype=self.text_encoder.dtype						,      device=self.device    )

							if do_classifier_free_guidance:
													lowerCAmelCase__	 =  "" if negative_prompt is None else negative_prompt

													lowerCAmelCase__	 =  self.tokenizer(
													    __UpperCAmelCase						,      padding="max_length"						,      max_length=self.tokenizer.model_max_length						,      truncation=__UpperCAmelCase						,      return_tensors="pt"						,      )

													lowerCAmelCase__	 =  self.text_encoder(uncond_input.input_ids.to(self.device    )    ).last_hidden_state

													lowerCAmelCase__	 =  torch.cat([negative_prompt_embeds, prompt_embeds]    )

							return prompt_embeds

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







							'''simple docstring'''





							lowerCAmelCase__	 =  self.mixing_coeff * x + (1 - self.mixing_coeff) * y
							lowerCAmelCase__	 =  self.mixing_coeff * y + (1 - self.mixing_coeff) * x

							return [x, y]

	def 						UpperCAmelCase					(							self						,      __UpperCAmelCase						,      __UpperCAmelCase    )->   int:







							'''simple docstring'''





							lowerCAmelCase__	 =  (y - (1 - self.mixing_coeff) * x) / self.mixing_coeff
							lowerCAmelCase__	 =  (x - (1 - self.mixing_coeff) * y) / self.mixing_coeff

							return [x, y]

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







							'''simple docstring'''





							lowerCAmelCase__	 =  int(__UpperCAmelCase    )

							lowerCAmelCase__	 =  self.scheduler.alphas_cumprod[t] if t >= 0 else self.scheduler.final_alpha_cumprod

							return alpha_prod, 1 - alpha_prod

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







							'''simple docstring'''





							lowerCAmelCase__	 =  timestep - self.scheduler.config.num_train_timesteps / self.scheduler.num_inference_steps

							lowerCAmelCase__ ,							lowerCAmelCase__	 =  self._get_alpha_and_beta(__UpperCAmelCase    )
							lowerCAmelCase__ ,							lowerCAmelCase__	 =  self._get_alpha_and_beta(__UpperCAmelCase    )

							lowerCAmelCase__	 =  (alpha_prod_t_prev / alpha_prod_t) ** 0.5
							lowerCAmelCase__	 =  -a_t * (beta_prod_t**0.5) + beta_prod_t_prev**0.5

							lowerCAmelCase__	 =  (base - b_t * model_output) / a_t

							return model_input, next_model_input.to(base.dtype    )

	def 						UpperCAmelCase					(							self						,      __UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase						,      )->   Any:







							'''simple docstring'''





							lowerCAmelCase__	 =  timestep - self.scheduler.config.num_train_timesteps / self.scheduler.num_inference_steps

							lowerCAmelCase__ ,							lowerCAmelCase__	 =  self._get_alpha_and_beta(__UpperCAmelCase    )
							lowerCAmelCase__ ,							lowerCAmelCase__	 =  self._get_alpha_and_beta(__UpperCAmelCase    )

							lowerCAmelCase__	 =  (alpha_prod_t_prev / alpha_prod_t) ** 0.5
							lowerCAmelCase__	 =  -a_t * (beta_prod_t**0.5) + beta_prod_t_prev**0.5
							lowerCAmelCase__	 =  a_t * base + b_t * model_output

							return model_input, next_model_input.to(base.dtype    )

	@torch.no_grad()
	def 						UpperCAmelCase					(							self						,      __UpperCAmelCase    )->   str:







							'''simple docstring'''





							lowerCAmelCase__	 =  1 / self.vae.config.scaling_factor * latents
							lowerCAmelCase__	 =  self.vae.decode(__UpperCAmelCase    ).sample
							lowerCAmelCase__	 =  (image / 2 + 0.5).clamp(0						,      1    )
							return image

	@torch.no_grad()
	def 						UpperCAmelCase					(							self						,      __UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase = None						,      )->   Any:







							'''simple docstring'''





							lowerCAmelCase__	 =  guidance_scale > 1.0

							lowerCAmelCase__	 =  image.to(device=self.device						,      dtype=text_embeds.dtype    )
							lowerCAmelCase__	 =  self.vae.encode(__UpperCAmelCase    ).latent_dist.sample(__UpperCAmelCase    )

							lowerCAmelCase__	 =  self.vae.config.scaling_factor * latent

							lowerCAmelCase__	 =  [latent.clone(), latent.clone()]

							for i, t in tqdm(enumerate(__UpperCAmelCase    )						,      total=len(__UpperCAmelCase    )    ):
													lowerCAmelCase__	 =  self.noise_mixing_layer(x=coupled_latents[0]						,      y=coupled_latents[1]    )

													# j - model_input index, k - base index
													for j in range(2    ):
																			lowerCAmelCase__	 =  j ^ 1

																			if self.leapfrog_steps:
																									if i % 2 == 0:
																															lowerCAmelCase__ ,							lowerCAmelCase__	 =  j, k

																			lowerCAmelCase__	 =  coupled_latents[j]
																			lowerCAmelCase__	 =  coupled_latents[k]

																			lowerCAmelCase__	 =  torch.cat([model_input] * 2    ) if do_classifier_free_guidance else model_input

																			lowerCAmelCase__	 =  self.unet(__UpperCAmelCase						,      __UpperCAmelCase						,      encoder_hidden_states=__UpperCAmelCase    ).sample

																			if do_classifier_free_guidance:
																									lowerCAmelCase__ ,							lowerCAmelCase__	 =  noise_pred.chunk(2    )
																									lowerCAmelCase__	 =  noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

																			lowerCAmelCase__ ,							lowerCAmelCase__	 =  self.noise_step(
																			    base=__UpperCAmelCase						,      model_input=__UpperCAmelCase						,      model_output=__UpperCAmelCase						,      timestep=__UpperCAmelCase						,      )

																			lowerCAmelCase__	 =  model_input

							return coupled_latents


	@torch.no_grad()
	def __call__(							self						,      __UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase = 3.0						,      __UpperCAmelCase = 50						,      __UpperCAmelCase = 0.8						,      __UpperCAmelCase = None						,      __UpperCAmelCase = None						,      __UpperCAmelCase = "pil"						,      )->   int:







							'''simple docstring'''





							lowerCAmelCase__	 =  guidance_scale > 1.0

							lowerCAmelCase__	 =  self.image_processor.preprocess(__UpperCAmelCase    )

							lowerCAmelCase__	 =  self._encode_prompt(__UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase    )
							lowerCAmelCase__	 =  self._encode_prompt(__UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase    )

							self.scheduler.set_timesteps(__UpperCAmelCase						,      self.device    )

							lowerCAmelCase__	 =  num_inference_steps - int(num_inference_steps * strength    )
							lowerCAmelCase__	 =  self.scheduler.timesteps[t_limit:]
							lowerCAmelCase__	 =  fwd_timesteps.flip(0    )

							lowerCAmelCase__	 =  self.prepare_latents(__UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase    )

							for i, t in tqdm(enumerate(__UpperCAmelCase    )						,      total=len(__UpperCAmelCase    )    ):
													# j - model_input index, k - base index
													for k in range(2    ):
																			lowerCAmelCase__	 =  k ^ 1

																			if self.leapfrog_steps:
																									if i % 2 == 1:
																															lowerCAmelCase__ ,							lowerCAmelCase__	 =  j, k

																			lowerCAmelCase__	 =  coupled_latents[j]
																			lowerCAmelCase__	 =  coupled_latents[k]

																			lowerCAmelCase__	 =  torch.cat([model_input] * 2    ) if do_classifier_free_guidance else model_input

																			lowerCAmelCase__	 =  self.unet(__UpperCAmelCase						,      __UpperCAmelCase						,      encoder_hidden_states=__UpperCAmelCase    ).sample

																			if do_classifier_free_guidance:
																									lowerCAmelCase__ ,							lowerCAmelCase__	 =  noise_pred.chunk(2    )
																									lowerCAmelCase__	 =  noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

																			lowerCAmelCase__ ,							lowerCAmelCase__	 =  self.denoise_step(
																			    base=__UpperCAmelCase						,      model_input=__UpperCAmelCase						,      model_output=__UpperCAmelCase						,      timestep=__UpperCAmelCase						,      )

																			lowerCAmelCase__	 =  model_input

													lowerCAmelCase__	 =  self.denoise_mixing_layer(x=coupled_latents[0]						,      y=coupled_latents[1]    )

							# either one is fine
							lowerCAmelCase__	 =  coupled_latents[0]

							if output_type not in ["latent", "pt", "np", "pil"]:
													lowerCAmelCase__	 =  (
													    F"the output_type {output_type} is outdated. Please make sure to set it to one of these instead: "
													    "`pil`, `np`, `pt`, `latent`"
													)
													deprecate("Unsupported output_type"						,      "1.0.0"						,      __UpperCAmelCase						,      standard_warn=__UpperCAmelCase    )
													lowerCAmelCase__	 =  "np"

							if output_type == "latent":
													lowerCAmelCase__	 =  final_latent
							else:
													lowerCAmelCase__	 =  self.decode_latents(__UpperCAmelCase    )
													lowerCAmelCase__	 =  self.image_processor.postprocess(__UpperCAmelCase						,      output_type=__UpperCAmelCase    )

							return image














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






