o
    ߥiB                     @   s   d dl Z d dlmZ d dlmZ ddlmZ ddlmZm	Z	 ddl
mZ ddlmZ dd	lmZmZmZmZmZ dd
lmZmZ ddlmZmZmZmZmZmZ ddlmZ ddl m!Z!m"Z"m#Z# G dd dej$Z%dS )    N)tensor_tree_map   )residue_constants   )gen_msa_attn_maskgen_tri_attn_mask)AuxiliaryHeads)residual)ExtraMSAEmbedderInputEmbedderRecyclingEmbedderTemplateAngleEmbedderTemplatePairEmbedder)EvoformerStackExtraMSAStack)atom14_to_atom37build_extra_msa_featbuild_template_angle_featbuild_template_pair_featbuild_template_pair_feat_v2pseudo_beta_fn)StructureModule)TemplatePairStackTemplatePointwiseAttentionTemplateProjectionc                       s   e Zd Z fddZdd Z fddZ fddZd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Z	dddZdd Z  ZS )	AlphaFoldc                    sp  t t|   |j| _|j}|j}|j}tdi |d d|ji| _	t
di |d | _|jjrPtdi |d | _tdi |d | _tdi |d | _nd | _|d j| _| jrgtdi |d | _n
tdi |d | _tdi |d | _tdi |d	 | _tdi |d
 | _tdi |d | _t |d | _!|| _"t#j$| _%| jj&| _&| jj'r| '  d S d S )Ninput_embedderuse_chain_relativerecycling_embeddertemplate_angle_embeddertemplate_pair_embeddertemplate_pair_stacktemplate_pointwise_attentionextra_msa_embedderextra_msa_stackevoformer_stackstructure_moduleheads )(superr   __init__globalsmodeltemplate	extra_msar   is_multimerr   r   r   enabledr   r   r   r    r   r!   #enable_template_pointwise_attentionr   template_pointwise_attr   template_projr
   r#   r   r$   r   	evoformerr   r&   r   	aux_headsconfigtorchfloatdtypeinfalphafold_original_mode)selfr6   template_configextra_msa_config	__class__r(   g/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/science/unifold/modules/alphafold.pyr*      sf   



zAlphaFold.__init__c                 C   s   | j  | _ | j | _d S N)r   r8   r   r<   r(   r(   rA   __make_input_float__H   s   zAlphaFold.__make_input_float__c                    *   t    t| dds|   tj| _| S N	inferenceF)r)   halfgetattrrD   r7   r9   rC   r?   r(   rA   rH   L   
   
zAlphaFold.halfc                    rE   rF   )r)   bfloat16rI   rD   r7   r9   rC   r?   r(   rA   rK   S   rJ   zAlphaFold.bfloat16c                 C      dd }|  | d S )Nc                 S   s.   t | dr	|   t | drt | _d S d S )Napply_alphafold_original_modeact)hasattrrM   nnReLUrN   moduler(   r(   rA   set_alphafold_original_mode\   s
   

zFAlphaFold.alphafold_original_mode.<locals>.set_alphafold_original_modeapply)r<   rT   r(   r(   rA   r;   Z   s   z!AlphaFold.alphafold_original_modec                 C   rL   )Nc                 S   s   t | dd d S )NrG   T)setattrrR   r(   r(   rA   set_inference_modef   s   z4AlphaFold.inference_mode.<locals>.set_inference_moderU   )r<   rX   r(   r(   rA   inference_moded   s   zAlphaFold.inference_modec                 C   s:   |D ]}|| j | j krd|v r|| | j ||< q|S )Nmask)r9   type)r<   batchkeyr(   r(   rA   __convert_input_dtype__k   s
   z!AlphaFold.__convert_input_dtype__c           
   
      s    j jjjr0t|f j jj j jj|d j jjd jd } fddt	|D }	n#t
