





"""simple docstring"""


import logging
import os
from dataclasses import dataclass, field
from typing import Dict, Optional

import datasets
import numpy as np
import tensorflow as tf

from transformers import (
    AutoConfig,
    AutoTokenizer,
    EvalPrediction,
    HfArgumentParser,
    PreTrainedTokenizer,
    TFAutoModelForSequenceClassification,
    TFTrainer,
    TFTrainingArguments,
)
from transformers.utils import logging as hf_logging


hf_logging.set_verbosity_info()
hf_logging.enable_default_handler()
hf_logging.enable_explicit_format()

def      UpperCamelCase							(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE = None							,     ):
						UpperCamelCase				:		Dict				   =			{}

						if train_file is not None:
												UpperCamelCase				:		List[str]				   =			[train_file]
						if eval_file is not None:
												UpperCamelCase				:		List[Any]				   =			[eval_file]
						if test_file is not None:
												UpperCamelCase				:		List[str]				   =			[test_file]

						UpperCamelCase				:		List[str]				   =			datasets.load_dataset("""csv"""							,     data_files=SCREAMING_SNAKE_CASE     )
						UpperCamelCase				:		Optional[Any]				   =			list(ds[list(files.keys()     )[0]].features.keys()     )
						UpperCamelCase				:		Union[str, Any]				   =			features_name.pop(SCREAMING_SNAKE_CASE     )
						UpperCamelCase				:		Tuple				   =			list(set(ds[list(files.keys()     )[0]][label_name]     )     )
						UpperCamelCase				:		Optional[int]				   =			{label: i for i, label in enumerate(SCREAMING_SNAKE_CASE     )}
						UpperCamelCase				:		Any				   =			tokenizer.model_input_names
						UpperCamelCase				:		Optional[int]				   =			{}

						if len(SCREAMING_SNAKE_CASE     ) == 1:
												for k in files.keys():
																		UpperCamelCase				:		Optional[int]				   =			ds[k].map(
																		    lambda SCREAMING_SNAKE_CASE     : tokenizer.batch_encode_plus(
																		        example[features_name[0]]							,     truncation=SCREAMING_SNAKE_CASE							,     max_length=SCREAMING_SNAKE_CASE							,     padding="""max_length"""     )							,     batched=SCREAMING_SNAKE_CASE							,     )
						elif len(SCREAMING_SNAKE_CASE     ) == 2:
												for k in files.keys():
																		UpperCamelCase				:		str				   =			ds[k].map(
																		    lambda SCREAMING_SNAKE_CASE     : tokenizer.batch_encode_plus(
																		        (example[features_name[0]], example[features_name[1]])							,     truncation=SCREAMING_SNAKE_CASE							,     max_length=SCREAMING_SNAKE_CASE							,     padding="""max_length"""							,     )							,     batched=SCREAMING_SNAKE_CASE							,     )

						def gen_train():
												for ex in transformed_ds[datasets.Split.TRAIN]:
																		UpperCamelCase				:		int				   =			{k: v for k, v in ex.items() if k in input_names}
																		UpperCamelCase				:		Optional[int]				   =			labelaid[ex[label_name]]
																		yield (d, label)

						def gen_val():
												for ex in transformed_ds[datasets.Split.VALIDATION]:
																		UpperCamelCase				:		int				   =			{k: v for k, v in ex.items() if k in input_names}
																		UpperCamelCase				:		List[Any]				   =			labelaid[ex[label_name]]
																		yield (d, label)

						def gen_test():
												for ex in transformed_ds[datasets.Split.TEST]:
																		UpperCamelCase				:		List[str]				   =			{k: v for k, v in ex.items() if k in input_names}
																		UpperCamelCase				:		Any				   =			labelaid[ex[label_name]]
																		yield (d, label)

						UpperCamelCase				:		Union[str, Any]				   =			(
						    tf.data.Dataset.from_generator(
						        SCREAMING_SNAKE_CASE							,     ({k: tf.intaa for k in input_names}, tf.intaa)							,     ({k: tf.TensorShape([None]     ) for k in input_names}, tf.TensorShape([]     ))							,     )
						    if datasets.Split.TRAIN in transformed_ds
						    else None
						)

						if train_ds is not None:
												UpperCamelCase				:		List[Any]				   =			train_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TRAIN]     )     )     )

						UpperCamelCase				:		Any				   =			(
						    tf.data.Dataset.from_generator(
						        SCREAMING_SNAKE_CASE							,     ({k: tf.intaa for k in input_names}, tf.intaa)							,     ({k: tf.TensorShape([None]     ) for k in input_names}, tf.TensorShape([]     ))							,     )
						    if datasets.Split.VALIDATION in transformed_ds
						    else None
						)

						if val_ds is not None:
												UpperCamelCase				:		Optional[Any]				   =			val_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.VALIDATION]     )     )     )

						UpperCamelCase				:		List[Any]				   =			(
						    tf.data.Dataset.from_generator(
						        SCREAMING_SNAKE_CASE							,     ({k: tf.intaa for k in input_names}, tf.intaa)							,     ({k: tf.TensorShape([None]     ) for k in input_names}, tf.TensorShape([]     ))							,     )
						    if datasets.Split.TEST in transformed_ds
						    else None
						)

						if test_ds is not None:
												UpperCamelCase				:		int				   =			test_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TEST]     )     )     )

						return train_ds, val_ds, test_ds, labelaid


