



import unittest

from diffusers.models.unet_ad_blocks import *  # noqa F403
from diffusers.utils import torch_device

from .test_unet_blocks_common import UNetBlockTesterMixin





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       List[Any]          =DownBlockaD  # noqa F405
		lowercase      :       Optional[Any]          ='''down'''






		def SCREAMING_SNAKE_CASE_			(						self      :     int      )				->					Dict:
								'''simple docstring'''







								UpperCAmelCase         =     [-0.02_32, -0.98_69, 0.80_54, -0.06_37, -0.16_88, -1.42_64, 0.44_70, -1.33_94, 0.09_04]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       Optional[int]          =ResnetDownsampleBlockaD  # noqa F405
		lowercase      :       Optional[int]          ='''down'''






		def SCREAMING_SNAKE_CASE_			(						self      :     Dict      )				->					Tuple:
								'''simple docstring'''







								UpperCAmelCase         =     [0.07_10, 0.24_10, -0.73_20, -1.07_57, -1.13_43, 0.35_40, -0.01_33, -0.25_76, 0.09_48]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       Dict          =AttnDownBlockaD  # noqa F405
		lowercase      :       str          ='''down'''






		def SCREAMING_SNAKE_CASE_			(						self      :     List[str]      )				->					Optional[Any]:
								'''simple docstring'''







								UpperCAmelCase         =     [0.06_36, 0.89_64, -0.62_34, -1.01_31, 0.08_44, 0.49_35, 0.34_37, 0.09_11, -0.29_57]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       List[str]          =CrossAttnDownBlockaD  # noqa F405
		lowercase      :       Any          ='''down'''






		def SCREAMING_SNAKE_CASE_			(						self      :     Dict      )				->					Dict:
								'''simple docstring'''







								UpperCAmelCase       ,		UpperCAmelCase         =     super().prepare_init_args_and_inputs_for_common()
								UpperCAmelCase         =     32
								return init_dict, inputs_dict







		def SCREAMING_SNAKE_CASE_			(						self      :     Optional[int]      )				->					Union[str, Any]:
								'''simple docstring'''







								UpperCAmelCase         =     [0.22_38, -0.73_96, -0.22_55, -0.38_29, 0.19_25, 1.16_65, 0.06_03, -0.72_95, 0.19_83]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       List[str]          =SimpleCrossAttnDownBlockaD  # noqa F405
		lowercase      :       Optional[int]          ='''down'''






		@property
		def SCREAMING_SNAKE_CASE_			(						self      :     Union[str, Any]      )				->					List[str]:
								'''simple docstring'''







								return super().get_dummy_input(include_encoder_hidden_states=UpperCamelCase__      )






		def SCREAMING_SNAKE_CASE_			(						self      :     str      )				->					int:
								'''simple docstring'''







								UpperCAmelCase       ,		UpperCAmelCase         =     super().prepare_init_args_and_inputs_for_common()
								UpperCAmelCase         =     32
								return init_dict, inputs_dict







		@unittest.skipIf(torch_device == "mps"				,		"MPS result is not consistent"      )
		def SCREAMING_SNAKE_CASE_			(						self      :     List[Any]      )				->					List[Any]:
								'''simple docstring'''







								UpperCAmelCase         =     [0.79_21, -0.09_92, -0.19_62, -0.76_95, -0.42_42, 0.78_04, 0.47_37, 0.27_65, 0.33_38]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       Tuple          =SkipDownBlockaD  # noqa F405
		lowercase      :       Optional[int]          ='''down'''






		@property
		def SCREAMING_SNAKE_CASE_			(						self      :     Optional[Any]      )				->					Dict:
								'''simple docstring'''







								return super().get_dummy_input(include_skip_sample=UpperCamelCase__      )







		def SCREAMING_SNAKE_CASE_			(						self      :     List[Any]      )				->					int:
								'''simple docstring'''







								UpperCAmelCase         =     [-0.08_45, -0.20_87, -0.24_65, 0.09_71, 0.19_00, -0.04_84, 0.26_64, 0.41_79, 0.50_69]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       List[str]          =AttnSkipDownBlockaD  # noqa F405
		lowercase      :       Tuple          ='''down'''






		@property
		def SCREAMING_SNAKE_CASE_			(						self      :     List[str]      )				->					str:
								'''simple docstring'''







								return super().get_dummy_input(include_skip_sample=UpperCamelCase__      )







		def SCREAMING_SNAKE_CASE_			(						self      :     Dict      )				->					Union[str, Any]:
								'''simple docstring'''







								UpperCAmelCase         =     [0.55_39, 0.16_09, 0.49_24, 0.05_37, -0.19_95, 0.40_50, 0.09_79, -0.27_21, -0.06_42]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       Optional[Any]          =DownEncoderBlockaD  # noqa F405
		lowercase      :       str          ='''down'''






		@property
		def SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					List[Any]:
								'''simple docstring'''







								return super().get_dummy_input(include_temb=UpperCamelCase__      )






		def SCREAMING_SNAKE_CASE_			(						self      :     Dict      )				->					Tuple:
								'''simple docstring'''







								UpperCAmelCase         =     {
								    "in_channels": 32,
								    "out_channels": 32,
								}
								UpperCAmelCase         =     self.dummy_input
								return init_dict, inputs_dict







		def SCREAMING_SNAKE_CASE_			(						self      :     Optional[Any]      )				->					Dict:
								'''simple docstring'''







								UpperCAmelCase         =     [1.11_02, 0.53_02, 0.48_72, -0.00_23, -0.80_42, 0.04_83, -0.34_89, -0.56_32, 0.76_26]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       int          =AttnDownEncoderBlockaD  # noqa F405
		lowercase      :       List[Any]          ='''down'''






		@property
		def SCREAMING_SNAKE_CASE_			(						self      :     Dict      )				->					Dict:
								'''simple docstring'''







								return super().get_dummy_input(include_temb=UpperCamelCase__      )






		def SCREAMING_SNAKE_CASE_			(						self      :     str      )				->					Any:
								'''simple docstring'''







								UpperCAmelCase         =     {
								    "in_channels": 32,
								    "out_channels": 32,
								}
								UpperCAmelCase         =     self.dummy_input
								return init_dict, inputs_dict







		def SCREAMING_SNAKE_CASE_			(						self      :     Any      )				->					Dict:
								'''simple docstring'''







								UpperCAmelCase         =     [0.89_66, -0.14_86, 0.85_68, 0.81_41, -0.90_46, -0.13_42, -0.09_72, -0.74_17, 0.15_38]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       Tuple          =UNetMidBlockaD  # noqa F405
		lowercase      :       Optional[int]          ='''mid'''






		def SCREAMING_SNAKE_CASE_			(						self      :     Any      )				->					str:
								'''simple docstring'''







								UpperCAmelCase         =     {
								    "in_channels": 32,
								    "temb_channels": 1_28,
								}
								UpperCAmelCase         =     self.dummy_input
								return init_dict, inputs_dict







		def SCREAMING_SNAKE_CASE_			(						self      :     int      )				->					Any:
								'''simple docstring'''







								UpperCAmelCase         =     [-0.10_62, 1.72_48, 0.34_94, 1.45_69, -0.09_10, -1.24_21, -0.99_84, 0.67_36, 1.00_28]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       Optional[Any]          =UNetMidBlockaDCrossAttn  # noqa F405
		lowercase      :       Tuple          ='''mid'''






		def SCREAMING_SNAKE_CASE_			(						self      :     List[str]      )				->					str:
								'''simple docstring'''







								UpperCAmelCase       ,		UpperCAmelCase         =     super().prepare_init_args_and_inputs_for_common()
								UpperCAmelCase         =     32
								return init_dict, inputs_dict







		def SCREAMING_SNAKE_CASE_			(						self      :     Optional[int]      )				->					List[str]:
								'''simple docstring'''







								UpperCAmelCase         =     [0.01_87, 2.42_20, 0.44_84, 1.12_03, -0.61_21, -1.51_22, -0.82_70, 0.78_51, 1.83_35]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       Any          =UNetMidBlockaDSimpleCrossAttn  # noqa F405
		lowercase      :       Any          ='''mid'''






		@property
		def SCREAMING_SNAKE_CASE_			(						self      :     List[Any]      )				->					Optional[Any]:
								'''simple docstring'''







								return super().get_dummy_input(include_encoder_hidden_states=UpperCamelCase__      )






		def SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					str:
								'''simple docstring'''







								UpperCAmelCase       ,		UpperCAmelCase         =     super().prepare_init_args_and_inputs_for_common()
								UpperCAmelCase         =     32
								return init_dict, inputs_dict







		def SCREAMING_SNAKE_CASE_			(						self      :     Dict      )				->					Union[str, Any]:
								'''simple docstring'''







								UpperCAmelCase         =     [0.71_43, 1.99_74, 0.54_48, 1.39_77, 0.12_82, -1.12_37, -1.42_38, 0.55_30, 0.88_80]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       List[Any]          =UpBlockaD  # noqa F405
		lowercase      :       Optional[int]          ='''up'''






		@property
		def SCREAMING_SNAKE_CASE_			(						self      :     Union[str, Any]      )				->					Union[str, Any]:
								'''simple docstring'''







								return super().get_dummy_input(include_res_hidden_states_tuple=UpperCamelCase__      )







		def SCREAMING_SNAKE_CASE_			(						self      :     str      )				->					Union[str, Any]:
								'''simple docstring'''







								UpperCAmelCase         =     [-0.20_41, -0.41_65, -0.30_22, 0.00_41, -0.66_28, -0.70_53, 0.19_28, -0.03_25, 0.05_23]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       Union[str, Any]          =ResnetUpsampleBlockaD  # noqa F405
		lowercase      :       Union[str, Any]          ='''up'''






		@property
		def SCREAMING_SNAKE_CASE_			(						self      :     str      )				->					int:
								'''simple docstring'''







								return super().get_dummy_input(include_res_hidden_states_tuple=UpperCamelCase__      )







		def SCREAMING_SNAKE_CASE_			(						self      :     Optional[int]      )				->					Any:
								'''simple docstring'''







								UpperCAmelCase         =     [0.22_87, 0.35_49, -0.13_46, 0.47_97, -0.17_15, -0.96_49, 0.73_05, -0.58_64, -0.62_44]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       Union[str, Any]          =CrossAttnUpBlockaD  # noqa F405
		lowercase      :       Tuple          ='''up'''






		@property
		def SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					Tuple:
								'''simple docstring'''







								return super().get_dummy_input(include_res_hidden_states_tuple=UpperCamelCase__      )






		def SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					Optional[Any]:
								'''simple docstring'''







								UpperCAmelCase       ,		UpperCAmelCase         =     super().prepare_init_args_and_inputs_for_common()
								UpperCAmelCase         =     32
								return init_dict, inputs_dict







		def SCREAMING_SNAKE_CASE_			(						self      :     Any      )				->					List[str]:
								'''simple docstring'''







								UpperCAmelCase         =     [-0.14_03, -0.35_15, -0.04_20, -0.14_25, 0.31_67, 0.50_94, -0.21_81, 0.59_31, 0.55_82]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       str          =SimpleCrossAttnUpBlockaD  # noqa F405
		lowercase      :       Tuple          ='''up'''






		@property
		def SCREAMING_SNAKE_CASE_			(						self      :     Dict      )				->					str:
								'''simple docstring'''







								return super().get_dummy_input(include_res_hidden_states_tuple=UpperCamelCase__				,		include_encoder_hidden_states=UpperCamelCase__      )






		def SCREAMING_SNAKE_CASE_			(						self      :     Optional[Any]      )				->					Optional[Any]:
								'''simple docstring'''







								UpperCAmelCase       ,		UpperCAmelCase         =     super().prepare_init_args_and_inputs_for_common()
								UpperCAmelCase         =     32
								return init_dict, inputs_dict







		def SCREAMING_SNAKE_CASE_			(						self      :     Union[str, Any]      )				->					Any:
								'''simple docstring'''







								UpperCAmelCase         =     [0.26_45, 0.14_80, 0.09_09, 0.80_44, -0.97_58, -0.90_83, 0.09_94, -1.14_53, -0.74_02]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       List[str]          =AttnUpBlockaD  # noqa F405
		lowercase      :       Any          ='''up'''






		@property
		def SCREAMING_SNAKE_CASE_			(						self      :     str      )				->					str:
								'''simple docstring'''







								return super().get_dummy_input(include_res_hidden_states_tuple=UpperCamelCase__      )







		@unittest.skipIf(torch_device == "mps"				,		"MPS result is not consistent"      )
		def SCREAMING_SNAKE_CASE_			(						self      :     Dict      )				->					Optional[Any]:
								'''simple docstring'''







								UpperCAmelCase         =     [0.09_79, 0.13_26, 0.00_21, 0.06_59, 0.22_49, 0.00_59, 0.11_32, 0.59_52, 0.10_33]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       Dict          =SkipUpBlockaD  # noqa F405
		lowercase      :       int          ='''up'''






		@property
		def SCREAMING_SNAKE_CASE_			(						self      :     Any      )				->					str:
								'''simple docstring'''







								return super().get_dummy_input(include_res_hidden_states_tuple=UpperCamelCase__      )







		def SCREAMING_SNAKE_CASE_			(						self      :     str      )				->					Tuple:
								'''simple docstring'''







								UpperCAmelCase         =     [-0.08_93, -0.12_34, -0.15_06, -0.03_32, 0.01_23, -0.02_11, 0.05_66, 0.01_43, 0.03_62]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       Optional[Any]          =AttnSkipUpBlockaD  # noqa F405
		lowercase      :       Optional[Any]          ='''up'''






		@property
		def SCREAMING_SNAKE_CASE_			(						self      :     Union[str, Any]      )				->					Optional[Any]:
								'''simple docstring'''







								return super().get_dummy_input(include_res_hidden_states_tuple=UpperCamelCase__      )







		def SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					List[Any]:
								'''simple docstring'''







								UpperCAmelCase         =     [0.03_61, 0.06_17, 0.27_87, -0.03_50, 0.03_42, 0.34_21, -0.08_43, 0.09_13, 0.30_15]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       Union[str, Any]          =UpDecoderBlockaD  # noqa F405
		lowercase      :       str          ='''up'''






		@property
		def SCREAMING_SNAKE_CASE_			(						self      :     List[Any]      )				->					Optional[int]:
								'''simple docstring'''







								return super().get_dummy_input(include_temb=UpperCamelCase__      )






		def SCREAMING_SNAKE_CASE_			(						self      :     List[Any]      )				->					List[Any]:
								'''simple docstring'''







								UpperCAmelCase         =     {"in_channels": 32, "out_channels": 32}

								UpperCAmelCase         =     self.dummy_input
								return init_dict, inputs_dict







		def SCREAMING_SNAKE_CASE_			(						self      :     int      )				->					Any:
								'''simple docstring'''







								UpperCAmelCase         =     [0.44_04, 0.19_98, -0.98_86, -0.33_20, -0.31_28, -0.70_34, -0.69_55, -0.23_38, -0.31_37]
								super().test_output(UpperCamelCase__      )





