




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






			'''simple docstring'''







			...




@overload
def       __magic_name__    (			A		: torch.Tensor,  A		: torch.Tensor,  A		: torch.Tensor       ):






			'''simple docstring'''







			...




def       __magic_name__    (			A		: List[str],  A		: Optional[int],  A		: Dict       ):






			'''simple docstring'''







			a   =      aatype == rc.restype_order["G"]
			a   =      rc.atom_order["CA"]
			a   =      rc.atom_order["CB"]
			a   =      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:
						a   =      torch.where(
						    A,  all_atom_masks[..., ca_idx],  all_atom_masks[..., cb_idx],  )
						return pseudo_beta, pseudo_beta_mask
			else:
						return pseudo_beta




def       __magic_name__    (			A		: torch.Tensor,  A		: Dict[str, torch.Tensor]       ):






			'''simple docstring'''







			a   =      batched_gather(
			    A,  batch["residx_atom37_to_atom14"],  dim=-2,  no_batch_dims=len(atomaa.shape[:-2]       ),  )

			a   =      atomaa_data * batch["atom37_atom_exists"][..., None]

			return atomaa_data




def       __magic_name__    (			A		: Dict[str, torch.Tensor]       ):






			'''simple docstring'''







			a   =      template_feats["template_aatype"]
			a   =      template_feats["template_torsion_angles_sin_cos"]
			a   =      template_feats["template_alt_torsion_angles_sin_cos"]
			a   =      template_feats["template_torsion_angles_mask"]
			a   =      torch.cat(
			    [
			        nn.functional.one_hot(A,  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       __magic_name__    (			A		: Dict[str, torch.Tensor],  A		: torch.types.Number,  A		: torch.types.Number,  A		: int,  A		: bool = False,  A		: float = 1E-20,  A		: float = 1E8,  ):






			'''simple docstring'''







			a   =      batch["template_pseudo_beta_mask"]
			a   =      template_mask[..., None] * template_mask[..., None, :]

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

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

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

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

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

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

			a   =      batch["template_all_atom_mask"]
			a   =      t_aa_masks[..., n] * t_aa_masks[..., ca] * t_aa_masks[..., c]
			a   =      template_mask[..., None] * template_mask[..., None, :]

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

			if not use_unit_vector:
						a   =      unit_vector * 0.0

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

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

			return act




def       __magic_name__    (			A		: Dict[str, torch.Tensor]       ):






			'''simple docstring'''







			a   =      nn.functional.one_hot(batch["extra_msa"],  23       )
			a   =      [
			    msa_ahot,
			    batch["extra_has_deletion"].unsqueeze(-1       ),
			    batch["extra_deletion_value"].unsqueeze(-1       ),
			]
			return torch.cat(A,  dim=-1       )




def       __magic_name__    (			A		: Rigid,  A		: torch.Tensor,  A		: torch.Tensor,  A		: torch.Tensor,  ):






			'''simple docstring'''







			a   =      rrgdf[aatype, ...]

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

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

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

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

			a   =      default_r.compose(Rigid(Rotation(rot_mats=A       ),  A       )       )

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

			a   =      all_frames[..., 4]
			a   =      chia_frame_to_bb.compose(A       )
			a   =      chia_frame_to_bb.compose(A       )
			a   =      chia_frame_to_bb.compose(A       )

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

			a   =      r[..., None].compose(A       )

			return all_frames_to_global






def       __magic_name__    (			A		: Rigid,  A		: torch.Tensor,  A		: torch.Tensor,  A		: torch.Tensor,  A		: torch.Tensor,  A		: torch.Tensor,  ):






			'''simple docstring'''







			a   =      group_idx[aatype, ...]

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

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

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

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

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

			return pred_positions












from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


__lowerCAmelCase		:    Dict	      =		logging.get_logger(__name__)

__lowerCAmelCase		:    Optional[int]	      =		{
    'facebook/mbart-large-cc25': 'https://huggingface.co/facebook/mbart-large-cc25/resolve/main/config.json',
    # See all MBART models at https://huggingface.co/models?filter=mbart
}




class    snake_case__							(_UpperCamelCase       ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_	:							int										= """mbart"""
				SCREAMING_SNAKE_CASE_	:							Optional[Any]										= ["""past_key_values"""]
				SCREAMING_SNAKE_CASE_	:							Any										= {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}





				def __init__(				self    :					List[Any] ,				__lowerCamelCase    :					str=5_02_65 ,				__lowerCamelCase    :					List[Any]=10_24 ,				__lowerCamelCase    :					Optional[Any]=12 ,				__lowerCamelCase    :					List[Any]=40_96 ,				__lowerCamelCase    :					int=16 ,				__lowerCamelCase    :					List[Any]=12 ,				__lowerCamelCase    :					Tuple=40_96 ,				__lowerCamelCase    :					Tuple=16 ,				__lowerCamelCase    :					Optional[Any]=0.0 ,				__lowerCamelCase    :					int=0.0 ,				__lowerCamelCase    :					Any=True ,				__lowerCamelCase    :					List[Any]=True ,				__lowerCamelCase    :					str="gelu" ,				__lowerCamelCase    :					Tuple=10_24 ,				__lowerCamelCase    :					Any=0.1 ,				__lowerCamelCase    :					Dict=0.0 ,				__lowerCamelCase    :					Tuple=0.0 ,				__lowerCamelCase    :					Optional[int]=0.02 ,				__lowerCamelCase    :					Dict=0.0 ,				__lowerCamelCase    :					Union[str, Any]=False ,				__lowerCamelCase    :					Optional[int]=1 ,				__lowerCamelCase    :					Tuple=0 ,				__lowerCamelCase    :					List[Any]=2 ,				__lowerCamelCase    :					Dict=2 ,				**__lowerCamelCase    :					Optional[int] ,				) ->					Optional[int]:
							a   =      vocab_size
							a   =      max_position_embeddings
							a   =      d_model
							a   =      encoder_ffn_dim
							a   =      encoder_layers
							a   =      encoder_attention_heads
							a   =      decoder_ffn_dim
							a   =      decoder_layers
							a   =      decoder_attention_heads
							a   =      dropout
							a   =      attention_dropout
							a   =      activation_dropout
							a   =      activation_function
							a   =      init_std
							a   =      encoder_layerdrop
							a   =      decoder_layerdrop
							a   =      classifier_dropout
							a   =      use_cache
							a   =      encoder_layers
							a   =      scale_embedding  # scale factor will be sqrt(d_model) if True
							super().__init__(
							    pad_token_id=__lowerCamelCase ,				bos_token_id=__lowerCamelCase ,				eos_token_id=__lowerCamelCase ,				is_encoder_decoder=__lowerCamelCase ,				forced_eos_token_id=__lowerCamelCase ,				**__lowerCamelCase ,				)




class    snake_case__							(_UpperCamelCase       ):
				"""simple docstring"""



				@property
				def 							__UpperCAmelCase							(				self    :					Optional[Any]							) ->					Mapping[str, Mapping[int, str]]:
							if self.task in ["default", "seq2seq-lm"]:
										a   =      OrderedDict(
										    [
										        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
										        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
										    ]							)

										if self.use_past:
													a   =      {0: "batch"}
													a   =      {0: "batch", 1: "past_decoder_sequence + sequence"}
										else:
													a   =      {0: "batch", 1: "decoder_sequence"}
													a   =      {0: "batch", 1: "decoder_sequence"}

										if self.use_past:
													self.fill_with_past_key_values_(__lowerCamelCase ,				direction="inputs"							)
							elif self.task == "causal-lm":
										# TODO: figure this case out.
										a   =      OrderedDict(
										    [
										        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
										        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
										    ]							)
										if self.use_past:
													a   ,		a   =      self.num_layers
													for i in range(__lowerCamelCase							):
																a   =      {0: "batch", 2: "past_sequence + sequence"}
																a   =      {0: "batch", 2: "past_sequence + sequence"}
							else:
										a   =      OrderedDict(
										    [
										        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
										        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
										        ("decoder_input_ids", {0: "batch", 1: "decoder_sequence"}),
										        ("decoder_attention_mask", {0: "batch", 1: "decoder_sequence"}),
										    ]							)

							return common_inputs





				@property
				def 							__UpperCAmelCase							(				self    :					Any							) ->					Mapping[str, Mapping[int, str]]:
							if self.task in ["default", "seq2seq-lm"]:
										a   =      super().outputs
							else:
										a   =      super(__lowerCamelCase ,				self							).outputs
										if self.use_past:
													a   ,		a   =      self.num_layers
													for i in range(__lowerCamelCase							):
																a   =      {0: "batch", 2: "past_sequence + sequence"}
																a   =      {0: "batch", 2: "past_sequence + sequence"}
							return common_outputs





				def 							__UpperCAmelCase							(				self    :					Union[str, Any] ,				__lowerCamelCase    :					PreTrainedTokenizer ,				__lowerCamelCase    :					int = -1 ,				__lowerCamelCase    :					int = -1 ,				__lowerCamelCase    :					bool = False ,				__lowerCamelCase    :					Optional[TensorType] = None ,				) ->					Mapping[str, Any]:
							a   =      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
							    __lowerCamelCase ,				__lowerCamelCase ,				__lowerCamelCase ,				__lowerCamelCase ,				__lowerCamelCase							)

							# Generate decoder inputs
							a   =      seq_length if not self.use_past else 1
							a   =      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
							    __lowerCamelCase ,				__lowerCamelCase ,				__lowerCamelCase ,				__lowerCamelCase ,				__lowerCamelCase							)
							a   =      {f"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
							a   =      dict(**__lowerCamelCase ,				**__lowerCamelCase							)

							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   =      common_inputs["input_ids"].shape
										a   =      common_inputs["decoder_input_ids"].shape[1]
										a   ,		a   =      self.num_attention_heads
										a   =      (
										    batch,
										    num_encoder_attention_heads,
										    encoder_seq_length,
										    self._config.hidden_size // num_encoder_attention_heads,
										)
										a   =      decoder_seq_length + 3
										a   =      (
										    batch,
										    num_decoder_attention_heads,
										    decoder_past_length,
										    self._config.hidden_size // num_decoder_attention_heads,
										)

										a   =      torch.cat(
										    [common_inputs["decoder_attention_mask"], torch.ones(__lowerCamelCase ,				__lowerCamelCase							)] ,				dim=1							)

										a   =      []
										# If the number of encoder and decoder layers are present in the model configuration, both are considered
										a   ,		a   =      self.num_layers
										a   =      min(__lowerCamelCase ,				__lowerCamelCase							)
										a   =      max(__lowerCamelCase ,				__lowerCamelCase							) - min_num_layers
										a   =      "encoder" if num_encoder_layers > num_decoder_layers else "decoder"

										for _ in range(__lowerCamelCase							):
													common_inputs["past_key_values"].append(
													    (
													        torch.zeros(__lowerCamelCase							),
													        torch.zeros(__lowerCamelCase							),
													        torch.zeros(__lowerCamelCase							),
													        torch.zeros(__lowerCamelCase							),
													    )							)
										# TODO: test this.
										a   =      encoder_shape if remaining_side_name == "encoder" else decoder_shape
										for _ in range(__lowerCamelCase ,				__lowerCamelCase							):
													common_inputs["past_key_values"].append((torch.zeros(__lowerCamelCase							), torch.zeros(__lowerCamelCase							))							)
							return common_inputs





				def 							__UpperCAmelCase							(				self    :					str ,				__lowerCamelCase    :					PreTrainedTokenizer ,				__lowerCamelCase    :					int = -1 ,				__lowerCamelCase    :					int = -1 ,				__lowerCamelCase    :					bool = False ,				__lowerCamelCase    :					Optional[TensorType] = None ,				) ->					Mapping[str, Any]:
							a   =      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
							    __lowerCamelCase ,				__lowerCamelCase ,				__lowerCamelCase ,				__lowerCamelCase ,				__lowerCamelCase							)

							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   =      common_inputs["input_ids"].shape
										# Not using the same length for past_key_values
										a   =      seqlen + 2
										a   ,		a   =      self.num_layers
										a   ,		a   =      self.num_attention_heads
										a   =      (
										    batch,
										    num_encoder_attention_heads,
										    past_key_values_length,
										    self._config.hidden_size // num_encoder_attention_heads,
										)

										a   =      common_inputs["attention_mask"].dtype
										a   =      torch.cat(
										    [common_inputs["attention_mask"], torch.ones(__lowerCamelCase ,				__lowerCamelCase ,				dtype=__lowerCamelCase							)] ,				dim=1							)
										a   =      [
										    (torch.zeros(__lowerCamelCase							), torch.zeros(__lowerCamelCase							)) for _ in range(__lowerCamelCase							)
										]
							return common_inputs





				def 							__UpperCAmelCase							(				self    :					Union[str, Any] ,				__lowerCamelCase    :					PreTrainedTokenizer ,				__lowerCamelCase    :					int = -1 ,				__lowerCamelCase    :					int = -1 ,				__lowerCamelCase    :					bool = False ,				__lowerCamelCase    :					Optional[TensorType] = None ,				) ->					Mapping[str, Any]:
							# Copied from OnnxConfig.generate_dummy_inputs
							# Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
							# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
							a   =      compute_effective_axis_dimension(
							    __lowerCamelCase ,				fixed_dimension=OnnxConfig.default_fixed_batch ,				num_token_to_add=0							)

							# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
							a   =      tokenizer.num_special_tokens_to_add(__lowerCamelCase							)
							a   =      compute_effective_axis_dimension(
							    __lowerCamelCase ,				fixed_dimension=OnnxConfig.default_fixed_sequence ,				num_token_to_add=__lowerCamelCase							)

							# Generate dummy inputs according to compute batch and sequence
							a   =      [" ".join([tokenizer.unk_token]							) * seq_length] * batch_size
							a   =      dict(tokenizer(__lowerCamelCase ,				return_tensors=__lowerCamelCase							)							)
							return common_inputs





				def 							__UpperCAmelCase							(				self    :					str ,				__lowerCamelCase    :					PreTrainedTokenizer ,				__lowerCamelCase    :					int = -1 ,				__lowerCamelCase    :					int = -1 ,				__lowerCamelCase    :					bool = False ,				__lowerCamelCase    :					Optional[TensorType] = None ,				) ->					Mapping[str, Any]:
							if self.task in ["default", "seq2seq-lm"]:
										a   =      self._generate_dummy_inputs_for_default_and_seqaseq_lm(
										    __lowerCamelCase ,				batch_size=__lowerCamelCase ,				seq_length=__lowerCamelCase ,				is_pair=__lowerCamelCase ,				framework=__lowerCamelCase							)

							elif self.task == "causal-lm":
										a   =      self._generate_dummy_inputs_for_causal_lm(
										    __lowerCamelCase ,				batch_size=__lowerCamelCase ,				seq_length=__lowerCamelCase ,				is_pair=__lowerCamelCase ,				framework=__lowerCamelCase							)
							else:
										a   =      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
										    __lowerCamelCase ,				batch_size=__lowerCamelCase ,				seq_length=__lowerCamelCase ,				is_pair=__lowerCamelCase ,				framework=__lowerCamelCase							)

							return common_inputs





				def 							__UpperCAmelCase							(				self    :					Optional[int] ,				__lowerCamelCase    :					Union[str, Any] ,				__lowerCamelCase    :					int ,				__lowerCamelCase    :					str ,				__lowerCamelCase    :					Optional[Any]							) ->					Dict:
							if self.task in ["default", "seq2seq-lm"]:
										a   =      super()._flatten_past_key_values_(__lowerCamelCase ,				__lowerCamelCase ,				__lowerCamelCase ,				__lowerCamelCase							)
							else:
										a   =      super(__lowerCamelCase ,				self							)._flatten_past_key_values_(
										    __lowerCamelCase ,				__lowerCamelCase ,				__lowerCamelCase ,				__lowerCamelCase							)






