Ë
    l^(h§  ã                  ób   — d dl m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)ÚTensorÚnn)ÚCrossEncoder)Úfullnamec                  ó`   ‡ — e Zd Z ej                  «       df	 	 	 	 	 	 	 dˆ fd„Zdd„Zd„ Zˆ xZS )ÚBinaryCrossEntropyLossNc                óÌ  •— t         ‰| �  «        || _        || _        || _        t        j                  dd|i|¤Ž| _        t        | j                  t        «      s8t        | j                  j                  › dt        | j                  «      › d�«      ‚| j                  j                  dk7  r9t        | j                  j                  › d| j                  j                  › d�«      ‚y)	a!  
        Computes the Binary Cross Entropy Loss for a CrossEncoder model. This loss is used to train a model to predict
        a high logit for positive pairs and a low logit for negative pairs. The model should be initialized with
        ``num_labels = 1`` (a.k.a. the default) to predict one class.

        It has been used to train many of the strong `CrossEncoder MS MARCO Reranker models <https://huggingface.co/models?author=cross-encoder&search=marco>`_.

        Args:
            model (:class:`~sentence_transformers.cross_encoder.CrossEncoder`): A CrossEncoder model to be trained.
            activation_fn (:class:`~torch.nn.Module`): Activation function applied to the logits before computing the loss. Defaults to :class:`~torch.nn.Identity`.
            pos_weight (Tensor, optional): A weight of positive examples. Must be a :class:`torch.Tensor` like ``torch.tensor(4)`` for a weight of 4. Defaults to None.
            **kwargs: Additional keyword arguments passed to the underlying :class:`torch.nn.BCEWithLogitsLoss`.

        References:
            - :class:`torch.nn.BCEWithLogitsLoss`
            - `Cross Encoder > Training Examples > Semantic Textual Similarity <../../../examples/cross_encoder/training/sts/README.html>`_
            - `Cross Encoder > Training Examples > Quora Duplicate Questions <../../../examples/cross_encoder/training/quora_duplicate_questions/README.html>`_
            - `Cross Encoder > Training Examples > MS MARCO <../../../examples/cross_encoder/training/ms_marco/README.html>`_
            - `Cross Encoder > Training Examples > Rerankers <../../../examples/cross_encoder/training/rerankers/README.html>`_

        Requirements:
            1. Your model must be initialized with `num_labels = 1` (a.k.a. the default) to predict one class.

        Inputs:
            +-------------------------------------------------+----------------------------------------+-------------------------------+
            | Texts                                           | Labels                                 | Number of Model Output Labels |
            +=================================================+========================================+===============================+
            | (anchor, positive/negative) pairs               | 1 if positive, 0 if negative           | 1                             |
            +-------------------------------------------------+----------------------------------------+-------------------------------+
            | (sentence_A, sentence_B) pairs                  | float similarity score between 0 and 1 | 1                             |
            +-------------------------------------------------+----------------------------------------+-------------------------------+

        Recommendations:
            - Use :class:`~sentence_transformers.util.mine_hard_negatives` with ``output_format="labeled-pair"``
              to convert question-answer pairs to the ``(anchor, positive/negative) pairs`` format with labels as 1 or 0,
              using hard negatives.

        Example:
            ::

                from sentence_transformers.cross_encoder import CrossEncoder, CrossEncoderTrainer, losses
                from datasets import Dataset

                model = CrossEncoder("microsoft/mpnet-base")
                train_dataset = Dataset.from_dict({
                    "query": ["What are pandas?", "What are pandas?"],
                    "response": ["Pandas are a kind of bear.", "Pandas are a kind of fish."],
                    "label": [1, 0],
                })
                loss = losses.BinaryCrossEntropyLoss(model)

                trainer = CrossEncoderTrainer(
                    model=model,
                    train_dataset=train_dataset,
                    loss=loss,
                )
                trainer.train()
        Ú
pos_weightz? expects a model of type CrossEncoder, but got a model of type ú.é   z; expects a model with 1 output label, but got a model with z output labels.N© )ÚsuperÚ__init__ÚmodelÚactivation_fnr   r   ÚBCEWithLogitsLossÚbce_with_logits_lossÚ
isinstancer   Ú
ValueErrorÚ	__class__Ú__name__ÚtypeÚ
num_labels)Úselfr   r   r   Úkwargsr   s        €ú/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sentence_transformers/cross_encoder/losses/BinaryCrossEntropyLoss.pyr   zBinaryCrossEntropyLoss.__init__
   sÜ   ø€ ôB 	‰ÑÔØˆŒ
Ø*ˆÔØ$ˆŒÜ$&×$8Ñ$8Ñ$YÀJÐ$YÐRXÑ$YˆÔ!ä˜$Ÿ*™*¤lÔ3ÜØ—>‘>×*Ñ*Ð+ð ,+Ü+/°·
±
Ó+;Ð*<¸Að?óð ð
 �:‰:× Ñ  AÒ%ÜØ—>‘>×*Ñ*Ð+ð ,(Ø(,¯
©
×(=Ñ(=Ð'>¸oðOóð ð &ó    c                óº  — t        |«      dk7  rt        dt        |«      › d�«      ‚t        t        |d   |d   «      «      }| j                  j                  |ddd¬«      }|j                  | j                  j                  «        | j                  d
i |¤Žd   j                  d	«      }| j                  |«      }| j                  ||j                  «       «      }|S )Né   z\BinaryCrossEntropyLoss expects a dataset with two non-label columns, but got a dataset with z	 columns.r   r   TÚpt)ÚpaddingÚ
truncationÚreturn_tensorséÿÿÿÿr   )Úlenr   ÚlistÚzipr   Ú	tokenizerÚtoÚdeviceÚviewr   r   Úfloat)r   ÚinputsÚlabelsÚpairsÚtokensÚlogitsÚlosss          r   ÚforwardzBinaryCrossEntropyLoss.forward]   sß   € Üˆv‹;˜!ÒÜØnÔorÐsyÓozÐn{ð  |Eð  Fóð ô ”S˜ ™ F¨1¡IÓ.Ó/ˆØ—‘×%Ñ%ØØØØð	 &ó 
ˆð 	�	‰	�$—*‘*×#Ñ#Ô$Ø�—‘Ñ%˜fÑ% aÑ(×-Ñ-¨bÓ1ˆØ×#Ñ# FÓ+ˆØ×(Ñ(¨°·±³Ó@ˆØˆr   c                ó–   — t        | j                  «      | j                  €| j                  dœS | j                  j                  «       dœS )N)r   r   )r   r   r   Úitem)r   s    r   Úget_config_dictz&BinaryCrossEntropyLoss.get_config_dictp   sD   € ä% d×&8Ñ&8Ó9Ø-1¯_©_Ð-D˜$Ÿ/™/ñ
ð 	
àJNÏ/É/×J^ÑJ^ÓJ`ñ
ð 	
r   )r   r   r   z	nn.Moduler   zTensor | NoneÚreturnÚNone)r.   zlist[list[str]]r/   r   r8   r   )	r   Ú
__module__Ú__qualname__r   ÚIdentityr   r4   r7   Ú__classcell__)r   s   @r   r	   r	   	   sN   ø„ ð $/ 2§;¡;£=Ø$(ð	QàðQð !ðQð "ð	Qð 
õQófö&
r   r	   N)Ú
__future__r   Útorchr   r   Ú0sentence_transformers.cross_encoder.CrossEncoderr   Úsentence_transformers.utilr   ÚModuler	   r   r   r   ú<module>rC      s#   ðÝ "ç å IÝ /ôk
˜RŸY™Yõ k
r   