



"""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


snake_case_				     =       """base_with_context"""

def  _lowerCAmelCase      (   lowercase_		,	lowercase_						):
						UpperCAmelCase             = nn.Parameter(torch.FloatTensor(weights['token_embedder']['embedding']						)						)
						UpperCAmelCase             = nn.Parameter(
						    torch.FloatTensor(weights['Embed_0']['embedding']						)		,	requires_grad=lowercase_						)
						for lyr_num, lyr in enumerate(model.encoders						):
												UpperCAmelCase             = weights[F"""layers_{lyr_num}"""]
												UpperCAmelCase             = nn.Parameter(
												    torch.FloatTensor(ly_weight['pre_attention_layer_norm']['scale']						)						)

												UpperCAmelCase             = ly_weight['attention']
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(attention_weights['query']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(attention_weights['key']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(attention_weights['value']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(attention_weights['out']['kernel'].T						)						)

												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(ly_weight['pre_mlp_layer_norm']['scale']						)						)

												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_0']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_1']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wo']['kernel'].T						)						)

						UpperCAmelCase             = nn.Parameter(torch.FloatTensor(weights['encoder_norm']['scale']						)						)
						return model

def  _lowerCAmelCase      (   lowercase_		,	lowercase_						):
						UpperCAmelCase             = nn.Parameter(torch.FloatTensor(weights['input_proj']['kernel'].T						)						)

						UpperCAmelCase             = nn.Parameter(
						    torch.FloatTensor(weights['Embed_0']['embedding']						)		,	requires_grad=lowercase_						)

						for lyr_num, lyr in enumerate(model.encoders						):
												UpperCAmelCase             = weights[F"""layers_{lyr_num}"""]
												UpperCAmelCase             = ly_weight['attention']

												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(attention_weights['query']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(attention_weights['key']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(attention_weights['value']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(attention_weights['out']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(
												    torch.FloatTensor(ly_weight['pre_attention_layer_norm']['scale']						)						)

												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_0']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_1']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wo']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(ly_weight['pre_mlp_layer_norm']['scale']						)						)

						UpperCAmelCase             = nn.Parameter(torch.FloatTensor(weights['encoder_norm']['scale']						)						)

						return model

def  _lowerCAmelCase      (   lowercase_		,	lowercase_						):
						UpperCAmelCase             = nn.Parameter(torch.FloatTensor(weights['time_emb_dense0']['kernel'].T						)						)
						UpperCAmelCase             = nn.Parameter(torch.FloatTensor(weights['time_emb_dense1']['kernel'].T						)						)

						UpperCAmelCase             = nn.Parameter(
						    torch.FloatTensor(weights['Embed_0']['embedding']						)		,	requires_grad=lowercase_						)

						UpperCAmelCase             = nn.Parameter(
						    torch.FloatTensor(weights['continuous_inputs_projection']['kernel'].T						)						)

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

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

												UpperCAmelCase             = ly_weight['self_attention']
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(attention_weights['query']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(attention_weights['key']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(attention_weights['value']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(attention_weights['out']['kernel'].T						)						)

												UpperCAmelCase             = ly_weight['MultiHeadDotProductAttention_0']
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(attention_weights['query']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(attention_weights['key']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(attention_weights['value']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(attention_weights['out']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(
												    torch.FloatTensor(ly_weight['pre_cross_attention_layer_norm']['scale']						)						)

												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(ly_weight['pre_mlp_layer_norm']['scale']						)						)
												UpperCAmelCase             = nn.Parameter(
												    torch.FloatTensor(ly_weight['FiLMLayer_1']['DenseGeneral_0']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_0']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_1']['kernel'].T						)						)
												UpperCAmelCase             = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wo']['kernel'].T						)						)

						UpperCAmelCase             = nn.Parameter(torch.FloatTensor(weights['decoder_norm']['scale']						)						)

						UpperCAmelCase             = nn.Parameter(torch.FloatTensor(weights['spec_out_dense']['kernel'].T						)						)

						return model

def  _lowerCAmelCase      (   lowercase_						):
						UpperCAmelCase             = checkpoints.load_tax_checkpoint(args.checkpoint_path						)
						UpperCAmelCase             = jnp.tree_util.tree_map(onp.array		,	lowercase_						)

						UpperCAmelCase             = [
						    '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()',
						]

						UpperCAmelCase             = os.path.join(args.checkpoint_path		,	'..'		,	'config.gin'						)
						UpperCAmelCase             = inference.parse_training_gin_file(lowercase_		,	lowercase_						)
						UpperCAmelCase             = inference.InferenceModel(args.checkpoint_path		,	lowercase_						)

						UpperCAmelCase             = DDPMScheduler(beta_schedule='squaredcos_cap_v2'		,	variance_type='fixed_large'						)

						UpperCAmelCase             = 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'		,	)

						UpperCAmelCase             = 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'		,	)

						UpperCAmelCase             = 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		,	)

						UpperCAmelCase             = load_notes_encoder(ta_checkpoint['target']['token_encoder']		,	lowercase_						)
						UpperCAmelCase             = load_continuous_encoder(ta_checkpoint['target']['continuous_encoder']		,	lowercase_						)
						UpperCAmelCase             = load_decoder(ta_checkpoint['target']['decoder']		,	lowercase_						)

						UpperCAmelCase             = OnnxRuntimeModel.from_pretrained('kashif/soundstream_mel_decoder'						)

						UpperCAmelCase             = SpectrogramDiffusionPipeline(
						    notes_encoder=lowercase_		,	continuous_encoder=lowercase_		,	decoder=lowercase_		,	scheduler=lowercase_		,	melgan=lowercase_		,	)
						if args.save:
												pipe.save_pretrained(args.output_path						)


if __name__ == "__main__":
				snake_case_				     =       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.""",
				)
				snake_case_				     =       parser.parse_args()

				main(args)







