





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       :				int           =			logging.get_logger(__name__)

_UpperCamelCase       :				int           =			{
    """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(		_a):

  """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    ,							UpperCAmelCase=5_02_57    ,							UpperCAmelCase=10_24    ,							UpperCAmelCase=7_68    ,							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_02_56    ,							UpperCAmelCase=5_02_56    ,							UpperCAmelCase=False    ,							UpperCAmelCase=False    ,							**UpperCAmelCase    ,							)->   Union[str, Any]:
     __A    =  vocab_size
     __A    =  n_positions
     __A    =  n_embd
     __A    =  n_layer
     __A    =  n_head
     __A    =  n_inner
     __A    =  activation_function
     __A    =  resid_pdrop
     __A    =  embd_pdrop
     __A    =  attn_pdrop
     __A    =  layer_norm_epsilon
     __A    =  initializer_range
     __A    =  summary_type
     __A    =  summary_use_proj
     __A    =  summary_activation
     __A    =  summary_first_dropout
     __A    =  summary_proj_to_labels
     __A    =  scale_attn_weights
     __A    =  use_cache
     __A    =  scale_attn_by_inverse_layer_idx
     __A    =  reorder_and_upcast_attn

     __A    =  bos_token_id
     __A    =  eos_token_id

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







class 		_lowerCAmelCase(		_a):

  """simple docstring"""







  def __init__(   self    ,							UpperCAmelCase    ,							UpperCAmelCase = "default"    ,							UpperCAmelCase = None    ,							UpperCAmelCase = False    ,							)->   str:
     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?
        __A    =  0

  @property
  def 						SCREAMING_SNAKE_CASE__			(   self      )->   Mapping[str, Mapping[int, str]]:
     __A    =  OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}}      )
     if self.use_past:
        self.fill_with_past_key_values_(UpperCAmelCase    ,							direction='''inputs'''      )
        __A    =  {0: '''batch''', 1: '''past_sequence + sequence'''}
     else:
        __A    =  {0: '''batch''', 1: '''sequence'''}

     return common_inputs

  @property
  def 						SCREAMING_SNAKE_CASE__			(   self      )->   int:
     return self._config.n_layer

  @property
  def 						SCREAMING_SNAKE_CASE__			(   self      )->   int:
     return self._config.n_head

  def 						SCREAMING_SNAKE_CASE__			(   self    ,							UpperCAmelCase    ,							UpperCAmelCase = -1    ,							UpperCAmelCase = -1    ,							UpperCAmelCase = False    ,							UpperCAmelCase = None    ,							)->   Mapping[str, Any]:
     __A    =  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()
     __A    =  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

           __A   ,		__A    =  common_inputs['''input_ids'''].shape
           # Not using the same length for past_key_values
           __A    =  seqlen + 2
           __A    =  (
               batch,
               self.num_attention_heads,
               past_key_values_length,
               self._config.hidden_size // self.num_attention_heads,
           )
           __A    =  [
               (torch.zeros(UpperCAmelCase      ), torch.zeros(UpperCAmelCase      )) for _ in range(self.num_layers      )
           ]

     __A    =  common_inputs['''attention_mask''']
     if self.use_past:
        __A    =  ordered_inputs['''attention_mask'''].dtype
        __A    =  torch.cat(
            [ordered_inputs['''attention_mask'''], torch.ones(UpperCAmelCase    ,							UpperCAmelCase    ,							dtype=UpperCAmelCase      )]    ,							dim=1      )

     return ordered_inputs





  @property
  def 						SCREAMING_SNAKE_CASE__			(   self      )->   int:
     return 13








class 		_lowerCAmelCase:

  """simple docstring"""







  def __init__(   self      )->   None:
     # numbermap keys are the number and its values are lists of indexes sorted
     # in ascending order
     __A    =  {}
     # indexmap keys are an index and it's values are the number at that index
     __A    =  {}

  def 						SCREAMING_SNAKE_CASE__			(   self    ,							UpperCAmelCase    ,							UpperCAmelCase      )->   list[int]:
     if isinstance(UpperCAmelCase    ,							(range, str)      ):
        __A    =  list(UpperCAmelCase      )
     elif not isinstance(UpperCAmelCase    ,							UpperCAmelCase      ):
        raise TypeError(
            '''binary_search_delete() only accepts either a list, range or str'''      )

     __A    =  0
     __A    =  len(UpperCAmelCase      ) - 1

     while low <= high:
        __A    =  (low + high) // 2
        if array[mid] == item:
           array.pop(UpperCAmelCase      )
           return array
        elif array[mid] < item:
           __A    =  mid + 1
        else:
           __A    =  mid - 1
     raise ValueError(
         '''Either the item is not in the array or the array was unsorted'''      )

  def 						SCREAMING_SNAKE_CASE__			(   self    ,							UpperCAmelCase    ,							UpperCAmelCase      )->   list[int]:
     if isinstance(UpperCAmelCase    ,							(range, str)      ):
        __A    =  list(UpperCAmelCase      )
     elif not isinstance(UpperCAmelCase    ,							UpperCAmelCase      ):
        raise TypeError(
            '''binary_search_insert() only accepts either a list, range or str'''      )

     __A    =  0
     __A    =  len(UpperCAmelCase      ) - 1

     while low <= high:
        __A    =  (low + high) // 2
        if array[mid] == index:
           # If the item already exists in the array,
           # insert it after the existing item
           array.insert(mid + 1    ,							UpperCAmelCase      )
           return array
        elif array[mid] < index:
           __A    =  mid + 1
        else:
           __A    =  mid - 1

        # If the item doesn't exist in the array, insert it at the appropriate position
     array.insert(UpperCAmelCase    ,							UpperCAmelCase      )
     return array

  def 						SCREAMING_SNAKE_CASE__			(   self    ,							UpperCAmelCase    ,							UpperCAmelCase      )->   None:
     # Remove previous index
     if index in self.indexmap:
        __A    =  self.indexmap[index]
        if len(self.numbermap[n]      ) == 1:
           del self.numbermap[n]
        else:
           __A    =  self.binary_search_delete(self.numbermap[n]    ,							UpperCAmelCase      )

        # Set new index
     __A    =  number

     # Number not seen before or empty so insert number value
     if number not in self.numbermap:
        __A    =  [index]

     # Here we need to perform a binary search insertion in order to insert
     # The item in the correct place
     else:
        __A    =  self.binary_search_insert(
            self.numbermap[number]    ,							UpperCAmelCase      )





  def 						SCREAMING_SNAKE_CASE__			(   self    ,							UpperCAmelCase      )->   int:
     return self.numbermap.get(UpperCAmelCase    ,							[-1]      )[0]


if __name__ == "__main__":
    import doctest

    doctest.testmod()

