U
    <A·fb*  ã                   @   sN   d dl Z d dlZd dlmZmZ d dlZd dlmZmZ G dd„ dejƒZ	dS )é    N)ÚAnyÚDict)ÚTensorÚnnc                       s¼   e Zd ZdZdZdeeeeeeeeddœ	‡ fdd„Zed	œd
d„Z	ed	œdd„Z
eeef eeef dœdd„Zed	œdd„Zeeef d	œdd„Zdd	œdd„Ze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>`_.
    )ÚclsÚ	lasttokenÚmaxÚmeanÚmean_sqrt_len_tokensÚweightedmeanNFT)	Úword_embedding_dimensionÚpooling_modeÚpooling_mode_cls_tokenÚpooling_mode_max_tokensÚpooling_mode_mean_tokensÚ!pooling_mode_mean_sqrt_len_tokensÚ pooling_mode_weightedmean_tokensÚpooling_mode_lasttokenÚreturnc
                    sØ   t t| ƒ ¡  ddddddddg| _|d k	r†| ¡ }|| jkrVtd	|› d
| j› dƒ‚|dk}|dk}|dk}|dk}|dk}|dk}|| _|| _|| _	|| _
|| _|| _|| _|	| _t||||||gƒ}
|
| | _d S )Nr   r   r   r   r   r   r   Úinclude_promptzSet invalid pooling mode: z. Valid pooling modes are: Ú.r   r	   r
   r   r   r   )Úsuperr   Ú__init__Úconfig_keysÚ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__© úH/tmp/pip-unpacked-wheel-i7fohqg6/sentence_transformers/models/Pooling.pyr   0   sP    ø
ÿúÿ
zPooling.__init__)r   c                 C   s   d  |  ¡ ¡S )NzPooling({}))ÚformatÚget_config_dict©r    r#   r#   r$   Ú__repr__n   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_strq   s    





zPooling.get_pooling_mode_str)Úfeaturesr   c                 C   s&  |d }|d }| j s6d|kr6d|d d …d |d …f< g }| jrb| d|d d …df ¡}| |¡ | jrª| d¡ | ¡ ¡ |j	¡}d||dk< t
 |d¡d }| |¡ | js¸| jrR| d¡ | ¡ ¡ |j	¡}t
 || d¡}d	|kr|d	  d¡ | ¡ ¡}	n
| d¡}	t
j|	d
d}	| jr6| ||	 ¡ | jrR| |t
 |	¡ ¡ | jr:| d¡ | ¡ ¡ |j	¡}t
jd|jd d d d¡ d¡ | ¡ ¡ |j	¡ |j¡}
|
j|j  krÒ|jksØn t‚||
 }t
 || d¡}d	|kr|d	  d¡ | ¡ ¡}	n
| d¡}	t
j|	d
d}	| ||	 ¡ | jr|j\}}}t
j ¡ rf| t
j¡}| d¡ d¡\}}t
 |dk|d |¡}|| d }| d¡ d|¡}| d¡}|j|d|fksÎt‚| d¡ | ¡ ¡ |j	¡}t
 || d|¡jdd}| |¡ t
 |d¡}||d< |S )NÚtoken_embeddingsÚattention_maskZprompt_lengthr   Zcls_token_embeddingséÿÿÿÿg    eÍÍÁé   Ztoken_weights_sumg•Ö&è.>)Úmin)ÚstartÚend)ZdimZsentence_embedding) r   r   Úgetr*   r   Z	unsqueezeÚexpandÚsizeÚtoZdtypeÚtorchr	   r   r   r   ÚclampÚsqrtr   ZarangeÚshapeZdeviceÚAssertionErrorr   ZjitÚ
is_tracingZint32ZflipÚwhereÚrepeatZgatherZsqueezeÚcat)r    r.   r/   r0   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_dimensionc                    s   ‡ fdd„ˆ j D ƒS )Nc                    s   i | ]}|ˆ j | “qS r#   )Ú__dict__)Ú.0Úkeyr'   r#   r$   Ú
<dictcomp>æ   s      z+Pooling.get_config_dict.<locals>.<dictcomp>)r   r'   r#   r'   r$   r&   å   s    zPooling.get_config_dictc              	   C   s8   t tj |d¡dƒ}tj|  ¡ |dd W 5 Q R X d S )Núconfig.jsonÚwé   )Úindent)ÚopenÚosÚpathr+   ÚjsonÚdumpr&   )r    Zoutput_pathZfOutr#   r#   r$   Úsaveè   s    zPooling.savec              	   C   s2   t tj | d¡ƒ}t |¡}W 5 Q R X tf |ŽS )NrM   )rQ   rR   rS   r+   rT   Úloadr   )Z
input_pathZfInÚconfigr#   r#   r$   rW   ì   s    zPooling.load)NFFTFFFT)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚintÚstrÚboolr   r(   r-   r   r   rG   rH   r   r&   rV   ÚstaticmethodrW   Ú__classcell__r#   r#   r!   r$   r   	   s:           öõ> _r   )
rT   rR   Útypingr   r   r:   r   r   ÚModuler   r#   r#   r#   r$   Ú<module>   s
   