
    +sg                        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y)
    )annotations)Iterable)AnyN)Tensornn)util)SentenceTransformerc                  J     e Zd Zdej                  fd fdZddZddZ xZS )%MultipleNegativesSymmetricRankingLossg      4@c                ~    t         |           || _        || _        || _        t        j                         | _        y)a  
        Given a list of (anchor, positive) pairs, this loss sums the following two losses:

        1. Forward loss: Given an anchor, find the sample with the highest similarity out of all positives in the batch.
           This is equivalent to :class:`MultipleNegativesRankingLoss`.
        2. Backward loss: Given a positive, find the sample with the highest similarity out of all anchors in the batch.

        For example with question-answer pairs, :class:`MultipleNegativesRankingLoss` just computes the loss to find
        the answer given a question, but :class:`MultipleNegativesSymmetricRankingLoss` additionally computes the
        loss to find the question given an answer.

        Note: If you pass triplets, the negative entry will be ignored. A anchor is just searched for the positive.

        Args:
            model: SentenceTransformer model
            scale: Output of similarity function is multiplied by scale
                value
            similarity_fct: similarity function between sentence
                embeddings. By default, cos_sim. Can also be set to dot
                product (and then set scale to 1)

        Requirements:
            1. (anchor, positive) pairs

        Inputs:
            +---------------------------------------+--------+
            | Texts                                 | Labels |
            +=======================================+========+
            | (anchor, positive) pairs              | none   |
            +---------------------------------------+--------+

        Recommendations:
            - Use ``BatchSamplers.NO_DUPLICATES`` (:class:`docs <sentence_transformers.training_args.BatchSamplers>`) to
              ensure that no in-batch negatives are duplicates of the anchor or positive samples.

        Relations:
            - Like :class:`MultipleNegativesRankingLoss`, but with an additional loss term.
            - :class:`CachedMultipleNegativesSymmetricRankingLoss` is equivalent to this loss, but it uses caching that
              allows for much higher batch sizes (and thus better performance) without extra memory usage. However, it
              is slightly slower.

        Example:
            ::

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

                model = SentenceTransformer("microsoft/mpnet-base")
                train_dataset = Dataset.from_dict({
                    "anchor": ["It's nice weather outside today.", "He drove to work."],
                    "positive": ["It's so sunny.", "He took the car to the office."],
                })
                loss = losses.MultipleNegativesSymmetricRankingLoss(model)

                trainer = SentenceTransformerTrainer(
                    model=model,
                    train_dataset=train_dataset,
                    loss=loss,
                )
                trainer.train()
        N)super__init__modelscalesimilarity_fctr   CrossEntropyLosscross_entropy_loss)selfr   r   r   	__class__s       u/var/www/html/venv/lib/python3.12/site-packages/sentence_transformers/losses/MultipleNegativesSymmetricRankingLoss.pyr   z.MultipleNegativesSymmetricRankingLoss.__init__   s8    | 	

,"$"5"5"7    c                   |D cg c]  }| j                  |      d    }}|d   }t        j                  |dd        }| j                  ||      | j                  z  }t        j
                  t        t        |            t        j                  |j                        }|d d dt        |d         f   }| j                  ||      }	| j                  |j                  dd      |      }
|	|
z   dz  S c c}w )Nsentence_embeddingr      )dtypedevice   )r   torchcatr   r   tensorrangelenlongr   r   	transpose)r   sentence_featureslabelssentence_featurerepsanchor
candidatesscoresanchor_positive_scoresforward_lossbackward_losss              r   forwardz-MultipleNegativesSymmetricRankingLoss.forwardR   s    [lmGW

+,-ABmmaYYtABx(
$$VZ84::E#f+ejj
 "(1s47|+;(;!<..vv>//0F0P0PQRTU0VX^_},11 ns   C3c                H    | j                   | j                  j                  dS )N)r   r   )r   r   __name__)r   s    r   get_config_dictz5MultipleNegativesSymmetricRankingLoss.get_config_dicta   s    t7J7J7S7STTr   )r   r	   r   floatreturnNone)r%   zIterable[dict[str, Tensor]]r&   r   r4   r   )r4   zdict[str, Any])	r1   
__module____qualname__r   cos_simr   r/   r2   __classcell__)r   s   @r   r   r      s!    BFW[WcWc B8H2Ur   r   )
__future__r   collections.abcr   typingr   r   r   r   sentence_transformersr   )sentence_transformers.SentenceTransformerr	   Moduler    r   r   <module>rA      s.    " $    & IUUBII UUr   