




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

lowerCamelCase_  						=   {
    "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					( __lowerCamelCase     ):


     """simple docstring"""


     _A				:    Any									=  "gpt2"
     _A				:    Tuple									=  ["past_key_values"]
     _A				:    Optional[int]									=  {
         "hidden_size": "n_embd",
         "max_position_embeddings": "n_positions",
         "num_attention_heads": "n_head",
         "num_hidden_layers": "n_layer",
     }




     def __init__(	self			: List[str]				,_UpperCamelCase			: Optional[int]=5_0_2_5_7				,_UpperCamelCase			: str=1_0_2_4				,_UpperCamelCase			: Optional[Any]=7_6_8				,_UpperCamelCase			: str=1_2				,_UpperCamelCase			: Union[str, Any]=1_2				,_UpperCamelCase			: List[str]=None				,_UpperCamelCase			: Any="gelu_new"				,_UpperCamelCase			: Optional[Any]=0.1				,_UpperCamelCase			: Union[str, Any]=0.1				,_UpperCamelCase			: Tuple=0.1				,_UpperCamelCase			: Dict=1e-5				,_UpperCamelCase			: int=0.02				,_UpperCamelCase			: str="cls_index"				,_UpperCamelCase			: Dict=True				,_UpperCamelCase			: Any=None				,_UpperCamelCase			: Any=True				,_UpperCamelCase			: str=0.1				,_UpperCamelCase			: int=True				,_UpperCamelCase			: List[Any]=True				,_UpperCamelCase			: int=5_0_2_5_6				,_UpperCamelCase			: Optional[Any]=5_0_2_5_6				,_UpperCamelCase			: int=False				,_UpperCamelCase			: List[Any]=False				,**_UpperCamelCase			: int				,)		->   List[Any]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=vocab_size
         SCREAMING_SNAKE_CASE__								=n_positions
         SCREAMING_SNAKE_CASE__								=n_embd
         SCREAMING_SNAKE_CASE__								=n_layer
         SCREAMING_SNAKE_CASE__								=n_head
         SCREAMING_SNAKE_CASE__								=n_inner
         SCREAMING_SNAKE_CASE__								=activation_function
         SCREAMING_SNAKE_CASE__								=resid_pdrop
         SCREAMING_SNAKE_CASE__								=embd_pdrop
         SCREAMING_SNAKE_CASE__								=attn_pdrop
         SCREAMING_SNAKE_CASE__								=layer_norm_epsilon
         SCREAMING_SNAKE_CASE__								=initializer_range
         SCREAMING_SNAKE_CASE__								=summary_type
         SCREAMING_SNAKE_CASE__								=summary_use_proj
         SCREAMING_SNAKE_CASE__								=summary_activation
         SCREAMING_SNAKE_CASE__								=summary_first_dropout
         SCREAMING_SNAKE_CASE__								=summary_proj_to_labels
         SCREAMING_SNAKE_CASE__								=scale_attn_weights
         SCREAMING_SNAKE_CASE__								=use_cache
         SCREAMING_SNAKE_CASE__								=scale_attn_by_inverse_layer_idx
         SCREAMING_SNAKE_CASE__								=reorder_and_upcast_attn

         SCREAMING_SNAKE_CASE__								=bos_token_id
         SCREAMING_SNAKE_CASE__								=eos_token_id

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






class       __a					( __lowerCamelCase     ):


     """simple docstring"""


     def __init__(	self			: Union[str, Any]				,_UpperCamelCase			: PretrainedConfig				,_UpperCamelCase			: str = "default"				,_UpperCamelCase			: List[PatchingSpec] = None				,_UpperCamelCase			: bool = False				,)		->   Any:


         '''simple docstring'''





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




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


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}}						)
         if self.use_past:
             self.fill_with_past_key_values_(_UpperCamelCase				,direction="""inputs"""						)
             SCREAMING_SNAKE_CASE__								={0: """batch""", 1: """past_sequence + sequence"""}
         else:
             SCREAMING_SNAKE_CASE__								={0: """batch""", 1: """sequence"""}

         return common_inputs




     @property
     def   __A				(	self			: Union[str, Any]						)		->   int:


         '''simple docstring'''





         return self._config.n_layer




     @property
     def   __A				(	self			: List[str]						)		->   int:


         '''simple docstring'''





         return self._config.n_head




     def   __A				(	self			: str				,_UpperCamelCase			: PreTrainedTokenizer				,_UpperCamelCase			: int = -1				,_UpperCamelCase			: int = -1				,_UpperCamelCase			: bool = False				,_UpperCamelCase			: Optional[TensorType] = None				,)		->   Mapping[str, Any]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=super(_UpperCamelCase				,self						).generate_dummy_inputs(
             _UpperCamelCase				,batch_size=_UpperCamelCase				,seq_length=_UpperCamelCase				,is_pair=_UpperCamelCase				,framework=_UpperCamelCase						)

         # We need to order the input in the way they appears in the forward()
         SCREAMING_SNAKE_CASE__								=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

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

         SCREAMING_SNAKE_CASE__								=common_inputs["""attention_mask"""]
         if self.use_past:
             SCREAMING_SNAKE_CASE__								=ordered_inputs["""attention_mask"""].dtype
             SCREAMING_SNAKE_CASE__								=torch.cat(
                 [ordered_inputs["""attention_mask"""], torch.ones(_UpperCamelCase				,_UpperCamelCase				,dtype=_UpperCamelCase						)]				,dim=1						)

         return ordered_inputs




     @property
     def   __A				(	self			: Optional[Any]						)		->   int:


         '''simple docstring'''





         return 1_3












