


import unittest

from transformers import AutoTokenizer, FalconConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    FalconForCausalLM,
		    FalconForQuestionAnswering,
		    FalconForSequenceClassification,
		    FalconForTokenClassification,
		    FalconModel,
		)







class     __lowercase    :



		"""simple docstring"""

		def __init__(      self						,	A						,	A=3						,	A=7						,	A=True						,	A=True						,	A=False						,	A=True						,	A=99						,	A=32						,	A=5						,	A=4						,	A=37						,	A="gelu"						,	A=0.1						,	A=0.1						,	A=5_12						,	A=16						,	A=2						,	A=0.02						,	A=3						,	A=4						,	A=None						,	)   ->      Dict:







					'''simple docstring'''







					lowerCamelCase  						=   parent
					lowerCamelCase  						=   batch_size
					lowerCamelCase  						=   seq_length
					lowerCamelCase  						=   is_training
					lowerCamelCase  						=   use_input_mask
					lowerCamelCase  						=   use_token_type_ids
					lowerCamelCase  						=   use_labels
					lowerCamelCase  						=   vocab_size
					lowerCamelCase  						=   hidden_size
					lowerCamelCase  						=   num_hidden_layers
					lowerCamelCase  						=   num_attention_heads
					lowerCamelCase  						=   intermediate_size
					lowerCamelCase  						=   hidden_act
					lowerCamelCase  						=   hidden_dropout_prob
					lowerCamelCase  						=   attention_probs_dropout_prob
					lowerCamelCase  						=   max_position_embeddings
					lowerCamelCase  						=   type_vocab_size
					lowerCamelCase  						=   type_sequence_label_size
					lowerCamelCase  						=   initializer_range
					lowerCamelCase  						=   num_labels
					lowerCamelCase  						=   num_choices
					lowerCamelCase  						=   scope







		def  __A					(      self     )   ->      Union[str, Any]:







					'''simple docstring'''







					lowerCamelCase  						=   ids_tensor([self.batch_size, self.seq_length]						,	self.vocab_size     )

					lowerCamelCase  						=   None
					if self.use_input_mask:
								lowerCamelCase  						=   random_attention_mask([self.batch_size, self.seq_length]     )

					lowerCamelCase  						=   None

					lowerCamelCase  						=   None
					lowerCamelCase  						=   None
					lowerCamelCase  						=   None
					if self.use_labels:
								lowerCamelCase  						=   ids_tensor([self.batch_size]						,	self.type_sequence_label_size     )
								lowerCamelCase  						=   ids_tensor([self.batch_size, self.seq_length]						,	self.num_labels     )
								lowerCamelCase  						=   ids_tensor([self.batch_size]						,	self.num_choices     )

					lowerCamelCase  						=   self.get_config()

					return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels







		def  __A					(      self     )   ->      str:







					'''simple docstring'''







					return FalconConfig(
					    vocab_size=self.vocab_size						,	hidden_size=self.hidden_size						,	num_hidden_layers=self.num_hidden_layers						,	num_attention_heads=self.num_attention_heads						,	intermediate_size=self.intermediate_size						,	hidden_act=self.hidden_act						,	hidden_dropout_prob=self.hidden_dropout_prob						,	attention_probs_dropout_prob=self.attention_probs_dropout_prob						,	max_position_embeddings=self.max_position_embeddings						,	type_vocab_size=self.type_vocab_size						,	is_decoder=UpperCAmelCase__						,	initializer_range=self.initializer_range						,	pad_token_id=1						,	new_decoder_architecture=UpperCAmelCase__						,	)







		def  __A					(      self						,	A						,	A						,	A						,	A						,	A						,	A						,	A     )   ->      Dict:







					'''simple docstring'''







					lowerCamelCase  						=   FalconModel(config=UpperCAmelCase__     )
					model.to(UpperCAmelCase__     )
					model.eval()
					lowerCamelCase  						=   model(UpperCAmelCase__						,	attention_mask=UpperCAmelCase__     )
					lowerCamelCase  						=   model(UpperCAmelCase__     )
					self.parent.assertEqual(result.last_hidden_state.shape						,	(self.batch_size, self.seq_length, self.hidden_size)     )







		def  __A					(      self						,	A						,	A						,	A						,	A						,	A						,	A						,	A						,	A						,	A						,	)   ->      Optional[int]:







					'''simple docstring'''







					lowerCamelCase  						=   True
					lowerCamelCase  						=   FalconModel(UpperCAmelCase__     )
					model.to(UpperCAmelCase__     )
					model.eval()
					lowerCamelCase  						=   model(
					    UpperCAmelCase__						,	attention_mask=UpperCAmelCase__						,	encoder_hidden_states=UpperCAmelCase__						,	encoder_attention_mask=UpperCAmelCase__						,	)
					lowerCamelCase  						=   model(
					    UpperCAmelCase__						,	attention_mask=UpperCAmelCase__						,	encoder_hidden_states=UpperCAmelCase__						,	)
					lowerCamelCase  						=   model(UpperCAmelCase__						,	attention_mask=UpperCAmelCase__     )
					self.parent.assertEqual(result.last_hidden_state.shape						,	(self.batch_size, self.seq_length, self.hidden_size)     )







		def  __A					(      self						,	A						,	A						,	A						,	A						,	A						,	A						,	A						,	A						,	A						,	)   ->      Tuple:







					'''simple docstring'''







					lowerCamelCase  						=   FalconForCausalLM(config=UpperCAmelCase__     )
					model.to(UpperCAmelCase__     )
					model.eval()
					lowerCamelCase  						=   model(UpperCAmelCase__						,	attention_mask=UpperCAmelCase__						,	labels=UpperCAmelCase__     )
					self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.seq_length, self.vocab_size)     )







		def  __A					(      self						,	A						,	A						,	A						,	A						,	A						,	A						,	A						,	A						,	A						,	)   ->      Optional[int]:







					'''simple docstring'''







					lowerCamelCase  						=   True
					lowerCamelCase  						=   True
					lowerCamelCase  						=   FalconForCausalLM(config=UpperCAmelCase__     )
					model.to(UpperCAmelCase__     )
					model.eval()

					# first forward pass
					lowerCamelCase  						=   model(
					    UpperCAmelCase__						,	attention_mask=UpperCAmelCase__						,	encoder_hidden_states=UpperCAmelCase__						,	encoder_attention_mask=UpperCAmelCase__						,	use_cache=UpperCAmelCase__						,	)
					lowerCamelCase  						=   outputs.past_key_values

					# create hypothetical multiple next token and extent to next_input_ids
					lowerCamelCase  						=   ids_tensor((self.batch_size, 3)						,	config.vocab_size     )
					lowerCamelCase  						=   ids_tensor((self.batch_size, 3)						,	vocab_size=2     )

					# append to next input_ids and
					lowerCamelCase  						=   torch.cat([input_ids, next_tokens]						,	dim=-1     )
					lowerCamelCase  						=   torch.cat([input_mask, next_mask]						,	dim=-1     )

					lowerCamelCase  						=   model(
					    UpperCAmelCase__						,	attention_mask=UpperCAmelCase__						,	encoder_hidden_states=UpperCAmelCase__						,	encoder_attention_mask=UpperCAmelCase__						,	output_hidden_states=UpperCAmelCase__						,	)["""hidden_states"""][0]
					lowerCamelCase  						=   model(
					    UpperCAmelCase__						,	attention_mask=UpperCAmelCase__						,	encoder_hidden_states=UpperCAmelCase__						,	encoder_attention_mask=UpperCAmelCase__						,	past_key_values=UpperCAmelCase__						,	output_hidden_states=UpperCAmelCase__						,	)["""hidden_states"""][0]

					# select random slice
					lowerCamelCase  						=   ids_tensor((1,)						,	output_from_past.shape[-1]     ).item()
					lowerCamelCase  						=   output_from_no_past[:, -3:, random_slice_idx].detach()
					lowerCamelCase  						=   output_from_past[:, :, random_slice_idx].detach()

					self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]     )

					# test that outputs are equal for slice
					self.parent.assertTrue(torch.allclose(UpperCAmelCase__						,	UpperCAmelCase__						,	atol=1e-3     )     )







		def  __A					(      self     )   ->      List[Any]:







					'''simple docstring'''







					lowerCamelCase  						=   self.prepare_config_and_inputs()
					(
					    (
					    lowerCamelCase  
					)						,						(
					    lowerCamelCase  
					)						,						(
					    lowerCamelCase  
					)						,						(
					    lowerCamelCase  
					)						,						(
					    lowerCamelCase  
					)						,						(
					    lowerCamelCase  
					)						,						(
					    lowerCamelCase  
					)						,						  
					)  						=   config_and_inputs
					lowerCamelCase  						=   {"""input_ids""": input_ids, """attention_mask""": input_mask}
					return config, inputs_dict







