§
    9²¸jš;  ã                  ó’   — d Z ddlmZ ddlmZ ddlZddlmc mZ	 ddlm
Z
 erddlmZmZ dd„Z	 ddd„Zd d„Z G d„ d¦  «        ZdS )!añ  Generation / inference loop for ESMFold2, kept out of the modeling file.

``EsmFold2Model.forward`` runs the folding trunk; this mixin owns the diffusion sampling loop that
turns its pair representation into 3D coordinates, the confidence head call, and the public
``infer_protein`` entry points. ESMFold2 emits continuous coordinates rather than tokens, so it uses
neither ``GenerationConfig`` nor the ``Cache`` classes: sampling hyperparameters are plain kwargs
falling back to the model config.é    )Úannotations)ÚTYPE_CHECKINGN)ÚTensoré   ©ÚEsmFold2AtomInputsÚEsmFold2OutputÚnum_rotationsÚintÚdtypeútorch.dtypeÚdeviceútorch.deviceÚreturnr   c                ó²  — t          j        | df||¬¦  «        }t          j        ||z                       d¬¦  «        ¦  «        }t          j        |dd…df         dk     | |¦  «        }||dd…df         z  }t          j        |d¬¦  «        \  }}}}	d||z                       d¬¦  «        z  }
t          j        d|
||z  |	|	z  z   z  z
  |
||z  |	|z  z
  z  |
||	z  ||z  z   z  |
||z  |	|z  z   z  d|
||z  |	|	z  z   z  z
  |
||	z  ||z  z
  z  |
||	z  ||z  z
  z  |
||	z  ||z  z   z  d|
||z  ||z  z   z  z
  f	d¬¦  «                             | d	d	¦  «        S )
zV``num_rotations`` uniformly random 3x3 rotation matrices, via random unit quaternions.é   )r   r   r   ©ÚdimNr   éÿÿÿÿg       @é   )ÚtorchÚrandnÚsqrtÚsumÚwhereÚunbindÚstackÚreshape)r
   r   r   ÚquaternionsÚscaleÚsignsÚrÚiÚjÚkÚtwo_ss              ú€/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/models/esmfold2/generation_esmfold2.pyÚ_random_rotationsr(   $   s´  € å”+˜}¨aÐ0¸ÀfÐMÑMÔM€KÝŒJ˜ kÑ1×6Ò6¸1Ð6Ñ=Ô=Ñ>Ô>€EÝŒK˜ A A A q DÔ)¨AÒ-°¨v°uÑ=Ô=€EØ  a a a¨ g¤Ñ.€KÝ”˜k¨rÐ2Ñ2Ô2�J€A€qˆ!ˆQØ�; Ñ,×1Ò1°bÐ1Ñ9Ô9Ñ9€EÝŒ;à�˜˜Q™  Q¡™Ñ'Ñ'Ø�Q˜‘U˜Q ™U‘]Ñ#Ø�Q˜‘U˜Q ™U‘]Ñ#Ø�Q˜‘U˜Q ™U‘]Ñ#Ø�˜˜Q™  Q¡™Ñ'Ñ'Ø�Q˜‘U˜Q ™U‘]Ñ#Ø�Q˜‘U˜Q ™U‘]Ñ#Ø�Q˜‘U˜Q ™U‘]Ñ#Ø�˜˜Q™  Q¡™Ñ'Ñ'ð
	
