
    fThNO                       S SK r S SKrS SKJr  S SKJr  S SKJrJrJ	r	J
r
JrJrJr  S SKrS SKrS SKJr  S SKJr  SSKJr  SSKJr  SS	KJrJrJrJr  S
SKJrJr  S
SK J!r!J"r"J#r#J$r$J%r%J&r&J'r'J(r(J)r)J*r*J+r+J,r,  \RZ                  " \.5      r/\ " S S\5      5       r0S r1S r2SRS\	\Rf                     S\4S\Rf                  4S jjr5S\Rf                  S\64S jr7S\Rf                  S\	\6   4S jr8S r9SSS jr:S r; " S S\Rx                  5      r= " S  S!\R|                  5      r?\R                  R                  STS\Rf                  S"\6S\Rf                  4S# jj5       rB " S$ S%\R|                  5      rC " S& S'\R|                  5      rD " S( S)\R|                  5      rE " S* S+\5      rF " S, S-\R|                  5      rG " S. S/\R|                  5      rH " S0 S1\R|                  5      rI " S2 S3\R|                  5      rJ " S4 S5\R|                  5      rK " S6 S7\R|                  5      rL " S8 S95      rMSUS: jrNS; rO " S< S=\R|                  5      rP " S> S?\R|                  5      rQ " S@ SA\R|                  5      rR " SB SC\R|                  5      rS " SD SE\R|                  5      rT " SF SG\R|                  5      rU " SH SI\R|                  5      rV " SJ SK\R|                  5      rW " SL SM\R|                  5      rX\" SNSO9 " SP SQ\5      5       rYSQS+/rZg)V    N)	dataclass)partial)CallableDictListOptionalSequenceTupleUnion)	LayerNorm   )is_deepspeed_available)ModelOutput)ContextManagersauto_docstringis_scipy_availablelogging   )EsmModelEsmPreTrainedModel)	OFProteinRigidRotationatom14_to_atom37chunk_layercompute_predicted_aligned_error
compute_tm-frames_and_literature_positions_to_atom14_posmake_atom14_masksresidue_constantsto_pdbtorsion_angles_to_framesc                      \ rS rSr% SrSr\\R                     \	S'   Sr
\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S	'   Sr\\R                     \	S
'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr g)EsmForProteinFoldingOutput6   aU
  
Output type of [`EsmForProteinFoldingOutput`].

Args:
    frames (`torch.FloatTensor`):
        Output frames.
    sidechain_frames (`torch.FloatTensor`):
        Output sidechain frames.
    unnormalized_angles (`torch.FloatTensor`):
        Predicted unnormalized backbone and side chain torsion angles.
    angles (`torch.FloatTensor`):
        Predicted backbone and side chain torsion angles.
    positions (`torch.FloatTensor`):
        Predicted positions of the backbone and side chain atoms.
    states (`torch.FloatTensor`):
        Hidden states from the protein folding trunk.
    s_s (`torch.FloatTensor`):
        Per-residue embeddings derived by concatenating the hidden states of each layer of the ESM-2 LM stem.
    s_z (`torch.FloatTensor`):
        Pairwise residue embeddings.
    distogram_logits (`torch.FloatTensor`):
        Input logits to the distogram used to compute residue distances.
    lm_logits (`torch.FloatTensor`):
        Logits output by the ESM-2 protein language model stem.
    aatype (`torch.FloatTensor`):
        Input amino acids (AlphaFold2 indices).
    atom14_atom_exists (`torch.FloatTensor`):
        Whether each atom exists in the atom14 representation.
    residx_atom14_to_atom37 (`torch.FloatTensor`):
        Mapping between atoms in the atom14 and atom37 representations.
    residx_atom37_to_atom14 (`torch.FloatTensor`):
        Mapping between atoms in the atom37 and atom14 representations.
    atom37_atom_exists (`torch.FloatTensor`):
        Whether each atom exists in the atom37 representation.
    residue_index (`torch.FloatTensor`):
        The index of each residue in the protein chain. Unless internal padding tokens are used, this will just be
        a sequence of integers from 0 to `sequence_length`.
    lddt_head (`torch.FloatTensor`):
        Raw outputs from the lddt head used to compute plddt.
    plddt (`torch.FloatTensor`):
        Per-residue confidence scores. Regions of low confidence may indicate areas where the model's prediction is
        uncertain, or where the protein structure is disordered.
    ptm_logits (`torch.FloatTensor`):
        Raw logits used for computing ptm.
    ptm (`torch.FloatTensor`):
        TM-score output representing the model's high-level confidence in the overall structure.
    aligned_confidence_probs (`torch.FloatTensor`):
        Per-residue confidence scores for the aligned structure.
    predicted_aligned_error (`torch.FloatTensor`):
        Predicted error between the model's prediction and the ground truth.
    max_predicted_aligned_error (`torch.FloatTensor`):
        Per-sample maximum predicted error.
Nframessidechain_framesunnormalized_anglesangles	positionsstatess_ss_zdistogram_logits	lm_logitsaatypeatom14_atom_existsresidx_atom14_to_atom37residx_atom37_to_atom14atom37_atom_existsresidue_index	lddt_headplddt
ptm_logitsptmaligned_confidence_probspredicted_aligned_errormax_predicted_aligned_error )!__name__
__module____qualname____firstlineno____doc__r&   r   torchFloatTensor__annotations__r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   __static_attributes__r=       `/var/www/auris/envauris/lib/python3.13/site-packages/transformers/models/esm/modeling_esmfold.pyr$   r$   6   s   4l +/FHU&&'.48hu00187;%"3"34;*.FHU&&'.-1Ix))*1*.FHU&&'.'+C%##	$+'+C%##	$+48hu0018-1Ix))*1*.FHU&&'.6:!2!23:;?Xe&7&78?;?Xe&7&78?6:!2!23:15M8E--.5-1Ix))*1)-E8E%%&-.2J**+2'+C%##	$+<@hu'8'89@;?Xe&7&78??C%*;*;!<CrG   r$   c                      [         R                  " 5       [         R                  :H  n U =(       a    [         R                  " 5       n U $ N)rC   get_autocast_gpu_dtypefloat16is_autocast_enabled)fp16_enableds    rH   is_fp16_enabledrO      s2    //1U]]BL?E$=$=$?LrG   c                      [        5       (       a  g SS Kn U R                  R                  5       $ ! [         a     gf = f)NFr   )r   	deepspeedutilsis_initialized	Exception)rQ   s    rH   is_deepspeed_initializedrU      s=    	 ??1133 		s   0 
==samplespad_vreturnc                    [        U 5      S:X  a  [        R                  " 5       $ [        U  Vs1 s H  o"R                  5       iM     sn5      S:w  a,  [	        SU  Vs/ s H  o"R                  5       PM     sn 35      e[        U  Vs1 s H  o"R                  iM     sn5      u  n[        U  Vs/ s H  o"R                  PM     sn6  Vs/ s H  n[        U5      PM     nn[        R                  " [        U 5      /UQ7U S   R                  US.6nUR                  U5        [        [        U 5      5       H)  nXg   nX   n	X[        S U	R                   5       5      '   M+     U$ s  snf s  snf s  snf s  snf s  snf )z
Takes a list of tensors with the following dimensions:
    [(d_11, ..., d_1K),
     (d_21, ..., d_2K), ..., (d_N1, ..., d_NK)]
and stack + pads them into a single tensor of:
(N, max_i=1,N { d_i1 }, ..., max_i=1,N {diK})
r   r   z Samples has varying dimensions: )dtypedevicec              3   :   #    U  H  n[        S U5      v   M     g7f)r   Nslice).0ks     rH   	<genexpr>(collate_dense_tensors.<locals>.<genexpr>   s     4GquQ{{Gs   )lenrC   TensordimRuntimeErrortupler[   zipshapemaxemptyrZ   fill_range)
rV   rW   xr[   lst	max_shaperesultiresult_its
             rH   collate_dense_tensorsru      s8    7|q||~
W%WEEGW%&!+=PW>XPW1uuwPW>X=YZ[[1Axx12IV%(G*DGq77G*D%EF%EcS%EIF[[WY	Y9I9IRXYF
LL3w< 9J894AGG445 ! M &>X1*DFs   E%E*
E/.E4	E9rt   no_dimsc                 F    U R                  U R                  S U*  S-   5      $ )N)reshaperi   )rt   rv   s     rH   flatten_final_dimsr{      s#    99QWWYwh'%/00rG   tensorindsc           	          S[        U5      -  n[        [        [        U R                  S U 5      5      5      nU R	                  X1 Vs/ s H  oBU-   PM	     sn-   5      $ s  snf )Nry   )rc   listrm   ri   permute)r|   r}   
zero_index
first_indsrr   s        rH   permute_final_dimsr      sV    c$iJeC[j 9:;<J>>*'E1Q'EEFF'Es   A
c                     US   n0 nUR                  5        HG  u  pEU Vs/ s H  ofU   PM	     nn[        U[        5      (       a  [        X5      X4'   M=  U " U5      X4'   MI     U$ s  snf )Nr   )items
isinstancedictdict_multimap)fndictsfirstnew_dictr`   vdall_vs           rH   r   r      sh    !HEH$%u!1u%a'2HKU)HK  O &s   A%c                 J   U R                   nU[        SUS   5      -  n[        5       (       dd  [        R	                  S5        [
        R                  " U5      n[        R                  R                  R                  XS9R                  SSU-  S9  g SSKJn  [
        R                  " U5      UR                  S	S
SSS9-  nUR                  S	S
SX@R!                  5       S9n["        R$                  " Xc5      nU R'                  [        R(                  " X`R*                  S95        g )Nr   zlThis init requires scipy, but scipy was not found, default to an approximation that might not be equivalent.)std               @)minrj   r   )	truncnorm   )ablocscale)r   r   r   r   sizer[   )ri   rj   r   loggerwarningmathsqrtrC   nninitnormal_clampscipy.statsr   r   rvsnumelnprz   copy_r|   r[   )weightsr   fanri   r   r   rV   s          rH   trunc_normal_init_r      s    MMEC58$$E	
 iig/55#395M 	*iiqaq!II--"q--/-R**W,ell7>>BCrG   c                     [         R                  " 5          SnU R                  U5        S S S 5        g ! , (       d  f       g = f)NgabR?)rC   no_gradrl   )r   softplus_inverse_1s     rH   ipa_point_weights_init_r      s(    	.() 
s	   3
Ac                      ^  \ rS rSrSr   SS\S\S\S\S\\	\
R                  \
R                  /S4      4
U 4S	 jjjrS
rU =r$ )EsmFoldLinear   z
A Linear layer with built-in nonstandard initializations. Called just like torch.nn.Linear.