import json
import multiprocessing
import os
import re
from collections import defaultdict

import torch
from accelerate import Accelerator
from accelerate.utils import set_seed
from arguments import HumanEvalArguments
from datasets import load_dataset, load_metric
from torch.utils.data import IterableDataset
from torch.utils.data.dataloader import DataLoader
from tqdm import tqdm

import transformers
from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList


__lowerCAmelCase			:     Optional[int]           =   ['\nclass', '\ndef', '\n#', '\n@', '\nprint', '\nif']




class        UpperCAmelCase_			( _A     ):


  '''simple docstring'''







  def __init__( self			: Union[str, Any]			,							UpperCamelCase__			: List[Any]			,							UpperCamelCase__			: Optional[int]			,							UpperCamelCase__			: Any=None			,							UpperCamelCase__			: Union[str, Any]=1    )			->	str:







    """simple docstring"""






    __magic_name__				     =       tokenizer
    __magic_name__				     =       dataset
    __magic_name__				     =       len(UpperCamelCase__    ) if n_tasks is None else n_tasks
    __magic_name__				     =       n_copies





  def __iter__( self			: List[Any]    )			->	List[str]:







    """simple docstring"""






    __magic_name__				     =       []
    for task in range(self.n_tasks    ):
      # without strip, the model generate commented codes ...
      prompts.append(self.tokenizer.eos_token + self.dataset[task]["""prompt"""].strip()    )
    __magic_name__				     =       self.tokenizer(UpperCamelCase__			,							padding=UpperCamelCase__			,							return_tensors="""pt"""    )
    for task in range(self.n_tasks    ):
      for _ in range(self.n_copies    ):
        yield {
            "ids": outputs.input_ids[task],
            "task_id": task,
            "input_len": outputs.attention_mask[task].sum(),
        }







class        UpperCAmelCase_			( _A     ):


  '''simple docstring'''







  def __init__( self			: Union[str, Any]			,							UpperCamelCase__			: Dict			,							UpperCamelCase__			: str			,							UpperCamelCase__			: str    )			->	List[str]:







    """simple docstring"""






    __magic_name__				     =       start_length
    __magic_name__				     =       eof_strings
    __magic_name__				     =       tokenizer





  def __call__( self			: List[Any]			,							UpperCamelCase__			: Tuple			,							UpperCamelCase__			: Optional[Any]			,							**UpperCamelCase__			: Optional[int]    )			->	List[str]:







    """simple docstring"""






    __magic_name__				     =       self.tokenizer.batch_decode(input_ids[:, self.start_length :]    )
    __magic_name__				     =       []
    for decoded_generation in decoded_generations:
      done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings    )    )
    return all(UpperCamelCase__    )







def a__    (       A_					):






  '''simple docstring'''





  __magic_name__				     =       re.split("""(%s)""" % """|""".join(A_					),     A_					)
  # last string should be ""
  return "".join(string_list[:-2]					)