ð ðñ ô ÷ ‚gˆm˜Q Ñ"Ô"ð#ó    ÚcoordsÚ	atom_maskÚsecond_coordsúTensor | Noneútuple[Tensor, Tensor | None]c                ó¢  — | j         d         }|                     d¦  «        }|                     dd¬¦  «                             d¬¦  «        }| |z                       dd¬¦  «        |z  }| |z
  } |�||z
  }t	          || j        | j        ¦  «        }| |z  } |�||z  }t          j        | dd…dd…dd…f         ¦  «        }| |z   } |�||z   }| |fS )zTAlgorithm 19: center + random rotation + translation. ``second_coords`` rides along.r   r   r   T©r   Úkeepdim©ÚminN)	ÚshapeÚ	unsqueezer   Úclampr(   r   r   r   Ú
randn_like)	r*   r+   r,   Ú
batch_sizeÚmaskÚdenominatorÚmeanÚ	rotationsÚtranslationss	            r'   Ú_center_random_augmentationr>   <   sý   € ð ”˜a”€JØ×Ò˜rÑ"Ô"€DØ—(’(˜q¨$�(Ñ/Ô/×5Ò5¸!Ð5Ñ<Ô<€KØ�T‰M×Ò 1¨dÐÑ3Ô3°kÑA€DØ�d‰]€FØÐ Ø%¨Ñ,ˆå! *¨f¬l¸F¼MÑJÔJ€IØ�iÑ€FØÐ Ø%¨	Ñ1ˆåÔ# F¨1¨1¨1¨a°¨c°1°1°1¨9Ô$5Ñ6Ô6€LØ�lÑ"€FØÐ Ø%¨Ñ4ˆØ�=Ð Ð r)   Útarget_coordsÚweightsr9   c                óª  — ||z                        d¦  «        }|                     dd¬¦  «                             d¬¦  «        }| |z                       dd¬¦  «        |z  }||z                       dd¬¦  «        |z  }| |z
  }||z
  }||z                       dd¦  «        |z  }	t          j                             |	|	j        rdnd¬	¦  «        \  }
}}t          j                             |
|z  ¦  «        }t	          j	        |¦  «        }|
t	          j
        t	          j        |||gd¬
¦  «        ¦  «        z  |z  }||                     dd¦  «        z  |z   S )zQKabsch alignment: align ``coords`` onto ``target_coords`` with the given weights.r   éþÿÿÿTr0   g:Œ0âŽyE>r2   ÚgesvdN)Údriverr   )r5   r   r6   Ú	transposer   ÚlinalgÚsvdÚis_cudaÚdetÚ	ones_likeÚ
diag_embedr   )r*   r?   r@   r9   r:   ÚcentroidÚtarget_centroidÚcentered_coordsÚcentered_target_coordsÚ
covarianceÚUÚ_ÚVhÚdeterminantÚonesÚrotations                   r'   Ú_weighted_rigid_alignrW   T   s^  € à�g‰~×(Ò(¨Ñ,Ô,€GØ—+’+ "¨d�+Ñ3Ô3×9Ò9¸dÐ9ÑCÔC€KØ˜Ñ ×%Ò%¨"°dÐ%Ñ;Ô;¸kÑI€HØ$ wÑ.×3Ò3¸ÀDÐ3ÑIÔIÈKÑW€OØ˜xÑ'€OØ*¨_Ñ<ÐØÐ2Ñ2×=Ò=¸bÀ"ÑEÔEÈÑW€JÝŒ|×Ò 
¸jÔ>PÐ3Z°7°7ÐVZÐÑ[Ô[�H€A€qˆ"Ý”,×"Ò" 1 r¡6Ñ*Ô*€KÝŒ?˜;Ñ'Ô'€DØ•5Ô#¥E¤K°°t¸[Ð0IÈrÐ$RÑ$RÔ$RÑSÔSÑSÐVXÑX€HØ˜X×/Ò/°°BÑ7Ô7Ñ7¸/ÑIÐIr)   c                  óÒ   — e Zd ZdZ ej        ¦   «         	 	 d#d$d„¦   «         Zd#d%d„Zd&d„Z	 	 	 d'd(d„Z	 ej        ¦   «         d)d „¦   «         Z
 ej        ¦   «         d*d+d"„¦   «         ZdS ),ÚEsmFold2FoldingMixinzLDiffusion sampling loop and the ``infer_protein`` entry points for ESMFold2.NÚattention_maskr   Úasym_idÚmol_typeÚdistogram_atom_idxÚnum_diffusion_samplesú
int | NoneÚnum_sampling_stepsr   r	   c                ó–  — ddl m}m}	 |�|n| j        j        j        }
 ||                     d¦  «        |                     d¦  «        |                     d¦  «        |                     d¦  «        |                     d¦  «        |                     d	¦  «        |                     d
