





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_       : int							=    logging.get_logger(__name__)

UpperCAmelCase_       : Union[str, Any]							=    {
    """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   lowercase__     (				__A	):
  __UpperCamelCase						  =	"""gpt2"""
  __UpperCamelCase						  =	["""past_key_values"""]
  __UpperCamelCase						  =	{
      """hidden_size""": """n_embd""",
      """max_position_embeddings""": """n_positions""",
      """num_attention_heads""": """n_head""",
      """num_hidden_layers""": """n_layer""",
  }







  def __init__(						self     ,    _lowercase=50_257     ,    _lowercase=1_024     ,    _lowercase=768     ,    _lowercase=12     ,    _lowercase=12     ,    _lowercase=None     ,    _lowercase="gelu_new"     ,    _lowercase=0.1     ,    _lowercase=0.1     ,    _lowercase=0.1     ,    _lowercase=1e-5     ,    _lowercase=0.02     ,    _lowercase="cls_index"     ,    _lowercase=True     ,    _lowercase=None     ,    _lowercase=True     ,    _lowercase=0.1     ,    _lowercase=True     ,    _lowercase=True     ,    _lowercase=50_256     ,    _lowercase=50_256     ,    _lowercase=False     ,    _lowercase=False     ,    **_lowercase     ,    ):
       lowerCAmelCase_       :							List[Any]			 =		vocab_size
       lowerCAmelCase_       :							List[Any]			 =		n_positions
       lowerCAmelCase_       :							List[Any]			 =		n_embd
       lowerCAmelCase_       :							List[Any]			 =		n_layer
       lowerCAmelCase_       :							str			 =		n_head
       lowerCAmelCase_       :							int			 =		n_inner
       lowerCAmelCase_       :							Tuple			 =		activation_function
       lowerCAmelCase_       :							str			 =		resid_pdrop
       lowerCAmelCase_       :							int			 =		embd_pdrop
       lowerCAmelCase_       :							Dict			 =		attn_pdrop
       lowerCAmelCase_       :							Any			 =		layer_norm_epsilon
       lowerCAmelCase_       :							int			 =		initializer_range
       lowerCAmelCase_       :							Union[str, Any]			 =		summary_type
       lowerCAmelCase_       :							Any			 =		summary_use_proj
       lowerCAmelCase_       :							Tuple			 =		summary_activation
       lowerCAmelCase_       :							Optional[int]			 =		summary_first_dropout
       lowerCAmelCase_       :							List[str]			 =		summary_proj_to_labels
       lowerCAmelCase_       :							Optional[Any]			 =		scale_attn_weights
       lowerCAmelCase_       :							str			 =		use_cache
       lowerCAmelCase_       :							int			 =		scale_attn_by_inverse_layer_idx
       lowerCAmelCase_       :							int			 =		reorder_and_upcast_attn

       lowerCAmelCase_       :							List[Any]			 =		bos_token_id
       lowerCAmelCase_       :							List[str]			 =		eos_token_id

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





class   lowercase__     (				__A	):







  def __init__(						self     ,    _lowercase     ,    _lowercase = "default"     ,    _lowercase = None     ,    _lowercase = False     ,    ):
       super().__init__(_lowercase     ,    task=_lowercase     ,    patching_specs=_lowercase     ,    use_past=_lowercase    )
       if not getattr(self._config     ,    """pad_token_id"""     ,    _lowercase    ):
            # TODO: how to do that better?
            lowerCAmelCase_       :							Tuple			 =		0







  @property
  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							str			 =		OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}}    )
       if self.use_past:
            self.fill_with_past_key_values_(_lowercase     ,    direction="""inputs"""    )
            lowerCAmelCase_       :							Optional[int]			 =		{0: """batch""", 1: """past_sequence + sequence"""}
       else:
            lowerCAmelCase_       :							Any			 =		{0: """batch""", 1: """sequence"""}

       return common_inputs







  @property
  def 						UpperCAmelCase__		(						self    ):
       return self._config.n_layer







  @property
  def 						UpperCAmelCase__		(						self    ):
       return self._config.n_head







  def 						UpperCAmelCase__		(						self     ,    _lowercase     ,    _lowercase = -1     ,    _lowercase = -1     ,    _lowercase = False     ,    _lowercase = None     ,    ):
       lowerCAmelCase_       :							Optional[int]			 =		super(_lowercase     ,    self    ).generate_dummy_inputs(
           _lowercase     ,    batch_size=_lowercase     ,    seq_length=_lowercase     ,    is_pair=_lowercase     ,    framework=_lowercase    )

       # We need to order the input in the way they appears in the forward()
       lowerCAmelCase_       :							List[str]			 =		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

                 lowerCAmelCase_ ,	lowerCAmelCase_       :							Optional[Any]			 =		common_inputs["""input_ids"""].shape
                 # Not using the same length for past_key_values
                 lowerCAmelCase_       :							Optional[Any]			 =		seqlen + 2
                 lowerCAmelCase_       :							Optional[Any]			 =		(
                     batch,
                     self.num_attention_heads,
                     past_key_values_length,
                     self._config.hidden_size // self.num_attention_heads,
                 )
                 lowerCAmelCase_       :							Tuple			 =		[
                     (torch.zeros(_lowercase    ), torch.zeros(_lowercase    )) for _ in range(self.num_layers    )
                 ]

       lowerCAmelCase_       :							Optional[int]			 =		common_inputs["""attention_mask"""]
       if self.use_past:
            lowerCAmelCase_       :							List[str]			 =		ordered_inputs["""attention_mask"""].dtype
            lowerCAmelCase_       :							Union[str, Any]			 =		torch.cat(
                [ordered_inputs["""attention_mask"""], torch.ones(_lowercase     ,    _lowercase     ,    dtype=_lowercase    )]     ,    dim=1    )

       return ordered_inputs







  @property
  def 						UpperCAmelCase__		(						self    ):
       return 13







def 				_lowerCAmelCase			(	_a :       str      )						->      int:
     lowerCAmelCase_       :							Dict			 =		0
     for plain_chr in plain_text:
          lowerCAmelCase_       :							List[Any]			 =		(
              ord(_a      ) + (hash_value << 6) + (hash_value << 16) - hash_value
          )
     return hash_value
