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

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


UpperCAmelCase							=       logging.get_logger(__name__)

UpperCAmelCase							=       {
    "gpt2": "https://huggingface.co/gpt2/resolve/main/config.json",
    "gpt2-medium": "https://huggingface.co/gpt2-medium/resolve/main/config.json",
    "gpt2-large": "https://huggingface.co/gpt2-large/resolve/main/config.json",
    "gpt2-xl": "https://huggingface.co/gpt2-xl/resolve/main/config.json",
    "distilgpt2": "https://huggingface.co/distilgpt2/resolve/main/config.json",
}



class 					snake_case__       (				__UpperCAmelCase							):
   _SCREAMING_SNAKE_CASE	: int						    =      """gpt2"""
   _SCREAMING_SNAKE_CASE	: Optional[int]						    =      ["""past_key_values"""]
   _SCREAMING_SNAKE_CASE	: Any						    =      {
       """hidden_size""": """n_embd""",
       """max_position_embeddings""": """n_positions""",
       """num_attention_heads""": """n_head""",
       """num_hidden_layers""": """n_layer""",
   }



   def __init__(				self   :   Tuple			,	A__   :   Optional[int]=5_02_57			,	A__   :   Optional[Any]=10_24			,	A__   :   List[str]=7_68			,	A__   :   List[Any]=12			,	A__   :   int=12			,	A__   :   Union[str, Any]=None			,	A__   :   Union[str, Any]="gelu_new"			,	A__   :   str=0.1			,	A__   :   Union[str, Any]=0.1			,	A__   :   Dict=0.1			,	A__   :   List[str]=1E-5			,	A__   :   Optional[Any]=0.02			,	A__   :   Optional[int]="cls_index"			,	A__   :   Dict=True			,	A__   :   Optional[Any]=None			,	A__   :   Any=True			,	A__   :   str=0.1			,	A__   :   str=True			,	A__   :   List[Any]=True			,	A__   :   Any=5_02_56			,	A__   :   Union[str, Any]=5_02_56			,	A__   :   Union[str, Any]=False			,	A__   :   List[str]=False			,	**A__   :   Dict			,	)							->     Union[str, Any]:




         '''simple docstring'''




         snake_case_     :			Tuple       =       vocab_size
         snake_case_     :			Tuple       =       n_positions
         snake_case_     :			List[str]       =       n_embd
         snake_case_     :			Optional[Any]       =       n_layer
         snake_case_     :			Union[str, Any]       =       n_head
         snake_case_     :			Tuple       =       n_inner
         snake_case_     :			Tuple       =       activation_function
         snake_case_     :			List[Any]       =       resid_pdrop
         snake_case_     :			Tuple       =       embd_pdrop
         snake_case_     :			Any       =       attn_pdrop
         snake_case_     :			List[str]       =       layer_norm_epsilon
         snake_case_     :			Optional[int]       =       initializer_range
         snake_case_     :			Optional[Any]       =       summary_type
         snake_case_     :			Union[str, Any]       =       summary_use_proj
         snake_case_     :			Dict       =       summary_activation
         snake_case_     :			Optional[Any]       =       summary_first_dropout
         snake_case_     :			List[str]       =       summary_proj_to_labels
         snake_case_     :			Optional[Any]       =       scale_attn_weights
         snake_case_     :			Optional[Any]       =       use_cache
         snake_case_     :			Dict       =       scale_attn_by_inverse_layer_idx
         snake_case_     :			Tuple       =       reorder_and_upcast_attn

         snake_case_     :			int       =       bos_token_id
         snake_case_     :			Union[str, Any]       =       eos_token_id

         super().__init__(bos_token_id=lowerCamelCase__			,	eos_token_id=lowerCamelCase__			,	**lowerCamelCase__   )



class 					snake_case__       (				__UpperCAmelCase							):



   def __init__(				self   :   List[str]			,	A__   :   int			,	A__   :   int = "default"			,	A__   :   Dict = None			,	A__   :   str = False			,	)							->     Optional[int]:




         '''simple docstring'''




         super().__init__(lowerCamelCase__			,	task=lowerCamelCase__			,	patching_specs=lowerCamelCase__			,	use_past=lowerCamelCase__   )
         if not getattr(self._config			,	"pad_token_id"			,	lowerCamelCase__   ):
               # TODO: how to do that better?
               snake_case_     :			Tuple       =       0



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




         '''simple docstring'''




         snake_case_     :			List[Any]       =       OrderedDict({"input_ids": {0: "batch", 1: "sequence"}}   )
         if self.use_past:
               self.fill_with_past_key_values_(lowerCamelCase__			,	direction="inputs"   )
               snake_case_     :			str       =       {0: "batch", 1: "past_sequence + sequence"}
         else:
               snake_case_     :			List[str]       =       {0: "batch", 1: "sequence"}

         return common_inputs



   @property
   def 					UpperCAmelCase__       (				self   :   List[Any]   )							->     int:




         '''simple docstring'''




         return self._config.n_layer



   @property
   def 					UpperCAmelCase__       (				self   :   str   )							->     int:




         '''simple docstring'''




         return self._config.n_head



   def 					UpperCAmelCase__       (				self   :   List[str]			,	A__   :   str			,	A__   :   Optional[int] = -1			,	A__   :   int = -1			,	A__   :   Dict = False			,	A__   :   Tuple = None			,	)							->     Mapping[str, Any]:




         '''simple docstring'''




         snake_case_     :			int       =       super(lowerCamelCase__			,	self   ).generate_dummy_inputs(
             lowerCamelCase__			,	batch_size=lowerCamelCase__			,	seq_length=lowerCamelCase__			,	is_pair=lowerCamelCase__			,	framework=lowerCamelCase__   )

         # We need to order the input in the way they appears in the forward()
         snake_case_     :			Dict       =       OrderedDict({"input_ids": common_inputs["input_ids"]}   )

         # Need to add the past_keys
         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_     :			int       =       common_inputs["input_ids"].shape
                     # Not using the same length for past_key_values
                     snake_case_     :			List[str]       =       seqlen + 2
                     snake_case_     :			List[str]       =       (
                         batch,
                         self.num_attention_heads,
                         past_key_values_length,
                         self._config.hidden_size // self.num_attention_heads,
                     )
                     snake_case_     :			Union[str, Any]       =       [
                         (torch.zeros(lowerCamelCase__   ), torch.zeros(lowerCamelCase__   )) for _ in range(self.num_layers   )
                     ]

         snake_case_     :			Optional[int]       =       common_inputs["attention_mask"]
         if self.use_past:
               snake_case_     :			Any       =       ordered_inputs["attention_mask"].dtype
               snake_case_     :			Optional[Any]       =       torch.cat(
                   [ordered_inputs["attention_mask"], torch.ones(lowerCamelCase__			,	lowerCamelCase__			,	dtype=lowerCamelCase__   )]			,	dim=1   )

         return ordered_inputs



   @property
   def 					UpperCAmelCase__       (				self   :   Tuple   )							->     int:




         '''simple docstring'''




         return 13




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast
from ...utils import logging


