






import argparse
from pathlib import Path
from typing import Dict, OrderedDict, Tuple

import torch
from audiocraft.models import MusicGen

from transformers import (
    AutoFeatureExtractor,
    AutoTokenizer,
    EncodecModel,
    MusicgenDecoderConfig,
    MusicgenForConditionalGeneration,
    MusicgenProcessor,
    TaEncoderModel,
)
from transformers.models.musicgen.modeling_musicgen import MusicgenForCausalLM
from transformers.utils import logging


logging.set_verbosity_info()
SCREAMING_SNAKE_CASE__				     =			logging.get_logger(__name__)


SCREAMING_SNAKE_CASE__				     =			['''model.decoder.embed_positions.weights''']

def        UpperCAmelCase__		(						lowerCamelCase_       :       Tuple				):
			if "emb" in name:
						__a						:  Any									=     name.replace('emb'   ,  'model.decoder.embed_tokens'				)
			if "transformer" in name:
						__a						:  str									=     name.replace('transformer'   ,  'model.decoder'				)
			if "cross_attention" in name:
						__a						:  List[Any]									=     name.replace('cross_attention'   ,  'encoder_attn'				)
			if "linear1" in name:
						__a						:  List[Any]									=     name.replace('linear1'   ,  'fc1'				)
			if "linear2" in name:
						__a						:  List[str]									=     name.replace('linear2'   ,  'fc2'				)
			if "norm1" in name:
						__a						:  List[str]									=     name.replace('norm1'   ,  'self_attn_layer_norm'				)
			if "norm_cross" in name:
						__a						:  List[Any]									=     name.replace('norm_cross'   ,  'encoder_attn_layer_norm'				)
			if "norm2" in name:
						__a						:  str									=     name.replace('norm2'   ,  'final_layer_norm'				)
			if "out_norm" in name:
						__a						:  int									=     name.replace('out_norm'   ,  'model.decoder.layer_norm'				)
			if "linears" in name:
						__a						:  Any									=     name.replace('linears'   ,  'lm_heads'				)
			if "condition_provider.conditioners.description.output_proj" in name:
						__a						:  List[Any]									=     name.replace('condition_provider.conditioners.description.output_proj'   ,  'enc_to_dec_proj'				)
			return name

def        UpperCAmelCase__		(						lowerCamelCase_       :       OrderedDict   ,  lowerCamelCase_       :       int				):
			__a						:  Union[str, Any]									=     list(state_dict.keys()				)
			__a						:  Optional[int]									=     {}
			for key in keys:
						__a						:  Optional[int]									=     state_dict.pop(lowerCamelCase_				)
						__a						:  List[Any]									=     rename_keys(lowerCamelCase_				)
						if "in_proj_weight" in key:
									# split fused qkv proj
									__a						:  Optional[Any]									=     val[:hidden_size, :]
									__a						:  Optional[Any]									=     val[hidden_size : 2 * hidden_size, :]
									__a						:  str									=     val[-hidden_size:, :]
						elif "enc_to_dec_proj" in key:
									__a						:  List[str]									=     val
						else:
									__a						:  Any									=     val
			return state_dict, enc_dec_proj_state_dict

def        UpperCAmelCase__		(						lowerCamelCase_       :       str				):
			if checkpoint == "small":
						# default config values
						__a						:  Union[str, Any]									=     1_0_2_4
						__a						:  Any									=     2_4
						__a						:  Tuple									=     1_6
			elif checkpoint == "medium":
						__a						:  Dict									=     1_5_3_6
						__a						:  Dict									=     4_8
						__a						:  Union[str, Any]									=     2_4
			elif checkpoint == "large":
						__a						:  int									=     2_0_4_8
						__a						:  Dict									=     4_8
						__a						:  Union[str, Any]									=     3_2
			else:
						raise ValueError(f'''Checkpoint should be one of `[\'small\', \'medium\', \'large\']`, got {checkpoint}.'''				)
			__a						:  str									=     MusicgenDecoderConfig(
			    hidden_size=lowerCamelCase_   ,  ffn_dim=hidden_size * 4   ,  num_hidden_layers=lowerCamelCase_   ,  num_attention_heads=lowerCamelCase_   ,  )
			return config

