o
    پi>                     @   s   d dl mZ d dlmZ d dlmZ d dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZ G dd	 d	eZeG d
d dZG dd dejZG dd dejZdS )    )	dataclass)IntEnum)OptionalN)PretrainedConfig)%get_cross_encoder_activation_function)ForwardBatchc                   @   s   e Zd ZdZdZdS )PoolingTyper      N)__name__
__module____qualname__LASTCLS r   r   L/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/layers/pooler.pyr      s    r   c                   @   s"   e Zd ZU ejeej B ed< dS )EmbeddingPoolerOutput
embeddingsN)r
   r   r   torchTensorlist__annotations__r   r   r   r   r      s   
 r   c                       sB   e Zd ZdZdedef fddZdejde	de
fd	d
Z  ZS )Poolera  A layer that pools specific information from hidden states.
    This layer does the following:
    1. Extracts specific tokens or aggregates data based on pooling method.
    2. Normalizes output if specified.
    3. Returns structured results as `PoolerOutput`.
    Attributes:
        pooling_type: The type of pooling to use (LAST, AVERAGE, MAX).
        normalize: Whether to normalize the pooled data.
    pooling_type	normalizec                    s   t    || _|| _d S N)super__init__r   r   )selfr   r   	__class__r   r   r   '   s   

zPooler.__init__hidden_statesforward_batchreturnc                 C   s  | j tjkrtj|jddd }|| }n.| j tjkr;|j}t|}|dd   tj|ddd d 7  < || }ntd| j  |j	d urjt
t|j	dk}|r_|dd |j	d f }ndd t||j	D }| jrt|trzd	d |D }n	tjj|d
dd}t|dS )Nr   )dimr	   zInvalid pooling type: .c                 S   s    g | ]\}}|d d|f qS ).Nr   ).0tensorr#   r   r   r   
<listcomp>@   s    z"Pooler.forward.<locals>.<listcomp>c                 S   s   g | ]}t jj|d ddqS )   r$   pr#   )nn
functionalr   )r%   r&   r   r   r   r'   G   s    r(   r)   r   )r   r   r   r   cumsumextend_seq_lensr   
zeros_like
ValueError
dimensionslensetzipr   
isinstancer   r+   r,   r   )r   r    r!   last_token_indicespooled_dataprompt_lensfirst_token_flat_indicesall_same_dimensionsr   r   r   forward,   s.   

&




zPooler.forward)r
   r   r   __doc__r   boolr   r   r   r   r   r<   __classcell__r   r   r   r   r      s    
r   c                       sR   e Zd ZdZ	ddedejdeej f fddZde	j
d	ed
efddZ  ZS )CrossEncodingPoolera  A layer that pools specific information from hidden states.

    This layer does the following:
    1. Extracts specific tokens or aggregates data based on pooling method.
    2. Normalizes output if specified.
    3. Returns structured results as `EmbeddingPoolerOutput`.
    Nconfig
classifierpoolerc                    s$   t    || _|| _t|| _d S r   )r   r   rB   rC   r   default_activation_function)r   rA   rB   rC   r   r   r   r   Z   s   
zCrossEncodingPooler.__init__r    r!   r"   c                 C   s   |j }d}g }|D ]$}||||  }| jdur| ||}n| |}|| ||7 }q	t|}	| jdur=| |	}	| |	d}
t|
dS )z2Pools sentence pair scores from the hidden_states.r   Nr$   r-   )	r/   rC   rB   appendr   stackrD   squeezer   )r   r    r!   r9   offsetpooled_data_lst
prompt_lenpooled_data_ifinal_shape_tensorpooled_outputscoresr   r   r   r<   e   s   







zCrossEncodingPooler.forwardr   )r
   r   r   r=   r   r+   Moduler   r   r   r   r   r   r<   r?   r   r   r   r   r@   Q   s"    r@   )dataclassesr   enumr   typingr   r   torch.nnr+   transformersr   sglang.srt.layers.activationr   ,sglang.srt.model_executor.forward_batch_infor   r   r   rO   r   r@   r   r   r   r   <module>   s   5