@require_torch
class     __lowercase    (				__lowercase       ,				__lowercase       ,				__lowercase       ,				unittest.TestCase   ):



		"""simple docstring"""

		UpperCamelCase			: List[Any]           =						(
		    (
		        FalconModel,
		        FalconForCausalLM,
		        FalconForSequenceClassification,
		        FalconForTokenClassification,
		        FalconForQuestionAnswering,
		    )
		    if is_torch_available()
		    else ()
		)
		UpperCamelCase			: List[Any]           =						(FalconForCausalLM,) if is_torch_available() else ()
		UpperCamelCase			: Tuple           =						(
		    {
		        '''feature-extraction''': FalconModel,
		        '''text-classification''': FalconForSequenceClassification,
		        '''text-generation''': FalconForCausalLM,
		        '''question-answering''': FalconForQuestionAnswering,
		        '''token-classification''': FalconForTokenClassification,
		        '''zero-shot''': FalconForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		UpperCamelCase			: str           =						False
		UpperCamelCase			: Tuple           =						False







		def  __A					(      self     )   ->      Dict:







					'''simple docstring'''







					lowerCamelCase  						=   FalconModelTester(self     )
					lowerCamelCase  						=   ConfigTester(self						,	config_class=UpperCAmelCase__						,	hidden_size=37     )







		def  __A					(      self     )   ->      Tuple:







					'''simple docstring'''







					self.config_tester.run_common_tests()







		def  __A					(      self     )   ->      Union[str, Any]:







					'''simple docstring'''







					lowerCamelCase  						=   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*UpperCAmelCase__     )







		def  __A					(      self     )   ->      Any:







					'''simple docstring'''







					lowerCamelCase						,						*lowerCamelCase  						=   self.model_tester.prepare_config_and_inputs()
					for alibi in [True, False]:
								lowerCamelCase  						=   alibi
								self.model_tester.create_and_check_model(UpperCAmelCase__						,	*UpperCAmelCase__     )







		def  __A					(      self     )   ->      List[str]:







					'''simple docstring'''







					lowerCamelCase						,						lowerCamelCase  						=   self.model_tester.prepare_config_and_inputs_for_common()
					lowerCamelCase  						=   3
					lowerCamelCase  						=   input_dict["""input_ids"""]
					lowerCamelCase  						=   input_ids.ne(1     ).to(UpperCAmelCase__     )
					lowerCamelCase  						=   ids_tensor([self.model_tester.batch_size]						,	self.model_tester.type_sequence_label_size     )
					lowerCamelCase  						=   FalconForSequenceClassification(UpperCAmelCase__     )
					model.to(UpperCAmelCase__     )
					model.eval()
					lowerCamelCase  						=   model(UpperCAmelCase__						,	attention_mask=UpperCAmelCase__						,	labels=UpperCAmelCase__     )
					self.assertEqual(result.logits.shape						,	(self.model_tester.batch_size, self.model_tester.num_labels)     )







		def  __A					(      self     )   ->      Dict:







					'''simple docstring'''







					lowerCamelCase						,						lowerCamelCase  						=   self.model_tester.prepare_config_and_inputs_for_common()
					lowerCamelCase  						=   3
					lowerCamelCase  						=   """single_label_classification"""
					lowerCamelCase  						=   input_dict["""input_ids"""]
					lowerCamelCase  						=   input_ids.ne(1     ).to(UpperCAmelCase__     )
					lowerCamelCase  						=   ids_tensor([self.model_tester.batch_size]						,	self.model_tester.type_sequence_label_size     )
					lowerCamelCase  						=   FalconForSequenceClassification(UpperCAmelCase__     )
					model.to(UpperCAmelCase__     )
					model.eval()
					lowerCamelCase  						=   model(UpperCAmelCase__						,	attention_mask=UpperCAmelCase__						,	labels=UpperCAmelCase__     )
					self.assertEqual(result.logits.shape						,	(self.model_tester.batch_size, self.model_tester.num_labels)     )







		def  __A					(      self     )   ->      int:







					'''simple docstring'''







					lowerCamelCase						,						lowerCamelCase  						=   self.model_tester.prepare_config_and_inputs_for_common()
					lowerCamelCase  						=   input_dict["""input_ids"""]
					lowerCamelCase  						=   FalconForCausalLM(UpperCAmelCase__     )
					model.to(UpperCAmelCase__     )
					model.eval()
					lowerCamelCase  						=   model(UpperCAmelCase__						,	use_cache=UpperCAmelCase__     )
					lowerCamelCase  						=   input_ids.shape[0]
					lowerCamelCase  						=   model._convert_to_rw_cache(result.past_key_values     )
					lowerCamelCase  						=   model._convert_cache_to_standard_format(UpperCAmelCase__						,	UpperCAmelCase__     )
					for layer in range(len(UpperCAmelCase__     )     ):
								for tensor_idx in range(2     ):
											self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3     )
											self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4     )
											self.assertTrue(
											    torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx]     )     )







		def  __A					(      self     )   ->      Dict:







					'''simple docstring'''







					lowerCamelCase						,						lowerCamelCase  						=   self.model_tester.prepare_config_and_inputs_for_common()
					lowerCamelCase  						=   3
					lowerCamelCase  						=   """multi_label_classification"""
					lowerCamelCase  						=   input_dict["""input_ids"""]
					lowerCamelCase  						=   input_ids.ne(1     ).to(UpperCAmelCase__     )
					lowerCamelCase  						=   ids_tensor(
					    [self.model_tester.batch_size, config.num_labels]						,	self.model_tester.type_sequence_label_size     ).to(torch.float     )
					lowerCamelCase  						=   FalconForSequenceClassification(UpperCAmelCase__     )
					model.to(UpperCAmelCase__     )
					model.eval()
					lowerCamelCase  						=   model(UpperCAmelCase__						,	attention_mask=UpperCAmelCase__						,	labels=UpperCAmelCase__     )
					self.assertEqual(result.logits.shape						,	(self.model_tester.batch_size, self.model_tester.num_labels)     )







		def  __A					(      self     )   ->      Optional[Any]:







					'''simple docstring'''







					for model_class in self.all_generative_model_classes:
								lowerCamelCase						,						lowerCamelCase  						=   self.model_tester.prepare_config_and_inputs_for_common()

								# If it doesn't support cache, pass the test
								if not hasattr(UpperCAmelCase__						,	"""use_cache"""     ):
											return

								lowerCamelCase  						=   model_class(UpperCAmelCase__     ).to(UpperCAmelCase__     )
								if "use_cache" not in inputs:
											lowerCamelCase  						=   True
								lowerCamelCase  						=   model(**UpperCAmelCase__     )

								# If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format)
								if "past_key_values" not in outputs:
											return

								lowerCamelCase  						=   (
								    getattr(UpperCAmelCase__						,	"""decoder_layers"""						,	UpperCAmelCase__     )
								    or getattr(UpperCAmelCase__						,	"""num_decoder_layers"""						,	UpperCAmelCase__     )
								    or config.num_hidden_layers
								)
								lowerCamelCase  						=   getattr(UpperCAmelCase__						,	"""num_kv_heads"""						,	config.num_attention_heads     )
								lowerCamelCase  						=   getattr(UpperCAmelCase__						,	"""d_model"""						,	config.hidden_size     )
								lowerCamelCase  						=   embed_dim // num_attention_heads

								lowerCamelCase  						=   outputs["""past_key_values"""]
								self.assertEqual(len(UpperCAmelCase__     )						,	UpperCAmelCase__     )

								lowerCamelCase						,						lowerCamelCase  						=   inputs["""input_ids"""].shape
								for i in range(UpperCAmelCase__     ):
											if config.new_decoder_architecture:
														lowerCamelCase  						=   config.num_attention_heads
											elif config.multi_query:
														lowerCamelCase  						=   1
											self.assertEqual(len(past_kv[0]     )						,	2     )  # K V for the decoder = 2
											self.assertEqual(
											    past_kv[i][0].shape						,	(batch_size, num_attention_heads, seq_length, per_head_embed_dim)     )
											self.assertEqual(
											    past_kv[i][1].shape						,	(batch_size, num_attention_heads, seq_length, per_head_embed_dim)     )







