o
    i:                     @   s   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	 d dl
mZmZ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 d dlmZ er`d dlm Z  edeedZ!edeedZ"G dd de#Z$G dd de%Z&de'e(e'df B fddZ)ede'de*e+ fddZ,ddde'e(e'df B fdd Z-dd!ed"d#e+d$ed%e+dB d&e.de'e! e(e'e! df B d'ede!fd(d)Z/ee/Z0ed*e!de*e+ fd+d,Z1dd!ed"d#e+d$ed%e+dB d&e.de'e! e(e'e! df B d'ede!fd-d.Z2efddde'e! e(e'e! df B d'ede!fd/d0Z3dd!d1d#e+d$ed%e+dB d&e.d'ef
d2d3Z4ee4Z5ddd'efd4d5Z6dd!d1d#e+d$ed%e+dB d&e.d'ef
d6d7Z7ee7Z8ddd'efd8d9Z9dd!dd:d#e+d$ed%e+dB d&e.d;e'e" dB d'efd<d=Z:ee:Z;	d@ddde'e" dB d'efd>d?Z<dS )A    N)	lru_cache)TYPE_CHECKINGAnycastget_argsget_type_hints)AutoFeatureExtractorAutoImageProcessorAutoProcessorAutoVideoProcessor)FeatureExtractionMixin)BaseImageProcessor)ProcessorMixin)BaseVideoProcessor)TypeVar)is_gguf)convert_model_repo_to_path) get_allowed_kwarg_only_overrides)ModelConfig_P)bounddefault_Vc                   @      e Zd ZdZdefddZdS )HashableDictz7
    A dictionary that can be hashed by lru_cache.
    returnc                 C   s   t t|  S N)hash	frozensetitemsself r"   W/home/ubuntu/vllm_env/lib/python3.10/site-packages/vllm/transformers_utils/processor.py__hash__'   s   zHashableDict.__hash__N__name__
__module____qualname____doc__intr$   r"   r"   r"   r#   r       s    r   c                   @   r   )HashableListz1
    A list that can be hashed by lru_cache.
    r   c                 C   s   t t| S r   )r   tupler    r"   r"   r#   r$   0   s   zHashableList.__hash__Nr%   r"   r"   r"   r#   r+   +   s    r+   processor_cls.c                 C   s,   t | ts	| tkrtjS t| dr| jS | S )Nfrom_pretrained)
isinstancer,   r   r
   r.   hasattr)r-   r"   r"   r#   _get_processor_factory_fn4   s
   
r1   
kwargs_clsr   c                 C   s\   t  }| d u r	|S t| }dD ]}||v r%t|| }|D ]}|| qq|h dO }|S )N)text_kwargsimages_kwargsvideos_kwargsaudio_kwargs>   r3   r6   r4   r5   )setr   add)r2   dynamic_kwargskwargs_type_annotationskw_typekw_annotationskw_namer"   r"   r#   ,_collect_dynamic_keys_from_processing_kwargs=   s   r>   model_configr   c          	      K   sl   |   }||}t|}t||ddd}| D ]\}}t|tr(t|||< t|tr3t	|||< q|S )NFT)requires_kw_onlyallow_var_kwargs)
get_multimodal_configmerge_mm_processor_kwargsr1   r   r   r/   dictr   listr+   )	r?   r-   kwargs	mm_configmerged_kwargsfactoryallowed_kwargskeyvaluer"   r"   r#   _merge_mm_kwargsN   s    
	

rM   Frevisiontrust_remote_coder-   processor_nameargsrO   rP   rF   c          	   
   O   s   |du rd}z;t | } t|ts|tkr$tj| g|R ||d|}nt|tr9|j| g|R ||d|}n||i |}W n tyX } z|sRd}t|||d}~ww t||sjt	d| dt
| |S )z:Load a processor for the given model name via HuggingFace.NmainrO   rP   zFailed to load the processor. If the processor is a custom processor 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.z6Invalid type of HuggingFace processor. Expected type: z, but found type: )r   r/   r,   r   r
   r.   
issubclass
ValueErrorRuntimeError	TypeErrortype)	rQ   rO   rP   r-   rR   rF   	processoreerr_msgr"   r"   r#   get_processorj   sZ   	
	

r]   rZ   c                 C   s   zIt t| jjd}|r|jnd }|d t jfvr$tt	|d W S t| j
}t|}t }t| D ]\}}|drF|t|B }q7|W S  tyU   t  Y S w )NrF   r   ProcessingKwargs)inspect	signaturerY   __call__
parametersget
annotation_emptyr>   r   r'   	importlibimport_moduler7   varsr   endswith	Exception)rZ   call_kwargscall_kwargs_annotationsmodule_namemodprocessor_kwargsnameobjr"   r"   r#   #get_processor_kwargs_from_processor   s.   




rr   c          	         sJ   t | |||d}t|  fdd| D }t | f|||d|}|S )NrN   c                    s   i | ]\}}| vr||qS r"   r"   ).0kvdynamic_keysr"   r#   
<dictcomp>   s    z?cached_get_processor_without_dynamic_kwargs.<locals>.<dictcomp>)cached_get_processorrr   r   )	rQ   rO   rP   r-   rR   rF   rZ   filtered_kwargsfinal_processorr"   rv   r#   +cached_get_processor_without_dynamic_kwargs   s$   	r|   c                 K   s^   t | jrt | jrJ d| j}| j}n| j}| j}t|f|| j|dt| |fi |S )Nz^For multimodal GGUF models, the original tokenizer should be used to correctly load processor.rN   )r   model	tokenizertokenizer_revisionrO   r|   rP   rM   )r?   r-   rF   r}   rO   r"   r"   r#   cached_processor_from_config   s"   
r   rT   c             
   O   d   zt | } tj| g|R ||d|}W n ty, } z|s&d}t|||d}~ww tt|S )zMLoad an audio feature extractor for the given model name
    via HuggingFace.rT   zFailed to load the feature extractor. If the feature extractor is a custom extractor 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.N)r   r   r.   rV   rW   r   r   )rQ   rO   rP   rR   rF   feature_extractorr[   r\   r"   r"   r#   get_feature_extractor  s*   	


r   c                 K   s*   t | jf| j| jdt| tfi |S )NrT   )cached_get_feature_extractorr}   rO   rP   rM   r   )r?   rF   r"   r"   r#   $cached_feature_extractor_from_config+  s   r   c             
   O   r   )zALoad an image processor for the given model name via HuggingFace.rT   zFailed to load the image processor. If the image processor is a custom processor 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.N)r   r	   r.   rV   rW   r   r   )rQ   rO   rP   rR   rF   rZ   r[   r\   r"   r"   r#   get_image_processor7  s*   


r   c                 K   s\   t | jrt | jrJ d| j}| j}n| j}| j}t|f|| jdt| tfi |S )NzdFor multimodal GGUF models, the original tokenizer should be used to correctly load image processor.rT   )	r   r}   r~   r   rO   cached_get_image_processorrP   rM   r	   )r?   rF   r}   rO   r"   r"   r#   "cached_image_processor_from_config^  s    
r   rO   rP   processor_cls_overridesr   c          
   
   O   sl   zt | } |pt}|j| g|R ||d|}W n ty0 } z|s*d}	t|	||d}~ww tt|S )z@Load a video processor for the given model name via HuggingFace.rT   zFailed to load the video processor. If the video processor is a custom processor 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.N)r   r   r.   rV   rW   r   r   )
rQ   rO   rP   r   rR   rF   r-   rZ   r[   r\   r"   r"   r#   get_video_processort  s,   	


r   c                 K   s,   t | jf| j| j|dt| tfi |S )Nr   )cached_get_video_processorr}   rO   rP   rM   r   )r?   r-   rF   r"   r"   r#   "cached_video_processor_from_config  s   r   r   )=rf   r_   	functoolsr   typingr   r   r   r   r   transformersr   r	   r
   r   %transformers.feature_extraction_utilsr   #transformers.image_processing_utilsr   transformers.processing_utilsr   #transformers.video_processing_utilsr   typing_extensionsr   "vllm.transformers_utils.gguf_utilsr   vllm.transformers_utils.utilsr   vllm.utils.func_utilsr   vllm.configr   r   r   rD   r   rE   r+   rY   r,   r1   r7   strr>   rM   boolr]   ry   rr   r|   r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   <module>   s  		

:$
$

$

$


&
