

'''simple docstring'''




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       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   None						):
  ...


@overload
def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor						):
  ...


def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Union[str, Any]       ,   SCREAMING_SNAKE_CASE      :   Tuple       ,   SCREAMING_SNAKE_CASE      :   Union[str, Any]						):
  UpperCAmelCase													=	aatype == rc.restype_order['G']
  UpperCAmelCase													=	rc.atom_order['CA']
  UpperCAmelCase													=	rc.atom_order['CB']
  UpperCAmelCase													=	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:
    UpperCAmelCase													=	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       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   Dict[str, torch.Tensor]						):
  UpperCAmelCase													=	batched_gather(
      SCREAMING_SNAKE_CASE       ,   batch['residx_atom37_to_atom14']       ,   dim=-2       ,   no_batch_dims=len(atomaa.shape[:-2]						)       ,   )

  UpperCAmelCase													=	atomaa_data * batch['atom37_atom_exists'][..., None]

  return atomaa_data


def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Dict[str, torch.Tensor]						):
  UpperCAmelCase													=	template_feats['template_aatype']
  UpperCAmelCase													=	template_feats['template_torsion_angles_sin_cos']
  UpperCAmelCase													=	template_feats['template_alt_torsion_angles_sin_cos']
  UpperCAmelCase													=	template_feats['template_torsion_angles_mask']
  UpperCAmelCase													=	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       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Dict[str, torch.Tensor]       ,   SCREAMING_SNAKE_CASE      :   torch.types.Number       ,   SCREAMING_SNAKE_CASE      :   torch.types.Number       ,   SCREAMING_SNAKE_CASE      :   int       ,   SCREAMING_SNAKE_CASE      :   bool = False       ,   SCREAMING_SNAKE_CASE      :   float = 1E-20       ,   SCREAMING_SNAKE_CASE      :   float = 1E8       ,   ):
  UpperCAmelCase													=	batch['template_pseudo_beta_mask']
  UpperCAmelCase													=	template_mask[..., None] * template_mask[..., None, :]

  # Compute distogram (this seems to differ slightly from Alg. 5)
  UpperCAmelCase													=	batch['template_pseudo_beta']
  UpperCAmelCase													=	torch.sum((tpb[..., None, :] - tpb[..., None, :, :]) ** 2       ,   dim=-1       ,   keepdim=SCREAMING_SNAKE_CASE						)
  UpperCAmelCase													=	torch.linspace(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE       ,   device=tpb.device						) ** 2
  UpperCAmelCase													=	torch.cat([lower[1:], lower.new_tensor([inf]						)]       ,   dim=-1						)
  UpperCAmelCase													=	((dgram > lower) * (dgram < upper)).type(dgram.dtype						)

  UpperCAmelCase													=	[dgram, template_mask_ad[..., None]]

  UpperCAmelCase													=	nn.functional.one_hot(
      batch['template_aatype']       ,   rc.restype_num + 2       ,   )

  UpperCAmelCase													=	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						)						)

  UpperCAmelCase							,      UpperCAmelCase							,      UpperCAmelCase													=	[rc.atom_order[a] for a in ['N', 'CA', 'C']]
  UpperCAmelCase													=	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       ,   )
  UpperCAmelCase													=	rigids.get_trans()[..., None, :, :]
  UpperCAmelCase													=	rigids[..., None].invert_apply(SCREAMING_SNAKE_CASE						)

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

  UpperCAmelCase													=	batch['template_all_atom_mask']
  UpperCAmelCase													=	t_aa_masks[..., n] * t_aa_masks[..., ca] * t_aa_masks[..., c]
  UpperCAmelCase													=	template_mask[..., None] * template_mask[..., None, :]

  UpperCAmelCase													=	inv_distance_scalar * template_mask_ad
  UpperCAmelCase													=	rigid_vec * inv_distance_scalar[..., None]

  if not use_unit_vector:
    UpperCAmelCase													=	unit_vector * 0.0

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

  UpperCAmelCase													=	torch.cat(SCREAMING_SNAKE_CASE       ,   dim=-1						)
  UpperCAmelCase													=	act * template_mask_ad[..., None]

  return act


def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Dict[str, torch.Tensor]						):
  UpperCAmelCase													=	nn.functional.one_hot(batch['extra_msa']       ,   23						)
  UpperCAmelCase													=	[
      msa_ahot,
      batch['extra_has_deletion'].unsqueeze(-1						),
      batch['extra_deletion_value'].unsqueeze(-1						),
  ]
  return torch.cat(SCREAMING_SNAKE_CASE       ,   dim=-1						)


