





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


snake_case__   :				List[str]						=	logging.get_logger(__name__)

snake_case__   :				Optional[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 A_   (     _a  ):
   lowerCAmelCase__  	=					"""gpt2"""
   lowerCAmelCase__  	=					["""past_key_values"""]
   lowerCAmelCase__  	=					{
       """hidden_size""": """n_embd""",
       """max_position_embeddings""": """n_positions""",
       """num_attention_heads""": """n_head""",
       """num_hidden_layers""": """n_layer""",
   }




   def __init__(self      :Optional[Any]       ,  _UpperCamelCase      :Tuple=5_0257       ,  _UpperCamelCase      :Optional[int]=1024       ,  _UpperCamelCase      :Any=768       ,  _UpperCamelCase      :Optional[int]=12       ,  _UpperCamelCase      :Union[str, Any]=12       ,  _UpperCamelCase      :Optional[int]=None       ,  _UpperCamelCase      :Tuple="gelu_new"       ,  _UpperCamelCase      :str=0.1       ,  _UpperCamelCase      :str=0.1       ,  _UpperCamelCase      :List[Any]=0.1       ,  _UpperCamelCase      :Optional[Any]=1e-5       ,  _UpperCamelCase      :Dict=0.0_2       ,  _UpperCamelCase      :Optional[int]="cls_index"       ,  _UpperCamelCase      :str=True       ,  _UpperCamelCase      :Tuple=None       ,  _UpperCamelCase      :Optional[int]=True       ,  _UpperCamelCase      :Dict=0.1       ,  _UpperCamelCase      :int=True       ,  _UpperCamelCase      :List[Any]=True       ,  _UpperCamelCase      :Optional[int]=5_0256       ,  _UpperCamelCase      :List[Any]=5_0256       ,  _UpperCamelCase      :Optional[int]=False       ,  _UpperCamelCase      :Dict=False       ,  **_UpperCamelCase      :List[Any]       ,  )->   List[str]:
         __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=_SCREAMING_SNAKE_CASE       ,  eos_token_id=_SCREAMING_SNAKE_CASE       ,  **_SCREAMING_SNAKE_CASE					)





class A_   (     _a  ):




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




   @property
   def _lowerCAmelCase (self      :List[str]					)->   Mapping[str, Mapping[int, str]]:
         __A			  =							OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}}					)
         if self.use_past:
               self.fill_with_past_key_values_(_SCREAMING_SNAKE_CASE       ,  direction='''inputs'''					)
               __A			  =							{0: """batch""", 1: """past_sequence + sequence"""}
         else:
               __A			  =							{0: """batch""", 1: """sequence"""}

         return common_inputs




   @property
   def _lowerCAmelCase (self      :List[Any]					)->   int:
         return self._config.n_layer




   @property
   def _lowerCAmelCase (self      :Optional[int]					)->   int:
         return self._config.n_head




   def _lowerCAmelCase (self      :Optional[int]       ,  _UpperCamelCase      :PreTrainedTokenizer       ,  _UpperCamelCase      :int = -1       ,  _UpperCamelCase      :int = -1       ,  _UpperCamelCase      :bool = False       ,  _UpperCamelCase      :Optional[TensorType] = None       ,  )->   Mapping[str, Any]:
         __A			  =							super(_SCREAMING_SNAKE_CASE       ,  self					).generate_dummy_inputs(
             _SCREAMING_SNAKE_CASE       ,  batch_size=_SCREAMING_SNAKE_CASE       ,  seq_length=_SCREAMING_SNAKE_CASE       ,  is_pair=_SCREAMING_SNAKE_CASE       ,  framework=_SCREAMING_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			  =							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(_SCREAMING_SNAKE_CASE					), torch.zeros(_SCREAMING_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(_SCREAMING_SNAKE_CASE       ,  _SCREAMING_SNAKE_CASE       ,  dtype=_SCREAMING_SNAKE_CASE					)]       ,  dim=1					)

         return ordered_inputs






   @property
   def _lowerCAmelCase (self      :Any					)->   int:
         return 13







'''simple docstring'''


__SCREAMING_SNAKE_CASE						=      {hex(i)[2:]: i for i in range(16)}  # Use [:2] to strip off the leading '0x'
def 					__magic_name__(       UpperCAmelCase__ :       str	):
						UpperCamelCase_     :	str					     =     hex_string.strip().lower()
						if not hex_string:
												raise ValueError("""Empty string was passed to the function"""	)
						UpperCamelCase_     :	Dict					     =     hex_string[0] == """-"""
						if is_negative:
												UpperCamelCase_     :	List[str]					     =     hex_string[1:]
						if not all(char in hex_table for char in hex_string	):
												raise ValueError("""Non-hexadecimal value was passed to the function"""	)
						UpperCamelCase_     :	int					     =     0
						for char in hex_string:
												UpperCamelCase_     :	Any					     =     16 * decimal_number + hex_table[char]
						return -decimal_number if is_negative else decimal_number


if __name__ == "__main__":
	from doctest import testmod

	testmod()




