
     `il                         d dl mZ 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ej                 dedededej        fdZdS )    )OptionalN
boundariesreturnc                     | d         | d         z
  }| |dz  z   }t          j        ||d         |z                       d          gd          }|S )N   r      dim)torchcat	unsqueeze)r   stepbin_centerss      /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/models/esm/openfold_utils/loss.py_calculate_bin_centersr      sY    a=:a=(Dtax'K)[;r?T+A*L*LR*P*PQWXYYYK    alignment_confidence_breaksaligned_distance_error_probsc                 b    t          |           }t          j        ||z  d          |d         fS )Nr	   r
   )r   r   sum)r   r   r   s      r   !_calculate_expected_aligned_errorr      s;     ))DEEK	.<"EEEB r      @   logitsmax_binno_binsc                     t          j        d||dz
  | j                  }t           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*   '   s{    ( 77Q;VVVJ$x2::6r:JJ;\$.%=< < <88 %=#:'B  r   :0yE>residue_weightsepsc                    | |                      | 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	        
                    | d
          }dd|dz  |
dz  z  z   z  }t          j        ||z  d
          }|||                                z   z  }t          j        ||z  d
          }||z  }|t          j        |          k                                    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_tmrA   J   sX     //&,r*:;;77Q;VVVJ(44K	IoRAAr

I	RW-	-	3BH''B'77E[!^A667J	%*"4"===)S?3F3F3H3H-HII/2EE2NNNM.H%)H---6688;Fv''r   )r   r   )Nr   r   r+   )typingr   r   Tensorr   r6   r   intdictstrr*   floatrA    r   r   <module>rI      sP          u|     !&"', 5<%&       L    
 
#u|
       J /3( (L(el+( ( 	(
 
( \( ( ( ( ( (r   