o
    :/i&W                  
   @   s8  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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/Z0dZ1dZ2dZ3e&de)de#diZ4e5e6e" e7f e8d< e9e:B e;B ej<B Z=ee8d< dd Z>dede?e7e7f dB fddZ@dedefddZAd ed!ed"eee7 egef defd#d$ZBG d%d& d&ZCG d'd( d(ZDG d)d* d*ZEded+e7e9B eB d,e?ed-f d.e5e7ef def
d/d0ZFG d1d2 d2ZGdS )3    N)CallableSequence)partial)isclass)FunctionType)AnyClassVar	TypeAliascast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&   a/lsinfo/ai/hellotax_ai/llm_service/venv_vllm/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   r0   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&   r2   )r5   kvr&   r&   r'   
<dictcomp>N   s    z/_encode_type_info_recursive.<locals>.<dictcomp>)r,   listdictitemsr0   )r1   r&   r&   r'   r3   F   s   r3   	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)r5   r9   rA   r@   r?   r&   r'   r;   [   s    z/_decode_type_info_recursive.<locals>.<dictcomp>r   r   c                    s   g | ]
\}}t || qS r&   rB   )r5   tid)rA   r&   r'   r7   d   s    
z/_decode_type_info_recursive.<locals>.<listcomp>)
isinstancer=   r<   lenstrzip)r?   r@   rA   r&   rD   r'   rC   R   s   



rC   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)selfrL   r&   r&   r'   __init__n   s   
zUtilityResult.__init__r+   )__name__r-   r.   __doc__r   rO   r&   r&   r&   r'   rK   k   s    rK   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   EncoderrT   encoderaux_buffersrS   !VLLM_ALLOW_INSECURE_SERIALIZATIONr(   )rN   rS   r&   r&   r'   rO   |   s   
zMsgpackEncoder.__init__r1   r*   c                 C   s2   zdg | _ }| j||d< |W d | _ S d | _ w )N    r   )rX   rW   encode)rN   r1   bufsr&   r&   r'   r[      s
   zMsgpackEncoder.encodebufc                 C   s2   z|g| _ | j }| j|| |W d | _ S d | _ w r+   )rX   rW   encode_into)rN   r1   r]   r\   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)r5   r:   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)"rG   torchTensor_encode_tensornpndarraydtypekind_encode_ndarrayslicetuplestartstopstepr   _encode_mm_itemr   _encode_mm_itemsrK   rM   r   rY   r3   	TypeErrorr,   r   r   ExtCUSTOM_TYPE_CLOUDPICKLEcloudpickledumpsCUSTOM_TYPE_PICKLEpickleHIGHEST_PROTOCOL)rN   r1   rM   r&   r&   r'   rT      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+   )rX   flagsc_contiguousr@   tobytesshapenbytesrS   r   rt   CUSTOM_TYPE_RAW_VIEWrH   appendri   rI   )rN   r1   arr_datar@   r&   r&   r'   rk      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.)rX   r   r   rS   r   rt   r   rH   r   rI   ri   removeprefixr~   )rN   r1   r   r@   ri   r&   r&   r'   rf      s   
zMsgpackEncoder._encode_tensorr>   c                        fdd|  D S )Nc                    $   i | ]\}}| fd d|D qS )c                       g | ]}  |qS r&   )rq   r4   rN   r&   r'   r7          z>MsgpackEncoder._encode_mm_items.<locals>.<dictcomp>.<listcomp>r&   r5   modalityitemlistr   r&   r'   r;          z3MsgpackEncoder._encode_mm_items.<locals>.<dictcomp>r>   )rN   r>   r&   r   r'   rr      s   
zMsgpackEncoder._encode_mm_itemsr6   c                    r   )Nc                       i | ]
\}}|  |qS r&   )_encode_mm_field_elemr5   keyelemr   r&   r'   r;          z2MsgpackEncoder._encode_mm_item.<locals>.<dictcomp>r   )rN   r6   r&   r   r'   rq      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   )rN   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   r5   xr   r&   r'   r7     r   z9MsgpackEncoder._encode_nested_tensors.<locals>.<listcomp>)rG   rd   re   rf   ra   float)rN   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r5   fr   r&   r'   r;     r   z3MsgpackEncoder._encode_mm_field.<locals>.<dictcomp>)r"   get	__class__rs   dataclassesfields)rN   r   r   
factory_kwr&   r   r'   r   	  s
   zMsgpackEncoder._encode_mm_fieldr+   )!rP   r-   r.   rQ   ra   rO   r   r   r#   r[   	bytearrayr^   rT   rg   rh   rm   rI   
memoryviewrk   rd   re   rf   r   r=   rr   r   rq   r   r   r   r   r   r   r&   r&   r&   r'   rR   r   s*    		.

	rR   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   decoderrX   r   rY   r(   )rN   r/   r   argsr&   r&   r'   rO     s   


