


'''simple docstring'''





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 							UpperCAmelCase ( _snake_case	):
 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       :  str ,					__lowerCamelCase       :  str=5_0_2_5_7 ,					__lowerCamelCase       :  List[Any]=1_0_2_4 ,					__lowerCamelCase       :  Union[str, Any]=7_6_8 ,					__lowerCamelCase       :  str=1_2 ,					__lowerCamelCase       :  str=1_2 ,					__lowerCamelCase       :  Any=None ,					__lowerCamelCase       :  Union[str, Any]="gelu_new" ,					__lowerCamelCase       :  List[str]=0.1 ,					__lowerCamelCase       :  Tuple=0.1 ,					__lowerCamelCase       :  List[str]=0.1 ,					__lowerCamelCase       :  Optional[int]=1e-5 ,					__lowerCamelCase       :  Optional[int]=0.02 ,					__lowerCamelCase       :  List[str]="cls_index" ,					__lowerCamelCase       :  Optional[Any]=True ,					__lowerCamelCase       :  List[str]=None ,					__lowerCamelCase       :  str=True ,					__lowerCamelCase       :  Any=0.1 ,					__lowerCamelCase       :  Union[str, Any]=True ,					__lowerCamelCase       :  Optional[Any]=True ,					__lowerCamelCase       :  List[Any]=5_0_2_5_6 ,					__lowerCamelCase       :  int=5_0_2_5_6 ,					__lowerCamelCase       :  Tuple=False ,					__lowerCamelCase       :  List[str]=False ,					**__lowerCamelCase       :  int ,					):
    UpperCAmelCase__  :Optional[int]            =			vocab_size
    UpperCAmelCase__  :Any            =			n_positions
    UpperCAmelCase__  :Optional[Any]            =			n_embd
    UpperCAmelCase__  :Tuple            =			n_layer
    UpperCAmelCase__  :Optional[Any]            =			n_head
    UpperCAmelCase__  :Optional[int]            =			n_inner
    UpperCAmelCase__  :List[str]            =			activation_function
    UpperCAmelCase__  :Union[str, Any]            =			resid_pdrop
    UpperCAmelCase__  :Optional[int]            =			embd_pdrop
    UpperCAmelCase__  :str            =			attn_pdrop
    UpperCAmelCase__  :Optional[int]            =			layer_norm_epsilon
    UpperCAmelCase__  :str            =			initializer_range
    UpperCAmelCase__  :Dict            =			summary_type
    UpperCAmelCase__  :str            =			summary_use_proj
    UpperCAmelCase__  :Optional[int]            =			summary_activation
    UpperCAmelCase__  :Optional[int]            =			summary_first_dropout
    UpperCAmelCase__  :str            =			summary_proj_to_labels
    UpperCAmelCase__  :Union[str, Any]            =			scale_attn_weights
    UpperCAmelCase__  :str            =			use_cache
    UpperCAmelCase__  :Any            =			scale_attn_by_inverse_layer_idx
    UpperCAmelCase__  :int            =			reorder_and_upcast_attn

    UpperCAmelCase__  :Union[str, Any]            =			bos_token_id
    UpperCAmelCase__  :Optional[int]            =			eos_token_id

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




class 							UpperCAmelCase ( _snake_case	):


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


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

    return common_inputs


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


 @property
 def 					__SCREAMING_SNAKE_CASE				(			self       :  int   ):
    return self._config.n_head


 def 					__SCREAMING_SNAKE_CASE				(			self       :  str ,					__lowerCamelCase       :  PreTrainedTokenizer ,					__lowerCamelCase       :  int = -1 ,					__lowerCamelCase       :  int = -1 ,					__lowerCamelCase       :  bool = False ,					__lowerCamelCase       :  Optional[TensorType] = None ,					):
    UpperCAmelCase__  :List[Any]            =			super(__lowerCamelCase ,					self   ).generate_dummy_inputs(
        __lowerCamelCase ,					batch_size=__lowerCamelCase ,					seq_length=__lowerCamelCase ,					is_pair=__lowerCamelCase ,					framework=__lowerCamelCase   )

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

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

    UpperCAmelCase__  :str            =			common_inputs['''attention_mask''']
    if self.use_past:
       UpperCAmelCase__  :Optional[Any]            =			ordered_inputs['''attention_mask'''].dtype
       UpperCAmelCase__  :Any            =			torch.cat(
           [ordered_inputs['''attention_mask'''], torch.ones(__lowerCamelCase ,					__lowerCamelCase ,					dtype=__lowerCamelCase   )] ,					dim=1   )

    return ordered_inputs





 @property
 def 					__SCREAMING_SNAKE_CASE				(			self       :  Tuple   ):
    return 1_3







'''simple docstring'''





from typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
    import torch

__lowerCamelCase       				=	logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
    from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):


    def 					a__    (				cls	:			Optional[int]	):
       return cls


def 					a__    (				UpperCamelCase_	:			Union[Tuple, List],      UpperCamelCase_	:			Optional[Union[List["torch.Generator"], "torch.Generator"]] = None,      UpperCamelCase_	:			Optional["torch.device"] = None,      UpperCamelCase_	:			Optional["torch.dtype"] = None,      UpperCamelCase_	:			Optional["torch.layout"] = None,      ):
   UpperCAmelCase__  :Union[str, Any]            =			device
   UpperCAmelCase__  :List[Any]            =			shape[0]

   UpperCAmelCase__  :Tuple            =			layout or torch.strided
   UpperCAmelCase__  :Union[str, Any]            =			device or torch.device('''cpu'''	)

   if generator is not None:
      UpperCAmelCase__  :str            =			generator.device.type if not isinstance(UpperCamelCase_,      UpperCamelCase_	) else generator[0].device.type
      if gen_device_type != device.type and gen_device_type == "cpu":
         UpperCAmelCase__  :Tuple            =			'''cpu'''
         if device != "mps":
            logger.info(
                F'''The passed generator was created on \'cpu\' even though a tensor on {device} was expected.'''
                F''' Tensors will be created on \'cpu\' and then moved to {device}. Note that one can probably'''
                F''' slighly speed up this function by passing a generator that was created on the {device} device.'''	)
      elif gen_device_type != device.type and gen_device_type == "cuda":
         raise ValueError(F'''Cannot generate a {device} tensor from a generator of type {gen_device_type}.'''	)

   if isinstance(UpperCamelCase_,      UpperCamelCase_	):
      UpperCAmelCase__  :List[str]            =			(1,) + shape[1:]
      UpperCAmelCase__  :Optional[int]            =			[
          torch.randn(UpperCamelCase_,      generator=generator[i],      device=UpperCamelCase_,      dtype=UpperCamelCase_,      layout=UpperCamelCase_	)
          for i in range(UpperCamelCase_	)
      ]
      UpperCAmelCase__  :int            =			torch.cat(UpperCamelCase_,      dim=0	).to(UpperCamelCase_	)
   else:
      UpperCAmelCase__  :Optional[int]            =			torch.randn(UpperCamelCase_,      generator=UpperCamelCase_,      device=UpperCamelCase_,      dtype=UpperCamelCase_,      layout=UpperCamelCase_	).to(UpperCamelCase_	)

   return latents


def 					a__    (				UpperCamelCase_	:			Dict	):
   if is_torch_version('''<''',      '''2.0.0'''	) or not hasattr(UpperCamelCase_,      '''_dynamo'''	):
      return False
   return isinstance(UpperCamelCase_,      torch._dynamo.eval_frame.OptimizedModule	)



