+
    œ°³jš;  ã                  ó¤   € R t ^ RIHt ^ RIHt ^ RIt^ RIHu Ht	 ^ RIH
t
 ]'       d	   ^RIHtHt R R ltRR R	 lltR
 R lt ! R R4      tR# )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ÚEsmFold2Outputc               ó(   € V ^8„  d   QhRRRRRRRR/# )	é   Únum_rotationsÚintÚdtypeztorch.dtypeÚdeviceútorch.deviceÚreturnr   © )Úformats   "Úq/home/luca/screen-checking/.venv/lib/python3.14/site-packages/transformers/models/esmfold2/generation_esmfold2.pyÚ__annotate__r   $   s)   € ÷ #ñ # Sð #°ð #Àlð #ÐW]ñ #ó    c                óÈ  € \         P                  ! V ^3WR7      p\         P                  ! W3,          P                  ^R7      4      p\         P                  ! VR,          ^ 8  V) V4      pW5R,          ,          p\         P
                  ! VRR7      w  rgr‰RW3,          P                  RR7      ,          p
\         P                  ! ^W¨V,          W™,          ,           ,          ,
          W§V,          W–,          ,
          ,          W§V	,          W†,          ,           ,          W§V,          W–,          ,           ,          ^W§V,          W™,          ,           ,          ,
          W¨V	,          Wv,          ,
          ,          W§V	,          W†,          ,
          ,          W¨V	,          Wv,          ,           ,          ^W§V,          Wˆ,          ,           ,          ,
          3	RR7      P                  V ^^4      # )zV``num_rotations`` uniformly random 3x3 rotation matrices, via random unit quaternions.)r   r   ©Údimg       @)ºNNNé    )r   Néÿÿÿÿ)ÚtorchÚrandnÚsqrtÚsumÚwhereÚunbindÚstackÚreshape)r
   r   r   ÚquaternionsÚscaleÚsignsÚrÚiÚjÚkÚtwo_ss   &&&        r   Ú_random_rotationsr+   $   sY  € ä—+’+˜}¨aÐ0¸ÔM€KÜ�JŠJ˜Õ1×6Ñ6¸1Ð6Ó=Ó>€EÜ�KŠK˜ DÕ)¨AÑ-°¨v°uÓ=€EØ g¥Õ.€KÜ—’˜k¨rÔ2�J€Aˆ!Ø�;Õ,×1Ñ1°bÐ1Ó9Õ9€EÜ�;Š;à�˜Q� ¥�Õ'Õ'Ø˜•U˜Q�U•]Õ#Ø˜•U˜Q�U•]Õ#Ø˜•U˜Q�U•]Õ#Ø�˜Q� ¥�Õ'Õ'Ø˜•U˜Q�U•]Õ#Ø˜•U˜Q�U•]Õ#Ø˜•U˜Q�U•]Õ#Ø�˜Q� ¥�Õ'Õ'ð
	
ð ô÷ �gˆm˜Q Ó"ð#r   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r	   Úcoordsr   Ú	atom_maskÚsecond_coordsúTensor | Noner   ztuple[Tensor, Tensor | None]r   )r   s   "r   r   r   <   s,   € ÷ !ñ !Øð!Ø%ð!Ø6Cð!à!ñ!r   c                óÈ  € V P                   ^ ,          pVP                  R4      pVP                  ^RR7      P                  ^R7      pW,          P                  ^RR7      V,          pW,
          p Ve	   W&,
          p\	        W0P
                  V P                  4      pW,          p Ve	   W',          p\        P                  ! V R,          4      pW,           p Ve	   W(,           pW3# )zTAlgorithm 19: center + random rotation + translation. ``second_coords`` rides along.T©r   Úkeepdim©Úminr   )r   :r   é   Nr   )	ÚshapeÚ	unsqueezer   Úclampr+   r   r   r   Ú
