




'''simple docstring'''






import argparse
import os
import pickle
import sys

import torch

from transformers import TransfoXLConfig, TransfoXLLMHeadModel, load_tf_weights_in_transfo_xl
from transformers.models.transfo_xl import tokenization_transfo_xl as data_utils
from transformers.models.transfo_xl.tokenization_transfo_xl import CORPUS_NAME, VOCAB_FILES_NAMES
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()

# We do this to be able to load python 2 datasets pickles
# See e.g. https://stackoverflow.com/questions/2121874/python-pickling-after-changing-a-modules-directory/2121918#2121918
UpperCAmelCase_       :   Tuple   =       data_utils.TransfoXLTokenizer
UpperCAmelCase_       :   Any   =       data_utils.TransfoXLCorpus
UpperCAmelCase_       :   Union[str, Any]   =       data_utils
UpperCAmelCase_       :   int   =       data_utils







def 		A_       (	_lowerCAmelCase   :    Tuple				,	_lowerCAmelCase   :    Union[str, Any]				,	_lowerCAmelCase   :    str				,	_lowerCAmelCase   :    List[str]							):



      """simple docstring"""




      if transfo_xl_dataset_file:
            # Convert a pre-processed corpus (see original TensorFlow repo)
            with open(_lowerCAmelCase				,	"rb"							) as fp:
                  _lowerCamelCase				:   List[Any]								=   pickle.load(_lowerCAmelCase				,	encoding="latin1"							)
            # Save vocabulary and dataset cache as Dictionaries (should be better than pickles for the long-term)
            _lowerCamelCase				:   Union[str, Any]								=   pytorch_dump_folder_path + "/" + VOCAB_FILES_NAMES["pretrained_vocab_file"]
            print(F'Save vocabulary to {pytorch_vocab_dump_path}'							)
            _lowerCamelCase				:   Any								=   corpus.vocab.__dict__
            torch.save(_lowerCAmelCase				,	_lowerCAmelCase							)

            _lowerCamelCase				:   Optional[Any]								=   corpus.__dict__
            corpus_dict_no_vocab.pop("vocab"				,	_lowerCAmelCase							)
            _lowerCamelCase				:   Tuple								=   pytorch_dump_folder_path + "/" + CORPUS_NAME
            print(F'Save dataset to {pytorch_dataset_dump_path}'							)
            torch.save(_lowerCAmelCase				,	_lowerCAmelCase							)

      if tf_checkpoint_path:
            # Convert a pre-trained TensorFlow model
            _lowerCamelCase				:   Union[str, Any]								=   os.path.abspath(_lowerCAmelCase							)
            _lowerCamelCase				:   Any								=   os.path.abspath(_lowerCAmelCase							)

            print(F'Converting Transformer XL checkpoint from {tf_path} with config at {config_path}.'							)
            # Initialise PyTorch model
            if transfo_xl_config_file == "":
                  _lowerCamelCase				:   List[Any]								=   TransfoXLConfig()
            else:
                  _lowerCamelCase				:   List[str]								=   TransfoXLConfig.from_json_file(_lowerCAmelCase							)
            print(F'Building PyTorch model from configuration: {config}'							)
            _lowerCamelCase				:   List[str]								=   TransfoXLLMHeadModel(_lowerCAmelCase							)

            _lowerCamelCase				:   List[str]								=   load_tf_weights_in_transfo_xl(_lowerCAmelCase				,	_lowerCAmelCase				,	_lowerCAmelCase							)
            # Save pytorch-model
            _lowerCamelCase				:   List[str]								=   os.path.join(_lowerCAmelCase				,	_lowerCAmelCase							)
            _lowerCamelCase				:   Union[str, Any]								=   os.path.join(_lowerCAmelCase				,	_lowerCAmelCase							)
            print(F'Save PyTorch model to {os.path.abspath(_lowerCAmelCase							)}'							)
            torch.save(model.state_dict()				,	_lowerCAmelCase							)
            print(F'Save configuration file to {os.path.abspath(_lowerCAmelCase							)}'							)
            with open(_lowerCAmelCase				,	"w"				,	encoding="utf-8"							) as f:
                  f.write(config.to_json_string()							)


if __name__ == "__main__":
 UpperCAmelCase_       :   Union[str, Any]   =       argparse.ArgumentParser()
 parser.add_argument(
     '--pytorch_dump_folder_path',
     default=None,
     type=str,
     required=True,
     help='Path to the folder to store the PyTorch model or dataset/vocab.',
 )
 parser.add_argument(
     '--tf_checkpoint_path',
     default='',
     type=str,
     help='An optional path to a TensorFlow checkpoint path to be converted.',
 )
 parser.add_argument(
     '--transfo_xl_config_file',
     default='',
     type=str,
     help=(
         'An optional config json file corresponding to the pre-trained BERT model. \n'
         'This specifies the model architecture.'
     ),
 )
 parser.add_argument(
     '--transfo_xl_dataset_file',
     default='',
     type=str,
     help='An optional dataset file to be converted in a vocabulary.',
 )
 UpperCAmelCase_       :   int   =       parser.parse_args()
 convert_transfo_xl_checkpoint_to_pytorch(
     args.tf_checkpoint_path,
     args.transfo_xl_config_file,
     args.pytorch_dump_folder_path,
     args.transfo_xl_dataset_file,
 )

'''simple docstring'''






import unittest

import numpy as np

from transformers import RobertaConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
 from transformers.models.roberta.modeling_flax_roberta import (
     FlaxRobertaForCausalLM,
     FlaxRobertaForMaskedLM,
     FlaxRobertaForMultipleChoice,
     FlaxRobertaForQuestionAnswering,
     FlaxRobertaForSequenceClassification,
     FlaxRobertaForTokenClassification,
     FlaxRobertaModel,
 )

