






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     a     (		lowerCamelCase_						,  lowerCamelCase_							):






				'''simple docstring'''
				# Load checkpoint
				lowercase__  					=     torch.load(lowerCamelCase_						,  map_location='''cpu'''							)

				lowercase__  					=     chkpt['''model''']

				# We have the base model one level deeper than the original XLM repository
				lowercase__  					=     {}
				for k, v in state_dict.items():
								if "pred_layer" in k:
												lowercase__  					=     v
								else:
												lowercase__  					=     v

				lowercase__  					=     chkpt['''params''']
				lowercase__  					=     {n: v for n, v in config.items() if not isinstance(lowerCamelCase_						,  (torch.FloatTensor, numpy.ndarray)							)}

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

				# Save pytorch-model
				lowercase__  					=     pytorch_dump_folder_path + '''/''' + WEIGHTS_NAME
				lowercase__  					=     pytorch_dump_folder_path + '''/''' + CONFIG_NAME
				lowercase__  					=     pytorch_dump_folder_path + '''/''' + VOCAB_FILES_NAMES['''vocab_file''']

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

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

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


if __name__ == "__main__":
							A__      :     Dict       =					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.'
							)
							A__      :     List[Any]       =					parser.parse_args()
							convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)












import importlib.metadata
import operator
import re
import sys
from typing import Optional

from packaging import version


A__      :     Tuple       =					{
    '<': operator.lt,
    '<=': operator.le,
    '==': operator.eq,
    '!=': operator.ne,
    '>=': operator.ge,
    '>': operator.gt,
}






def     a     (		lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_							):






				'''simple docstring'''
				if got_ver is None or want_ver is None:
								raise ValueError(
								    F"""Unable to compare versions for {requirement}: need={want_ver} found={got_ver}. This is unusual. Consider"""
								    F""" reinstalling {pkg}."""							)
				if not ops[op](version.parse(lowerCamelCase_							)						,  version.parse(lowerCamelCase_							)							):
								raise ImportError(
								    F"""{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}"""							)






def     a     (		lowerCamelCase_						,  lowerCamelCase_ = None							):






				'''simple docstring'''
				lowercase__  					=     F"""\n{hint}""" if hint is not None else ''''''

				# non-versioned check
				if re.match(r'''^[\w_\-\d]+$'''						,  lowerCamelCase_							):
								lowercase__					,		lowercase__					,		lowercase__  					=     requirement, None, None
				else:
								lowercase__  					=     re.findall(r'''^([^!=<>\s]+)([\s!=<>]{1,2}.+)'''						,  lowerCamelCase_							)
								if not match:
												raise ValueError(
												    '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but'''
												    F""" got {requirement}"""							)
								lowercase__					,		lowercase__  					=     match[0]
								lowercase__  					=     want_full.split(''','''							)  # there could be multiple requirements
								lowercase__  					=     {}
								for w in want_range:
												lowercase__  					=     re.findall(r'''^([\s!=<>]{1,2})(.+)'''						,  lowerCamelCase_							)
												if not match:
																raise ValueError(
																    '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,'''
																    F""" but got {requirement}"""							)
												lowercase__					,		lowercase__  					=     match[0]
												lowercase__  					=     want_ver
												if op not in ops:
																raise ValueError(F"""{requirement}: need one of {list(ops.keys()							)}, but got {op}"""							)

    # special case
				if pkg == "python":
								lowercase__  					=     '''.'''.join([str(lowerCamelCase_							) for x in sys.version_info[:3]]							)
								for op, want_ver in wanted.items():
												_compare_versions(lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_							)
								return

				# check if any version is installed
				try:
								lowercase__  					=     importlib.metadata.version(lowerCamelCase_							)
				except importlib.metadata.PackageNotFoundError:
								raise importlib.metadata.PackageNotFoundError(
								    F"""The '{requirement}' distribution was not found and is required by this application. {hint}"""							)

				# check that the right version is installed if version number or a range was provided
				if want_ver is not None:
								for op, want_ver in wanted.items():
												_compare_versions(lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_							)






def     a     (		lowerCamelCase_							):






				'''simple docstring'''
				lowercase__  					=     '''Try: pip install transformers -U or pip install -e \'.[dev]\' if you\'re working with git main'''
				return require_version(lowerCamelCase_						,  lowerCamelCase_							)




