



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


_UpperCamelCase							    =						logging.get_logger(__name__)

_UpperCamelCase							    =						{
    '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_   (    _lowerCAmelCase					):
  """simple docstring"""




  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				:     Tuple      ,     _a				:     Dict=5_0257      ,     _a				:     Tuple=1024      ,     _a				:     str=768      ,     _a				:     str=12      ,     _a				:     List[Any]=12      ,     _a				:     str=None      ,     _a				:     Dict="gelu_new"      ,     _a				:     Any=0.1      ,     _a				:     Union[str, Any]=0.1      ,     _a				:     Union[str, Any]=0.1      ,     _a				:     Any=1e-5      ,     _a				:     List[str]=0.02      ,     _a				:     int="cls_index"      ,     _a				:     str=True      ,     _a				:     Tuple=None      ,     _a				:     int=True      ,     _a				:     int=0.1      ,     _a				:     Dict=True      ,     _a				:     List[Any]=True      ,     _a				:     List[Any]=5_0256      ,     _a				:     List[Any]=5_0256      ,     _a				:     Any=False      ,     _a				:     Dict=False      ,     **_a				:     Optional[int]      ,     ) -> str:
    __lowerCamelCase	:	str   	=			vocab_size
    __lowerCamelCase	:	Any   	=			n_positions
    __lowerCamelCase	:	List[Any]   	=			n_embd
    __lowerCamelCase	:	Union[str, Any]   	=			n_layer
    __lowerCamelCase	:	int   	=			n_head
    __lowerCamelCase	:	Optional[Any]   	=			n_inner
    __lowerCamelCase	:	Union[str, Any]   	=			activation_function
    __lowerCamelCase	:	Tuple   	=			resid_pdrop
    __lowerCamelCase	:	Optional[Any]   	=			embd_pdrop
    __lowerCamelCase	:	Optional[Any]   	=			attn_pdrop
    __lowerCamelCase	:	Optional[Any]   	=			layer_norm_epsilon
    __lowerCamelCase	:	str   	=			initializer_range
    __lowerCamelCase	:	Dict   	=			summary_type
    __lowerCamelCase	:	Optional[int]   	=			summary_use_proj
    __lowerCamelCase	:	Tuple   	=			summary_activation
    __lowerCamelCase	:	List[Any]   	=			summary_first_dropout
    __lowerCamelCase	:	Optional[Any]   	=			summary_proj_to_labels
    __lowerCamelCase	:	Any   	=			scale_attn_weights
    __lowerCamelCase	:	Union[str, Any]   	=			use_cache
    __lowerCamelCase	:	Union[str, Any]   	=			scale_attn_by_inverse_layer_idx
    __lowerCamelCase	:	int   	=			reorder_and_upcast_attn

    __lowerCamelCase	:	Tuple   	=			bos_token_id
    __lowerCamelCase	:	Any   	=			eos_token_id

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






class  lowerCamelCase_   (    _lowerCAmelCase					):
  """simple docstring"""




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




  @property
  def 						_lowercase					(			self				:     Optional[Any]       ) -> Optional[int]:
    __lowerCamelCase	:	Optional[Any]   	=			OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}}       )
    if self.use_past:
      self.fill_with_past_key_values_(_lowerCamelCase      ,     direction='inputs'       )
      __lowerCamelCase	:	Any   	=			{0: 'batch', 1: 'past_sequence + sequence'}
    else:
      __lowerCamelCase	:	int   	=			{0: 'batch', 1: 'sequence'}

    return common_inputs




  @property
  def 						_lowercase					(			self				:     Dict       ) -> Tuple:
    return self._config.n_layer




  @property
  def 						_lowercase					(			self				:     Tuple       ) -> Dict:
    return self._config.n_head




  def 						_lowercase					(			self				:     Optional[Any]      ,     _a				:     PreTrainedTokenizer      ,     _a				:     int = -1      ,     _a				:     int = -1      ,     _a				:     bool = False      ,     _a				:     Optional[TensorType] = None      ,     ) -> Dict:
    __lowerCamelCase	:	Dict   	=			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()
    __lowerCamelCase	:	Any   	=			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

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

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

    return ordered_inputs




  @property
  def 						_lowercase					(			self				:     Optional[Any]       ) -> List[Any]:
    return 13










'''simple docstring'''





import math
from timeit import timeit

def 	a_					(							_lowerCAmelCase	)				->				int:
  __lowerCamelCase	:	int   	=			0
  __lowerCamelCase	:	Optional[int]   	=			abs(_lowerCAmelCase	)
  while True:
    __lowerCamelCase	:	List[str]   	=			n // 10
    digits += 1
    if n == 0:
      break
  return digits

def 	a_					(							_lowerCAmelCase	)				->				int:
  return 1 if n == 0 else math.floor(math.log(abs(_lowerCAmelCase	)	,10	) + 1	)

def 	a_					(							_lowerCAmelCase	)				->				int:
  return len(str(abs(_lowerCAmelCase	)	)	)

def 	a_					(							)				->				None:
  from collections.abc import Callable

  def benchmark_a_function(_lowerCAmelCase	,_lowerCAmelCase	) -> None:
    __lowerCamelCase	:	Any   	=			F'{func.__name__}({value})'
    __lowerCamelCase	:	int   	=			timeit(F'__main__.{call}'	,setup='import __main__'	)
    print(F'{call}: {func(_lowerCAmelCase	)} -- {timing} seconds'	)

  for value in (262144, 1125899906842624, 1267650600228229401496703205376):
    for func in (num_digits, num_digits_fast, num_digits_faster):
      benchmark_a_function(_lowerCAmelCase	,_lowerCAmelCase	)
    print()


if __name__ == "__main__":
   import doctest

   doctest.testmod()
   benchmark()