¦  «        ¬¦  «        } | d||||dœ|¤Ž}|                      |j        |j	        |j
        |j        ||
|¬¦  «        }|                      |j	        |j        ||||j        j        |j        j        |||
|j
        |j        ¬¦  «        } |	d|j        |dœ|¤ŽS )u  
        Predict a structure end-to-end from featurized inputs: run the trunk
        ([`EsmFold2Model.forward`], which documents the feature arguments), sample coordinates from the
        diffusion structure head, and score them with the confidence head.

        Only the arguments the sampler and the confidence head need are named here; the rest are
        forwarded to the trunk untouched, so ``fold`` takes the same arguments as ``forward``.

        attention_mask (`torch.Tensor` of shape `(batch_size, num_tokens)`):
            Mask marking valid tokens (``1``) versus padding (``0``). Also forwarded to the trunk.
        asym_id (`torch.Tensor` of shape `(batch_size, num_tokens)`):
            Asymmetric-unit (chain) ID for each token. Also forwarded to the trunk.
        mol_type (`torch.Tensor` of shape `(batch_size, num_tokens)`):
            Molecule-type code for each token (``0`` = protein). Also forwarded to the trunk.
        distogram_atom_idx (`torch.Tensor` of shape `(batch_size, num_tokens)`):
            Index of the representative atom (CÎ², or CÎ± for glycine) of each token. Used by the
            confidence head; the trunk does not need it.
        num_diffusion_samples (`int`, *optional*):
            Number of parallel structure samples to draw; the confidence head re-runs once per sample.
            Defaults to `config.structure_head.num_diffusion_samples`.
        num_sampling_steps (`int`, *optional*):
            Number of diffusion sampling steps. Defaults to `config.structure_head.inference_num_steps`.
        trunk_kwargs:
            The remaining featurized inputs (`num_loops` included, plus the raw per-atom tensors bundled
            below into `atom_inputs`), forwarded to [`EsmFold2Model.forward`], which documents them.
        r   r   NÚref_posÚ
ref_chargeÚatom_attention_maskÚref_elementÚref_atom_name_charsÚref_space_uidÚatom_to_token)rb   rc   rd   re   rf   rg   rh   )rZ   r[   r\   Úatom_inputs)Ú
pair_trunkÚsingle_inputsÚrelative_position_encodingri   rZ   r^   r`   )rk   Úpair_statesÚpredicted_coordsr]   rZ   rh   rd   r[   r\   r^   rl   Útoken_bonds_encoding)Údistogram_logitsÚsample_atom_coords© )Úmodeling_esmfold2r   r	   ÚconfigÚstructure_headr^   ÚpopÚ_sample_structurerm   rk   rl   ri   Úconfidence_headrh   rd   ro   rp   )ÚselfrZ   r[   r\   r]   r^   r`   Útrunk_kwargsr   r	   Únum_samplesri   ÚtrunkÚsample_coordsÚconfidence_outputs                  r'   ÚfoldzEsmFold2FoldingMixin.foldg   sÌ  € ðJ 	JÐIÐIÐIÐIÐIÐIÐIð %Ð0ð "Ð!à”Ô+ÔAð 	ð )Ð(Ø ×$Ò$ YÑ/Ô/Ø#×'Ò'¨Ñ5Ô5Ø ,× 0Ò 0Ð1FÑ GÔ GØ$×(Ò(¨Ñ7Ô7Ø ,× 0Ò 0Ð1FÑ GÔ GØ&×*Ò*¨?Ñ;Ô;Ø&×*Ò*¨?Ñ;Ô;ð
ñ 
ô 
ˆð �ð 
Ø)ØØØ#ð	
ð 
ð
 ð
