ó
    — óh  ã                  ób   • S SK Jr  S SKJr  S SKJr  S SKJr  S SKJ	r	  S SK
Jr   " S S\	5      rg)	é    )Úannotations)ÚIterableN)ÚTensor)ÚCosineSimilarityLoss)ÚSparseEncoderc                  óˆ   ^ • \ rS rSr\R
                  " 5       \R                  " 5       4       SU 4S jjjrSS jrSr	U =r
$ )ÚSparseCosineSimilarityLossé   c                ó.   >• SUl         [        TU ]	  XUS9$ )aÕ  
SparseCosineSimilarityLoss expects that the InputExamples consists of two texts and a float label. It computes the
vectors ``u = model(sentence_A)`` and ``v = model(sentence_B)`` and measures the cosine-similarity between the two.
By default, it minimizes the following loss: ``||input_label - cos_score_transformation(cosine_sim(u,v))||_2``.

Args:
    model: SparseEncoder model
    loss_fct: Which pytorch loss function should be used to
        compare the ``cosine_similarity(u, v)`` with the
        input_label? By default, MSE is used: ``||input_label -
        cosine_sim(u, v)||_2``
    cos_score_transformation: The cos_score_transformation
        function is applied on top of cosine_similarity. By
        default, the identify function is used (i.e. no change).

Requirements:
    - Need to be used in SpladeLoss or CSRLoss as a loss function.
    - Sentence pairs with corresponding similarity scores in range `[0, 1]`

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

Relations:
    - :class:`SparseAnglELoss` is :class:`SparseCoSENTLoss` with ``pairwise_angle_sim`` as the metric, rather than ``pairwise_cos_sim``.

Example:
    ::

        from datasets import Dataset
        from sentence_transformers.sparse_encoder import SparseEncoder, SparseEncoderTrainer, losses

        model = SparseEncoder("distilbert/distilbert-base-uncased")
        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.SpladeLoss(
            model=model,
            loss=losses.SparseCosineSimilarityLoss(model),
            document_regularizer_weight=5e-5,
            use_document_regularizer_only=True,
        )

        trainer = SparseEncoderTrainer(model=model, train_dataset=train_dataset, loss=loss)
        trainer.train()
Úcosine)Úloss_fctÚcos_score_transformation)Úsimilarity_fn_nameÚsuperÚ__init__)ÚselfÚmodelr   r   Ú	__class__s       €Ú€/home/james-whalen/.local/lib/python3.13/site-packages/sentence_transformers/sparse_encoder/losses/SparseCosineSimilarityLoss.pyr   Ú#SparseCosineSimilarityLoss.__init__   s$   ø€ ðv $,ˆÔ Ü‰wÑ ÐSkÐÐlÐló    c                ó   • [        S5      e)NzWSparseCosineSimilarityLoss should not be used alone. Use it with SpladeLoss or CSRLoss.)ÚAttributeError)r   Úsentence_featuresÚlabelss      r   ÚforwardÚ"SparseCosineSimilarityLoss.forwardK   s   € ÜÐvÓwÐwr   © )r   r   r   ú	nn.Moduler   r   ÚreturnÚNone)r   zIterable[dict[str, Tensor]]r   r   r    r   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__ÚnnÚMSELossÚIdentityr   r   Ú__static_attributes__Ú__classcell__)r   s   @r   r	   r	      sZ   ø† ð !Ÿjšj›lØ.0¯kªk«mð	<màð<mð ð<mð #,ð	<mð
 
÷<mð <m÷|xò xr   r	   )Ú
__future__r   Úcollections.abcr   Útorch.nnr&   Útorchr   Ú1sentence_transformers.losses.CosineSimilarityLossr   Ú2sentence_transformers.sparse_encoder.SparseEncoderr   r	   r   r   r   Ú<module>r1      s(   ðÝ "å $å Ý å RÝ Lô@xÐ!5õ @xr   