@require_torch
class     __lowercase    (				unittest.TestCase   ):



		"""simple docstring"""

		@slow
		def  __A					(      self     )   ->      List[str]:







					'''simple docstring'''







					lowerCamelCase  						=   AutoTokenizer.from_pretrained("""Rocketknight1/falcon-rw-1b"""     )
					lowerCamelCase  						=   FalconForCausalLM.from_pretrained("""Rocketknight1/falcon-rw-1b"""     )
					model.eval()
					model.to(UpperCAmelCase__     )
					lowerCamelCase  						=   tokenizer("""My favorite food is"""						,	return_tensors="""pt"""     ).to(UpperCAmelCase__     )

					lowerCamelCase  						=   (
					    """My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday."""
					)

					lowerCamelCase  						=   model.generate(**UpperCAmelCase__						,	do_sample=UpperCAmelCase__						,	max_new_tokens=19     )
					lowerCamelCase  						=   tokenizer.batch_decode(UpperCAmelCase__     )[0]

					self.assertEqual(UpperCAmelCase__						,	UpperCAmelCase__     )







		@slow
		def  __A					(      self     )   ->      List[Any]:







					'''simple docstring'''







					for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
								lowerCamelCase  						=   AutoTokenizer.from_pretrained(UpperCAmelCase__     )
								lowerCamelCase  						=   FalconForCausalLM.from_pretrained(UpperCAmelCase__     )
								model.eval()
								model.to(UpperCAmelCase__     )
								lowerCamelCase  						=   tokenizer("""My favorite food is"""						,	return_tensors="""pt"""     ).to(UpperCAmelCase__     )

								# We just test that these run without errors - the models are randomly initialized
								# and so the actual text outputs will be garbage
								model.generate(**UpperCAmelCase__						,	do_sample=UpperCAmelCase__						,	max_new_tokens=4     )
								model.generate(**UpperCAmelCase__						,	do_sample=UpperCAmelCase__						,	max_new_tokens=4     )
								model.generate(**UpperCAmelCase__						,	num_beams=2						,	max_new_tokens=4     )







		@slow
		def  __A					(      self     )   ->      Any:







					'''simple docstring'''







					with torch.no_grad():
								for repo in [
								    "Rocketknight1/falcon-rw-1b",
								    "Rocketknight1/tiny-random-falcon-7b",
								    "Rocketknight1/tiny-random-falcon-40b",
								]:
											lowerCamelCase  						=   AutoTokenizer.from_pretrained(UpperCAmelCase__     )
											lowerCamelCase  						=   FalconForCausalLM.from_pretrained(UpperCAmelCase__     )
											model.eval()
											model.to(device=UpperCAmelCase__     )
											lowerCamelCase  						=   tokenizer("""My favorite food is"""						,	return_tensors="""pt"""     ).to(UpperCAmelCase__     )

											# Test results are the same with and without cache
											lowerCamelCase  						=   model.generate(**UpperCAmelCase__						,	do_sample=UpperCAmelCase__						,	max_new_tokens=20						,	use_cache=UpperCAmelCase__     )
											lowerCamelCase  						=   model.generate(**UpperCAmelCase__						,	do_sample=UpperCAmelCase__						,	max_new_tokens=20						,	use_cache=UpperCAmelCase__     )
											self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0     )





