




"""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


_lowercase		 =      logging.get_logger(__name__)

_lowercase		 =      {
    """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  __lowercase    (     snake_case_		):


				'''simple docstring'''


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

										snake_case__    :   Optional[Any]							     =   bos_token_id
										snake_case__    :   Tuple							     =   eos_token_id

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







class  __lowercase    (     snake_case_		):


				'''simple docstring'''


				def __init__(				self				:       List[Any] ,SCREAMING_SNAKE_CASE_				:       PretrainedConfig ,SCREAMING_SNAKE_CASE_				:       str = "default" ,SCREAMING_SNAKE_CASE_				:       List[PatchingSpec] = None ,SCREAMING_SNAKE_CASE_				:       bool = False ,):
										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?
																snake_case__    :   List[str]							     =   0

				@property
				def  _lowerCamelCase   (				self				:       Union[str, Any] ):
										snake_case__    :   Dict							     =   OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}} )
										if self.use_past:
																self.fill_with_past_key_values_(SCREAMING_SNAKE_CASE_ ,direction='''inputs''' )
																snake_case__    :   Dict							     =   {0: '''batch''', 1: '''past_sequence + sequence'''}
										else:
																snake_case__    :   List[Any]							     =   {0: '''batch''', 1: '''sequence'''}

										return common_inputs

				@property
				def  _lowerCamelCase   (				self				:       Any ):
										return self._config.n_layer

				@property
				def  _lowerCamelCase   (				self				:       Optional[Any] ):
										return self._config.n_head

				def  _lowerCamelCase   (				self				:       Optional[Any] ,SCREAMING_SNAKE_CASE_				:       PreTrainedTokenizer ,SCREAMING_SNAKE_CASE_				:       int = -1 ,SCREAMING_SNAKE_CASE_				:       int = -1 ,SCREAMING_SNAKE_CASE_				:       bool = False ,SCREAMING_SNAKE_CASE_				:       Optional[TensorType] = None ,):
										snake_case__    :   List[Any]							     =   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()
										snake_case__    :   Dict							     =   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

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

										snake_case__    :   Dict							     =   common_inputs['''attention_mask''']
										if self.use_past:
																snake_case__    :   str							     =   ordered_inputs['''attention_mask'''].dtype
																snake_case__    :   Optional[Any]							     =   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				:       str ):
										return 1_3








"""simple docstring"""





def 						lowerCAmelCase							(	lowerCamelCase						):
						'''simple docstring'''


						return int(lowerCamelCase						) if x - int(lowerCamelCase						) <= 0 else int(lowerCamelCase						) + 1


if __name__ == "__main__":
							import doctest

							doctest.testmod()


