






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

SCREAMING_SNAKE_CASE__		:       List[str]												=							{
    """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 							UpperCAmelCase_     (		__lowerCamelCase		):
     __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  ,	_lowerCAmelCase=50257  ,	_lowerCAmelCase=1024  ,	_lowerCAmelCase=768  ,	_lowerCAmelCase=12  ,	_lowerCAmelCase=12  ,	_lowerCAmelCase=None  ,	_lowerCAmelCase="gelu_new"  ,	_lowerCAmelCase=0.1  ,	_lowerCAmelCase=0.1  ,	_lowerCAmelCase=0.1  ,	_lowerCAmelCase=1e-5  ,	_lowerCAmelCase=0.0_2  ,	_lowerCAmelCase="cls_index"  ,	_lowerCAmelCase=True  ,	_lowerCAmelCase=None  ,	_lowerCAmelCase=True  ,	_lowerCAmelCase=0.1  ,	_lowerCAmelCase=True  ,	_lowerCAmelCase=True  ,	_lowerCAmelCase=50256  ,	_lowerCAmelCase=50256  ,	_lowerCAmelCase=False  ,	_lowerCAmelCase=False  ,	**_lowerCAmelCase  ,	):
          UpperCAmelCase__			:     str										= vocab_size
          UpperCAmelCase__			:     Any										= n_positions
          UpperCAmelCase__			:     Optional[Any]										= n_embd
          UpperCAmelCase__			:     Tuple										= n_layer
          UpperCAmelCase__			:     Dict										= n_head
          UpperCAmelCase__			:     Tuple										= n_inner
          UpperCAmelCase__			:     str										= activation_function
          UpperCAmelCase__			:     Optional[Any]										= resid_pdrop
          UpperCAmelCase__			:     Tuple										= embd_pdrop
          UpperCAmelCase__			:     List[str]										= attn_pdrop
          UpperCAmelCase__			:     Optional[Any]										= layer_norm_epsilon
          UpperCAmelCase__			:     str										= initializer_range
          UpperCAmelCase__			:     Any										= summary_type
          UpperCAmelCase__			:     Optional[Any]										= summary_use_proj
          UpperCAmelCase__			:     Dict										= summary_activation
          UpperCAmelCase__			:     List[str]										= summary_first_dropout
          UpperCAmelCase__			:     List[str]										= summary_proj_to_labels
          UpperCAmelCase__			:     Optional[int]										= scale_attn_weights
          UpperCAmelCase__			:     Optional[Any]										= use_cache
          UpperCAmelCase__			:     List[str]										= scale_attn_by_inverse_layer_idx
          UpperCAmelCase__			:     str										= reorder_and_upcast_attn

          UpperCAmelCase__			:     int										= bos_token_id
          UpperCAmelCase__			:     Any										= eos_token_id

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







class 							UpperCAmelCase_     (		__lowerCamelCase		):




     def __init__(							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase = "default"  ,	_lowerCAmelCase = None  ,	_lowerCAmelCase = False  ,	):
          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?
               UpperCAmelCase__			:     Optional[Any]										= 0




     @property
     def      __UpperCAmelCase     (							self		):
          UpperCAmelCase__			:     Dict										= OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}}		)
          if self.use_past:
               self.fill_with_past_key_values_(_lowerCAmelCase  ,	direction="""inputs"""		)
               UpperCAmelCase__			:     Any										= {0: """batch""", 1: """past_sequence + sequence"""}
          else:
               UpperCAmelCase__			:     str										= {0: """batch""", 1: """sequence"""}

          return common_inputs




     @property
     def      __UpperCAmelCase     (							self		):
          return self._config.n_layer




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




     def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase = -1  ,	_lowerCAmelCase = -1  ,	_lowerCAmelCase = False  ,	_lowerCAmelCase = None  ,	):
          UpperCAmelCase__			:     Union[str, Any]										= 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()
          UpperCAmelCase__			:     Optional[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

                    UpperCAmelCase__			:     Union[str, Any]										= common_inputs["""input_ids"""].shape
                    # Not using the same length for past_key_values
                    UpperCAmelCase__			:     Dict										= seqlen + 2
                    UpperCAmelCase__			:     Tuple										= (
                        batch,
                        self.num_attention_heads,
                        past_key_values_length,
                        self._config.hidden_size // self.num_attention_heads,
                    )
                    UpperCAmelCase__			:     List[str]										= [
                        (torch.zeros(_lowerCAmelCase		), torch.zeros(_lowerCAmelCase		)) for _ in range(self.num_layers		)
                    ]

          UpperCAmelCase__			:     Optional[int]										= common_inputs["""attention_mask"""]
          if self.use_past:
               UpperCAmelCase__			:     List[Any]										= ordered_inputs["""attention_mask"""].dtype
               UpperCAmelCase__			:     Union[str, Any]										= torch.cat(
                   [ordered_inputs["""attention_mask"""], torch.ones(_lowerCAmelCase  ,	_lowerCAmelCase  ,	dtype=_lowerCAmelCase		)]  ,	dim=1		)

          return ordered_inputs




     @property
     def      __UpperCAmelCase     (							self		):
          return 13














from __future__ import annotations







class 							UpperCAmelCase_     :




     def __init__(							self  ,	_lowerCAmelCase		):
          UpperCAmelCase__			:     List[Any]										= value
          UpperCAmelCase__			:     Node | None										= None
          UpperCAmelCase__			:     Node | None										= None







class 							UpperCAmelCase_     :
     __lowerCamelCase										=     42




     def __init__(							self		):
          UpperCAmelCase__			:     Any										= 0




     def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase		):
          if node is None:
               return

          if path_sum == self.target:
               self.paths += 1

          if node.left:
               self.depth_first_search(node.left  ,	path_sum + node.left.value		)
          if node.right:
               self.depth_first_search(node.right  ,	path_sum + node.right.value		)




     def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase = None		):
          if node is None:
               return 0
          if target is not None:
               UpperCAmelCase__			:     List[Any]										= target

          self.depth_first_search(_lowerCAmelCase  ,	node.value		)
          self.path_sum(node.left		)
          self.path_sum(node.right		)

          return self.paths


if __name__ == "__main__":
      import doctest

      doctest.testmod()






