





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







  """simple docstring"""





  lowerCamelCase__:   Optional[Any]    						=torch.load(__A			,					map_location="cpu"	)

  lowerCamelCase__:   int    						=chkpt["model"]

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

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

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

  # Save pytorch-model
  lowerCamelCase__:   List[str]    						=pytorch_dump_folder_path + "/" + WEIGHTS_NAME
  lowerCamelCase__:   Optional[int]    						=pytorch_dump_folder_path + "/" + CONFIG_NAME
  lowerCamelCase__:   Optional[Any]    						=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__":
     __A             =       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             =       parser.parse_args()
     convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)








# Lint as: python3
import dataclasses
import re
from dataclasses import dataclass
from functools import total_ordering
from typing import Optional, Union


__A             =       re.compile(R"^(?P<major>\d+)" R"\.(?P<minor>\d+)" R"\.(?P<patch>\d+)$")



@total_ordering
@dataclass
class      _SCREAMING_SNAKE_CASE		:







 '''simple docstring'''
 lowercase_	=    42
 lowercase_	=    None
 lowercase_	=    None
 lowercase_	=    None
 lowercase_	=    None

 def 							SCREAMING_SNAKE_CASE_      (self       :     List[Any]) ->Tuple:



   '''simple docstring'''



   lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__:   List[str]    						=_str_to_version_tuple(self.version_str)

 def __repr__(self       :     Union[str, Any]) ->Union[str, Any]:



   '''simple docstring'''



   return F"""{self.tuple[0]}.{self.tuple[1]}.{self.tuple[2]}"""

 @property
 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[Any]) ->Optional[Any]:



   '''simple docstring'''



   return self.major, self.minor, self.patch

 def 							SCREAMING_SNAKE_CASE_      (self       :     Dict ,				UpperCAmelCase_       :     Tuple) ->Any:



   '''simple docstring'''



   if isinstance(UpperCAmelCase_ ,				UpperCAmelCase_):
     return Version(UpperCAmelCase_)
   elif isinstance(UpperCAmelCase_ ,				UpperCAmelCase_):
     return other
   raise TypeError(F"""{other} (type {type(UpperCAmelCase_)}) cannot be compared to version.""")

 def __eq__(self       :     Optional[Any] ,				UpperCAmelCase_       :     Tuple) ->List[str]:



   '''simple docstring'''



   try:
     lowerCamelCase__:   List[Any]    						=self._validate_operand(UpperCAmelCase_)
   except (TypeError, ValueError):
     return False
   else:
     return self.tuple == other.tuple

 def __lt__(self       :     Dict ,				UpperCAmelCase_       :     List[str]) ->Union[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:   Tuple    						=self._validate_operand(UpperCAmelCase_)
   return self.tuple < other.tuple

 def __hash__(self       :     Any) ->Dict:



   '''simple docstring'''



   return hash(_version_tuple_to_str(self.tuple))

 @classmethod
 def 							SCREAMING_SNAKE_CASE_      (cls       :     Union[str, Any] ,				UpperCAmelCase_       :     Optional[Any]) ->Union[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:   str    						={f.name for f in dataclasses.fields(cls)}
   return cls(**{k: v for k, v in dic.items() if k in field_names})







 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[int]) ->str:



   '''simple docstring'''



   return self.version_str





def   lowerCAmelCase_				( __a	)  ->	List[Any]:







  """simple docstring"""





  lowerCamelCase__:   Optional[int]    						=_VERSION_REG.match(__a	)
  if not res:
    raise ValueError(F"""Invalid version '{version_str}'. Format should be x.y.z with {{x,y,z}} being digits."""	)
  return tuple(int(__a	) for v in [res.group("major"	), res.group("minor"	), res.group("patch"	)]	)






def   lowerCAmelCase_				( __a	)  ->	Dict:







  """simple docstring"""





  return ".".join(str(__a	) for v in version_tuple	)