class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       Any          =AttnUpDecoderBlockaD  # noqa F405
		lowercase      :       Optional[Any]          ='''up'''






		@property
		def SCREAMING_SNAKE_CASE_			(						self      :     Union[str, Any]      )				->					List[str]:
								'''simple docstring'''







								return super().get_dummy_input(include_temb=UpperCamelCase__      )






		def SCREAMING_SNAKE_CASE_			(						self      :     List[Any]      )				->					Optional[Any]:
								'''simple docstring'''







								UpperCAmelCase         =     {"in_channels": 32, "out_channels": 32}

								UpperCAmelCase         =     self.dummy_input
								return init_dict, inputs_dict







		def SCREAMING_SNAKE_CASE_			(						self      :     List[Any]      )				->					Optional[Any]:
								'''simple docstring'''







								UpperCAmelCase         =     [0.67_38, 0.44_91, 0.10_55, 1.07_10, 0.73_16, 0.33_39, 0.33_52, 0.10_23, 0.35_68]
								super().test_output(UpperCamelCase__      )








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     :							Dict  							=				logging.get_logger(__name__)


__lowerCamelCase     :							Tuple  							=				["model.decoder.embed_positions.weights"]

def    lowerCamelCase_(lowerCamelCase_    ) ->       Any:
						if "emb" in name:
												UpperCAmelCase         =     name.replace("emb"    ,   "model.decoder.embed_tokens"    )
						if "transformer" in name:
												UpperCAmelCase         =     name.replace("transformer"    ,   "model.decoder"    )
						if "cross_attention" in name:
												UpperCAmelCase         =     name.replace("cross_attention"    ,   "encoder_attn"    )
						if "linear1" in name:
												UpperCAmelCase         =     name.replace("linear1"    ,   "fc1"    )
						if "linear2" in name:
												UpperCAmelCase         =     name.replace("linear2"    ,   "fc2"    )
						if "norm1" in name:
												UpperCAmelCase         =     name.replace("norm1"    ,   "self_attn_layer_norm"    )
						if "norm_cross" in name:
												UpperCAmelCase         =     name.replace("norm_cross"    ,   "encoder_attn_layer_norm"    )
						if "norm2" in name:
												UpperCAmelCase         =     name.replace("norm2"    ,   "final_layer_norm"    )
						if "out_norm" in name:
												UpperCAmelCase         =     name.replace("out_norm"    ,   "model.decoder.layer_norm"    )
						if "linears" in name:
												UpperCAmelCase         =     name.replace("linears"    ,   "lm_heads"    )
						if "condition_provider.conditioners.description.output_proj" in name:
												UpperCAmelCase         =     name.replace("condition_provider.conditioners.description.output_proj"    ,   "enc_to_dec_proj"    )
						return name

