o
    پi
                     @   s8   d Z ddlZddlmZ dd Zdedejfdd	ZdS )
z.Utility functions for vision attention layers.    N)get_attention_tp_sizec                 C   sr   t  }t| jdt| jdd}| jj| }d}|| dkr)|| d | | | }t| jd| t| jd| dS )zWUpdate HF config to ensure vision attention num_attention_heads is divisible by tp_size	num_headsnum_attention_headsNr      head_dimnum_dummy_heads)r   getattrvision_confighidden_sizesetattr)configtp_sizer   r   r    r   \/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/layers/attention/vision_utils.py"update_vit_attn_dummy_heads_config   s   r   nameloaded_weightc           	         s  | j j}|dkr
|S | j jdv r\|jddd\}}}dr*||jd g ndr4|g ntd  fd	d
}||||||}}}tj|||gdd}|S t	fdddD rdru||jd g ndr|g ntd |
 }tj|ddf|gdddd}|S dv r|
|jd | }tj||gdd}|S dv sdv r|
| }tj||gdd}|S )z)Pad attention qkv weights for dummy headsr   zattn.qkv_proj   dimz.weightz.biaszUnsupported weight with name=c                    s,   t j| ddf|  gddddS )Nr   r   r   r   )torchcat	unflatten	new_zerosflatten)x)dummy_shaper   r   r   <lambda>)   s    
z*pad_vit_attn_dummy_heads.<locals>.<lambda>c                    s   g | ]}| v qS r   r   ).0_)r   r   r   
<listcomp>.   s    z,pad_vit_attn_dummy_heads.<locals>.<listcomp>)zattn.q_projzattn.k_projzattn.v_projr   zattn.proj.weightzattn.q_norm.weightzattn.k_norm.weight)r	   r   r   chunkendswithshapeRuntimeErrorr   r   anyr   r   r   )	r   r   r   r   wqwkwvpad_funcpadded_weightr   )r   r   r   r   pad_vit_attn_dummy_heads   sL   






r,   )__doc__r   sglang.srt.layers.dp_attentionr   r   strTensorr,   r   r   r   r   <module>   s
    