|f j jj j jjd j jj fddtj|dD }	 j|	|||| jj jj j d	S )
N)r:   epsmultichain_mask_2dr   c                    s&   g | ]   fd dD qS )c              	      s*   g | ]}|d  ddddddf qS .Nr(   .0xtir(   rA   
<listcomp>   s    "zBAlphaFold.embed_templates_pair_core.<locals>.<listcomp>.<listcomp>r    )rd   r<   tzrf   rA   rh   ~   s    z7AlphaFold.embed_templates_pair_core.<locals>.<listcomp>)r:   r_   c                    s   g | ]}  |qS r(   ri   rc   )r<   rl   r(   rA   rh      s    
dim)tri_start_attn_masktri_end_attn_mask	templ_dim
chunk_size
block_sizereturn_mean)r6   r-   r    
v2_featurer   r:   r_   	distogramshaperanger   r7   unbindr!   r+   rr   rs   r1   )
r<   r\   rl   	pair_maskro   rp   rq   r`   num_templatesingle_templatesr(   rj   rA   embed_templates_pair_corer   sH   
z#AlphaFold.embed_templates_pair_corec              	      sz  j jjjr&d|v r&|d dd d d f |d dd d d f kdnd js.jrk|}jrcj||d j	j
d}tj|d ddddk}|d	 |j}||9 }|S |}|S |d
 j}	t|	dkrxdnd |d
 j  }
|
dkrd }n-dd | D  f	dd}|d}td|
D ]}|||7 }q||
 }|}|S )Nasym_id.r   template_mask)r   rr   T)rn   keepdims).NNtemplate_aatype   r   c                 S   s    i | ]\}}| d r||qS )	template_)
startswith)rd   kvr(   r(   rA   
<dictcomp>   s
    z2AlphaFold.embed_templates_pair.<locals>.<dictcomp>c              	      s2    fdd}t |}|	}|S )Nc                    s*   dd | j D }td | < | | S )Nc                 S   s   g | ]}t d qS rB   )slice)rd   _r(   r(   rA   rh      s    zmAlphaFold.embed_templates_pair.<locals>.embed_one_template.<locals>.slice_template_tensor.<locals>.<listcomp>r   )rw   r   )rk   s)batch_templ_dimir(   rA   slice_template_tensor   s   zYAlphaFold.embed_templates_pair.<locals>.embed_one_template.<locals>.slice_template_tensor)r   r}   )r   r   template_featsrk   	r   r`   rz   r<   rq   template_batchrp   ro   rl   )r   rA   embed_one_template   s   z:AlphaFold.embed_templates_pair.<locals>.embed_one_template)r6   r-   r    ru   	unsqueezetrainingr1   r}   r2   r+   rr   r7   sumr[   r9   r3   rw   lenitemsrx   )r<   r\   rl   rz   ro   rp   rq   rk   t_masktemplate_aatype_shapen_templr   r   r(   r   rA   embed_templates_pair   sZ   

'%
zAlphaFold.embed_templates_pairc                 C   s*   t || jjjjd\}}| |}||fS )N)ru   )r   r6   r-   r    ru   r   )r<   r\   template_angle_feattemplate_angle_maskrk   r(   r(   rA   embed_templates_angle   s   


zAlphaFold.embed_templates_anglec                 C   s  |d j d d }|d j d }|d }|d |dd d d f  }|d }	| |d |d \}
}|d u rF|
jg ||| jjjR dd	}|d u r]|jg |||| jjjR dd	}|d u rr|jg ||tjd
R dd	}t|d |d }|| j	
|7 }| 	||\}}|
ddd d d d f  |7  < ||7 }|| j|d  |dd |dd |dd |dd 7 }|
| j}
|| j}t|| j\}}| jjjr|d }t|rt|| j|||||dd| j}| jjjr| t|}t|d | jdd}| j|||d | jj| jj ||d ||d
}| jjj!r=| "|\}}tj#|
|gdd}
tj#|d |gdd}	t|	| jd\}}| j$|
||	|||||| jj| jj d
\}
}}|
|||	||fS )Ntarget_featseq_maskrb   .msa_maskmsa_featF)requires_gradr   aatyper   residue_indexsym_idr~   	entity_idnum_symr   ra   )rq   extra_msa_mask)r:   gen_col_mask)r   rr   rs   rz   msa_row_attn_maskmsa_col_attn_maskro   rp   rm   )r:   )r   rz   r   r   ro   rp   rr   rs   )%rw   r   	new_zerosr6   d_msad_pairr   atom_type_numr   r   
recyle_pos
relpos_emblonggetr[   r9   r   r:   r-   r0   r7   anyr	   r   r   r.   r#   r   r   r$   r+   rr   rs   embed_anglesr   catr4   )r<   featsm_1_prevz_prevx_prev
batch_dimsnr   rz   r   mrl   m_1_prev_emb
z_prev_embro   rp   r   aextra_msa_row_masktemplate_1d_feattemplate_1d_maskmsa_row_maskmsa_col_maskr   r(   r(   rA   iteration_evoformer   s    







zAlphaFold.iteration_evoformerr   c                    s  d\}}	|d j d }
|dksJ t|D ]+}|| |   fdd}t||}| ||||\}}}}}}||7 }|	|7 }	~~q|dkrQ|t| }|	t| }	i }|dd |
d d d d f |d< ||d	< |	|d
< t| dds||d kr|}|ddd d d d f |  |ddd d d d f< ||  }||d< ||d< ||d< | j|	||d |d d|d< t|d d ||d< |d |d< |d d d |d< t| dds|ddd d d d f  }| }|d  }n|ddd d d d f }|}|d }||||fS )N)r   r   r   r   r   c                    s   | t | jd d  df S )Nr   r   .)minrw   )rk   idxr(   rA   fetch_cur_batche  s   zGAlphaFold.iteration_evoformer_structure_module.<locals>.fetch_cur_batch.msapairsinglerG   Fr   	delta_msa
delta_pairmsa_norm_maskr   r   )rZ   sm	positionsfinal_atom_positionsatom37_atom_existsfinal_atom_maskframesr   pred_frame_tensor)	rw   rx   r   r   r8   rI   detachr&   r   )r<   r\   r   r   r   cycle_nonum_recyclingnum_ensemblesrl   r   n_seqensemble_nor   r   r   z0s0r   r   r   outputsr   r   r(   r   rA   $iteration_evoformer_structure_moduleV  sp   
 
z.AlphaFold.iteration_evoformer_structure_modulec                 C   s  | dd }| dd }| dd }t }t|d d }t|d jd | }| jr2|dks2J | |}t|D ]2}||d k}	t|oH|	 | j	|||||||d\}
}}}W d    n1 sew   Y  |	sm~
q;d	|v rz|d	 d
 |
d	< |

| |
 |
S )Nr   r   r   num_recycling_itersr   r   r   )r   r   r   r~   )r   .)r   r7   is_grad_enabledintrw   r   r^   rx   set_grad_enabledr   updater5   )r<   r\   r   r   r   r   	num_itersr   r   is_final_iterr   r(   r(   rA   forward  sD   
zAlphaFold.forward)r   )__name__
__module____qualname__r*   rD   rH   rK   r;   rY   r^   r}   r   r   r   r   r   __classcell__r(   r(   r?   rA   r      s    .
)By
Er   )&r7   torch.nnrP   unicore.utilsr   datar   
attentionsr   r   auxillary_headsr   commonr	   	embeddersr
   r   r   r   r   r4   r   r   featurizationr   r   r   r   r   r   r&   r   r-   r   r   r   Moduler   r(   r(   r(   rA   <module>   s    