

'''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()
lowerCAmelCase_							=	logging.get_logger(__name__)


lowerCAmelCase_							=	['model.decoder.embed_positions.weights']
def       A__	(							A  :     int):


			'''simple docstring'''


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


			'''simple docstring'''


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


			'''simple docstring'''


			if checkpoint == "small":
						# default config values
						UpperCamelCase				:	Dict    						=					10_24
						UpperCamelCase				:	Optional[Any]    						=					24
						UpperCamelCase				:	Union[str, Any]    						=					16
			elif checkpoint == "medium":
						UpperCamelCase				:	Optional[int]    						=					15_36
						UpperCamelCase				:	Any    						=					48
						UpperCamelCase				:	List[str]    						=					24
			elif checkpoint == "large":
						UpperCamelCase				:	List[Any]    						=					20_48
						UpperCamelCase				:	List[str]    						=					48
						UpperCamelCase				:	Union[str, Any]    						=					32
			else:
						raise ValueError(F'''Checkpoint should be one of `[\'small\', \'medium\', \'large\']`, got {checkpoint}.''')
			UpperCamelCase				:	str    						=					MusicgenDecoderConfig(
			    hidden_size=UpperCamelCase_							,       ffn_dim=hidden_size * 4							,       num_hidden_layers=UpperCamelCase_							,       num_attention_heads=UpperCamelCase_							,       )
			return config
@torch.no_grad()
def       A__	(							A  :     Optional[int]							,       A  :     List[Any]=None							,       A  :     Dict=None							,       A  :     Any="cpu"):


			'''simple docstring'''


			UpperCamelCase				:	Dict    						=					MusicGen.get_pretrained(UpperCamelCase_							,       device=UpperCamelCase_)
			UpperCamelCase				:	Optional[int]    						=					decoder_config_from_checkpoint(UpperCamelCase_)

			UpperCamelCase				:	int    						=					fairseq_model.lm.state_dict()
			UpperCamelCase						,   UpperCamelCase				:	List[str]    						=					rename_state_dict(
			    UpperCamelCase_							,       hidden_size=decoder_config.hidden_size)

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

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

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

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

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

			# init the composite model
			UpperCamelCase				:	Optional[Any]    						=					MusicgenForConditionalGeneration(text_encoder=UpperCamelCase_							,       audio_encoder=UpperCamelCase_							,       decoder=UpperCamelCase_)

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

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

			with torch.no_grad():
						UpperCamelCase				:	str    						=					model(input_ids=UpperCamelCase_							,       decoder_input_ids=UpperCamelCase_).logits

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

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

			UpperCamelCase				:	Union[str, Any]    						=					MusicgenProcessor(feature_extractor=UpperCamelCase_							,       tokenizer=UpperCamelCase_)

			# set the appropriate bos/pad token ids
			UpperCamelCase				:	Optional[int]    						=					20_48
			UpperCamelCase				:	Union[str, Any]    						=					20_48

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

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

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


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

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





import faiss  # noqa: F401 # Here to have a nice missing dependency error message early on
import numpy  # noqa: F401 # Here to have a nice missing dependency error message early on
import requests  # noqa: F401 # Here to have a nice missing dependency error message early on
import sklearn  # noqa: F401 # Here to have a nice missing dependency error message early on
import tqdm  # noqa: F401 # Here to have a nice missing dependency error message early on
from mauve import compute_mauve  # From: mauve-text

import datasets


_lowercase    	=		"\\n@inproceedings{pillutla-etal:mauve:neurips2021,\n  title={MAUVE: Measuring the Gap Between Neural Text and Human Text using Divergence Frontiers},\n  author={Pillutla, Krishna and Swayamdipta, Swabha and Zellers, Rowan and Thickstun, John and Welleck, Sean and Choi, Yejin and Harchaoui, Zaid},\n  booktitle = {NeurIPS},\n  year      = {2021}\n}\n\n"

_lowercase    	=		"\\nMAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure.\n\nMAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences.\n\nFor details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021).\n\nThis metrics is a wrapper around the official implementation of MAUVE:\nhttps://github.com/krishnap25/mauve\n"

_lowercase    	=		"\nCalculates MAUVE scores between two lists of generated text and reference text.\nArgs:\n    predictions: list of generated text to score. Each predictions\n        should be a string with tokens separated by spaces.\n    references: list of reference for each prediction. Each\n        reference should be a string with tokens separated by spaces.\nOptional Args:\n    num_buckets: the size of the histogram to quantize P and Q. Options: 'auto' (default) or an integer\n    pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1\n    kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9\n    kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5\n    kmeans_max_iter: maximum number of k-means iterations. Default 500\n    featurize_model_name: name of the model from which features are obtained. Default 'gpt2-large' Use one of ['gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl'].\n    device_id: Device for featurization. Supply a GPU id (e.g. 0 or 3) to use GPU. If no GPU with this id is found, use CPU\n    max_text_length: maximum number of tokens to consider. Default 1024\n    divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25\n    mauve_scaling_factor: \"c\" from the paper. Default 5.\n    verbose: If True (default), print running time updates\n    seed: random seed to initialize k-means cluster assignments.\nReturns:\n    mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer,\n    frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer,\n    divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve,\n    p_hist: a discrete distribution, which is a quantized version of the text distribution p_text,\n    q_hist: same as above, but with q_text.\nExamples:\n\n    >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest\n    >>> import datasets\n    >>> mauve = datasets.load_metric('mauve')\n    >>> predictions = [\"hello there\", \"general kenobi\"]\n    >>> references = [\"hello there\", \"general kenobi\"]\n    >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP\n    >>> print(out.mauve) # doctest: +SKIP\n    1.0\n"



@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION      ,   _KWARGS_DESCRIPTION  )
class      _UpperCAmelCase      (							datasets.Metric  ):




     def 		snake_case_		(					self):
         return datasets.MetricInfo(
             description=_DESCRIPTION			,    citation=_CITATION			,    homepage='''https://github.com/krishnap25/mauve'''			,    inputs_description=_KWARGS_DESCRIPTION			,    features=datasets.Features(
                 {
                     '''predictions''': datasets.Value('''string'''			,    id='''sequence'''),
                     '''references''': datasets.Value('''string'''			,    id='''sequence'''),
                 })			,    codebase_urls=['''https://github.com/krishnap25/mauve''']			,    reference_urls=[
                 '''https://arxiv.org/abs/2102.01454''',
                 '''https://github.com/krishnap25/mauve''',
             ]			,    )




     def 		snake_case_		(					self			,    a__			,    a__			,    a__=None			,    a__=None			,    a__=None			,    a__=None			,    a__="auto"			,    a__=-1			,    a__=0.9			,    a__=5			,    a__=5_0_0			,    a__="gpt2-large"			,    a__=-1			,    a__=1_0_2_4			,    a__=2_5			,    a__=5			,    a__=True			,    a__=2_5			,    ):
         A__					=       compute_mauve(
             p_text=a__			,    q_text=a__			,    p_features=a__			,    q_features=a__			,    p_tokens=a__			,    q_tokens=a__			,    num_buckets=a__			,    pca_max_data=a__			,    kmeans_explained_var=a__			,    kmeans_num_redo=a__			,    kmeans_max_iter=a__			,    featurize_model_name=a__			,    device_id=a__			,    max_text_length=a__			,    divergence_curve_discretization_size=a__			,    mauve_scaling_factor=a__			,    verbose=a__			,    seed=a__			,    )
         return out
