
    h                    V    S SK Jr  S SKJrJr  S SKJr   " S S\R                  5      rg)    )annotations)Tensornn)CrossEncoderc                  Z   ^  \ rS rSr\R
                  " 5       4SU 4S jjjrSS jrSrU =r	$ )CrossEntropyLoss   c                  > [         TU ]  5         Xl        X l        [        R
                  " S0 UD6U l        [        U R                  [        5      (       d8  [        U R                  R                   S[        U R                  5       S35      eg)a  
Computes the Cross Entropy Loss for a CrossEncoder model. This loss is used to train a model to predict the
correct class label for a given pair of sentences. The number of classes should be equal to the number of model
output labels.

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`.
    **kwargs: Additional keyword arguments passed to the underlying :class:`torch.nn.CrossEntropyLoss`.

References:
    - :class:`torch.nn.CrossEntropyLoss`
    - `Cross Encoder > Training Examples > Natural Language Inference <../../../examples/cross_encoder/training/nli/README.html>`_

Requirements:
    1. Your model can be initialized with `num_labels > 1` to predict multiple classes.
    2. The number of dataset classes should be equal to the number of model output labels (`model.num_labels`).

Inputs:
    +-------------------------------------------------+--------+-------------------------------+
    | Texts                                           | Labels | Number of Model Output Labels |
    +=================================================+========+===============================+
    | (sentence_A, sentence_B) pairs                  | class  | `num_classes`                 |
    +-------------------------------------------------+--------+-------------------------------+

Example:
    ::

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

        model = CrossEncoder("microsoft/mpnet-base", num_labels=2)
        train_dataset = Dataset.from_dict({
            "sentence1": ["How can I be a good geologist?", "What is the capital of France?"],
            "sentence2": ["What should I do to be a great geologist?", "What is the capital of Germany?"],
            "label": [1, 0],  # 1: duplicate, 0: not duplicate
        })
        loss = losses.CrossEntropyLoss(model)

        trainer = CrossEncoderTrainer(
            model=model,
            train_dataset=train_dataset,
            loss=loss,
        )
        trainer.train()
z? expects a model of type CrossEncoder, but got a model of type .N )super__init__modelactivation_fnr   r   ce_loss
isinstancer   
ValueError	__class____name__type)selfr   r   kwargsr   s       u/home/james-whalen/.local/lib/python3.13/site-packages/sentence_transformers/cross_encoder/losses/CrossEntropyLoss.pyr   CrossEntropyLoss.__init__	   s    ^ 	
***4V4$**l33>>**+ ,++/

+;*<A?  4    c                z   [        U5      S:w  a  [        S[        U5       S35      e[        [        US   US   5      5      nU R                  R                  USSSS9nUR                  U R                  R                  5        U R                  " S	0 UD6S   nU R                  U5      nU R                  XR5      nU$ )
N   zVCrossEntropyLoss expects a dataset with two non-label columns, but got a dataset with z	 columns.r      Tpt)padding
truncationreturn_tensorsr   )
lenr   listzipr   	tokenizertodevicer   r   )r   inputslabelspairstokenslogitslosss          r   forwardCrossEntropyLoss.forwardC   s    v;!hilmsithuu~  SF1I./%%	 & 
 			$**##$%f%a(##F+||F+r   )r   r   r   )r   r   r   z	nn.ModulereturnNone)r)   zlist[list[str]]r*   r   r1   r   )
r   
__module____qualname____firstlineno__r   Identityr   r/   __static_attributes____classcell__)r   s   @r   r   r      s!    GI{{} 8 8t r   r   N)	
__future__r   torchr   r   0sentence_transformers.cross_encoder.CrossEncoderr   Moduler   r   r   r   <module>r=      s     "  ILryy Lr   