
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


__lowerCAmelCase   :     List[Any]      =						logging.get_logger(__name__)

__lowerCAmelCase   :     int      =						{
    "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   __lowerCAmelCase		(		lowerCAmelCase_ ):



      """simple docstring"""







      A__		:     Optional[Any]       				=      '''gpt2'''
      A__		:     str       				=      ['''past_key_values''']
      A__		:     List[Any]       				=      {
          '''hidden_size''': '''n_embd''',
          '''max_position_embeddings''': '''n_positions''',
          '''num_attention_heads''': '''n_head''',
          '''num_hidden_layers''': '''n_layer''',
      }




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

          __lowercase						:     Any		      =    bos_token_id
          __lowercase						:     str		      =    eos_token_id

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






class   __lowerCAmelCase		(		lowerCAmelCase_ ):



      """simple docstring"""







      def __init__( self						:    Dict			,    _snake_case						:    PretrainedConfig			,    _snake_case						:    str = "default"			,    _snake_case						:    List[PatchingSpec] = None			,    _snake_case						:    bool = False			,    ):
          super().__init__(_snake_case			,    task=_snake_case			,    patching_specs=_snake_case			,    use_past=_snake_case			)
          if not getattr(self._config			,    '''pad_token_id'''			,    _snake_case			):
              # TODO: how to do that better?
              __lowercase						:     Optional[Any]		      =    0




      @property
      def 						snake_case_     ( self						:    str			):
          __lowercase						:     Any		      =    OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}}			)
          if self.use_past:
              self.fill_with_past_key_values_(_snake_case			,    direction='''inputs'''			)
              __lowercase						:     Optional[Any]		      =    {0: '''batch''', 1: '''past_sequence + sequence'''}
          else:
              __lowercase						:     Dict		      =    {0: '''batch''', 1: '''sequence'''}

          return common_inputs




      @property
      def 						snake_case_     ( self						:    Optional[Any]			):
          return self._config.n_layer




      @property
      def 						snake_case_     ( self						:    str			):
          return self._config.n_head




      def 						snake_case_     ( self						:    Any			,    _snake_case						:    PreTrainedTokenizer			,    _snake_case						:    int = -1			,    _snake_case						:    int = -1			,    _snake_case						:    bool = False			,    _snake_case						:    Optional[TensorType] = None			,    ):
          __lowercase						:     Optional[Any]		      =    super(_snake_case			,    self			).generate_dummy_inputs(
              _snake_case			,    batch_size=_snake_case			,    seq_length=_snake_case			,    is_pair=_snake_case			,    framework=_snake_case			)

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

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

          __lowercase						:     str		      =    common_inputs['''attention_mask''']
          if self.use_past:
              __lowercase						:     int		      =    ordered_inputs['''attention_mask'''].dtype
              __lowercase						:     int		      =    torch.cat(
                  [ordered_inputs['''attention_mask'''], torch.ones(_snake_case			,    _snake_case			,    dtype=_snake_case			)]			,    dim=1			)

          return ordered_inputs




      @property
      def 						snake_case_     ( self						:    Dict			):
          return 13




__lowerCAmelCase   :     List[Any]      =						"\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n"

__lowerCAmelCase   :     Optional[Any]      =						[{"type": "code", "content": INSTALL_CONTENT}]
__lowerCAmelCase   :     List[str]      =						{
    "{processor_class}": "FakeProcessorClass",
    "{model_class}": "FakeModelClass",
    "{object_class}": "FakeObjectClass",
}


