o
    8wiu                     @  s  d dl mZ d dlZd dlZd dlZd dlmZ d dlZd dlZd dlm	Z	m
Z
 d dlmZ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mZ d	dlmZ dd Zdd Z	d4ddZ 	d5d6ddZ!d7d#d$Z"			d8d9d&d'Z#d(d)d*d+Z$	d:d;d2d3Z%dS )<    )annotationsN)Optional)file_existshf_hub_download)EntryNotFoundErrorLocalEntryNotFoundError)	load_file)http_user_agent)PEFT_TYPE_TO_PREFIX_MAPPING   )EMBEDDING_LAYER_NAMESSAFETENSORS_WEIGHTS_NAMEWEIGHTS_NAMEAuxiliaryTrainingWrappercheck_file_exists_on_hf_hubinfer_device)PeftTypec                 C  s"   t | dot| jtjjtjjfS )z.Check if the layer has an embedding base layer
base_layer)hasattr
isinstancer   torchnnLinear	Embedding)layer r   U/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/peft/utils/save_and_load.pyhas_valid_embedding_base_layer)   s   "r   c                 C  s:   |   D ]\}}|s||ks|t|ddkr|  S qdS )z7Get the name of the embedding module for a given layer.r   N)named_modulesgetattr)modelr   is_embedding_in_target_modulesnamemoduler   r   r   get_embedding_layer_name.   s
   r$   defaultFautoc              	     s  |rt | d| } | j  du r|  jtjtjfv rj}|dkr/fddD }n9|dkr=fddD }n+|dkrfi }D ]}d	|v rd| ||< |d	d
 d }|v rd| ||< qEnt	 fdd|
 D }jtjkrj}	|	dur fdd|	
 D }	|	_| |	| }jrd  dfddfdd|
 D }njtjkrj}|dkrʇfddD }nw|dkrهfddD }nh|dkri }D ]!}d|v r| ||< |dd
 d }|v r| ||< qn;t	jtjkrfddD }n'jrUi }jtjkr?| j  j|d< | j  j|d< | j  jj}
