
    h                    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                  X   ^  \ 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5       rSr	U =r
$ )	FlopsLoss   c                :   > [         TU ]  5         Xl        X l        g)aO  
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://arxiv.org/pdf/2004.05665 for the general FLOPS loss and https://arxiv.org/pdf/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      o/home/james-whalen/.local/lib/python3.13/site-packages/sentence_transformers/sparse_encoder/losses/FlopsLoss.pyr   FlopsLoss.__init__   s    4 	
"    c                    [        S5      e)NzaFlopsLoss is not intended to be used directly. Use it as a regulizer within the SpladeLoss class.)NotImplementedError)r   sentence_featureslabelss      r   forwardFlopsLoss.forward*   s    !o
 	
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+++2Gnn,335D#nnQ&77JyyJA6!;<<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.citation7   s    r   )r   r   )N)r   r   r   zfloat | NonereturnNone)r   zIterable[dict[str, Tensor]]r   r   r-   r   )r$   zlist[torch.Tensor]r-   ztorch.Tensor)r-   str)__name__
__module____qualname____firstlineno__r   r   r'   propertyr+   __static_attributes____classcell__)r   s   @r   r	   r	      s+    # #<

=  r   r	   )
__future__r   collections.abcr   r"   r   r   2sentence_transformers.sparse_encoder.SparseEncoderr   Moduler	   r*   r   r   <module>r;      s$    " $   L5		 5r   