o
    ei                  	   @   s4   d Z ddlZ	d
dejdejdedejfdd	ZdS )z&Distance metrics and related functions    N:0yE>abepsreturnc                 C   s   |   |  ksJ d|   dksJ d| jdd |jdd ks(J dtjj| ddd}tjj|ddd}| tj||d	 }|tj||d	 }||dd S )
a  Computes a matrix evaluating all pairwise cosine similarities.
    The cosine similarity can otherwise be determined with
    :class:`torch.nn.CosineSimilarity`.

    Arguments
    ---------
    a : torch.Tensor
        Tensor of shape `[..., X, dim]` where `dim` is the dimension where the
        cosine similarity will be computed and `X` is any value `>= 0`.
    b : torch.Tensor
        Tensor of shape `[..., Y, dim]`, where other dimensions are otherwise
        identical to `a`'s and `Y` is any value `>= 0`.
    eps : float
        Epsilon value for numerical stability, in order to avoid a division by
        zero. Does not significantly affect results.

    Returns
    -------
    torch.Tensor
        Tensor of shape `[..., X, Y]` living on the same device and dtype as the
        input tensors. e.g. ignoring first dimensions `out[3, 0]` would be the
        cosine similarity of `a[3]` and `b[0]`.
    zInputs must be of the same dim   z)Expected at least 2 dims [X, cos_sim_dim]Nz(Input shape must match until last 2 dims)dim)min)r
   shapetorchlinalgvector_norm	unsqueezeclamp	transpose)r   r   r   a_normb_norma_normalizedb_normalized r   Y/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/speechbrain/utils/distances.pycosine_similarity_matrix   s   r   )r   )__doc__r   Tensorfloatr   r   r   r   r   <module>   s    