§
    *�’j$  ã                  ód   — d dl mZ d dlmZmZ d dlmZ d dlmZm	Z	  G d„ dej
        ¦  «        ZdS )é    )Úannotations)ÚTensorÚnn)ÚCrossEncoder)Úbatch_to_deviceÚfullnamec                  óP   ‡ — e Zd Z ej        ¦   «         fdˆ fd„Z	 ddd„Zd„ Zˆ xZS )ÚMSELossÚmodelr   Úactivation_fnú	nn.ModuleÚreturnÚNonec                ó˜  •— t          ¦   «                              ¦   «          || _        || _        t	          j        di |¤Ž| _        t          | j        t          ¦  «        s1t          | j
        j        › dt          | j        ¦  «        › d�¦  «        ‚| j        j        dk    r)t          | j
        j        › d| j        j        › d�¦  «        ‚dS )a¹  
        Computes the MSE loss between the computed query-document score and a target query-document score. This loss
        is used to distill a cross-encoder model from a teacher cross-encoder model or gold labels.

        Args:
            model (:class:`~sentence_transformers.cross_encoder.model.CrossEncoder`): A CrossEncoder model to be trained.
            activation_fn (:class:`~torch.nn.Module`): Activation function applied to the logits before computing the loss.
            **kwargs: Additional keyword arguments passed to the underlying :class:`torch.nn.MSELoss`.

        .. note::

            Be mindful of the magnitude of both the labels and what the model produces. If the teacher model produces
            logits with Sigmoid to bound them to [0, 1], then you may wish to use a Sigmoid activation function in the loss.

        References:
            - Improving Efficient Neural Ranking Models with Cross-Architecture Knowledge Distillation: https://huggingface.co/papers/2010.02666
            - `Cross Encoder > Training Examples > Distillation <../../../examples/cross_encoder/training/distillation/README.html>`_

        Requirements:
            1. Your model must be initialized with `num_labels = 1` (a.k.a. the default) to predict one class.
            2. Usually uses a finetuned CrossEncoder teacher M in a knowledge distillation setup.

        Inputs:
            +-----------------------------------------+-----------------------------+-------------------------------+
            | Inputs                                  | Labels                      | Number of Model Output Labels |
            +=========================================+=============================+===============================+
            | (input_A, input_B) pairs                | similarity score            | 1                             |
            +-----------------------------------------+-----------------------------+-------------------------------+

        Relations:
            - :class:`MarginMSELoss` is similar to this loss, but with a margin through a negative pair.

        Example:
            ::

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

                student_model = CrossEncoder("microsoft/mpnet-base")
                teacher_model = CrossEncoder("cross-encoder/ms-marco-MiniLM-L12-v2")
                train_dataset = Dataset.from_dict({
                    "query": ["What are pandas?", "What is the capital of France?"],
                    "answer": ["Pandas are a kind of bear.", "The capital of France is Paris."],
                })

                def compute_labels(batch):
                    return {
                        "label": teacher_model.predict(list(zip(batch["query"], batch["answer"])))
                    }

                train_dataset = train_dataset.map(compute_labels, batched=True)
                loss = losses.MSELoss(student_model)

                trainer = CrossEncoderTrainer(
                    model=student_model,
                    train_dataset=train_dataset,
                    loss=loss,
                )
                trainer.train()
        z? 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__r   r   r   r
   Úloss_fctÚ
isinstancer   Ú
ValueErrorÚ	__class__Ú__name__ÚtypeÚ
num_labels)Úselfr   r   Úkwargsr   s       €ú\C:\Projects\DSCons\.venv\Lib\site-packages\sentence_transformers\cross_encoder\losses\mse.pyr   zMSELoss.__init__
   sð   ø€ õz 	‰Œ×ÒÑÔÐØˆŒ
Ø*ˆÔÝœ
Ð,Ð, VÐ,Ð,ˆŒå˜$œ*¥lÑ3Ô3ð 	ÝØ”>Ô*ð ?ð ?Ý+/°´
Ñ+;Ô+;ð?ð ?ð ?ñô ð ð
 Œ:Ô  AÒ%Ð%ÝØ”>Ô*ð Oð OØ(,¬
Ô(=ðOð Oð Oñô ð ð &Ð%ó    NÚinputsúlist[list[str]]Úlabelsr   Úpromptú
str | NoneÚtaskc                ó&  — t          |¦  «        dk    r t          dt          |¦  «        › d�¦  «        ‚t          t          |d         |d         ¦  «        ¦  «        }| j                             |||¬¦  «        }t          || j        j        ¦  «        }|                      |¦  «        d                              d¦  «        }|  	                    |¦  «        }|  
                    ||                     ¦   «                              ¦   «         ¦  «        }|S )	Né   zMMSELoss expects a dataset with two non-label columns, but got a dataset with z	 columns.r   r   )r$   r&   Úscoreséÿÿÿÿ)Úlenr   ÚlistÚzipr   Ú
preprocessr   ÚdeviceÚviewr   r   ÚdetachÚfloat)r   r!   r#   r$   r&   ÚpairsÚlogitsÚlosss           r   ÚforwardzMSELoss.forwardX   só   € õ ˆv‰;Œ;˜!ÒÐÝØvÕ`cÐdjÑ`kÔ`kÐvÐvÐvñô ð õ •S˜ œ F¨1¤IÑ.Ô.Ñ/Ô/ˆØ”×&Ò& u°VÀ$Ð&ÑGÔGˆÝ  ¨¬Ô):Ñ;Ô;ˆØ—’˜FÑ#Ô# HÔ-×2Ò2°2Ñ6Ô6ˆØ×#Ò# FÑ+Ô+ˆØ�}Š}˜V V§]¢]¡_¤_×%:Ò%:Ñ%<Ô%<Ñ=Ô=ˆØˆr    c                ó.   — dt          | j        ¦  «        iS )Nr   )r   r   )r   s    r   Úget_config_dictzMSELoss.get_config_dicth   s   € à�X dÔ&8Ñ9Ô9ð
ð 	
r    )r   r   r   r   r   r   )NN)
r!   r"   r#   r   r$   r%   r&   r%   r   r   )	r   Ú
__module__Ú__qualname__r   ÚIdentityr   r6   r8   Ú__classcell__)r   s   @r   r
   r
   	   s„   ø€ € € € € ØGRÀrÄ{Á}Ä}ð Lð Lð Lð Lð Lð Lð Lð^ fjðð ð ð ð ð 
ð 
ð 
ð 
ð 
ð 
ð 
r    r
   N)Ú
__future__r   Útorchr   r   Ú)sentence_transformers.cross_encoder.modelr   Úsentence_transformers.utilr   r   ÚModuler
   r   r    r   ú<module>rB      s™   ðØ "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ð Ð Ð Ð à BÐ BÐ BÐ BÐ BÐ BØ @Ð @Ð @Ð @Ð @Ð @Ð @Ð @ðb
ð b
ð b
ð b
ð b
ˆbŒiñ b
ô b
ð b
ð b
ð b
r    