


"""simple docstring"""



from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


__snake_case             =   logging.get_logger(__name__)

__snake_case             =   {
    """Helsinki-NLP/opus-mt-en-de""": """https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/config.json""",
    # See all Marian models at https://huggingface.co/models?filter=marian
}







class _lowerCAmelCase    (				snake_case_     ):
   __UpperCAmelCase			:	List[Any]							  =  '''marian'''
   __UpperCAmelCase			:	int							  =  ['''past_key_values''']
   __UpperCAmelCase			:	str							  =  {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}







   def __init__(     self						,     UpperCamelCase__=5_8101						,     UpperCamelCase__=None						,     UpperCamelCase__=1024						,     UpperCamelCase__=12						,     UpperCamelCase__=4096						,     UpperCamelCase__=16						,     UpperCamelCase__=12						,     UpperCamelCase__=4096						,     UpperCamelCase__=16						,     UpperCamelCase__=0.0						,     UpperCamelCase__=0.0						,     UpperCamelCase__=True						,     UpperCamelCase__=True						,     UpperCamelCase__="gelu"						,     UpperCamelCase__=1024						,     UpperCamelCase__=0.1						,     UpperCamelCase__=0.0						,     UpperCamelCase__=0.0						,     UpperCamelCase__=0.02						,     UpperCamelCase__=5_8100						,     UpperCamelCase__=False						,     UpperCamelCase__=5_8100						,     UpperCamelCase__=0						,     UpperCamelCase__=0						,     UpperCamelCase__=True						,     **UpperCamelCase__						,     )     ->		Tuple:

       '''simple docstring'''




       snake_case   :				Any				 =   vocab_size
       snake_case   :				List[str]				 =   decoder_vocab_size or vocab_size
       snake_case   :				str				 =   max_position_embeddings
       snake_case   :				str				 =   d_model
       snake_case   :				str				 =   encoder_ffn_dim
       snake_case   :				int				 =   encoder_layers
       snake_case   :				List[Any]				 =   encoder_attention_heads
       snake_case   :				str				 =   decoder_ffn_dim
       snake_case   :				List[str]				 =   decoder_layers
       snake_case   :				Any				 =   decoder_attention_heads
       snake_case   :				Any				 =   dropout
       snake_case   :				str				 =   attention_dropout
       snake_case   :				Optional[int]				 =   activation_dropout
       snake_case   :				int				 =   activation_function
       snake_case   :				Union[str, Any]				 =   init_std
       snake_case   :				str				 =   encoder_layerdrop
       snake_case   :				List[Any]				 =   decoder_layerdrop
       snake_case   :				Any				 =   use_cache
       snake_case   :				int				 =   encoder_layers
       snake_case   :				Union[str, Any]				 =   scale_embedding  # scale factor will be sqrt(d_model) if True
       snake_case   :				Optional[int]				 =   share_encoder_decoder_embeddings
       super().__init__(
           pad_token_id=UpperCamelCase__						,     eos_token_id=UpperCamelCase__						,     is_encoder_decoder=UpperCamelCase__						,     decoder_start_token_id=UpperCamelCase__						,     forced_eos_token_id=UpperCamelCase__						,     **UpperCamelCase__						,     )







