





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


__A       : Any						    = logging.get_logger(__name__)

__A       : Dict						    = {
    "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(	__snake_case  ):
     '''simple docstring'''


     __magic_name__        = 'gpt2'
     __magic_name__        = ['past_key_values']
     __magic_name__        = {
         'hidden_size': 'n_embd',
         'max_position_embeddings': 'n_positions',
         'num_attention_heads': 'n_head',
         'num_hidden_layers': 'n_layer',
     }




     def __init__(						self							,    snake_case_=5_0257							,    snake_case_=1024							,    snake_case_=768							,    snake_case_=12							,    snake_case_=12							,    snake_case_=None							,    snake_case_="gelu_new"							,    snake_case_=0.1							,    snake_case_=0.1							,    snake_case_=0.1							,    snake_case_=1E-5							,    snake_case_=0.02							,    snake_case_="cls_index"							,    snake_case_=True							,    snake_case_=None							,    snake_case_=True							,    snake_case_=0.1							,    snake_case_=True							,    snake_case_=True							,    snake_case_=5_0256							,    snake_case_=5_0256							,    snake_case_=False							,    snake_case_=False							,    **snake_case_							,    ):
         _A    =       vocab_size
         _A    =       n_positions
         _A    =       n_embd
         _A    =       n_layer
         _A    =       n_head
         _A    =       n_inner
         _A    =       activation_function
         _A    =       resid_pdrop
         _A    =       embd_pdrop
         _A    =       attn_pdrop
         _A    =       layer_norm_epsilon
         _A    =       initializer_range
         _A    =       summary_type
         _A    =       summary_use_proj
         _A    =       summary_activation
         _A    =       summary_first_dropout
         _A    =       summary_proj_to_labels
         _A    =       scale_attn_weights
         _A    =       use_cache
         _A    =       scale_attn_by_inverse_layer_idx
         _A    =       reorder_and_upcast_attn

         _A    =       bos_token_id
         _A    =       eos_token_id

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







class     lowerCamelCase(	__snake_case  ):
     '''simple docstring'''


     def __init__(						self							,    snake_case_							,    snake_case_ = "default"							,    snake_case_ = None							,    snake_case_ = 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?
             _A    =       0




     @property
     def 					lowerCAmelCase__		(						self       ):
         _A    =       OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}}       )
         if self.use_past:
             self.fill_with_past_key_values_(snake_case_							,    direction='inputs'       )
             _A    =       {0: 'batch', 1: 'past_sequence + sequence'}
         else:
             _A    =       {0: 'batch', 1: 'sequence'}

         return common_inputs




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




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




     def 					lowerCAmelCase__		(						self							,    snake_case_							,    snake_case_ = -1							,    snake_case_ = -1							,    snake_case_ = False							,    snake_case_ = None							,    ):
         _A    =       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()
         _A    =       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

                 _A, _A    =       common_inputs['input_ids'].shape
                 # Not using the same length for past_key_values
                 _A    =       seqlen + 2
                 _A    =       (
                     batch,
                     self.num_attention_heads,
                     past_key_values_length,
                     self._config.hidden_size // self.num_attention_heads,
                 )
                 _A    =       [
                     (torch.zeros(snake_case_       ), torch.zeros(snake_case_       )) for _ in range(self.num_layers       )
                 ]

         _A    =       common_inputs['attention_mask']
         if self.use_past:
             _A    =       ordered_inputs['attention_mask'].dtype
             _A    =       torch.cat(
                 [ordered_inputs['attention_mask'], torch.ones(snake_case_							,    snake_case_							,    dtype=snake_case_       )]							,    dim=1       )

         return ordered_inputs




     @property
     def 					lowerCAmelCase__		(						self       ):
         return 13












from ..utils import DummyObject, requires_backends


__A       : str						    = None





def 							__lowerCAmelCase(					*_SCREAMING_SNAKE_CASE       ,    **_SCREAMING_SNAKE_CASE							)       ->   Tuple:




    """simple docstring"""




    requires_backends(_SCREAMING_SNAKE_CASE       ,    ['sentencepiece', 'tokenizers']							)





