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


__SCREAMING_SNAKE_CASE						=      logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE						=      {
    "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     __snake_case			(    _a	):
		a_          =					"gpt2"
		a_          =					["past_key_values"]
		a_          =					{
		    "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					:Tuple=5_0_2_5_7			,    _SCREAMING_SNAKE_CASE					:Optional[int]=1_0_2_4			,    _SCREAMING_SNAKE_CASE					:Any=7_6_8			,    _SCREAMING_SNAKE_CASE					:Optional[int]=1_2			,    _SCREAMING_SNAKE_CASE					:Union[str, Any]=1_2			,    _SCREAMING_SNAKE_CASE					:Optional[int]=None			,    _SCREAMING_SNAKE_CASE					:Tuple="gelu_new"			,    _SCREAMING_SNAKE_CASE					:str=0.1			,    _SCREAMING_SNAKE_CASE					:str=0.1			,    _SCREAMING_SNAKE_CASE					:List[Any]=0.1			,    _SCREAMING_SNAKE_CASE					:Optional[Any]=1E-5			,    _SCREAMING_SNAKE_CASE					:Dict=0.02			,    _SCREAMING_SNAKE_CASE					:Optional[int]="cls_index"			,    _SCREAMING_SNAKE_CASE					:str=True			,    _SCREAMING_SNAKE_CASE					:Tuple=None			,    _SCREAMING_SNAKE_CASE					:Optional[int]=True			,    _SCREAMING_SNAKE_CASE					:Dict=0.1			,    _SCREAMING_SNAKE_CASE					:int=True			,    _SCREAMING_SNAKE_CASE					:List[Any]=True			,    _SCREAMING_SNAKE_CASE					:Optional[int]=5_0_2_5_6			,    _SCREAMING_SNAKE_CASE					:List[Any]=5_0_2_5_6			,    _SCREAMING_SNAKE_CASE					:Optional[int]=False			,    _SCREAMING_SNAKE_CASE					:Dict=False			,    **_SCREAMING_SNAKE_CASE					:List[Any]			,    )					->			List[str]:






								'''simple docstring'''






								UpperCamelCase_     :	Optional[Any]					     =     vocab_size
								UpperCamelCase_     :	Any					     =     n_positions
								UpperCamelCase_     :	Dict					     =     n_embd
								UpperCamelCase_     :	Dict					     =     n_layer
								UpperCamelCase_     :	Optional[Any]					     =     n_head
								UpperCamelCase_     :	int					     =     n_inner
								UpperCamelCase_     :	int					     =     activation_function
								UpperCamelCase_     :	str					     =     resid_pdrop
								UpperCamelCase_     :	Dict					     =     embd_pdrop
								UpperCamelCase_     :	Optional[Any]					     =     attn_pdrop
								UpperCamelCase_     :	List[Any]					     =     layer_norm_epsilon
								UpperCamelCase_     :	List[str]					     =     initializer_range
								UpperCamelCase_     :	str					     =     summary_type
								UpperCamelCase_     :	List[str]					     =     summary_use_proj
								UpperCamelCase_     :	Union[str, Any]					     =     summary_activation
								UpperCamelCase_     :	Union[str, Any]					     =     summary_first_dropout
								UpperCamelCase_     :	Tuple					     =     summary_proj_to_labels
								UpperCamelCase_     :	List[str]					     =     scale_attn_weights
								UpperCamelCase_     :	Optional[Any]					     =     use_cache
								UpperCamelCase_     :	Any					     =     scale_attn_by_inverse_layer_idx
								UpperCamelCase_     :	Union[str, Any]					     =     reorder_and_upcast_attn

								UpperCamelCase_     :	str					     =     bos_token_id
								UpperCamelCase_     :	Optional[int]					     =     eos_token_id

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







class     __snake_case			(    _a	):




		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			,    )					->			Dict:






								'''simple docstring'''






								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?
														UpperCamelCase_     :	Dict					     =     0




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






								'''simple docstring'''






								UpperCamelCase_     :	int					     =     OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}})
								if self.use_past:
														self.fill_with_past_key_values_(_SCREAMING_SNAKE_CASE			,    direction="""inputs""")
														UpperCamelCase_     :	List[Any]					     =     {0: """batch""", 1: """past_sequence + sequence"""}
								else:
														UpperCamelCase_     :	Optional[int]					     =     {0: """batch""", 1: """sequence"""}

								return common_inputs




		@property
		def 	_snake_case	(  self					:List[Any])					->			int:






								'''simple docstring'''






								return self._config.n_layer




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






								'''simple docstring'''






								return self._config.n_head




		def 	_snake_case	(  self					:Optional[int]			,    _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			,    )					->			Mapping[str, Any]:






								'''simple docstring'''






								UpperCamelCase_     :	Tuple					     =     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()
								UpperCamelCase_     :	str					     =     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_     ,UpperCamelCase_     :	str					     =     common_inputs["""input_ids"""].shape
																				# Not using the same length for past_key_values
																				UpperCamelCase_     :	List[str]					     =     seqlen + 2
																				UpperCamelCase_     :	int					     =     (
																				    batch,
																				    self.num_attention_heads,
																				    past_key_values_length,
																				    self._config.hidden_size // self.num_attention_heads,
																				)
																				UpperCamelCase_     :	Optional[int]					     =     [
																				    (torch.zeros(_SCREAMING_SNAKE_CASE), torch.zeros(_SCREAMING_SNAKE_CASE)) for _ in range(self.num_layers)
																				]

								UpperCamelCase_     :	Union[str, Any]					     =     common_inputs["""attention_mask"""]
								if self.use_past:
														UpperCamelCase_     :	Dict					     =     ordered_inputs["""attention_mask"""].dtype
														UpperCamelCase_     :	int					     =     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 	_snake_case	(  self					:Any)					->			int:






								'''simple docstring'''






								return 1_3