'''simple docstring'''




import argparse

import torch

from transformers import (
    EncodecConfig,
    EncodecFeatureExtractor,
    EncodecModel,
    logging,
)


# checkpoints downloaded from:
# https://dl.fbaipublicfiles.com/encodec/v0/encodec_24khz-d7cc33bc.th
# https://huggingface.co/facebook/musicgen-small/resolve/main/compression_state_dict.bin
# https://dl.fbaipublicfiles.com/encodec/v0/encodec_48khz-7e698e3e.th


logging.set_verbosity_info()
__snake_case    						=logging.get_logger("""transformers.models.encodec""")

__snake_case    						={
    """quantizer.vq.layers.*._codebook.inited""": """quantizer.layers.*.codebook.inited""",
    """quantizer.vq.layers.*._codebook.cluster_size""": """quantizer.layers.*.codebook.cluster_size""",
    """quantizer.vq.layers.*._codebook.embed""": """quantizer.layers.*.codebook.embed""",
    """quantizer.vq.layers.*._codebook.embed_avg""": """quantizer.layers.*.codebook.embed_avg""",
}
__snake_case    						={
    """encoder.model.0.conv.conv""": """encoder.layers.0.conv""",
    """encoder.model.1.block.1.conv.conv""": """encoder.layers.1.block.1.conv""",
    """encoder.model.1.block.3.conv.conv""": """encoder.layers.1.block.3.conv""",
    """encoder.model.1.shortcut.conv.conv""": """encoder.layers.1.shortcut.conv""",
    """encoder.model.3.conv.conv""": """encoder.layers.3.conv""",
    """encoder.model.4.block.1.conv.conv""": """encoder.layers.4.block.1.conv""",
    """encoder.model.4.block.3.conv.conv""": """encoder.layers.4.block.3.conv""",
    """encoder.model.4.shortcut.conv.conv""": """encoder.layers.4.shortcut.conv""",
    """encoder.model.6.conv.conv""": """encoder.layers.6.conv""",
    """encoder.model.7.block.1.conv.conv""": """encoder.layers.7.block.1.conv""",
    """encoder.model.7.block.3.conv.conv""": """encoder.layers.7.block.3.conv""",
    """encoder.model.7.shortcut.conv.conv""": """encoder.layers.7.shortcut.conv""",
    """encoder.model.9.conv.conv""": """encoder.layers.9.conv""",
    """encoder.model.10.block.1.conv.conv""": """encoder.layers.10.block.1.conv""",
    """encoder.model.10.block.3.conv.conv""": """encoder.layers.10.block.3.conv""",
    """encoder.model.10.shortcut.conv.conv""": """encoder.layers.10.shortcut.conv""",
    """encoder.model.12.conv.conv""": """encoder.layers.12.conv""",
    """encoder.model.13.lstm""": """encoder.layers.13.lstm""",
    """encoder.model.15.conv.conv""": """encoder.layers.15.conv""",
}
__snake_case    						={
    """encoder.model.0.conv.norm""": """encoder.layers.0.norm""",
    """encoder.model.1.block.1.conv.norm""": """encoder.layers.1.block.1.norm""",
    """encoder.model.1.block.3.conv.norm""": """encoder.layers.1.block.3.norm""",
    """encoder.model.1.shortcut.conv.norm""": """encoder.layers.1.shortcut.norm""",
    """encoder.model.3.conv.norm""": """encoder.layers.3.norm""",
    """encoder.model.4.block.1.conv.norm""": """encoder.layers.4.block.1.norm""",
    """encoder.model.4.block.3.conv.norm""": """encoder.layers.4.block.3.norm""",
    """encoder.model.4.shortcut.conv.norm""": """encoder.layers.4.shortcut.norm""",
    """encoder.model.6.conv.norm""": """encoder.layers.6.norm""",
    """encoder.model.7.block.1.conv.norm""": """encoder.layers.7.block.1.norm""",
    """encoder.model.7.block.3.conv.norm""": """encoder.layers.7.block.3.norm""",
    """encoder.model.7.shortcut.conv.norm""": """encoder.layers.7.shortcut.norm""",
    """encoder.model.9.conv.norm""": """encoder.layers.9.norm""",
    """encoder.model.10.block.1.conv.norm""": """encoder.layers.10.block.1.norm""",
    """encoder.model.10.block.3.conv.norm""": """encoder.layers.10.block.3.norm""",
    """encoder.model.10.shortcut.conv.norm""": """encoder.layers.10.shortcut.norm""",
    """encoder.model.12.conv.norm""": """encoder.layers.12.norm""",
    """encoder.model.15.conv.norm""": """encoder.layers.15.norm""",
}
__snake_case    						={
    """decoder.model.0.conv.conv""": """decoder.layers.0.conv""",
    """decoder.model.1.lstm""": """decoder.layers.1.lstm""",
    """decoder.model.3.convtr.convtr""": """decoder.layers.3.conv""",
    """decoder.model.4.block.1.conv.conv""": """decoder.layers.4.block.1.conv""",
    """decoder.model.4.block.3.conv.conv""": """decoder.layers.4.block.3.conv""",
    """decoder.model.4.shortcut.conv.conv""": """decoder.layers.4.shortcut.conv""",
    """decoder.model.6.convtr.convtr""": """decoder.layers.6.conv""",
    """decoder.model.7.block.1.conv.conv""": """decoder.layers.7.block.1.conv""",
    """decoder.model.7.block.3.conv.conv""": """decoder.layers.7.block.3.conv""",
    """decoder.model.7.shortcut.conv.conv""": """decoder.layers.7.shortcut.conv""",
    """decoder.model.9.convtr.convtr""": """decoder.layers.9.conv""",
    """decoder.model.10.block.1.conv.conv""": """decoder.layers.10.block.1.conv""",
    """decoder.model.10.block.3.conv.conv""": """decoder.layers.10.block.3.conv""",
    """decoder.model.10.shortcut.conv.conv""": """decoder.layers.10.shortcut.conv""",
    """decoder.model.12.convtr.convtr""": """decoder.layers.12.conv""",
    """decoder.model.13.block.1.conv.conv""": """decoder.layers.13.block.1.conv""",
    """decoder.model.13.block.3.conv.conv""": """decoder.layers.13.block.3.conv""",
    """decoder.model.13.shortcut.conv.conv""": """decoder.layers.13.shortcut.conv""",
    """decoder.model.15.conv.conv""": """decoder.layers.15.conv""",
}
__snake_case    						={
    """decoder.model.0.conv.norm""": """decoder.layers.0.norm""",
    """decoder.model.3.convtr.norm""": """decoder.layers.3.norm""",
    """decoder.model.4.block.1.conv.norm""": """decoder.layers.4.block.1.norm""",
    """decoder.model.4.block.3.conv.norm""": """decoder.layers.4.block.3.norm""",
    """decoder.model.4.shortcut.conv.norm""": """decoder.layers.4.shortcut.norm""",
    """decoder.model.6.convtr.norm""": """decoder.layers.6.norm""",
    """decoder.model.7.block.1.conv.norm""": """decoder.layers.7.block.1.norm""",
    """decoder.model.7.block.3.conv.norm""": """decoder.layers.7.block.3.norm""",
    """decoder.model.7.shortcut.conv.norm""": """decoder.layers.7.shortcut.norm""",
    """decoder.model.9.convtr.norm""": """decoder.layers.9.norm""",
    """decoder.model.10.block.1.conv.norm""": """decoder.layers.10.block.1.norm""",
    """decoder.model.10.block.3.conv.norm""": """decoder.layers.10.block.3.norm""",
    """decoder.model.10.shortcut.conv.norm""": """decoder.layers.10.shortcut.norm""",
    """decoder.model.12.convtr.norm""": """decoder.layers.12.norm""",
    """decoder.model.13.block.1.conv.norm""": """decoder.layers.13.block.1.norm""",
    """decoder.model.13.block.3.conv.norm""": """decoder.layers.13.block.3.norm""",
    """decoder.model.13.shortcut.conv.norm""": """decoder.layers.13.shortcut.norm""",
    """decoder.model.15.conv.norm""": """decoder.layers.15.norm""",
}
__snake_case    						={
    **MAPPING_QUANTIZER,
    **MAPPING_ENCODER,
    **MAPPING_DECODER,
}
__snake_case    						={
    **MAPPING_QUANTIZER,
    **MAPPING_ENCODER,
    **MAPPING_ENCODER_48K,
    **MAPPING_DECODER,
    **MAPPING_DECODER_48K,
}
__snake_case    						=[]
__snake_case    						=[]