__magic_name__       :					Any						=			logging.getLogger(__name__)






@dataclass
class 							lowercase__			:

			"""simple docstring"""
			__lowerCAmelCase     :				int										=  field(metadata={"""help""": """Which column contains the label"""}	)
			__lowerCAmelCase     :				str										=  field(default=__SCREAMING_SNAKE_CASE					,     metadata={"""help""": """The path of the training file"""}	)
			__lowerCAmelCase     :				Optional[str]										=  field(default=__SCREAMING_SNAKE_CASE					,     metadata={"""help""": """The path of the development file"""}	)
			__lowerCAmelCase     :				Optional[str]										=  field(default=__SCREAMING_SNAKE_CASE					,     metadata={"""help""": """The path of the test file"""}	)
			__lowerCAmelCase     :				int										=  field(
			    default=128					,     metadata={
			        """help""": (
			            """The maximum total input sequence length after tokenization. Sequences longer """
			            """than this will be truncated, sequences shorter will be padded."""
			        )
			    }					,     )
			__lowerCAmelCase     :				bool										=  field(
			    default=__SCREAMING_SNAKE_CASE					,     metadata={"""help""": """Overwrite the cached training and evaluation sets"""}	)






@dataclass
class 							lowercase__			:

			"""simple docstring"""
			__lowerCAmelCase     :				str										=  field(
			    metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}	)
			__lowerCAmelCase     :				Optional[str]										=  field(
			    default=__SCREAMING_SNAKE_CASE					,     metadata={"""help""": """Pretrained config name or path if not the same as model_name"""}	)
			__lowerCAmelCase     :				Optional[str]										=  field(
			    default=__SCREAMING_SNAKE_CASE					,     metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""}	)
			__lowerCAmelCase     :				bool										=  field(default=__SCREAMING_SNAKE_CASE					,     metadata={"""help""": """Set this flag to use fast tokenization."""}	)
			# If you want to tweak more attributes on your tokenizer, you should do it in a distinct script,
			# or just modify its tokenizer_config.json.
			__lowerCAmelCase     :				Optional[str]										=  field(
			    default=__SCREAMING_SNAKE_CASE					,     metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""}					,     )


