o
    i#&                     @   s   d dl Z d dl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
mZmZ d dlmZ d dlmZ d dlmZ eeZG d	d
 d
ZdS )    N)init_logger)LoRALayerWeights)
PEFTHelper)get_lora_idis_base_embeddding_weightsparse_fine_tuned_lora_name)TensorizerConfig)WeightsMapper)is_pin_memory_availablec                   @   sV  e Zd ZdZdededeeef ddfddZdedd fd	d
Z	dededB fddZ
dedefddZededee defddZe					d#dedeeejf dededejdB dedB dedB dee dB dd fddZedddddddddedee dededB dedejdB dedB dedB d edB dee dB dd fd!d"ZdS )$	LoRAModelzA LoRA fine-tuned model.lora_model_idranklorasreturnNc                 C   s.   || _ |dksJ d| j  || _|| _dS )z
        Args:
            lora_model_id: The integer id for the lora model.
            rank: lora rank.
            loras: module name -> weights for lora-replaced layers.

        r   z.a valid lora id should be greater than 0, got N)idr   r   )selfr   r   r    r   J/home/ubuntu/vllm_env/lib/python3.10/site-packages/vllm/lora/lora_model.py__init__   s   


zLoRAModel.__init__c                 C   s   | j || j| j dS )z[Return a copy of the object with different ids.

        Will share the underlying tensors.)r   r   )	__class__r   r   copy)r   r   r   r   r   clone0   s
   zLoRAModel.clonemodule_namec                 C   s   | j |dS )z#Get LoRA for a given module by nameN)r   get)r   r   r   r   r   get_lora:   s   zLoRAModel.get_lora	lora_namec                 C   s
   || j v S )N)r   )r   r   r   r   r   check_lora_name>   s   
zLoRAModel.check_lora_nameskip_prefixesc                 C   s,   |D ]}d| | v s|  |r dS qdS )z:Check if a module should be skipped based on skip prefixes.TF)
startswith)r   r   prefixr   r   r   _should_skip_moduleA   s
   zLoRAModel._should_skip_modulecudatensorspeft_helperdevicedtypemodel_vocab_sizeweights_mapperc	                 C   s  t |dkot }	i }
| D ]q\}}t|rq|r!| ||r!qt||\}}||
vr4t|||
|< |rjd|v rS|durS||jd krSt	d|jd  d| d|j
||d|
| _|	ri|
| j |
| _q|j
||d|
| _|	r|
| j |
| _q| ||j|
S )	z0Create a LoRAModel from a dictionary of tensors.cpulora_embedding_AN   zThe embedding LoRA size(z;) must be consistent with the base model's vocabulary size(z).)r%   r&   )strr
   itemsr   r!   r   r   from_configshapeRuntimeErrortolora_a
pin_memorylora_br)clsr   r#   r$   r%   r&   r'   r(   r   r3   r   tensor_nametensorr   	is_lora_ar   r   r   from_lora_tensorsI   s@   zLoRAModel.from_lora_tensors)r   r%   r&   r'   r(   tensorizer_config_dictr   lora_direxpected_lora_modulesr;   c             
      s  t jd}t jd}t jd}i }g dtf fdd}|	rSddlm} tdi |	}t j|jd	}| }||fd
|j	i|j
}|| n\t j|rg tj|dd}|| | D ]	}||||< qkW d   n1 sw   Y  n*t j|st j|rt j|r|n|}tj||dd}|| nt d j|du rt n||||||dS )a
  Create a LoRAModel from a local checkpoint.

        Args:
            lora_dir: The local path that has lora data.
            expected_lora_modules: Name of modules that are expected to be
                replaced by lora.
            peft_helper: Loaded lora configuration information.
            lora_model_id: LoRA model id. If not given, automatically set by
                a global counter.
            device: Device where the lora model is loaded.
            dtype: dtype of the lora model weights.
            skip_prefixes: List of module name prefixes to skip during loading.
                Models can define this to skip modules not used in inference
                (e.g., MTP layers). Format: ["mtp."]

        Returns:
            Loaded LoRA Model.
        zadapter_model.safetensorszadapter_model.binzadapter_model.ptmodulesc                    s   |   D ]F}t|rqd|v rqr |rqt|\}}d|v r;|d}||d d  }|vr:| q|ddd vrJ| qr[td d d d	d S )
N
base_layerz.expertsr+   r   zWhile loading z, expected target modules in z but received z6. Please verify that the loaded LoRA module is correct)keysr   r!   r   findappendrsplit
ValueError)r>   lora_moduler   _
expert_idxexpert_suffixr6   r=   r<   r   unexpected_modulesr(   r   r   check_unexpected_modules   s8   


zALoRAModel.from_local_checkpoint.<locals>.check_unexpected_modulesr   )TensorDeserializerzadapter_model.tensorsr&   pt)	frameworkNT)map_locationweights_onlyz doesn't contain tensors)r   r#   r$   r%   r&   r'   r(   r   r   )ospathjoindict
tensorizerrM   r   tensorizer_dir_construct_tensorizer_argsr&   deserialization_kwargsisfilesafetensors	safe_openrA   
get_tensortorchloadrE   r:   r   )r6   r<   r=   r$   r   r%   r&   r'   r(   r;   r   lora_tensor_pathlora_bin_file_pathlora_pt_file_pathr#   rL   rM   tensorizer_configtensorizer_argsfmodulelora_file_pathr   rJ   r   from_local_checkpoint{   s`   ! 


zLoRAModel.from_local_checkpoint)r"   NNNN)__name__
__module____qualname____doc__intrU   r,   r   r   r   r   boolr   staticmethodlistr!   classmethodr^   Tensorr   r&   r	   r:   setrh   r   r   r   r   r      s    



	
1	

r   )rR   r[   r^   vllm.loggerr   vllm.lora.lora_weightsr   vllm.lora.peft_helperr   vllm.lora.utilsr   r   r   +vllm.model_executor.model_loader.tensorizerr    vllm.model_executor.models.utilsr	   vllm.utils.platform_utilsr
   ri   loggerr   r   r   r   r   <module>   s   