



import argparse

import torch
from torch import nn

from transformers import SpeechaTextConfig, SpeechaTextForConditionalGeneration






def        A       (      _lowerCamelCase						):





							'''simple docstring'''




							_lowerCAmelCase  :							Optional[int]				     =					[
							    "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(_lowerCamelCase       ,				_lowerCamelCase						)






def        A       (      _lowerCamelCase						):





							'''simple docstring'''




							_lowerCAmelCase  :							Optional[int]				     =					list(s_dict.keys()						)
							for key in keys:
														if "transformer_layers" in key:
																					_lowerCAmelCase  :							str				     =					s_dict.pop(_lowerCamelCase						)
														elif "subsample" in key:
																					_lowerCAmelCase  :							Optional[int]				     =					s_dict.pop(_lowerCamelCase						)






def        A       (      _lowerCamelCase						):





							'''simple docstring'''




							_lowerCAmelCase     ,					_lowerCAmelCase  :							Tuple				     =					emb.weight.shape
							_lowerCAmelCase  :							Any				     =					nn.Linear(_lowerCamelCase       ,				_lowerCamelCase       ,				bias=_lowerCamelCase						)
							_lowerCAmelCase  :							List[Any]				     =					emb.weight.data
							return lin_layer






def        A       (      _lowerCamelCase       ,				_lowerCamelCase						):





							'''simple docstring'''




							_lowerCAmelCase  :							str				     =					torch.load(_lowerCamelCase       ,				map_location="cpu"						)
							_lowerCAmelCase  :							Optional[Any]				     =					mam_aaa["args"]
							_lowerCAmelCase  :							Optional[Any]				     =					mam_aaa["model"]
							_lowerCAmelCase  :							str				     =					state_dict["decoder.output_projection.weight"]

							remove_ignore_keys_(_lowerCamelCase						)
							rename_keys(_lowerCamelCase						)

							_lowerCAmelCase  :							str				     =					state_dict["decoder.embed_tokens.weight"].shape[0]

							_lowerCAmelCase  :							str				     =					args.share_decoder_input_output_embed

							_lowerCAmelCase  :							List[Any]				     =					[int(_lowerCamelCase						) for i in args.conv_kernel_sizes.split(","						)]
							_lowerCAmelCase  :							Any				     =					SpeechaTextConfig(
							    vocab_size=_lowerCamelCase       ,				max_source_positions=args.max_source_positions       ,				max_target_positions=args.max_target_positions       ,				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       ,				dropout=args.dropout       ,				attention_dropout=args.attention_dropout       ,				activation_dropout=args.activation_dropout       ,				activation_function="relu"       ,				num_conv_layers=len(_lowerCamelCase						)       ,				conv_channels=args.conv_channels       ,				conv_kernel_sizes=_lowerCamelCase       ,				input_feat_per_channel=args.input_feat_per_channel       ,				input_channels=args.input_channels       ,				tie_word_embeddings=_lowerCamelCase       ,				num_beams=5       ,				max_length=200       ,				use_cache=_lowerCamelCase       ,				decoder_start_token_id=2       ,				early_stopping=_lowerCamelCase       ,				)

							_lowerCAmelCase  :							str				     =					SpeechaTextForConditionalGeneration(_lowerCamelCase						)
							_lowerCAmelCase     ,					_lowerCAmelCase  :							Dict				     =					model.model.load_state_dict(_lowerCamelCase       ,				strict=_lowerCamelCase						)
							if len(_lowerCamelCase						) > 0 and not set(_lowerCamelCase						) <= {
							    "encoder.embed_positions.weights",
							    "decoder.embed_positions.weights",
							}:
														raise ValueError(
														    "Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights`  are allowed to be missing,"
														    F" but all the following weights are missing {missing}"						)

							if tie_embeds:
														_lowerCAmelCase  :							Tuple				     =					make_linear_from_emb(model.model.decoder.embed_tokens						)
							else:
														_lowerCAmelCase  :							str				     =					lm_head_weights

							model.save_pretrained(_lowerCamelCase						)


if __name__ == "__main__":
						_snake_case	=						argparse.ArgumentParser()
						# Required parameters
						parser.add_argument("--fairseq_path", type=str, help="Path to the fairseq model (.pt) file.")
						parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.")
						_snake_case	=						parser.parse_args()
						convert_fairseq_sat_checkpoint_to_tfms(args.fairseq_path, args.pytorch_dump_folder_path)





import warnings

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


_snake_case	=						logging.get_logger(__name__)

_snake_case	=						{
    "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/config.json",
    "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/config.json",
}



class 						UpperCAmelCase_					( a):
							lowerCamelCase__         =	'xlnet'
							lowerCamelCase__         =	['mems']
							lowerCamelCase__         =	{
							    'n_token': 'vocab_size',  # Backward compatibility
							    'hidden_size': 'd_model',
							    'num_attention_heads': 'n_head',
							    'num_hidden_layers': 'n_layer',
							}







							def __init__(     self,    __a=3_2000,    __a=1024,    __a=24,    __a=16,    __a=4096,    __a="gelu",    __a=True,    __a="bi",    __a=0.02,    __a=1E-12,    __a=0.1,    __a=512,    __a=None,    __a=True,    __a=False,    __a=False,    __a=-1,    __a=False,    __a="last",    __a=True,    __a="tanh",    __a=0.1,    __a=5,    __a=5,    __a=5,    __a=1,    __a=2,    **__a,    ):

														'''simple docstring'''

														_lowerCAmelCase  :							int				     =					vocab_size
														_lowerCAmelCase  :							Optional[int]				     =					d_model
														_lowerCAmelCase  :							Tuple				     =					n_layer
														_lowerCAmelCase  :							List[Any]				     =					n_head
														if d_model % n_head != 0:
																					raise ValueError(f"'d_model % n_head' ({d_model % n_head}) should be equal to 0")
														if "d_head" in kwargs:
																					if kwargs["d_head"] != d_model // n_head:
																												raise ValueError(
																												    f"`d_head` ({kwargs['d_head']}) should be equal to `d_model // n_head` ({d_model // n_head})")
														_lowerCAmelCase  :							Optional[int]				     =					d_model // n_head
														_lowerCAmelCase  :							List[str]				     =					ff_activation
														_lowerCAmelCase  :							Tuple				     =					d_inner
														_lowerCAmelCase  :							List[Any]				     =					untie_r
														_lowerCAmelCase  :							List[str]				     =					attn_type

														_lowerCAmelCase  :							Union[str, Any]				     =					initializer_range
														_lowerCAmelCase  :							Any				     =					layer_norm_eps

														_lowerCAmelCase  :							List[Any]				     =					dropout
														_lowerCAmelCase  :							Optional[int]				     =					mem_len
														_lowerCAmelCase  :							Union[str, Any]				     =					reuse_len
														_lowerCAmelCase  :							List[str]				     =					bi_data
														_lowerCAmelCase  :							List[str]				     =					clamp_len
														_lowerCAmelCase  :							Any				     =					same_length

														_lowerCAmelCase  :							List[str]				     =					summary_type
														_lowerCAmelCase  :							int				     =					summary_use_proj
														_lowerCAmelCase  :							Optional[Any]				     =					summary_activation
														_lowerCAmelCase  :							Tuple				     =					summary_last_dropout
														_lowerCAmelCase  :							Union[str, Any]				     =					start_n_top
														_lowerCAmelCase  :							Optional[int]				     =					end_n_top

														_lowerCAmelCase  :							Tuple				     =					bos_token_id
														_lowerCAmelCase  :							List[Any]				     =					pad_token_id
														_lowerCAmelCase  :							Dict				     =					eos_token_id

														if "use_cache" in kwargs:
																					warnings.warn(
																					    "The `use_cache` argument is deprecated and will be removed in a future version, use `use_mems_eval`"
																					    " instead.",    __a,    )
																					_lowerCAmelCase  :							Union[str, Any]				     =					kwargs["use_cache"]

														_lowerCAmelCase  :							Union[str, Any]				     =					use_mems_eval
														_lowerCAmelCase  :							Any				     =					use_mems_train
														super().__init__(pad_token_id=__a,    bos_token_id=__a,    eos_token_id=__a,    **__a)







							@property
							def      snake_case__   (     self):

														'''simple docstring'''

														logger.info(f"The model {self.model_type} is one of the few models that has no sequence length limit.")
														return -1







							@max_position_embeddings.setter
							def      snake_case__   (     self,    __a):

														'''simple docstring'''

														raise NotImplementedError(
														    f"The model {self.model_type} is one of the few models that has no sequence length limit.")
