o
    i)M                  
   @   s0  U 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	 d dl
mZ d dlmZmZmZ d dlZd dlZd dlZd dlZd dl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"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z( d dl)m*Z* d dl+m,Z, ee-Z.dZ/dZ0dZ1e$de'de!diZ2e3e4e  e5f e6d< e7e8B e9B ej:B Z;ee6d< dd Z<dede=e5e5f dB fddZ>dedefddZ?d ed!ed"eee5 egef defd#d$Z@G d%d& d&ZAG d'd( d(ZBG d)d* d*ZCded+e5e7B eB d,e=ed-f d.e3e5ef def
d/d0ZDG d1d2 d2ZEdS )3    N)CallableSequence)partial)isclass)FunctionType)Any	TypeAliasget_type_hints)msgpack)GetCoreSchemaHandler)core_schema)envs)init_logger)	BaseMultiModalFieldMultiModalBatchedFieldMultiModalFieldConfigMultiModalFieldElemMultiModalFlatFieldMultiModalKwargsItemMultiModalKwargsItemsMultiModalSharedFieldNestedTensors)is_pin_memory_available)tensor_data         flatsharedbatchedMMF_CLASS_TO_FACTORYbytestrc                   C   s   t d d S )NzWAllowing insecure serialization using pickle due to VLLM_ALLOW_INSECURE_SERIALIZATION=1)loggerwarning_once r$   r$   J/home/ubuntu/vllm_env/lib/python3.10/site-packages/vllm/v1/serial_utils.py#_log_insecure_serialization_warning8   s   r&   valreturnc                 C   s    | d u rd S t | }|j|jfS N)type
__module____qualname__)r'   tr$   r$   r%   _typestr?   s   r.   objc                 C   sL   | du rdS t | tu rdd | D S t | tu r"dd |  D S t| S )zMRecursively encode type information for nested structures of
    lists/dicts.Nc                 S   s   g | ]}t |qS r$   _encode_type_info_recursive.0itemr$   r$   r%   
<listcomp>L       z/_encode_type_info_recursive.<locals>.<listcomp>c                 S   s   i | ]	\}}|t |qS r$   r0   )r3   kvr$   r$   r%   
<dictcomp>N   s    z/_encode_type_info_recursive.<locals>.<dictcomp>)r*   listdictitemsr.   )r/   r$   r$   r%   r1   F   s   r1   	type_infodata
convert_fnc                    s   du rS t trt tsJ  fddD S t trBtdks/t d tsBt ts6J  fddtD S  S )zMRecursively decode type information for nested structures of
    lists/dicts.Nc                    s"   i | ]}|t | |  qS r$   _decode_type_info_recursive)r3   r7   r?   r>   r=   r$   r%   r9   [   s    z/_decode_type_info_recursive.<locals>.<dictcomp>r   r   c                    s   g | ]
\}}t || qS r$   r@   )r3   tid)r?   r$   r%   r5   d   s    
z/_decode_type_info_recursive.<locals>.<listcomp>)
isinstancer;   r:   lenstrzip)r=   r>   r?   r$   rB   r%   rA   R   s   



rA   c                   @   s    e Zd ZdZddefddZdS )UtilityResultz<Wrapper for special handling when serializing/deserializing.Nrc                 C   s
   || _ d S r)   )result)selfrJ   r$   r$   r%   __init__n   s   
zUtilityResult.__init__r)   )__name__r+   r,   __doc__r   rM   r$   r$   r$   r%   rI   k   s    rI   c                   @   s(  e Zd ZdZd#dedB fddZdedee fdd	Z	ded
e
dee fddZdedefddZdejdeeeedf eeB f fddZdejdeeeedf eeB f fddZdedeeef fddZdedeeef fddZdedeeef fddZdedefddZd efd!d"Z dS )$MsgpackEncodera\  Encoder with custom torch tensor and numpy array serialization.

    Note that unlike vanilla `msgspec` Encoders, this interface is generally
    not thread-safe when encoding tensors / numpy arrays.

    By default, arrays below 256B are serialized inline Larger will get sent
    via dedicated messages. Note that this is a per-tensor limit.
    Nsize_thresholdc                 C   s>   |d u rt j}tj| jd| _d | _|| _t jrt	  d S d S )N)enc_hook)
r    VLLM_MSGPACK_ZERO_COPY_THRESHOLDr
   EncoderrR   encoderaux_buffersrQ   !VLLM_ALLOW_INSECURE_SERIALIZATIONr&   )rL   rQ   r$   r$   r%   rM   |   s   
