





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

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


__A     =    logging.get_logger(__name__)

__A     =    {
    '''facebook/blenderbot_small-90M''': '''https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json''',
    # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small
}



class        _SCREAMING_SNAKE_CASE						(     lowerCamelCase_   ):







 '''simple docstring'''
 lowercase_		=      '''blenderbot-small'''
 lowercase_		=      ['''past_key_values''']
 lowercase_		=      {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}

 def __init__(self    :       Any	,			UpperCAmelCase_    :       Union[str, Any]=50_265	,			UpperCAmelCase_    :       Optional[int]=512	,			UpperCAmelCase_    :       Optional[Any]=8	,			UpperCAmelCase_    :       Tuple=2_048	,			UpperCAmelCase_    :       Union[str, Any]=16	,			UpperCAmelCase_    :       Union[str, Any]=8	,			UpperCAmelCase_    :       int=2_048	,			UpperCAmelCase_    :       Optional[int]=16	,			UpperCAmelCase_    :       List[Any]=0.0	,			UpperCAmelCase_    :       List[str]=0.0	,			UpperCAmelCase_    :       Optional[Any]=True	,			UpperCAmelCase_    :       Dict=True	,			UpperCAmelCase_    :       int="gelu"	,			UpperCAmelCase_    :       Optional[int]=512	,			UpperCAmelCase_    :       str=0.1	,			UpperCAmelCase_    :       int=0.0	,			UpperCAmelCase_    :       str=0.0	,			UpperCAmelCase_    :       Any=0.02	,			UpperCAmelCase_    :       int=1	,			UpperCAmelCase_    :       List[Any]=False	,			UpperCAmelCase_    :       List[str]=0	,			UpperCAmelCase_    :       List[Any]=1	,			UpperCAmelCase_    :       List[Any]=2	,			UpperCAmelCase_    :       Dict=2	,			**UpperCAmelCase_    :       Optional[int]	,			)	->Any:



   '''simple docstring'''



   lowerCamelCase__:				Union[str, Any]        =vocab_size
   lowerCamelCase__:				Optional[Any]        =max_position_embeddings
   lowerCamelCase__:				str        =d_model
   lowerCamelCase__:				Optional[Any]        =encoder_ffn_dim
   lowerCamelCase__:				Tuple        =encoder_layers
   lowerCamelCase__:				List[Any]        =encoder_attention_heads
   lowerCamelCase__:				int        =decoder_ffn_dim
   lowerCamelCase__:				List[Any]        =decoder_layers
   lowerCamelCase__:				Optional[Any]        =decoder_attention_heads
   lowerCamelCase__:				Optional[int]        =dropout
   lowerCamelCase__:				int        =attention_dropout
   lowerCamelCase__:				Optional[Any]        =activation_dropout
   lowerCamelCase__:				List[str]        =activation_function
   lowerCamelCase__:				Dict        =init_std
   lowerCamelCase__:				Optional[Any]        =encoder_layerdrop
   lowerCamelCase__:				Optional[int]        =decoder_layerdrop
   lowerCamelCase__:				str        =use_cache
   lowerCamelCase__:				Union[str, Any]        =encoder_layers
   lowerCamelCase__:				Optional[int]        =scale_embedding  # scale factor will be sqrt(d_model) if True

   super().__init__(
       pad_token_id=__snake_case	,			bos_token_id=__snake_case	,			eos_token_id=__snake_case	,			is_encoder_decoder=__snake_case	,			decoder_start_token_id=__snake_case	,			forced_eos_token_id=__snake_case	,			**__snake_case	,			)



