





from typing import Dict, Tuple, overload

import torch
import torch.types
from torch import nn

from . import residue_constants as rc
from .rigid_utils import Rigid, Rotation
from .tensor_utils import batched_gather







@overload
def      A__				(       SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__)  ->       torch.Tensor:
  ...







@overload
def      A__				(       SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__)  ->       Tuple[torch.Tensor, torch.Tensor]:
  ...







def      A__				(       SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__)  ->       Dict:
  __snake_case:		str					    =		aatype == rc.restype_order["""G"""]
  __snake_case:		Dict					    =		rc.atom_order["""CA"""]
  __snake_case:		List[Any]					    =		rc.atom_order["""CB"""]
  __snake_case:		List[str]					    =		torch.where(
      is_gly[..., None].expand(*((-1,) * len(is_gly.shape))   ,   3)   ,   all_atom_positions[..., ca_idx, :]   ,   all_atom_positions[..., cb_idx, :]   ,   )

  if all_atom_masks is not None:
    __snake_case:		Optional[int]					    =		torch.where(
        SCREAMING_SNAKE_CASE__   ,   all_atom_masks[..., ca_idx]   ,   all_atom_masks[..., cb_idx]   ,   )
    return pseudo_beta, pseudo_beta_mask
  else:
    return pseudo_beta







def      A__				(       SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__)  ->       torch.Tensor:
  __snake_case:		Optional[Any]					    =		batched_gather(
      SCREAMING_SNAKE_CASE__   ,   batch["""residx_atom37_to_atom14"""]   ,   dim=-2   ,   no_batch_dims=len(atomaa.shape[:-2])   ,   )

  __snake_case:		Tuple					    =		atomaa_data * batch["""atom37_atom_exists"""][..., None]

  return atomaa_data







def      A__				(       SCREAMING_SNAKE_CASE__)  ->       torch.Tensor:
  __snake_case:		List[str]					    =		template_feats["""template_aatype"""]
  __snake_case:		Optional[Any]					    =		template_feats["""template_torsion_angles_sin_cos"""]
  __snake_case:		int					    =		template_feats["""template_alt_torsion_angles_sin_cos"""]
  __snake_case:		Union[str, Any]					    =		template_feats["""template_torsion_angles_mask"""]
  __snake_case:		List[Any]					    =		torch.cat(
      [
          nn.functional.one_hot(SCREAMING_SNAKE_CASE__   ,   22),
          torsion_angles_sin_cos.reshape(*torsion_angles_sin_cos.shape[:-2]   ,   14),
          alt_torsion_angles_sin_cos.reshape(*alt_torsion_angles_sin_cos.shape[:-2]   ,   14),
          torsion_angles_mask,
      ]   ,   dim=-1   ,   )

  return template_angle_feat







def      A__				(       SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__ = False   ,   SCREAMING_SNAKE_CASE__ = 1e-20   ,   SCREAMING_SNAKE_CASE__ = 1e8   ,   )  ->       torch.Tensor:
  __snake_case:		str					    =		batch["""template_pseudo_beta_mask"""]
  __snake_case:		Union[str, Any]					    =		template_mask[..., None] * template_mask[..., None, :]

  # Compute distogram (this seems to differ slightly from Alg. 5)
  __snake_case:		List[Any]					    =		batch["""template_pseudo_beta"""]
  __snake_case:		Dict					    =		torch.sum((tpb[..., None, :] - tpb[..., None, :, :]) ** 2   ,   dim=-1   ,   keepdim=SCREAMING_SNAKE_CASE__)
  __snake_case:		Optional[Any]					    =		torch.linspace(SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   device=tpb.device) ** 2
  __snake_case:		int					    =		torch.cat([lower[1:], lower.new_tensor([inf])]   ,   dim=-1)
  __snake_case:		Optional[int]					    =		((dgram > lower) * (dgram < upper)).type(dgram.dtype)

  __snake_case:		str					    =		[dgram, template_mask_ad[..., None]]

  __snake_case:		torch.LongTensor					    =		nn.functional.one_hot(
      batch["""template_aatype"""]   ,   rc.restype_num + 2   ,   )

  __snake_case:		Any					    =		batch["""template_aatype"""].shape[-1]
  to_concat.append(aatype_one_hot[..., None, :, :].expand(*aatype_one_hot.shape[:-2]   ,   SCREAMING_SNAKE_CASE__   ,   -1   ,   -1))
  to_concat.append(aatype_one_hot[..., None, :].expand(*aatype_one_hot.shape[:-2]   ,   -1   ,   SCREAMING_SNAKE_CASE__   ,   -1))

  __snake_case    ,  __snake_case    ,  __snake_case:		str					    =		[rc.atom_order[a] for a in ["""N""", """CA""", """C"""]]
  __snake_case:		str					    =		Rigid.make_transform_from_reference(
      n_xyz=batch["""template_all_atom_positions"""][..., n, :]   ,   ca_xyz=batch["""template_all_atom_positions"""][..., ca, :]   ,   c_xyz=batch["""template_all_atom_positions"""][..., c, :]   ,   eps=SCREAMING_SNAKE_CASE__   ,   )
  __snake_case:		str					    =		rigids.get_trans()[..., None, :, :]
  __snake_case:		Union[str, Any]					    =		rigids[..., None].invert_apply(SCREAMING_SNAKE_CASE__)

  __snake_case:		Any					    =		torch.rsqrt(eps + torch.sum(rigid_vec**2   ,   dim=-1))

  __snake_case:		Dict					    =		batch["""template_all_atom_mask"""]
  __snake_case:		Optional[Any]					    =		t_aa_masks[..., n] * t_aa_masks[..., ca] * t_aa_masks[..., c]
  __snake_case:		Optional[Any]					    =		template_mask[..., None] * template_mask[..., None, :]

  __snake_case:		Tuple					    =		inv_distance_scalar * template_mask_ad
  __snake_case:		Optional[int]					    =		rigid_vec * inv_distance_scalar[..., None]

  if not use_unit_vector:
    __snake_case:		Tuple					    =		unit_vector * 0.0

  to_concat.extend(torch.unbind(unit_vector[..., None, :]   ,   dim=-1))
  to_concat.append(template_mask_ad[..., None])

  __snake_case:		Optional[Any]					    =		torch.cat(SCREAMING_SNAKE_CASE__   ,   dim=-1)
  __snake_case:		str					    =		act * template_mask_ad[..., None]

  return act