zMsgpackEncoder.__init__r/   r(   c                 C   s2   zdg | _ }| j||d< |W d | _ S d | _ w )N    r   )rV   rU   encode)rL   r/   bufsr$   r$   r%   rY      s
   zMsgpackEncoder.encodebufc                 C   s2   z|g| _ | j }| j|| |W d | _ S d | _ w r)   )rV   rU   encode_into)rL   r/   r[   rZ   r$   r$   r%   r\      s   zMsgpackEncoder.encode_intoc                 C   s   t |tjr| |S t |tjr|jjdvr| |S t |t	r0t
dd |j|j|jfD S t |tr:| |S t |trD| |S t |trY|j}tjsSd |fS t||fS tjsftdt| dt |trtttt|S ttt j|t j!dS )N)OVc                 s   s$    | ]}|d urt |nd V  qd S r)   )int)r3   r8   r$   r$   r%   	<genexpr>   s
    
z*MsgpackEncoder.enc_hook.<locals>.<genexpr>zObject of type zl is not serializableSet VLLM_ALLOW_INSECURE_SERIALIZATION=1 to allow fallback to pickle-based serialization.)protocol)"rE   torchTensor_encode_tensornpndarraydtypekind_encode_ndarrayslicetuplestartstopstepr   _encode_mm_itemr   _encode_mm_itemsrI   rK   r   rW   r1   	TypeErrorr*   r   r
   ExtCUSTOM_TYPE_CLOUDPICKLEcloudpickledumpsCUSTOM_TYPE_PICKLEpickleHIGHEST_PROTOCOL)rL   r/   rK   r$   r$   r%   rR      s4   








zMsgpackEncoder.enc_hook.c                 C   sj   | j d usJ |jjr|jn| }|jr|j| jk r"t	t
|}nt| j }| j | |jj|j|fS r)   )rV   flagsc_contiguousr>   tobytesshapenbytesrQ   r
   rr   CUSTOM_TYPE_RAW_VIEWrF   appendrg   rG   )rL   r/   arr_datar>   r$   r$   r%   ri      s   
zMsgpackEncoder._encode_ndarrayc                 C   sb   | j d usJ t|}|j| jk rtt|}nt| j }| j | t	|j
d}||j|fS )Nztorch.)rV   r   r}   rQ   r
   rr   r~   rF   r   rG   rg   removeprefixr|   )rL   r/   r   r>   rg   r$   r$   r%   rd      s   
zMsgpackEncoder._encode_tensorr<   c                        fdd|  D S )Nc                    $   i | ]\}}| fd d|D qS )c                       g | ]}  |qS r$   )ro   r2   rL   r$   r%   r5          z>MsgpackEncoder._encode_mm_items.<locals>.<dictcomp>.<listcomp>r$   r3   modalityitemlistr   r$   r%   r9          z3MsgpackEncoder._encode_mm_items.<locals>.<dictcomp>r<   )rL   r<   r$   r   r%   rp      s   
zMsgpackEncoder._encode_mm_itemsr4   c                    r   )Nc                       i | ]
\}}|  |qS r$   )_encode_mm_field_elemr3   keyelemr   r$   r%   r9          z2MsgpackEncoder._encode_mm_item.<locals>.<dictcomp>r   )rL   r4   r$   r   r%   ro      s   zMsgpackEncoder._encode_mm_itemr   c                 C   s(   |j d u rd n| |j | |jdS )N)r>   field)r>   _encode_nested_tensors_encode_mm_fieldr   )rL   r   r$   r$   r%   r      s   
z$MsgpackEncoder._encode_mm_field_elemntc                    s:   t |tjr |S t |ttfr|S  fdd|D S )Nc                    r   r$   )r   r3   xr   r$   r%   r5     r   z9MsgpackEncoder._encode_nested_tensors.<locals>.<listcomp>)rE   rb   rc   rd   r_   float)rL   r   r$   r   r%   r      s
   
z%MsgpackEncoder._encode_nested_tensorsr   c                    s@   t  j}|std j  fddt D }||fS )NzUnsupported field type: c                    s   i | ]
}|j t |j qS r$   )namegetattrr3   fr   r$   r%   r9     r   z3MsgpackEncoder._encode_mm_field.<locals>.<dictcomp>)r    get	__class__rq   dataclassesfields)rL   r   r   
factory_kwr$   r   r%   r   	  s
   zMsgpackEncoder._encode_mm_fieldr)   )!rN   r+   r,   rO   r_   rM   r   r   r!   rY   	bytearrayr\   rR   re   rf   rk   rG   