def      UpperCamelCase							():
						# See all possible arguments in src/transformers/training_args.py
						# or by passing the --help flag to this script.
						# We now keep distinct sets of args, for a cleaner separation of concerns.
						UpperCamelCase				:		int				   =			HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments)     )
						UpperCamelCase   ,						UpperCamelCase   ,						UpperCamelCase				:		Optional[Any]				   =			parser.parse_args_into_dataclasses()

						if (
						    os.path.exists(training_args.output_dir     )
						    and os.listdir(training_args.output_dir     )
						    and training_args.do_train
						    and not training_args.overwrite_output_dir
						):
												raise ValueError(
												    f"""Output directory ({training_args.output_dir}) already exists and is not empty. Use"""
												    """ --overwrite_output_dir to overcome."""     )

						# Setup logging
						logging.basicConfig(
						    format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s"""							,     datefmt="""%m/%d/%Y %H:%M:%S"""							,     level=logging.INFO							,     )
						logger.info(
						    f"""n_replicas: {training_args.n_replicas}, distributed training: {bool(training_args.n_replicas > 1     )}, """
						    f"""16-bits training: {training_args.fpaa}"""     )
						logger.info(f"""Training/evaluation parameters {training_args}"""     )

						# Load pretrained model and tokenizer
						#
						# Distributed training:
						# The .from_pretrained methods guarantee that only one local process can concurrently
						# download model & vocab.

						UpperCamelCase				:		str				   =			AutoTokenizer.from_pretrained(
						    model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path							,     cache_dir=model_args.cache_dir							,     )

						UpperCamelCase   ,						UpperCamelCase   ,						UpperCamelCase   ,						UpperCamelCase				:		List[str]				   =			get_tfds(
						    train_file=data_args.train_file							,     eval_file=data_args.dev_file							,     test_file=data_args.test_file							,     tokenizer=SCREAMING_SNAKE_CASE							,     label_column_id=data_args.label_column_id							,     max_seq_length=data_args.max_seq_length							,     )

						UpperCamelCase				:		Dict				   =			AutoConfig.from_pretrained(
						    model_args.config_name if model_args.config_name else model_args.model_name_or_path							,     num_labels=len(SCREAMING_SNAKE_CASE     )							,     labelaid=SCREAMING_SNAKE_CASE							,     idalabel={id: label for label, id in labelaid.items()}							,     finetuning_task="""text-classification"""							,     cache_dir=model_args.cache_dir							,     )

						with training_args.strategy.scope():
												UpperCamelCase				:		str				   =			TFAutoModelForSequenceClassification.from_pretrained(
												    model_args.model_name_or_path							,     from_pt=bool(""".bin""" in model_args.model_name_or_path     )							,     config=SCREAMING_SNAKE_CASE							,     cache_dir=model_args.cache_dir							,     )

						def compute_metrics(SCREAMING_SNAKE_CASE     ) -> Dict:
												UpperCamelCase				:		Dict				   =			np.argmax(p.predictions							,     axis=1     )

												return {"acc": (preds == p.label_ids).mean()}

						# Initialize our Trainer
						UpperCamelCase				:		List[Any]				   =			TFTrainer(
						    model=SCREAMING_SNAKE_CASE							,     args=SCREAMING_SNAKE_CASE							,     train_dataset=SCREAMING_SNAKE_CASE							,     eval_dataset=SCREAMING_SNAKE_CASE							,     compute_metrics=SCREAMING_SNAKE_CASE							,     )

						# Training
						if training_args.do_train:
												trainer.train()
												trainer.save_model()
												tokenizer.save_pretrained(training_args.output_dir     )

						# Evaluation
						UpperCamelCase				:		Any				   =			{}
						if training_args.do_eval:
												logger.info("""*** Evaluate ***"""     )
												UpperCamelCase				:		Optional[Any]				   =			trainer.evaluate()
												UpperCamelCase				:		Optional[Any]				   =			os.path.join(training_args.output_dir							,     """eval_results.txt"""     )

												with open(SCREAMING_SNAKE_CASE							,     """w"""     ) as writer:
																		logger.info("""***** Eval results *****"""     )

																		for key, value in result.items():
																								logger.info(f"""  {key} = {value}"""     )
																								writer.write(f"""{key} = {value}\n"""     )

																		results.update(SCREAMING_SNAKE_CASE     )

						return results


if __name__ == "__main__":
		main()










"""simple docstring"""


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()
__magic_name__       :					List[Any]						=			logging.get_logger(__name__)


__magic_name__       :					List[str]						=			["""model.decoder.embed_positions.weights"""]

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

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

def      UpperCamelCase							(SCREAMING_SNAKE_CASE     ):
						if checkpoint == "small":
												# default config values
												UpperCamelCase				:		Tuple				   =			1024
												UpperCamelCase				:		Dict				   =			24
												UpperCamelCase				:		Optional[int]				   =			16
						elif checkpoint == "medium":
												UpperCamelCase				:		Optional[Any]				   =			1536
												UpperCamelCase				:		int				   =			48
												UpperCamelCase				:		int				   =			24
						elif checkpoint == "large":
												UpperCamelCase				:		Any				   =			2048
												UpperCamelCase				:		List[Any]				   =			48
												UpperCamelCase				:		str				   =			32
						else:
												raise ValueError(f"""Checkpoint should be one of `['small', 'medium', 'large']`, got {checkpoint}."""     )
						UpperCamelCase				:		Optional[int]				   =			MusicgenDecoderConfig(
						    hidden_size=SCREAMING_SNAKE_CASE							,     ffn_dim=hidden_size * 4							,     num_hidden_layers=SCREAMING_SNAKE_CASE							,     num_attention_heads=SCREAMING_SNAKE_CASE							,     )
						return config

@torch.no_grad()
def      UpperCamelCase							(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE=None							,     SCREAMING_SNAKE_CASE=None							,     SCREAMING_SNAKE_CASE="cpu"     ):
						UpperCamelCase				:		Optional[Any]				   =			MusicGen.get_pretrained(SCREAMING_SNAKE_CASE							,     device=SCREAMING_SNAKE_CASE     )
						UpperCamelCase				:		Tuple				   =			decoder_config_from_checkpoint(SCREAMING_SNAKE_CASE     )

						UpperCamelCase				:		Optional[Any]				   =			fairseq_model.lm.state_dict()
						UpperCamelCase   ,						UpperCamelCase				:		Optional[int]				   =			rename_state_dict(
						    SCREAMING_SNAKE_CASE							,     hidden_size=decoder_config.hidden_size     )

						UpperCamelCase				:		int				   =			TaEncoderModel.from_pretrained("""t5-base"""     )
						UpperCamelCase				:		List[str]				   =			EncodecModel.from_pretrained("""facebook/encodec_32khz"""     )
						UpperCamelCase				:		Optional[int]				   =			MusicgenForCausalLM(SCREAMING_SNAKE_CASE     ).eval()

						# load all decoder weights - expect that we'll be missing embeddings and enc-dec projection
						UpperCamelCase   ,						UpperCamelCase				:		Tuple				   =			decoder.load_state_dict(SCREAMING_SNAKE_CASE							,     strict=SCREAMING_SNAKE_CASE     )

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

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

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

						# init the composite model
						UpperCamelCase				:		Tuple				   =			MusicgenForConditionalGeneration(text_encoder=SCREAMING_SNAKE_CASE							,     audio_encoder=SCREAMING_SNAKE_CASE							,     decoder=SCREAMING_SNAKE_CASE     )

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

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

						with torch.no_grad():
												UpperCamelCase				:		Any				   =			model(input_ids=SCREAMING_SNAKE_CASE							,     decoder_input_ids=SCREAMING_SNAKE_CASE     ).logits

						if logits.shape != (8, 1, 2048):
												raise ValueError("""Incorrect shape for logits"""     )

						# now construct the processor
						UpperCamelCase				:		Any				   =			AutoTokenizer.from_pretrained("""t5-base"""     )
						UpperCamelCase				:		Tuple				   =			AutoFeatureExtractor.from_pretrained("""facebook/encodec_32khz"""							,     padding_side="""left"""     )

						UpperCamelCase				:		int				   =			MusicgenProcessor(feature_extractor=SCREAMING_SNAKE_CASE							,     tokenizer=SCREAMING_SNAKE_CASE     )

						# set the appropriate bos/pad token ids
						UpperCamelCase				:		Optional[int]				   =			2048
						UpperCamelCase				:		Tuple				   =			2048

						# set other default generation config params
						UpperCamelCase				:		Optional[int]				   =			int(30 * audio_encoder.config.frame_rate     )
						UpperCamelCase				:		int				   =			True
						UpperCamelCase				:		Dict				   =			3.0

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

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


if __name__ == "__main__":
		__magic_name__       :					Any						=			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."""
		)

		__magic_name__       :					Optional[Any]						=			parser.parse_args()
		convert_musicgen_checkpoint(args.checkpoint, args.pytorch_dump_folder, args.push_to_hub)