def    lowerCamelCase_(lowerCamelCase_    ,   lowerCamelCase_    ) ->       Tuple[Dict, Dict]:
						UpperCAmelCase         =     list(state_dict.keys()    )
						UpperCAmelCase         =     {}
						for key in keys:
												UpperCAmelCase         =     state_dict.pop(lowerCamelCase_    )
												UpperCAmelCase         =     rename_keys(lowerCamelCase_    )
												if "in_proj_weight" in key:
																		# split fused qkv proj
																		UpperCAmelCase         =     val[:hidden_size, :]
																		UpperCAmelCase         =     val[hidden_size : 2 * hidden_size, :]
																		UpperCAmelCase         =     val[-hidden_size:, :]
												elif "enc_to_dec_proj" in key:
																		UpperCAmelCase         =     val
												else:
																		UpperCAmelCase         =     val
						return state_dict, enc_dec_proj_state_dict

def    lowerCamelCase_(lowerCamelCase_    ) ->       MusicgenDecoderConfig:
						if checkpoint == "small":
												# default config values
												UpperCAmelCase         =     1_024
												UpperCAmelCase         =     24
												UpperCAmelCase         =     16
						elif checkpoint == "medium":
												UpperCAmelCase         =     1_536
												UpperCAmelCase         =     48
												UpperCAmelCase         =     24
						elif checkpoint == "large":
												UpperCAmelCase         =     2_048
												UpperCAmelCase         =     48
												UpperCAmelCase         =     32
						else:
												raise ValueError(F'Checkpoint should be one of `[\'small\', \'medium\', \'large\']`, got {checkpoint}.'    )
						UpperCAmelCase         =     MusicgenDecoderConfig(
						    hidden_size=lowerCamelCase_    ,   ffn_dim=hidden_size * 4    ,   num_hidden_layers=lowerCamelCase_    ,   num_attention_heads=lowerCamelCase_    ,   )
						return config

