a
    ãÀh],  ã                   @  s@   d dl mZ d dlZd dlmZ d dlmZ G dd„ deƒZdS )é    )ÚannotationsN)ÚTensor)ÚModulec                      s˜   e Zd ZdZdZg d¢Zdddd	d	d	d	d	d	d	d
dœ
‡ fdd„Zddœdd„Zddœdd„Zdddœdd„Z	ddœdd„Z
ddœdd	d
dœdd„Z‡  ZS )ÚPoolinga§  
    Performs pooling (max or mean) on the token embeddings.

    Using pooling, it generates from a variable sized sentence a fixed sized sentence embedding. This layer also allows
    to use the CLS token if it is returned by the underlying word embedding model. You can concatenate multiple poolings
    together.

    Args:
        word_embedding_dimension: Dimensions for the word embeddings
        pooling_mode: Either "cls", "lasttoken", "max", "mean",
            "mean_sqrt_len_tokens", or "weightedmean". If set,
            overwrites the other pooling_mode_* settings
        pooling_mode_cls_token: Use the first token (CLS token) as text
            representations
        pooling_mode_max_tokens: Use max in each dimension over all
            tokens.
        pooling_mode_mean_tokens: Perform mean-pooling
        pooling_mode_mean_sqrt_len_tokens: Perform mean-pooling, but
            divide by sqrt(input_length).
        pooling_mode_weightedmean_tokens: Perform (position) weighted
            mean pooling. See `SGPT: GPT Sentence Embeddings for
            Semantic Search <https://arxiv.org/abs/2202.08904>`_.
        pooling_mode_lasttoken: Perform last token pooling. See `SGPT:
            GPT Sentence Embeddings for Semantic Search
            <https://arxiv.org/abs/2202.08904>`_ and `Text and Code
            Embeddings by Contrastive Pre-Training
            <https://arxiv.org/abs/2201.10005>`_.
        include_prompt: If set to false, the prompt tokens are not
            included in the pooling. This is useful for reproducing
            work that does not include the prompt tokens in the pooling
            like INSTRUCTOR, but otherwise not recommended.
    )ÚclsÚ	lasttokenÚmaxÚmeanÚmean_sqrt_len_tokensÚweightedmean)Úword_embedding_dimensionÚpooling_mode_cls_tokenÚpooling_mode_mean_tokensÚpooling_mode_max_tokensÚ!pooling_mode_mean_sqrt_len_tokensÚ pooling_mode_weightedmean_tokensÚpooling_mode_lasttokenÚinclude_promptNFTÚintz
str | NoneÚboolÚNone)
r   Úpooling_moder   r   r   r   r   r   r   Úreturnc
                   s¾   t ƒ  ¡  |d url| ¡ }|| jvr<td|› d| j› dƒ‚|dk}|dk}|dk}|dk}|dk}|d	k}|| _|| _|| _|| _|| _	|| _
|| _|	| _t||||||gƒ}
|
| | _d S )
NzSet invalid pooling mode: z. Valid pooling modes are: Ú.r   r   r	   r
   r   r   )ÚsuperÚ__init__ÚlowerÚPOOLING_MODESÚ
ValueErrorr   r   r   r   r   r   r   r   ÚsumÚpooling_output_dimension)Úselfr   r   r   r   r   r   r   r   r   Zpooling_mode_multiplier©Ú	__class__© ú`/var/www/html/assistant/venv/lib/python3.9/site-packages/sentence_transformers/models/Pooling.pyr   ?   s>    

ÿúÿ
zPooling.__init__Ústr)r   c                 C  s   d|   ¡ › dS )NzPooling(ú))Zget_config_dict©r!   r$   r$   r%   Ú__repr__r   s    zPooling.__repr__c                 C  sn   g }| j r| d¡ | jr$| d¡ | jr4| d¡ | jrD| d¡ | jrT| d¡ | jrd| d¡ d |¡S )z"Returns the pooling mode as stringr   r	   r   r
   r   r   ú+)r   Úappendr   r   r   r   r   Újoin)r!   Úmodesr$   r$   r%   Úget_pooling_mode_stru   s    





zPooling.get_pooling_mode_strzdict[str, Tensor])Úfeaturesr   c                 C  sh  |d }d|v r|d nt j|jd d… |jt jd}| jsxd|v rx|d }t|t jƒrd|d  ¡ }d|d d …d |…f< g }| j	r¤| 
d|d d …df ¡}| |¡ | jrì| d¡ | ¡ ¡ |j¡}d||dk< t  |d	¡d }| |¡ | jsú| jr”| d¡ | ¡ ¡ |j¡}t  || d	¡}	d
|v rJ|d
  d¡ |	 ¡ ¡}
n
| d	¡}
t j|
dd}
| jrx| |	|
 ¡ | jr”| |	t  |
¡ ¡ | jr|| d¡ | ¡ ¡ |j¡}t jd	|jd	 d	 d d¡ d¡ | ¡ ¡ |j¡ |j¡}|j|j  kr|jksn J ‚|| }t  || d	¡}	d
|v rV|d
  d¡ |	 ¡ ¡}
n
| d	¡}
t j|
dd}
| |	|
 ¡ | jrP|j\}}}t j ¡ r¨| t j¡}| d	¡ d	¡\}}t  |dk|d	 |¡}|| d	 }| d¡  d	|¡}| d	¡}|j|d	|fksJ ‚| d¡ | ¡ ¡ |j¡}t  !|| d	|¡j"d	d}| |¡ t  #|d	¡}||d< |S )NÚtoken_embeddingsÚattention_maskéÿÿÿÿ)ÚdeviceÚdtypeÚprompt_lengthr   Zcls_token_embeddingsg    eÍÍÁé   Ztoken_weights_sumg•Ö&è.>)Úmin)ÚstartÚend)ÚdimZsentence_embedding)$ÚtorchZonesÚshaper3   Zint64r   Ú
isinstancer   Úitemr   Úgetr+   r   Z	unsqueezeÚexpandÚsizeÚtor4   r   r   r   r   ÚclampÚsqrtr   Zaranger   ZjitÚ
is_tracingZint32ÚflipÚwhereÚrepeatZgatherZsqueezeÚcat)r!   r/   r0   r1   r5   Zoutput_vectorsZ	cls_tokenZinput_mask_expandedZmax_over_timeZsum_embeddingsZsum_maskÚweightsÚbsZseq_lenZ
hidden_dimÚvaluesÚindicesZgather_indicesZ	embeddingZoutput_vectorr$   r$   r%   Úforward‡   s    ÿ
ý
ÿ
ÿ

ÿÿþýüûÿ$


ÿ
zPooling.forwardc                 C  s   | j S ©N)r    r(   r$   r$   r%   Ú get_sentence_embedding_dimensionó   s    z(Pooling.get_sentence_embedding_dimension)Úsafe_serialization)Úoutput_pathrQ   r   c                O  s   |   |¡ d S rO   )Zsave_config)r!   rR   rQ   ÚargsÚkwargsr$   r$   r%   Úsaveö   s    zPooling.save)NFFTFFFT)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Zconfig_keysr   r)   r.   rN   rP   rU   Ú__classcell__r$   r$   r"   r%   r   	   s"   !	        ö&3lr   )Ú
__future__r   r;   r   Z#sentence_transformers.models.Moduler   r   r$   r$   r$   r%   Ú<module>   s   