class        _SCREAMING_SNAKE_CASE						(     lowerCamelCase_   ):







 '''simple docstring'''

 @property
 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[int])	->Mapping[str, Mapping[int, str]]:



   '''simple docstring'''



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

     if self.use_past:
       lowerCamelCase__:				Tuple        ={0: "batch"}
       lowerCamelCase__:				str        ={0: "batch", 1: "past_decoder_sequence + sequence"}
     else:
       lowerCamelCase__:				List[str]        ={0: "batch", 1: "decoder_sequence"}
       lowerCamelCase__:				Union[str, Any]        ={0: "batch", 1: "decoder_sequence"}

     if self.use_past:
       self.fill_with_past_key_values_(__snake_case	,			direction="inputs")
   elif self.task == "causal-lm":
     # TODO: figure this case out.
     lowerCamelCase__:				Any        =OrderedDict(
         [
             ("input_ids", {0: "batch", 1: "encoder_sequence"}),
             ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
         ])
     if self.use_past:
       lowerCamelCase__  , lowerCamelCase__:				List[Any]        =self.num_layers
       for i in range(__snake_case):
         lowerCamelCase__:				List[str]        ={0: "batch", 2: "past_sequence + sequence"}
         lowerCamelCase__:				List[Any]        ={0: "batch", 2: "past_sequence + sequence"}
   else:
     lowerCamelCase__:				str        =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
 def 							SCREAMING_SNAKE_CASE_ (self    :       Dict)	->Mapping[str, Mapping[int, str]]:



   '''simple docstring'''



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

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[Any]	,			UpperCAmelCase_    :       PreTrainedTokenizer	,			UpperCAmelCase_    :       int = -1	,			UpperCAmelCase_    :       int = -1	,			UpperCAmelCase_    :       bool = False	,			UpperCAmelCase_    :       Optional[TensorType] = None	,			)	->Mapping[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:				str        =self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
       __snake_case	,			__snake_case	,			__snake_case	,			__snake_case	,			__snake_case)

   # Generate decoder inputs
   lowerCamelCase__:				Optional[Any]        =seq_length if not self.use_past else 1
   lowerCamelCase__:				List[Any]        =self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
       __snake_case	,			__snake_case	,			__snake_case	,			__snake_case	,			__snake_case)
   lowerCamelCase__:				Union[str, Any]        ={F"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
   lowerCamelCase__:				List[str]        =dict(**__snake_case	,			**__snake_case)

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

     lowerCamelCase__:				Any        =torch.cat(
         [common_inputs["decoder_attention_mask"], torch.ones(__snake_case	,			__snake_case)]	,			dim=1)

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

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

 def 							SCREAMING_SNAKE_CASE_ (self    :       Dict	,			UpperCAmelCase_    :       PreTrainedTokenizer	,			UpperCAmelCase_    :       int = -1	,			UpperCAmelCase_    :       int = -1	,			UpperCAmelCase_    :       bool = False	,			UpperCAmelCase_    :       Optional[TensorType] = None	,			)	->Mapping[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:				Optional[int]        =self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
       __snake_case	,			__snake_case	,			__snake_case	,			__snake_case	,			__snake_case)

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

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

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[Any]	,			UpperCAmelCase_    :       PreTrainedTokenizer	,			UpperCAmelCase_    :       int = -1	,			UpperCAmelCase_    :       int = -1	,			UpperCAmelCase_    :       bool = False	,			UpperCAmelCase_    :       Optional[TensorType] = None	,			)	->Mapping[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:				str        =compute_effective_axis_dimension(
       __snake_case	,			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
   lowerCamelCase__:				Dict        =tokenizer.num_special_tokens_to_add(__snake_case)
   lowerCamelCase__:				Optional[int]        =compute_effective_axis_dimension(
       __snake_case	,			fixed_dimension=OnnxConfig.default_fixed_sequence	,			num_token_to_add=__snake_case)

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

 def 							SCREAMING_SNAKE_CASE_ (self    :       str	,			UpperCAmelCase_    :       PreTrainedTokenizer	,			UpperCAmelCase_    :       int = -1	,			UpperCAmelCase_    :       int = -1	,			UpperCAmelCase_    :       bool = False	,			UpperCAmelCase_    :       Optional[TensorType] = None	,			)	->Mapping[str, Any]:



   '''simple docstring'''



   if self.task in ["default", "seq2seq-lm"]:
     lowerCamelCase__:				List[str]        =self._generate_dummy_inputs_for_default_and_seqaseq_lm(
         __snake_case	,			batch_size=__snake_case	,			seq_length=__snake_case	,			is_pair=__snake_case	,			framework=__snake_case)

   elif self.task == "causal-lm":
     lowerCamelCase__:				Tuple        =self._generate_dummy_inputs_for_causal_lm(
         __snake_case	,			batch_size=__snake_case	,			seq_length=__snake_case	,			is_pair=__snake_case	,			framework=__snake_case)
   else:
     lowerCamelCase__:				Tuple        =self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
         __snake_case	,			batch_size=__snake_case	,			seq_length=__snake_case	,			is_pair=__snake_case	,			framework=__snake_case)

   return common_inputs







 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str]	,			UpperCAmelCase_    :       Union[str, Any]	,			UpperCAmelCase_    :       List[str]	,			UpperCAmelCase_    :       List[str]	,			UpperCAmelCase_    :       Optional[Any])	->List[str]:



   '''simple docstring'''



   if self.task in ["default", "seq2seq-lm"]:
     lowerCamelCase__:				List[Any]        =super()._flatten_past_key_values_(__snake_case	,			__snake_case	,			__snake_case	,			__snake_case)
   else:
     lowerCamelCase__:				Tuple        =super(__snake_case	,			self)._flatten_past_key_values_(
         __snake_case	,			__snake_case	,			__snake_case	,			__snake_case)








from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand





def 					lowerCAmelCase_			(	__a		)					->		int:







  """simple docstring"""





  return ConvertCommand(
      args.model_type     ,   args.tf_checkpoint     ,   args.pytorch_dump_output     ,   args.config     ,   args.finetuning_task_name		)


__A     =    "\ntransformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires\nTensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.\n"