@torch.no_grad()
def    lowerCamelCase_(lowerCamelCase_    ,   lowerCamelCase_=None    ,   lowerCamelCase_=None    ,   lowerCamelCase_="cpu"    ) ->       List[str]:
						UpperCAmelCase         =     MusicGen.get_pretrained(lowerCamelCase_    ,   device=lowerCamelCase_    )
						UpperCAmelCase         =     decoder_config_from_checkpoint(lowerCamelCase_    )

						UpperCAmelCase         =     fairseq_model.lm.state_dict()
						UpperCAmelCase       ,		UpperCAmelCase         =     rename_state_dict(
						    lowerCamelCase_    ,   hidden_size=decoder_config.hidden_size    )

						UpperCAmelCase         =     TaEncoderModel.from_pretrained("t5-base"    )
						UpperCAmelCase         =     EncodecModel.from_pretrained("facebook/encodec_32khz"    )
						UpperCAmelCase         =     MusicgenForCausalLM(lowerCamelCase_    ).eval()

						# load all decoder weights - expect that we'll be missing embeddings and enc-dec projection
						UpperCAmelCase       ,		UpperCAmelCase         =     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
						UpperCAmelCase         =     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
						UpperCAmelCase         =     torch.arange(0    ,   8    ,   dtype=torch.long    ).reshape(2    ,   -1    )
						UpperCAmelCase         =     input_ids.reshape(2 * 4    ,   -1    )

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

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

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

						UpperCAmelCase         =     MusicgenProcessor(feature_extractor=lowerCamelCase_    ,   tokenizer=lowerCamelCase_    )

						# set the appropriate bos/pad token ids
						UpperCAmelCase         =     2_048
						UpperCAmelCase         =     2_048

						# set other default generation config params
						UpperCAmelCase         =     int(30 * audio_encoder.config.frame_rate    )
						UpperCAmelCase         =     True
						UpperCAmelCase         =     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__":
		__lowerCamelCase     :							List[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."
		)

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



