o
    S"ågð-  ã                   @  sV   d dl mZ d dlZd dlZd dlmZ d dlZd dlmZmZ G dd„ dej	ƒZ
dS )é    )ÚannotationsN)ÚAny)ÚTensorÚnnc                      s‚   e Zd ZdZdZ								d'd(‡ fdd„Zd)dd„Zd)dd„Zd*dd„Zd+dd„Z	d,d!d"„Z
d-d#d$„Ze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ÚweightedmeanNFTÚword_embedding_dimensionÚintÚpooling_modeÚstrÚpooling_mode_cls_tokenÚboolÚpooling_mode_max_tokensÚpooling_mode_mean_tokensÚ!pooling_mode_mean_sqrt_len_tokensÚ pooling_mode_weightedmean_tokensÚpooling_mode_lasttokenÚinclude_promptÚreturnÚNonec
                   sÈ   t ƒ  ¡  g d¢| _|d ur;| ¡ }|| jvr#td|› d| j› dƒ‚|dk}|dk}|dk}|dk}|d	k}|d
k}|| _|| _|| _|| _	|| _
|| _|| _|	| _t||||||gƒ}
|
| | _d S )N)r   r   r   r   r   r   r   r   zSet invalid pooling mode: z. Valid pooling modes are: Ú.r   r	   r
   r   r   r   )ÚsuperÚ__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   Úpooling_mode_multiplier©Ú	__class__© úf/mnt/skqttb/ctump_chatbot/chatbot/lib/python3.10/site-packages/sentence_transformers/models/Pooling.pyr   6   s@   


ÿúÿ
zPooling.__init__c                 C  s   d|   ¡ › dS )NzPooling(ú))Úget_config_dict©r$   r(   r(   r)   Ú__repr__t   s   zPooling.__repr__c                 C  sn   g }| j r
| d¡ | jr| d¡ | jr| d¡ | jr"| d¡ | jr*| d¡ | jr2| 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_strw   s   






zPooling.get_pooling_mode_strÚfeaturesúdict[str, Tensor]c                 C  sb  |d }d|v r|d nt j|jd d… |jt jd}| js<d|v r<|d }t|t jƒr2|d  ¡ }d|d d …d |…f< g }| j	rR| 
d|d d …df ¡}| |¡ | jrv| d¡ | ¡ ¡ |j¡}d||dk< t  |d	¡d }| |¡ | js|| jrÆ| d¡ | ¡ ¡ |j¡}t  || d	¡}	d
|v r£|d
  d¡ |	 ¡ ¡}
n| d	¡}
t j|
dd}
| jr¹| |	|
 ¡ | 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
J ‚ J ‚|| }t  || d	¡}	d
|v r(|d
  d¡ |	 ¡ ¡}
n| d	¡}
t j|
dd}
| |	|
 ¡ | jr¥|j\}}}t j ¡ rQ| 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   Úcls_token_embeddingsg    eÍÍÁé   Útoken_weights_sumg•Ö&è.>)Úmin)ÚstartÚend)ÚdimÚsentence_embedding)$ÚtorchÚonesÚshaper8   Úint64r   Ú
isinstancer   Úitemr   Úgetr/   r   Ú	unsqueezeÚexpandÚsizeÚtor9   r	   r   r   r"   ÚclampÚsqrtr   Úaranger   ÚjitÚ
is_tracingÚint32ÚflipÚwhereÚrepeatÚgatherÚsqueezeÚcat)r$   r3   r5   r6   r:   Úoutput_vectorsÚ	cls_tokenÚinput_mask_expandedÚmax_over_timeÚsum_embeddingsÚsum_maskÚweightsÚbsÚseq_lenÚ
hidden_dimÚvaluesÚindicesÚgather_indicesÚ	embeddingÚ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údict[str, Any]c                   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  sN   t tj |d¡dƒ}tj|  ¡ |dd W d   ƒ d S 1 s w   Y  d S )Núconfig.jsonÚwé   )Úindent)ÚopenÚosÚpathr0   ÚjsonÚdumpr+   )r$   Úoutput_pathÚfOutr(   r(   r)   Úsaveû   s   "ÿzPooling.savec                 C  sJ   t tj | d¡ƒ}t |¡}W d   ƒ n1 sw   Y  tdi |¤ŽS )Nrp   r(   )rt   ru   rv   r0   rw   Úloadr   )Ú
input_pathÚfInÚconfigr(   r(   r)   r|   ÿ   s   ÿzPooling.load)NFFTFFFT)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   )r3   r4   r   r4   )r   r   )r   rk   )r   r   )r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    r   r-   r2   ri   rj   r+   r{   Ústaticmethodr|   Ú__classcell__r(   r(   r&   r)   r      s(    !ö
>


l

r   )Ú
__future__r   rw   ru   Útypingr   rC   r   r   ÚModuler   r(   r(   r(   r)   Ú<module>   s    