o
    پi`                     @   sr   d dl mZ d dlZd dlmZ d dlm  mZ d dlm	Z	 d dl
mZ eG dd dZG dd dejZdS )	    )	dataclassN)PretrainedConfig)ForwardBatchc                   @   s   e Zd ZU ejed< dS )SparseEmbeddingOutput
embeddingsN)__name__
__module____qualname__torchTensor__annotations__ r   r   S/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/layers/sparse_pooler.pyr      s   
 r   c                       sL   e Zd ZdZdef fddZdejdede	fdd	Z
d
efddZ  ZS )SparsePooleraJ  A layer that pools hidden states into sparse vocabulary-space embeddings.

    This layer does the following:
    1. Applies a linear transformation + ReLU to get token-level weights
    2. Maps these weights to vocabulary positions using token IDs
    3. Aggregates weights for repeated tokens using max pooling
    4. Returns sparse embeddings in vocabulary space

    Attributes:
        config: Model configuration containing vocab_size and hidden_size
        sparse_linear: Linear layer for computing token weights
        vocab_size: Size of vocabulary for output embeddings
    configc                    sh   t    t|dstdt| dt|ds#tdt| d|j| _t|jd| _	d| _
d S )N
vocab_sizezConfig z( missing required 'vocab_size' attributehidden_sizez) missing required 'hidden_size' attribute   F)super__init__hasattrAttributeErrortyper   nnLinearr   sparse_linear_weights_loaded)selfr   	__class__r   r   r      s   



zSparsePooler.__init__hidden_statesforward_batchreturnc                 C   s   | j stdt| |d}ttjt	|j
|jd|j
}tjt	|j
| j|j|jd}|| j |j }|djd||dd t|dS )	aM  
        Forward pass for sparse pooling.

        Args:
            hidden_states: Packed sequence hidden states [total_tokens, hidden_size]
            forward_batch: Batch information with sequence lengths and input_ids

        Returns:
            SparseEmbeddingOutput with embeddings of shape [batch_size, vocab_size]
        z<Sparse pooling weights not loaded. Call load_weights() first)device)dtyper$   r   amax)reduce)r   )r   
ValueErrorFrelur   squeezer
   repeat_interleavearangelenextend_seq_lensr$   zerosr   r%   	input_idsviewscatter_reduce_r   )r   r    r!   token_weightsbatch_indicessparse_embeddingflat_indicesr   r   r   forward0   s0   

zSparsePooler.forward
state_dictc                 C   s   | j | d| _dS )z3Load weights from state dict (called by the model).TN)r   load_state_dictr   )r   r9   r   r   r   load_weights_   s   
zSparsePooler.load_weights)r   r   r	   __doc__r   r   r
   r   r   r   r8   dictr;   __classcell__r   r   r   r   r      s    
/r   )dataclassesr   r
   torch.nnr   torch.nn.functional
functionalr)   transformersr   &sglang.srt.model_executor.model_runnerr   r   Moduler   r   r   r   r   <module>   s    