


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


A__:   Optional[int]    =	logging.get_logger(__name__)

A__:   Optional[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 A__	(						UpperCAmelCase__				):
      __UpperCamelCase	:  List[Any]     	=							"gpt2"
      __UpperCamelCase	:  Tuple     	=							["past_key_values"]
      __UpperCamelCase	:  List[Any]     	=							{
          "hidden_size": "n_embd",
          "max_position_embeddings": "n_positions",
          "num_attention_heads": "n_head",
          "num_hidden_layers": "n_layer",
      }






      def __init__(			self      :List[str]					,						SCREAMING_SNAKE_CASE      :Optional[int]=5_0_2_5_7					,						SCREAMING_SNAKE_CASE      :Tuple=1_0_2_4					,						SCREAMING_SNAKE_CASE      :Union[str, Any]=7_6_8					,						SCREAMING_SNAKE_CASE      :Tuple=1_2					,						SCREAMING_SNAKE_CASE      :Optional[Any]=1_2					,						SCREAMING_SNAKE_CASE      :Tuple=None					,						SCREAMING_SNAKE_CASE      :Optional[Any]="gelu_new"					,						SCREAMING_SNAKE_CASE      :Dict=0.1					,						SCREAMING_SNAKE_CASE      :List[Any]=0.1					,						SCREAMING_SNAKE_CASE      :List[Any]=0.1					,						SCREAMING_SNAKE_CASE      :str=1e-5					,						SCREAMING_SNAKE_CASE      :int=0.02					,						SCREAMING_SNAKE_CASE      :List[Any]="cls_index"					,						SCREAMING_SNAKE_CASE      :Optional[int]=True					,						SCREAMING_SNAKE_CASE      :Tuple=None					,						SCREAMING_SNAKE_CASE      :List[str]=True					,						SCREAMING_SNAKE_CASE      :Optional[int]=0.1					,						SCREAMING_SNAKE_CASE      :int=True					,						SCREAMING_SNAKE_CASE      :List[Any]=True					,						SCREAMING_SNAKE_CASE      :List[str]=5_0_2_5_6					,						SCREAMING_SNAKE_CASE      :Any=5_0_2_5_6					,						SCREAMING_SNAKE_CASE      :Tuple=False					,						SCREAMING_SNAKE_CASE      :List[Any]=False					,						**SCREAMING_SNAKE_CASE      :Any					,						)	-> List[str]:




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

             _a   :			List[Any]					 =bos_token_id
             _a   :			Optional[int]					 =eos_token_id

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







class A__	(						UpperCAmelCase__				):






      def __init__(			self      :Tuple					,						SCREAMING_SNAKE_CASE      :PretrainedConfig					,						SCREAMING_SNAKE_CASE      :str = "default"					,						SCREAMING_SNAKE_CASE      :List[PatchingSpec] = None					,						SCREAMING_SNAKE_CASE      :bool = False					,						)	-> Tuple:




             '''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?
                    _a   :			str					 =0






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




             '''simple docstring'''
             _a   :			Optional[int]					 =OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}}     )
             if self.use_past:
                    self.fill_with_past_key_values_(SCREAMING_SNAKE_CASE					,						direction="""inputs"""     )
                    _a   :			str					 ={0: """batch""", 1: """past_sequence + sequence"""}
             else:
                    _a   :			Any					 ={0: """batch""", 1: """sequence"""}

             return common_inputs






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




             '''simple docstring'''
             return self._config.n_layer






      @property
      def __UpperCAmelCase	(			self      :str     )	-> int:




             '''simple docstring'''
             return self._config.n_head






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

             _a   :			Optional[int]					 =common_inputs["""attention_mask"""]
             if self.use_past:
                    _a   :			str					 =ordered_inputs["""attention_mask"""].dtype
                    _a   :			Optional[Any]					 =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 __UpperCAmelCase	(			self      :List[Any]     )	-> int:




             '''simple docstring'''
             return 1_3






'''simple docstring'''







from typing import Dict, Optional, Tuple

import torch


def      SCREAMING_SNAKE_CASE_ (  _UpperCAmelCase     :     torch.Tensor  )  ->      torch.Tensor:
       _a   :			Tuple					 =boundaries[1] - boundaries[0]
       _a   :			Dict					 =boundaries + step / 2
       _a   :			List[Any]					 =torch.cat([bin_centers, (bin_centers[-1] + step).unsqueeze(-1  )]   ,dim=0  )
       return bin_centers


def      SCREAMING_SNAKE_CASE_ (  _UpperCAmelCase     :     torch.Tensor   ,_UpperCAmelCase     :     torch.Tensor   ,)  ->      Tuple[torch.Tensor, torch.Tensor]:
       _a   :			Any					 =_calculate_bin_centers(_UpperCAmelCase  )
       return (
           torch.sum(aligned_distance_error_probs * bin_centers   ,dim=-1  ),
           bin_centers[-1],
       )


def      SCREAMING_SNAKE_CASE_ (  _UpperCAmelCase     :     torch.Tensor   ,_UpperCAmelCase     :     int = 31   ,_UpperCAmelCase     :     int = 64   ,**_UpperCAmelCase     :     Dict   ,)  ->      Dict[str, torch.Tensor]:
       _a   :			Dict					 =torch.linspace(0   ,_UpperCAmelCase   ,steps=(no_bins - 1)   ,device=logits.device  )

       _a   :			int					 =torch.nn.functional.softmax(_UpperCAmelCase   ,dim=-1  )
       _a ,   _a   :			List[Any]					 =_calculate_expected_aligned_error(
           alignment_confidence_breaks=_UpperCAmelCase   ,aligned_distance_error_probs=_UpperCAmelCase   ,)

       return {
           "aligned_confidence_probs": aligned_confidence_probs,
           "predicted_aligned_error": predicted_aligned_error,
           "max_predicted_aligned_error": max_predicted_aligned_error,
       }


def      SCREAMING_SNAKE_CASE_ (  _UpperCAmelCase     :     torch.Tensor   ,_UpperCAmelCase     :     Optional[torch.Tensor] = None   ,_UpperCAmelCase     :     int = 31   ,_UpperCAmelCase     :     int = 64   ,_UpperCAmelCase     :     float = 1e-8   ,**_UpperCAmelCase     :     Optional[Any]   ,)  ->      torch.Tensor:
       if residue_weights is None:
              _a   :			Dict					 =logits.new_ones(logits.shape[-2]  )

       _a   :			str					 =torch.linspace(0   ,_UpperCAmelCase   ,steps=(no_bins - 1)   ,device=logits.device  )

       _a   :			Optional[int]					 =_calculate_bin_centers(_UpperCAmelCase  )
       torch.sum(_UpperCAmelCase  )
       _a   :			List[str]					 =logits.shape[-2]
       _a   :			List[Any]					 =max(_UpperCAmelCase   ,19  )

       _a   :			Union[str, Any]					 =1.2_4 * (clipped_n - 15) ** (1.0 / 3) - 1.8

       _a   :			Tuple					 =torch.nn.functional.softmax(_UpperCAmelCase   ,dim=-1  )

       _a   :			Dict					 =1.0 / (1 + (bin_centers**2) / (da**2))
       _a   :			int					 =torch.sum(probs * tm_per_bin   ,dim=-1  )

       _a   :			List[Any]					 =residue_weights / (eps + residue_weights.sum())
       _a   :			List[str]					 =torch.sum(predicted_tm_term * normed_residue_mask   ,dim=-1  )

       _a   :			int					 =per_alignment * residue_weights

       _a   :			List[Any]					 =(weighted == torch.max(_UpperCAmelCase  )).nonzero()[0]
       return per_alignment[tuple(_UpperCAmelCase  )]