import unittest
from dataclasses import dataclass
from typing import Optional

from transformers.utils import ModelOutput
@dataclass
class       __a					( __lowerCamelCase     ):


     """simple docstring"""


     _A				:    float
     _A				:    Optional[float]									=  None
     _A				:    Optional[float]									=  None






class       __a					( unittest.TestCase     ):


     """simple docstring"""


     def   __A				(	self			: Dict						)		->   Optional[int]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0						)
         self.assertEqual(x.a				,3_0						)
         self.assertIsNone(x.b						)
         self.assertIsNone(x.c						)
         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=x.d




     def   __A				(	self			: Tuple						)		->   Dict:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0				,b=1_0						)
         self.assertEqual(x[0]				,3_0						)
         self.assertEqual(x[1]				,1_0						)
         self.assertEqual(x[:2]				,(3_0, 1_0)						)
         self.assertEqual(x[:]				,(3_0, 1_0)						)

         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0				,c=1_0						)
         self.assertEqual(x[0]				,3_0						)
         self.assertEqual(x[1]				,1_0						)
         self.assertEqual(x[:2]				,(3_0, 1_0)						)
         self.assertEqual(x[:]				,(3_0, 1_0)						)




     def   __A				(	self			: List[str]						)		->   Dict:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0				,b=1_0						)
         self.assertEqual(x["""a"""]				,3_0						)
         self.assertEqual(x["""b"""]				,1_0						)
         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=x["""c"""]

         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0				,c=1_0						)
         self.assertEqual(x["""a"""]				,3_0						)
         self.assertEqual(x["""c"""]				,1_0						)
         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=x["""b"""]




     def   __A				(	self			: Optional[int]						)		->   List[str]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0						)
         self.assertEqual(list(x.keys()						)				,["""a"""]						)
         self.assertEqual(list(x.values()						)				,[3_0]						)
         self.assertEqual(list(x.items()						)				,[("""a""", 3_0)]						)
         self.assertEqual(list(_UpperCamelCase						)				,["""a"""]						)

         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0				,b=1_0						)
         self.assertEqual(list(x.keys()						)				,["""a""", """b"""]						)
         self.assertEqual(list(x.values()						)				,[3_0, 1_0]						)
         self.assertEqual(list(x.items()						)				,[("""a""", 3_0), ("""b""", 1_0)]						)
         self.assertEqual(list(_UpperCamelCase						)				,["""a""", """b"""]						)

         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0				,c=1_0						)
         self.assertEqual(list(x.keys()						)				,["""a""", """c"""]						)
         self.assertEqual(list(x.values()						)				,[3_0, 1_0]						)
         self.assertEqual(list(x.items()						)				,[("""a""", 3_0), ("""c""", 1_0)]						)
         self.assertEqual(list(_UpperCamelCase						)				,["""a""", """c"""]						)

         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=x.update({"""d""": 2_0}						)
         with self.assertRaises(_UpperCamelCase						):
             del x["a"]
         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=x.pop("""a"""						)
         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=x.setdefault("""d"""				,3_2						)




     def   __A				(	self			: int						)		->   List[Any]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0						)
         SCREAMING_SNAKE_CASE__								=1_0
         self.assertEqual(x.a				,1_0						)
         self.assertEqual(x["""a"""]				,1_0						)




     def   __A				(	self			: Union[str, Any]						)		->   Optional[Any]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=3_0						)
         SCREAMING_SNAKE_CASE__								=1_0
         self.assertEqual(x.a				,1_0						)
         self.assertEqual(x["""a"""]				,1_0						)




     def   __A				(	self			: Dict						)		->   List[str]:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest({"""a""": 3_0, """b""": 1_0}						)
         self.assertEqual(list(x.keys()						)				,["""a""", """b"""]						)
         self.assertEqual(x.a				,3_0						)
         self.assertEqual(x.b				,1_0						)




     def   __A				(	self			: Optional[int]						)		->   int:


         '''simple docstring'''





         SCREAMING_SNAKE_CASE__								=ModelOutputTest([("""a""", 3_0), ("""b""", 1_0)]						)
         self.assertEqual(list(x.keys()						)				,["""a""", """b"""]						)
         self.assertEqual(x.a				,3_0						)
         self.assertEqual(x.b				,1_0						)

         with self.assertRaises(_UpperCamelCase						):
             SCREAMING_SNAKE_CASE__								=ModelOutputTest([("""a""", 3_0), (1_0, 1_0)]						)

         SCREAMING_SNAKE_CASE__								=ModelOutputTest(a=(3_0, 3_0)						)
         self.assertEqual(list(x.keys()						)				,["""a"""]						)
         self.assertEqual(x.a				,(3_0, 3_0)						)