class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''

 @staticmethod
 def 							SCREAMING_SNAKE_CASE_ (UpperCAmelCase_    :       ArgumentParser)	->str:



   '''simple docstring'''



   lowerCamelCase__:				Dict        =parser.add_parser(
       "convert"	,			help="CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints."	,			)
   train_parser.add_argument("--model_type"	,			type=UpperCAmelCase_	,			required=UpperCAmelCase_	,			help="Model's type.")
   train_parser.add_argument(
       "--tf_checkpoint"	,			type=UpperCAmelCase_	,			required=UpperCAmelCase_	,			help="TensorFlow checkpoint path or folder.")
   train_parser.add_argument(
       "--pytorch_dump_output"	,			type=UpperCAmelCase_	,			required=UpperCAmelCase_	,			help="Path to the PyTorch saved model output.")
   train_parser.add_argument("--config"	,			type=UpperCAmelCase_	,			default=""	,			help="Configuration file path or folder.")
   train_parser.add_argument(
       "--finetuning_task_name"	,			type=UpperCAmelCase_	,			default=UpperCAmelCase_	,			help="Optional fine-tuning task name if the TF model was a finetuned model."	,			)
   train_parser.set_defaults(func=UpperCAmelCase_)

 def __init__(self    :       List[Any]	,			UpperCAmelCase_    :       str	,			UpperCAmelCase_    :       str	,			UpperCAmelCase_    :       str	,			UpperCAmelCase_    :       str	,			UpperCAmelCase_    :       str	,			*UpperCAmelCase_    :       Optional[int]	,			)	->List[str]:



   '''simple docstring'''



   lowerCamelCase__:				Dict        =logging.get_logger("transformers-cli/converting")

   self._logger.info(F"""Loading model {model_type}""")
   lowerCamelCase__:				Any        =model_type
   lowerCamelCase__:				Optional[int]        =tf_checkpoint
   lowerCamelCase__:				Any        =pytorch_dump_output
   lowerCamelCase__:				Union[str, Any]        =config
   lowerCamelCase__:				str        =finetuning_task_name







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



   '''simple docstring'''



   if self._model_type == "albert":
     try:
       from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import (
           convert_tf_checkpoint_to_pytorch,
       )
     except ImportError:
       raise ImportError(UpperCAmelCase_)

     convert_tf_checkpoint_to_pytorch(self._tf_checkpoint	,			self._config	,			self._pytorch_dump_output)
   elif self._model_type == "bert":
     try:
       from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import (
           convert_tf_checkpoint_to_pytorch,
       )
     except ImportError:
       raise ImportError(UpperCAmelCase_)

     convert_tf_checkpoint_to_pytorch(self._tf_checkpoint	,			self._config	,			self._pytorch_dump_output)
   elif self._model_type == "funnel":
     try:
       from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import (
           convert_tf_checkpoint_to_pytorch,
       )
     except ImportError:
       raise ImportError(UpperCAmelCase_)

     convert_tf_checkpoint_to_pytorch(self._tf_checkpoint	,			self._config	,			self._pytorch_dump_output)
   elif self._model_type == "t5":
     try:
       from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch
     except ImportError:
       raise ImportError(UpperCAmelCase_)

     convert_tf_checkpoint_to_pytorch(self._tf_checkpoint	,			self._config	,			self._pytorch_dump_output)
   elif self._model_type == "gpt":
     from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import (
         convert_openai_checkpoint_to_pytorch,
     )

     convert_openai_checkpoint_to_pytorch(self._tf_checkpoint	,			self._config	,			self._pytorch_dump_output)
   elif self._model_type == "transfo_xl":
     try:
       from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import (
           convert_transfo_xl_checkpoint_to_pytorch,
       )
     except ImportError:
       raise ImportError(UpperCAmelCase_)

     if "ckpt" in self._tf_checkpoint.lower():
       lowerCamelCase__:				Tuple        =self._tf_checkpoint
       lowerCamelCase__:				List[str]        =""
     else:
       lowerCamelCase__:				Any        =self._tf_checkpoint
       lowerCamelCase__:				Dict        =""
     convert_transfo_xl_checkpoint_to_pytorch(
         UpperCAmelCase_	,			self._config	,			self._pytorch_dump_output	,			UpperCAmelCase_)
   elif self._model_type == "gpt2":
     try:
       from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import (
           convert_gpta_checkpoint_to_pytorch,
       )
     except ImportError:
       raise ImportError(UpperCAmelCase_)

     convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint	,			self._config	,			self._pytorch_dump_output)
   elif self._model_type == "xlnet":
     try:
       from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import (
           convert_xlnet_checkpoint_to_pytorch,
       )
     except ImportError:
       raise ImportError(UpperCAmelCase_)

     convert_xlnet_checkpoint_to_pytorch(
         self._tf_checkpoint	,			self._config	,			self._pytorch_dump_output	,			self._finetuning_task_name)
   elif self._model_type == "xlm":
     from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import (
         convert_xlm_checkpoint_to_pytorch,
     )

     convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint	,			self._pytorch_dump_output)
   elif self._model_type == "lxmert":
     from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import (
         convert_lxmert_checkpoint_to_pytorch,
     )

     convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint	,			self._pytorch_dump_output)
   elif self._model_type == "rembert":
     from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import (
         convert_rembert_tf_checkpoint_to_pytorch,
     )

     convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint	,			self._config	,			self._pytorch_dump_output)
   else:
     raise ValueError(
         "--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]")