def      A__				(       SCREAMING_SNAKE_CASE__)  ->       torch.Tensor:
  __snake_case:		torch.LongTensor					    =		nn.functional.one_hot(batch["""extra_msa"""]   ,   23)
  __snake_case:		Any					    =		[
      msa_ahot,
      batch["""extra_has_deletion"""].unsqueeze(-1),
      batch["""extra_deletion_value"""].unsqueeze(-1),
  ]
  return torch.cat(SCREAMING_SNAKE_CASE__   ,   dim=-1)







def      A__				(       SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   )  ->       Rigid:
  # [*, N, 8, 4, 4]
  __snake_case:		int					    =		rrgdf[aatype, ...]

  # [*, N, 8] transformations, i.e.
  #   One [*, N, 8, 3, 3] rotation matrix and
  #   One [*, N, 8, 3]    translation matrix
  __snake_case:		Optional[Any]					    =		r.from_tensor_axa(SCREAMING_SNAKE_CASE__)

  __snake_case:		Tuple					    =		alpha.new_zeros((*((1,) * len(alpha.shape[:-1])), 2))
  __snake_case:		Optional[Any]					    =		1

  # [*, N, 8, 2]
  __snake_case:		Any					    =		torch.cat([bb_rot.expand(*alpha.shape[:-2]   ,   -1   ,   -1), alpha]   ,   dim=-2)

  # [*, N, 8, 3, 3]
  # Produces rotation matrices of the form:
  # [
  #   [1, 0  , 0  ],
  #   [0, a_2,-a_1],
  #   [0, a_1, a_2]
  # ]
  # This follows the original code rather than the supplement, which uses
  # different indices.

  __snake_case:		Optional[int]					    =		alpha.new_zeros(default_r.get_rots().get_rot_mats().shape)
  __snake_case:		Union[str, Any]					    =		1
  __snake_case:		Tuple					    =		alpha[..., 1]
  __snake_case:		List[Any]					    =		-alpha[..., 0]
  __snake_case:		Dict					    =		alpha

  __snake_case:		int					    =		default_r.compose(Rigid(Rotation(rot_mats=SCREAMING_SNAKE_CASE__)   ,   SCREAMING_SNAKE_CASE__))

  __snake_case:		Union[str, Any]					    =		all_frames[..., 5]
  __snake_case:		List[str]					    =		all_frames[..., 6]
  __snake_case:		int					    =		all_frames[..., 7]

  __snake_case:		List[str]					    =		all_frames[..., 4]
  __snake_case:		Optional[int]					    =		chia_frame_to_bb.compose(SCREAMING_SNAKE_CASE__)
  __snake_case:		str					    =		chia_frame_to_bb.compose(SCREAMING_SNAKE_CASE__)
  __snake_case:		Tuple					    =		chia_frame_to_bb.compose(SCREAMING_SNAKE_CASE__)

  __snake_case:		Optional[int]					    =		Rigid.cat(
      [
          all_frames[..., :5],
          chia_frame_to_bb.unsqueeze(-1),
          chia_frame_to_bb.unsqueeze(-1),
          chia_frame_to_bb.unsqueeze(-1),
      ]   ,   dim=-1   ,   )

  __snake_case:		str					    =		r[..., None].compose(SCREAMING_SNAKE_CASE__)

  return all_frames_to_global







