
    qiM                     n   d dl Z de j                  de j                  fdZde j                  de j                  dee j                  e j                  f   fdZ	 	 dde j                  d	ed
edeee j                  f   fdZ	 	 	 	 dde j                  de j                  dz  d	ed
ede	de j                  fdZ
y)    N
boundariesreturnc                     | d   | d   z
  }| |dz  z   }t        j                  ||d   |z   j                  d      gd      }|S )N   r      dim)torchcat	unsqueeze)r   stepbin_centerss      ]/opt/pipecat/venv/lib/python3.12/site-packages/transformers/models/esm/openfold_utils/loss.py_calculate_bin_centersr      sR    a=:a=(Dtax'K))[;r?T+A*L*LR*PQWXYK    alignment_confidence_breaksaligned_distance_error_probsc                 V    t        |       }t        j                  ||z  d      |d   fS )Nr   r	   )r   r   sum)r   r   r   s      r   !_calculate_expected_aligned_errorr      s4     ))DEK		.<"EB r   logitsmax_binno_binsc                     t        j                  d||dz
  | j                        }t         j                  j                  j                  | d      }t        ||      \  }}|||dS )aP  Computes aligned confidence metrics from logits.

    Args:
      logits: [*, num_res, num_res, num_bins] the logits output from
        PredictedAlignedErrorHead.
      max_bin: Maximum bin value
      no_bins: Number of bins
    Returns:
      aligned_confidence_probs: [*, num_res, num_res, num_bins] the predicted
        aligned error probabilities over bins for each residue pair.
      predicted_aligned_error: [*, num_res, num_res] the expected aligned distance
        error for each pair of residues.
      max_predicted_aligned_error: [*] the maximum predicted error possible.
    r   r   stepsdevicer   r	   )r   r   )aligned_confidence_probspredicted_aligned_errormax_predicted_aligned_error)r   linspacer   nn
functionalsoftmaxr   )r   r   r   kwargsr   r   r    r!   s           r   compute_predicted_aligned_errorr'   &   sl    ( 77Q;VJ$xx22::6r:J;\$.%=<88 %=#:'B r   residue_weightsepsc                    || j                  | j                  d         }t        j                  d||dz
  | j                        }t        |      }t        j                  |       | j                  d   }t        |d      }	d|	dz
  dz  z  d	z
  }
t        j                  j                  j                  | d
      }dd|dz  |
dz  z  z   z  }t        j                  ||z  d
      }|||j                         z   z  }t        j                  ||z  d
      }||z  }|t        j                  |      k(  j                         d   }|t        |         S )Nr   r   r      gףp=
?   gUUUUUU?g?r   r	   g      ?r   )new_onesshaper   r"   r   r   r   maxr#   r$   r%   nonzerotuple)r   r(   r   r   r)   r&   r   r   n	clipped_nd0probs
tm_per_binpredicted_tm_termnormed_residue_maskper_alignmentweightedargmaxs                     r   
compute_tmr=   I   s?     //&,,r*:;77Q;VJ(4K	IIoRAAr
I	RW-	-	3BHH''B'7E[!^A667J		%*"4"=)S?3F3F3H-HIII/2EE2NM.H%))H--668;Fv''r   )   @   )Nr>   r?   g:0yE>)r   Tensorr   r2   r   intdictstrr'   floatr=    r   r   <module>rF      s   " u||  !&"',, 5<<%&  LL    
 
#u||
 J ,0(LL(\\D(( ( 	(
 
( \\(r   