njrK| j  jj}
n|  }
|
|d< njtjkrtj fddD }jrd  vr{tdd   |d  < d   |d  < njtjkrfddD }njtjkr!i }jd k rt j!}njd!k rt j"}njd"k rt j#}nt j$}j%rD ]:}d#|v r
| &j&\}}|'|d$ |j(|d%i |'|d& t j)|d'd(dddddd'f * i qn	fd)dD }d*   |d*  < n jt+tv r9tj fd+dD }ntd,j | , D ](\}t-|t.rlfd-d
 D }|'fd.d|/ |
 D  qEd/}|d0krt0d1rt1fd2d3t2D rjtj3krt45d4 d5 }}n`|d0krt t | d6dd7d}t d8d}d/}|durt6j78t6j79|d9}|pt:|d9}|du rt45d:| d; d/}n|}|r|r|r|| j;j<=|j>krt45d< d5}nd/}|r-t0| d=r-| ? | @ fD ]"}|rtA|r*tB| ||r*|'fd>d
 D  q	n|r5t45d?  fd@d|
 D }|S )AuQ  
    Get the state dict of the Peft model.

    Args:
        model ([`PeftModel`]): The Peft model. When using torch.nn.DistributedDataParallel, DeepSpeed or FSDP,
            the model should be the underlying model/unwrapped model (i.e. model.module).
        state_dict (`dict`, *optional*, defaults to `None`):
            The state dict of the model. If not provided, the state dict of the passed model will be used.
        adapter_name (`str`, *optional*, defaults to `"default"`):
            The name of the adapter whose state dict should be returned.
        unwrap_compiled (`bool`, *optional*, defaults to `False`):
            Whether to unwrap the model if torch.compile was used.
        save_embedding_layers (`Union[bool, str]`, , *optional*, defaults to `auto`):
            If `True`, save the embedding layers in addition to adapter weights. If `auto`, checks the common embedding
            layers `peft.utils.other.EMBEDDING_LAYER_NAMES` in config's `target_modules` when available. Based on it
            sets the boolean flag. This only works for 🤗 transformers models.
    	_orig_modNnonec                      i | ]}d |v r| | qS )lora_r   .0k
state_dictr   r   
<dictcomp>X       z-get_peft_model_state_dict.<locals>.<dictcomp>allc                   &   i | ]}d |v sd|v r| | qS r*   biasr   r+   r.   r   r   r0   Z      & 	lora_onlyr*   r   r5   c                   s.   i | ]\}}d |v r |v sd|v r||qS r4   r   r,   r-   vadapter_namer   r   r0   e   s   . c                   $   i | ]\}}| d   d|qS . replacer8   r:   r   r   r0   i      $ lora_magnitude_vector..weightc                   s   |   r| d d } | S )Niendswithr-   )new_dora_suffixr   r   renamed_dora_weightss   s   
z7get_peft_model_state_dict.<locals>.renamed_dora_weightsc                      i | ]	\}} ||qS r   r   r8   rI   r   r   r0   x       c                   r)   )boft_r   r+   r.   r   r   r0   }   r1   c                   r3   )rM   r5   r   r+   r.   r   r   r0      r6   	boft_onlyrM   c                   s*   i | ]}| d d dr| | qS )r>   	adaption_)split
startswithr+   r.   r   r   r0      s   * prefix_task_colsprefix_task_rowsprompt_embeddingsc                   s   i | ]}|v r| | qS r   r   r+   )r/   vera_prefixr   r   r0      r1   zbase_model.vera_A.zModel was initialised to not save vera_A and vera_B but config now specifies to save projection! Set `config.save_projection` to `False`.zbase_model.vera_B.c                   r)   )internal_xlora_classifierr   r+   r.   r   r   r0      r1      i   l        vblora_logits_topk_indices)dtype_topk_weightsrO   dimc                   r)   )rY   r   r+   r.   r   r   r0      r1   zbase_model.vblora_vector_bank.c                   s   i | ]} |v r|| qS r   r   r+   )prefixr/   r   r   r0      r1   zUnknown PEFT type passed: c                   s2   i | ]\}}|   d r|  d |qS r>   )rR   removeprefixr8   r"   r   r   r0      s
    c                   s    i | ]\}}  d | |qS r`   r   r8   rb   r   r   r0      s     Fr&   target_modulesc                 3  s    | ]}| j v V  qd S )N)rc   r+   )configr   r   	<genexpr>   s    z,get_peft_model_state_dict.<locals>.<genexpr>zXSetting `save_embedding_layers` to `True` as embedding layers found in `target_modules`.Trd   
vocab_sizebase_model_name_or_pathzconfig.jsonz Could not find a config file in z4 - will assume that the vocabulary was not modified.zdSetting `save_embedding_layers` to `True` as the embedding layer has been resized during finetuning.get_input_embeddingsc                   s   i | ]\}} |v r||qS r   r   r8   )embedding_module_namer   r   r0   	  r1   uY   Could not identify embedding layer(s) because the model is not a 🤗 transformers model.c                   r<   r=   r@   r8   r:   r   r   r0     rB   )Cr   peft_configr/   	peft_typer   LORAADALORAr5   rQ   NotImplementedErroritemsrank_pattern!resize_state_dict_by_rank_patternuse_doraBOFTADAPTION_PROMPTis_prompt_learningMULTITASK_PROMPT_TUNINGprompt_encoderrS   rT   	embeddingweightinference_modeget_prompt_embedding_to_saveVERAr
   save_projection
ValueErrorXLORAVBLORAnum_vectorsr   uint8int16int32int64save_only_topk_weightstopkupdatetosoftmax
contiguouslistr   r   r   adapter_state_dictr   anyr   TRAINABLE_TOKENSwarningswarnospathexistsjoinr   rd   	__class__from_pretrainedrf   rh   get_output_embeddingsr   r$   )r    r/   r;   unwrap_compiledsave_embedding_layersr5   	to_returnr-   	bias_namerp   rU   indices_dtypelogitsindicesr#   module_state_dictr!   rf   model_idhas_base_configlocal_config_existsr   r   r   )	r;   rd   ri   r"   rH   r_   rI   r/   rV   r   get_peft_model_state_dict6   s8  







6









