o
    ॵi                     @   s   d dl Z d dlZd dlmZ d dlmZmZ d dlmZ d dl	m
Z
 d dlmZmZmZ dedee d	ee d
efddZdefddZdd Zdd Zded
efddZd
ee fddZdedee d
ee fddZdededee fddZdS )    N)
MethodType)AnyOptional)Tasks)	INDEX_KEY)LazyImportModuleis_torch_availableis_transformers_available	model_dir	task_name
model_typereturnc                 C   sN   |d u s|d u r
dS d||ft jt v rdS tj| d}tj|r%dS dS )NFMODELSTzms_wrapper.py)r   	AST_INDEXr   ospathjoinexists)r
   r   r   ms_wrapper_path r   T/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/utils/automodel_utils.pycan_load_by_ms   s   r   
module_objc                 C   sF   ddl m} t| drdt| jj v r!t|j| | _d S d S d S )Nr   PreTrainedModel_set_gradient_checkpointingvalue)	transformersr   hasattrinspect	signaturer   
parameterskeysr   )r   r   r   r   r   fix_upgrade   s   

r#   c                 O   s   t |  | j|i | d S )N)r#   post_init_origin)selfargskwargsr   r   r   	post_init"   s   r(   c                  C   sn   t  r/t r1dd l} ddlm} ddlm} || j|dkr3t|ds5|j	|_
t	|_	d S d S d S d S d S )Nr   r   )versionz4.35.0r$   )r	   r   r   r   	packagingr)   parse__version__r   r(   r$   )r   r   r)   r   r   r   fix_transformers_upgrade'   s   
r-   automodel_classc                 C   s8   | j }t|| j v rdS t|dr||jv rdS dS )NTauto_mapF)__name__type_model_mappingr"   r   r/   )r.   configautomodel_class_namer   r   r   _can_load_by_hf_automodel3   s   r5   c                    s   dd l m  m} t| dsd S | j dd   D }t|dkr)t||d S t|dkrEtt fdd|D dkrEt||d S d S )Nr   r/   c                 S   s   g | ]	}| d r|qS )	AutoModel)
startswith.0kr   r   r   
<listcomp>A   s    z)get_default_automodel.<locals>.<listcomp>   c                    s   g | ]} | qS r   r   r8   r/   r   r   r;   E   s    )	modelscope.utils.hf_utilutilshf_utilr   r/   r"   lengetattrset)r3   r@   automodel_listr   r=   r   get_default_automodel<   s   
rE   c              
   C   s   ddl m}m}m}m}m}m} tj|tj	|tj
|tj|tj|i}tj| d}	tj|	s0d S z4|j| dd}
|d u rAt|
}n||d }|d u rNW d S t||
rV|W S ||u rbt||
rb|W S W d S  tyn   Y d S w )Nr   )
AutoConfigr6   AutoModelForCausalLMAutoModelForSeq2SeqLMAutoModelForTokenClassification"AutoModelForSequenceClassificationzconfig.jsonT)trust_remote_code)r>   rF   r6   rG   rH   rI   rJ   r   backbonechattext_generationtext_classificationtoken_classificationr   r   r   r   from_pretrainedrE   getr5   	Exception)r
   r   rF   r6   rG   rH   rI   rJ   automodel_mappingconfig_pathr3   r.   r   r   r   get_hf_automodel_classJ   s6    

rV   use_hfc                 K   sH   t | |}|r|d u rtd| dd }|d ur"|j| fi |}|S )Nz&Model import failed. You used `use_hf=z&`, but the model is not a model of hf.)rV   
ValueErrorrQ   )r
   r   rW   r'   r.   modelr   r   r   try_to_load_hf_modeln   s   
rZ   )r   r   typesr   typingr   r   modelscope.metainfor   modelscope.utils.ast_utilsr   modelscope.utils.import_utilsr   r   r	   strboolr   r#   r(   r-   r1   r5   rE   rV   rZ   r   r   r   r   <module>   s2    
	

$