






import argparse
import json

import numpy
import torch

from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()




def      lowercase__      (     __snake_case    :							int     ,       __snake_case    :							str  ):


					'''simple docstring'''


					UpperCAmelCase_    :      int            =						torch.load(__snake_case     ,       map_location='cpu'  )

					UpperCAmelCase_    :      List[str]            =						chkpt['model']

					# We have the base model one level deeper than the original XLM repository
					UpperCAmelCase_    :      int            =						{}
					for k, v in state_dict.items():
										if "pred_layer" in k:
															UpperCAmelCase_    :      str            =						v
										else:
															UpperCAmelCase_    :      List[Any]            =						v

					UpperCAmelCase_    :      Optional[Any]            =						chkpt['params']
					UpperCAmelCase_    :      int            =						{n: v for n, v in config.items() if not isinstance(__snake_case     ,       (torch.FloatTensor, numpy.ndarray)  )}

					UpperCAmelCase_    :      Optional[Any]            =						chkpt['dico_word2id']
					UpperCAmelCase_    :      str            =						{s + '</w>' if s.find('@@'  ) == -1 and i > 13 else s.replace('@@'     ,       ''  ): i for s, i in vocab.items()}

					# Save pytorch-model
					UpperCAmelCase_    :      Union[str, Any]            =						pytorch_dump_folder_path + '/' + WEIGHTS_NAME
					UpperCAmelCase_    :      str            =						pytorch_dump_folder_path + '/' + CONFIG_NAME
					UpperCAmelCase_    :      str            =						pytorch_dump_folder_path + '/' + VOCAB_FILES_NAMES['vocab_file']

					print(F"Save PyTorch model to {pytorch_weights_dump_path}"  )
					torch.save(__snake_case     ,       __snake_case  )

					print(F"Save configuration file to {pytorch_config_dump_path}"  )
					with open(__snake_case     ,       'w'     ,       encoding='utf-8'  ) as f:
										f.write(json.dumps(__snake_case     ,       indent=2  ) + '\n'  )

					print(F"Save vocab file to {pytorch_config_dump_path}"  )
					with open(__snake_case     ,       'w'     ,       encoding='utf-8'  ) as f:
										f.write(json.dumps(__snake_case     ,       indent=2  ) + '\n'  )


if __name__ == "__main__":
		__UpperCAmelCase								=						argparse.ArgumentParser()
		# Required parameters
		parser.add_argument(
		    '--xlm_checkpoint_path', default=None, type=str, required=True, help='Path the official PyTorch dump.'
		)
		parser.add_argument(
		    '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
		)
		__UpperCAmelCase								=						parser.parse_args()
		convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)














import os

# Precomputes a list of the 100 first triangular numbers
__UpperCAmelCase								=						[int(0.5 * n * (n + 1)) for n in range(1, 101)]




def      lowercase__      (     ):


					'''simple docstring'''


					UpperCAmelCase_    :      Any            =						os.path.dirname(os.path.realpath(__snake_case  )  )
					UpperCAmelCase_    :      Optional[Any]            =						os.path.join(__snake_case     ,       'words.txt'  )

					UpperCAmelCase_    :      Union[str, Any]            =						''
					with open(__snake_case  ) as f:
										UpperCAmelCase_    :      List[Any]            =						f.readline()

					UpperCAmelCase_    :      Optional[int]            =						[word.strip('"'  ) for word in words.strip('\r\n'  ).split(','  )]
					UpperCAmelCase_    :      Optional[int]            =						[
					    word
					    for word in [sum(ord(__snake_case  ) - 64 for x in word  ) for word in words]
					    if word in TRIANGULAR_NUMBERS
					]
					return len(__snake_case  )


if __name__ == "__main__":
		print(solution())