r   r    torch.nn.Modulepeft_model_state_dictdict[str, torch.Tensor]ignore_mismatched_sizesboolreturnRtuple[dict[str, torch.Tensor], list[tuple[str, tuple[int, ...], tuple[int, ...]]]]c                 C  s   |s|g fS g }|   }| D ]3\}}||vrq|| jd dkr/||  d | kr/q|| j|jkrC|||j|| jf q|D ]\}}}||= qF||fS )NrO   r      )r/   ro   shapenumelappend)r    r   r   
mismatchedr/   keytensor_r   r   r   _find_mismatched_keys  s   *r   r/   r;   strparameter_prefixc                 C  s   i }|   D ];\}}||v r=||d }d|v r1d|ddd }||| d| }n| d| }|||< q|||< q|S )zbUtility function to remap the state_dict keys to fit the PEFT model by inserting the adapter name.r   r>   N)ro   rQ   r   rA   )r/   r;   r   r   r   valsuffixsuffix_to_replacer   r   r   $_insert_adapter_name_into_state_dict.  s   

r   low_cpu_mem_usagec                   s|  | j | }|}|  D ],\}}t|tr7||}	|	D ]}
| d|
 }| d|	|
  }|| ||< ||= qq|jsA|jtjkrE|}n|jtj	krN|}n|jt
v rJi }t
|j }|jtjkr|jr| j| j\}}t| }|D ][}
d|
v r||
 tj}|
dd}||
dd }tj|d|jddd gdd	}t|}tg |jd
d |td|jd||}|||< ||
= ||
dd= qtt|||d}|jtjkr|j}|d
ur|  || na|jtj!kr|j"rd|vrt#d|j"sd|v rt$%d n@|j"st$%d n6|jtj&kr4d|   fddfdd|' D }n|jtj(krIt)dd |D rIt#dnt*t+| ||d\}}|rt| j,|ddd}| - D ]}t.|drq|/| qdn| j,|dd}|jr| j0| j1j,d|d idd |jtj2kr| j0| j,|dd |rd 3d!d" |D }d#| j4j5 d$| d}t$%| |S )%a  
    Set the state dict of the Peft model.

    Args:
        model ([`PeftModel`]):
            The Peft model.
        peft_model_state_dict (`dict`):
            The state dict of the Peft model.
        adapter_name (`str`, *optional*, defaults to `"default"`):
            The name of the adapter whose state dict should be set.
        ignore_mismatched_sizes (`bool`, *optional*, defaults to `False`):
            Whether to ignore mismatched in the state dict.
        low_cpu_mem_usage (`bool`, `optional`, defaults to `False`):
            This argument must be `True` if the `model` was loaded with adapter weights on the meta device, e.g. after
            calling `inject_adapter_in_model` with `low_cpu_mem_usage=True`. Otherwise, leave it as `False`.

    r>   rZ   r?   r\   r   rO   T)keepdimr]   Nz-inf)r;   r   zbase_model.vera_AzXSpecified to load vera_A and vera_B from state dictionary however they were not present!zSpecified to not load vera_A and vera_B from state dictionary however they are present in state dictionary! Consider using them to ensure checkpoint loading is correct on all platforms using `peft_config.save_projection = True`zSpecified to not load vera_A and vera_B from state dictionary. This means we will be relying on PRNG initialisation to restore these projections using `config.projection_prng_key`, which may not be accurate on all system configurations.rC   c                   s   |   r	| d } | S )NrD   rE   rG   )old_dora_suffixr   r   rI     s   
z7set_peft_model_state_dict.<locals>.renamed_dora_weightsc                   rJ   r   r   r8   rK   r   r   r0     rL   z-set_peft_model_state_dict.<locals>.<dictcomp>c                 s  s    | ]}d |v V  qdS )z.oft_r.Nr   )r,   r   r   r   r   re     s    z,set_peft_model_state_dict.<locals>.<genexpr>zTrying to load old OFT checkpoint, which is no longer supported. Please install PEFT <= v0.15.2 to load it or train a new OFT adapter.)r   F)strictassign%_move_adapter_to_device_of_base_layer)r   ry   rU   
c              	   S  s*   g | ]\}}}d | d| d| dqS )z- z: found shape z in the checkpoint and z in the model instantiatedr   )r,   r   shape1shape2r   r   r   
<listcomp>  s    z-set_peft_model_state_dict.<locals>.<listcomp>zSome weights of zy were not initialized from the model checkpoint and are being ignored because you passed `ignore_mismatched_sizes=True`: )6rj   r   r   r   adapter_state_dict_load_mapru   rk   r   rt   r   r
   r   r   vblora_vector_bankr   r   keysr   r   longrA   catsumlogzerosfill_floatdevicescatterr   rm   rp   resize_modules_by_rank_patternr|   r}   r~   r   r   rl   ro   OFTr   rn   r   load_state_dictmodulesr   r   rw   rx   rv   r   r   __name__)r    r   r;   r   r   rd   r/   r"   r#   key_mapr-   