'''simple docstring'''


import unittest

import numpy as np

from transformers import is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
	import tensorflow as tf

	from transformers.activations_tf import get_tf_activation




@require_tf
class     __snake_case			(    unittest.TestCase	):




		def 	_snake_case	(  self					:Tuple)					->			List[Any]:






								'''simple docstring'''






								UpperCamelCase_     :	List[str]					     =     tf.constant([-1_0_0, -1.0, -0.1, 0, 0.1, 1.0, 100.0])
								UpperCamelCase_     :	Any					     =     get_tf_activation("""gelu""")
								UpperCamelCase_     :	List[Any]					     =     get_tf_activation("""gelu_10""")

								UpperCamelCase_     :	Optional[int]					     =     gelu(_SCREAMING_SNAKE_CASE)
								UpperCamelCase_     :	Dict					     =     geluaa(_SCREAMING_SNAKE_CASE)

								UpperCamelCase_     :	List[Any]					     =     tf.where(y_gelu_aa < 10.0			,    1.0			,    0.0)

								self.assertEqual(tf.math.reduce_max(_SCREAMING_SNAKE_CASE).numpy().item()			,    10.0)
								self.assertTrue(np.allclose(y_gelu * clipped_mask			,    y_gelu_aa * clipped_mask))





		def 	_snake_case	(  self					:Any)					->			Dict:






								'''simple docstring'''






								get_tf_activation("""gelu""")
								get_tf_activation("""gelu_10""")
								get_tf_activation("""gelu_fast""")
								get_tf_activation("""gelu_new""")
								get_tf_activation("""glu""")
								get_tf_activation("""mish""")
								get_tf_activation("""quick_gelu""")
								get_tf_activation("""relu""")
								get_tf_activation("""sigmoid""")
								get_tf_activation("""silu""")
								get_tf_activation("""swish""")
								get_tf_activation("""tanh""")
								with self.assertRaises(_SCREAMING_SNAKE_CASE):
														get_tf_activation("""bogus""")
								with self.assertRaises(_SCREAMING_SNAKE_CASE):
														get_tf_activation(_SCREAMING_SNAKE_CASE)




