ó
    >:jé  ã                  ój   • S SK Jr  S SKJr  S SKrS SKJrJr  S SKJr   " S S\R                  5      r
g)é    )Úannotations)ÚIterableN)ÚTensorÚnn)ÚSparseEncoderc                  ób   ^ • \ rS rSrSS	U 4S jjjrS
S jrSS jrSS jr\SS j5       r	Sr
U =r$ )Ú	FlopsLossé   c                ó:   >• [         TU ]  5         Xl        X l        g)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__ÚmodelÚ	threshold)Úselfr   r   Ú	__class__s      €Ún/home/mande/repo/quber/.venv/lib/python3.13/site-packages/sentence_transformers/sparse_encoder/losses/flops.pyr   ÚFlopsLoss.__init__   s   ø€ ô4 	‰ÑÔØŒ
Ø"�ó    c                ó   • [        S5      e)NzcFlopsLoss is not intended to be used directly. Use it as a regularizer within the SpladeLoss class.)ÚNotImplementedError)r   Úsentence_featuresÚlabelss      r   ÚforwardÚFlopsLoss.forward*   s   € Ü!Øqó
ð 	
r   c                óö   • U R                   bA  US:g  R                  SS9nX R                   :„  R                  5       nXR                  S5      -  n[        R                  " [        R
                  " USS9S-  5      $ )Nr   é   )Údimé   )r   ÚsumÚfloatÚ	unsqueezeÚtorchÚmean)r   Ú
embeddingsÚl0_normÚmasks       r   Úcompute_loss_from_embeddingsÚ&FlopsLoss.compute_loss_from_embeddings/   sh   € Ø�>‰>Ñ%Ø! Q‘×+Ñ+°Ð+Ð2ˆGØŸn™nÑ,×3Ñ3Ó5ˆDØ#§n¡n°QÓ&7Ñ7ˆJä�yŠyœŸš J°AÑ6¸!Ñ;Ó<Ð<r   c                ó   • SU R                   0$ )Nr   )r   ©r   s    r   Úget_config_dictÚFlopsLoss.get_config_dict7   s   € Ø˜TŸ^™^Ð,Ð,r   c                ó   • g)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   ÚcitationÚFlopsLoss.citation:   s   € ðr   )r   r   )N)r   r   r   zfloat | NoneÚreturnÚNone)r   zIterable[dict[str, Tensor]]r   r   r1   r   )r$   útorch.Tensorr1   r3   )r1   Údict)r1   Ústr)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__r   r   r'   r+   Úpropertyr/   Ú__static_attributes__Ú__classcell__)r   s   @r   r	   r	      s0   ø† ÷#ñ #ô<
ô
=ô-ð óó ör   r	   )Ú
__future__r   Úcollections.abcr   r"   r   r   Ú*sentence_transformers.sparse_encoder.modelr   ÚModuler	   r.   r   r   Ú<module>rA      s$   ðÝ "å $ã ß å Dô8�—	‘	õ 8r   