def 	a_					( lowerCamelCase	:     Optional[Any]	, lowerCamelCase	:     Union[str, Any]	, lowerCamelCase	:     int	, lowerCamelCase	:     Any	, lowerCamelCase	:     List[str]				):
   for attribute in key.split('.'				):
      lowerCAmelCase     =			getattr(lowerCamelCase	, lowerCamelCase				)

   if weight_type is not None:
      lowerCAmelCase     =			getattr(lowerCamelCase	, lowerCamelCase				).shape
   else:
      lowerCAmelCase     =			hf_pointer.shape

   if hf_shape != value.shape:
      raise ValueError(
          f'''Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be'''
          f''' {value.shape} for {full_name}'''				)

   if weight_type == "weight":
      lowerCAmelCase     =			value
   elif weight_type == "weight_g":
      lowerCAmelCase     =			value
   elif weight_type == "weight_v":
      lowerCAmelCase     =			value
   elif weight_type == "bias":
      lowerCAmelCase     =			value
   elif weight_type == "running_mean":
      lowerCAmelCase     =			value
   elif weight_type == "running_var":
      lowerCAmelCase     =			value
   elif weight_type == "num_batches_tracked":
      lowerCAmelCase     =			value
   elif weight_type == "weight_ih_l0":
      lowerCAmelCase     =			value
   elif weight_type == "weight_hh_l0":
      lowerCAmelCase     =			value
   elif weight_type == "bias_ih_l0":
      lowerCAmelCase     =			value
   elif weight_type == "bias_hh_l0":
      lowerCAmelCase     =			value
   elif weight_type == "weight_ih_l1":
      lowerCAmelCase     =			value
   elif weight_type == "weight_hh_l1":
      lowerCAmelCase     =			value
   elif weight_type == "bias_ih_l1":
      lowerCAmelCase     =			value
   elif weight_type == "bias_hh_l1":
      lowerCAmelCase     =			value
   else:
      lowerCAmelCase     =			value

   logger.info(f'''{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}.'''				)





