



'''simple docstring'''

import argparse

import torch
from torch import nn

from transformers import MaMaaaConfig, MaMaaaForConditionalGeneration






def       __lowercase  (_SCREAMING_SNAKE_CASE      :int       ):
				SCREAMING_SNAKE_CASE  :						Tuple      					= [
				    'encoder.version',
				    'decoder.version',
				    'model.encoder.version',
				    'model.decoder.version',
				    'decoder.output_projection.weight',
				    '_float_tensor',
				    'encoder.embed_positions._float_tensor',
				    'decoder.embed_positions._float_tensor',
				]
				for k in ignore_keys:
								state_dict.pop(_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE       )






def       __lowercase  (_SCREAMING_SNAKE_CASE      :Union[str, Any]       ):
				SCREAMING_SNAKE_CASE  :						Optional[int]      					= emb.weight.shape
				SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= nn.Linear(_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	bias=_SCREAMING_SNAKE_CASE       )
				SCREAMING_SNAKE_CASE  :						Optional[Any]      					= emb.weight.data
				return lin_layer






def       __lowercase  (_SCREAMING_SNAKE_CASE      :str       ):
				SCREAMING_SNAKE_CASE  :						Tuple      					= torch.load(_SCREAMING_SNAKE_CASE    ,	map_location='''cpu'''       )
				SCREAMING_SNAKE_CASE  :						str      					= mam_aaa['args'] or mam_aaa['cfg']['model']
				SCREAMING_SNAKE_CASE  :						str      					= mam_aaa['model']
				remove_ignore_keys_(_SCREAMING_SNAKE_CASE       )
				SCREAMING_SNAKE_CASE  :						Tuple      					= state_dict['encoder.embed_tokens.weight'].shape[0]

				SCREAMING_SNAKE_CASE  :						int      					= MaMaaaConfig(
				    vocab_size=_SCREAMING_SNAKE_CASE    ,	max_position_embeddings=10_24    ,	encoder_layers=args.encoder_layers    ,	decoder_layers=args.decoder_layers    ,	encoder_attention_heads=args.encoder_attention_heads    ,	decoder_attention_heads=args.decoder_attention_heads    ,	encoder_ffn_dim=args.encoder_ffn_embed_dim    ,	decoder_ffn_dim=args.decoder_ffn_embed_dim    ,	d_model=args.encoder_embed_dim    ,	encoder_layerdrop=args.encoder_layerdrop    ,	decoder_layerdrop=args.decoder_layerdrop    ,	dropout=args.dropout    ,	attention_dropout=args.attention_dropout    ,	activation_dropout=args.activation_dropout    ,	activation_function='''relu'''    ,	)

				SCREAMING_SNAKE_CASE  :						Any      					= state_dict['decoder.embed_tokens.weight']
				SCREAMING_SNAKE_CASE  :						Dict      					= MaMaaaForConditionalGeneration(_SCREAMING_SNAKE_CASE       )
				model.model.load_state_dict(_SCREAMING_SNAKE_CASE    ,	strict=_SCREAMING_SNAKE_CASE       )
				SCREAMING_SNAKE_CASE  :						Optional[int]      					= make_linear_from_emb(model.model.shared       )

				return model


if __name__ == "__main__":
	snake_case_			=     argparse.ArgumentParser()
	# Required parameters
	parser.add_argument("""fairseq_path""", type=str, help="""path to a model.pt on local filesystem.""")
	parser.add_argument("""pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""")
	snake_case_			=     parser.parse_args()
	snake_case_			=     convert_fairseq_mamaaa_checkpoint_from_disk(args.fairseq_pathß)
	model.save_pretrained(args.pytorch_dump_folder_path)









'''simple docstring'''

from ..utils import DummyObject, requires_backends


class 	a__     (				metaclass=_lowercase	):
	__magic_name__		:    Dict	    =     ["torch", "transformers", "onnx"]


	def __init__(self : List[str],					*__UpperCAmelCase : Dict,					**__UpperCAmelCase : List[Any]      )     ->			Union[str, Any]:

					"""simple docstring"""

					requires_backends(self,					['''torch''', '''transformers''', '''onnx''']      )


	@classmethod
	def 				lowercase__  (cls : Optional[Any],					*__UpperCAmelCase : Tuple,					**__UpperCAmelCase : int      )     ->			int:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )






	@classmethod
	def 				lowercase__  (cls : Any,					*__UpperCAmelCase : Tuple,					**__UpperCAmelCase : Tuple      )     ->			Tuple:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )



