from typing import Dict, Optional, Tuple

import torch






def 					SCREAMING_SNAKE_CASE_			(		lowerCAmelCase_: torch.Tensor     ):
      snake_case_     :			int       =       boundaries[1] - boundaries[0]
      snake_case_     :			Optional[int]       =       boundaries + step / 2
      snake_case_     :			Union[str, Any]       =       torch.cat([bin_centers, (bin_centers[-1] + step).unsqueeze(-1     )] ,	dim=0     )
      return bin_centers






def 					SCREAMING_SNAKE_CASE_			(		lowerCAmelCase_: torch.Tensor ,	lowerCAmelCase_: torch.Tensor ,	):
      snake_case_     :			List[str]       =       _calculate_bin_centers(lowerCAmelCase_     )
      return (
          torch.sum(aligned_distance_error_probs * bin_centers ,	dim=-1     ),
          bin_centers[-1],
      )






def 					SCREAMING_SNAKE_CASE_			(		lowerCAmelCase_: torch.Tensor ,	lowerCAmelCase_: int = 3_1 ,	lowerCAmelCase_: int = 6_4 ,	**lowerCAmelCase_: Union[str, Any] ,	):
      snake_case_     :			Optional[Any]       =       torch.linspace(0 ,	lowerCAmelCase_ ,	steps=(no_bins - 1) ,	device=logits.device     )

      snake_case_     :			Optional[Any]       =       torch.nn.functional.softmax(lowerCAmelCase_ ,	dim=-1     )
      snake_case_  ,snake_case_     :			List[str]       =       _calculate_expected_aligned_error(
          alignment_confidence_breaks=lowerCAmelCase_ ,	aligned_distance_error_probs=lowerCAmelCase_ ,	)

      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_			(		lowerCAmelCase_: torch.Tensor ,	lowerCAmelCase_: Optional[torch.Tensor] = None ,	lowerCAmelCase_: int = 3_1 ,	lowerCAmelCase_: int = 6_4 ,	lowerCAmelCase_: float = 1e-8 ,	**lowerCAmelCase_: List[str] ,	):
      if residue_weights is None:
            snake_case_     :			str       =       logits.new_ones(logits.shape[-2]     )

      snake_case_     :			int       =       torch.linspace(0 ,	lowerCAmelCase_ ,	steps=(no_bins - 1) ,	device=logits.device     )

      snake_case_     :			Dict       =       _calculate_bin_centers(lowerCAmelCase_     )
      torch.sum(lowerCAmelCase_     )
      snake_case_     :			int       =       logits.shape[-2]
      snake_case_     :			List[str]       =       max(lowerCAmelCase_ ,	1_9     )

      snake_case_     :			Optional[int]       =       1.2_4 * (clipped_n - 1_5) ** (1.0 / 3) - 1.8

      snake_case_     :			List[str]       =       torch.nn.functional.softmax(lowerCAmelCase_ ,	dim=-1     )

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

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

      snake_case_     :			int       =       per_alignment * residue_weights

      snake_case_     :			List[str]       =       (weighted == torch.max(lowerCAmelCase_     )).nonzero()[0]
      return per_alignment[tuple(lowerCAmelCase_     )]




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


UpperCAmelCase							=       {
    "configuration_nystromformer": ["NYSTROMFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "NystromformerConfig"],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCAmelCase							=       [
         "NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST",
         "NystromformerForMaskedLM",
         "NystromformerForMultipleChoice",
         "NystromformerForQuestionAnswering",
         "NystromformerForSequenceClassification",
         "NystromformerForTokenClassification",
         "NystromformerLayer",
         "NystromformerModel",
         "NystromformerPreTrainedModel",
     ]


if TYPE_CHECKING:
     from .configuration_nystromformer import NYSTROMFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, NystromformerConfig

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_nystromformer import (
              NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
              NystromformerForMaskedLM,
              NystromformerForMultipleChoice,
              NystromformerForQuestionAnswering,
              NystromformerForSequenceClassification,
              NystromformerForTokenClassification,
              NystromformerLayer,
              NystromformerModel,
              NystromformerPreTrainedModel,
          )


else:
     import sys

     UpperCAmelCase							=       _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



