


"""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       lowerCamelCase_		(      lowercase ):





				"""simple docstring"""







				_lowerCAmelCase   :   Union[str, Any]   				=       "gpt2"
				_lowerCAmelCase   :   Optional[Any]   				=       ["past_key_values"]
				_lowerCAmelCase   :   Optional[Any]   				=       {
				    "hidden_size": "n_embd",
				    "max_position_embeddings": "n_positions",
				    "num_attention_heads": "n_head",
				    "num_hidden_layers": "n_layer",
				}



				def __init__(    self							,		UpperCAmelCase__=5_0257							,		UpperCAmelCase__=1024							,		UpperCAmelCase__=768							,		UpperCAmelCase__=12							,		UpperCAmelCase__=12							,		UpperCAmelCase__=None							,		UpperCAmelCase__="gelu_new"							,		UpperCAmelCase__=0.1							,		UpperCAmelCase__=0.1							,		UpperCAmelCase__=0.1							,		UpperCAmelCase__=1e-5							,		UpperCAmelCase__=0.02							,		UpperCAmelCase__="cls_index"							,		UpperCAmelCase__=True							,		UpperCAmelCase__=None							,		UpperCAmelCase__=True							,		UpperCAmelCase__=0.1							,		UpperCAmelCase__=True							,		UpperCAmelCase__=True							,		UpperCAmelCase__=5_0256							,		UpperCAmelCase__=5_0256							,		UpperCAmelCase__=False							,		UpperCAmelCase__=False							,		**UpperCAmelCase__							,		):
								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       lowerCamelCase_		(      lowercase ):





				"""simple docstring"""







				def __init__(    self							,		UpperCAmelCase__							,		UpperCAmelCase__ = "default"							,		UpperCAmelCase__ = None							,		UpperCAmelCase__ = False							,		):
								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 	lowerCAmelCase__		(    self  ):
								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 	lowerCAmelCase__		(    self  ):
								return self._config.n_layer



				@property
				def 	lowerCAmelCase__		(    self  ):
								return self._config.n_head



				def 	lowerCAmelCase__		(    self							,		UpperCAmelCase__							,		UpperCAmelCase__ = -1							,		UpperCAmelCase__ = -1							,		UpperCAmelCase__ = False							,		UpperCAmelCase__ = None							,		):
								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 	lowerCAmelCase__		(    self  ):
								return 13




"""simple docstring"""






def __lowercase  (   lowerCamelCase_	:   str	):
				SCREAMING_SNAKE_CASE__  				=					str(lowerCamelCase_	).strip()
				if not oct_string:
								raise ValueError("Empty string was passed to the function"	)
				SCREAMING_SNAKE_CASE__  				=					oct_string[0] == "-"
				if is_negative:
								SCREAMING_SNAKE_CASE__  				=					oct_string[1:]
				if not oct_string.isdigit() or not all(0 <= int(lowerCamelCase_	) <= 7 for char in oct_string	):
								raise ValueError("Non-octal value was passed to the function"	)
				SCREAMING_SNAKE_CASE__  				=					0
				for char in oct_string:
								SCREAMING_SNAKE_CASE__  				=					8 * decimal_number + int(lowerCamelCase_	)
				if is_negative:
								SCREAMING_SNAKE_CASE__  				=					-decimal_number
				return decimal_number


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

	testmod()
