
    rh3                        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 d dlmZ  G d	 d
ej                        Zy)    )annotations)Iterable)AnyN)Tensornn)util)SentenceTransformer)all_gather_with_gradc                  b     e Zd Zdej                  df	 	 	 	 	 	 	 d fdZddZd	dZd
dZ xZ	S )%MultipleNegativesSymmetricRankingLossg      4@Fc                    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. In some literature, the scaling parameter
                is referred to as temperature, which is the inverse of the scale. In short: scale = 1 / temperature, so
                scale=20.0 is equivalent to temperature=0.05.
            similarity_fct: similarity function between sentence embeddings. By default, cos_sim. Can also be set to
                dot product (and then set scale to 1)
            gather_across_devices: If True, gather the embeddings across all devices before computing the loss.
                Recommended when training on multiple GPUs, as it allows for larger batch sizes, but it may slow down
                training due to communication overhead, and can potentially lead to out-of-memory errors.

        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_fctgather_across_devicesr   CrossEntropyLosscross_entropy_loss)selfr   r   r   r   	__class__s        /var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/sentence_transformers/losses/MultipleNegativesSymmetricRankingLoss.pyr   z.MultipleNegativesSymmetricRankingLoss.__init__   s@    N 	

,%:""$"5"5"7    c                r    |D cg c]  }| j                  |      d    }}| j                  ||      S c c}w )Nsentence_embedding)r   compute_loss_from_embeddings)r   sentence_featureslabelssentence_feature
embeddingss        r   forwardz-MultipleNegativesSymmetricRankingLoss.forward]   s?    arsM]djj!123GHs
s00VDD ts   4c                    |d   }|dd  }|j                  d      }d}| j                  rdt        |      }|D cg c]  }t        |       }}t        j                  j                         r#t        j                  j                         }||z  }t        j                  |d      }t        j                  |||z   |j                        }	| j                  rE| j                  ||	   |      | j                  z  }
| j                  ||	   |      | j                  z  }n4| j                  ||      | j                  z  }
|
d d d |f   j                  }| j                  |
|	      }| j                  ||	      }||z   dz  S c c}w )Nr      )dim)device   )sizer   r
   torchdistributedis_initializedget_rankcataranger%   r   r   Tr   )r   r    r   anchors
candidates
batch_sizeoffsetembedding_columnrankrange_labelsforward_scoresbackward_scoresforward_lossbackward_losss                 r   r   zBMultipleNegativesSymmetricRankingLoss.compute_loss_from_embeddingsb   sw   Q-^
\\!_
%% +73GYcdEU./?@dJd   //1((113
*YYzq1

 ||FFZ,?W %%!001F
SVZV`V``N"11*\2JGTW[WaWaaO "00*E

RN,Q^<>>O..~|L//N},117 es   E;c                ^    | j                   | j                  j                  | j                  dS )N)r   r   r   )r   r   __name__r   )r   s    r   get_config_dictz5MultipleNegativesSymmetricRankingLoss.get_config_dict   s+    ZZ"11::%)%?%?
 	
r   )r   r	   r   floatr   boolreturnNone)r   zIterable[dict[str, Tensor]]r   r   r?   r   )r    zlist[Tensor]r   r   r?   r   )r?   zdict[str, Any])
r;   
__module____qualname__r   cos_simr   r!   r   r<   __classcell__)r   s   @r   r   r      sT     ||&+L8"L8 L8
  $L8 
L8\E
&2P
r   r   )
__future__r   collections.abcr   typingr   r(   r   r   sentence_transformersr   )sentence_transformers.SentenceTransformerr	   sentence_transformers.utilr
   Moduler    r   r   <module>rM      s/    " $    & I ;A
BII A
r   