
    Ch                        S SK Jr  S SKJr  S SKJr  S SKrS SKJrJr  S SK	J
r
  S SKJr   " S S	\R                  5      rg)
    )annotations)Iterable)AnyN)Tensornn)util)SentenceTransformerc                  z   ^  \ rS rSrS\R
                  4S	U 4S jjjrS
S jrSS jrSS jr	\
SS j5       rSrU =r$ )
CoSENTLoss   g      4@c                F   > [         TU ]  5         Xl        X0l        X l        g)aM
  
This class implements CoSENT (Cosine Sentence) 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(i,j)-s(k,l))+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://kexue.fm/archives/8847

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__modelsimilarity_fctscale)selfr   r   r   	__class__s       _/var/www/html/shao/venv/lib/python3.13/site-packages/sentence_transformers/losses/CoSENTLoss.pyr   CoSENTLoss.__init__   s!    | 	
,
    c                r    U Vs/ sH  o0R                  U5      S   PM     nnU R                  XB5      $ s  snf )Nsentence_embedding)r   compute_loss_from_embeddings)r   sentence_featureslabelssentence_feature
embeddingss        r   forwardCoSENTLoss.forwardQ   s>    arsarM]jj!123GHar
s00DD ts   4c                   U R                  US   US   5      nX0R                  -  nUSS2S4   USSS24   -
  nUSS2S4   USSS24   :  nUR                  5       nUSU-
  S-  -
  n[        R                  " [        R
                  " S5      R                  UR                  5      UR                  S5      4SS9n[        R                  " USS9nU$ )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   floattorchcatzerostodeviceview	logsumexp)r   r   r   scoreslosss        r   r   'CoSENTLoss.compute_loss_from_embeddingsV   s     $$Z]JqMB**$46$'?2 46$'?2 1v:-- EKKN--fmm<fkk"oNTUVv1-r   c                H    U R                   U R                  R                  S.$ )N)r   r   )r   r   __name__r   s    r   get_config_dictCoSENTLoss.get_config_dicts   s    t7J7J7S7STTr   c                    g)Nz
@online{kexuefm-8847,
    title={CoSENT: A more efficient sentence vector scheme than Sentence-BERT},
    author={Su Jianlin},
    year={2022},
    month={Jan},
    url={https://kexue.fm/archives/8847},
}
 r2   s    r   citationCoSENTLoss.citationv   s    r   )r   r   r   )r   r	   r   r%   returnNone)r   zIterable[dict[str, Tensor]]r   r   r9   r   )r   zlist[Tensor]r   r   r9   r   )r9   zdict[str, Any])r9   str)r1   
__module____qualname____firstlineno__r   pairwise_cos_simr   r   r   r3   propertyr7   __static_attributes____classcell__)r   s   @r   r   r      sA    BFW[WlWl A AFE
:U 	 	r   r   )
__future__r   collections.abcr   typingr   r&   r   r   sentence_transformersr   )sentence_transformers.SentenceTransformerr	   Moduler   r6   r   r   <module>rI      s,    " $    & Is sr   