def 	a_					( lowerCamelCase	:     Optional[Any]	, lowerCamelCase	:     Optional[Any]				):
   for key in ignore_keys:
      if key.endswith('.*'				):
         if name.startswith(key[:-1]				):
            return True
      elif ".*." in key:
         lowerCAmelCase     ,  lowerCAmelCase     =			key.split('.*.'				)
         if prefix in name and suffix in name:
            return True
      elif key in name:
         return True
   return False





def 	a_					( lowerCamelCase	:     List[Any]	, lowerCamelCase	:     Any	, lowerCamelCase	:     str				):
   lowerCAmelCase     =			[]

   if model_name == "encodec_24khz" or "encodec_32khz":
      lowerCAmelCase     =			MAPPING_24K
   elif model_name == "encodec_48khz":
      lowerCAmelCase     =			MAPPING_48K
   else:
      raise ValueError(f'''Unsupported model: {model_name}'''				)

   for name, value in orig_dict.items():
      if should_ignore(lowerCamelCase	, lowerCamelCase				):
         logger.info(f'''{name} was ignored'''				)
         continue

      lowerCAmelCase     =			False
      for key, mapped_key in MAPPING.items():
         if "*" in key:
            lowerCAmelCase     ,  lowerCAmelCase     =			key.split('.*.'				)
            if prefix in name and suffix in name:
               lowerCAmelCase     =			suffix

         if key in name:
            # HACK otherwise .embed gets initialized with .embed_avg too
            if key.endswith('embed'				) and name.endswith('embed_avg'				):
               continue

            lowerCAmelCase     =			True
            if "*" in mapped_key:
               lowerCAmelCase     =			name.split(lowerCamelCase				)[0].split('.'				)[-2]
               lowerCAmelCase     =			mapped_key.replace('*'	, lowerCamelCase				)
            if "weight_g" in name:
               lowerCAmelCase     =			'weight_g'
            elif "weight_v" in name:
               lowerCAmelCase     =			'weight_v'
            elif "weight_ih_l0" in name:
               lowerCAmelCase     =			'weight_ih_l0'
            elif "weight_hh_l0" in name:
               lowerCAmelCase     =			'weight_hh_l0'
            elif "bias_ih_l0" in name:
               lowerCAmelCase     =			'bias_ih_l0'
            elif "bias_hh_l0" in name:
               lowerCAmelCase     =			'bias_hh_l0'
            elif "weight_ih_l1" in name:
               lowerCAmelCase     =			'weight_ih_l1'
            elif "weight_hh_l1" in name:
               lowerCAmelCase     =			'weight_hh_l1'
            elif "bias_ih_l1" in name:
               lowerCAmelCase     =			'bias_ih_l1'
            elif "bias_hh_l1" in name:
               lowerCAmelCase     =			'bias_hh_l1'
            elif "bias" in name:
               lowerCAmelCase     =			'bias'
            elif "weight" in name:
               lowerCAmelCase     =			'weight'
            elif "running_mean" in name:
               lowerCAmelCase     =			'running_mean'
            elif "running_var" in name:
               lowerCAmelCase     =			'running_var'
            elif "num_batches_tracked" in name:
               lowerCAmelCase     =			'num_batches_tracked'
            else:
               lowerCAmelCase     =			None
            set_recursively(lowerCamelCase	, lowerCamelCase	, lowerCamelCase	, lowerCamelCase	, lowerCamelCase				)
         continue
      if not is_used:
         unused_weights.append(lowerCamelCase				)

   logger.warning(f'''Unused weights: {unused_weights}'''				)





