
    Ch                        S SK Jr  S SKrS SKrS SKJr  S SKJr  S SKJ	r	  SSK
JrJrJrJr  \	R                  " \5      rSS jrSS	 jrSS
 jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrg)    )annotationsN)pairwise_distances)Tensor)logging   )_convert_to_batch_tensor_convert_to_tensornormalize_embeddingsto_scipy_cooc                    [        X5      $ )
Computes the cosine similarity between two tensors.

Args:
    a (Union[list, np.ndarray, Tensor]): The first tensor.
    b (Union[list, np.ndarray, Tensor]): The second tensor.

Returns:
    Tensor: Matrix with res[i][j] = cos_sim(a[i], b[j])
)cos_simabs     ]/var/www/html/shao/venv/lib/python3.13/site-packages/sentence_transformers/util/similarity.pypytorch_cos_simr      s     1=    c                    [        U 5      n [        U5      n[        U 5      n[        U5      n[        R                  " X#R	                  SS5      5      R                  5       $ )r   r   r   )r   r
   torchmm	transposeto_denser   r   a_normb_norms       r   r   r      sQ     	!#A #A!!$F!!$F88F,,Q23<<>>r   c                4   [        U 5      n [        U5      nU R                  (       d  UR                  (       a5  [        U 5      n[        U5      nX#-  R                  SS9R	                  5       $ [        [        U 5      [        U5      5      R	                  5       $ )z
Computes the pairwise cosine similarity cos_sim(a[i], b[i]).

Args:
    a (Union[list, np.ndarray, Tensor]): The first tensor.
    b (Union[list, np.ndarray, Tensor]): The second tensor.

Returns:
    Tensor: Vector with res[i] = cos_sim(a[i], b[i])
dim)r	   	is_sparser
   sumr   pairwise_dot_scorer   s       r   pairwise_cos_simr$   0   s}     	1A1A 	{{akk%a(%a($$$,5577!"6q"9;OPQ;RS\\^^r   c                    [        U 5      n [        U5      n[        R                  " XR                  SS5      5      R	                  5       $ )a  
Computes the dot-product dot_prod(a[i], b[j]) for all i and j.

Args:
    a (Union[list, np.ndarray, Tensor]): The first tensor.
    b (Union[list, np.ndarray, Tensor]): The second tensor.

Returns:
    Tensor: Matrix with res[i][j] = dot_prod(a[i], b[j])
r   r   )r   r   r   r   r   r   s     r   	dot_scorer&   G   s;     	!#A #A88A{{1a()2244r   c                l    [        U 5      n [        U5      nX-  R                  SS9R                  5       $ )z
Computes the pairwise dot-product dot_prod(a[i], b[i]).

Args:
    a (Union[list, np.ndarray, Tensor]): The first tensor.
    b (Union[list, np.ndarray, Tensor]): The second tensor.

Returns:
    Tensor: Vector with res[i] = dot_prod(a[i], b[i])
r   r   )r	   r"   r   r   s     r   r#   r#   X   s4     	1A1AE;;2;''))r   c                   [        U 5      n [        U5      nU R                  (       d  UR                  (       a  [        R                  S5        [	        U 5      n[	        U5      n[        X#SS9n[        R                  " U* 5      R                  5       R                  U R                  5      R                  5       $ [        R                  " XSS9R                  5       * $ )a`  
Computes the manhattan similarity (i.e., negative distance) between two tensors.
Handles sparse tensors without converting to dense when possible.

Args:
    a (Union[list, np.ndarray, Tensor]): The first tensor.
    b (Union[list, np.ndarray, Tensor]): The second tensor.

Returns:
    Tensor: Matrix with res[i][j] = -manhattan_distance(a[i], b[j])
z8Using scipy for sparse Manhattan similarity computation.	manhattan)metricg      ?p)r   r!   loggerwarning_oncer   r   r   
from_numpyfloattodevicer   cdist)r   r   a_coob_coodists        r   manhattan_simr7   i   s     	!#A #A{{akkVWQQ!%{C&,,.11!((;DDFF AC(11333r   c                    [        U 5      n [        U5      n[        R                  " [        R                  " X-
  5      SS9R	                  5       * $ )a   
Computes the manhattan similarity (i.e., negative distance) between pairs of tensors.

Args:
    a (Union[list, np.ndarray, Tensor]): The first tensor.
    b (Union[list, np.ndarray, Tensor]): The second tensor.

Returns:
    Tensor: Vector with res[i] = -manhattan_distance(a[i], b[i])
r   r   )r	   r   r"   absr   r   s     r   pairwise_manhattan_simr:      s@     	1A1AIIeii&B/88:::r   c                ^   [        U 5      n [        U5      nU R                  (       a  [        R                  R	                  X -  SS9R                  5       R                  S5      n[        R                  R	                  X-  SS9R                  5       R                  S5      n[        R                  " XR                  5       5      R                  5       nUSU-  -
  U-   n[        R                  " USS9n[        R                  " U5      R                  5       * $ [        R                  " XSS9* $ )	a`  
Computes the euclidean similarity (i.e., negative distance) between two tensors.
Handles sparse tensors without converting to dense when possible.

Args:
    a (Union[list, np.ndarray, Tensor]): The first tensor.
    b (Union[list, np.ndarray, Tensor]): The second tensor.

Returns:
    Tensor: Matrix with res[i][j] = -euclidean_distance(a[i], b[j])
r   r   r      g        )ming       @r+   )r   r!   r   sparser"   r   	unsqueezematmultclampsqrtr3   )r   r   	a_norm_sq	b_norm_sqdot_productsquared_dists         r   euclidean_simrH      s     	!#A #A{{LL$$QU$2;;=GGJ	LL$$QU$2;;=GGJ	ll1cce,557 !1{?2Y> {{<S9

<(11333AC(((r   c                    [        U 5      n [        U5      n[        R                  " [        R                  " X-
  S-  SS95      R	                  5       * $ )a  
Computes the euclidean distance (i.e., negative distance) between pairs of tensors.

Args:
    a (Union[list, np.ndarray, Tensor]): The first tensor.
    b (Union[list, np.ndarray, Tensor]): The second tensor.

Returns:
    Tensor: Vector with res[i] = -euclidean_distance(a[i], b[i])
r<   r   r   )r	   r   rC   r"   r   r   s     r   pairwise_euclidean_simrJ      sD     	1A1AJJuyy!%A267@@BBBr   c                   U R                   (       aQ  [        R                  S5        U R                  5       R	                  5       n UR                  5       R	                  5       n[        U 5      n [        U5      n[        R                  " U SSS9u  p#[        R                  " USSS9u  pE[        R                  " US-  US-  -   SSS9nX$-  X5-  -   U-  nX4-  X%-  -
  U-  n[        R                  " US-  US-  -   SSS9S-  n	[        R                  " US-  US-  -   SSS9S-  n
XyU
-  -  nXU
-  -  n[        R                  " [        R                  " Xx4SS9SS9n[        R                  " U5      $ )a0  
Computes the absolute normalized angle distance. See :class:`~sentence_transformers.losses.AnglELoss`
or https://arxiv.org/abs/2309.12871v1 for more information.

Args:
    x (Tensor): The first tensor.
    y (Tensor): The second tensor.

Returns:
    Tensor: Vector with res[i] = angle_sim(a[i], b[i])
zOPairwise angle similarity does not support sparse tensors. Converting to dense.r<   r   r   T)r    keepdimg      ?)r!   r-   r.   coalescer   r	   r   chunkr"   concatr9   )xyr   r   cdzreimdzdw
norm_angles               r   pairwise_angle_simrZ      sJ    	{{mnJJL!!#JJL!!#1A1A ;;q!#DA;;q!#DA		!Q$A+1d3A
%!%-1	B
%!%-1	B	1a4!Q$;At	4	;B	1a4!Q$;At	4	;Br'MBr'MB5<<a8a@J99Z  r   )r   r   r   r   returnr   )r   list | np.ndarray | Tensorr   r\   r[   r   )r   r\   r   r\   )rP   r   rQ   r   r[   r   )
__future__r   numpynpr   sklearn.metricsr   r   transformers.utilsr   tensorr   r	   r
   r   
get_logger__name__r-   r   r   r$   r&   r#   r7   r:   rH   rJ   rZ    r   r   <module>rf      sc    "   .  & d d 
		H	%?&_.5"*"46;")>C"#!r   