ð 
ˆð ×.Ò.ØÔ(ØÔ-Ø',Ô'GØÔ)Ø)Ø"-Ø1ð /ñ 
ô 
ˆð !×0Ò0ØÔ-ØÔ)Ø*Ø1Ø)àÔ+Ô9Ø %Ô 1Ô EØØØ"-Ø',Ô'GØ!&Ô!;ð 1ñ 
ô 
Ðð  ˆ~ð 
Ø"Ô3Ø,ð
ð 
ð  ð
ð 
ð 	
r)   Ú	num_stepsr   útorch.device | Nonec                óä  — | j         j        }|€|j        nt          |¦  «        }|dk    r5t	          j        |j        |j        j        z  dg|t          j	        ¬¦  «        S d|j
        z  }t	          j        ||t          j	        ¬¦  «        }|j        |z  ||dz
  z  |j        |z  |j        |z  z
  z  z   }|j        j        |                     |j
        ¦  «        z  }t          j        |dd¬¦  «        S )z Karras power-law noise schedule.Nr   ç        ©r   r   ç      ð?)r   r   ©Úvalue)rt   ru   Úinference_num_stepsr   r   ÚtensorÚinference_sigma_max_ratioÚdiffusion_moduleÚ
sigma_dataÚfloat32Úinference_exponentÚarangeÚinference_sigma_min_ratioÚpowÚFÚpad)	ry   r€   r   Úhead_configÚstepsÚinverse_exponentÚrampÚbaseÚschedules	            r'   Úinference_noise_schedulez-EsmFold2FoldingMixin.inference_noise_scheduleÅ   s
  € à”kÔ0ˆØ3<Ð3D�Ô/Ð/Í#ÈiÉ.Ì.ˆØ�AŠ:ˆ:Ý”<ØÔ6¸Ô9UÔ9`Ñ`ÐbeÐfØÝ”mðñ ô ð ð
  Ô!?Ñ?ÐÝŒ|˜E¨&½¼ÐFÑFÔFˆØÔ4Ð6FÑFÈ$ÐRWÐZ[ÑR[ÑJ\ØÔ1Ð3CÑCØÔ3Ð5EÑEñFñJ
ñ 
ˆð Ô/Ô:¸T¿XºXÀkÔFdÑ=eÔ=eÑeˆÝŒu�X˜v¨SÐ1Ñ1Ô1Ð1r)   r   útuple[Tensor, Tensor]c                óx  — | j         j        }|€|j        nt          |¦  «        }|                      ||¦  «        }|j        �-|||j        k             }t          j        |d|j        ¬¦  «        }t          j	        ||j
        k    t          j        ||j        ¦  «        t          j        |¦  «        ¦  «        }||fS )uÒ   Karras Ïƒ schedule (Algorithm 18) + per-step Î³ churn factors, capped at
        ``config.structure_head.inference_sigma_cap``.

        Read from ``config`` at call time, so edits after loading apply.
        N)r   r   r†   )rt   ru   rˆ   r   rš   Úinference_sigma_capr’   r“   r   r   Ú	gamma_minÚ	full_likeÚgamma_0Ú
