o
    )i9                     @   s.  U d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
mZmZmZmZ d dl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mZ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' d dl(m)Z) d dl*m+Z+ d dl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; d dl<m=Z= d dl>m?Z? e)j@rd dlAmBZB nd dlmBZB dZCe+eDZEdeeF fddZGi de-de.de1de4de7de7de0de3d e2d!e/d"e8d#e5d$e6d%e;d&e:d'e9ZHeIeFeJe f eKd(< d)d*iZLeIeFeFf eKd+< d,d-id.d-id,d-id/ZMeIeFeIeFe
f f eKd0< G d1d2 d2eFe jNZOed3ZP	4	4dd5eg ePf d6eFd7eQd8eQdePf
d9d:ZRedddd;d<eFd=eeF d>eeF d?eeFeSdf deTeF f
d@dAZddddBd<eFdCeFd>eeF d=eeF d?eeFeSdf deSfdDdEZUdFeeFef dGeFd=eeF deSfdHdIZVdJeddfdKdLZWdMeIeFe
f ddfdNdOZXdJedeSfdPdQZYdJedeSfdRdSZZdJedeSfdTdUZ[dJedeSfdVd.Z\dJedeSfdWdXZ]dYeIeFe
f dZeFfd[d\Z^dJedefd]d^Z_	ddFeFd_eFd`eSd=eeF de`eFeFf f
dadbZaddeOjbddfdFeeFef d`eSd=eeF dceeF ddeOdeeeIeFe
f  dfeeegef  defdgdhZc	iddFeeFef dCeFd=eeF dee fdjdkZd	iddCeFdFeeFef d=eeF fdldmZeeddFeFd=eeF fdndoZfdpeFdeeFdf fdqdrZge	iddFeeFef d=eeF fdsdtZhddudvZi		ddFeeFef dweeeSeFf  d=eeF deIeFe
f fdxdyZjdJefdzd{Zk	ddFeFd`eSd=eeF dee fd|d}Zldd~dFeFd=eeF fddZm	ddeeFejnf d`eSd=eeF deeIeFe
f  fddZodeIfddZpdeQfddZqdS )    N)cachepartial)Path)AnyCallableOptionalTypeVarUnion)get_safetensors_metadatahf_hub_downloadlist_repo_files)try_to_load_from_cache)EntryNotFoundErrorHfHubHTTPErrorHFValidationErrorLocalEntryNotFoundErrorRepositoryNotFoundErrorRevisionNotFoundError)GenerationConfigPretrainedConfig)get_image_processor_config)!MODEL_FOR_CAUSAL_LM_MAPPING_NAMES)get_tokenizer_config)CONFIG_NAME)envs)init_logger)ChatGLMConfigDeepseekVLV2ConfigEAGLEConfig
JAISConfigKimiVLConfigMedusaConfigMLPSpeculatorConfigNemotron_Nano_VL_ConfigNemotronConfig
OvisConfigRWConfigSpeculatorsConfigStep3TextConfigStep3VLConfigUltravoxConfig)adapt_config_dict)check_gguf_file)
AutoConfigparams.jsonreturnc                  C   s   t d} | r|  r| S dS )a)  
    Get the HuggingFace token from environment variable.

    Returns None if the token is not set, is an empty string, 
    or contains only whitespace.
    This follows the same pattern as huggingface_hub library which
    treats empty string tokens as None to avoid authentication errors.
    ZHF_TOKENN)osgetenvstrip)token r5   j/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/vllm/transformers_utils/config.py_get_hf_token7   s   
	r7   ZchatglmZdeepseek_vl_v2Zkimi_vlZLlama_Nemotron_Nano_VLZ
RefinedWebZRefinedWebModelZjaisZmlp_speculatorZmedusaeaglespeculatorsZnemotronZovisZultravoxZstep3_vlZ
step3_text_CONFIG_REGISTRYZ
llm_configtext_config_CONFIG_ATTRS_MAPPINGZhas_no_defaults_at_initTis_encoder_decoder)Zinternvl_chatZmllamaZNVLM_D_AUTO_CONFIG_KWARGS_OVERRIDESc                   @   s   e Zd ZdZdZdZdS )ConfigFormatautoZhfZmistralN)__name__
__module____qualname__AUTOHFMISTRALr5   r5   r5   r6   r?   l   s    r?   _R   funclog_msgmax_retriesretry_delayc                 C   s   t |D ]<}z|  W   S  ty@ } z(||d kr"td||  td|||d | t| |d9 }W Y d }~qd }~ww td)N   z%s: %sz%s: %s, retrying %d of %drH   zShould not be reached)range	ExceptionloggererrortimesleepAssertionError)rI   rJ   rK   rL   attempter5   r5   r6   
with_retryu   s   
	rW   revision	repo_typer4   repo_idrY   rZ   r4   c                   s&   dt t f fdd}t|dS )Nr0   c                     s   t    r fdd dD S ztjr*ddlm}  | tdd dW S t	dW S  t
jjy?   g  Y S w )	Nc                    s"   g | ]}|  rt| qS r5   )is_filestrrelative_to).0file
local_pathr5   r6   
<listcomp>   s
    z9list_repo_files.<locals>.lookup_files.<locals>.<listcomp>*r   modelscope_list_repo_filesZMODELSCOPE_API_TOKENrY   r4   rX   )r   existsrglobr   VLLM_USE_MODELSCOPEvllm.transformers_utils.utilsrf   r1   r2   hf_list_repo_fileshuggingface_huberrorsOfflineModeIsEnabledre   r[   rZ   rY   r4   ra   r6   lookup_files   s,   
z%list_repo_files.<locals>.lookup_fileszError retrieving file list)listr]   rW   )r[   rY   rZ   r4   rq   r5   rp   r6   r      s   	
r   rZ   rY   r4   	file_namec                C   s   t | |||d}||v S )Nrs   r   )r[   rt   rZ   rY   r4   	file_listr5   r5   r6   file_exists   s   rv   modelconfig_namec                 C   sN   t |  } r||  S t| ||d}t|trdS tt| ||t dS )Nr[   filenamerY   Trg   )r   rh   r\   r   
isinstancer]   rv   r7   )rw   rx   rY   rb   cached_filepathr5   r5   r6   file_or_path_exists   s   
r}   configc                 C   s@   t | dd}|durt| t | dd}|durt| dS dS )z)Provide backwards compatibility for RoPE.r;   Nrope_scaling)getattrpatch_rope_scalingpatch_rope_scaling_dict)r~   r;   r   r5   r5   r6   r      s   r   r   c                 C   s   d| v rd| v r| d }| d }||krt d| d| dd| vr2d| v r2| d | d< td d| vr:t d| d dkrKd	| d< td
 d S | d dkrbd| v sWJ d| d< td d S d S )N	rope_typetypez#Found conflicts between 'rope_type=z' (modern field) and 'type=z6' (legacy field). You should only specify one of them.z,Replacing legacy 'type' key with 'rope_type'z*rope_scaling should have a 'rope_type' keyZsuZlongropez/Replacing legacy rope_type 'su' with 'longrope'Zmropemrope_sectiondefaultz1Replacing legacy rope_type 'mrope' with 'default')
ValueErrorrP   infowarning)r   r   Zrope_type_legacyr5   r5   r6   r      s,   
r   c                 C   s    t | dd }|d u rdS d|v S )Nr   Fr   r   )r~   r   r5   r5   r6   _uses_mrope   s   r   c                 C   s   t | pt |  pt| S )z1Detect if the model with this config uses M-ROPE.)r   get_text_configthinker_uses_mroper~   r5   r5   r6   
uses_mrope  s
   
r   c                 C   s8   t | dd}|du rdS t |dd}|du rdS t|S )zADetect if the model contains a thinker config and it uses M-ROPE.thinker_configNFr;   )r   r   )r~   r   Zthinker_text_configr5   r5   r6   r     s   r   c                 C   s&   dt dtfdd}|| p||  S )zCDetect if the model with this config is used as an encoder/decoder.r~   r0   c                 S   s   t | ddS )Nr=   Fr   r   r5   r5   r6   _is_encoder_decoder  s   z/is_encoder_decoder.<locals>._is_encoder_decoder)r   boolr   )r~   r   r5   r5   r6   r=     s   
c                 C   s.   |   }t|dd }rddh}||S dS )zR
    Detect if the model with this config is used with interleaved attention.
    layer_typesNfull_attentionsliding_attentionF)r   r   issubset)r~   r;   r   Zinterleaved_typesr5   r5   r6   is_interleaved#  s
   
r   kwargs
model_typec                 C   s   |t v r| t |  | S )zI
    Update kwargs for AutoConfig initialization based on model_type
    )r>   update)r   r   r5   r5   r6    _maybe_update_auto_config_kwargs.  s   r   c                 C   sL   t  D ]\}}t| |r#t| |s| |t| |i td|| q| S )z4Remap config attributes to match the expected names.z&Remapped config attribute '%s' to '%s')r<   itemshasattrr   r   rP   debug)r~   old_attrnew_attrr5   r5   r6   _maybe_remap_hf_config_attrs7  s   

r   	tokenizertrust_remote_codec           
      K   s   t | }|rt| j|d< t| j}nd}tj|du r| n|f||t d|\}}|dd}	|	dur=|	d d  } }| |fS )zS
    If running a speculators config, override running model with target model
    	gguf_fileN)rY   r   r4   speculators_configZverifierZname_or_path)r-   r   nameparentr   get_config_dictr7   get)
rw   r   r   rY   r   is_ggufZgguf_model_repoconfig_dict_Zspec_configr5   r5   r6   ,maybe_override_with_speculators_target_modelB  s$   

r   code_revisionconfig_formathf_overrides_kwhf_overrides_fnc              
   K   s&  t | }|rt| j|d< t| j} |tjkrMz|s!t| t|dr%tj}nt| t	|dr0tj
}ntdW n tyL }	 zdj| d}
t|
|	d }	~	ww |tjkrtj| f||t d|\}}|d}|d u rv|dd urtd	n|}|tv rt| }|j| f||t d|}n5zt||d
}tj| f|||t d|}W n ty }	 z|sdt|	v rd}t||	|	d }	~	ww t|}nf|tj
krt| |}|d }d u rt| |fi |}||d< t|}t|dd  }rt|tr|jt | }|| }dd |D |_!t"t#d |d |_$ndd tD }td| dd%| d|rL|j&t'vr?td|j& dt'|j& }|(d|gi |dd }|d u rdt| d|rdt)d| |}|d url||_*|rzt+,d| |(| |rt+,d| ||}t-| |rt.  |S )Nr   rY   zCould not detect config format for no config file found. Ensure your model has either config.json (HF format) or params.json (Mistral format).a  Invalid repository ID or local directory specified: '{model}'.
Please verify the following requirements:
1. Provide a valid Hugging Face repository ID.
2. Specify a local directory that contains a recognized configuration file.
   - For Hugging Face models: ensure the presence of a 'config.json'.
   - For Mistral models: ensure the presence of a 'params.json'.
3. For GGUF: pass the local path of the GGUF checkpoint.
   Loading GGUF from a remote repo directly is not yet supported.
)rw   )rY   r   r4   r   r   r9   )r   )r   rY   r   r4   z.requires you to execute the configuration filezFailed to load the model config. If the model is a custom model not yet available in the HuggingFace transformers library, consider setting `trust_remote_code=True` in LLM or using the `--trust-remote-code` flag in the CLI.max_position_embeddingssliding_windowc                 S   s   g | ]
}|d u r
dndqS )Nr   r   r5   )r_   Z
layer_typer5   r5   r6   rc     s    zget_config.<locals>.<listcomp>c                 S   s   g | ]
}|t jkr|jqS r5   )r?   rD   value)r_   fmtr5   r5   r6   rc     s    zUnsupported config format: z. Supported formats are: z, ze. Ensure your model uses one of these configuration formats or specify the correct format explicitly.zCan't get gguf config for .Zarchitecturesquantization_configzhf_quant_config.jsonzOverriding HF config with %s)/r-   r   r   r   r?   rD   r}   HF_CONFIG_NAMErE   MISTRAL_CONFIG_NAMErF   r   rO   formatr   r   r7   r   r:   from_pretrainedr   r.   r]   RuntimeErrorr   _download_mistral_config_file_maybe_retrieve_max_pos_from_hfr,   r   r{   rr   Znum_hidden_layerslenr   nextfilterr   joinr   r   r   get_hf_file_to_dictr   rP   r   r   (maybe_register_config_serialize_by_value)rw   r   rY   r   r   r   r   r   r   rV   error_messager   r   r   Zconfig_classr~   err_msgr   r   Zpattern_repeatsr   Zsupported_formatsr   r5   r5   r6   
get_config`  s  