class  UpperCAmelCase__						(       unittest.TestCase ):



    def __init__(	self      :       Union[str, Any],__A      :       Dict,__A      :       List[str]=1_3,__A      :       Any=7,__A      :       str=True,__A      :       Optional[int]=True,__A      :       Optional[Any]=True,__A      :       Any=True,__A      :       List[str]=9_9,__A      :       str=3_2,__A      :       List[str]=5,__A      :       Optional[Any]=4,__A      :       Any=3_7,__A      :       Optional[Any]="gelu",__A      :       List[Any]=0.1,__A      :       Any=0.1,__A      :       Dict=5_1_2,__A      :       Tuple=1_6,__A      :       Tuple=2,__A      :       List[Any]=0.02,__A      :       Any=4,):
          _lowerCamelCase				:   List[Any]								=   parent
          _lowerCamelCase				:   Optional[int]								=   batch_size
          _lowerCamelCase				:   Tuple								=   seq_length
          _lowerCamelCase				:   Tuple								=   is_training
          _lowerCamelCase				:   Union[str, Any]								=   use_attention_mask
          _lowerCamelCase				:   Optional[Any]								=   use_token_type_ids
          _lowerCamelCase				:   List[Any]								=   use_labels
          _lowerCamelCase				:   str								=   vocab_size
          _lowerCamelCase				:   List[str]								=   hidden_size
          _lowerCamelCase				:   Union[str, Any]								=   num_hidden_layers
          _lowerCamelCase				:   Tuple								=   num_attention_heads
          _lowerCamelCase				:   Union[str, Any]								=   intermediate_size
          _lowerCamelCase				:   Optional[Any]								=   hidden_act
          _lowerCamelCase				:   Tuple								=   hidden_dropout_prob
          _lowerCamelCase				:   Optional[Any]								=   attention_probs_dropout_prob
          _lowerCamelCase				:   List[Any]								=   max_position_embeddings
          _lowerCamelCase				:   Union[str, Any]								=   type_vocab_size
          _lowerCamelCase				:   Union[str, Any]								=   type_sequence_label_size
          _lowerCamelCase				:   str								=   initializer_range
          _lowerCamelCase				:   List[Any]								=   num_choices



    def   lowerCamelCase_  (	self      :       int     ):
          _lowerCamelCase				:   Optional[int]								=   ids_tensor([self.batch_size, self.seq_length],self.vocab_size     )

          _lowerCamelCase				:   Dict								=   None
          if self.use_attention_mask:
                _lowerCamelCase				:   Optional[int]								=   random_attention_mask([self.batch_size, self.seq_length]     )

          _lowerCamelCase				:   List[str]								=   None
          if self.use_token_type_ids:
                _lowerCamelCase				:   Tuple								=   ids_tensor([self.batch_size, self.seq_length],self.type_vocab_size     )

          _lowerCamelCase				:   Optional[int]								=   RobertaConfig(
              vocab_size=self.vocab_size,hidden_size=self.hidden_size,num_hidden_layers=self.num_hidden_layers,num_attention_heads=self.num_attention_heads,intermediate_size=self.intermediate_size,hidden_act=self.hidden_act,hidden_dropout_prob=self.hidden_dropout_prob,attention_probs_dropout_prob=self.attention_probs_dropout_prob,max_position_embeddings=self.max_position_embeddings,type_vocab_size=self.type_vocab_size,is_decoder=__A,initializer_range=self.initializer_range,)

          return config, input_ids, token_type_ids, attention_mask



    def   lowerCamelCase_  (	self      :       Any     ):
          _lowerCamelCase				:   List[str]								=   self.prepare_config_and_inputs()
          _lowerCamelCase						,		_lowerCamelCase						,		_lowerCamelCase						,		_lowerCamelCase				:   str								=   config_and_inputs
          _lowerCamelCase				:   List[Any]								=   {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask}
          return config, inputs_dict



    def   lowerCamelCase_  (	self      :       List[str]     ):
          _lowerCamelCase				:   Any								=   self.prepare_config_and_inputs()
          _lowerCamelCase						,		_lowerCamelCase						,		_lowerCamelCase						,		_lowerCamelCase				:   Dict								=   config_and_inputs

          _lowerCamelCase				:   int								=   True
          _lowerCamelCase				:   List[Any]								=   floats_tensor([self.batch_size, self.seq_length, self.hidden_size]     )
          _lowerCamelCase				:   Optional[int]								=   ids_tensor([self.batch_size, self.seq_length],vocab_size=2     )

          return (
              config,
              input_ids,
              token_type_ids,
              encoder_hidden_states,
              encoder_attention_mask,
          )


@require_flax
class  UpperCAmelCase__						(       A						,	unittest.TestCase ):
    lowerCAmelCase_				    =		True

    lowerCAmelCase_				    =		(
        (
            FlaxRobertaModel,
            FlaxRobertaForCausalLM,
            FlaxRobertaForMaskedLM,
            FlaxRobertaForSequenceClassification,
            FlaxRobertaForTokenClassification,
            FlaxRobertaForMultipleChoice,
            FlaxRobertaForQuestionAnswering,
        )
        if is_flax_available()
        else ()
    )



    def   lowerCamelCase_  (	self      :       Optional[int]     ):
          _lowerCamelCase				:   List[str]								=   FlaxRobertaModelTester(self     )



    @slow
    def   lowerCamelCase_  (	self      :       Any     ):
          for model_class_name in self.all_model_classes:
                _lowerCamelCase				:   Union[str, Any]								=   model_class_name.from_pretrained("roberta-base",from_pt=__A     )
                _lowerCamelCase				:   Union[str, Any]								=   model(np.ones((1, 1)     )     )
                self.assertIsNotNone(__A     )