o
    )iK                     @   sf  d dl 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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 ergd dlmZ d dlmZmZmZ d dl m!Z! d dl"m#Z# neZeZeZeZeZ!eZ#edZ$edeedZ%edeedZ&ee'Z(eddG dd dZ)eddG dd de)Z*G dd deZ+G dd dZ,dS )    )Mapping)	dataclass)TYPE_CHECKINGAny
NamedTupleOptionalUnionN)BatchFeaturePretrainedConfigProcessorMixin)TypeVar)init_logger)cached_processor_from_config) get_allowed_kwarg_only_overrides)JSONTreejson_map_leaves)ModelConfig)MultiModalDataDictMultiModalPlaceholderDictMultiModalRegistrySequenceData)AnyTokenizer_T_C)bounddefault_PT)frozenc                   @   s   e Zd ZU dZeed< 	 efdeee	 e
ee	 df f de	fddZdeeef fdd	Zd
d Zefdeee e
ee df f dedefddZdee dedefddZdS )InputContextzZ
    Contains information about the model which may be used to
    modify the inputs.
    model_configtyp.returnc                C   s.   | j j}t||std| dt| |S )z
        Get the HuggingFace configuration
        (`transformers.PretrainedConfig`) of the model,
        additionally checking its type.

        Raises:
            TypeError: If the configuration is not of the specified type.
        z3Invalid type of HuggingFace config. Expected type: z, but found type: )r    	hf_config
isinstance	TypeErrortype)selfr!   r#    r(   `/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/vllm/inputs/registry.pyget_hf_config/   s   
zInputContext.get_hf_configc                 C   s   | j jS )zQ
        Get the HuggingFace image processor configuration of the model.
        )r    Zhf_image_processor_configr'   r(   r(   r)   get_hf_image_processor_configD   s   z*InputContext.get_hf_image_processor_configc                 C   s   | j j}|du rtd|S )z
        Get the multimodal config of the model.

        Raises:
            RuntimeError: If the model is not a multimodal model.
        NzNot a multimodal model)r    Zmultimodal_configRuntimeError)r'   	mm_configr(   r(   r)   get_mm_configJ   s   zInputContext.get_mm_configkwargsc                K   s   t | jfd|i|S )z
        Get the HuggingFace processor
        (`transformers.ProcessorMixin`) of the model,
        additionally checking its type.

        Raises:
            TypeError: If the processor is not of the specified type.
        Zprocessor_cls)r   r    r'   r!   r0   r(   r(   r)   get_hf_processorW   s   zInputContext.get_hf_processorc                K   s6   | j  }|j}|du ri }i ||}|di |S )z
        Initialize a HuggingFace-like processor class, merging the
        keyword arguments with those in the model's configuration.
        Nr(   )r    get_multimodal_configZmm_processor_kwargs)r'   r!   r0   r.   Zbase_kwargsmerged_kwargsr(   r(   r)   init_processork   s   

zInputContext.init_processorN)__name__
__module____qualname____doc__r   __annotations__r
   r   r&   r   tupler*   dictstrr   r,   r/   r   r   objectr2   r   r5   r(   r(   r(   r)   r   %   s6   
 

r   c                       s   e Zd ZU eed< 	 efdeee e	ee df f de
def fddZi fded	eee
f deee
f deeef fd
dZ  ZS )InputProcessingContext	tokenizerr!   .r0   r"   c                   s   t  j|fd| ji|S )Nr@   )superr2   r@   r1   	__class__r(   r)   r2      s   z'InputProcessingContext.get_hf_processorhf_processordatac              
      s   t |sJ  j }||}t||ddd} fdd}z0|di ||ddi}t|tr<t||j}	t|	W S t||}	t	
t|j d |	W S  tym }
 zd	t|j d
| d| }t||
d}
~
ww )z~
        Call `hf_processor` on the prompt `data`
        (text, image, audio...) with configurable options `kwargs`.
        FT)Zrequires_kw_onlyZallow_var_kwargsc                    s(   t | tjr|  r| j jjdS | S )N)dtype)r$   torchZTensorZis_floating_pointtor    rF   )xr+   r(   r)   maybe_cast_dtype   s   zBInputProcessingContext.call_hf_processor.<locals>.maybe_cast_dtypeZreturn_tensorsptzy did not return `BatchFeature`. Make sure to match the behaviour of `ProcessorMixin` when implementing custom processors.zFailed to apply z	 on data=z with kwargs=Nr(   )callabler    r3   Zmerge_mm_processor_kwargsr   r$   r	   r   rE   loggerZwarning_oncer&   r6   	Exception
ValueError)r'   rD   rE   r0   r.   r4   Zallowed_kwargsrJ   outputZcast_outputexcmsgr(   r+   r)   call_hf_processor   sB   







z(InputProcessingContext.call_hf_processor)r6   r7   r8   r   r:   r   r   r&   r   r;   r>   r2   r   r=   r	   r   rS   __classcell__r(   r(   rB   r)   r?      s*   
 


r?   c                   @   s:   e Zd ZU dZeed< dZee ed< dZ	ee
 ed< dS )	DummyDatazL
    Dummy data used for profiling.

    Note: This is only used in V0.
    seq_dataNmulti_modal_datamulti_modal_placeholders)r6   r7   r8   r9   r   r:   rW   r   r   rX   r   r(   r(   r(   r)   rU      s
   
 rU   c                   @   s2   e Zd ZdZ	ddededededef
dd	Z	d
S )InputRegistryz(
    Note: This is only used in V0.
    Fr    seq_lenmm_registryis_encoder_datar"   c           	      C   sz   ddl m} |js|d|f}t|dS |r(|||}||j}t|dS |||}t||j|j	
 |jdS )z
        Create dummy data for profiling the memory usage of a model.

        The model is identified by ``model_config``.
        r   r   )rV   )rV   rW   rX   )vllm.sequencer   Zis_multimodal_modelZfrom_prompt_token_countsrU   Zget_encoder_dummy_dataZ	from_seqsZprompt_token_idsZget_decoder_dummy_datarW   get_datarX   )	r'   r    rZ   r[   r\   r   rV   Zenc_dataZdec_datar(   r(   r)   dummy_data_for_profiling   s    


z&InputRegistry.dummy_data_for_profilingN)F)
r6   r7   r8   r9   r   intr   boolrU   r_   r(   r(   r(   r)   rY      s    	rY   )-collections.abcr   dataclassesr   typingr   r   r   r   r   rG   Ztransformersr	   r
   r   Ztyping_extensionsr   Zvllm.loggerr   Z!vllm.transformers_utils.processorr   Z
vllm.utilsr   Zvllm.utils.jsontreer   r   Zvllm.configr   Zvllm.multimodalr   r   r   r]   r   Z!vllm.transformers_utils.tokenizerr   r   r   r   r6   rM   r   r?   rU   rY   r(   r(   r(   r)   <module>   s>   YD