


from importlib import import_module

from .logging import get_logger


__UpperCamelCase				:  Dict      					=							get_logger(__name__)





class  lowerCAmelCase__:






    '''simple docstring'''





    def __init__(	self				:   Optional[int]       ,   __snake_case				:   List[str]       ,   __snake_case				:   str=None	):





       '''simple docstring'''
       UpperCAmelCase_							:      Any						=   attrs or []
       if module is not None:
          for key in module.__dict__:
             if key in attrs or not key.startswith('''__'''	):
                setattr(self       ,   _lowercase       ,   getattr(_lowercase       ,   _lowercase	)	)
       UpperCAmelCase_							:      Optional[int]						=   module._original_module if isinstance(_lowercase       ,   _PatchedModuleObj	) else module





class  lowerCAmelCase__:






    '''simple docstring'''





    A_  :     Any			= []





    def __init__(	self				:   Optional[Any]       ,   __snake_case				:   List[Any]       ,   __snake_case				:   str       ,   __snake_case				:   List[str]       ,   __snake_case				:   Union[str, Any]=None	):





       '''simple docstring'''
       UpperCAmelCase_							:      List[Any]						=   obj
       UpperCAmelCase_							:      Tuple						=   target
       UpperCAmelCase_							:      List[str]						=   new
       UpperCAmelCase_							:      Dict						=   target.split('''.'''	)[0]
       UpperCAmelCase_							:      Optional[Any]						=   {}
       UpperCAmelCase_							:      int						=   attrs or []





    def __enter__(	self				:   List[str]	):





       '''simple docstring'''
       *UpperCAmelCase_		,      UpperCAmelCase_							:      Union[str, Any]						=   self.target.split('''.'''	)

       # Patch modules:
       # it's used to patch attributes of submodules like "os.path.join";
       # in this case we need to patch "os" and "os.path"

       for i in range(len(_lowercase	)	):
          try:
             UpperCAmelCase_							:      List[Any]						=   import_module('''.'''.join(submodules[: i + 1]	)	)
          except ModuleNotFoundError:
             continue
          # We iterate over all the globals in self.obj in case we find "os" or "os.path"
          for attr in self.obj.__dir__():
             UpperCAmelCase_							:      Optional[Any]						=   getattr(self.obj       ,   _lowercase	)
             # We don't check for the name of the global, but rather if its value *is* "os" or "os.path".
             # This allows to patch renamed modules like "from os import path as ospath".
             if obj_attr is submodule or (
                 (isinstance(_lowercase       ,   _PatchedModuleObj	) and obj_attr._original_module is submodule)
             ):
                UpperCAmelCase_							:      Union[str, Any]						=   obj_attr
                # patch at top level
                setattr(self.obj       ,   _lowercase       ,   _PatchedModuleObj(_lowercase       ,   attrs=self.attrs	)	)
                UpperCAmelCase_							:      Optional[Any]						=   getattr(self.obj       ,   _lowercase	)
                # construct lower levels patches
                for key in submodules[i + 1 :]:
                   setattr(_lowercase       ,   _lowercase       ,   _PatchedModuleObj(getattr(_lowercase       ,   _lowercase       ,   _lowercase	)       ,   attrs=self.attrs	)	)
                   UpperCAmelCase_							:      str						=   getattr(_lowercase       ,   _lowercase	)
                # finally set the target attribute
                setattr(_lowercase       ,   _lowercase       ,   self.new	)

        # Patch attribute itself:
        # it's used for builtins like "open",
        # and also to patch "os.path.join" we may also need to patch "join"
        # itself if it was imported as "from os.path import join".

       if submodules:  # if it's an attribute of a submodule like "os.path.join"
          try:
             UpperCAmelCase_							:      Dict						=   getattr(import_module('''.'''.join(_lowercase	)	)       ,   _lowercase	)
          except (AttributeError, ModuleNotFoundError):
             return
          # We iterate over all the globals in self.obj in case we find "os.path.join"
          for attr in self.obj.__dir__():
             # We don't check for the name of the global, but rather if its value *is* "os.path.join".
             # This allows to patch renamed attributes like "from os.path import join as pjoin".
             if getattr(self.obj       ,   _lowercase	) is attr_value:
                UpperCAmelCase_							:      List[Any]						=   getattr(self.obj       ,   _lowercase	)
                setattr(self.obj       ,   _lowercase       ,   self.new	)
       elif target_attr in globals()["__builtins__"]:  # if it'a s builtin like "open"
          UpperCAmelCase_							:      List[str]						=   globals()['''__builtins__'''][target_attr]
          setattr(self.obj       ,   _lowercase       ,   self.new	)
       else:
          raise RuntimeError(f'''Tried to patch attribute {target_attr} instead of a submodule.'''	)





    def __exit__(	self				:   List[Any]       ,   *__snake_case				:   Dict	):





       '''simple docstring'''
       for attr in list(self.original	):
          setattr(self.obj       ,   _lowercase       ,   self.original.pop(_lowercase	)	)





    def 					_lowerCamelCase (	self				:   List[str]	):





       '''simple docstring'''
       self.__enter__()
       self._active_patches.append(self	)





    def 					_lowerCamelCase (	self				:   List[Any]	):





       '''simple docstring'''
       try:
          self._active_patches.remove(self	)
       except ValueError:
          # If the patch hasn't been started this will fail
          return None

       return self.__exit__()



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      snake_case_						( __lowercase			,       __lowercase       ):
   # Load checkpoint
   UpperCAmelCase_							:      Tuple						=   torch.load(__lowercase			,       map_location='''cpu'''       )

   UpperCAmelCase_							:      Optional[int]						=   chkpt['''model''']

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

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

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

   # Save pytorch-model
   UpperCAmelCase_							:      Tuple						=   pytorch_dump_folder_path + '''/''' + WEIGHTS_NAME
   UpperCAmelCase_							:      Tuple						=   pytorch_dump_folder_path + '''/''' + CONFIG_NAME
   UpperCAmelCase_							:      Dict						=   pytorch_dump_folder_path + '''/''' + VOCAB_FILES_NAMES['''vocab_file''']

   print(F'''Save PyTorch model to {pytorch_weights_dump_path}'''       )
   torch.save(__lowercase			,       __lowercase       )

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

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


if __name__ == "__main__":
       __UpperCamelCase				:  str      					=							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				:  Dict      					=							parser.parse_args()
       convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)