
    bi                        d dl mZ d dlmZ d dlmZ d dlZd dlmZmZ d dl	m
Z
 d dlmZ  G d d	ej                  ZdS )
    )annotations)Iterable)AnyN)Tensornn)util)SentenceTransformerc                  `     e Zd Zdej        fd fdZddZddZddZe	dd            Z
 xZS )
CoSENTLossg      4@modelr	   scalefloatreturnNonec                r    t                                                       || _        || _        || _        dS )a  
        This class implements CoSENT (Consistent SENTence embedding) loss.
        It expects that each of the InputExamples consists of a pair of texts and a float valued label, representing
        the expected similarity score between the pair.

        It computes the following loss function:

        ``loss = logsum(1+exp(s(k,l)-s(i,j))+exp...)``, where ``(i,j)`` and ``(k,l)`` are any of the input pairs in the
        batch such that the expected similarity of ``(i,j)`` is greater than ``(k,l)``. The summation is over all possible
        pairs of input pairs in the batch that match this condition.

        Anecdotal experiments show that this loss function produces a more powerful training signal than :class:`CosineSimilarityLoss`,
        resulting in faster convergence and a final model with superior performance. Consequently, CoSENTLoss may be used
        as a drop-in replacement for :class:`CosineSimilarityLoss` in any training script.

        Args:
            model: SentenceTransformerModel
            similarity_fct: Function to compute the PAIRWISE similarity
                between embeddings. Default is
                ``util.pairwise_cos_sim``.
            scale: Output of similarity function is multiplied by scale
                value. Represents the inverse temperature.

        References:
            - For further details, see: https://penghao-bdsc.github.io/papers/CoSENT_TASLP2024.pdf

        Requirements:
            - Sentence pairs with corresponding similarity scores in range of the similarity function. Default is [-1,1].

        Inputs:
            +--------------------------------+------------------------+
            | Texts                          | Labels                 |
            +================================+========================+
            | (sentence_A, sentence_B) pairs | float similarity score |
            +--------------------------------+------------------------+

        Relations:
            - :class:`AnglELoss` is CoSENTLoss with ``pairwise_angle_sim`` as the metric, rather than ``pairwise_cos_sim``.
            - :class:`CosineSimilarityLoss` seems to produce a weaker training signal than CoSENTLoss. In our experiments, CoSENTLoss is recommended.

        Example:
            ::

                from sentence_transformers import SentenceTransformer, SentenceTransformerTrainer, losses
                from datasets import Dataset

                model = SentenceTransformer("microsoft/mpnet-base")
                train_dataset = Dataset.from_dict({
                    "sentence1": ["It's nice weather outside today.", "He drove to work."],
                    "sentence2": ["It's so sunny.", "She walked to the store."],
                    "score": [1.0, 0.3],
                })
                loss = losses.CoSENTLoss(model)

                trainer = SentenceTransformerTrainer(
                    model=model,
                    train_dataset=train_dataset,
                    loss=loss,
                )
                trainer.train()
        N)super__init__r   similarity_fctr   )selfr   r   r   	__class__s       b/root/projects/butler/venv/lib/python3.11/site-packages/sentence_transformers/losses/CoSENTLoss.pyr   zCoSENTLoss.__init__   s6    | 	
,


    sentence_featuresIterable[dict[str, Tensor]]labelsr   c                L      fd|D             }                      ||          S )Nc                F    g | ]}                     |          d          S )sentence_embedding)r   ).0sentence_featurer   s     r   
<listcomp>z&CoSENTLoss.forward.<locals>.<listcomp>R   s-    sssM]djj!1223GHsssr   )compute_loss_from_embeddings)r   r   r   
embeddingss   `   r   forwardzCoSENTLoss.forwardQ   s3    ssssarsss
00VDDDr   r#   list[Tensor]c                   |                      |d         |d                   }|| j        z  }|dddf         |dddf         z
  }|dddf         |dddf         k     }|                                }|d|z
  dz  z
  }t          j        t          j        d                              |j                  |                    d          fd          }t          j	        |d          }|S )z
        Compute the CoSENT loss from embeddings.

        Args:
            embeddings: List of embeddings
            labels: Labels indicating the similarity scores of the pairs

        Returns:
            Loss value
        r      Ng   mB)dim)
r   r   r   torchcatzerostodeviceview	logsumexp)r   r#   r   scoreslosss        r   r"   z'CoSENTLoss.compute_loss_from_embeddingsV   s     $$Z]JqMBB$*$46$'?2 46$'?2 1v:-- EKNN--fm<<fkk"ooNTUVVVv1---r   dict[str, Any]c                *    | j         | j        j        dS )N)r   r   )r   r   __name__r   s    r   get_config_dictzCoSENTLoss.get_config_dicts   s    t7J7STTTr   strc                    dS )Na|  
@article{10531646,
    author={Huang, Xiang and Peng, Hao and Zou, Dongcheng and Liu, Zhiwei and Li, Jianxin and Liu, Kay and Wu, Jia and Su, Jianlin and Yu, Philip S.},
    journal={IEEE/ACM Transactions on Audio, Speech, and Language Processing},
    title={CoSENT: Consistent Sentence Embedding via Similarity Ranking},
    year={2024},
    doi={10.1109/TASLP.2024.3402087}
}
 r6   s    r   citationzCoSENTLoss.citationv   s     r   )r   r	   r   r   r   r   )r   r   r   r   r   r   )r#   r%   r   r   r   r   )r   r3   )r   r8   )r5   
__module____qualname__r   pairwise_cos_simr   r$   r"   r7   propertyr;   __classcell__)r   s   @r   r   r      s        BFW[Wl A A A A A A AFE E E E
   :U U U U 	 	 	 X	 	 	 	 	r   r   )
__future__r   collections.abcr   typingr   r*   r   r   sentence_transformersr   )sentence_transformers.SentenceTransformerr	   Moduler   r:   r   r   <module>rG      s    " " " " " " $ $ $ $ $ $                & & & & & & I I I I I Is s s s s s s s s sr   