memoryviewri   rb   rc   rd   r   r;   rp   r   ro   r   r   r   r   r   r   r$   r$   r$   r%   rP   r   s*    		.

	rP   c                   @   s4  e Zd ZdZd(dedB defddZdeee B d	efd
dZ	de
ded	efddZded	efddZdee ded	efddZded	ejfddZded	ejfddZdeeef d	efddZdeeef d	efddZdeeef d	efddZded	efd d!Zded	efd"d#Zd$e d%e!d	efd&d'Z"dS ))MsgpackDecoderzDecoder with custom torch tensor and numpy array serialization.

    Note that unlike vanilla `msgspec` Decoders, this interface is generally
    not thread-safe when encoding tensors / numpy arrays.
    NTr-   	share_memc                 C   sR   || _ t | _|d u rdn|f}tj|| j| jd| _d| _t	j
r't  d S d S )Nr$   )ext_hookdec_hook)r   r   pin_tensorsr
   Decoderr   r   decoderrV   r   rW   r&   )rL   r-   r   argsr$   r$   r%   rM     s   


zMsgpackDecoder.__init__rZ   r(   c                 C   s>   t |tr| j|S || _z| j|d W d| _S d| _w )Nr   r$   )rE   r!   r   decoderV   )rL   rZ   r$   r$   r%   r   '  s   
zMsgpackDecoder.decoder/   c                 C   s   t |r?t|tjr| |S t|tjr| |S |tu r"t| S t|t	r,| 
|S t|tr6| |S |tu r?| |S |S r)   )r   
issubclassre   rf   _decode_ndarrayrb   rc   _decode_tensorrj   r   _decode_mm_itemr   _decode_mm_itemsrI   _decode_utility_result)rL   r-   r/   r$   r$   r%   r   1  s   






zMsgpackDecoder.dec_hookc                 C   s4   |\}}|d urt jstdt||| j}t|S )NzPVLLM_ALLOW_INSECURE_SERIALIZATION must be set to use custom utility result types)r   rW   rq   rA   _convert_resultrI   )rL   r/   result_typerK   r$   r$   r%   r   B  s   z%MsgpackDecoder._decode_utility_resultr   rK   c                 C   s:   |d u r|S |\}}t |}t||}tj||| jdS )N)r   )	importlibimport_moduler   msgspecconvertr   )rL   r   rK   mod_namer   modr$   r$   r%   r   P  s   

zMsgpackDecoder._convert_resultarrc                 C   sH   |\}}}t |tr| j| n|}tj||d}| js| }||S )Nrg   )rE   r_   rV   re   
frombufferr   copyreshape)rL   r   rg   r|   r>   bufferr$   r$   r%   r   X  s   

zMsgpackDecoder._decode_ndarrayc                 C   s   |\}}}t |t}|r| j| n|}t |tr|nt|}tt|}t |tjs+J |js;d|v s4J tj||dS tj	|tj
d}|sJ| }n| jsX| jrT| n| }|||S )Nr   r   )rE   r_   rV   r   r   rb   rg   r}   emptyr   uint8cloner   r   
pin_memoryview)rL   r   rg   r|   r>   is_auxr   torch_dtyper$   r$   r%   r   b  s   



zMsgpackDecoder._decode_tensorc                       t  fdd| D S )Nc                    r   )c                    r   r$   )r   r2   r   r$   r%   r5   {  r   z>MsgpackDecoder._decode_mm_items.<locals>.<dictcomp>.<listcomp>r$   r   r   r$   r%   r9   z  r   z3MsgpackDecoder._decode_mm_items.<locals>.<dictcomp>)r   r<   rL   r/   r$   r   r%   r   x  s
   
zMsgpackDecoder._decode_mm_itemsc                    r   )Nc                    r   r$   )_decode_mm_field_elemr   r   r$   r%   r9     r   z2MsgpackDecoder._decode_mm_item.<locals>.<dictcomp>)r   r<   r   r$   r   r%   r     s   zMsgpackDecoder._decode_mm_itemc                 C   sp   |d d ur|  |d |d< |d \}}tt|}|dkr'| |d |d< |di |j|d< tdi |S )Nr>   r   r   slices )r   r$   )_decode_nested_tensorsr   r   _decode_nested_slicesr   r   )rL   r/   factory_meth_namer   factory_methr$   r$   r%   r     s   
z$MsgpackDecoder._decode_mm_field_elemc                    s\   t |ttfr	|S t |tstdt| |r%t |d tr% |S  fdd|D S )Nz#Unexpected NestedTensors contents: r   c                    r   r$   )r   r   r   r$   r%   r5     r   z9MsgpackDecoder._decode_nested_tensors.<locals>.<listcomp>)rE   r_   r   r:   rq   r*   rG   r   r   r$   r   r%   r     s   

