o
    Nie                     @  s2  U d dl mZ d dlZd dl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 ddl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eeeeed
Zded< G dd dZG dd deZG dd deZ G dd deZ!G dd deZ"G dd deZ#G dd deZ$G dd deZ%dS )    )annotationsN)Optional)	AutoModelAutoModelForCausalLMAutoModelForQuestionAnsweringAutoModelForSeq2SeqLM"AutoModelForSequenceClassificationAutoModelForTokenClassificationAutoTokenizer   )
PeftConfig)	PeftModelPeftModelForCausalLMPeftModelForFeatureExtractionPeftModelForQuestionAnsweringPeftModelForSeq2SeqLM"PeftModelForSequenceClassificationPeftModelForTokenClassification)TOKENIZER_CONFIG_NAME)check_file_exists_on_hf_hub)SEQ_CLSSEQ_2_SEQ_LM	CAUSAL_LM	TOKEN_CLSQUESTION_ANSFEATURE_EXTRACTIONzdict[str, type[PeftModel]] MODEL_TYPE_TO_PEFT_MODEL_MAPPINGc                   @  s4   e Zd ZdZdZdd Ze				ddddZdS )_BaseAutoPeftModelNc                 O  s&   t | jj d| jj d| jj d)Nz+ is designed to be instantiated using the `z5.from_pretrained(pretrained_model_name_or_path)` or `z.from_config(config)` methods.)EnvironmentError	__class____name__)selfargskwargs r$   =/home/ubuntu/.local/lib/python3.10/site-packages/peft/auto.py__init__;   s   
z_BaseAutoPeftModel.__init__defaultFadapter_namestris_trainableboolconfigOptional[PeftConfig]revisionOptional[str]c                 K  s  t j|fd|i|}|j}|j}	t|dd}
| jdur | j}n| jdu r-|
dur-td|
durKt|
 }| jj	|j	krJtd|j	 d| jj	 dn)|
du rpt|dddurpt|dd}|d	 }|d
 }t
|}t||}ntd|j|fd|	i|}d}tjtj|trd}n|dd}|du r|dd}t|t||dd|d}|rt|drtj||ddd}| jjd }t||kr|t| | jj||f|||d|S )a  
        A wrapper around all the preprocessing steps a user needs to perform in order to load a PEFT model. The kwargs
        are passed along to `PeftConfig` that automatically takes care of filtering the kwargs of the Hub methods and
        the config object init.
        r.   	task_typeNzCannot use `AutoPeftModel` with a task type, please use a specific class for your task type. (e.g. `AutoPeftModelForCausalLM` for `task_type='CAUSAL_LM'`)zExpected target PEFT class: z, but you have asked for: zE make sure that you are loading the correct model for your task type.auto_mappingbase_model_classparent_libraryzxCannot infer the auto class from the config, please make sure that you are loading the correct model for your task type.FTtokenuse_auth_token	repo_type)repo_idfilenamer.   r6   r4   get_input_embeddingstrust_remote_code)r:   r   )r(   r*   r,   )r   from_pretrainedbase_model_name_or_pathr.   getattr_target_class
ValueErrorr   _target_peft_classr    	importlibimport_moduleospathexistsjoinr   getr   hasattrr
   r9   weightshapelenresize_token_embeddings)clspretrained_model_name_or_pathr(   r*   r,   r.   r#   peft_configbase_model_pathbase_model_revisionr0   target_classexpected_target_classr1   r2   parent_library_namer3   
base_modeltokenizer_existsr4   	tokenizerembedding_sizer$   r$   r%   r;   C   sr   


z"_BaseAutoPeftModel.from_pretrained)r'   FNN)r(   r)   r*   r+   r,   r-   r.   r/   )r    
__module____qualname__r>   r@   r&   classmethodr;   r$   r$   r$   r%   r   7   s    r   c                   @  s   e Zd ZdZeZdS )AutoPeftModelN)r    rY   rZ   r>   r   r@   r$   r$   r$   r%   r\          r\   c                   @     e Zd ZeZeZdS )AutoPeftModelForCausalLMN)r    rY   rZ   r   r>   r   r@   r$   r$   r$   r%   r_      r]   r_   c                   @  r^   )AutoPeftModelForSeq2SeqLMN)r    rY   rZ   r   r>   r   r@   r$   r$   r$   r%   r`      r]   r`   c                   @  r^   )&AutoPeftModelForSequenceClassificationN)r    rY   rZ   r   r>   r   r@   r$   r$   r$   r%   ra      r]   ra   c                   @  r^   )#AutoPeftModelForTokenClassificationN)r    rY   rZ   r	   r>   r   r@   r$   r$   r$   r%   rb      r]   rb   c                   @  r^   )!AutoPeftModelForQuestionAnsweringN)r    rY   rZ   r   r>   r   r@   r$   r$   r$   r%   rc      r]   rc   c                   @  r^   )!AutoPeftModelForFeatureExtractionN)r    rY   rZ   r   r>   r   r@   r$   r$   r$   r%   rd      r]   rd   )&
__future__r   rA   rC   typingr   transformersr   r   r   r   r   r	   r
   r,   r   
peft_modelr   r   r   r   r   r   r   utils.constantsr   utils.otherr   r   __annotations__r   r\   r_   r`   ra   rb   rc   rd   r$   r$   r$   r%   <module>   s0   $
$	
a