@torch.no_grad()
def        UpperCAmelCase__		(						lowerCamelCase_       :       Tuple   ,  lowerCamelCase_       :       Optional[Any]=None   ,  lowerCamelCase_       :       int=None   ,  lowerCamelCase_       :       List[Any]="cpu"				):
			__a						:  str									=     MusicGen.get_pretrained(lowerCamelCase_   ,  device=lowerCamelCase_				)
			__a						:  str									=     decoder_config_from_checkpoint(lowerCamelCase_				)

			__a						:  Tuple									=     fairseq_model.lm.state_dict()
			__a						,					__a						:  int									=     rename_state_dict(
			    lowerCamelCase_   ,  hidden_size=decoder_config.hidden_size				)

			__a						:  int									=     TaEncoderModel.from_pretrained('t5-base'				)
			__a						:  List[Any]									=     EncodecModel.from_pretrained('facebook/encodec_32khz'				)
			__a						:  Tuple									=     MusicgenForCausalLM(lowerCamelCase_				).eval()

			# load all decoder weights - expect that we'll be missing embeddings and enc-dec projection
			__a						,					__a						:  Optional[int]									=     decoder.load_state_dict(lowerCamelCase_   ,  strict=lowerCamelCase_				)

			for key in missing_keys.copy():
						if key.startswith(('text_encoder', 'audio_encoder')				) or key in EXPECTED_MISSING_KEYS:
									missing_keys.remove(lowerCamelCase_				)

			if len(lowerCamelCase_				) > 0:
						raise ValueError(f'''Missing key(s) in state_dict: {missing_keys}'''				)

			if len(lowerCamelCase_				) > 0:
						raise ValueError(f'''Unexpected key(s) in state_dict: {unexpected_keys}'''				)

			# init the composite model
			__a						:  Union[str, Any]									=     MusicgenForConditionalGeneration(text_encoder=lowerCamelCase_   ,  audio_encoder=lowerCamelCase_   ,  decoder=lowerCamelCase_				)

			# load the pre-trained enc-dec projection (from the decoder state dict)
			model.enc_to_dec_proj.load_state_dict(lowerCamelCase_				)

			# check we can do a forward pass
			__a						:  Any									=     torch.arange(0   ,  8   ,  dtype=torch.long				).reshape(2   ,  -1				)
			__a						:  Tuple									=     input_ids.reshape(2 * 4   ,  -1				)

			with torch.no_grad():
						__a						:  Any									=     model(input_ids=lowerCamelCase_   ,  decoder_input_ids=lowerCamelCase_				).logits

			if logits.shape != (8, 1, 2_0_4_8):
						raise ValueError('Incorrect shape for logits'				)

			# now construct the processor
			__a						:  Any									=     AutoTokenizer.from_pretrained('t5-base'				)
			__a						:  Optional[Any]									=     AutoFeatureExtractor.from_pretrained('facebook/encodec_32khz'   ,  padding_side='left'				)

			__a						:  Union[str, Any]									=     MusicgenProcessor(feature_extractor=lowerCamelCase_   ,  tokenizer=lowerCamelCase_				)

			# set the appropriate bos/pad token ids
			__a						:  Tuple									=     2_0_4_8
			__a						:  int									=     2_0_4_8

			# set other default generation config params
			__a						:  Union[str, Any]									=     int(3_0 * audio_encoder.config.frame_rate				)
			__a						:  List[Any]									=     True
			__a						:  Any									=     3.0

			if pytorch_dump_folder is not None:
						Path(lowerCamelCase_				).mkdir(exist_ok=lowerCamelCase_				)
						logger.info(f'''Saving model {checkpoint} to {pytorch_dump_folder}'''				)
						model.save_pretrained(lowerCamelCase_				)
						processor.save_pretrained(lowerCamelCase_				)

			if repo_id:
						logger.info(f'''Pushing model {checkpoint} to {repo_id}'''				)
						model.push_to_hub(lowerCamelCase_				)
						processor.push_to_hub(lowerCamelCase_				)


if __name__ == "__main__":
							SCREAMING_SNAKE_CASE__				     =			argparse.ArgumentParser()
							# Required parameters
							parser.add_argument(
							    '''--checkpoint''',
							    default='''small''',
							    type=str,
							    help='''Checkpoint size of the MusicGen model you\'d like to convert. Can be one of: `[\'small\', \'medium\', \'large\']`.''',
							)
							parser.add_argument(
							    '''--pytorch_dump_folder''',
							    required=True,
							    default=None,
							    type=str,
							    help='''Path to the output PyTorch model directory.''',
							)
							parser.add_argument(
							    '''--push_to_hub''', default=None, type=str, help='''Where to upload the converted model on the 🤗 hub.'''
							)
							parser.add_argument(
							    '''--device''', default='''cpu''', type=str, help='''Torch device to run the conversion, either cpu or cuda.'''
							)

							SCREAMING_SNAKE_CASE__				     =			parser.parse_args()
							convert_musicgen_checkpoint(args.checkpoint, args.pytorch_dump_folder, args.push_to_hub)













'''simple docstring'''

import json
import os
from datetime import date
from pathlib import Path

from tabulate import DataRow, TableFormat, tabulate


UpperCamelCase      					=TableFormat(
    lineabove=None,
    linebelowheader=None,
    linebetweenrows=None,
    linebelow=None,
    headerrow=DataRow("", "|", "|"),
    datarow=DataRow("", "|", "|"),
    padding=1,
    with_header_hide=None,
)