class _lowerCAmelCase    (				snake_case_     ):







   @property
   # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
   def 			lowerCamelCase		(     self						)     ->		Mapping[str, Mapping[int, str]]:

       '''simple docstring'''




       if self.task in ["default", "seq2seq-lm"]:
           snake_case   :				Any				 =   OrderedDict(
               [
                   ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                   ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
               ]						)

           if self.use_past:
               snake_case   :				Dict				 =   {0: "batch"}
               snake_case   :				str				 =   {0: "batch", 1: "past_decoder_sequence + sequence"}
           else:
               snake_case   :				str				 =   {0: "batch", 1: "decoder_sequence"}
               snake_case   :				str				 =   {0: "batch", 1: "decoder_sequence"}

           if self.use_past:
               self.fill_with_past_key_values_(UpperCamelCase__						,     direction="inputs"						)
       elif self.task == "causal-lm":
           # TODO: figure this case out.
           snake_case   :				Tuple				 =   OrderedDict(
               [
                   ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                   ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
               ]						)
           if self.use_past:
               snake_case ,snake_case   :				List[str]				 =   self.num_layers
               for i in range(UpperCamelCase__						):
                   snake_case   :				Tuple				 =   {0: "batch", 2: "past_sequence + sequence"}
                   snake_case   :				Dict				 =   {0: "batch", 2: "past_sequence + sequence"}
       else:
           snake_case   :				Optional[int]				 =   OrderedDict(
               [
                   ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                   ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
                   ("decoder_input_ids", {0: "batch", 1: "decoder_sequence"}),
                   ("decoder_attention_mask", {0: "batch", 1: "decoder_sequence"}),
               ]						)

       return common_inputs







   @property
   # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
   def 			lowerCamelCase		(     self						)     ->		Mapping[str, Mapping[int, str]]:

       '''simple docstring'''




       if self.task in ["default", "seq2seq-lm"]:
           snake_case   :				str				 =   super().outputs
       else:
           snake_case   :				Tuple				 =   super(UpperCamelCase__						,     self						).outputs
           if self.use_past:
               snake_case ,snake_case   :				Dict				 =   self.num_layers
               for i in range(UpperCamelCase__						):
                   snake_case   :				int				 =   {0: "batch", 2: "past_sequence + sequence"}
                   snake_case   :				int				 =   {0: "batch", 2: "past_sequence + sequence"}
       return common_outputs







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__ = -1						,     UpperCamelCase__ = -1						,     UpperCamelCase__ = False						,     UpperCamelCase__ = None						,     )     ->		Mapping[str, Any]:

       '''simple docstring'''




       snake_case   :				List[str]				 =   self._generate_dummy_inputs_for_encoder_and_decoder(
           UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)

       # Generate decoder inputs
       snake_case   :				List[Any]				 =   seq_length if not self.use_past else 1
       snake_case   :				List[str]				 =   self._generate_dummy_inputs_for_encoder_and_decoder(
           UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)
       snake_case   :				Optional[Any]				 =   {F'decoder_{name}': tensor for name, tensor in decoder_inputs.items()}
       snake_case   :				Dict				 =   dict(**UpperCamelCase__						,     **UpperCamelCase__						)

       if self.use_past:
           if not is_torch_available():
               raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed."						)
           else:
               import torch
           snake_case ,snake_case   :				Optional[Any]				 =   common_inputs["input_ids"].shape
           snake_case   :				Optional[Any]				 =   common_inputs["decoder_input_ids"].shape[1]
           snake_case ,snake_case   :				List[Any]				 =   self.num_attention_heads
           snake_case   :				Optional[Any]				 =   (
               batch,
               num_encoder_attention_heads,
               encoder_seq_length,
               self._config.hidden_size // num_encoder_attention_heads,
           )
           snake_case   :				Any				 =   decoder_seq_length + 3
           snake_case   :				Tuple				 =   (
               batch,
               num_decoder_attention_heads,
               decoder_past_length,
               self._config.hidden_size // num_decoder_attention_heads,
           )

           snake_case   :				Tuple				 =   torch.cat(
               [common_inputs["decoder_attention_mask"], torch.ones(UpperCamelCase__						,     UpperCamelCase__						)]						,     dim=1						)

           snake_case   :				Tuple				 =   []
           # If the number of encoder and decoder layers are present in the model configuration, both are considered
           snake_case ,snake_case   :				Dict				 =   self.num_layers
           snake_case   :				Dict				 =   min(UpperCamelCase__						,     UpperCamelCase__						)
           snake_case   :				Optional[Any]				 =   max(UpperCamelCase__						,     UpperCamelCase__						) - min_num_layers
           snake_case   :				Optional[int]				 =   "encoder" if num_encoder_layers > num_decoder_layers else "decoder"

           for _ in range(UpperCamelCase__						):
               common_inputs["past_key_values"].append(
                   (
                       torch.zeros(UpperCamelCase__						),
                       torch.zeros(UpperCamelCase__						),
                       torch.zeros(UpperCamelCase__						),
                       torch.zeros(UpperCamelCase__						),
                   )						)
           # TODO: test this.
           snake_case   :				Union[str, Any]				 =   encoder_shape if remaining_side_name == "encoder" else decoder_shape
           for _ in range(UpperCamelCase__						,     UpperCamelCase__						):
               common_inputs["past_key_values"].append((torch.zeros(UpperCamelCase__						), torch.zeros(UpperCamelCase__						))						)
       return common_inputs







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__ = -1						,     UpperCamelCase__ = -1						,     UpperCamelCase__ = False						,     UpperCamelCase__ = None						,     )     ->		Mapping[str, Any]:

       '''simple docstring'''




       snake_case   :				Optional[int]				 =   self._generate_dummy_inputs_for_encoder_and_decoder(
           UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)

       if self.use_past:
           if not is_torch_available():
               raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed."						)
           else:
               import torch
           snake_case ,snake_case   :				List[str]				 =   common_inputs["input_ids"].shape
           # Not using the same length for past_key_values
           snake_case   :				List[str]				 =   seqlen + 2
           snake_case ,snake_case   :				Tuple				 =   self.num_layers
           snake_case ,snake_case   :				Optional[Any]				 =   self.num_attention_heads
           snake_case   :				List[Any]				 =   (
               batch,
               num_encoder_attention_heads,
               past_key_values_length,
               self._config.hidden_size // num_encoder_attention_heads,
           )

           snake_case   :				int				 =   common_inputs["attention_mask"].dtype
           snake_case   :				List[str]				 =   torch.cat(
               [common_inputs["attention_mask"], torch.ones(UpperCamelCase__						,     UpperCamelCase__						,     dtype=UpperCamelCase__						)]						,     dim=1						)
           snake_case   :				Union[str, Any]				 =   [
               (torch.zeros(UpperCamelCase__						), torch.zeros(UpperCamelCase__						)) for _ in range(UpperCamelCase__						)
           ]
       return common_inputs







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__ = -1						,     UpperCamelCase__ = -1						,     UpperCamelCase__ = False						,     UpperCamelCase__ = None						,     )     ->		Mapping[str, Any]:

       '''simple docstring'''




       snake_case   :				Optional[Any]				 =   compute_effective_axis_dimension(
           UpperCamelCase__						,     fixed_dimension=OnnxConfig.default_fixed_batch						,     num_token_to_add=0						)

       # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
       snake_case   :				Optional[Any]				 =   tokenizer.num_special_tokens_to_add(UpperCamelCase__						)
       snake_case   :				Union[str, Any]				 =   compute_effective_axis_dimension(
           UpperCamelCase__						,     fixed_dimension=OnnxConfig.default_fixed_sequence						,     num_token_to_add=UpperCamelCase__						)

       # Generate dummy inputs according to compute batch and sequence
       snake_case   :				int				 =   [" ".join([tokenizer.unk_token]						) * seq_length] * batch_size
       snake_case   :				Optional[int]				 =   dict(tokenizer(UpperCamelCase__						,     return_tensors=UpperCamelCase__						)						)
       return common_inputs







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__ = -1						,     UpperCamelCase__ = -1						,     UpperCamelCase__ = False						,     UpperCamelCase__ = None						,     )     ->		Mapping[str, Any]:

       '''simple docstring'''




       if self.task in ["default", "seq2seq-lm"]:
           snake_case   :				int				 =   self._generate_dummy_inputs_for_default_and_seqaseq_lm(
               UpperCamelCase__						,     batch_size=UpperCamelCase__						,     seq_length=UpperCamelCase__						,     is_pair=UpperCamelCase__						,     framework=UpperCamelCase__						)

       else:
           snake_case   :				Optional[Any]				 =   self._generate_dummy_inputs_for_causal_lm(
               UpperCamelCase__						,     batch_size=UpperCamelCase__						,     seq_length=UpperCamelCase__						,     is_pair=UpperCamelCase__						,     framework=UpperCamelCase__						)

       return common_inputs







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		int:

       '''simple docstring'''




       if self.task in ["default", "seq2seq-lm"]:
           snake_case   :				Union[str, Any]				 =   super()._flatten_past_key_values_(UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)
       else:
           snake_case   :				int				 =   super(UpperCamelCase__						,     self						)._flatten_past_key_values_(
               UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)







   @property
   def 			lowerCamelCase		(     self						)     ->		float:

       '''simple docstring'''




       return 1e-4









"""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
__snake_case             =   data_utils.TransfoXLTokenizer
__snake_case             =   data_utils.TransfoXLCorpus
__snake_case             =   data_utils
__snake_case             =   data_utils







def   __lowerCAmelCase						(						lowercase  :  Optional[int]						,							lowercase  :  int						,							lowercase  :  List[Any]						,							lowercase  :  Union[str, Any]			)     ->						List[Any]:


    """simple docstring"""





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

        snake_case   :				str				 =   corpus.__dict__
        corpus_dict_no_vocab.pop("vocab"						,							lowercase			)
        snake_case   :				Dict				 =   pytorch_dump_folder_path + "/" + CORPUS_NAME
        print(F'Save dataset to {pytorch_dataset_dump_path}'			)
        torch.save(lowercase						,							lowercase			)

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

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

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


if __name__ == "__main__":
  __snake_case             =   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.""",
  )
  __snake_case             =   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,
  )