zMsgpackDecoder.__init__r\   r*   c                 C   s>   t |tr| j|S || _z| j|d W d| _S d| _w )Nr   r&   )rG   r#   r   decoderX   )rN   r\   r&   r&   r'   r   '  s   
zMsgpackDecoder.decoder1   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   
issubclassrg   rh   _decode_ndarrayrd   re   _decode_tensorrl   r   _decode_mm_itemr   _decode_mm_itemsrK   _decode_utility_result)rN   r/   r1   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   rY   rs   rC   _convert_resultrK   )rN   r1   result_typerM   r&   r&   r'   r   B  s   z%MsgpackDecoder._decode_utility_resultr   rM   c                 C   s:   |d u r|S |\}}t |}t||}tj||| jdS )N)r   )	importlibimport_moduler   msgspecconvertr   )rN   r   rM   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ri   )rG   ra   rX   rg   
frombufferr   copyreshape)rN   r   ri   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   )rG   ra   rX   r   r   rd   ri   r   emptyr   uint8cloner   r   
pin_memoryview)rN   r   ri   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   r4   r   r&   r'   r7   {  r   z>MsgpackDecoder._decode_mm_items.<locals>.<dictcomp>.<listcomp>r&   r   r   r&   r'   r;   z  r   z3MsgpackDecoder._decode_mm_items.<locals>.<dictcomp>)r   r>   rN   r1   r&   r   r'   r   x  s
   
zMsgpackDecoder._decode_mm_itemsc                    r   )Nc                    r   r&   )_decode_mm_field_elemr   r   r&   r'   r;     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   )rN   r1   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'   r7     r   z9MsgpackDecoder._decode_nested_tensors.<locals>.<listcomp>)rG   ra   r   r<   rs   r,   rI   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'   r7     r   z8MsgpackDecoder._decode_nested_slices.<locals>.<listcomp>)rG   r<   rm   rl   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   rY   rx   ry   loadsru   rv   NotImplementedError)rN   r   r@   r&   r&   r'   r     s   

zMsgpackDecoder.ext_hook)NT)#rP   r-   r.   rQ   r   boolrO   r#   r   r   r,   r   rK   r   rI   r   rg   rh   r   rd   re   r   r=   r   r   r   r   r   r   r   r   r   ra   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)	rG   bytesr   rv   r   rI   r   AttributeErrorr   )r1   r   r   r   funcr&   r&   r'   
run_method  s   



r   c                   @   sp   e Zd ZU dZe Zeee  ed< e	de
dedejfddZe	de
de
fd	d
Zede
de
fddZdS )PydanticMsgspecMixina  Make a ``msgspec.Struct`` compatible with Pydantic for both
    **validation** (JSON/dict -> Struct) and **serialization**
    (Struct -> JSON-safe dict).

    Subclasses may set ``__pydantic_msgspec_exclude__`` (a ``set[str]``)
    to list non-underscore field names that should also be stripped from
    serialized output.  Fields whose names start with ``_`` are always
    excluded automatically.
    __pydantic_msgspec_exclude__source_typehandlerr*   c                 C   s  dd t j|D }t|}i }| D ]N\}}||vrq|dr$q|| }||}	|jt jurDtj	|	|jd}
tj
|
dd||< q|jt jur\tj	|	|jd}
tj
|
dd||< qt
|	||< qt| jt|}tj| jdd}tjt||g|d	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'   r;     r8   zEPydanticMsgspecMixin.__get_pydantic_core_schema__.<locals>.<dictcomp>_)schemadefault_factoryF)required)r   default)info_arg)serialization)r   structsr   r   r>   
startswithr   	NODEFAULTr   with_default_schematyped_dict_fieldr    no_info_after_validator_function_validate_msgspectyped_dict_schema$plain_serializer_function_ser_schema_serialize_msgspecunion_schemais_instance_schema)clsr   r   msgspec_fields
type_hintsr   r   hintmsgspec_fieldfield_schemawrapped_schematyped_dict_then_convert
serializerr&   r&   r'   __get_pydantic_core_schema__  sR   

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&   )rG   r=   r   r   )r   r   r&   r&   r'   r   $  s
   

z&PydanticMsgspecMixin._validate_msgspecc                 C   s^   t | }t|ts|S ttt tt| dt }t	|D ]}|
ds)||v r,||= q|S )a$  Serialize a msgspec.Struct to a JSON-compatible dict, stripping
        private (``_``-prefixed) and explicitly excluded fields.

        Uses ``msgspec.to_builtins`` which respects ``omit_defaults=True``,
        so only fields that differ from their declared defaults are included.
        r   r   )r   to_builtinsrG   r=   r
   setrI   r   r,   r<   r   )r   rawexcluder   r&   r&   r'   r   -  s   

z'PydanticMsgspecMixin._serialize_msgspecN)rP   r-   r.   rQ   r  r   r   rI   __annotations__classmethodr   r   r   
CoreSchemar   r   staticmethodr   r&   r&   r&   r'   r     s   
 Gr   )Hr   r   ry   collections.abcr   r   	functoolsr   inspectr   typesr   typingr   r   r	   r
   r   rv   r   numpyrg   rd   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   rP   r$   rx   ru   r   r"   r=   r,   rI   r  r   r   r   Framer#   r(   rm   r0   r3   rC   rK   rR   r   r   r   r&   r&   r&   r'   <module>   sv   
,
 $ 