randn_like)	r-   r.   r/   Ú
batch_sizeÚmaskÚdenominatorÚmeanÚ	rotationsÚtranslationss	   &&&      r   Ú_center_random_augmentationrA   <   sÐ   € ð —‘˜a•€JØ×Ñ˜rÓ"€DØ—(‘(˜q¨$�(Ó/×5Ñ5¸!Ð5Ó<€KØ�M×Ñ 1¨dÐÓ3°kÕA€DØ�]€FØÒ Ø%Õ,ˆä! *¯l©l¸F¿M¹MÓJ€IØÕ€FØÒ Ø%Õ1ˆä×#Ò# F¨9Õ$5Ó6€LØÕ"€FØÒ Ø%Õ4ˆØÐ Ð r   c          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )r	   r-   r   Útarget_coordsÚweightsr<   r   r   )r   s   "r   r   r   T   s8   € ÷ Jñ J &ð J¸ð JÈ&ð JÐX^ð JÐciñ Jr   c                óî  € W2,          P                  R	4      pVP                  R
RR7      P                  RR7      pW,          P                  R
RR7      V,          pW,          P                  R
RR7      V,          pW,
          pW,
          pW(,          P                  R	R
4      V,          p	\        P
                  P                  Y™P                  '       d   RMRR7      w  r«p\        P
                  P                  W¬,          4      p\        P                  ! V4      pV
\        P                  ! \        P                  ! WîV.R	R7      4      ,          V,          pWP                  R	R
4      ,          V,           # )zQKabsch alignment: align ``coords`` onto ``target_coords`` with the given weights.Tr2   g:Œ0âŽyE>r4   ÚgesvdN)Údriverr   r   éþÿÿÿ)r8   r   r9   Ú	transposer   ÚlinalgÚsvdÚis_cudaÚdetÚ	ones_likeÚ
diag_embedr!   )r-   rC   rD   r<   r=   ÚcentroidÚtarget_centroidÚcentered_coordsÚcentered_target_coordsÚ
covarianceÚUÚ_ÚVhÚdeterminantÚonesÚrotations   &&&&            r   Ú_weighted_rigid_alignr[   T   s,  € à�~×(Ñ(¨Ó,€GØ—+‘+ "¨d�+Ó3×9Ñ9¸dÐ9ÓC€KØÕ ×%Ñ%¨"°dÐ%Ó;¸kÕI€HØ$Õ.×3Ñ3¸ÀDÐ3ÓIÈKÕW€OØÕ'€OØ*Õ<ÐØÕ2×=Ñ=¸bÀ"ÓEÈÕW€JÜ�|‰|×Ñ 
×>P×>PÐ>P±7ÐVZÐÓ[�H€Aˆ"Ü—,‘,×"Ñ" 1¥6Ó*€KÜ�?Š?˜;Ó'€DØ”5×#Ò#¤E§K¢K°¸[Ð0IÈrÔ$RÓSÕSÐVXÕX€HØ×/Ñ/°°BÓ7Õ7¸/ÕIÐIr   c                  óê   € ] tR t^dtRt]P                  ! 4       RR R ll4       tRR R lltR R lt	RR	 R
 llt
]P                  ! 4       R R l4       t]P                  ! 4       RR R ll4       tRtR# )ÚEsmFold2FoldingMixinzLDiffusion sampling loop and the ``infer_protein`` entry points for ESMFold2.Nc               ó4   € V ^8„  d   QhRRRRRRRRRRRRR	R
/# )r	   Úattention_maskr   Úasym_idÚmol_typeÚdistogram_atom_idxÚnum_diffusion_samplesú
int | NoneÚnum_sampling_stepsr   r   r   )r   s   "r   r   Ú!EsmFold2FoldingMixin.__annotate__h   sY   € ÷ [
ñ [
àð[
ð ð[
ð ð	[
ð
 #ð[
ð  *ð[
ð 'ð[
ð 
ñ[
r   c                óô  € ^RI HpHp	 Ve   TMV P                  P                  P
                  p
V! VP                  R4      VP                  R4      VP                  R4      VP                  R4      VP                  R4      VP                  R4      VP                  R4      R	7      pV ! RR
VRVRVRV/VB pV P                  VP                  VP                  VP                  VP                  VV
VR7      pV P                  VP                  VP                  VVVVP                  P                  VP                  P                  VVV
VP                  VP                  R7      pV	! RRVP                   RV/VB # )uL  
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   Úref_posÚ
ref_chargeÚatom_attention_maskÚref_elementÚref_atom_name_charsÚref_space_uidÚatom_to_token)rh   ri   rj   rk   rl   rm   rn   r_   r`   ra   Úatom_inputs)Ú
pair_trunkÚsingle_inputsÚrelative_position_encodingro   r_   rc   re   )rq   Úpair_statesÚpredicted_coordsrb   r_   rn   rj   r`   ra   rc   rr   Útoken_bonds_encodingÚdistogram_logitsÚsample_atom_coordsr   )Úmodeling_esmfold2r   r   ÚconfigÚstructure_headrc   ÚpopÚ_sample_structurers   rq   rr   ro   Úconfidence_headrn   rj   ru   rv   )Úselfr_   r`   ra   rb   rc   re   Útrunk_kwargsr   r   Únum_samplesro   ÚtrunkÚsample_coordsÚconfidence_outputs   &&&&&&&,       r   ÚfoldÚEsmFold2FoldingMixin.foldg   s·  € ÷J 	Jð %Ò0ñ "à—‘×+Ñ+×AÑAð 	ñ )Ø ×$Ñ$ YÓ/Ø#×'Ñ'¨Ó5Ø ,× 0Ñ 0Ð1FÓ GØ$×(Ñ(¨Ó7Ø ,× 0Ñ 0Ð1FÓ GØ&×*Ñ*¨?Ó;Ø&×*Ñ*¨?Ó;ô
ˆñ ñ 
Ø)ð
àð
ð ð
ð $ð	
ð
 ñ
ˆð ×.Ñ.Ø×(Ñ(Ø×-Ñ-Ø',×'GÑ'GØ×)Ñ)Ø)Ø"-Ø1ð /ó 
ˆð !×0Ñ0Ø×-Ñ-Ø×)Ñ)Ø*Ø1Ø)à×+Ñ+×9Ñ9Ø %× 1Ñ 1× EÑ EØØØ"-Ø',×'GÑ'GØ!&×!;Ñ!;ð 1ó 
Ðñ  ñ 
Ø"×3Ñ3ð
à,ð
ð  ñ
ð 	
r   c               ó$   € V ^8„  d   QhRRRRRR/# )r	   Ú	num_stepsrd   r   ztorch.device | Noner   r   r   )r   s   "r   r   rf   Å   s#   € ÷ 2ñ 2°*ð 2ÐM`ð 2Ðlrñ 2r   c                óÌ  € V P                   P                  pVf   VP                  M
\        V4      pV^8X  dO   \        P
                  ! VP                  VP                  P                  ,          R.V\        P                  R7      # RVP                  ,          p\        P                  ! WB\        P                  R7      pVP                  V,          Wd^,
          ,          VP                  V,          VP                  V,          ,
          ,          ,           pVP                  P                  VP                  VP                  4      ,          p\        P                  ! VRRR7      # )z Karras power-law noise schedule.ç        ©r   r   ç      ð?©Úvalue)r   r6   )ry   rz   Úinference_num_stepsr   r   ÚtensorÚinference_sigma_max_ratioÚdiffusion_moduleÚ
sigma_dataÚfloat32Úinference_exponentÚarangeÚinference_sigma_min_ratioÚpowÚFÚpad)	r~   r‡   r   Úhead_configÚstepsÚinverse_exponentÚrampÚbaseÚschedules	   &&&      r   Úinference_noise_scheduleÚ-EsmFold2FoldingMixin.inference_noise_scheduleÅ   s  € à—k‘k×0Ñ0ˆØ3<Ò3D�×/Ò/Ì#ÈiË.ˆØ�AŒ:Ü—<’<Ø×6Ñ6¸×9UÑ9U×9`Ñ9`Õ`ÐbeÐfØÜ—m‘môð ð
  ×!?Ñ!?Õ?ÐÜ�|Š|˜E¼¿¹ÔFˆØ×4Ñ4Ð6FÕFÈ$ÐZ[ÕR[ÕJ\Ø×1Ñ1Ð3CÕCØ×3Ñ3Ð5EÕEõFõJ
õ 
ˆð ×/Ñ/×:Ñ:¸T¿X¹XÀk×FdÑFdÓ=eÕeˆÜ�uŠu�X˜v¨SÔ1Ð1r   c               ó$   € V ^8„  d   QhRRRRRR/# )r	   re   rd   r   r   r   ztuple[Tensor, Tensor]r   )r   s   "r   r   rf   Ø   s"   € ÷  ñ  ¸
ð  ÈLð  Ð]rñ  r   c                óÄ  € V P                   P                  pVf   VP                  M
\        V4      pV P	                  WB4      pVP
                  e9   WUVP
                  8*  ,          p\        P                  ! VRVP
                  R7      p\        P                  ! WSP                  8„  \        P                  ! WSP                  4      \        P                  ! V4      4      pWV3# )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.
rŒ   )r6   r   )ry   rz   rŽ   r   r    Úinference_sigma_capr˜   r™   r   r   Ú	gamma_minÚ	full_likeÚgamma_0Ú
zeros_like)r~   re   r   rš   r›   rŸ   Úgammass   &&&    r   Ú_build_noise_scheduleÚ*EsmFold2FoldingMixin._build_noise_scheduleØ   s¸   € ð —k‘k×0Ñ0ˆØ3EÒ3M�×/Ò/ÔSVÐWiÓSjˆØ×0Ñ0°Ó?ˆà×*Ñ*Ò6Ø¨K×,KÑ,KÑ KÕLˆHÜ—u’u˜X v°[×5TÑ5TÔUˆHÜ—’Ø×,Ñ,Ñ,Ü�OŠO˜H×&9Ñ&9Ó:Ü×Ò˜XÓ&ó
ˆð
 ÐÐr   c               ó8   € V ^8„  d   QhRRRRRRRRRRR	R
RRRR/# )r	   rp   r   rq   rr   ro   r   r_   r0   rc   r   re   rd   r   r   )r   s   "r   r   rf   ì   sd   € ÷ Eñ EàðEð ðEð %+ð	Eð
 (ðEð &ðEð  #ðEð 'ðEð 
ñEr   c                óÌ  € V P                   pVP                  P                  ^,          p	VP                  p
VP                  ^ ,          V,          pVP	                  VVVVVVR7      pV P                  Wz4      w  rÞV P                  P                   P                  pV P                  P                   P                  pV^ ,          \        P                  ! W¹^V
\        P                  R7      ,          pVP                  P                  V^ 4      P                  4       pRp\        \!        VRR
 VR,          VR,          4      4      pV EF  w  ppp\#        VVVR7      w  ppVP%                  4       pVRVP%                  4       ,           ,          pV\'        V^,          V^,          ,
          R4      R,          ,          pVV\        P(                  ! V4      ,          ,           pV! V\        P*                  ! V3VV
\        P                  R7      VR	7      p\-        VVVV4      pVP%                  4       pVV,
          V,          pVVVV,
          ,          V,          ,           pTpEK  	  V# )z«Diffusion sampling (Algorithm 18): returns the sampled atom coordinates.

Only ``num_sampling_steps`` is an argument; the other sampling hyperparameters come from config.
)ro   rp   rr   rq   r_   rc   rŠ   N:r6   NN)r/   r‹   r‰   g      à?)Únoisy_coordsÚnoise_levelÚconditioningr   )rz   rn   r7   r   Úprepare_conditioningrª   ry   Únoise_scaleÚ
step_scaler   r   r“   rj   Úrepeat_interleaveÚfloatÚlistÚziprA   ÚitemÚmaxr:   Úfullr[   )r~   rp   rq   rr   ro   r_   rc   re   Ú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   r|   Ú&EsmFold2FoldingMixin._sample_structureì   s=  € ð ×&Ñ&ˆØ×-Ñ-×3Ñ3°AÕ6ˆ	Ø×%Ñ%ˆØ$×*Ñ*¨1Õ-Ð0EÕEˆð  ×4Ñ4Ø#Ø!Ø'AØ'Ø)Ø"7ð 5ó 
ˆð  ×5Ñ5Ð6HÓQÑˆà—k‘k×0Ñ0×<Ñ<ˆØ—[‘[×/Ñ/×:Ñ:ˆ
à˜q•k¤E§K¢K°ÈÐSYÔaf×anÑanÔ$oÕoˆØ×3Ñ3×EÑEÐF[Ð]^Ó_×eÑeÓgˆ	à.2Ðäœc (¨3¨B -°¸"µ¸vÀb½zÓJÓKˆä-;Ñ)ˆJ˜
 EÜ0KØ˜YÐ6Jô1Ñ-ˆKÐ-ð  *Ÿ™Ó0ÐØ 0°C¸%¿*¹*»,Õ4FÕ GÐØ)¬CÐ0AÀ1Õ0DÐGWÐYZÕGZÕ0ZÐ\_Ó,`ÐdgÕ,gÕgˆOØ&¨¼5×;KÒ;KÈKÓ;XÕ)XÕXˆLñ 'Ø)Ü!ŸJšJ¨ Ð8IÐRXÔ`e×`mÑ`mÔnØ)ôˆOô 1°¸ÐPYÐ[dÓeˆLð  *Ÿ™Ó0ÐØ".°Õ"@ÐDUÕ!UÐØ&¨Ð7GÐJ[Õ7[Õ)\Ð_qÕ)qÕqˆKà#2Ó ñ3 .<ð6 Ðr   c               ó    € V ^8„  d   QhRRRR/# )r	   ÚsequenceÚstrr   r   r   )r   s   "r   r   rf   4  s   € ÷ eñ e cð eÀñ er   c           	     óX   € ^RI Hp V P                  ! R/ V! WP                  R7      BVB # )zUFeaturize ``sequence`` and fold it. ``forward_kwargs`` go to [`~EsmFold2Model.fold`].)Úprepare_protein_features©r   r   )Úprotein_utilsrÏ   r„   r   )r~   rÌ   Úforward_kwargsrÏ   s   &&, r   Úinfer_proteinÚ"EsmFold2FoldingMixin.infer_protein3  s)   € õ 	<à�yŠyÑdÑ3°HÇ[Á[ÔQÐdÐUcÑdÐdr   c               ó$   € V ^8„  d   QhRRRRRR/# )r	   rÌ   rÍ   Ú
sample_idxrd   r   r   )r   s   "r   r   rf   ;  s'   € ÷ 	Fñ 	F¨Sð 	F¸jð 	FÐfiñ 	Fr   c                ót   € ^RI HpHp V! WP                  R7      pV P                  ! R/ VBVB pV! WvVR7      # )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.
)Úoutput_to_pdbrÏ   rÐ   )rÖ   r   )rÑ   rØ   rÏ   r   r„   )r~   rÌ   rÖ   rÒ   rØ   rÏ   ÚfeaturesÚoutputs   &&&,    r   Úinfer_protein_as_pdbÚ)EsmFold2FoldingMixin.infer_protein_as_pdb:  s9   € ÷ 	Ká+¨H¿[¹[ÔIˆØ—’Ñ8˜XÐ8¨Ñ8ˆÙ˜V¸*ÔEÐEr   r   )NN)Nr6   N©N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   Úno_gradr„   r    rª   r|   rÓ   rÛ   Ú__static_attributes__r   r   r   r]   r]   d   si   † ÙVà
‡]‚]ƒ_ö[
ó ð[
÷z2õ& ÷(EðN ‡]‚]ƒ_ôeó ðeð ‡]‚]ƒ_ö	Fó ô	Fr   r]   rÝ   )râ   Ú
__future__r   Útypingr   r   Útorch.nn.functionalÚnnÚ
functionalr˜   r   rx   r   r   r+   rA   r[   r]   r   r   r   Ú<module>rê      sD   ðñ%õ #å  ã ß Ð Ý ÷ ßEõ#÷0!õ0J÷ `Fó `Fr   