
    rh                    V    d dl mZ d dlmZmZ d dlmZ  G d dej                        Zy)    )annotations)Tensornn)CrossEncoderc                  J     e Zd Z ej                         fd fdZddZ xZS )CrossEntropyLossc                   t         |           || _        || _        t	        j
                  di || _        t        | j                  t              s8t        | j                  j                   dt        | j                         d      y)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       /var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/sentence_transformers/cross_encoder/losses/CrossEntropyLoss.pyr   zCrossEntropyLoss.__init__	   s    ^ 	
***4V4$**l3>>**+ ,++/

+;*<A?  4    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                  |      }| j                  ||      }|S )
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   forwardzCrossEntropyLoss.forwardC   s    v;!hilmsithuu~  SF1I./%%	 & 
 			$**##$%f%a(##F+||FF+r   )r   r   r   z	nn.ModulereturnNone)r'   zlist[list[str]]r(   r   r.   r   )r   
__module____qualname__r   Identityr   r-   __classcell__)r   s   @r   r   r      s    GRr{{} 8tr   r   N)	
__future__r   torchr   r   0sentence_transformers.cross_encoder.CrossEncoderr   Moduler   r   r   r   <module>r8      s     "  ILryy Lr   