
    Ch],                    F    S SK Jr  S SKrS SKJr  S SKJr   " S S\5      rg)    )annotationsN)Tensor)Modulec                     ^  \ rS rSrSrSr/ SQr        S                   SU 4S jjjrSS jrSS jr	SS	 jr
SS
 jrSS.SS jjrSrU =r$ )Pooling	   a3  
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_promptTc
                n  > [         TU ]  5         UbY  UR                  5       nX R                  ;  a  [	        SU SU R                   S35      eUS:H  nUS:H  nUS:H  nUS:H  nUS:H  nUS	:H  nXl        X0l        XPl        X@l        X`l	        Xpl
        Xl        Xl        [        UUUUUU/5      n
X-  U l        g )
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   pooling_moder   r   r   r   r   r   r   pooling_mode_multiplier	__class__s              \/var/www/html/shao/venv/lib/python3.13/site-packages/sentence_transformers/models/Pooling.pyr   Pooling.__init__?   s     	#'--/L#5#55 0>YZ^ZlZlYmmno  &2U%:"&2e&;#'3v'=$0<@V0V-/;~/M,%1[%@"(@%&<#(@%'>$1R.0P-&<#,"%&'(10&	#
 )@(Z%    c                *    SU R                  5        S3$ )NzPooling())get_config_dictr    s    r$   __repr__Pooling.__repr__r   s    $..0133r&   c                   / nU R                   (       a  UR                  S5        U R                  (       a  UR                  S5        U R                  (       a  UR                  S5        U R                  (       a  UR                  S5        U R
                  (       a  UR                  S5        U R                  (       a  UR                  S5        SR                  U5      $ )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_strPooling.get_pooling_mode_stru   s    &&LL((LL ''LL11LL/000LL(&&LL%xxr&   c                   US   nSU;   a  US   O:[         R                  " UR                  S S UR                  [         R                  S9nU R
                  (       dH  SU;   aB  US   n[        U[         R                  5      (       a  US   R                  5       nSUS S 2S U24'   / nU R                  (       a*  UR                  SUS S 2S4   5      nUR                  U5        U R                  (       ay  UR                  S5      R                  UR                  5       5      R!                  UR"                  5      nSX'S:H  '   [         R$                  " US	5      S   nUR                  U5        U R&                  (       d  U R(                  (       Ga  UR                  S5      R                  UR                  5       5      R!                  UR"                  5      n[         R*                  " X'-  S	5      n	S
U;   a2  US
   R                  S5      R                  U	R                  5       5      n
OUR+                  S	5      n
[         R,                  " U
SS9n
U R&                  (       a  UR                  X-  5        U R(                  (       a(  UR                  U	[         R.                  " U
5      -  5        U R0                  (       Ga  UR                  S5      R                  UR                  5       5      R!                  UR"                  5      n[         R2                  " S	UR                  S	   S	-   S9R                  S5      R                  S5      R                  UR                  5       5      R!                  UR"                  5      R!                  UR                  5      nUR                  UR                  s=:X  a  UR                  :X  d   e   eX{-  n[         R*                  " X'-  S	5      n	S
U;   a2  US
   R                  S5      R                  U	R                  5       5      n
OUR+                  S	5      n
[         R,                  " U
SS9n
UR                  X-  5        U R4                  (       Ga`  UR                  u  pn[         R6                  R9                  5       (       a  UR!                  [         R:                  5      nUR=                  S	5      R%                  S	5      u  nn[         R>                  " US:H  US	-
  U5      nUU-
  S	-
  nUR                  S5      RA                  S	U5      nUR                  S	5      nUR                  US	U4:X  d   eUR                  S5      R                  UR                  5       5      R!                  UR"                  5      n[         RB                  " X'-  S	U5      RE                  S	S9nUR                  U5        [         RF                  " US	5      nUUS'   U$ )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    featuresr5   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_vectors                       r$   forwardPooling.forward   s   #$67  8+ %&,223B7@P@W@W_d_j_jk 	
 ""('B$_5M -66 -a 0 5 5 701N1n}n,- && %;=MaQRd=STI!!),''((,334D4I4I4KLOOP`PfPfg   :>A56!II&6:1=M!!-0((D,R,R,R((,334D4I4I4KLOOP`PfPfg   #YY'7'MqQN #h.#$78BB2FMMnNaNaNcd.2215{{86H,,%%n&?@55%%nuzz(7K&KL000((,334D4I4I4KLOOP`PfPfg  
 1*:*@*@*Ca*GH12(--/0$**+$++,  ==$4$:$:W>Q>W>WWWWWW"5"?"YY'7'MqQN #h.#$78BB2FMMnNaNaNcd.2215{{86H!!.";<&&&&6&<&<#B
 yy##%%!/!2!25;;!?,11!488;OFGkk&A+w{GDG$w.2N ,55b9@@JON+55a8N!''B:+>>>> ((,334D4I4I4KLOOP`PfPfg   %5%KQP^_gglmgnI!!),		.!4)6%&r&   c                    U R                   $ N)r   r*   s    r$    get_sentence_embedding_dimension(Pooling.get_sentence_embedding_dimension   s    ,,,r&   )safe_serializationc               &    U R                  U5        g rm   )save_config)r    output_pathrp   argskwargss        r$   savePooling.save   s    %r&   )	r   r   r   r   r   r   r   r   r   )NFFTFFFT)r   intr!   z
str | Noner   boolr   ry   r   ry   r   ry   r   ry   r   ry   r   ry   returnNone)rz   str)rZ   dict[str, Tensor]rz   r}   )rz   rx   )rs   r|   rp   ry   rz   r{   )__name__
__module____qualname____firstlineno____doc__r   config_keysr   r+   r2   rj   rn   rv   __static_attributes____classcell__)r#   s   @r$   r   r   	   s    BM	K $(',(-)-2716',#1["%1[ !1[ !%	1[
 "&1[ #'1[ ,01[ +/1[ !%1[ 1[ 
1[ 1[f4$jX- HL & &r&   r   )
__future__r   rC   r   #sentence_transformers.models.Moduler   r    r&   r$   <module>r      s    "   6n&f n&r&   