o
    1Ai=                     @  sX  U d dl mZ d dlmZmZ d dl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mZ d dlmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ dd	l%m&Z& d d
l'm(Z( ermd dl)m*Z* eeeeeedZ+de,d< ee$e"e#eeeeeeee dZ-de,d< e&eeeeee!dZ.d(ddZ/	d)d*d#d$Z0	d+d,d&d'Z1dS )-    )annotations)TYPE_CHECKINGAnyN)
PeftConfig)PeftMixedModel)	PeftModelPeftModelForCausalLMPeftModelForFeatureExtractionPeftModelForQuestionAnsweringPeftModelForSeq2SeqLM"PeftModelForSequenceClassificationPeftModelForTokenClassification)AdaLoraConfigAdaLoraModelAdaptionPromptConfig	IA3ConfigIA3Model
LoHaConfig	LoHaModel
LoKrConfig	LoKrModel
LoraConfigMultitaskPromptTuningConfig	OFTConfigOFTModel
PolyConfig	PolyModelPrefixTuningConfigPromptEncoderConfigPromptTuningConfig   )	LoraModel)_prepare_prompt_learning_config)PreTrainedModel)SEQ_CLSSEQ_2_SEQ_LM	CAUSAL_LM	TOKEN_CLSQUESTION_ANSFEATURE_EXTRACTIONzdict[str, PeftModel] MODEL_TYPE_TO_PEFT_MODEL_MAPPING)ADAPTION_PROMPTPROMPT_TUNINGPREFIX_TUNINGP_TUNINGLORALOHALOKRADALORAIA3MULTITASK_PROMPT_TUNINGOFTPOLYzdict[str, PeftConfig]PEFT_TYPE_TO_CONFIG_MAPPING)r/   r0   r1   r2   r3   r5   r6   config_dictdict[str, Any]returnr   c                 C  s   t | d  di | S )z
    Returns a Peft config object from a dictionary.

    Args:
        config_dict (`Dict[str, Any]`): Dictionary containing the configuration parameters.
    	peft_typeN )r7   )r8   r<   r<   @/data/cameron/vidgen/svd_motion_lora/Motion-LoRA/LoRA/mapping.pyget_peft_config`   s   r>   defaultFmodelr#   peft_configadapter_namestrmixedboolPeftModel | PeftMixedModelc                 C  s   t | dddi}t|dr| }| jdd|_|r"t| ||dS |jt	 vr3|j
s3t| ||dS |j
r;t||}t|j | ||dS )aK  
    Returns a Peft model object from a model and a config.

    Args:
        model ([`transformers.PreTrainedModel`]):
            Model to be wrapped.
        peft_config ([`PeftConfig`]):
            Configuration object containing the parameters of the Peft model.
        adapter_name (`str`, `optional`, defaults to `"default"`):
            The name of the adapter to be injected, if not provided, the default adapter name is used ("default").
        mixed (`bool`, `optional`, defaults to `False`):
            Whether to allow mixing different (compatible) adapter types.
    config
model_typecustomto_dictname_or_pathNrB   )getattrhasattrrJ   __dict__getbase_model_name_or_pathr   	task_typer*   keysis_prompt_learningr   r"   )r@   rA   rB   rD   model_configr<   r<   r=   get_peft_modelk   s   

rV   torch.nn.Modulec                 C  sR   | j s| jr
td| jt vrtd| j dt| j }||| |d}|jS )a  
    A simple API to create and inject adapter in-place into a model. Currently the API does not support prompt learning
    methods and adaption prompt. Make sure to have the correct `target_names` set in the `peft_config` object. The API
    calls `get_peft_model` under the hood but would be restricted only to non-prompt learning methods.

    Args:
        peft_config (`PeftConfig`):
            Configuration object containing the parameters of the Peft model.
        model (`torch.nn.Module`):
            The input model where the adapter will be injected.
        adapter_name (`str`, `optional`, defaults to `"default"`):
            The name of the adapter to be injected, if not provided, the default adapter name is used ("default").
    zN`create_and_replace` does not support prompt learning and adaption prompt yet.z+`inject_adapter_in_model` does not support z" yet. Please use `get_peft_model`.rL   )rT   is_adaption_prompt
ValueErrorr;   PEFT_TYPE_TO_TUNER_MAPPINGrS   r@   )rA   r@   rB   	tuner_cls
peft_modelr<   r<   r=   inject_adapter_in_model   s   
r]   )r8   r9   r:   r   )r?   F)
r@   r#   rA   r   rB   rC   rD   rE   r:   rF   )r?   )rA   r   r@   rW   rB   rC   r:   rW   )2
__future__r   typingr   r   torchZpeft.configr   Zpeft.mixed_modelr   Zpeft.peft_modelr   r   r	   r
   r   r   r   Zpeft.tunersr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r@   r!   
peft.utilsr"   transformersr#   r*   __annotations__r7   rZ   r>   rV   r]   r<   r<   r<   r=   <module>   sX   $P	

"