Implements the initializers in 1.11.4, plus some additional ones found in the code.
Nin_dimout_dimbiasr   init_fnc                    > [         TU ]  XUS9  U(       a9  [        R                  " 5          U R                  R                  S5        SSS5        X@l        XPl        US;  a  [        S5      eg! , (       d  f       N,= f)a  
Args:
    in_dim:
        The final dimension of inputs to the layer
    out_dim:
        The final dimension of layer outputs
    bias:
        Whether to learn an additive bias. True by default
    init:
        The initializer to use. Choose from:

        "default": LeCun fan-in truncated normal initialization "relu": He initialization w/ truncated normal
        distribution "glorot": Fan-average Glorot uniform initialization "gating": Weights=0, Bias=1 "normal":
        Normal initialization with std=1/sqrt(fan_in) "final": Weights=0, Bias=0

        Overridden by init_fn if the latter is not None.
    init_fn:
        A custom initializer taking weight and bias as inputs. Overrides init if not None.
r   r   N)defaultreluglorotgatingnormalfinalzInvalid init string.)	super__init__rC   r   r   rl   r   r   
ValueError)selfr   r   r   r   r   	__class__s         rH   r   EsmFoldLinear.__init__   se    6 	t4		" !	QQ344 R !s   A..
A<)r   r   )Tr   N)r>   r?   r@   rA   rB   intboolstrr   r   rC   rd   r   rF   __classcell__r   s   @rH   r   r      si     JN$5$5 $5 	$5
 $5 (ELL%,,#?#EFG$5 $5rG   r   c                   2   ^  \ rS rSrSU 4S jjrS rSrU =r$ )EsmFoldLayerNormi  c                    > [         TU ]  5         U4U l        X l        [        R
                  " [        R                  " U5      5      U l        [        R
                  " [        R                  " U5      5      U l
        g rJ   )r   r   c_inepsr   	ParameterrC   onesweightzerosr   )r   r   r   r   s      rH   r   EsmFoldLayerNorm.__init__  sN    G	ll5::d#34LLT!23	rG   c           
      F   UR                   nU[        R                  L a  [        5       (       d  [        R                  R
                  R                  SS9   [        R                  R                  XR                  U R                  R                  US9U R                  R                  US9U R                  5      nS S S 5        U$ [        R                  R                  XR                  U R                  U R                  U R                  5      nU$ ! , (       d  f       W$ = f)NFenabledrZ   )rZ   rC   bfloat16rU   cudaampautocastr   
functional
layer_normr   r   tor   r   )r   rn   r   outs       rH   forwardEsmFoldLayerNorm.forward  s    GG'?'A'A(((7mm..q))T[[^^RS^=TVZV_V_VbVbijVbVkmqmumuv 8
 
 --**1iidiiQUQYQYZC
 87
 
s   A%D
D )r   r   r   r   )gh㈵>r>   r?   r@   rA   r   r   rF   r   r   s   @rH   r   r     s    4 rG   r   re   c                    U R                   nU[        R                  L ah  [        5       (       dY  [        R                  R
                  R                  SS9   [        R                  R                  R                  XS9nSSS5        U$ [        R                  R                  R                  XS9nU$ ! , (       d  f       W$ = f)zS
Softmax, but without automatic casting to fp32 when the input is of type bfloat16
Fr   re   N)
rZ   rC   r   rU   r   r   r   r   r   softmax)rt   re   r   ss       rH   softmax_no_castr   (  s    
 	
AENN#;#=#=ZZ^^$$U$3##++A+7A 4
 H HH'''3H 43
 Hs   (B11
C c                     ^  \ rS rSrSr SS\S\S\S\S\S\4U 4S	 jjjrS
\R                  S\R                  S\
\R                  \R                  \R                  4   4S jrS\R                  S
\R                  S\R                  4S jr       SS
\R                  S\R                  S\\\R                        S\S\S\S\S\S\\R                     S\R                  4S jjrSrU =r$ )EsmFoldAttentioni7  zm
Standard multi-head attention using AlphaFold's default layer initialization. Allows multiple bias vectors.
c_qc_kc_vc_hiddenno_headsr   c                   > [         TU ]  5         Xl        X l        X0l        X@l        XPl        X`l        [        U R                  U R
                  U R                  -  SSS9U l	        [        U R                  U R
                  U R                  -  SSS9U l
        [        U R                  U R
                  U R                  -  SSS9U l        [        U R
                  U R                  -  U R                  SS9U l        SU l        U R                  (       a1  [        U R                  U R
                  U R                  -  SS9U l        [        R                  " 5       U l        g)a:  
Args:
    c_q:
        Input dimension of query data
    c_k:
        Input dimension of key data
    c_v:
        Input dimension of value data
    c_hidden:
        Per-head hidden dimension
    no_heads:
        Number of attention heads
    gating:
        Whether the output should be gated using query data
Fr   r   r   r   r   Nr   )r   r   r   r   r   r   r   r   r   linear_qlinear_klinear_vlinear_olinear_gr   Sigmoidsigmoid)r   r   r   r   r   r   r   r   s          rH   r   EsmFoldAttention.__init__<  s    0 	  
 &dhh0MTY`hi%dhh0MTY`hi%dhh0MTY`hi%dmmdmm&CTXXT[\;;)$((DMMDMM4QX`aDMzz|rG   q_xkv_xrX   c                 0   U R                  U5      nU R                  U5      nU R                  U5      nUR                  UR                  S S U R
                  S4-   5      nUR                  UR                  S S U R
                  S4-   5      nUR                  UR                  S S U R
                  S4-   5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nU[        R                  " U R                  5      -  nX4U4$ )Nry   r   )
r   r   r   viewri   r   	transposer   r   r   )r   r   r   qr`   r   s         rH   	_prep_qkvEsmFoldAttention._prep_qkvk  s    MM#MM$MM$ FF1773B<4==""556FF1773B<4==""556FF1773B<4==""556 KKBKKBKKB	TYYt}}%%QwrG   oc                     U R                   bQ  U R                  U R                  U5      5      nUR                  UR                  S S U R                  S4-   5      nX-  n[        US5      nU R                  U5      nU$ )Nry   r   )r   r   r   ri   r   r{   r   )r   r  r   gs       rH   _wrap_upEsmFoldAttention._wrap_up  su    ==$T]]3/0A qwws|t}}b&99:AA q!$ MM!rG   biasesuse_memory_efficient_kerneluse_lmalma_q_chunk_sizelma_kv_chunk_size	use_flash
flash_maskc
                    U(       a  Ub  Uc  [        S5      eU(       a  Ub  [        S5      eXEU/n
[        U
5      S:  a  [        S5      eUc  / nU R                  X5      u  pn[        US5      n[        R
                  " X5      nU H  nX-  nM	     [        US5      n[        R
                  " X5      nUR                  SS5      nU R                  X5      nU$ )	a  
Args:
    q_x:
        [*, Q, C_q] query data
    kv_x:
        [*, K, C_k] key data
    biases:
        List of biases that broadcast to [*, H, Q, K]
    use_memory_efficient_kernel:
        Whether to use a custom memory-efficient attention kernel. This should be the default choice for most.
        If none of the "use_<...>" flags are True, a stock PyTorch implementation is used instead
    use_lma:
        Whether to use low-memory attention (Staats & Rabe 2021). If none of the "use_<...>" flags are True, a
        stock PyTorch implementation is used instead
    lma_q_chunk_size:
        Query chunk size (for LMA)
    lma_kv_chunk_size:
        Key/Value chunk size (for LMA)
Returns
    [*, Q, C_q] attention update
zPIf use_lma is specified, lma_q_chunk_size and lma_kv_chunk_size must be providedzSuse_flash is incompatible with the bias option. For masking, use flash_mask insteadr   z2Choose at most one alternative attention algorithm)r   r   ry   r   r   )	r   sumr  r   rC   matmulr   r  r  )r   r   r   r
  r  r  r  r  r  r  attn_optionsquerykeyvalueoutputr   s                   rH   r   EsmFoldAttention.forward  s    B (04E4Mopp+rss3iH|q QRR>F !NN35E f- e)AKF  , f,!!"b)v+rG   )r   r   r   r   r   r   r   r   r   r   r   r   T)NFFi   i   FN)r>   r?   r@   rA   rB   r   r   r   rC   rd   r
   r  r  r   r   r   rF   r   r   s   @rH   r   r   7  s^    -$-$ -$ 	-$
 -$ -$ -$ -$^U\\  %V[VbVbdidpdpHpBq (%,, U\\ ell ( 04,1 $!%-1=\\= ll= ell+,	=
 &*= = = = = U\\*= 
= =rG   r   c                   L  ^  \ rS rSrSU 4S jjr\R                  R                     SS\R                  S\	\R                     S\
S\S\S\S	\R                  4S
 jj5       r     SS\R                  S\\R                     S\\
   S\S\S\S	\R                  4S jjrSrU =r$ )EsmFoldTriangleAttentioni  c                 R  > [         TU ]  5         Xl        X l        X0l        X@l        XPl        [        U R                  5      U l        [        XR                  SSS9U l
        [        U R                  U R                  U R                  U R                  U R                  5      U l        g)z
Args:
    c_in:
        Input channel dimension
    c_hidden:
        Overall hidden channel dimension (not per-head)
    no_heads:
        Number of attention heads
Fr   r   N)r   r   r   r   r   startinginfr   r   r   linearr   mha)r   r   r   r   r  r  r   s         rH   r   !EsmFoldTriangleAttention.__init__  sx     		   #DII.#D--e(S#DIItyy$))T]]TXTaTabrG   rn   r
  
chunk_sizer  r  inplace_saferX   c           
          UUUS.n[        [        U R                  XES9UU[        UR                  SS 5      U(       a  US9$ SS9$ )ztriangle! triangle!)r   r   r
  )r  r  Nr   )r#  no_batch_dims_out)r   r   r!  rc   ri   )r   rn   r
  r#  r  r  r$  
mha_inputss           rH   _chunkEsmFoldTriangleAttention._chunk  s^     

 DHH:Ug!aggcrl+"
 	

 )-
 	
rG   maskc           	         Uc  UR                  UR                  SS 5      nU R                  (       d$  UR                  SS5      nUR                  SS5      nU R	                  U5      nU R
                  US-
  -  SSS2SSSS24   n[        U R                  U5      S5      nUR                  S5      nXx/n	Ub  U R                  UU	UUUUS	9nOU R                  XXUS
9nU R                  (       d  UR                  SS5      nU$ )z}
Args:
    x:
        [*, I, J, C_in] input tensor (e.g. the pair representation)
Returns:
    [*, I, J, C_in] output tensor
Nry   r   r   r   .r   r   r   )r  r  r$  )r   r   r
  r  r  )new_onesri   r  r  r   r  r   r   	unsqueezer)  r!  )
r   rn   r+  r#  r  r  r$  	mask_biastriangle_biasr
  s
             rH   r    EsmFoldTriangleAttention.forward  s     <::D }}B#A>>"b)D OOA XX*CD$,AB	 +4;;q>9E &//3+!,G)  A fov  A }}B#ArG   )r   r   r  r   r   r!  r   r  )T    eA)FFF)NNFFF)r>   r?   r@   rA   r   rC   jitignorerd   r   r   r   r)  r   r   rF   r   r   s   @rH   r  r    s    c0 YY -2"
<<
 U\\"
 	

 &*
 
 
 

 
6 (,$(,1"9<<9 u||$9 SM	9
 &*9 9 9 
9 9rG   r  c                   `  ^  \ rS rSrSrSU 4S jjr SS\R                  S\R                  S\\	   S\R                  4S jjr
   SS	\R                  S
\\R                     S\\	   S\4S jjr    SS	\R                  S
\\R                     S\S\S\\	   S\R                  4S jjrSrU =r$ )#EsmFoldTriangleMultiplicativeUpdatei=  z"
Implements Algorithms 11 and 12.
c                 r  > [         TU ]  5         UR                  nX l        [	        X35      U l        [	        X3SS9U l        [	        X35      U l        [	        X3SS9U l        [	        X3SS9U l	        [	        X3SS9U l
        [        U5      U l        [        U5      U l        [        R                  " 5       U l        g )Nr   r   r   )r   r   pairwise_state_dim	_outgoingr   
linear_a_p
linear_a_g
linear_b_p
linear_b_gr   linear_zr   layer_norm_inlayer_norm_outr   r   r   )r   configr;  r   r   s       rH   r   ,EsmFoldTriangleMultiplicativeUpdate.__init__B  s    ,,"';'J';'J%hxH%hwG&x0'1zz|rG   r   r   _inplace_chunk_sizerX   c                    U R                   (       a  [        US5      n[        US5      nO[        US5      n[        US5      nUbl  [        SUR                  S   U5       HK  nUSXDU-   2S S 2S S 24   nUSXDU-   2S S 2S S 24   n[        R
                  " UU5      USXDU-   2S S 2S S 24'   MM     UnO[        R
                  " X5      n[        US5      $ )Nr-  )r   r   r   r   r   .r   r   r   )r;  r   rm   ri   rC   r  )r   r   r   rE  rr   a_chunkb_chunkps           rH   _combine_projections8EsmFoldTriangleMultiplicativeUpdate._combine_projectionsS  s     >>"1i0A"1i0A"1i0A"1i0A*1aggbk+>?C)<%<!<aBCC)<%<!<aBC<ALL=#q222Aq89 @ AQ"A!!Y//rG   zr+  inplace_chunk_sizewith_addc                   ^ ^^#^$^%^&^'^(^) Uc  UR                  UR                  SS 5      nUR                  S5      nSU 4S jjm$SU$UU 4S jjnU" XSSS9nTGb  UR                  S   m'T'S-  T'S-  -   m&Sm(S	m#T R                  (       a  T(OT#nS
 m%U%4S jm)U#U%U&U'U(U)4S jn[	        UR                  5      n	T&U	T#'   UR                  U	5      n
T%" U
5      n[        ST&5      UT#'   U
R                  X   5        Sn[	        [        ST&T5      5      n[        XSS T&/-   5       VVs/ s H	  u  pX-
  PM     nnn[	        [        T&T'T5      5      nU Vs/ s H  nTPM     nn[        UU-   UU-   5      nU GHk  u  nnU(       d  UT&:  a
  U" X5      n
SnT)" UUUU-   U5      nT)" UUUU-   U5      nUR                  5       nUT#:X  a  T)" UUUU-   T#5      nOCU(       d)  T%" U5      n[        ST&5      UT#'   T)" U
UUU-   T(5      UU'   OUT&-
  nT)" U
UUU-   T(5      nU" UUSSS9nA[        R                  " UU5      n[        US5      nT R                  U5      nT R                  U5      nT)" UUUU-   T#5      nT R!                  T R#                  U5      5      nUR%                  5         AUU-  nT%" U5      n[        UUU-   5      UT#'   U(       a  UU==   U-  ss'   GMf  UUU'   GMn     U$ U" XSS5      n [        R                  " UU 5      n!T R                  U!5      n!T R                  U!5      n!T R!                  U5      n"U"R%                  5         U!U"-  n!U(       a  UU!-  nU$ U!nU$ s  snnf s  snf )aa  
Args:
    z:
        A [*, N, N, C_z] pair representation
    mask:
        A [*, N, N] pair mask
    inplace_chunk_size:
        Size of chunks used in the main computation. Increase to trade memory for speed.
    with_add:
        If True, z is overwritten with (z + update). Otherwise, it is overwritten with (update).
Returns:
    A reference to the overwritten z

More memory-efficient, inference-only version of the forward function. Uses in-place operations, fusion of the
addition that happens after this module in the Evoformer, a smidge of recomputation, and a cache of overwritten
values to lower peak memory consumption of this module from 5x the size of the input tensor z to 2.5x its size.
Useful for inference on extremely long sequences.

It works as follows. We will make reference to variables used in the default forward implementation below.
Naively, triangle multiplication attention requires the manifestation of 5 tensors the size of z: 1) z, the
"square" input tensor, 2) a, the first projection of z, 3) b, the second projection of b, 4) g, a z-sized mask,
and 5) a z-sized tensor for intermediate computations. For large N, this is prohibitively expensive; for
N=4000, for example, z is more than 8GB alone. To avoid this problem, we compute b, g, and all intermediate
tensors in small chunks, noting that the chunks required to compute a chunk of the output depend only on the
tensor a and corresponding vertical and horizontal chunks of z. This suggests an algorithm that loops over
pairs of chunks of z: hereafter "columns" and "rows" of z, even though each "column" and "row" in fact contains
inplace_chunk_size contiguous true columns and rows of z. Writing output chunks to a new tensor would bring
total memory consumption down to 3x the size of z. However, more memory can be saved by writing output chunks
directly to z in-place. WLOG, we choose to write output chunks vertically, overwriting the ith "column" of z at
the end of the ith iteration of the main loop. Despite this overwriting, the ith column is always one column
ahead of previously overwritten columns and can be recovered directly from z. After the first iteration,
however, the ith row of z is always at least partially overwritten. For this reason, we introduce the z-cache,
a tensor one-half the size of z. The z-cache initially contains the left half (2nd and 3rd quadrants) of z. For
0 < i < N/2, the missing left part of the ith row of z is recovered from this cache at the beginning of the ith
iteration. Once i exceeds n/2, the cache is "reoriented" to encompass the 3rd and 4th quadrants of z instead.
Though the 3rd quadrant of the original z is entirely overwritten at this point, it can be recovered from the
z-cache itself. Thereafter, the ith row of z can be recovered in its entirety from the reoriented z-cache.
After the final iteration, z has been completely overwritten and contains the triangular multiplicative update.
If with_add is True, it instead contains the sum of z and the triangular multiplicative update. In either case,
peak memory consumption is just 2.5x the size of z, disregarding memory used for chunks and other small
variables.
Nry   Tc                    > U(       a  TR                   nTR                  nOTR                  nTR                  nTR	                  U 5      n U" U 5      nUR                  5         XT" U 5      -  nXQ-  n[        US5      nU$ )Nr-  )r=  r<  r?  r>  rA  sigmoid_r   )pairr+  r   r   linear_prJ  r   s         rH   compute_projection_helperYEsmFoldTriangleMultiplicativeUpdate._inference_forward.<locals>.compute_projection_helper  sr    ????????%%d+DAJJL$AIA"1i0AHrG   c           
      l  > TR                   U-  nU(       d$  T" XU5      nU(       a  UR                  SS5      nU$ U(       a  TR                  OTR                  nUR                  R
                  S   nU R
                  S S U4-   U R
                  SS -   nU R                  U5      n[        SU R
                  S   T5       Hn  n	U SXT-   2S S 2S S 24   n
T" U SXT-   2S S 2S S 24   USXT-   2S S 2S S 24   U5      n
U(       a  U
R                  SS5      n
XSXT-   24'   OXSXT-   2S S 24'   A
Mp     U$ )Nry   r   r   r   .)r;  r  r=  r?  r   ri   	new_zerosrm   )rS  r+  r   chunkedneed_transposerJ  r   c	out_shaperr   
pair_chunkrU  rN  r   s              rH   compute_projectionREsmFoldTriangleMultiplicativeUpdate._inference_forward.<locals>.compute_projection  s^   !^^a/N-d!<!B+A. H' /04??T__MM''+ JJsOqd2TZZ25FF	NN9-q$**R.2DEA!%c13E/E+Eq!&K!LJ!:S!*<&<"<aBCS!*<&<"<aBC"J
 &%/%9%9"b%A
=G#q'9#999:@J#q'9#991<=" F HrG   )rY  r   r   r   c                 X    U R                    Vs/ s H  n[        S 5      PM     sn$ s  snf rJ   )ri   r^   )rt   _s     rH   empty_slicerLEsmFoldTriangleMultiplicativeUpdate._inference_forward.<locals>.empty_slicer  s"    -.WW5WdW555s   'c                 6   > T" U 5      n[        X5      XC'   X   $ rJ   r]   )rt   startendre   r   rb  s        rH   slice_tensorLEsmFoldTriangleMultiplicativeUpdate._inference_forward.<locals>.slice_tensor  s     Ou*trG   c                    > T" U TS T
5      nU R                  T
T5      n U SS T	S-  2S S 2S S 24   n T" U 5      n[        ST5      UT'   X U'   T" UTS T
5      nT" UTS T5      nT" U 5      n[        TS 5      UT'   X@U'   U $ )N.r   r   )r  r^   )z_cacherM  
quadrant_3first_half_slicer
quadrant_4quadrant_3_slicercol_dimrb  half_nnrow_dimrg  s         rH   flip_z_cache_MEsmFoldTriangleMultiplicativeUpdate._inference_forward.<locals>.flip_z_cache_  s     *'64I
!++GW= "#z!q&z1a"78 %1$9!-21f-=!'*-7)* *!VT7C
)*fdGL
 %1$9!-264-@!'*-7)*rG   r   Fr   )r   rY  rG  r  )TT)r/  ri   r0  r;  r   rX  r^   r   rm   rh   clonerC   r  r   rB  r@  r   rA  rR  )*r   rM  r+  rN  rO  r^  r   b_chunk_dimrs  z_cache_shaperj  z_cache_slicerz_cache_rotatedi_rangei_1i_2initial_offsets
after_halfra  after_half_offsetscombined_range_with_offsetsrr   offset	z_chunk_b
mask_chunkz_chunk_slicerz_cache_offsetrI  x_chunk	z_chunk_gg_chunkz_slicerr   rn   r  ro  rU  rb  rp  rq  rr  rg  s*   `  `                               @@@@@@@rH   _inference_forward6EsmFoldTriangleMultiplicativeUpdate._inference_forwardm  s   b <::aggcrl+D~~b!	 	 	B qd;)A!Va!e^FGG%)^^'K6 8 !MM%+M'"kk-0G)'2N&+Av&6N7#MM!+,#O
 5F,>?@G9<WabkU[T\F\9]^9]XSsy9]O^eFA/ABCJ>H!Ij"4j!I*-g
.BOVhDh*i'8	6&1;+G7G&*O(Aq6z;G	)$1v:{K
%OO-	') ,Q1v:w GI
 +)5i)@2762Bw/4@!QQWZY`4a	.1)*V$0..[aJacj$k	,Y
eUZ[,,q'2,Wi@--g6--0 )Aq6z7C	--(:(:9(EF  "7" (?$)!QZ$8!hK7*K")AhK] 9x  #1E59AQ"A##A&Aa Aa AJJLFAQ  A _!Is   #MM%r$  _add_with_inplacec                 N   U(       a  U R                  UUUUS9nU$ Uc  UR                  UR                  SS 5      nUR                  S5      nU R	                  U5      nUnXpR                  U R                  U5      5      -  nXpR                  U5      -  nUnXR                  U R                  U5      5      -  nXR                  U5      -  n[        5       (       a_  [        R                  R                  R                  SS9   U R                  UR!                  5       UR!                  5       5      nSSS5        OU R                  Xx5      nAAU R#                  W5      nU R%                  U5      nU R                  U R'                  U5      5      n	Xi-  nU$ ! , (       d  f       NX= f)z
Args:
    x:
        [*, N_res, N_res, C_z] input tensor
    mask:
        [*, N_res, N_res] input mask
Returns:
    [*, N_res, N_res, C_z] output tensor
)rN  rO  Nry   Fr   )r  r/  ri   r0  rA  r   r=  r<  r?  r>  rO   rC   r   r   r   rK  floatrB  r@  r   )
r   rM  r+  r$  r  rE  rn   r   r   r  s
             rH   r   +EsmFoldTriangleMultiplicativeUpdate.forwardP  ss   " ''#6*	 ( A H<::aggcrl+D~~b!q!T__Q/00""T__Q/00""(((7--aggiC 87 ))!/Aq"MM!LLq)*E 87s   /F
F$)
r;  rA  rB  r=  r<  r?  r>  r   r@  r   r  rJ   )NNT)NFF   )r>   r?   r@   rA   rB   r   rC   rd   r   r   rK  r   r  r   rF   r   r   s   @rH   r8  r8  =  s    $$ VZ00"',,0EMc]0	0: (,,0a<<a u||$a %SM	a
 aL (,""'-03<<3 u||$3 	3
  3 &c]3 
3 3rG   r8  c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )EsmFoldPreTrainedModeli  zz
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
models.
c                   > [        U[        5      (       Ga  [        R                  " 5          UR                  b(  UR	                  UR
                  UR                  5        GOJUR                  S:X  a  [        UR
                  SS9  GO$UR                  S:X  a  [        UR
                  SS9  OUR                  S:X  a)  [        R                  R                  UR
                  SS	9  OUR                  S
:X  aH  UR
                  R                  S5        UR                  (       a  UR                  R                  S5        OnUR                  S:X  a3  [        R                  R                  R                  UR
                  SS9  O+UR                  S:X  a  UR
                  R                  S5        SSS5        g[        U[        5      (       a  [        UR                  5        g[        U[         5      (       Ga  [        R                  R                  R#                  UR$                  R&                  R
                  5        [        R                  R                  R#                  UR$                  R&                  R                  5        [        R                  R                  R#                  UR(                  R&                  R
                  5        [        R                  R                  R#                  UR(                  R&                  R                  5        [        R                  R                  R#                  UR*                  R,                  R.                  R
                  5        [        R                  R                  R#                  UR*                  R,                  R.                  R                  5        [        R                  R                  R#                  UR0                  R,                  R.                  R
                  5        [        R                  R                  R#                  UR0                  R,                  R.                  R                  5        [        R                  R                  R#                  UR2                  R4                  R
                  5        [        R                  R                  R#                  UR2                  R4                  R                  5        [        R                  R                  R#                  UR6                  R8                  R
                  5        [        R                  R                  R#                  UR:                  R4                  R
                  5        [        R                  R                  R#                  UR:                  R4                  R                  5        [        R                  R                  R#                  UR<                  R>                  S   R
                  5        [        R                  R                  R#                  UR<                  R>                  S   R                  5        [        R                  R                  R#                  UR@                  R>                  S   R
                  5        [        R                  R                  R#                  UR@                  R>                  S   R                  5        g[B        TU ]  U5        g! , (       d  f       g= f)zInitialize the weightsNr         ?)r   r   r   r   r   )gainr   r   r   r   )nonlinearityr   r   )#r   r   rC   r   r   r   r   r   r   r   xavier_uniform_rl   kaiming_normal_EsmFoldInvariantPointAttentionr   head_weights#EsmFoldTriangularSelfAttentionBlockzeros_
tri_mul_inr@  tri_mul_outtri_att_startr!  r   tri_att_endsequence_to_pairo_projpair_to_sequencer   seq_attentionmlp_seqmlpmlp_pairr   _init_weights)r   moduler   s     rH   r  $EsmFoldPreTrainedModel._init_weights  s*   fm,,>>-NN6==&++>[[I-&v}}C@[[F*&v}}C@[[H,GG++FMM+B[[H,MM'',{{))#.[[H,HHMM11&--h1W[[G+MM'',! !"  >??#F$7$78 CDDHHMM  !2!2!;!;!B!BCHHMM  !2!2!;!;!@!@AHHMM  !3!3!<!<!C!CDHHMM  !3!3!<!<!A!ABHHMM  !5!5!9!9!B!B!I!IJHHMM  !5!5!9!9!B!B!G!GHHHMM  !3!3!7!7!@!@!G!GHHHMM  !3!3!7!7!@!@!E!EFHHMM  !8!8!?!?!F!FGHHMM  !8!8!?!?!D!DEHHMM  !8!8!?!?!F!FGHHMM  !5!5!<!<!C!CDHHMM  !5!5!<!<!A!ABHHMM  !3!3B!7!>!>?HHMM  !3!3B!7!<!<=HHMM  !4!4R!8!?!?@HHMM  !4!4R!8!=!=>G!&)O !s   F [33
\r=   )r>   r?   r@   rA   rB   r  rF   r   r   s   @rH   r  r    s    ** **rG   r  c                   6   ^  \ rS rSrSU 4S jjrSS jrSrU =r$ )EsmFoldSelfAttentioni  c                   > [         TU ]  5         XU-  :X  d   eXl        X l        X0l        [
        R                  " XS-  SS9U l        [
        R                  " XSS9U l        X@l	        U(       a  [
        R                  " X5      U l
        [        R
                  R                  R                  U R                  R                  5        [        R
                  R                  R                  U R                  R                   5        U R                  S-  U l        [        R
                  R                  R                  U R                  R                   5        g )Nr   Fr   T      )r   r   	embed_dim	num_heads
head_widthr   Linearprojr  gatedg_projrC   r   r  r   ones_r   rescale_factor)r   r  r  r  r  r   s        rH   r   EsmFoldSelfAttention.__init__  s    
2222""$IIiQUC	ii	4@
))I9DKHHMM  !3!34HHMM 0 01"oot3T[[--.rG   c                    U R                  U5      R                  " / UR                  SS QU R                  PSP76 nUR	                  SSSS5      nUR                  SSS9u  pgnU R                  U-  n[        R                  " SXg5      n	Ub  XR	                  SSSS5      -   n	Ub.  USS2SS4   nU	R                  US	:H  [        R                  * 5      n	[        R                  R                  U	SS9n	[        R                  " S
X5      n
U
R                  " / U
R                  SS QSP76 n
U R                   (       a"  U R#                  U5      R%                  5       U
-  n
U R'                  U
5      n
XR	                  SSSS5      4$ )a  
Basic self attention with optional mask and external pairwise bias. To handle sequences of different lengths,
use mask.

Inputs:
    x: batch of input sequences (.. x L x C) mask: batch of boolean masks where 1=valid, 0=padding position (..
    x L_k) bias: batch of scalar pairwise attention biases (.. x Lq x Lk x num_heads)

Outputs:
  sequence projection (B x L x embed_dim), attention maps (B x L x L x num_heads)
Nr   ry   r   r   r   r   z...qc,...kc->...qkFz...hqk,...hkc->...qhc)r  r   ri   r  r   chunkr  rC   einsummasked_fillr   r  r   r   r   rz   r  r  r   r  )r   rn   r+  r   indicesrt   r  r`   r   r   ys              rH   r   EsmFoldSelfAttention.forward  sd    IIaL?qwwr{?DNN?B?IIaAq!''!'$a!#LL-q4 LLAq!,,A 4&DdembffW5AMM!!!!,LL0!7II'qwwr{'B'::A&&(1,AKKN))Aq!Q'''rG   )r  r  r  r  r  r  r  r  )F)NNNr   r   s   @rH   r  r    s    /(&( &(rG   r  c                      ^  \ rS rSrSrS\S\\\\   4   4U 4S jjr	S\
R                  S\
R                  4S jrS	rU =r$ )
EsmFoldDropouti  zd
Implementation of dropout with the ability to share the dropout mask along a particular dimension.
r	batch_dimc                    > [         TU ]  5         Xl        [        U[        5      (       a  U/nX l        [        R                  " U R                  5      U l        g rJ   )	r   r   r  r   r   r  r   Dropoutdropout)r   r  r  r   s      rH   r   EsmFoldDropout.__init__  s@    i%%"I"zz$&&)rG   rn   rX   c                     [        UR                  5      nU R                  b  U R                   H  nSX#'   M	     XR                  UR	                  U5      5      -  $ )Nr   )r   ri   r  r  r/  )r   rn   ri   bds       rH   r   EsmFoldDropout.forward  sI    QWW>>%nn	 %<<

5 1222rG   )r  r  r  )r>   r?   r@   rA   rB   r  r   r   r   r   rC   rd   r   rF   r   r   s   @rH   r  r    sH    *% *E#tCy.,A *3 3%,, 3 3rG   r  c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )EsmFoldSequenceToPairi  c                   > [         TU ]  5         [        R                  " U5      U l        [        R
                  " XS-  SS9U l        [        R
                  " SU-  USS9U l        [        R                  R                  R                  U R                  R                  5        [        R                  R                  R                  U R                  R                  5        g )Nr   Tr   )r   r   r   r   	layernormr  r  r  rC   r   r  r   )r   sequence_state_dim	inner_dimr:  r   s       rH   r   EsmFoldSequenceToPair.__init__  s    &89II0a-dK	iiI/AMTYY^^,T[[--.rG   c                 n   [        UR                  5      S:X  d   eU R                  U5      nU R                  U5      nUR	                  SSS9u  p4USS2SSS2SS24   USS2SS2SSS24   -  nUSS2SSS2SS24   USS2SS2SSS24   -
  n[
        R                  " XV/SS9nU R                  U5      nU$ )z
Inputs:
  sequence_state: B x L x sequence_state_dim

Output:
  pairwise_state: B x L x L x pairwise_state_dim

Intermediate state:
  B x L x L x 2*inner_dim
r   r   ry   r   N)rc   ri   r  r  r  rC   catr  )r   sequence_stater   r  r`   proddiffrn   s           rH   r   EsmFoldSequenceToPair.forward  s     >''(A---NN>*IIaLwwqbw!D!Q!Aq$M"22D!Q!Aq$M"22IItl+KKNrG   )r  r  r  r   r   s   @rH   r  r    s    / rG   r  c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )EsmFoldPairToSequencei4  c                    > [         TU ]  5         [        R                  " U5      U l        [        R
                  " XSS9U l        g )NFr   )r   r   r   r   r  r  r   )r   r:  r  r   s      rH   r   EsmFoldPairToSequence.__init__5  s2    &89ii 2EJrG   c                     [        UR                  5      S:X  d   eU R                  U5      nU R                  U5      nU$ )zj
Inputs:
  pairwise_state: B x L x L x pairwise_state_dim

Output:
  pairwise_bias: B x L x L x num_heads
   )rc   ri   r  r   )r   pairwise_staterM  pairwise_biass       rH   r   EsmFoldPairToSequence.forward;  s>     >''(A---NN>*ArG   )r  r   r   r   s   @rH   r  r  4  s    K rG   r  c                   2   ^  \ rS rSrSU 4S jjrS rSrU =r$ )EsmFoldResidueMLPiI  c           	      &  > [         TU ]  5         [        R                  " [        R                  " U5      [        R
                  " X5      [        R                  " 5       [        R
                  " X!5      [        R                  " U5      5      U l        g rJ   )	r   r   r   
Sequentialr   r  ReLUr  r  )r   r  r  r  r   s       rH   r   EsmFoldResidueMLP.__init__J  sW    ==LL#IIi+GGIIIi+JJw
rG   c                 (    XR                  U5      -   $ rJ   r  )r   rn   s     rH   r   EsmFoldResidueMLP.forwardU  s    88A;rG   r  r   r   r   s   @rH   r  r  I  s    	
 rG   r  c                   2   ^  \ rS rSrU 4S jrSS jrSrU =r$ )r  iY  c                 &  > [         TU ]  5         Xl        UR                  nUR                  nX!R
                  -  nX1R                  -  n[        R                  " U5      U l	        [        X#S-  U5      U l        [        X45      U l        [        X$UR
                  SS9U l        [!        USS9U l        [!        USS9U l        ['        X1R                  USSS9U l        ['        X1R                  USSS9U l        [-        USU-  UR.                  S	9U l        [-        USU-  UR.                  S	9U l        [        R4                  " UR.                  5      U l        [9        UR.                  S-  S5      U l        [9        UR.                  S-  S
5      U l        g )Nr   T)r  )r;  Fr4  )r  r  r  )r  r   )r   r   rC  r  r:  sequence_head_widthpairwise_head_widthr   r   layernorm_1r  r  r  r  r  r  r8  r  r  r  r  r  r  r  r  r  r  dropr  row_dropcol_drop)r   rC  r  r:  sequence_num_headspairwise_num_headsr   s         rH   r   ,EsmFoldTriangularSelfAttentionBlock.__init__Z  s|   #66#66/3M3MM/3M3MM<<(:; 56H`aJacu v 56H ]1F4N4NVZ
 ?vQUV=fPUV5 : :<NTWbf
 4 : :<NTWbg
 ));QAS=S]c]k]kl)*<aBT>T^d^l^lmJJv~~.	&v~~'91=&v~~'91=rG   c           	         [        UR                  5      S:w  a"  [        S[        UR                  5       S35      e[        UR                  5      S:w  a"  [        S[        UR                  5       S35      eUb;  [        UR                  5      S:w  a"  [        S[        UR                  5       S35      eUR                  u  pgnUR                  S   n	XR                  R                  :w  a&  [        S	U S
U R                  R                   S35      eXR                  R
                  :w  a&  [        SU	 S
U R                  R
                   S35      eXbR                  S   :w  a  [        SU S
UR                  S    S35      eXrR                  S   :w  d  XrR                  S   :w  a/  [        SU S
UR                  S    SUR                  S    S35      eU R                  U5      n
U R                  U5      nU R                  XU
S9u  pXR                  U5      -   nU R                  U5      nX R                  U5      -   nUb#  UR                  S5      UR                  S5      -  OSnX R                  U R                  X-S95      -   nX R                  U R!                  X-S95      -   nX R                  U R#                  X-US95      -   nX R                  U R%                  X-US95      -   nU R'                  U5      nX4$ )z
Inputs:
  sequence_state: B x L x sequence_state_dim pairwise_state: B x L x L x pairwise_state_dim mask: B x L boolean
  tensor of valid positions

Output:
  sequence_state: B x L x sequence_state_dim pairwise_state: B x L x L x pairwise_state_dim
r   z,`sequence_state` should be a 3d-tensor, got  dims.r  z,`pairwise_state` should be a 4d-tensor, got Nr   "`mask` should be a 2d-tensor, got zR`sequence_state` last dimension should be equal to `self.sequence_state_dim`. Got  != .zR`pairwise_state` last dimension should be equal to `self.pairwise_state_dim`. Got r   zD`sequence_state` and `pairwise_state` have inconsistent batch size: r   zI`sequence_state` and `pairwise_state` have inconsistent sequence length: z or )r+  r   r+  )r+  r#  )rc   ri   r   rC  r  r:  r  r  r  r  r  r  r0  r  r  r  r  r  r  r  )r   r  r  r+  r#  ,_EsmFoldTriangularSelfAttentionBlock__kwargsr  seq_dimr  r:  r   r  ra  tri_masks                 rH   r   +EsmFoldTriangularSelfAttentionBlock.forward|  s)    ~##$)KCP^PdPdLeKfflmnn~##$)KCP^PdPdLeKfflmnnDJJ1 4A#djj/ARRXYZZ1?1E1E.	.+11!4!?!??d%&d4;;+I+I*J!M  !?!??d%&d4;;+I+I*J!M  ,,Q//VW`Vaae!''*+1.  **1--<P<PQR<S1S[\c[ddh!''*+40D0DQ0G/HK  $$^4 ^,!!!T!:'))A,6n5 (*?*?*OO =A<L4>>!$t~~a'88RV'--8H8H8H8g*hh'--8f*gg'--~T+
 
 (--^zR+
 

 ~6--rG   )r  rC  r  r  r  r  r  r  r  r  r  r  r  r  )NNr   r   s   @rH   r  r  Y  s     >DB. B.rG   r  c                   *    \ rS rSrSS jrS rS rSrg)EsmCategoricalMixturei  c                     Xl         [        R                  " X4US-   U R                   R                  U R                   R                  S9nUS S USS  -   S-  U l        g )Nr   r[   rZ   ry   r   )logitsrC   linspacer[   rZ   v_bins)r   parambinsre  rf  s        rH   r   EsmCategoricalMixture.__init__  sS    ~~e$(4;;;M;MUYU`U`UfUfgCRy48+q0rG   c                 6   UR                  S5      U R                  S /UR                  -     -
  R                  5       R	                  S5      nU R
                  R                  S5      n[        R                  " X2R                  S5      SS9R                  S5      $ )Nry   r   )
r0  r  ndimabsargminr  log_softmaxrC   take_along_dimsqueeze)r   true
true_indexnlls       rH   log_probEsmCategoricalMixture.log_prob  s~     nnR(4;;v		7I+JJOOQXXY[\
kk%%b)##C)=)=b)ArJRRSUVVrG   c                     U R                   R                  S5      U R                  R                  S5      -  R	                  S5      $ )Nry   r   )r  r   r  r0  r  )r   s    rH   meanEsmCategoricalMixture.mean  s6    ##B'$++*?*?*BBKKBOOrG   )r  r  N)2   r   r   )r>   r?   r@   rA   r   r  r  rF   r=   rG   rH   r  r    s    1WPrG   r  c                 0    [        XS9R                  5       $ )Nr  )r  r  )r  r  s     rH   categorical_lddtr    s     388::rG   c                 
   U c  g[        U R                  5      S:w  a"  [        S[        U R                  5       S35      eU R                  u  pU R                  S5      R	                  XU5      nUR                  X-  U5      nU$ )z
Helper to convert B x L mask of valid positions to axial mask used in row column attentions.

Input:
  mask: B x L tensor of booleans

Output:
  mask: B x L x L tensor of booleans
Nr   r  r  r   )rc   ri   r   r0  expandrz   )r+  r  r  ms       rH   get_axial_maskr     sy     |
4::!=c$**o=NfUVVIq  W=A			)%w/AHrG   c                   2   ^  \ rS rSrU 4S jrSS jrSrU =r$ )EsmFoldRelativePositioni  c                    > [         TU ]  5         UR                  U l        [        R
                  R                  SU R                  -  S-   UR                  5      U l        g Nr   )	r   r   position_binsr  rC   r   	Embeddingr:  	embeddingr   rC  r   s     rH   r    EsmFoldRelativePosition.__init__  sH    ((	 ++A		MA,=v?X?XYrG   c                    UR                   [        R                  :w  a  [        SUR                    S35      eUb@  UR                  UR                  :w  a&  [        SUR                   SUR                   S35      eUSS2SSS24   USS2SS2S4   -
  nUR                  U R                  * U R                  5      nX0R                  -   S-   nUb   USS2SSS24   USS2SS2S4   -  nSX2S	:H  '   U R                  U5      nU$ )
z
Input:
  residue_index: B x L tensor of indices (dtype=torch.long) mask: B x L tensor of booleans

Output:
  pairwise_state: B x L x L x pairwise_state_dim tensor of embeddings
z`residue_index` has dtype z, it should be `torch.long`.Nz5`residue_index` and `mask` have inconsistent shapes: r  r  r   r   F)rZ   rC   longr   ri   r   r  r'  )r   r5   r+  r  r  s        rH   r   EsmFoldRelativePosition.forward  s    %**,9-:M:M9NNjkll 3 3tzz AGH[H[G\\`aeakak`llmn  QaZ(=At+DDzz499*dii0ii!#4
#d1a:&66D"#D%rG   )r  r'  rJ   r   r   s   @rH   r"  r"    s    Z rG   r"  c                   b   ^  \ rS rSrU 4S jrS\R                  S\R                  4S jrSrU =r	$ )EsmFoldAngleResnetBlocki  c                    > [         TU ]  5         [        UR                  UR                  SS9U l        [        UR                  UR                  SS9U l        [        R                  " 5       U l        g Nr   r   r   )	r   r   r   
resnet_dimlinear_1linear_2r   r  r   r(  s     rH   r    EsmFoldAngleResnetBlock.__init__  sU    %f&7&79J9JQWX%f&7&79J9JQXYGGI	rG   r   rX   c                     UnU R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nX-   $ rJ   )r   r2  r3  )r   r   	s_initials      rH   r   EsmFoldAngleResnetBlock.forward  sE    	IIaLMM!IIaLMM!}rG   )r2  r3  r   )
r>   r?   r@   rA   r   rC   rd   r   rF   r   r   s   @rH   r.  r.    s(     %,,  rG   r.  c                      ^  \ rS rSrSrU 4S jrS\R                  S\R                  S\\R                  \R                  4   4S jr	Sr
U =r$ )	EsmFoldAngleResneti'  z&
Implements Algorithm 20, lines 11-14
c                   > [         TU ]  5         Xl        [        UR                  UR
                  5      U l        [        UR                  UR
                  5      U l        [        R                  " 5       U l
        [        UR                  5       H)  n[        U5      nU R                  R                  U5        M+     [        UR
                  UR                  S-  5      U l        [        R"                  " 5       U l        g r$  )r   r   rC  r   sequence_dimr1  	linear_inlinear_initialr   
ModuleListlayersrm   num_resnet_blocksr.  append
num_angles
linear_outr  r   )r   rC  ra  layerr   s       rH   r   EsmFoldAngleResnet.__init__,  s    &v':':F<M<MN+F,?,?ARARSmmov//0A+F3EKKu% 1 ((9(96;L;Lq;PQGGI	rG   r   r6  rX   c           
         U R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nX-   nU R                   H  nU" U5      nM     U R                  U5      nU R	                  U5      nUR                  UR                  SS S-   5      nUn[        R                  " [        R                  " [        R                  " US-  SSS9U R                  R                  S95      nX-  nXA4$ )z
Args:
    s:
        [*, C_hidden] single embedding
    s_initial:
        [*, C_hidden] single embedding as of the start of the StructureModule
Returns:
    [*, no_angles, 2] predicted angles
Nry   )ry   r   r   T)re   keepdim)r   )r   r=  r<  r?  rC  r   ri   rC   r   r   r  rC  epsilon)r   r   r6  lunnormalized_s
norm_denoms         rH   r   EsmFoldAngleResnet.forward<  s     IIi(	''	2	IIaLNN1MA!A  IIaL OOA FF1773B<')*ZZKK		!Q$B5KK''

 N  rG   )rC  r?  r<  r=  rC  r   r>   r?   r@   rA   rB   r   rC   rd   r
   r   rF   r   r   s   @rH   r9  r9  '  sI     )! )!%,, )!5W\WcWcIcCd )! )!rG   r9  c                      ^  \ rS rSrSrU 4S jr  SS\R                  S\\R                     S\	S\R                  S\
S	\\\R                        S
\R                  4S jjrSrU =r$ )r  ih  z
Implements Algorithm 22.
c                   > [         TU ]  5         Xl        UR                  nUR                  nUR
                  U l        UR                  U l        UR                  U l	        UR                  U l
        UR
                  UR                  -  n[        X$5      U l        [        USU-  5      U l        UR                  UR                  -  S-  n[        X%5      U l        UR                  UR                  UR                  -   -  S-  n[        X&5      U l        [        X1R                  5      U l        ["        R$                  " [&        R(                  " UR                  5      5      U l        UR                  X1R
                  -   UR                  S-  -   -  n[        XrSS9U l        ["        R.                  " SS9U l        ["        R2                  " 5       U l        g )Nr   r   r  r   r   ry   r   )r   r   rC  r;  pairwise_dimipa_dim
hidden_dimnum_heads_ipar  num_qk_pointsnum_v_pointsr   r   	linear_kvlinear_q_pointslinear_kv_pointslinear_br   r   rC   r   r  rC  Softmaxr   Softplussoftplus)	r   rC  c_sc_zhchpqhpkvconcat_out_dimr   s	           rH   r   'EsmFoldInvariantPointAttention.__init__m  sr   !!!! ..--#11"// ^^f222%c.&sAF3""V%9%99A=,S6##v';';f>Q>Q'QRUVV -c 8%c+?+?@LLf6J6J)LM--~~1EH[H[^_H_1_`''Jzzb)rG   r   rM  r  r+  _offload_inference_z_reference_listrX   c           
      &   U/nU R                  U5      nU R                  U5      nUR                  UR                  SS U R                  S4-   5      nUR                  UR                  SS U R                  S4-   5      n[
        R                  " XR                  SS9u  pU R                  U5      n[
        R                  " XR                  S   S-  SS9n[
        R                  " USS9nUS   R                  U5      nUR                  UR                  SS U R                  U R                  S4-   5      nU R                  U5      n[
        R                  " XR                  S   S-  SS9n[
        R                  " USS9nUS   R                  U5      nUR                  UR                  SS U R                  SS4-   5      n[
        R                  " XR                  U R                  /SS9u  pU R                  US   5      nU(       a5  [        R                   " US   5      S:X  d   eUS   R#                  5       US'   [%        5       (       ax  [
        R&                  R(                  R+                  S	S
9   [
        R,                  " [/        UR1                  5       S5      [/        U	R1                  5       S5      5      nSSS5        O+[
        R,                  " [/        US5      [/        U	S5      5      nW[2        R4                  " SSU R                  -  -  5      -  nU[2        R4                  " S5      [/        US5      -  -  nUR7                  S5      UR7                  S5      -
  nUS-  n[9        [
        R:                  " USS95      nU R=                  U R>                  5      R                  " S[A        UR                  SS 5      -  S-   6 nU[2        R4                  " SSU R                  S-  S-  -  -  5      -  nUU-  n[
        R8                  " USS9S-  nUR7                  S5      UR7                  S5      -  nU RB                  RD                  US-
  -  n[/        US5      nUU-   nUUR7                  S5      -   nU RG                  U5      n[
        R,                  " UU
RI                  SS5      RK                  URL                  S95      RI                  SS5      n[O        US5      n[
        R8                  " USSSS2SS2S4   [/        US5      SSSS2SS24   -  SS9n[/        US5      nUS   RQ                  U5      n[O        [
        R4                  " [
        R8                  " US-  SS9U RB                  RR                  -   5      S5      nURT                  " / UR                  SS QSPSP76 nU(       a!  US   RK                  URV                  5      US'   [
        R,                  " URI                  SS5      US   RK                  URL                  S95      n[O        US5      nU RY                  [
        RZ                  " U/[
        R:                  " USS9QUPUP7SS9RK                  US   RL                  S95      nU$ ! , (       d  f       GN= f)a  
Args:
    s:
        [*, N_res, C_s] single representation
    z:
        [*, N_res, N_res, C_z] pair representation
    r:
        [*, N_res] transformation object
    mask:
        [*, N_res] mask
Returns:
    [*, N_res, C_s] single representation update
Nry   r   r   ).Nr   r   r   Fr   )r   r   r   rG  r  gUUUUUU?r-  r.  )r   )ry   r   g      "@r  r   r   r   .)r   r   r   r   )r   r   r   r   ).NN).r   rV  r   ri   r  rC   splitrR  rW  stackapplyrT  rX  rU  rY  sysgetrefcountcpurO   r   r   r   r  r   r  r   r   r0  r  unbindr\  r  rc   rC  r  r   r  r   rZ   r{   invert_applyrH  rz   r[   rC  r  )r   r   rM  r  r+  rd  re  r  kvr`   r   q_ptskv_ptsk_ptsv_ptsr   r   pt_attr  square_maskr  o_pt	o_pt_normo_pairs                           rH   r   &EsmFoldInvariantPointAttention.forward  s   , C MM!^^A FF1773B<4>>2"667 WWRXXcr]dnnb%99: {{2B7 $$Q' E;;r?a#7R@Er*)""5) 

5;;s+t~~t?Q?QST.UUV &&q) V\\"%5%:CV,9##F+ V\\#2.$.."a1HHI {{6,>,>@Q@Q+RXZ[ MM!A$??1Q4(A---Q488:AaD (((7LL&qwwy)<&qwwy)< 87 "1i0"1i0A
 	
TYYsa$//1233	TYYw"4Q	"BBB $ur':: U\\&b12}}T%6%67<<tc&,,WZXZJ[F\?\_f?fh#diiqD<N<NQT<TWX<X7Y0Z&[[,& 6r*d3nnR(4>>"+==kkooq9 $FI6J%%b))LLO LLAKKB/222ABLLRQST q!$ yysD!Q$%(:5,(OPSUY[\^_P_(``
 "$5!..t4 'uzz%))D!G2Lt{{ObOb2b'cefg	 ||4TZZ_4b4!4Q4774;;'AaD akk"b11Q47773IJ $FA. OOIIqI5<<"5IyI&IrRUU\]^_\`\f\fUg
 Y 87s   8AZ
Z)rC  r  rR  rY  rV  rX  rC  r   rW  r  rT  rU  r   r\  )FN)r>   r?   r@   rA   rB   r   rC   rd   r   r   r   r	   r   rF   r   r   s   @rH   r  r  h  s    !&R $)>BZ<<Z ELL!Z 	Z
 llZ !Z $HU\\$:;Z 
Z ZrG   r  c                      ^  \ rS rSrSrU 4S jrS\R                  S\\R                  \R                  4   4S jr	Sr
U =r$ )EsmFoldBackboneUpdatei-  z"
Implements part of Algorithm 23.
c                 V   > [         TU ]  5         [        UR                  SSS9U l        g )N   r   r   )r   r   r   r;  r   r(  s     rH   r   EsmFoldBackboneUpdate.__init__2  s$    #F$7$7IrG   r   rX   c                 (    U R                  U5      nU$ )zZ
Args:
    [*, N_res, C_s] single representation
Returns:
    [*, N_res, 6] update vector
r   )r   r   updates      rH   r   EsmFoldBackboneUpdate.forward7  s     QrG   r  rM  r   s   @rH   r|  r|  -  s<    J

 
%ell0J*K 
 
rG   r|  c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )%EsmFoldStructureModuleTransitionLayeriD  c                 .  > [         TU ]  5         [        UR                  UR                  SS9U l        [        UR                  UR                  SS9U l        [        UR                  UR                  SS9U l        [        R                  " 5       U l	        g r0  )
r   r   r   r;  r2  r3  linear_3r   r  r   r(  s     rH   r   .EsmFoldStructureModuleTransitionLayer.__init__E  sr    %f&9&96;N;NU[\%f&9&96;N;NU[\%f&9&96;N;NU\]GGI	rG   c                     UnU R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nX-   nU$ rJ   )r2  r   r3  r  )r   r   r6  s      rH   r   -EsmFoldStructureModuleTransitionLayer.forwardN  sW    	MM!IIaLMM!IIaLMM!MrG   )r2  r3  r  r   r   r   s   @rH   r  r  D  s    
 
rG   r  c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ ) EsmFoldStructureModuleTransitioni[  c                 d  > [         TU ]  5         Xl        [        R                  " 5       U l        [        UR                  5       H)  n[        U5      nU R
                  R                  U5        M+     [        R                  " UR                  5      U l        [        UR                  5      U l        g rJ   )r   r   rC  r   r>  r?  rm   num_transition_layersr  rA  r  dropout_rater  r   r;  r   )r   rC  ra  rI  r   s       rH   r   )EsmFoldStructureModuleTransition.__init__\  s{    mmov334A5f=AKKq! 5 zz&"5"56#F$7$78rG   c                     U R                    H  nU" U5      nM     U R                  U5      nU R                  U5      nU$ rJ   )r?  r  r   )r   r   rI  s      rH   r   (EsmFoldStructureModuleTransition.forwardh  s;    A!A  LLOOOArG   )rC  r  r   r?  r   r   s   @rH   r  r  [  s    
9 rG   r  c                   H   ^  \ rS rSrU 4S jr  SS jrS rS rS rSr	U =r
$ )	EsmFoldStructureModuleir  c                   > [         TU ]  5         Xl        [        UR                  5      U l        [        UR                  5      U l        [        UR                  UR                  5      U l	        [        U5      U l        [        R                  " UR                  5      U l        [        UR                  5      U l        [#        U5      U l        ['        U5      U l        [+        U5      U l        g rJ   )r   r   rC  r   r;  layer_norm_srP  layer_norm_zr   r<  r  ipar   r  r  ipa_dropoutlayer_norm_ipar  
transitionr|  	bb_updater9  angle_resnetr(  s     rH   r   EsmFoldStructureModule.__init__s  s     &f&9&9:%f&9&9:&v':':F<O<OP1&9::f&9&9:'(;(;<:6B.v6.v6rG   c                    US   nUc  UR                  UR                  SS 5      nU R                  U5      nU R                  US   5      nSnU(       a:  [        R
                  " US   5      S:X  d   eUS   R                  5       US'   U/nSnUnU R                  U5      n[        R                  " UR                  SS UR                  UR                  U R                  SS9n	/ n
[        U R                  R                  5       GHq  nXPR!                  UUU	UUUS9-   nU R#                  U5      nU R%                  U5      nU R'                  U5      nU	R)                  U R+                  U5      5      n	[        [-        U	R/                  5       R1                  5       SS	9U	R3                  5       5      nUR5                  U R                  R6                  5      nU R9                  XX5      u  pU R;                  XU5      nU R=                  X5      nU	R5                  U R                  R6                  5      nUR?                  5       URA                  5       UUUUS
.nU
RC                  U5        U	RE                  5       n	GMt     AAU(       a!  US   RG                  UR                  5      US'   [I        [J        RL                  U
5      n
XZS'   U
$ )ag  
Args:
    evoformer_output_dict:
        Dictionary containing:
            "single":
                [*, N_res, C_s] single representation
            "pair":
                [*, N_res, N_res, C_z] pair representation
    aatype:
        [*, N_res] amino acid indices
    mask:
        Optional [*, N_res] sequence mask
Returns:
    A dictionary of outputs
singleNry   rS  r   quat)fmt)rd  re  )rot_matsquats)r&   r'   r(   r)   r*   r+   )'r/  ri   r  r  rk  rl  rm  r<  r   identityrZ   r[   trainingrm   rC  
num_blocksr  r  r  r  compose_q_update_vecr  r   get_rotsget_rot_mats	get_transscale_translationtrans_scale_factorr  r"   r   to_tensor_7to_tensor_4x4rA  stop_rot_gradientr   r   rC   ri  )r   evoformer_output_dictr0   r+  rd  r   rM  z_reference_listr6  rigidsoutputsrr   backb_to_globalr(   r)   all_frames_to_globalpred_xyzscaled_rigidspredss                      rH   r   EsmFoldStructureModule.forward  s   , "(+<::aggcrl+D a  3F;<??#8#@AQFFF,A&,I,M,M,O!&) !sA 	NN1 GGCRLGGHHMM
 t{{--.AHH#5"2   A   #A##A&A"A 001BCF
 $&//"3"@"@"B$O  "O
 .??@^@^_O +/*;*;A*I'#'#@#@Z`#a IIJ^gH"44T[[5S5STM (335$8$F$F$H': %E NN5!--/F_ /b ,A&,I,L,LQXX,V!&)W5rG   c           
      ,   [        U S5      (       d4  U R                  S[        R                  " [        R
                  UUSS9SS9  [        U S5      (       d3  U R                  S[        R                  " [        R                  USS9SS9  [        U S5      (       d4  U R                  S[        R                  " [        R                  UUSS9SS9  [        U S5      (       d5  U R                  S[        R                  " [        R                  UUSS9SS9  g g )	Ndefault_framesF)rZ   r[   requires_grad)
persistent	group_idx)r[   r  	atom_masklit_positions)	hasattrregister_bufferrC   r|   r    !restype_rigid_group_default_framerestype_atom14_to_rigid_grouprestype_atom14_mask$restype_atom14_rigid_group_positions)r   float_dtyper[   s      rH   _init_residue_constants.EsmFoldStructureModule._init_residue_constants  s#   t-..   %GG%!"'	 ! ! 	 t[))  %CC!"'
 ! !  t[))  %99%!"'	 ! ! 	 t_--  %JJ%!"'	 ! ! 	 .rG   c                 z    U R                  UR                  UR                  5        [        XX0R                  5      $ rJ   )r  rZ   r[   r"   r  )r   r  alphafs       rH   r"   /EsmFoldStructureModule.torsion_angles_to_frames)  s-    $$U[[%,,?'!5H5HIIrG   c                     U R                  UR                  5       R                  UR                  5       R                  5        [	        UUU R
                  U R                  U R                  U R                  5      $ rJ   )	r  r  rZ   r[   r   r  r  r  r  )r   r  r  s      rH   r   DEsmFoldStructureModule.frames_and_literature_positions_to_atom14_pos/  s]    $$QZZ\%7%79L9LM<NNNN
 	
rG   )
r  r  rC  r  r  r  r  r  r<  r  )NF)r>   r?   r@   rA   r   r   r  r"   r   rF   r   r   s   @rH   r  r  r  s-    78  ob+ZJ

 

rG   r  c                   D   ^  \ rS rSrU 4S jrS rS r\S 5       rSr	U =r
$ )EsmFoldingTrunki<  c                 Z  > [         TU ]  5         Xl        UR                  nUR                  n[        U5      U l        [        R                  " [        UR                  5       Vs/ s H  n[        U5      PM     sn5      U l        SU l        [        R                  " U5      U l        [        R                  " U5      U l        [        R"                  " U R                  U5      U l        U R$                  R&                  S   R)                  5       R+                  5         [-        UR.                  5      U l        [        R0                  " X!R.                  R2                  5      U l        [        R0                  " X1R.                  R6                  5      U l        UR:                  U l        g s  snf )N   r   )r   r   rC  r  r:  r"  pairwise_positional_embeddingr   r>  rm   r  r  blocksrecycle_binsr   recycle_s_normrecycle_z_normr&  recycle_distor   detachzero_r  structure_moduler  r;  
trunk2sm_srP  
trunk2sm_zr#  )r   rC  r]  r^  ra  r   s        rH   r   EsmFoldingTrunk.__init__=  s2   ''''-DV-L*mmZ_`f`q`qZr$sZrUV%H%PZr$st ll3/ ll3/\\$*;*;SA!!!$++-335 6v7N7N O))C)@)@)M)MN))C)@)@)M)MN ++ %ts   %F(c                     Xl         g rJ   )r#  )r   r#  s     rH   set_chunk_sizeEsmFoldingTrunk.set_chunk_sizeT  s	    
 %rG   c           	      N  ^  UR                   nUnUn	Uc  T R                  R                  nOUS:  a  [        S5      eUS-  nU 4S jn
UnU	n[        R
                  " U5      n[        R
                  " U5      n[        R                  " UR                  SS U[        R                  S.6n[        U5       GHN  n[        UUS-
  :X  a  / O[        R                  " 5       /5         T R                  UR                  5       5      R                  U5      nT R                  UR                  5       5      R                  U5      nUT R!                  UR                  5       5      R                  U5      -  nU
" X-   X-   XE5      u  pT R#                  T R%                  U5      T R'                  U5      S.UUR)                  5       5      nUnUn[*        R-                  US	   S   SS2SS2SS
24   SST R.                  5      nSSS5        GMQ     UWS'   UUS'   U$ ! , (       d  f       GMo  = f)aN  
Inputs:
  seq_feats: B x L x C tensor of sequence features pair_feats: B x L x L x C tensor of pair features residx: B
  x L long tensor giving the position in the sequence mask: B x L boolean tensor indicating valid residues

Output:
  predicted_structure: B x L x (num_atoms_per_residue * 3) tensor wrapped in a Coordinates object
Nr   z(Number of recycles must not be negative.r   c           	      |   > UTR                  X#S9-   nTR                   H  nU" XX2TR                  S9u  pM     X4$ )Nr  )r+  r5   r#  )r  r  r#  )r   rM  residxr+  blockr   s        rH   
trunk_iter+EsmFoldingTrunk.forward.<locals>.trunk_iterp  sF    D66v6IIAQt_1 %4KrG   ry   r  )r  rS  r*   r   g      @g     `5@r,   r-   )r[   rC  max_recyclesr   rC   
zeros_liker   ri   int64rm   r   r   r  r  r   r  r  r  r  r  r  r  	distogramr  )r   	seq_feats
pair_featstrue_aar  r+  no_recyclesr[   s_s_0s_z_0r  r,   r-   	recycle_s	recycle_zr  recycle_idx	structures   `                 rH   r   EsmFoldingTrunk.forward[  s    !!++22KQ !KLL1K	 $$S)	$$S)	{{CIIcrN6U -K {kAo'EEMMOK\] //	0@0@0BCFFvN	 //	0@0@0BCFFvN	T//0C0C0EFII&QQ	%e&79JFY !11#s3T__S=QRJJL	  		.88k*2.q!RaRx8%%	 % ^] .4 	%	%9 ^]s   #DH
H$	c                    [         R                  " UUUS-
  U R                  S9nUS-  nU R                  SSS9 Vs/ s H  oUR	                  S5      PM     snu  pgnXv-
  n	X-
  n
U	R                  U
SS9nSU-  S	U	-  -   S
U
-  -
  U-   nUSS S S 2S S 24   USS S 2S S S 24   -
  R                  S5      R                  SSS9n[         R                  " X:  SS9nU$ s  snf )Nr   r   r   r   r   r   ry   gƠvl¥gO[I-?g:4M?.T)re   keepdims)rC   r  r[   r  r  crosspowr  )coordsmin_binmax_binnum_bins
boundariesrn   NCACr   r[  r   CBdistsr  s                  rH   r  EsmFoldingTrunk.distogram  s     ^^qL==	

  ]
+1<<r<+BC+BaIIbM+BCqFFGGA2G1_zA~-
Q>CCq!O$r#q$/'::??BFF2X\F]yy+4 Ds   C)r  r#  rC  r  r  r  r  r  r  r  r  )r>   r?   r@   rA   r   r  r   staticmethodr  rF   r   r   s   @rH   r  r  <  s'    ,.%?B  rG   r  a}  
    ESMForProteinFolding is the HuggingFace port of the original ESMFold model. It consists of an ESM-2 "stem" followed
    by a protein folding "head", although unlike most other output heads, this "head" is similar in size and runtime to
    the rest of the model combined! It outputs a dictionary containing predicted structural information about the input
    protein(s).
    )custom_introc                     ^  \ rS rSrSS/rSrU 4S jr\S\\	   S\
R                  4S j5       r\     SS	\
R                  S
\\
R                     S\\
R                     S\\
R                     S\\   S\\   S\4S jj5       rS rS\
R                  S\
R                  4S jrS r\
R,                  " 5        SS\\	\\	   4   4S jj5       r\S\S\\	   4S j5       rS\	4S jrS\\	   S\\	   4S jrSrU =r$ )EsmForProteinFoldingi  r  r  Fc           
      	  > [         TU ]  U5        Xl        SU l        [	        USS9U l        U R
                  R                  S5        U R                  R                  R                  (       a  U R
                  R                  5         U R                  R                  U l        U R                  R                  U R                  R                  -  U l        U R                  R                  U l        U R!                  SU R#                  UR$                  5      5        [&        R(                  " [*        R,                  " U R                  S-   5      5      U l        U R                  R                  R0                  nUR2                  nUR4                  n[&        R6                  " [9        U R                  5      [&        R:                  " U R                  U5      [&        R<                  " 5       [&        R:                  " X35      5      U l        [@        RB                  S-   U l"        SU l#        U RD                  S-
  U l$        U RD                  S-
  U l%        U R                  R$                  RM                  S	5      U l'        U R                  R$                  RM                  S
5      U l(        U R                  R$                  RM                  S5      U l)        U R                  R$                  RM                  S5      U l*        U R                  R                  RV                  (       a%  [&        RX                  " U RD                  USS9U l-        []        U5      U l        [&        R:                  " X@R                  5      U l/        [&        R:                  " X@R                  5      U l0        [&        R:                  " X0RD                  5      U l1        SU l2        URf                  n[&        R6                  " [&        R8                  " URh                  5      [&        R:                  " URh                  U R                  R                  Rj                  5      [&        R:                  " U R                  R                  Rj                  U R                  R                  Rj                  5      [&        R:                  " U R                  R                  Rj                  SU Rd                  -  5      5      U l6        g )N@   F)add_pooling_layer
af2_to_esmr   r   r   r   z<cls>z<mask>z<eos><pad>)padding_idxr  %   )7r   r   rC  distogram_binsr   esmrequires_grad_esmfold_configfp16_esmhalfhidden_size	esm_featsnum_hidden_layersnum_attention_heads	esm_attns
esm_layersr  _af2_to_esm_from_vocab_list
vocab_listr   r   rC   r   esm_s_combinetrunkr  r:  r  r   r  r  	esm_s_mlpr    restype_numn_tokens_embedpad_idxunk_idxmask_idxindexesm_dict_cls_idxesm_dict_mask_idxesm_dict_eos_idxesm_dict_padding_idxembed_aar&  r'  r  distogram_headptm_headlm_head	lddt_binsr  r;  lddt_head_hid_dimr6   )r   rC  trunk_configr]  r^  structure_module_configr   s         rH   r   EsmForProteinFolding.__init__  s2     Fe<&;;%%..HHMMO00669X9XX++77\4+K+KFL]L]+^_\\%++doo6I*JK{{1177----dnn%IIdnnc*GGIIIc	
 0;;a?**Q.++a/ $ 6 6 < <W E!%!7!7!=!=h!G $ 6 6 < <W E$(KK$:$:$@$@$I!;;%%..\\$*=*=sPQRDN$\2
 ii-@-@A		#':':;yy&9&9:"."?"?LL0==>II-::DKK<V<V<h<hiIIdkk00BBDKKD^D^DpDpqIIdkk00BBBDWX	
rG   r   rX   c                     U R                  S5      /[        R                   Vs/ s H  oR                  U5      PM     sn-   n[        R                  " U5      $ s  snf )Nr  )r)  r    restypes_with_xrC   r|   )r   r   esm_reorders      rH   r  0EsmForProteinFolding._af2_to_esm_from_vocab_list  sR     "''01RcRsRs4tRsQ5E5Ea5HRs4tt||K(( 5us   A	input_idsattention_maskposition_idsmasking_patternnum_recyclesoutput_hidden_statesc           	         U R                   R                  nUnUR                  S   n	UR                  S   n
UR                  nUc  [        R
                  " XS9nUc#  [        R                  " XS9R                  U5      nU R                  X5      nUb  U R                  XX$5      u  pnOUnSnU R                  U5      nUR                  U R                  R                  5      nUR                  (       a  US-  nUR                  5       nU R                  R!                  S5      R#                  S5      U-  R%                  S5      nU R'                  U5      nUR)                  XXR*                  R,                  5      nU R                   R                  R.                  (       a  UU R1                  U5      -  nU R+                  UUXX%S9nUR3                  5        VVs0 s H  u  nnUS;   d  M  UU_M     nnnU(       a  UUS'   U R5                  US	   5      nUUR7                  SS5      -   S-  nUUS
'   U R9                  US   5      nUUS'   UUS'   [;        U5        S H  nUU==   UR#                  S5      -  ss'   M!     UUS'   U R=                  US   5      R?                  US   R                  S   XSU R@                  5      nUUS'   [C        US   U R@                  S9nUUS'   U RE                  US	   5      nUUS'   [G        USU RH                  S9US'   URK                  [M        USU RH                  S95        [O        S0 UD6$ s  snnf )a  
masking_pattern (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
    Locations of tokens to mask during training as a form of regularization. Mask values selected in `[0, 1]`.
num_recycles (`int`, *optional*, defaults to `None`):
    Number of times to recycle the input sequence. If `None`, defaults to `config.num_recycles`. "Recycling"
    consists of passing the output of the folding trunk back in as input to the trunk. During training, the
    number of recycles should vary with each batch, to ensure that the model learns to output valid predictions
    after each recycle. During inference, num_recycles should be set to the highest value that the model was
    trained with for maximum accuracy. Accordingly, when this value is set to `None`, config.max_recycles is
    used.

Example:

```python
>>> from transformers import AutoTokenizer, EsmForProteinFolding

>>> model = EsmForProteinFolding.from_pretrained("facebook/esmfold_v1")
>>> tokenizer = AutoTokenizer.from_pretrained("facebook/esmfold_v1")
>>> inputs = tokenizer(["MLKNVQVQLV"], return_tensors="pt", add_special_tokens=False)  # A tiny random peptide
>>> outputs = model(**inputs)
>>> folded_positions = outputs.positions
```

r   r   Nr   r   )r  )r-   r,   r&   r'   r(   r)   r*   r+   mlm_targetsr-   r.   r,   r/   r0   )r1   r4   ry   r5   r+   r6   r  r7   r8      )r  no_binsr9   r=   )(rC  r  ri   r[   rC   	ones_likearange	expand_asaf2_idx_to_esm_idx	bert_mask&compute_language_model_representationsr   r!  rZ   esm_ablate_sequencer  r   r0  r  r#  rX  r"  r:  r.  r'  r   r/  r  r1  r   r6   rz   r2  r  r0  r   r  r  r   r$   )r   r;  r<  r=  r>  r?  r@  cfgaaBLr[   esmaa	masked_aarB  esm_sr  r  r  r`   r   disto_logitsr/   r6   r7   r8   s                             rH   r   EsmForProteinFolding.forward  sF   D kk((HHQKHHQK!!!"__R?N <<9CCINL '';&,0NN2n,f)IkIK ;;EB
 ++112""AIE ##++A.88;eCLLQOu%a)E)EF;;%%..T^^I..E**UE2^*n	 ")
)1	
 AqD) 	 
" '2Im$**9U+;<$|'='=a'CCqH(4	$%LL5!12	!*	+ 	()$

A aLN44R88L	

 &2	/"NN9X#67??	(@S@Y@YZ[@\^_dfhlhvhvw	!*	+ 2T^^D"	']]9U#34
",	,%j"dFYFYZ	%8RY]YlYlmn)6I66e
s   M,Mc                     U R                   R                  UR                  :w  a*  U R                   R                  UR                  5      U l         US-   R                  US:g  S5      nU R                   U   $ Nr   r   )r  r[   r   r  )r   rM  r+  s      rH   rH  'EsmForProteinFolding.af2_idx_to_esm_idx  s[    ??!!RYY."oo00;DO1f!!$!)Q/r""rG   rP  c           	         [        U R                  5       5      R                  nUR                  u  p4U R                  R
                  R                  (       a;  [        R                  " X4U R                  R                  S   SU R                  US9nU$ U R                  U R                  pvUR                  US4U5      nUR                  US4U R                  5      n	[        R                   " XU	/SS9nXq[#        U5      US:g  R%                  S5      4'   U R'                  XS:g  SS9S   n
[        R(                  " U
S	S9nUS S 2SS24   nU$ )
Nr   ry   r   r   r   T)r<  r@  hidden_statesr   )next
parametersr[   ri   rC  r  	bypass_lmrC   r   r!  r   r  r*  r,  new_fullr-  r  rm   r  r  ri  )r   rP  r[   rN  rO  rR  bosieosiboseosesm_hidden_statess              rH   rJ  ;EsmForProteinFolding.compute_language_model_representations  s%   doo'(//{{;;%%//KKd&8&8&=&=a&@"dnn]cdEL**D,A,AdnnaVT*nnaVT%>%>?		3s+3/3eAh!((++,
 !HHUA:\`Habqr-15a2grG   c                     UR                  5       nUR                  5       nUR                  5       nU R                  XTS:H  '   SXdS:g  '   U R                  XtS:H  '   XWU4$ rV  )ru  r(  r+  )r   rM  rP  r+  patternnew_aatarget	new_esmaas           rH   rI  EsmForProteinFolding.bert_mask  s\    KKM	#}}!| !|"&"8"8	Q,&((rG   seqsc                    [        U[        5      (       a  U/nOUn[        U R                  5       5      R                  n[        U Vs/ s HW  n[        R                  " [        R                  " U[        R                  SS95      R                  U5      R                  SS9PMY     sn5      n[        U Vs/ s H  oVR                  [        U5      5      PM     sn5      nUc;  [        R                  " UR                   S   US9R#                  [        U5      S5      OUR                  U5      nUR$                  S:X  a  UR'                  S5      nU R)                  UUUS9$ s  snf s  snf )	NT)sequencemappingmap_unknown_to_xr   r   r   ry   r   )r=  )r   r   rZ  r[  r[   ru   rC   
from_numpyr    sequence_to_onehotrestype_order_with_xr   argmaxr/  rc   rF  ri   r  r  r0  r   )r   rj  r=  ro   r[   seqr0   r+  s           rH   inferEsmForProteinFolding.infer  sT    dC  &CCdoo'(//&  C   %88!$ 1 F F)- FA 
 %3%O3Cooc#h&?3%OP # LLa8??C"M( 	
 !'11!4L||%  
 	
+ &Ps   AE!6#E&r  c           	         U R                  5        VVs0 s H$  u  pXR                  S5      R                  5       _M&     n nn/ n[        U S   S   U 5      nU S   n[	        U S   R
                  S   5       HK  nU S   U   nXF   nXV   n	U S   U   S-   n
[        UUU	U
U S	   U   S
9nUR                  [        U5      5        MM     U$ s  snnf )zDReturns the pbd (file) string from the model given the model output.rm  r*   ry   r4   r0   r   r5   r   r7   )r0   atom_positionsr  r5   	b_factors)	r   r   numpyr   rm   ri   r   rA  r!   )r  r`   r   pdbsfinal_atom_positionsfinal_atom_maskrr   rM  pred_posr+  residpreds               rH   output_to_pdb"EsmForProteinFolding.output_to_pdb  s     6<\\^D^TQ!TT%[&&((^D/{0CB0GP !56vh'--a01A!!$B+.H"%D?+A.2E'# /!,D KKt% 2 # Es   +Cc                     [        U[        5      (       d   eU R                  " U/UQ70 UD6nU R                  U5      S   $ )EReturns the pdb (file) string from the model given an input sequence.r   )r   r   rt  r  r   rj  argskwargsr  s        rH   	infer_pdbEsmForProteinFolding.infer_pdb  sC    $$$$$D24262!!&)!,,rG   c                 P    U R                   " U/UQ70 UD6nU R                  U5      $ )r  )rt  r  r  s        rH   
infer_pdbsEsmForProteinFolding.infer_pdbs  s+    D24262!!&))rG   )r  rC  r  r/  r'  r  r  r*  r,  r+  r-  r  r  r!  r#  r2  r6   r1  r(  r%  r&  r0  r"  r'  )NNNNFrJ   )r>   r?   r@   rA   _no_split_modules_supports_flash_attn_2r   r  r   r   rC   rd   r  r   r   r   r   r$   r   rH  rJ  rI  r   r   rt  r   r  r  r  rF   r   r   s   @rH   r  r    s    23XY"5
n )S	 )ell ) )
  26/326&*/4C7<<C7 !.C7 u||,	C7
 "%,,/C7 smC7 'tnC7 
$C7 C7J#ELL U\\ 2) ]]_ %
CcN#%
 %
N d tCy  *-# -*tCy *d3i * *rG   r  r  )r  fan_inrx   )r  )[r   rk  dataclassesr   	functoolsr   typingr   r   r   r   r	   r
   r   ry  r   rC   torch.nnr   r   integrations.deepspeedr   modeling_outputsr   rR   r   r   r   r   modeling_esmr   r   openfold_utilsr   r   r   r   r   r   r   r   r   r    r!   r"   
get_loggerr>   r   r$   rO   rU   rd   r  ru   r   r{   r   r   r   r   r  r   Moduler   r5  r6  r   r   r  r8  r  r  r  r  r  r  r  r  r  r   r"  r.  r9  r  r|  r  r  r  r  r  __all__r=   rG   rH   <module>r     s    
 !  I I I     < +  7     
		H	% MD MD MD`
4#5 e ELL .1%,, 1 1Gu|| G49 G
D**+5BII +5\ryy * u|| # u||  Uryy Upkryy k\F")) FR
1*/ 1*h;(299 ;(|3RYY 3,#BII #LBII *		  e.")) e.PP P&;
,!bii !Hbii (>! >!BBRYY BJBII .BII .ryy .G
RYY G
Trbii rr w*- w*w*t	 "#;
<rG   