class 	a__     (				metaclass=_lowercase	):
	__magic_name__		:    Any	    =     ["torch", "transformers", "onnx"]


	def __init__(self : Dict,					*__UpperCAmelCase : Union[str, Any],					**__UpperCAmelCase : Optional[Any]      )     ->			int:

					"""simple docstring"""

					requires_backends(self,					['''torch''', '''transformers''', '''onnx''']      )


	@classmethod
	def 				lowercase__  (cls : Any,					*__UpperCAmelCase : Tuple,					**__UpperCAmelCase : List[str]      )     ->			Optional[int]:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )






	@classmethod
	def 				lowercase__  (cls : int,					*__UpperCAmelCase : List[Any],					**__UpperCAmelCase : Any      )     ->			Optional[Any]:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )



class 	a__     (				metaclass=_lowercase	):
	__magic_name__		:    Optional[Any]	    =     ["torch", "transformers", "onnx"]


	def __init__(self : Dict,					*__UpperCAmelCase : Dict,					**__UpperCAmelCase : Any      )     ->			List[str]:

					"""simple docstring"""

					requires_backends(self,					['''torch''', '''transformers''', '''onnx''']      )


	@classmethod
	def 				lowercase__  (cls : Tuple,					*__UpperCAmelCase : Union[str, Any],					**__UpperCAmelCase : int      )     ->			Optional[Any]:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )






	@classmethod
	def 				lowercase__  (cls : Union[str, Any],					*__UpperCAmelCase : List[str],					**__UpperCAmelCase : str      )     ->			Optional[int]:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )



class 	a__     (				metaclass=_lowercase	):
	__magic_name__		:    Tuple	    =     ["torch", "transformers", "onnx"]


	def __init__(self : Union[str, Any],					*__UpperCAmelCase : Optional[int],					**__UpperCAmelCase : List[Any]      )     ->			Tuple:

					"""simple docstring"""

					requires_backends(self,					['''torch''', '''transformers''', '''onnx''']      )


	@classmethod
	def 				lowercase__  (cls : str,					*__UpperCAmelCase : Optional[int],					**__UpperCAmelCase : Tuple      )     ->			str:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )






	@classmethod
	def 				lowercase__  (cls : int,					*__UpperCAmelCase : List[Any],					**__UpperCAmelCase : List[str]      )     ->			Tuple:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )



class 	a__     (				metaclass=_lowercase	):
	__magic_name__		:    List[Any]	    =     ["torch", "transformers", "onnx"]


	def __init__(self : Dict,					*__UpperCAmelCase : Optional[Any],					**__UpperCAmelCase : int      )     ->			List[str]:

					"""simple docstring"""

					requires_backends(self,					['''torch''', '''transformers''', '''onnx''']      )


	@classmethod
	def 				lowercase__  (cls : List[str],					*__UpperCAmelCase : Union[str, Any],					**__UpperCAmelCase : Dict      )     ->			Union[str, Any]:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )






	@classmethod
	def 				lowercase__  (cls : Any,					*__UpperCAmelCase : List[str],					**__UpperCAmelCase : List[Any]      )     ->			Dict:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )



class 	a__     (				metaclass=_lowercase	):
	__magic_name__		:    Dict	    =     ["torch", "transformers", "onnx"]


	def __init__(self : Tuple,					*__UpperCAmelCase : Optional[Any],					**__UpperCAmelCase : List[str]      )     ->			Any:

					"""simple docstring"""

					requires_backends(self,					['''torch''', '''transformers''', '''onnx''']      )


	@classmethod
	def 				lowercase__  (cls : Tuple,					*__UpperCAmelCase : Any,					**__UpperCAmelCase : Union[str, Any]      )     ->			Optional[int]:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )






	@classmethod
	def 				lowercase__  (cls : Optional[Any],					*__UpperCAmelCase : Union[str, Any],					**__UpperCAmelCase : Optional[Any]      )     ->			int:

					"""simple docstring"""

					requires_backends(cls,					['''torch''', '''transformers''', '''onnx''']      )