def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Rigid       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   ):
  # [*, N, 8, 4, 4]
  UpperCAmelCase													=	rrgdf[aatype, ...]

  # [*, N, 8] transformations, i.e.
  #   One [*, N, 8, 3, 3] rotation matrix and
  #   One [*, N, 8, 3]    translation matrix
  UpperCAmelCase													=	r.from_tensor_axa(SCREAMING_SNAKE_CASE						)

  UpperCAmelCase													=	alpha.new_zeros((*((1,) * len(alpha.shape[:-1]						)), 2)						)
  UpperCAmelCase													=	1

  # [*, N, 8, 2]
  UpperCAmelCase													=	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.

  UpperCAmelCase													=	alpha.new_zeros(default_r.get_rots().get_rot_mats().shape						)
  UpperCAmelCase													=	1
  UpperCAmelCase													=	alpha[..., 1]
  UpperCAmelCase													=	-alpha[..., 0]
  UpperCAmelCase													=	alpha

  UpperCAmelCase													=	default_r.compose(Rigid(Rotation(rot_mats=SCREAMING_SNAKE_CASE						)       ,   SCREAMING_SNAKE_CASE						)						)

  UpperCAmelCase													=	all_frames[..., 5]
  UpperCAmelCase													=	all_frames[..., 6]
  UpperCAmelCase													=	all_frames[..., 7]

  UpperCAmelCase													=	all_frames[..., 4]
  UpperCAmelCase													=	chia_frame_to_bb.compose(SCREAMING_SNAKE_CASE						)
  UpperCAmelCase													=	chia_frame_to_bb.compose(SCREAMING_SNAKE_CASE						)
  UpperCAmelCase													=	chia_frame_to_bb.compose(SCREAMING_SNAKE_CASE						)

  UpperCAmelCase													=	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       ,   )

  UpperCAmelCase													=	r[..., None].compose(SCREAMING_SNAKE_CASE						)

  return all_frames_to_global



def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Rigid       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   SCREAMING_SNAKE_CASE      :   torch.Tensor       ,   ):
  # [*, N, 14]
  UpperCAmelCase													=	group_idx[aatype, ...]

  # [*, N, 14, 8]
  UpperCAmelCase													=	nn.functional.one_hot(
      SCREAMING_SNAKE_CASE       ,   num_classes=default_frames.shape[-3]       ,   )

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

  # [*, N, 14]
  UpperCAmelCase													=	t_atoms_to_global.map_tensor_fn(lambda SCREAMING_SNAKE_CASE						: torch.sum(SCREAMING_SNAKE_CASE       ,   dim=-1						)						)

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

  # [*, N, 14, 3]
  UpperCAmelCase													=	lit_positions[aatype, ...]
  UpperCAmelCase													=	t_atoms_to_global.apply(SCREAMING_SNAKE_CASE						)
  UpperCAmelCase													=	pred_positions * atom_mask

  return pred_positions






'''simple docstring'''




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


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

_a				: Dict 							=						{
    'mnaylor/mega-base-wikitext': 'https://huggingface.co/mnaylor/mega-base-wikitext/resolve/main/config.json',
}



class        lowercase_	(		a ):

  '''simple docstring'''





  __lowerCAmelCase					: List[str]      =   "mega"
  def __init__(     self   , a_=3_0_5_2_2   , a_=1_2_8   , a_=4   , a_=2_5_6   , a_=1_6   , a_=True   , a_=6_4   , a_=False   , a_=-1   , a_=None   , a_=True   , a_="scalenorm"   , a_=True   , a_="silu"   , a_="softmax"   , a_=0.1   , a_=0.1   , a_=0.1   , a_=False   , a_=True   , a_=2_5_6   , a_=True   , a_=0.1   , a_=2_0_4_8   , a_=False   , a_=2   , a_=0.02   , a_=0.2   , a_=0.02   , a_=1.0   , a_=1   , a_=0   , a_=2   , a_="rotary"   , a_=None   , a_=True   , a_=True   , **a_   , )							->				Any:


    """simple docstring"""
    super().__init__(pad_token_id=a_   , bos_token_id=a_   , eos_token_id=a_   , **a_     )

    UpperCAmelCase													=	vocab_size
    UpperCAmelCase													=	hidden_size
    UpperCAmelCase													=	num_hidden_layers
    UpperCAmelCase													=	activation
    UpperCAmelCase													=	attention_activation
    UpperCAmelCase													=	intermediate_size
    UpperCAmelCase													=	ema_projection_size
    UpperCAmelCase													=	bidirectional
    UpperCAmelCase													=	shared_representation_size
    UpperCAmelCase													=	use_chunking
    UpperCAmelCase													=	chunk_size
    UpperCAmelCase													=	truncation
    UpperCAmelCase													=	normalize_before_mega
    UpperCAmelCase													=	normalization_type
    UpperCAmelCase													=	norm_affine
    UpperCAmelCase													=	dropout_prob
    UpperCAmelCase													=	hidden_dropout_prob
    UpperCAmelCase													=	attention_probs_dropout_prob
    UpperCAmelCase													=	use_feature_dropout
    UpperCAmelCase													=	use_normalized_ffn
    UpperCAmelCase													=	nffn_hidden_size
    UpperCAmelCase													=	normalize_before_ffn
    UpperCAmelCase													=	nffn_activation_dropout_prob
    UpperCAmelCase													=	max_positions
    UpperCAmelCase													=	add_token_type_embeddings
    UpperCAmelCase													=	type_vocab_size
    UpperCAmelCase													=	initializer_range
    UpperCAmelCase													=	ema_delta_alpha_range
    UpperCAmelCase													=	ema_beta_range
    UpperCAmelCase													=	ema_gamma_omega_range
    UpperCAmelCase													=	relative_positional_bias
    UpperCAmelCase													=	use_cache
    UpperCAmelCase													=	classifier_dropout
    UpperCAmelCase													=	add_lm_hidden_dense_layer
    UpperCAmelCase													=	1  # not used but required by Hugging Face




class        lowercase_	(		a ):

  '''simple docstring'''





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


    """simple docstring"""
    if self.task == "multiple-choice":
      UpperCAmelCase													=	{0: 'batch', 1: 'choice', 2: 'sequence'}
    else:
      UpperCAmelCase													=	{0: 'batch', 1: 'sequence'}
    return OrderedDict(
        [
            ('input_ids', dynamic_axis),
            ('attention_mask', dynamic_axis),
        ]     )