def      A__				(       SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   )  ->       torch.Tensor:
  # [*, N, 14]
  __snake_case:		Optional[Any]					    =		group_idx[aatype, ...]

  # [*, N, 14, 8]
  __snake_case:		torch.LongTensor					    =		nn.functional.one_hot(
      SCREAMING_SNAKE_CASE__   ,   num_classes=default_frames.shape[-3]   ,   )

  # [*, N, 14, 8]
  __snake_case:		int					    =		r[..., None, :] * group_mask_one_hot

  # [*, N, 14]
  __snake_case:		Optional[int]					    =		t_atoms_to_global.map_tensor_fn(lambda SCREAMING_SNAKE_CASE__: torch.sum(SCREAMING_SNAKE_CASE__   ,   dim=-1))

  # [*, N, 14, 1]
  __snake_case:		str					    =		atom_mask[aatype, ...].unsqueeze(-1)

  # [*, N, 14, 3]
  __snake_case:		Union[str, Any]					    =		lit_positions[aatype, ...]
  __snake_case:		int					    =		t_atoms_to_global.apply(SCREAMING_SNAKE_CASE__)
  __snake_case:		List[Any]					    =		pred_positions * atom_mask

  return pred_positions













from ...configuration_utils import PretrainedConfig
from ...utils import logging


__UpperCAmelCase			:  List[str]										=				logging.get_logger(__name__)

__UpperCAmelCase			:  int										=				{
    # pcqm4mv1 now deprecated
    "graphormer-base": "https://huggingface.co/clefourrier/graphormer-base-pcqm4mv2/resolve/main/config.json",
    # See all Graphormer models at https://huggingface.co/models?filter=graphormer
}







class        __snake_case			(				__lowerCamelCase     ):





       '''simple docstring'''







       lowerCAmelCase__ 						=	"""graphormer"""
       lowerCAmelCase__ 						=	["""past_key_values"""]




       def __init__(    self					:       List[Any]				,							A					:       int = 1				,							A					:       int = 512 * 9				,							A					:       int = 512 * 3				,							A					:       int = 512				,							A					:       int = 512				,							A					:       int = 512				,							A					:       int = 128				,							A					:       int = 5				,							A					:       int = 1_024				,							A					:       str = "multi_hop"				,							A					:       int = 512				,							A					:       bool = False				,							A					:       int = 12				,							A					:       int = 768				,							A					:       int = 768				,							A					:       int = 32				,							A					:       float = 0.1				,							A					:       float = 0.1				,							A					:       float = 0.0				,							A					:       bool = False				,							A					:       bool = False				,							A					:       bool = False				,							A					:       str = "gelu"				,							A					:       float = None				,							A					:       bool = False				,							A					:       int = 0				,							A					:       bool = False				,							A					:       float = 0.0				,							A					:       int = 8				,							A					:       int = None				,							A					:       int = None				,							A					:       bool = True				,							A					:       bool = True				,							A					:       Any=0				,							A					:       Optional[int]=1				,							A					:       Any=2				,							**A					:       Optional[Any]				,							):
         __snake_case:		List[str]					    =		num_classes
         __snake_case:		int					    =		num_atoms
         __snake_case:		List[str]					    =		num_in_degree
         __snake_case:		str					    =		num_out_degree
         __snake_case:		Tuple					    =		num_edges
         __snake_case:		Optional[int]					    =		num_spatial
         __snake_case:		int					    =		num_edge_dis
         __snake_case:		Tuple					    =		edge_type
         __snake_case:		str					    =		multi_hop_max_dist
         __snake_case:		List[str]					    =		spatial_pos_max
         __snake_case:		Optional[Any]					    =		max_nodes
         __snake_case:		Optional[int]					    =		num_hidden_layers
         __snake_case:		Union[str, Any]					    =		embedding_dim
         __snake_case:		Union[str, Any]					    =		embedding_dim
         __snake_case:		Optional[int]					    =		ffn_embedding_dim
         __snake_case:		List[str]					    =		num_attention_heads
         __snake_case:		Any					    =		dropout
         __snake_case:		Dict					    =		attention_dropout
         __snake_case:		Any					    =		layerdrop
         __snake_case:		Dict					    =		encoder_normalize_before
         __snake_case:		Any					    =		pre_layernorm
         __snake_case:		Optional[int]					    =		apply_graphormer_init
         __snake_case:		List[str]					    =		activation_fn
         __snake_case:		Optional[int]					    =		embed_scale
         __snake_case:		Dict					    =		freeze_embeddings
         __snake_case:		Any					    =		num_trans_layers_to_freeze
         __snake_case:		int					    =		share_input_output_embed
         __snake_case:		Dict					    =		traceable
         __snake_case:		str					    =		q_noise
         __snake_case:		Union[str, Any]					    =		qn_block_size

         # These parameters are here for future extensions
         # atm, the model only supports self attention
         __snake_case:		str					    =		kdim
         __snake_case:		Dict					    =		vdim
         __snake_case:		List[Any]					    =		self_attention
         __snake_case:		Tuple					    =		bias

         super().__init__(
             pad_token_id=A				,							bos_token_id=A				,							eos_token_id=A				,							**A				,							)