@torch.no_grad()
def 	a_					( lowerCamelCase	:     Optional[int]	, lowerCamelCase	:     Union[str, Any]	, lowerCamelCase	:     str	, lowerCamelCase	:     Dict=None	, lowerCamelCase	:     Union[str, Any]=None	, ):
   if config_path is not None:
      lowerCAmelCase     =			EncodecConfig.from_pretrained(lowerCamelCase				)
   else:
      lowerCAmelCase     =			EncodecConfig()

   if model_name == "encodec_24khz":
      pass  # config is already correct
   elif model_name == "encodec_32khz":
      lowerCAmelCase     =			[8, 5, 4, 4]
      lowerCAmelCase     =			[2.2]
      lowerCAmelCase     =			64
      lowerCAmelCase     =			32000
      lowerCAmelCase     =			2048
      lowerCAmelCase     =			False
      lowerCAmelCase     =			False
      lowerCAmelCase     =			False
   elif model_name == "encodec_48khz":
      lowerCAmelCase     =			[8, 5, 4, 2]
      lowerCAmelCase     =			[3.0, 6.0, 12.0, 24.0]
      lowerCAmelCase     =			48000
      lowerCAmelCase     =			2
      lowerCAmelCase     =			False
      lowerCAmelCase     =			'time_group_norm'
      lowerCAmelCase     =			True
      lowerCAmelCase     =			1.0
      lowerCAmelCase     =			0.01
   else:
      raise ValueError(f'''Unknown model name: {model_name}'''				)

   lowerCAmelCase     =			EncodecModel(lowerCamelCase				)

   lowerCAmelCase     =			EncodecFeatureExtractor(
       feature_size=config.audio_channels	, sampling_rate=config.sampling_rate	, chunk_length_s=config.chunk_length_s	, overlap=config.overlap	, )
   feature_extractor.save_pretrained(lowerCamelCase				)

   lowerCAmelCase     =			torch.load(lowerCamelCase				)
   if "best_state" in original_checkpoint:
      # we might have a training state saved, in which case discard the yaml results and just retain the weights
      lowerCAmelCase     =			original_checkpoint['best_state']
   recursively_load_weights(lowerCamelCase	, lowerCamelCase	, lowerCamelCase				)
   model.save_pretrained(lowerCamelCase				)

   if repo_id:
      print('Pushing to the hub...'				)
      feature_extractor.push_to_hub(lowerCamelCase				)
      model.push_to_hub(lowerCamelCase				)


if __name__ == "__main__":
       __snake_case    						=argparse.ArgumentParser()
       parser.add_argument(
           """--model""",
           default="""encodec_24khz""",
           type=str,
           help="""The model to convert. Should be one of 'encodec_24khz', 'encodec_32khz', 'encodec_48khz'.""",
       )
       parser.add_argument("""--checkpoint_path""", required=True, default=None, type=str, help="""Path to original checkpoint""")
       parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""")
       parser.add_argument(
           """--pytorch_dump_folder_path""", required=True, default=None, type=str, help="""Path to the output PyTorch model."""
       )
       parser.add_argument(
           """--push_to_hub""", default=None, type=str, help="""Where to upload the converted model on the 🤗 hub."""
       )

       __snake_case    						=parser.parse_args()
       convert_checkpoint(
           args.model,
           args.checkpoint_path,
           args.pytorch_dump_folder_path,
           args.config_path,
           args.push_to_hub,
       )