lookup_key	store_keyr   r   r   state_dict_keysr9   original_keytopk_weightstopk_logitsmatrixrp   mismatched_keysload_resultmismatched_warningmsgr   )r   rI   r   set_peft_model_state_dictA  s   



 






r   T)weights_onlyc                 O  s   t j|d| i|S )zCall torch.load and handle weights_only.

    Defaults to weights_only=True to anticipate upcoming switch on the PyTorch side.

    r   )r   load)r   argskwargsr   r   r   
torch_load  s   r   r   r   Optional[str]key_mappingOptional[dict[str, str]]dictc                   s    dddurtj|  d n| }|du rt }d fdd	}d vr*t  d< tjtj|tr?tj|t}d}ntjtj|trTtj|t}d}nt	j
jr|dd} d	d zt| |fd	di }d}W nv ty   |dd}t| |fd	di }d}Y n\w   d
d}	|	du r  dd}	|dd}t| |  dd  dd|	d}
|
}|
rt| tfi  }n'zt| tfi  }W n ty   td|  d|  dt dt d|  dw |r	ttjdr|tdkrt|dd}nt||d}n	t|t|d}|s|}|S i }| D ]F\}}|dr,d}n|dr5d}ntd||}| D ]\}}t|||\}}|dkrX|} nqB| | }|||< q|S )aP  
    A helper method to load the PEFT weights from the HuggingFace Hub or locally

    Args:
        model_id (`str`):
            The local path to the adapter weights or the name of the adapter to load from the HuggingFace Hub.
        device (`str`):
            The device to load the weights onto.
        key_mapping (dict, *optional*, defaults to None)
            Extra mapping of PEFT `state_dict` keys applied before loading the `state_dict`. When this mapping is
            applied, the PEFT-specific `"base_model.model"` prefix is removed beforehand and the adapter name (e.g.
            `"default"`) is not inserted yet. Only pass this argument if you know what you're doing.
        hf_hub_download_kwargs (`dict`):
            Additional arguments to pass to the `hf_hub_download` method when loading from the HuggingFace Hub.
    	subfolderNTc                   s2   | rt nt} dd d urtj d |S |S )Nr   )r   r   getr   r   r   )use_safetensorsweights_namehf_hub_download_kwargsr   r   get_hub_filename  s   z+load_peft_weights.<locals>.get_hub_filename
user_agentF)r   local_files_onlytokenuse_auth_tokenrevision	repo_type)repo_idfilenamer   r   r   zCan't find weights for z in z8 or in the Hugging Face Hub. Please check that the file z or z is present at r>   mpscpu)r   )map_locationzbase_model.model.zbase_model.zAn error occurred while trying to load a PEFT state_dict with key_mapping. This should not happen. Please open an issue on https://github.com/huggingface/peft/issues and report the error.r   )T)r   r   r   r   r   r	   r   r   r   huggingface_hub	constantsHF_HUB_OFFLINEpopr   r   r   r   r~   r   r   backendsr   safe_load_filer   ro   rR   ra   resubn)r   r   r   r   r   r   r   r   hub_filenamer   has_remote_safetensors_fileadapters_weightsremapped_adapters_weightsr   r   r_   patternreplacementkey_new	n_replacekey_with_prefixr   r   r   load_peft_weights  s   







r  )Nr%   Fr&   )F)r    r   r   r   r   r   r   r   )r/   r   r;   r   r   r   r   r   )r%   FF)r   r   r   r   )NN)r   r   r   r   r   r   r   r   )&
__future__r   r   r  r   typingr   r   r   r   r   huggingface_hub.errorsr   r   safetensors.torchr   r  transformers.utilsr	   peft.mappingr
   otherr   r   r   r   r   r   
peft_typesr   r   r$   r   r   r   r   r   r  r   r   r   r   <module>   s<    	
 ^
 "