zeros_like)ry   r`   r   r”   r•   r™   Úgammass          r'   Ú_build_noise_schedulez*EsmFold2FoldingMixin._build_noise_scheduleØ   s¼   € ð ”kÔ0ˆØ3EÐ3M�Ô/Ð/ÕSVÐWiÑSjÔSjˆØ×0Ò0°¸Ñ?Ô?ˆàÔ*Ð6Ø ¨KÔ,KÒ KÔLˆHÝ”u˜X v°[Ô5TÐUÑUÔUˆHÝ”Ø�{Ô,Ò,ÝŒO˜H kÔ&9Ñ:Ô:ÝÔ˜XÑ&Ô&ñ
ô 
ˆð
 ˜ÐÐr)   r   rj   rk   rl   ri   r   r-   r   c           
     ó  — | j         }|j        j        d         }	|j        }
|j        d         |z  }|                     ||||||¬¦  «        }|                      ||
¦  «        \  }}| j        j         j        }| j        j         j        }|d         t          j
        ||	d|
t          j        ¬¦  «        z  }|j                             |d¦  «                             ¦   «         }d}t          t!          |dd…         |dd…         |dd…         ¦  «        ¦  «        }|D ]ï\  }}}t#          |||¬¦  «        \  }}|                     ¦   «         }|d	|                     ¦   «         z   z  }|t'          |d
z  |d
z  z
  d¦  «        dz  z  }||t          j        |¦  «        z  z   } ||t          j        |f||
t          j        ¬¦  «        |¬¦  «        }t-          ||||¦  «        }|                     ¦   «         }||z
  |z  }||||z
  z  |z  z   }|}Œð|S )z»Diffusion sampling (Algorithm 18): returns the sampled atom coordinates.

        Only ``num_sampling_steps`` is an argument; the other sampling hyperparameters come from config.
        r   r   )ri   rj   rl   rk   rZ   r^   r   r„   Nr   )r,   r…   é   rƒ   g      à?)Únoisy_coordsÚnoise_levelÚconditioning)ru   rh   r4   r   Úprepare_conditioningr£   rt   Únoise_scaleÚ
step_scaler   r   r�   rd   Úrepeat_interleaveÚfloatÚlistÚzipr>   ÚitemÚmaxr7   ÚfullrW   )ry   rj   rk   rl   ri   rZ   r^   r`   ÚdenoiserÚ	num_atomsr   Útarget_batchr¨   r™   r¢   rª   r«   Úatom_coordsr+   Úprev_denoised_coordsÚschedule_stepsÚ
sigma_prevÚ
sigma_nextÚgammaÚsigma_prev_valueÚnoise_level_valueÚchurn_noise_stdr¦   Údenoised_coordsÚsigma_next_valueÚdenoised_directions                                  r'   rw   z&EsmFold2FoldingMixin._sample_structureì   sk  € ð Ô&ˆØÔ-Ô3°AÔ6ˆ	ØÔ%ˆØ$Ô*¨1Ô-Ð0EÑEˆð  ×4Ò4Ø#Ø!Ø'AØ'Ø)Ø"7ð 5ñ 
ô 
ˆð  ×5Ò5Ð6HÈ&ÑQÔQÑˆ�&à”kÔ0Ô<ˆØ”[Ô/Ô:ˆ
à˜q”k¥E¤K°¸iÈÐSYÕafÔanÐ$oÑ$oÔ$oÑoˆØÔ3×EÒEÐF[Ð]^Ñ_Ô_×eÒeÑgÔgˆ	à.2Ðå�c (¨3¨B¨3¤-°¸!¸"¸"´¸vÀaÀbÀb¼zÑJÔJÑKÔKˆà-;ð 	3ð 	3Ñ)ˆJ˜
 EÝ0KØ˜YÐ6Jð1ñ 1ô 1Ñ-ˆKÐ-ð  *ŸšÑ0Ô0ÐØ 0°C¸%¿*º*¹,¼,Ñ4FÑ GÐØ)­CÐ0AÀ1Ñ0DÐGWÐYZÑGZÑ0ZÐ\_Ñ,`Ô,`ÐdgÑ,gÑgˆOØ&¨½5Ô;KÈKÑ;XÔ;XÑ)XÑXˆLð '˜hØ)Ý!œJ¨ Ð8IÐRXÕ`eÔ`mÐnÑnÔnØ)ðñ ô ˆOõ 1°¸ÐPYÐ[dÑeÔeˆLð  *ŸšÑ0Ô0ÐØ".°Ñ"@ÐDUÑ!UÐØ&¨Ð7GÐJ[Ñ7[Ñ)\Ð_qÑ)qÑqˆKà#2Ð Ð àÐr)   ÚsequenceÚstrc                óL   — ddl m}  | j        di  ||| j        ¬¦  «        ¤|¤ŽS )zUFeaturize ``sequence`` and fold it. ``forward_kwargs`` go to [`~EsmFold2Model.fold`].r   )Úprepare_protein_features©r   rr   )Úprotein_utilsrÅ   r   r   )ry   rÂ   Úforward_kwargsrÅ   s       r'   Úinfer_proteinz"EsmFold2FoldingMixin.infer_protein3  sG   € ð 	<Ð;Ð;Ð;Ð;Ð;àˆtŒyÐdÐdÐ3Ð3°HÀTÄ[ÐQÑQÔQÐdÐUcÐdÐdÐdr)   Ú
