§
    +�’jé  ã                  óh   — d dl mZ d dlmZ d dlZd dlmZmZ d dlmZ  G d„ dej	        ¦  «        Z
dS )é    )Úannotations)ÚIterableN)ÚTensorÚnn)ÚSparseEncoderc                  óR   ‡ — e Zd Zddˆ fd„Zdd„Zdd„Zdd„Zedd„¦   «         Zˆ xZ	S )Ú	FlopsLossNÚmodelr   Ú	thresholdúfloat | NoneÚreturnÚNonec                ód   •— t          ¦   «                              ¦   «          || _        || _        dS )aï  
        FlopsLoss implements a regularization technique to promote sparsity in sparse encoder models.
        It calculates the squared L2 norm of the mean embedding vector, which helps reduce the number of floating-point
        operations (FLOPs) required during inference by encouraging more zero values in the embeddings.
        It can use a threshold to ignore embeddings with too few non-zero (active) elements.

        This loss is used as a regularization component within other losses like :class:`SpladeLoss` rather than
        being used as a standalone loss function.

        Args:
            model: SparseEncoder model to be regularized
            threshold: Optional threshold for the number of non-zero (active) elements in the embeddings.
                If specified, only embeddings with more than this number of non-zero (active) elements will be considered.
                This can help to ignore embeddings that are too sparse and may not contribute meaningfully to the loss.

        References:
            - For further details, see: https://huggingface.co/papers/2004.05665 for the general FLOPS loss and https://huggingface.co/papers/2504.14839 for FLOPS with thresholds, a.k.a. FLOPS with l0 masking.

        Relations:
            - Used as a component within :class:`SpladeLoss` to regularize both query and document embeddings

        Example:
            - This loss is typically used within the :class:`SpladeLoss` class, which combines it with other loss components.

        N)ÚsuperÚ__init__r
   r   )Úselfr
   r   Ú	__class__s      €ú_C:\Projects\DSCons\.venv\Lib\site-packages\sentence_transformers\sparse_encoder\losses\flops.pyr   zFlopsLoss.__init__   s-   ø€ õ4 	‰Œ×ÒÑÔÐØˆŒ
Ø"ˆŒˆˆó    Úsentence_featuresúIterable[dict[str, Tensor]]Úlabelsr   c                ó    — t          d¦  «        ‚)NzcFlopsLoss is not intended to be used directly. Use it as a regularizer within the SpladeLoss class.)ÚNotImplementedError)r   r   r   s      r   ÚforwardzFlopsLoss.forward*   s   € Ý!Øqñ
ô 
ð 	
r   Ú
embeddingsútorch.Tensorc                ó  — | j         �O|dk                         d¬¦  «        }|| j         k                         ¦   «         }||                     d¦  «        z  }t	          j        t	          j        |d¬¦  «        dz  ¦  «        S )Nr   é   )Údimé   )r   ÚsumÚfloatÚ	unsqueezeÚtorchÚmean)r   r   Úl0_normÚmasks       r   Úcompute_loss_from_embeddingsz&FlopsLoss.compute_loss_from_embeddings/   sv   € ØŒ>Ð%Ø! Q’×+Ò+°Ð+Ñ2Ô2ˆGØ˜dœnÒ,×3Ò3Ñ5Ô5ˆDØ# d§n¢n°QÑ&7Ô&7Ñ7ˆJåŒy�œ J°AÐ6Ñ6Ô6¸!Ñ;Ñ<Ô<Ð<r   Údictc                ó   — d| j         iS )Nr   )r   ©r   s    r   Úget_config_dictzFlopsLoss.get_config_dict7   s   € Ø˜Tœ^Ð,Ð,r   Ústrc                ó   — dS )Na(  
@article{paria2020minimizing,
    title={Minimizing flops to learn efficient sparse representations},
    author={Paria, Biswajit and Yeh, Chih-Kuan and Yen, Ian EH and Xu, Ning and Ravikumar, Pradeep and P{'o}czos, Barnab{'a}s},
    journal={arXiv preprint arXiv:2004.05665},
    year={2020}
}
© r,   s    r   ÚcitationzFlopsLoss.citation:   s   € ðð r   )N)r
   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   )r   r*   )r   r.   )
Ú__name__Ú
__module__Ú__qualname__r   r   r)   r-   Úpropertyr1   Ú__classcell__)r   s   @r   r	   r	      sž   ø€ € € € € ð#ð #ð #ð #ð #ð #ð #ð<
ð 
ð 
ð 
ð
=ð =ð =ð =ð-ð -ð -ð -ð ðð ð ñ „Xðð ð ð ð r   r	   )Ú
__future__r   Úcollections.abcr   r%   r   r   Ú*sentence_transformers.sparse_encoder.modelr   ÚModuler	   r0   r   r   ú<module>r;      s’   ðØ "Ð "Ð "Ð "Ð "Ð "à $Ð $Ð $Ð $Ð $Ð $à €€€Ø Ð Ð Ð Ð Ð Ð Ð à DÐ DÐ DÐ DÐ DÐ Dð8ð 8ð 8ð 8ð 8�”	ñ 8ô 8ð 8ð 8ð 8r   