
    bie                    d   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      Z G d de      Z G d de      Z  G d de      Z! G d de      Z" G d de      Z# G d de      Z$ G d de      Z%y)    )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                  D    e Zd ZdZdZd Ze	 	 	 	 d	 	 	 	 	 	 	 dd       Zy)_BaseAutoPeftModelNc                    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kwargss      D/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/peft/auto.py__init__z_BaseAutoPeftModel.__init__;   sR    ~~&&' (..112 3''((FH
 	
    c           	        t        j                  |fd|i|}|j                  }|j                  }	t	        |dd      }
| j
                  | j
                  }n| j
                  |
t        d      |
\t        |
   }| j                  j                  |j                  k7  rt        d|j                   d| j                  j                   d      |
Ft	        |dd      9t	        |dd      }|d	   }|d
   }t        j                  |      }t	        ||      }nt        d       j                  |fd|	i|}d}t        j                  j                  t        j                  j                  |t                     rd}nJ|j#                  dd      }||j#                  dd      }t%        |t         ||j#                  dd      |      }|rt'        |d      rvt)        j                  ||j#                  dd            }|j+                         j,                  j.                  d   }t1        |      |kD  r|j3                  t1        |              | 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.
        revision	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+   r2   r0   get_input_embeddingstrust_remote_code)r6   r   )adapter_nameis_trainableconfig)r   from_pretrainedbase_model_name_or_pathr+   getattr_target_class
ValueErrorr   _target_peft_classr#   	importlibimport_moduleospathexistsjoinr   getr   hasattrr   r5   weightshapelenresize_token_embeddings)clspretrained_model_name_or_pathr7   r8   r9   r+   r&   peft_configbase_model_pathbase_model_revisionr,   target_classexpected_target_classr-   r.   parent_library_namer/   
base_modeltokenizer_existsr0   	tokenizerembedding_sizes                         r'   r:   z"_BaseAutoPeftModel.from_pretrainedC   s    !001NlYalekl%==)22Kd;	(,,L&9+@ m   $DY$O!%%..2G2P2PP 23H3Q3Q2RRlmp  nD  nD  nM  nM  mN[\  7;#M#Y";EL+,>?"./?"@&445HIN">3CDL K  2\11/jL_jcij
 77>>"'',,'DF[\]#JJw-E}

#3T::5.! **[$7  
4J K%55-L_afAgI (<<>EEKKANN9~.223y>B5s%%55)
 &%
 
 	
r)   )defaultFNN)r7   strr8   boolr9   zOptional[PeftConfig]r+   zOptional[str])r#   
__module____qualname__r=   r?   r(   classmethodr:    r)   r'   r   r   7   sb    M
  &"'+"&Q
 Q
 	Q

 %Q
  Q
 Q
r)   r   c                      e Zd ZdZeZy)AutoPeftModelN)r#   r[   r\   r=   r   r?   r^   r)   r'   r`   r`      s    M"r)   r`   c                      e Zd ZeZeZy)AutoPeftModelForCausalLMN)r#   r[   r\   r   r=   r   r?   r^   r)   r'   rb   rb      s    (M-r)   rb   c                      e Zd ZeZeZy)AutoPeftModelForSeq2SeqLMN)r#   r[   r\   r   r=   r   r?   r^   r)   r'   rd   rd      s    )M.r)   rd   c                      e Zd ZeZeZy)&AutoPeftModelForSequenceClassificationN)r#   r[   r\   r	   r=   r   r?   r^   r)   r'   rf   rf      s    6M;r)   rf   c                      e Zd ZeZeZy)#AutoPeftModelForTokenClassificationN)r#   r[   r\   r
   r=   r   r?   r^   r)   r'   rh   rh      s    3M8r)   rh   c                      e Zd ZeZeZy)!AutoPeftModelForQuestionAnsweringN)r#   r[   r\   r   r=   r   r?   r^   r)   r'   rj   rj      s    1M6r)   rj   c                      e Zd ZeZeZy)!AutoPeftModelForFeatureExtractionN)r#   r[   r\   r   r=   r   r?   r^   r)   r'   rl   rl      s    M6r)   rl   )&
__future__r   r@   rB   typingr   transformersr   r   r   r   r	   r
   r   r9   r   
peft_modelr   r   r   r   r   r   r   utils.constantsr   utils.otherr   r   __annotations__r   r`   rb   rd   rf   rh   rj   rl   r^   r)   r'   <module>rt      s    #  	       3 4 2)%017@  "< ^
 ^
B#& #
.1 .
/ 2 /
<-? <
9*< 9
7(: 7
7(: 7r)   