sample_idxc                óp   — ddl m}m}  ||| j        ¬¦  «        } | j        di |¤|¤Ž} ||||¬¦  «        S )z¥Fold ``sequence`` and render the prediction as a PDB string.

        ``sample_idx`` picks which diffusion sample to render; by default the best-ranked one.
        r   )Úoutput_to_pdbrÅ   rÆ   )rÊ   rr   )rÇ   rÌ   rÅ   r   r   )ry   rÂ   rÊ   rÈ   rÌ   rÅ   ÚfeaturesÚoutputs           r'   Úinfer_protein_as_pdbz)EsmFold2FoldingMixin.infer_protein_as_pdb:  sg   € ð 	KÐJÐJÐJÐJÐJÐJÐJà+Ð+¨H¸T¼[ÐIÑIÔIˆØ�”Ð8Ð8˜XÐ8¨Ð8Ð8ˆØˆ}˜V X¸*ÐEÑEÔEÐEr)   )NN)rZ   r   r[   r   r\   r   r]   r   r^   r_   r`   r_   r   r	   )r€   r_   r   r�   r   r   )r`   r_   r   r   r   r›   )Nr   N)rj   r   rk   r   rl   r   ri   r   rZ   r-   r^   r   r`   r_   r   r   )rÂ   rÃ   r   r	   ©N)rÂ   rÃ   rÊ   r_   r   rÃ   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úno_gradr   rš   r£   rw   rÉ   rÏ   rr   r)   r'   rY   rY   d   s
  € € € € € ØVÐVà€U„]�_„_ð -1Ø)-ð[
ð [
ð [
ð [
ñ „_ð[
ðz2ð 2ð 2ð 2ð 2ð& ð  ð  ð  ð4 )-Ø%&Ø)-ðEð Eð Eð Eð EðN €U„]�_„_ðeð eð eñ „_ðeð €U„]�_„_ð	Fð 	Fð 	Fð 	Fñ „_ð	Fð 	Fð 	Fr)   rY   )r
   r   r   r   r   r   r   r   rÐ   )r*   r   r+   r   r,   r-   r   r.   )
r*   r   r?   r   r@   r   r9   r   r   r   )rÔ   Ú
__future__r   Útypingr   r   Útorch.nn.functionalÚnnÚ
functionalr’   r   rs   r   r	   r(   r>   rW   rY   rr   r)   r'   ú<module>rÛ      s   ðð%ð %ð #Ð "Ð "Ð "Ð "Ð "à  Ð  Ð  Ð  Ð  Ð  à €€€Ø Ð Ð Ð Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð ð ð FØEÐEÐEÐEÐEÐEÐEÐEð#ð #ð #ð #ð2 GKð!ð !ð !ð !ð !ð0Jð Jð Jð Jð `Fð `Fð `Fð `Fð `Fñ `Fô `Fð `Fð `Fð `Fr)   