r   mainc                 C   sV   t | | }| r|S zt| ||d}t|trt |W S W d S  ty*   Y d S w )Nry   )r   r\   r   r{   r]   r   )rw   rt   rY   	file_pathr|   r5   r5   r6   try_get_local_file
  s    

r   c                 C   s   t || |d}|du r[z	t|| |d}W nB tjjy    Y dS  ttttfy< } zt	
d| W Y d}~dS d}~w tyV } zt	jd| |d W Y d}~dS d}~ww t|}|dur|| r|t|}t|W  d   S 1 sww   Y  dS )a  
    Downloads a file from the Hugging Face Hub and returns
    its contents as a dictionary.

    Parameters:
    - file_name (str): The name of the file to download.
    - model (str): The name of the model on the Hugging Face Hub.
    - revision (str): The specific version of the model.

    Returns:
    - config_dict (dict): A dictionary containing
    the contents of the downloaded file.
    rw   rt   rY   Nr   z/File or repository not found in hf_hub_downloadzDCannot connect to Hugging Face Hub. Skipping file download for '%s':exc_info)r   r   rm   rn   ro   r   r   r   r   rP   r   r   r   r   r\   openjsonload)rt   rw   rY   r   Zhf_hub_filerV   r`   r5   r5   r6   r     s<   
 r   c           	      C   s   d}d}t | ||drt|| |}|du rdS td tdd |D d}ttdd |D d}|r_d	|d
 }t|| |}tdd | D d}|durUt|}td ||dS dS )a  
    This function gets the pooling and normalize
    config from the model - only applies to
    sentence-transformers models.

    Args:
        model (str): The name of the Hugging Face model.
        revision (str, optional): The specific version
        of the model to use. Defaults to 'main'.

    Returns:
        dict: A dictionary containing the pooling
        type and whether normalization is used.
    zmodules.jsonN)rw   rx   rY   z2Found sentence-transformers modules configuration.c                 s        | ]}|d  dkr|V  qdS )r   z$sentence_transformers.models.PoolingNr5   r_   itemr5   r5   r6   	<genexpr>h      z%get_pooling_config.<locals>.<genexpr>c                 s   r   )r   z&sentence_transformers.models.NormalizeNr5   r   r5   r5   r6   r   l  r   Fz{}/config.jsonpathc                 s   s     | ]\}}|d u r|V  qdS )TNr5   )r_   r   valr5   r5   r6   r   u  s    zFound pooling configuration.)Zpooling_type	normalize)	r}   r   rP   r   r   r   r   r   get_pooling_config_name)	rw   rY   Zmodules_file_nameZmodules_dictZpoolingr   Zpooling_file_nameZpooling_dictpooling_type_namer5   r5   r6   get_pooling_configJ  s:   


r   pooling_namec                 C   sb   d| v r
|  dd} d| v r| dd } d| v rd} g d}|  }||v r)|S td| d	)
NZpooling_mode_ r   r   Z	lasttokenlast)ZLASTALLZCLSZSTEPZMEANzPooling type z not supported)replacesplitupperNotImplementedError)r   Zsupported_pooling_typesr   r5   r5   r6   r     s   
r   c                    s   g d}d |D ]}t | ||ddurt|| |  r nq sOt|  sOz
t| |t d}W n ty;   g }Y nw |D ]}||v rNt|| |  rN nq> sSdS td t	 fdddD re S dS )	a  
    Returns the tokenization configuration dictionary for a
    given Sentence Transformer BERT model.

    Parameters:
    - model (str|Path): The name of the Sentence Transformer
    BERT model.
    - revision (str, optional): The revision of the m
    odel to use. Defaults to 'main'.

    Returns:
    - dict: A dictionary containing the configuration parameters
    for the Sentence Transformer BERT model.
    )zsentence_bert_config.jsonzsentence_roberta_config.jsonzsentence_distilbert_config.jsonzsentence_camembert_config.jsonzsentence_albert_config.jsonz sentence_xlm-roberta_config.jsonzsentence_xlnet_config.jsonNr   rg   z3Found sentence-transformers tokenize configuration.c                 3   s    | ]}| v V  qd S Nr5   )r_   kZencoder_dictr5   r6   r     s    z<get_sentence_transformer_tokenizer_config.<locals>.<genexpr>)Zmax_seq_lengthZdo_lower_case)
r   r   r   is_absoluter   r7   rO   rP   r   all)rw   rY   Z!sentence_transformer_config_filesconfig_fileZ
repo_filesrx   r5   r   r6   )get_sentence_transformer_tokenizer_config  sH   	

r   c               
      s   zddl } d}W n ty   d}Y nw zAddl}ddlddl ddlm} d|f fdd}|j|| |rP 	|  dd	l
m} |rS|j	|  W dS W dS W dS  tyo } ztjd
|d W Y d}~dS d}~ww )a  Try to register HF model configuration class to serialize by value

        If trust_remote_code is set, and the model's config file specifies an
        `AutoConfig` class, then the config class is typically an instance of
        a custom class imported from the HF modules cache.

        Examples:

        >>> from transformers import AutoConfig
        >>> klass = AutoConfig.from_pretrained('meta-llama/Meta-Llama-3-8B', trust_remote_code=True)
        >>> klass.__class__ # transformers.models.llama.configuration_llama.LlamaConfig
        >>> import transformers_modules # error, not initialized
        >>> klass = AutoConfig.from_pretrained('deepseek-ai/DeepSeek-V2.5', trust_remote_code=True)
        >>> import transformers_modules # success, initialized
        >>> klass.__class__ # transformers_modules.deepseek-ai.DeepSeek-V2.5.98b11844770b2c3ffc18b175c758a803640f4e77.configuration_deepseek.DeepseekV2Config

        In the DeepSeek example, the config class is an instance of a custom
        class that is not serializable by default. This class will not be
        importable in spawned workers, and won't exist at all on
        other nodes, which breaks serialization of the config.

        In this function we tell the cloudpickle serialization library to pass
        instances of these generated classes by value instead of by reference,
        i.e. the class definition is serialized along with its data so that the
        class module does not need to be importable on the receiving end.

        See: https://github.com/cloudpipe/cloudpickle?tab=readme-ov-file#overriding-pickles-serialization-mechanism-for-importable-constructs
    r   NTF)
VllmConfigr~   c                    s   j  | ffS r   )loadsdumpsr   cloudpicklepickler5   r6   _reduce_config  s   z@maybe_register_config_serialize_by_value.<locals>._reduce_config)rayzUnable to register remote classes used by trust_remote_code with by-value serialization. This may lead to a later error. If remote code is not needed remove `--trust-remote-code`r   )transformers_modulesImportErrormultiprocessingr   r   Zvllm.configr   ZreducerregisterZregister_pickle_by_valueZvllm.executor.ray_utilsr   rO   rP   r   )r   Ztransformers_modules_availabler   r   r   r   rV   r5   r   r6   r     s6   
r   hf_tokenc                 K   s4   t jrt S t| rt| j} t| f||d|S )N)r4   rY   )r   rj   dictr-   r   r   r   )rw   r   rY   r   r5   r5   r6   get_hf_image_processor_config  s   
r   c                 C   s"   |   }|| urt|dsJ |S )zaGet the "sub" config relevant to llm for multi modal models.
    No op for pure text models.
    Znum_attention_heads)r   r   )r~   r;   r5   r5   r6   get_hf_text_config*  s   r   c                 C   sV   zt j| |dW S  ty*   zt| ||d}t |W  Y S  ty)   Y Y d S w w )Nr   r   rY   )r   r   OSErrorr   Zfrom_model_config)rw   r   rY   r~   r5   r5   r6   try_get_generation_config9  s"   r   r   c                C   s4   t t| |t d}zt|dW S  ty   Y d S w )Nrg   zError retrieving safetensors)r   r
   r7   rW   rO   )rw   rY   Z get_safetensors_metadata_partialr5   r5   r6   try_get_safetensors_metadataO  s   r   pretrained_model_name_or_pathc                 C   s&   zt | ||dW S  ty   Y d S w )Nr   )r   rO   )r   r   rY   r5   r5   r6   try_get_tokenizer_configb  s   r  c                 C   s@   d}t || |}|d u rtd| d|  dt|tsJ |S )Nr/   zFailed to load mistral 'z' config for model zT. Please check if the model is a mistral-format model and if the config file exists.)r   r   r{   r   )rw   rY   Zconfig_file_namer   r5   r5   r6   r   q  s   r   c              
   K   sv   d}z| dd}t| ||tjd}| j }r|}W |S W |S  ty: } ztjd|d W Y d }~|S d }~ww )Ni  r   F)rw   r   rY   r   z|The params.json file is missing 'max_position_embeddings' and could not get a value from the HF config. Defaulting to 128000r   )	r   r   r?   rE   r   r   rO   rP   r   )rw   rY   r   r   Ztrust_remote_code_valZ	hf_configZhf_valuerV   r5   r5   r6   r   }  s*   	r   )rH   rH   r   )r   )r0   N)NN)renumr   r1   rR   	functoolsr   r   pathlibr   typingr   r   r   r   r	   rm   r
   r   r   rl   r   Zhuggingface_hub.utilsr   r   r   r   r   r   Ztransformersr   r   Z.transformers.models.auto.image_processing_autor   Z&transformers.models.auto.modeling_autor   Z*transformers.models.auto.tokenization_autor   Ztransformers.utilsr   r   Zvllmr   Zvllm.loggerr   Zvllm.transformers_utils.configsr   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   Z'vllm.transformers_utils.configs.mistralr,   rk   r-   rj   Z
modelscoper.   r   rA   rP   r]   r7   r:   r   r   __annotations__r<   r>   Enumr?   rG   intrW   r   rr   rv   r}   r   r   r   r   r   r=   r   r   r   tupler   rD   r   r   r   r   r   r   r   r   r   r   r   PathLiker  r   r   r5   r5   r5   r6   <module>   s  
 D
	


*


	

!


 -


.5
>G





