o
    پi                     @   s(  d dl mZ d dlmZ d dlmZmZmZmZm	Z	 d dl
Z
d dlmZ d dlmZ eG dd dZG d	d
 d
eZ	 d#dedede
jjdededee fddZde	eee f dee fddZdedefddZdedee defddZddgZddgZ	d$ded ee
j de
jfd!d"ZdS )%    )	dataclass)Enum)IterableOptionalSetTupleUnionN)ForwardBatch)
AutoConfigc                   @   sv   e Zd ZU eed< eed< eed< ejed< ejed< ejed< ejed< ee ed< eej ed	< eej ed
< dS )LoRABatchInfouse_cuda_graphbsnum_segments
seg_indptrweight_indices
lora_ranksscalingsmax_lenseg_lenspermutationN)	__name__
__module____qualname__bool__annotations__inttorchTensorr    r   r   I/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/lora/utils.pyr      s   
 



r   c                   @   s   e Zd ZdZdZdS )LoRATyper      N)r   r   r   LORA_ALORA_Br   r   r   r   r    ,   s    r    module_nameconfig
base_model	layer_idxlora_added_vocab_sizereturnc                 C   s   t |dr|| |S 	 t|d|j|j }| dkr'|j||j|jd   fS | dkr3||j |jfS | dkr?|j|jd fS | dkrI|j|jfS | dkrU|j| |jfS | d	kra|j|j| fS td
|  )zm
    Given a module_name (might be a stacked name), return the hidden dims of modules' input and output.
    get_hidden_dimhead_dimqkv_proj   o_projgate_up_proj	down_projembed_tokenslm_headz#get_hidden_dim not implemented for )	hasattrr*   getattrhidden_sizenum_attention_headsnum_key_value_headsintermediate_size
vocab_sizeNotImplementedError)r$   r%   r&   r'   r(   r+   r   r   r   r*   1   s2   
r*   target_modulesc                 C   sf   t | trdhS ddddddddddddd}t }| D ]}|dd }|||}|| q|S )	a  
    Mapping a list of target module name to names of the normalized LoRA weights.
    Handles both base module names (e.g., "gate_proj") and prefixed module names (e.g., "feed_forward.gate_proj").

    Also handles PEFT shorthand strings like "all-linear" or "all" by returning
    {"all"} as a sentinel value (the caller should check for "all" and fall
    back to the CLI --lora-target-modules to determine the concrete module set).
    allr,   r/   r1   r2   )q_projk_projv_proj	gate_projup_projr1   	vocab_emb
embeddingsword_embeddingsr2   outputunembed_tokens.)
isinstancestrsetsplitgetadd)r;   params_mappingresultname	base_namenormalized_namer   r   r   get_normalized_target_modulesd   s*   
rT   c                 C   s   ddd}| |v r||  S dS )zM
    Mapping a lora module name to its magnification at output dimension
       r-   )r,   r/   r!   r   )r$   stacked_rankr   r   r   get_stacked_multiply   s   rW   full_module_namec                 C   s.   |D ]
}|| v r|  S qt d|  d| )z
    Get the target module name in target_modules that can match full_module_name.

    If there is a target module name in target_modules that can match full_module_name, return this name
    Else raise ValueError.
    z#Cannot find target module name for z in )
ValueError)rX   r;   target_moduler   r   r   get_target_module_name   s   r[   r1   r2   r.   r0   forward_batchdevicec                 C   s   |d u rt  n|}t |[ | j rt j| jt jd}n3| j r2t j	| jf| j
jt jd}n(| j rJ| jj|kr@| jnt j| jt jd}ntd| j W d    |S W d    |S W d    |S 1 smw   Y  |S )N)dtype)size
fill_valuer^   zUnsupported forward mode: )r   get_default_devicer]   forward_mode	is_decodeones
batch_sizeint32is_target_verifyfull	spec_infodraft_token_num	is_extendextend_seq_lenstensorextend_seq_lens_cpurY   )r\   r]   r   r   r   r   generate_sequence_lengths   s<   


	


ro   )r   )N)dataclassesr   enumr   typingr   r   r   r   r   r   ,sglang.srt.model_executor.forward_batch_infor	   &sglang.srt.utils.hf_transformers_utilsr
   r   r    rJ   nnModuler   r*   rK   rT   rW   r[   EMBEDDING_NAMES!ROW_PARALLELISM_LINEAR_LORA_NAMESr]   r   ro   r   r   r   r   <module>   sP     

3
(