
"""simple docstring"""





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_     (__a				,   __a				):

					'''simple docstring'''





					A_				 =						torch.load(__a				,   map_location="cpu"				)

					A_				 =						chkpt["model"]

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

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

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

					# Save pytorch-model
					A_				 =						pytorch_dump_folder_path + "/" + WEIGHTS_NAME
					A_				 =						pytorch_dump_folder_path + "/" + CONFIG_NAME
					A_				 =						pytorch_dump_folder_path + "/" + VOCAB_FILES_NAMES["vocab_file"]

					print(f'Save PyTorch model to {pytorch_weights_dump_path}'				)
					torch.save(__a				,   __a				)

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

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


if __name__ == "__main__":
		UpperCamelCase_					:						Optional[Any]				= 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_					:						Any				= parser.parse_args()
		convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)






"""simple docstring"""





import inspect
import re

from transformers.utils import direct_transformers_import


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_config_docstrings.py
UpperCamelCase_					:						Optional[Any]				= '''src/transformers'''


# This is to make sure the transformers module imported is the one in the repo.
UpperCamelCase_					:						Union[str, Any]				= direct_transformers_import(PATH_TO_TRANSFORMERS)

UpperCamelCase_					:						List[Any]				= transformers.models.auto.configuration_auto.CONFIG_MAPPING

# Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`.
# For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)`
UpperCamelCase_					:						str				= re.compile(R'''\[(.+?)\]\((https://huggingface\.co/.+?)\)''')


UpperCamelCase_					:						Dict				= {
    '''DecisionTransformerConfig''',
    '''EncoderDecoderConfig''',
    '''MusicgenConfig''',
    '''RagConfig''',
    '''SpeechEncoderDecoderConfig''',
    '''TimmBackboneConfig''',
    '''VisionEncoderDecoderConfig''',
    '''VisionTextDualEncoderConfig''',
    '''LlamaConfig''',
}
def 							A_     (__a				):

					'''simple docstring'''





					A_				 =						None

					# source code of `config_class`
					A_				 =						inspect.getsource(__a				)
					A_				 =						_re_checkpoint.findall(__a				)

					# Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link.
					# For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')`
					for ckpt_name, ckpt_link in checkpoints:
										# allow the link to end with `/`
										if ckpt_link.endswith("/"				):
															A_				 =						ckpt_link[:-1]

										# verify the checkpoint name corresponds to the checkpoint link
										A_				 =						f'https://huggingface.co/{ckpt_name}'
										if ckpt_link == ckpt_link_from_name:
															A_				 =						ckpt_name
															break

					return checkpoint
def 							A_     ():

					'''simple docstring'''





					A_				 =						[]

					for config_class in list(CONFIG_MAPPING.values()				):
										# Skip deprecated models
										if "models.deprecated" in config_class.__module__:
															continue
										A_				 =						get_checkpoint_from_config_class(__a				)

										A_				 =						config_class.__name__
										if checkpoint is None and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK:
															configs_without_checkpoint.append(__a				)

					if len(__a				) > 0:
										A_				 =						"\n".join(sorted(__a				)				)
										raise ValueError(f'The following configurations don\'t contain any valid checkpoint:\n{message}'				)


if __name__ == "__main__":
		check_config_docstrings_have_checkpoints()




