
    Ng-                    l    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ej	                  Z
dS )    )annotationsN)Any)Tensornnc                       e Zd ZdZdZ	 	 	 	 	 	 	 	 dd  fdZd!dZd!dZd"dZd#dZ	d$dZ
d%dZed&d            Z x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
                   t                                                       g d| _        |\|                                }|| j        vrt          d| d| j         d          |dk    }|dk    }|dk    }|dk    }|d	k    }|d
k    }|| _        || _        || _        || _	        || _
        || _        || _        |	| _        t          ||||||g          }
|
|z  | _        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__s              `/var/www/html/ai-engine/env/lib/python3.11/site-packages/sentence_transformers/models/Pooling.pyr    zPooling.__init__6   sE    		
 	
 	
 #'--//L4#555 oooZ^Zlooo   &2U%:"&2e&;#'3v'=$0<@V0V-/;~/M,%1[%@"(@%&<#(@%'>$1R.0P-&<#,"%&'(10&	#
 	#
 )@BZ(Z%%%    c                2    d|                                   dS )NzPooling())get_config_dictr'   s    r*   __repr__zPooling.__repr__t   s    3$..003333r+   c                   g }| j         r|                    d           | j        r|                    d           | j        r|                    d           | j        r|                    d           | j        r|                    d           | j        r|                    d           d                    |          S )z"Returns the pooling mode as stringr	   r   r   r   r   r
   +)r   appendr   r   r   r   r   join)r'   modess     r*   get_pooling_mode_strzPooling.get_pooling_mode_strw   s    & 	 LL( 	!LL   ' 	 LL1 	1LL/0000 	)LL(((& 	&LL%%%xxr+   featuresdict[str, Tensor]c                   |d         }d|v r|d         n2t          j        |j        d d         |j        t           j                  }| j        sKd|v rG|d         }t          |t           j                  r|d                                         }d|d d d |f<   g }| j	        r5|
                    d|d d df                   }|                    |           | j        r|                    d                              |                                                              |j                  }d||dk    <   t          j        |d	          d         }|                    |           | j        s| j        r*|                    d                              |                                                              |j                  }t          j        ||z  d	          }	d
|v rA|d
                             d                              |	                                          }
n|                    d	          }
t          j        |
d          }
| j        r|                    |	|
z             | j        r*|                    |	t          j        |
          z             | j        r|                    d                              |                                                              |j                  }t          j        d	|j        d	         d	z                                 d                              d                              |                                                              |j                                      |j                  }|j        |j        cxk    r|j        k    sn J ||z  }t          j        ||z  d	          }	d
|v rA|d
                             d                              |	                                          }
n|                    d	          }
t          j        |
d          }
|                    |	|
z             | j        rz|j        \  }}}t           j                                        r|                    t           j                  }|                    d	                              d	          \  }}t          j        |dk    |d	z
  |          }||z
  d	z
  }|                    d                               d	|          }|                    d	          }|j        |d	|fk    sJ |                    d                              |                                                              |j                  }t          j!        ||z  d	|          "                    d	          }|                    |           t          j#        |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shaper=   int64r   
isinstancer   itemr   getr3   r   	unsqueezeexpandsizetor>   r   r   r   r%   clampsqrtr   aranger   jit
is_tracingint32flipwhererepeatgathersqueezecat)r'   r7   r:   r;   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_vectors                       r*   forwardzPooling.forward   s   #$67  8++ %&&,23B37@P@W_d_jkkk 	
 " 
	2('B'B$_5M -66 8 -a 0 5 5 7 701N111n}n,- & 	- %;=MaaaQRd=STTI!!),,,' 	1((,,334D4I4I4K4KLLOOP`Pfgg   :>0A56!I&6::1=M!!-000( 	MD,R 	M((,,334D4I4I4K4KLLOOP`Pfgg   #Y'7:M'MqQQN #h..#$78BB2FFMMnNaNaNcNcdd.22155{8666H, A%%nx&?@@@5 M%%nuz(7K7K&KLLL0 	=((,,334D4I4I4K4KLLOOP`Pfgg  
 1*:*@*Ca*GHHH12(--//00$*++$+,,  =$4$:WWWW>Q>WWWWWWW"5"?"Y'7:M'MqQQN #h..#$78BB2FFMMnNaNaNcNcdd.22155{8666H!!.8";<<<& 	-&6&<#B
 y##%% @!/!2!25;!?!?,11!4488;;OFGk&A+w{GDDG$w.2N ,55b99@@JOON+55a88N!'B:+>>>>> ((,,334D4I4I4K4KLLOOP`Pfgg   %58K%KQP^__gglmgnnI!!),,,	.!44)6%&r+   c                    | j         S )N)r&   r/   s    r*    get_sentence_embedding_dimensionz(Pooling.get_sentence_embedding_dimension   s    ,,r+   dict[str, Any]c                *      fd j         D             S )Nc                ,    i | ]}|j         |         S  )__dict__).0keyr'   s     r*   
<dictcomp>z+Pooling.get_config_dict.<locals>.<dictcomp>   s"    DDDCT]3'DDDr+   )r!   r/   s   `r*   r.   zPooling.get_config_dict   s     DDDD43CDDDDr+   c                    t          t          j                            |d          d          5 }t	          j        |                                 |d           d d d            d S # 1 swxY w Y   d S )Nconfig.jsonw   )indent)openospathr4   jsondumpr.   )r'   output_pathfOuts      r*   savezPooling.save   s    "',,{M::C@@ 	>DId**,,d1====	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	>s   *A&&A*-A*c                    t          t          j                            | d                    5 }t	          j        |          }d d d            n# 1 swxY w Y   t          di |S )Nrz   rt   )r~   r   r   r4   r   loadr   )
input_pathfInconfigs      r*   r   zPooling.load   s    "',,z=99:: 	$cYs^^F	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$      s   AAA)NFFTFFFT)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   )r7   r8   r   r8   )r   r   )r   rq   )r   r   )r   r   )__name__
__module____qualname____doc__r#   r    r0   r6   rn   rp   r.   r   staticmethodr   __classcell__)r)   s   @r*   r   r      s        BM !',(-)-2716',#<[ <[ <[ <[ <[ <[ <[|4 4 4 4   $j j j jX- - - -E E E E> > > > ! ! ! \! ! ! ! !r+   r   )
__future__r   r   r   typingr   rH   r   r   Moduler   rt   r+   r*   <module>r      s    " " " " " "  				               y! y! y! y! y!bi y! y! y! y! y!r+   