def a__    (       A_,     A_,     A_,     A_,     A_,     A_=20,     **A_					):






  '''simple docstring'''





  __magic_name__				     =       defaultdict(A_					)  # dict of list of generated tokens
  for step, batch in tqdm(enumerate(A_					)					):
    with torch.no_grad():
      __magic_name__				     =       batch["""ids"""].shape[-1]
      __magic_name__				     =       accelerator.unwrap_model(A_					).generate(
          input_ids=batch["""ids"""][:, : batch["""input_len"""]],     num_return_sequences=A_,     **A_					)
      # each task is generated batch_size times
      __magic_name__				     =       batch["""task_id"""].repeat(A_					)
      __magic_name__				     =       accelerator.pad_across_processes(
          A_,     dim=1,     pad_index=tokenizer.pad_token_id					)

      __magic_name__     ,    __magic_name__				     =       accelerator.gather((generated_tokens, generated_tasks)					)
      __magic_name__				     =       generated_tokens.cpu().numpy()
      __magic_name__				     =       generated_tasks.cpu().numpy()

      for task, generated_tokens in zip(A_,     A_					):
        gen_token_dict[task].append(A_					)

  __magic_name__				     =       [[] for _ in range(A_					)]
  for task, generated_tokens in gen_token_dict.items():
    for s in generated_tokens:
      __magic_name__				     =       tokenizer.decode(A_,     skip_special_tokens=A_,     clean_up_tokenization_spaces=A_					)
      code_gens[task].append(remove_last_block(A_					)					)
  return code_gens

def a__    (       ):






  '''simple docstring'''





  __magic_name__				     =       HfArgumentParser(A_					)
  __magic_name__				     =       parser.parse_args()

  transformers.logging.set_verbosity_error()
  # enables code execution in code_eval metric
  __magic_name__				     =       args.HF_ALLOW_CODE_EVAL
  # make sure tokenizer plays nice with multiprocessing
  __magic_name__				     =       """false"""

  if args.num_workers is None:
    __magic_name__				     =       multiprocessing.cpu_count()

  # Use dataset load to feed to accelerate
  __magic_name__				     =       Accelerator()
  set_seed(args.seed,     device_specific=A_					)

  # Load model and tokenizer
  __magic_name__				     =       AutoTokenizer.from_pretrained(args.model_ckpt					)
  __magic_name__				     =       tokenizer.eos_token
  __magic_name__				     =       AutoModelForCausalLM.from_pretrained(args.model_ckpt					)

  # Generation settings
  __magic_name__				     =       {
      """do_sample""": args.do_sample,
      """temperature""": args.temperature,
      """max_new_tokens""": args.max_new_tokens,
      """top_p""": args.top_p,
      """top_k""": args.top_k,
      """stopping_criteria""": StoppingCriteriaList([EndOfFunctionCriteria(0,     A_,     A_					)]					),
  }

  # Load evaluation dataset and metric
  __magic_name__				     =       load_dataset("""openai_humaneval"""					)
  __magic_name__				     =       load_metric("""code_eval"""					)

  __magic_name__				     =       args.num_tasks if args.num_tasks is not None else len(human_eval["""test"""]					)
  __magic_name__				     =       args.n_samples // args.batch_size

  __magic_name__				     =       TokenizedDataset(A_,     human_eval["""test"""],     n_copies=A_,     n_tasks=A_					)
  # do not confuse args.batch_size, which is actually the num_return_sequences
  __magic_name__				     =       DataLoader(A_,     batch_size=1					)

  # Run a quick test to see if code evaluation is enabled
  try:
    __magic_name__				     =       code_eval_metric.compute(references=[""""""],     predictions=[[""""""]]					)
  except ValueError as exception:
    print(
        """Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL=\"1\"`"""
        """ flag to enable code evaluation."""					)
    raise exception

  __magic_name__     ,    __magic_name__				     =       accelerator.prepare(A_,     A_					)

  __magic_name__				     =       complete_code(
      A_,     A_,     A_,     A_,     n_tasks=A_,     batch_size=args.batch_size,     **A_,     )

  if accelerator.is_main_process:
    __magic_name__				     =       []

    for task in tqdm(range(A_					)					):
      __magic_name__				     =       human_eval["""test"""][task]["""test"""]
      __magic_name__				     =       f'''check({human_eval['test'][task]['entry_point']})'''
      references.append("""\n""" + test_func + """\n""" + entry_point					)

    # Evaluate completions with "code_eval" metric
    __magic_name__     ,    __magic_name__				     =       code_eval_metric.compute(
        references=A_,     predictions=A_,     num_workers=args.num_workers					)
    print(f'''Results: {pass_at_k}'''					)

    # Save results to json file
    with open(args.output_file,     """w"""					) as fp:
      json.dump(A_,     A_					)


# For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing
# https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script
if __name__ == "__main__":
    main()