UpperCamelCase      					=[]
UpperCamelCase      					=[]

UpperCamelCase      					={"type": "section", "text": {"type": "plain_text", "text": "No failed tests! 🤗", "emoji": True}}

UpperCamelCase      					=[
    {
        "type": "header",
        "text": {
            "type": "plain_text",
            "text": f"🤗 Accelerate nightly {os.environ.get('TEST_TYPE', '')} test results",
            "emoji": True,
        },
    }
]

UpperCamelCase      					=0
for log in Path().glob("*.log"):
      UpperCamelCase      					=0
      with open(log, "r") as f:
            for line in f:
                  UpperCamelCase      					=json.loads(line)
                  if line.get("nodeid", "") != "":
                        UpperCamelCase      					=line["nodeid"]
                        if line.get("duration", None) is not None:
                              UpperCamelCase      					=f"{line['duration']:.4f}"
                              if line.get("outcome", "") == "failed":
                                    section_num_failed += 1
                                    failed.append([test, duration, log.name.split("_")[0]])
                                    total_num_failed += 1
      group_info.append([str(log), section_num_failed, failed])
      UpperCamelCase      					=[]
      log.unlink()

UpperCamelCase      					=""
UpperCamelCase      					=[]
if total_num_failed > 0:
      for name, num_failed, failed_tests in group_info:
            if num_failed > 0:
                  if num_failed == 1:
                        message += f"*{name[1:]}: {num_failed} failed test*\n"
                  else:
                        message += f"*{name[1:]}: {num_failed} failed tests*\n"
                  UpperCamelCase      					=[]
                  UpperCamelCase      					={}
                  for test in failed_tests:
                        UpperCamelCase      					=test[0].split("::")
                        UpperCamelCase      					=data[0].split("/")[-1]
                        if data[0] not in filesafailed:
                              UpperCamelCase      					=[data[1:]]
                        else:
                              filesafailed[data[0]] += [data[1:]]
                        failed_table.append(data)

                  UpperCamelCase      					=[test[0] for test in failed_table]
                  UpperCamelCase      					=list(set(files))
                  # Count number of instances in failed_tests
                  UpperCamelCase      					=[]
                  for file in individual_files:
                        table.append([file, len(filesafailed[file])])

                  UpperCamelCase      					=tabulate(
                      table,
                      headers=["Test Location", "Num Failed"],
                      tablefmt=hf_table_format,
                      stralign="right",
                  )
                  message += f"\n```\n{failed_table}\n```"
                  all_filesafailed.append(filesafailed)
      if len(message) > 3000:
            UpperCamelCase      					="Too many failed tests, please see the full report in the Action results."
            UpperCamelCase      					=len(err) + 10
            UpperCamelCase      					=message[: 3000 - offset] + f"\n...\n```\n{err}"
      print(f"### {message}")
else:
      UpperCamelCase      					="No failed tests! 🤗"
      print(f"## {message}")
      payload.append(no_error_payload)

if os.environ.get("TEST_TYPE", "") != "":
      from slack_sdk import WebClient

      UpperCamelCase      					=WebClient(token=os.environ["SLACK_API_TOKEN"])
      if message != "No failed tests! 🤗":
            UpperCamelCase      					={
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": message,
                },
            }
            payload.append(md_report)
            UpperCamelCase      					={
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": "*For more details:*",
                },
                "accessory": {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "Check Action results",
                        "emoji": True,
                    },
                    "url": f"https://github.com/{os.environ['GITHUB_REPOSITORY']}/actions/runs/{os.environ['GITHUB_RUN_ID']}",
                },
            }
            payload.append(action_button)
            UpperCamelCase      					={
                "type": "context",
                "elements": [
                    {
                        "type": "plain_text",
                        "text": f"Nightly {os.environ.get('TEST_TYPE')} test results for {date.today()}",
                    }
                ],
            }
            payload.append(date_report)
      UpperCamelCase      					=client.chat_postMessage(channel="#accelerate-ci-daily", text=message, blocks=payload)
      UpperCamelCase      					=response.data["ts"]
      for failed_file in all_filesafailed:
            for test_location, test_failures in failed_file.items():
                  # Keep only the first instance of the test name
                  UpperCamelCase      					=""
                  for i, row in enumerate(test_failures):
                        if row[0] != test_class:
                              UpperCamelCase      					=row[0]
                        else:
                              UpperCamelCase      					=""

                  UpperCamelCase      					={
                      "type": "section",
                      "text": {
                          "type": "mrkdwn",
                          "text": f"Test location: {test_location}\n```\n{tabulate(test_failures, headers=['Class', 'Test'], tablefmt=hf_table_format, stralign='right')}\n```",
                      },
                  }

                  client.chat_postMessage(
                      channel="#accelerate-ci-daily",
                      thread_ts=ts,
                      blocks=[payload],
                  )