UpperCAmelCase							=       logging.get_logger(__name__)

UpperCAmelCase							=       {
    "uclanlp/plbart-base": "https://huggingface.co/uclanlp/plbart-base/resolve/main/config.json",
    # See all PLBART models at https://huggingface.co/models?filter=plbart
}



class 					snake_case__       (				_UpperCamelCase							):
   _SCREAMING_SNAKE_CASE	: List[str]						    =      "plbart"
   _SCREAMING_SNAKE_CASE	: Union[str, Any]						    =      ["past_key_values"]
   _SCREAMING_SNAKE_CASE	: Dict						    =      {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}



   def __init__(				self   :   Dict			,	A__   :   Any=5_00_05			,	A__   :   Optional[int]=10_24			,	A__   :   Tuple=6			,	A__   :   Optional[Any]=30_72			,	A__   :   Dict=12			,	A__   :   Union[str, Any]=6			,	A__   :   Union[str, Any]=30_72			,	A__   :   Tuple=12			,	A__   :   List[str]=0.0			,	A__   :   Dict=0.0			,	A__   :   int=True			,	A__   :   Tuple=True			,	A__   :   List[str]="gelu"			,	A__   :   List[Any]=7_68			,	A__   :   Optional[Any]=0.1			,	A__   :   List[Any]=0.1			,	A__   :   Any=0.0			,	A__   :   int=0.02			,	A__   :   Optional[Any]=0.0			,	A__   :   List[str]=True			,	A__   :   Any=1			,	A__   :   Tuple=0			,	A__   :   Any=2			,	A__   :   int=2			,	**A__   :   Dict			,	)							->     int:




         '''simple docstring'''




         snake_case_     :			int       =       vocab_size
         snake_case_     :			List[Any]       =       max_position_embeddings
         snake_case_     :			str       =       d_model
         snake_case_     :			Tuple       =       encoder_ffn_dim
         snake_case_     :			Union[str, Any]       =       encoder_layers
         snake_case_     :			Dict       =       encoder_attention_heads
         snake_case_     :			Union[str, Any]       =       decoder_ffn_dim
         snake_case_     :			Union[str, Any]       =       decoder_layers
         snake_case_     :			List[str]       =       decoder_attention_heads
         snake_case_     :			List[Any]       =       dropout
         snake_case_     :			Union[str, Any]       =       attention_dropout
         snake_case_     :			Optional[int]       =       activation_dropout
         snake_case_     :			List[str]       =       activation_function
         snake_case_     :			Tuple       =       init_std
         snake_case_     :			str       =       encoder_layerdrop
         snake_case_     :			int       =       decoder_layerdrop
         snake_case_     :			Optional[int]       =       classifier_dropout
         snake_case_     :			List[str]       =       use_cache
         snake_case_     :			List[Any]       =       encoder_layers
         snake_case_     :			Tuple       =       scale_embedding  # scale factor will be sqrt(d_model) if True
         super().__init__(
             pad_token_id=A__			,	bos_token_id=A__			,	eos_token_id=A__			,	is_encoder_decoder=A__			,	forced_eos_token_id=A__			,	**A__			,	)



class 					snake_case__       (				_UpperCamelCase							):



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




         '''simple docstring'''




         return OrderedDict(
             [
                 ("input_ids", {0: "batch", 1: "sequence"}),
                 ("attention_mask", {0: "batch", 1: "sequence"}),
             ]   )



   @property
   def 					UpperCAmelCase__       (				self   :   List[str]   )							->     Mapping[str, Mapping[int, str]]:




         '''simple docstring'''




         if self.use_past:
               return OrderedDict(
                   [
                       ("last_hidden_state", {0: "batch", 1: "sequence"}),
                       ("past_keys", {0: "batch", 2: "sequence"}),
                       ("encoder_last_hidden_state", {0: "batch", 1: "sequence"}),
                   ]   )
         else:
               return OrderedDict(
                   [
                       ("last_hidden_state", {0: "batch", 1: "sequence"}),
                       ("encoder_last_hidden_state", {0: "batch", 1: "sequence"}),
                   ]   )



