





'''simple docstring'''






from ...configuration_utils import PretrainedConfig
from ...utils import logging


A__				:   int      =logging.get_logger(__name__)

A__				:   Union[str, Any]      ={
    '''tiiuae/falcon-40b''': '''https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json''',
    '''tiiuae/falcon-7b''': '''https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json''',
}




class  UpperCAmelCase     (			snake_case_   ):
      _lowercase:			Optional[int]						     =   '''falcon'''
      _lowercase:			Dict						     =   ['''past_key_values''']




      def __init__(			self			: Optional[int]			,			__snake_case			: int=6_50_24			,			__snake_case			: Optional[int]=45_44			,			__snake_case			: str=32			,			__snake_case			: Dict=71			,			__snake_case			: List[Any]=1E-5			,			__snake_case			: Union[str, Any]=0.02			,			__snake_case			: Optional[int]=True			,			__snake_case			: Optional[Any]=0.0			,			__snake_case			: str=0.0			,			__snake_case			: Optional[Any]=None			,			__snake_case			: Union[str, Any]=False			,			__snake_case			: List[str]=False			,			__snake_case			: int=True			,			__snake_case			: int=True			,			__snake_case			: Any=False			,			__snake_case			: str=11			,			__snake_case			: int=11			,			**__snake_case			: Optional[int]			,			)				->      List[str]:
            _lowerCAmelCase							     =							vocab_size
            # Backward compatibility with n_embed kwarg
            _lowerCAmelCase							     =							kwargs.pop("""n_embed"""			,			__snake_case  )
            _lowerCAmelCase							     =							hidden_size if n_embed is None else n_embed
            _lowerCAmelCase							     =							num_hidden_layers
            _lowerCAmelCase							     =							num_attention_heads
            _lowerCAmelCase							     =							layer_norm_epsilon
            _lowerCAmelCase							     =							initializer_range
            _lowerCAmelCase							     =							use_cache
            _lowerCAmelCase							     =							hidden_dropout
            _lowerCAmelCase							     =							attention_dropout

            _lowerCAmelCase							     =							bos_token_id
            _lowerCAmelCase							     =							eos_token_id
            _lowerCAmelCase							     =							num_attention_heads if num_kv_heads is None else num_kv_heads
            _lowerCAmelCase							     =							alibi
            _lowerCAmelCase							     =							new_decoder_architecture
            _lowerCAmelCase							     =							multi_query  # Ignored when new_decoder_architecture is True
            _lowerCAmelCase							     =							parallel_attn
            _lowerCAmelCase							     =							bias

            super().__init__(bos_token_id=__snake_case			,			eos_token_id=__snake_case			,			**__snake_case  )




      @property
      def 				lowercase__    (			self			: str  )				->      Optional[Any]:
            return self.hidden_size // self.num_attention_heads




      @property
      def 				lowercase__    (			self			: Any  )				->      Tuple:
            return not self.alibi










'''simple docstring'''

import argparse

import torch
from torch import nn

from transformers import MBartConfig, MBartForConditionalGeneration

def _SCREAMING_SNAKE_CASE      (A       )       ->       Optional[Any]:






       """simple docstring"""







       lowercase__      			= [
           '''encoder.version''',
           '''decoder.version''',
           '''model.encoder.version''',
           '''model.decoder.version''',
           '''_float_tensor''',
           '''decoder.output_projection.weight''',
       ]
       for k in ignore_keys:
              state_dict.pop(A  , A       )

def _SCREAMING_SNAKE_CASE      (A       )       ->       List[str]:






       """simple docstring"""







       lowercase__			,lowercase__      			= emb.weight.shape
       lowercase__      			= nn.Linear(A  , A  , bias=A       )
       lowercase__      			= emb.weight.data
       return lin_layer

def _SCREAMING_SNAKE_CASE      (A  , A="facebook/mbart-large-en-ro"  , A=False  , A=False       )       ->       Union[str, Any]:






       """simple docstring"""







       lowercase__      			= torch.load(A  , map_location='''cpu'''       )['''model''']
       remove_ignore_keys_(A       )
       lowercase__      			= state_dict['''encoder.embed_tokens.weight'''].shape[0]

       lowercase__      			= MBartConfig.from_pretrained(A  , vocab_size=A       )
       if mbart_aa and finetuned:
              lowercase__      			= '''relu'''

       lowercase__      			= state_dict['''decoder.embed_tokens.weight''']
       lowercase__      			= MBartForConditionalGeneration(A       )
       model.model.load_state_dict(A       )

       if finetuned:
              lowercase__      			= make_linear_from_emb(model.model.shared       )

       return model


if __name__ == "__main__":
 lowerCamelCase							:   Any  						=	argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     'fairseq_path', type=str, help='bart.large, bart.large.cnn or a 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.')
 parser.add_argument(
     '--hf_config',
     default='facebook/mbart-large-cc25',
     type=str,
     help='Which huggingface architecture to use: mbart-large',
 )
 parser.add_argument('--mbart_50', action='store_true', help='whether the model is mMART-50 checkpoint')
 parser.add_argument('--finetuned', action='store_true', help='whether the model is a fine-tuned checkpoint')
 lowerCamelCase							:   Any  						=	parser.parse_args()
 lowerCamelCase							:   List[str]  						=	convert_fairseq_mbart_checkpoint_from_disk(
     args.fairseq_path, hf_config_path=args.hf_config, finetuned=args.finetuned, mbart_aa=args.mbart_aa
 )
 model.save_pretrained(args.pytorch_dump_folder_path)