z%MsgpackDecoder._decode_nested_tensorsc                    sB   t |ttfs	J |rt |d ttfst| S  fdd|D S )Nr   c                    r   r$   )r   r   r   r$   r%   r5     r   z8MsgpackDecoder._decode_nested_slices.<locals>.<listcomp>)rE   r:   rk   rj   r   r$   r   r%   r     s   z$MsgpackDecoder._decode_nested_slicescoder>   c                 C   sF   |t kr|S tjr|tkrt|S |tkrt|S td| d)NzExtension type code z is not supported)	r~   r   rW   rv   rw   loadsrs   rt   NotImplementedError)rL   r   r>   r$   r$   r%   r     s   

zMsgpackDecoder.ext_hook)NT)#rN   r+   r,   rO   r   boolrM   r!   r   r   r*   r   rI   r   rG   r   re   rf   r   rb   rc   r   r;   r   r   r   r   r   r   r   r   r   r_   r   r   r$   r$   r$   r%   r     s    

r   methodr   .kwargsc                 C   sn   t |trtt|| }n"t |tr+zt| |}W n ty*   td|ddw t|| }||i |S )a3  
    Run a method of an object with the given arguments and keyword arguments.
    If the method is string, it will be converted to a method using getattr.
    If the method is serialized bytes and will be deserialized using
    cloudpickle.
    If the method is a callable, it will be called directly.
    zMethod z is not implemented.N)	rE   bytesr   rt   r   rG   r   AttributeErrorr   )r/   r   r   r   funcr$   r$   r%   
run_method  s   



r   c                   @   s>   e Zd ZedededejfddZededefddZ	d	S )
PydanticMsgspecMixinsource_typehandlerr(   c                 C   s   dd t j|D }t|}i }| D ]?\}}|| }||}	|jt jur7tj|	|jd}
t	|
||< q|j
t jurMtj|	|j
d}
t	|
||< qt	|	||< qt| jt|S )a  
        Make msgspec.Struct compatible with Pydantic, respecting defaults.
        Handle JSON=>msgspec.Struct. Used when exposing msgspec.Struct to the
        API as input or in `/docs`. Note this is cached by Pydantic and not
        called on every validation.
        c                 S   s   i | ]}|j |qS r$   )r   r   r$   r$   r%   r9     r6   zEPydanticMsgspecMixin.__get_pydantic_core_schema__.<locals>.<dictcomp>)schemadefault_factory)r   default)r   structsr   r	   r<   r   	NODEFAULTr   with_default_schematyped_dict_fieldr    no_info_after_validator_function_validate_msgspectyped_dict_schema)clsr   r   msgspec_fields
type_hintsr   r   hintmsgspec_fieldfield_schemawrapped_schemar$   r$   r%   __get_pydantic_core_schema__  s.   
z1PydanticMsgspecMixin.__get_pydantic_core_schema__valuec                 C   s4   t || r|S t |tr| di |S tj|| dS )z6Validate and convert input to msgspec.Struct instance.)r*   Nr$   )rE   r;   r   r   )r   r   r$   r$   r%   r     s
   

z&PydanticMsgspecMixin._validate_msgspecN)
rN   r+   r,   classmethodr   r   r   
CoreSchemar   r   r$   r$   r$   r%   r     s    )r   )Fr   r   rw   collections.abcr   r   	functoolsr   inspectr   typesr   typingr   r   r	   rt   r   numpyre   rb   zmqr
   pydanticr   pydantic_corer   vllmr   vllm.loggerr   vllm.multimodal.inputsr   r   r   r   r   r   r   r   r   vllm.utils.platform_utilsr   vllm.v1.utilsr   rN   r"   rv   rs   r~   r    r;   r*   rG   __annotations__r   r   r   Framer!   r&   rk   r.   r1   rA   rI   rP   r   r   r   r$   r$   r$   r%   <module>   sv   
,
 $ 



