


import unittest

from transformers import AutoConfig, AutoTokenizer, BertConfig, TensorType, is_flax_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, slow


if is_flax_available():
       import jax

       from transformers.models.auto.modeling_flax_auto import FlaxAutoModel
       from transformers.models.bert.modeling_flax_bert import FlaxBertModel
       from transformers.models.roberta.modeling_flax_roberta import FlaxRobertaModel





@require_flax
class  lowerCAmelCase__(    unittest.TestCase						):






    '''simple docstring'''





    @slow
    def 					_lowerCamelCase (	self				:   List[Any]	):





       '''simple docstring'''
       for model_name in ["bert-base-cased", "bert-large-uncased"]:
          with self.subTest(__snake_case	):
             UpperCAmelCase_							:      Union[str, Any]						=   AutoConfig.from_pretrained(__snake_case	)
             self.assertIsNotNone(__snake_case	)
             self.assertIsInstance(__snake_case       ,   __snake_case	)

             UpperCAmelCase_							:      str						=   FlaxAutoModel.from_pretrained(__snake_case	)
             self.assertIsNotNone(__snake_case	)
             self.assertIsInstance(__snake_case       ,   __snake_case	)





    @slow
    def 					_lowerCamelCase (	self				:   Any	):





       '''simple docstring'''
       for model_name in ["roberta-base", "roberta-large"]:
          with self.subTest(__snake_case	):
             UpperCAmelCase_							:      Dict						=   AutoConfig.from_pretrained(__snake_case	)
             self.assertIsNotNone(__snake_case	)
             self.assertIsInstance(__snake_case       ,   __snake_case	)

             UpperCAmelCase_							:      Tuple						=   FlaxAutoModel.from_pretrained(__snake_case	)
             self.assertIsNotNone(__snake_case	)
             self.assertIsInstance(__snake_case       ,   __snake_case	)





    @slow
    def 					_lowerCamelCase (	self				:   int	):





       '''simple docstring'''
       for model_name in ["bert-base-cased", "bert-large-uncased"]:
          UpperCAmelCase_							:      int						=   AutoTokenizer.from_pretrained(__snake_case	)
          UpperCAmelCase_							:      int						=   FlaxBertModel.from_pretrained(__snake_case	)
          UpperCAmelCase_							:      Tuple						=   tokenizer('''Do you support jax jitted function?'''       ,   return_tensors=TensorType.JAX	)

          @jax.jit
          def eval(**__snake_case				:   Optional[Any]	):
             return model(**__snake_case	)

          eval(**__snake_case	).block_until_ready()





    @slow
    def 					_lowerCamelCase (	self				:   Tuple	):





       '''simple docstring'''
       for model_name in ["roberta-base", "roberta-large"]:
          UpperCAmelCase_							:      Any						=   AutoTokenizer.from_pretrained(__snake_case	)
          UpperCAmelCase_							:      Union[str, Any]						=   FlaxRobertaModel.from_pretrained(__snake_case	)
          UpperCAmelCase_							:      Union[str, Any]						=   tokenizer('''Do you support jax jitted function?'''       ,   return_tensors=TensorType.JAX	)

          @jax.jit
          def eval(**__snake_case				:   List[str]	):
             return model(**__snake_case	)

          eval(**__snake_case	).block_until_ready()





    def 					_lowerCamelCase (	self				:   int	):





       '''simple docstring'''
       with self.assertRaisesRegex(
           __snake_case       ,   '''bert-base is not a local folder and is not a valid model identifier'''	):
          UpperCAmelCase_							:      Any						=   FlaxAutoModel.from_pretrained('''bert-base'''	)





    def 					_lowerCamelCase (	self				:   Optional[int]	):





       '''simple docstring'''
       with self.assertRaisesRegex(
           __snake_case       ,   R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''	):
          UpperCAmelCase_							:      Tuple						=   FlaxAutoModel.from_pretrained(__snake_case       ,   revision='''aaaaaa'''	)





    def 					_lowerCamelCase (	self				:   List[Any]	):





       '''simple docstring'''
       with self.assertRaisesRegex(
           __snake_case       ,   '''hf-internal-testing/config-no-model does not appear to have a file named flax_model.msgpack'''       ,   ):
          UpperCAmelCase_							:      Optional[Any]						=   FlaxAutoModel.from_pretrained('''hf-internal-testing/config-no-model'''	)





    def 					_lowerCamelCase (	self				:   Tuple	):





       '''simple docstring'''
       with self.assertRaisesRegex(__snake_case       ,   '''Use `from_pt=True` to load this model'''	):
          UpperCAmelCase_							:      Optional[int]						=   FlaxAutoModel.from_pretrained('''hf-internal-testing/tiny-bert-pt-only'''	)



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)