o
    )io$                     @   s  U d dl Z d dlmZmZmZ d dlZd dlZd dlm	Z	m
Z
mZmZ d dlmZ d dlmZ 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mZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ d d
l%m&Z& eryd dl'm(Z( d dl)m*Z* d dl+m,Z, ee-Z.e$eee!e e#e"eeeeeehZ/e0e1e  e2d< 	d/dej3de4dede5dee dej3fddZ6	d/ddddde4dedee defddZ7dej3de8dej3dej3fdd Z9	d/d!e8d"ed# de:e8e;e;f fd$d%Z<d&ee8e5e8 f d'e5e8 de;fd(d)Z=dej3de5e8 fd*d+Z>d,e8de8fd-d.Z?dS )0    N)TYPE_CHECKINGOptionalUnion)EntryNotFoundErrorHfHubHTTPErrorHFValidationErrorRepositoryNotFoundError)nn)PretrainedConfig)
LoRAConfig)init_logger)#ColumnParallelLinearWithShardedLoRA)MergedColumnParallelLinearWithShardedLoRA&MergedQKVParallelLinearWithShardedLoRA QKVParallelLinearWithShardedLoRA RowParallelLinearWithShardedLoRA)	BaseLayerWithLoRAColumnParallelLinearWithLoRALogitsProcessorWithLoRA"MergedColumnParallelLinearWithLoRAMergedQKVParallelLinearWithLoRAQKVParallelLinearWithLoRAReplicatedLinearWithLoRARowParallelLinearWithLoRAVocabParallelEmbeddingWithLoRA)
LinearBase)LogitsProcessor)ParallelLMHead)WeightsMapper_all_lora_classeslayer	max_loraslora_configpacked_modules_listmodel_configreturnc                 C   s>   t D ]}|j| |||dr|| }|||| |  S q| S )N)Zsource_layerr"   r#   r$   )r   Zcan_replace_layercreate_lora_weights)r    r!   r"   r#   r$   Zlora_clsZinstance_layer r'   [/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/vllm/lora/utils.py
from_layer=   s   r)   r   lm_headr   c                 C   s0   t | |j|jj|jj| }|||| |S N)r   Zembedding_dimweightZdtypeZdeviceZget_sharded_to_full_mappingr&   )r    r*   r!   r"   r$   retr'   r'   r(   from_layer_logits_processorO   s   r.   modelmodule_name
new_modulec                 C   s<   |  d|ddd }|dd }t||| |S )z1Replace a submodule in a model with a new module..N)Zget_submodulejoinsplitsetattr)r/   r0   r1   parenttarget_namer'   r'   r(   replace_submodule]   s   r9   nameweights_mapperr   c                 C   s  |  dr| dd} |r|| n| } d|  } n	|r || n| } |  dr)dnd}| d}|d dkrT|d d	ksB|d d
krTd||d }||d d	kdfS |d dks`|d dkrrd||d }||d dkdfS |d dkrd||d }|ddfS t|  d)a  Parse the name of lora weights.

    args:
        name: the name of the fine-tuned LoRA, e.g.
            base_model.model.dense1.weight
        weights_mapper: maps the name of weight, e.g.
            `model.` -> `language_model.model.`,
    return:
        tuple(module_name, is_lora_a):
            module_name: the name of the module, e.g. model.dense1,
            is_lora_a whether the tensor is lora_a or lora_b.
            is_bias whether the tensor is lora bias.
    zbase_model.model.    r   r2   r3   r,   Zlora_AZlora_BFZlora_embedding_AZlora_embedding_BZbiasTz is unsupported LoRA weight)
startswithreplaceZ	_map_namer5   r4   
ValueError)r:   r;   start_indexpartsnew_namer'   r'   r(   parse_fine_tuned_lora_namef   s$   



rE   load_modulesexpected_lora_modulesc                 C   sT   dd }dd }t | tsdS || r(td| }|r(|dd}|||S dS )	a  
    PEFT supports passing `target_modules` in the form of regular expressions, 
    such as `model.*(q_proj|k_proj|v_proj)$`. This function is mainly used to 
    determine whether the suffix in the regular expression is present in the 
    `expected_lora_modules`.
    c                 S   s(   zt |  W dS  t jy   Y dS w )NTF)recompileerror)patternr'   r'   r(   is_valid_regex   s   
z/is_regex_target_modules.<locals>.is_valid_regexc                 S   s   t | t |S r+   )setissubset)Zsub_listZ	full_listr'   r'   r(   	is_subset   s   z*is_regex_target_modules.<locals>.is_subsetFz\((.*?)\)\$?$   |)
isinstancestrrH   searchgroupr5   )rF   rG   rL   rO   matchsuffixr'   r'   r(   is_regex_target_modules   s   	

rX   c                 C   sh   t  }|  D ](\}}t|dd}|dur|D ]}|| qt|tfr/||dd  qt|S )z2
    In vLLM, all linear layers support LoRA.
    embedding_modulesNr2   r3   )rM   Znamed_modulesgetattraddrR   r   r5   list)r/   Zsupported_lora_modulesr:   modulerY   r'   r'   r(   get_supported_lora_modules   s   r^   	lora_pathc                 C   sz   t j| r| S | drt j| S t j| rt j| S z	tj| d}W |S  t	t
ttfy<   td |  Y S w )a'  
    Resolves the given lora_path to an absolute local path.

    If the lora_path is identified as a Hugging Face model identifier,
    it will download the model and return the local snapshot path.
    Otherwise, it treats the lora_path as a local file path and
    converts it to an absolute path.

    Parameters:
    lora_path (str): The path to the lora model, which can be an absolute path,
                     a relative path, or a Hugging Face model identifier.

    Returns:
    str: The resolved absolute local path to the lora model.
    ~)Zrepo_idz'Error downloading the HuggingFace model)ospathisabsr?   
expanduserexistsabspathhuggingface_hubZsnapshot_downloadr   r   r   r   logger	exception)r_   Zlocal_snapshot_pathr'   r'   r(   get_adapter_absolute_path   s"   
	

rj   r+   )@ra   typingr   r   r   rg   regexrH   Zhuggingface_hub.utilsr   r   r   r   Ztorchr	   Ztransformersr
   Zvllm.configr   Zvllm.loggerr   Zvllm.lora.fully_sharded_layersr   r   r   r   r   Zvllm.lora.layersr   r   r   r   r   r   r   r   r   Z!vllm.model_executor.layers.linearr   Z+vllm.model_executor.layers.logits_processorr   Z3vllm.model_executor.layers.vocab_parallel_embeddingr   Z vllm.model_executor.models.utilsr   __name__rh   r   rM   type__annotations__Moduleintr\   r)   r.   rS   r9   tupleboolrE   rX   r^   rj   r'   r'   r'   r(   <module>   s   
,




3
 