






'''simple docstring'''
import argparse
import os

import jax as jnp
import numpy as onp
import torch
import torch.nn as nn
from music_spectrogram_diffusion import inference
from tax import checkpoints

from diffusers import DDPMScheduler, OnnxRuntimeModel, SpectrogramDiffusionPipeline
from diffusers.pipelines.spectrogram_diffusion import SpectrogramContEncoder, SpectrogramNotesEncoder, TaFilmDecoder


a_					=	'base_with_context'





def 	_a(					UpperCamelCase__			:  Tuple,		UpperCamelCase__			:  Tuple							):




      '''simple docstring'''






      SCREAMING_SNAKE_CASE__       :			Any													=nn.Parameter(torch.FloatTensor(weights['''token_embedder''']['''embedding''']							)							)
      SCREAMING_SNAKE_CASE__       :			Optional[int]													=nn.Parameter(
          torch.FloatTensor(weights['''Embed_0''']['''embedding''']							),		requires_grad=UpperCamelCase__							)
      for lyr_num, lyr in enumerate(model.encoders							):
            SCREAMING_SNAKE_CASE__       :			List[str]													=weights[f"layers_{lyr_num}"]
            SCREAMING_SNAKE_CASE__       :			List[str]													=nn.Parameter(
                torch.FloatTensor(ly_weight['''pre_attention_layer_norm''']['''scale''']							)							)

            SCREAMING_SNAKE_CASE__       :			int													=ly_weight['''attention''']
            SCREAMING_SNAKE_CASE__       :			List[str]													=nn.Parameter(torch.FloatTensor(attention_weights['''query''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			Any													=nn.Parameter(torch.FloatTensor(attention_weights['''key''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			List[str]													=nn.Parameter(torch.FloatTensor(attention_weights['''value''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			List[Any]													=nn.Parameter(torch.FloatTensor(attention_weights['''out''']['''kernel'''].T							)							)

            SCREAMING_SNAKE_CASE__       :			Optional[Any]													=nn.Parameter(torch.FloatTensor(ly_weight['''pre_mlp_layer_norm''']['''scale''']							)							)

            SCREAMING_SNAKE_CASE__       :			Any													=nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wi_0''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			Tuple													=nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wi_1''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			List[Any]													=nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wo''']['''kernel'''].T							)							)

      SCREAMING_SNAKE_CASE__       :			Optional[int]													=nn.Parameter(torch.FloatTensor(weights['''encoder_norm''']['''scale''']							)							)
      return model





def 	_a(					UpperCamelCase__			:  int,		UpperCamelCase__			:  List[str]							):




      '''simple docstring'''






      SCREAMING_SNAKE_CASE__       :			Dict													=nn.Parameter(torch.FloatTensor(weights['''input_proj''']['''kernel'''].T							)							)

      SCREAMING_SNAKE_CASE__       :			Tuple													=nn.Parameter(
          torch.FloatTensor(weights['''Embed_0''']['''embedding''']							),		requires_grad=UpperCamelCase__							)

      for lyr_num, lyr in enumerate(model.encoders							):
            SCREAMING_SNAKE_CASE__       :			Tuple													=weights[f"layers_{lyr_num}"]
            SCREAMING_SNAKE_CASE__       :			Any													=ly_weight['''attention''']

            SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=nn.Parameter(torch.FloatTensor(attention_weights['''query''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			Tuple													=nn.Parameter(torch.FloatTensor(attention_weights['''key''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			Any													=nn.Parameter(torch.FloatTensor(attention_weights['''value''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			Optional[Any]													=nn.Parameter(torch.FloatTensor(attention_weights['''out''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			List[str]													=nn.Parameter(
                torch.FloatTensor(ly_weight['''pre_attention_layer_norm''']['''scale''']							)							)

            SCREAMING_SNAKE_CASE__       :			List[Any]													=nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wi_0''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			str													=nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wi_1''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			int													=nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wo''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			int													=nn.Parameter(torch.FloatTensor(ly_weight['''pre_mlp_layer_norm''']['''scale''']							)							)

      SCREAMING_SNAKE_CASE__       :			Dict													=nn.Parameter(torch.FloatTensor(weights['''encoder_norm''']['''scale''']							)							)

      return model





def 	_a(					UpperCamelCase__			:  List[Any],		UpperCamelCase__			:  Dict							):




      '''simple docstring'''






      SCREAMING_SNAKE_CASE__       :			List[Any]													=nn.Parameter(torch.FloatTensor(weights['''time_emb_dense0''']['''kernel'''].T							)							)
      SCREAMING_SNAKE_CASE__       :			Optional[Any]													=nn.Parameter(torch.FloatTensor(weights['''time_emb_dense1''']['''kernel'''].T							)							)

      SCREAMING_SNAKE_CASE__       :			str													=nn.Parameter(
          torch.FloatTensor(weights['''Embed_0''']['''embedding''']							),		requires_grad=UpperCamelCase__							)

      SCREAMING_SNAKE_CASE__       :			List[str]													=nn.Parameter(
          torch.FloatTensor(weights['''continuous_inputs_projection''']['''kernel'''].T							)							)

      for lyr_num, lyr in enumerate(model.decoders							):
            SCREAMING_SNAKE_CASE__       :			int													=weights[f"layers_{lyr_num}"]
            SCREAMING_SNAKE_CASE__       :			int													=nn.Parameter(
                torch.FloatTensor(ly_weight['''pre_self_attention_layer_norm''']['''scale''']							)							)

            SCREAMING_SNAKE_CASE__       :			Tuple													=nn.Parameter(
                torch.FloatTensor(ly_weight['''FiLMLayer_0''']['''DenseGeneral_0''']['''kernel'''].T							)							)

            SCREAMING_SNAKE_CASE__       :			Optional[Any]													=ly_weight['''self_attention''']
            SCREAMING_SNAKE_CASE__       :			Tuple													=nn.Parameter(torch.FloatTensor(attention_weights['''query''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			Tuple													=nn.Parameter(torch.FloatTensor(attention_weights['''key''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			Optional[int]													=nn.Parameter(torch.FloatTensor(attention_weights['''value''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			int													=nn.Parameter(torch.FloatTensor(attention_weights['''out''']['''kernel'''].T							)							)

            SCREAMING_SNAKE_CASE__       :			Optional[Any]													=ly_weight['''MultiHeadDotProductAttention_0''']
            SCREAMING_SNAKE_CASE__       :			str													=nn.Parameter(torch.FloatTensor(attention_weights['''query''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			List[Any]													=nn.Parameter(torch.FloatTensor(attention_weights['''key''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			Any													=nn.Parameter(torch.FloatTensor(attention_weights['''value''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			int													=nn.Parameter(torch.FloatTensor(attention_weights['''out''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=nn.Parameter(
                torch.FloatTensor(ly_weight['''pre_cross_attention_layer_norm''']['''scale''']							)							)

            SCREAMING_SNAKE_CASE__       :			Dict													=nn.Parameter(torch.FloatTensor(ly_weight['''pre_mlp_layer_norm''']['''scale''']							)							)
            SCREAMING_SNAKE_CASE__       :			Dict													=nn.Parameter(
                torch.FloatTensor(ly_weight['''FiLMLayer_1''']['''DenseGeneral_0''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			List[Any]													=nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wi_0''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			Optional[int]													=nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wi_1''']['''kernel'''].T							)							)
            SCREAMING_SNAKE_CASE__       :			List[Any]													=nn.Parameter(torch.FloatTensor(ly_weight['''mlp''']['''wo''']['''kernel'''].T							)							)

      SCREAMING_SNAKE_CASE__       :			int													=nn.Parameter(torch.FloatTensor(weights['''decoder_norm''']['''scale''']							)							)

      SCREAMING_SNAKE_CASE__       :			Tuple													=nn.Parameter(torch.FloatTensor(weights['''spec_out_dense''']['''kernel'''].T							)							)

      return model





def 	_a(					UpperCamelCase__			:  Union[str, Any]							):




      '''simple docstring'''






      SCREAMING_SNAKE_CASE__       :			int													=checkpoints.load_tax_checkpoint(args.checkpoint_path							)
      SCREAMING_SNAKE_CASE__       :			Any													=jnp.tree_util.tree_map(onp.array,		UpperCamelCase__							)

      SCREAMING_SNAKE_CASE__       :			Any													=[
          '''from __gin__ import dynamic_registration''',
          '''from music_spectrogram_diffusion.models.diffusion import diffusion_utils''',
          '''diffusion_utils.ClassifierFreeGuidanceConfig.eval_condition_weight = 2.0''',
          '''diffusion_utils.DiffusionConfig.classifier_free_guidance = @diffusion_utils.ClassifierFreeGuidanceConfig()''',
      ]

      SCREAMING_SNAKE_CASE__       :			List[str]													=os.path.join(args.checkpoint_path,		'''..''',		'''config.gin'''							)
      SCREAMING_SNAKE_CASE__       :			Tuple													=inference.parse_training_gin_file(UpperCamelCase__,		UpperCamelCase__							)
      SCREAMING_SNAKE_CASE__       :			Optional[int]													=inference.InferenceModel(args.checkpoint_path,		UpperCamelCase__							)

      SCREAMING_SNAKE_CASE__       :			Optional[int]													=DDPMScheduler(beta_schedule='''squaredcos_cap_v2''',		variance_type='''fixed_large'''							)

      SCREAMING_SNAKE_CASE__       :			str													=SpectrogramNotesEncoder(
          max_length=synth_model.sequence_length['''inputs'''],		vocab_size=synth_model.model.module.config.vocab_size,		d_model=synth_model.model.module.config.emb_dim,		dropout_rate=synth_model.model.module.config.dropout_rate,		num_layers=synth_model.model.module.config.num_encoder_layers,		num_heads=synth_model.model.module.config.num_heads,		d_kv=synth_model.model.module.config.head_dim,		d_ff=synth_model.model.module.config.mlp_dim,		feed_forward_proj='''gated-gelu''',		)

      SCREAMING_SNAKE_CASE__       :			List[Any]													=SpectrogramContEncoder(
          input_dims=synth_model.audio_codec.n_dims,		targets_context_length=synth_model.sequence_length['''targets_context'''],		d_model=synth_model.model.module.config.emb_dim,		dropout_rate=synth_model.model.module.config.dropout_rate,		num_layers=synth_model.model.module.config.num_encoder_layers,		num_heads=synth_model.model.module.config.num_heads,		d_kv=synth_model.model.module.config.head_dim,		d_ff=synth_model.model.module.config.mlp_dim,		feed_forward_proj='''gated-gelu''',		)

      SCREAMING_SNAKE_CASE__       :			Optional[Any]													=TaFilmDecoder(
          input_dims=synth_model.audio_codec.n_dims,		targets_length=synth_model.sequence_length['''targets_context'''],		max_decoder_noise_time=synth_model.model.module.config.max_decoder_noise_time,		d_model=synth_model.model.module.config.emb_dim,		num_layers=synth_model.model.module.config.num_decoder_layers,		num_heads=synth_model.model.module.config.num_heads,		d_kv=synth_model.model.module.config.head_dim,		d_ff=synth_model.model.module.config.mlp_dim,		dropout_rate=synth_model.model.module.config.dropout_rate,		)

      SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=load_notes_encoder(ta_checkpoint['''target''']['''token_encoder'''],		UpperCamelCase__							)
      SCREAMING_SNAKE_CASE__       :			Dict													=load_continuous_encoder(ta_checkpoint['''target''']['''continuous_encoder'''],		UpperCamelCase__							)
      SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=load_decoder(ta_checkpoint['''target''']['''decoder'''],		UpperCamelCase__							)

      SCREAMING_SNAKE_CASE__       :			List[str]													=OnnxRuntimeModel.from_pretrained('''kashif/soundstream_mel_decoder'''							)

      SCREAMING_SNAKE_CASE__       :			Optional[int]													=SpectrogramDiffusionPipeline(
          notes_encoder=UpperCamelCase__,		continuous_encoder=UpperCamelCase__,		decoder=UpperCamelCase__,		scheduler=UpperCamelCase__,		melgan=UpperCamelCase__,		)
      if args.save:
            pipe.save_pretrained(args.output_path							)


if __name__ == "__main__":
  a_					=	argparse.ArgumentParser()

  parser.add_argument('--output_path', default=None, type=str, required=True, help='Path to the converted model.')
  parser.add_argument(
      '--save', default=True, type=bool, required=False, help='Whether to save the converted model or not.'
  )
  parser.add_argument(
      '--checkpoint_path',
      default=F'''{MODEL}/checkpoint_500000''',
      type=str,
      required=False,
      help='Path to the original jax model checkpoint.',
  )
  a_					=	parser.parse_args()

  main(args)







'''simple docstring'''
import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionSAGPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()






class        __SCREAMING_SNAKE_CASE (     lowerCamelCase		,			lowerCamelCase		,			unittest.TestCase			):
 snake_case_												=							StableDiffusionSAGPipeline
 snake_case_												=							TEXT_TO_IMAGE_PARAMS
 snake_case_												=							TEXT_TO_IMAGE_BATCH_PARAMS
 snake_case_												=							TEXT_TO_IMAGE_IMAGE_PARAMS
 snake_case_												=							TEXT_TO_IMAGE_IMAGE_PARAMS
 snake_case_												=							False




 def        __magic_name__		( self		:							str )		->  Union[str, Any]:
       torch.manual_seed(0 )
       SCREAMING_SNAKE_CASE__       :			Tuple													=UNetaDConditionModel(
           block_out_channels=(32, 64)   , layers_per_block=2   , sample_size=32   , in_channels=4   , out_channels=4   , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')   , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')   , cross_attention_dim=32   , )
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=DDIMScheduler(
           beta_start=0.00085   , beta_end=0.012   , beta_schedule='''scaled_linear'''   , clip_sample=__lowercase   , set_alpha_to_one=__lowercase   , )
       torch.manual_seed(0 )
       SCREAMING_SNAKE_CASE__       :			List[Any]													=AutoencoderKL(
           block_out_channels=[32, 64]   , in_channels=3   , out_channels=3   , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']   , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']   , latent_channels=4   , )
       torch.manual_seed(0 )
       SCREAMING_SNAKE_CASE__       :			List[Any]													=CLIPTextConfig(
           bos_token_id=0   , eos_token_id=2   , hidden_size=32   , intermediate_size=37   , layer_norm_eps=1e-05   , num_attention_heads=4   , num_hidden_layers=5   , pad_token_id=1   , vocab_size=10_00   , )
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=CLIPTextModel(__lowercase )
       SCREAMING_SNAKE_CASE__       :			List[str]													=CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' )

       SCREAMING_SNAKE_CASE__       :			List[Any]													={
           '''unet''': unet,
           '''scheduler''': scheduler,
           '''vae''': vae,
           '''text_encoder''': text_encoder,
           '''tokenizer''': tokenizer,
           '''safety_checker''': None,
           '''feature_extractor''': None,
       }
       return components




 def        __magic_name__		( self		:							int   , __lowercase		:							Union[str, Any]   , __lowercase		:							Any=0 )		->  Optional[Any]:
       if str(__lowercase ).startswith('''mps''' ):
             SCREAMING_SNAKE_CASE__       :			Optional[int]													=torch.manual_seed(__lowercase )
       else:
             SCREAMING_SNAKE_CASE__       :			Optional[int]													=torch.Generator(device=__lowercase ).manual_seed(__lowercase )
       SCREAMING_SNAKE_CASE__       :			Dict													={
           '''prompt''': '''.''',
           '''generator''': generator,
           '''num_inference_steps''': 2,
           '''guidance_scale''': 1.0,
           '''sag_scale''': 1.0,
           '''output_type''': '''numpy''',
       }
       return inputs




 def        __magic_name__		( self		:							int )		->  str:
       super().test_inference_batch_single_identical(expected_max_diff=3e-3 )






@slow
@require_torch_gpu
class        __SCREAMING_SNAKE_CASE (     unittest.TestCase			):




 def        __magic_name__		( self		:							int )		->  Optional[int]:
       # clean up the VRAM after each test
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()




 def        __magic_name__		( self		:							int )		->  Optional[Any]:
       SCREAMING_SNAKE_CASE__       :			Any													=StableDiffusionSAGPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4''' )
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=sag_pipe.to(__lowercase )
       sag_pipe.set_progress_bar_config(disable=__lowercase )

       SCREAMING_SNAKE_CASE__       :			List[Any]													='''.'''
       SCREAMING_SNAKE_CASE__       :			Tuple													=torch.manual_seed(0 )
       SCREAMING_SNAKE_CASE__       :			List[Any]													=sag_pipe(
           [prompt]   , generator=__lowercase   , guidance_scale=7.5   , sag_scale=1.0   , num_inference_steps=20   , output_type='''np''' )

       SCREAMING_SNAKE_CASE__       :			int													=output.images

       SCREAMING_SNAKE_CASE__       :			int													=image[0, -3:, -3:, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       SCREAMING_SNAKE_CASE__       :			str													=np.array([0.1568, 0.1738, 0.1695, 0.1693, 0.1507, 0.1705, 0.1547, 0.1751, 0.1949] )

       assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-2




 def        __magic_name__		( self		:							List[Any] )		->  Any:
       SCREAMING_SNAKE_CASE__       :			Tuple													=StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' )
       SCREAMING_SNAKE_CASE__       :			List[Any]													=sag_pipe.to(__lowercase )
       sag_pipe.set_progress_bar_config(disable=__lowercase )

       SCREAMING_SNAKE_CASE__       :			List[str]													='''.'''
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=torch.manual_seed(0 )
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=sag_pipe(
           [prompt]   , generator=__lowercase   , guidance_scale=7.5   , sag_scale=1.0   , num_inference_steps=20   , output_type='''np''' )

       SCREAMING_SNAKE_CASE__       :			Tuple													=output.images

       SCREAMING_SNAKE_CASE__       :			Tuple													=image[0, -3:, -3:, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       SCREAMING_SNAKE_CASE__       :			List[Any]													=np.array([0.3459, 0.2876, 0.2537, 0.3002, 0.2671, 0.2160, 0.3026, 0.2262, 0.2371] )

       assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-2




 def        __magic_name__		( self		:							str )		->  Any:
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' )
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=sag_pipe.to(__lowercase )
       sag_pipe.set_progress_bar_config(disable=__lowercase )

       SCREAMING_SNAKE_CASE__       :			List[Any]													='''.'''
       SCREAMING_SNAKE_CASE__       :			Dict													=torch.manual_seed(0 )
       SCREAMING_SNAKE_CASE__       :			Tuple													=sag_pipe(
           [prompt]   , width=7_68   , height=5_12   , generator=__lowercase   , guidance_scale=7.5   , sag_scale=1.0   , num_inference_steps=20   , output_type='''np'''   , )

       SCREAMING_SNAKE_CASE__       :			Any													=output.images

       assert image.shape == (1, 5_12, 7_68, 3)