
    .`iXM              
          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<   e7e8z  e9z  ej:        z  Z;ee6d<   d Z<dede=e5e5f         dz  fdZ>dedefdZ?dededeee5         egef         defd Z@ G d! d"          ZA G d# d$          ZB G d% d&          ZCded'e5e7z  ez  d(e=ed)f         d*e3e5ef         def
d+ZD G d, d-          ZEdS ).    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                  :    t                               d           d S )NzWAllowing insecure serialization using pickle due to VLLM_ALLOW_INSECURE_SERIALIZATION=1)loggerwarning_once     h/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/v1/serial_utils.py#_log_insecure_serialization_warningr)   8   s)    
	.    r'   valreturnc                 D    | d S t          |           }|j        |j        fS N)type
__module____qualname__)r*   ts     r(   _typestrr2   ?   s&    
{tS		A<''r'   objc                     | dS t          |           t          u rd | D             S t          |           t          u rd |                                 D             S t	          |           S )zMRecursively encode type information for nested structures of
    lists/dicts.Nc                 ,    g | ]}t          |          S r&   _encode_type_info_recursive).0items     r(   
<listcomp>z/_encode_type_info_recursive.<locals>.<listcomp>L   s!    BBBd+D11BBBr'   c                 4    i | ]\  }}|t          |          S r&   r6   )r8   kvs      r(   
<dictcomp>z/_encode_type_info_recursive.<locals>.<dictcomp>N   s'    JJJda.q11JJJr'   )r.   listdictitemsr2   )r3   s    r(   r7   r7   F   sl     {tCyyDBBcBBBBCyyDJJciikkJJJJC==r'   	type_infodata
convert_fnc                      S t           t                    r't          t                    sJ  fd D             S t           t                    rat                     dk    st           d         t                    s3t          t                    sJ fdt                     D             S             S )zMRecursively decode type information for nested structures of
    lists/dicts.Nc                 L    i | ] }|t          |         |                   !S r&   _decode_type_info_recursive)r8   r<   rD   rC   rB   s     r(   r>   z/_decode_type_info_recursive.<locals>.<dictcomp>[   s@     
 
 
 *9Q<a*MM
 
 
r'   r   r   c                 8    g | ]\  }}t          ||          S r&   rG   )r8   tidrD   s      r(   r:   z/_decode_type_info_recursive.<locals>.<listcomp>d   s9     
 
 
A (Az::
 
 
r'   )
isinstancer@   r?   lenstrzip)rB   rC   rD   s   ```r(   rH   rH   R   s   
 )T"" 
$%%%%%
 
 
 
 
 

 
 
 	
 )T"" 
I!:ilC#@#@$%%%%%
 
 
 
Y--
 
 
 	
 :i&&&r'   c                        e Zd ZdZddefdZdS )UtilityResultz<Wrapper for special handling when serializing/deserializing.Nrc                     || _         d S r-   )result)selfrR   s     r(   __init__zUtilityResult.__init__n   s    r'   r-   )__name__r/   r0   __doc__r   rV   r&   r'   r(   rQ   rQ   k   s:        FF #      r'   rQ   c                   ~   e Zd ZdZddedz  fdZdedee         fdZ	dede
dee         fd	Zdedefd
Zdej        deeeedf         eez  f         fdZdej        deeeedf         eez  f         fdZdedeeef         fdZdedeeeef                  fdZdedeeef         fdZdedefdZde f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                     |t           j        }t          j        | j                  | _        d | _        || _        t           j        rt                       d S d S )N)enc_hook)
r    VLLM_MSGPACK_ZERO_COPY_THRESHOLDr   Encoderr]   encoderaux_buffersr[   !VLLM_ALLOW_INSECURE_SERIALIZATIONr)   )rU   r[   s     r(   rV   zMsgpackEncoder.__init__|   s_    !!BN>>> 26,1 	2/11111	2 	2r'   r3   r+   c                 |    	 dgx| _         }| j                            |          |d<   |d | _         S # d | _         w xY w)Nr'   r   )ra   r`   encode)rU   r3   bufss      r(   rd   zMsgpackEncoder.encode   sS    		$'*e+Dtl))#..DG
 #DtD####s   (2 	;bufc                     	 |g| _         | j         }| j                            ||           |d | _         S # d | _         w xY wr-   )ra   r`   encode_into)rU   r3   rf   re   s       r(   rh   zMsgpackEncoder.encode_into   sS    	$ #uD#DL$$S#...#DtD####s   +5 	>c                    t          |t          j                  r|                     |          S t          |t          j                  r#|j        j        dvr|                     |          S t          |t                    r+t          d |j        |j        |j        fD                       S t          |t                    r|                     |          S t          |t                     r|                     |          S t          |t$                    r(|j        }t(          j        sd |fS t-          |          |fS t(          j        s t/          dt1          |           d          t          |t2                    r,t5          j        t8          t;          j        |                    S t5          j        t>          tA          j        |t@          j!                            S )N)OVc              3   <   K   | ]}|t          |          nd V  d S r-   )int)r8   r=   s     r(   	<genexpr>z*MsgpackEncoder.enc_hook.<locals>.<genexpr>   sD         -AT     r'   zObject of type zl is not serializableSet VLLM_ALLOW_INSECURE_SERIALIZATION=1 to allow fallback to pickle-based serialization.)protocol)"rL   torchTensor_encode_tensornpndarraydtypekind_encode_ndarrayslicetuplestartstopstepr   _encode_mm_itemr   _encode_mm_itemsrQ   rT   r   rb   r7   	TypeErrorr.   r   r   ExtCUSTOM_TYPE_CLOUDPICKLEcloudpickledumpsCUSTOM_TYPE_PICKLEpickleHIGHEST_PROTOCOL)rU   r3   rT   s      r(   r]   zMsgpackEncoder.enc_hook   s   c5<(( 	,&&s+++ c2:&& 	-39>+K+K'',,,c5!! 	  )SXsx8     
 c/00 	-'',,,c011 	.((---c=)) 	?ZF9 $V|# /v66>>5 	:$s)) : : :   c<(( 	P ;68I#8N8NOOO{S6;R S S S
 
 	
r'   .c                 J   | j         J |j        j        r|j        n|                                }|j        r|j        | j        k     rt          j	        t          |          }n.t          | j                   }| j                             |           |j        j        |j        |fS r-   )ra   flagsc_contiguousrC   tobytesshapenbytesr[   r   r   CUSTOM_TYPE_RAW_VIEWrM   appendru   rN   )rU   r3   arr_datarC   s       r(   rw   zMsgpackEncoder._encode_ndarray   s     +++"y5H3883;;==y 	.CJ)<<< ;3X>>DD t'((D##H---
 y}ci--r'   c                 F   | j         J t          |          }|j        | j        k     rt	          j        t          |          }n.t          | j                   }| j                             |           t          |j
                                      d          }||j        |fS )Nztorch.)ra   r   r   r[   r   r   r   rM   r   rN   ru   removeprefixr   )rU   r3   r   rC   ru   s        r(   rr   zMsgpackEncoder._encode_tensor   s     +++s##:+++;3X>>DD t'((D##H---CI++H55ci%%r'   rA   c                 D      fd|                                 D             S )Nc                 4    i | ]\  }}|fd |D             S )c                 :    g | ]}                     |          S r&   )r}   r8   r9   rU   s     r(   r:   z>MsgpackEncoder._encode_mm_items.<locals>.<dictcomp>.<listcomp>   s'    GGGdt++D11GGGr'   r&   r8   modalityitemlistrU   s      r(   r>   z3MsgpackEncoder._encode_mm_items.<locals>.<dictcomp>   sD     
 
 
"( GGGGhGGG
 
 
r'   )rA   )rU   rA   s   ` r(   r~   zMsgpackEncoder._encode_mm_items   s6    
 
 
 
&+kkmm
 
 
 	
r'   r9   c                 D      fd|                                 D             S )Nc                 :    g | ]}                     |          S r&   )_encode_mm_field_elem)r8   elemrU   s     r(   r:   z2MsgpackEncoder._encode_mm_item.<locals>.<listcomp>   s'    KKKT**400KKKr'   )values)rU   r9   s   ` r(   r}   zMsgpackEncoder._encode_mm_item   s%    KKKKT[[]]KKKKr'   r   c                     |j         |j        |j        d n|                     |j                  |                     |j                  dS )N)r   keyrC   field)r   r   rC   _encode_nested_tensors_encode_mm_fieldr   )rU   r   s     r(   r   z$MsgpackEncoder._encode_mm_field_elem   sL    8	)t/J/J49/U/U**4:66
 
 	
r'   ntc                      t          |t          j                  r                     |          S t          |t          t
          f          r|S  fd|D             S )Nc                 :    g | ]}                     |          S r&   )r   r8   xrU   s     r(   r:   z9MsgpackEncoder._encode_nested_tensors.<locals>.<listcomp>	  s'    ;;;1++A..;;;r'   )rL   rp   rq   rr   rm   float)rU   r   s   ` r(   r   z%MsgpackEncoder._encode_nested_tensors  sb    b%,'' 	+&&r***b3,'' 	 I;;;;;;;;r'   r   c                     t                               j                  }|st          dj                   fdt	          j                  D             }||fS )NzUnsupported field type: c                 F    i | ]}|j         t          |j                   S r&   )namegetattr)r8   fr   s     r(   r>   z3MsgpackEncoder._encode_mm_field.<locals>.<dictcomp>  s)    XXXafgeQV44XXXr'   )r!   get	__class__r   dataclassesfields)rU   r   r   
factory_kws    `  r(   r   zMsgpackEncoder._encode_mm_field  sm    #''88 	JHuHHIII YXXXk>PQV>W>WXXX
Zr'   r-   )"rW   r/   r0   rX   rm   rV   r   r   r"   rd   	bytearrayrh   r]   rs   rt   ry   rN   
memoryviewrw   rp   rq   rr   r   r@   r~   r   r?   r}   r   r   r   r   r   r   r&   r'   r(   rZ   rZ   r   s        
2 
2sTz 
2 
2 
2 
2
$# 
$(7"3 
$ 
$ 
$ 
$$s $ $x7H $ $ $ $,
C ,
C ,
 ,
 ,
 ,
\.:.	sE#s(OS:%55	6. . . .(&<&	sE#s(OS:%55	6& & & & 
&; 
S#X 
 
 
 
L$8 LT$sCx.=Q L L L L
*= 
$sCx. 
 
 
 
< <3 < < < <	 &9 	  	  	  	  	  	 r'   rZ   c                   T   e Zd ZdZddedz  defdZdeee         z  defd	Z	de
d
edefdZd
edefdZdee         dedefdZdedej        fdZdedej        fdZd
eeef         defdZd
ee         defdZd
eeef         defdZd
edefdZd
edefdZ de!de"def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.
    NTr1   	share_memc                     || _         t                      | _        |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   decoderra   r   rb   r)   )rU   r1   r   argss       r(   rV   zMsgpackDecoder.__init__  sz    "244YrrQDDMDM
 
 
 /11 	2/11111	2 	2r'   re   r+   c                     t          |t                    r| j                            |          S || _        	 | j                            |d                   d| _        S # d| _        w xY w)Nr   r&   )rL   r"   r   decodera   )rU   re   s     r(   r   zMsgpackDecoder.decode)  sm    dG$$ 	-<&&t,,,	"<&&tAw//!DrD!!!!s   A 	A(r3   c                    t          |          rt          |t          j                  r|                     |          S t          |t
          j                  r|                     |          S |t          u r	t          | S t          |t                    r| 
                    |          S t          |t                    r|                     |          S |t          u r|                     |          S |S r-   )r   
issubclassrs   rt   _decode_ndarrayrp   rq   _decode_tensorrx   r   _decode_mm_itemr   _decode_mm_itemsrQ   _decode_utility_result)rU   r1   r3   s      r(   r   zMsgpackDecoder.dec_hook3  s    1:: 	8!RZ(( 1++C000!U\** 0**3///Ezzc{"!122 1++C000!233 2,,S111M!!223777
r'   c                     |\  }}|1t           j        st          d          t          ||| j                  }t          |          S )NzPVLLM_ALLOW_INSECURE_SERIALIZATION must be set to use custom utility result types)r   rb   r   rH   _convert_resultrQ   )rU   r3   result_typerT   s       r(   r   z%MsgpackDecoder._decode_utility_resultD  s`    !V"9 @  
 1VT%9 F V$$$r'   r   rT   c                     ||S |\  }}t          j        |          }t          ||          }t          j        ||| j                  S )N)r   )	importlibimport_moduler   msgspecconvertr   )rU   r   rT   mod_namer   mods         r(   r   zMsgpackDecoder._convert_resultR  sN    M$$%h//c4((v{T]KKKKr'   arrc                     |\  }}}t          |t                    r| j        |         n|}t          j        ||          }| j        s|                                }|                    |          S )Nru   )rL   rm   ra   rs   
frombufferr   copyreshape)rU   r   ru   r   rC   buffers         r(   r   zMsgpackDecoder._decode_ndarrayZ  sn     ud ,6dC+@+@J!$''dmF%000~ 	((**C{{5!!!r'   c                 x   |\  }}}t          |t                    }|r| j        |         n|}t          |t                    r|nt          |          }t	          t
          |          }t          |t
          j                  sJ |j        sd|v sJ t          j        ||          S t          j	        |t
          j
                  }|s|                                }n6| j        s/| j        r|                                n|                                }|                    |                              |          S )Nr   r   )rL   rm   ra   r   r   rp   ru   r   emptyr   uint8cloner   r   
pin_memoryview)rU   r   ru   r   rC   is_auxr   torch_dtypes           r(   r   zMsgpackDecoder._decode_tensord  s'    udD#&&+1;!$''t%fj99Qz&?Q?QeU+++u{33333} 	9::::;uK8888vU[999  	H))++CC 	H&*&6G#.."""CIIKKCxx$$))%000r'   c                 ^     t           fd|                                D                       S )Nc                 4    i | ]\  }}|fd |D             S )c                 :    g | ]}                     |          S r&   )r   r   s     r(   r:   z>MsgpackDecoder._decode_mm_items.<locals>.<dictcomp>.<listcomp>}  s'    KKK$4//55KKKr'   r&   r   s      r(   r>   z3MsgpackDecoder._decode_mm_items.<locals>.<dictcomp>|  sD       &Hh KKKK(KKK  r'   )r   rA   rU   r3   s   ` r(   r   zMsgpackDecoder._decode_mm_itemsz  sC    $   *-))++  
 
 	
r'   c                 D     t          j         fd|D                       S )Nc                 :    g | ]}                     |          S r&   )_decode_mm_field_elem)r8   r=   rU   s     r(   r:   z2MsgpackDecoder._decode_mm_item.<locals>.<listcomp>  s'    888qT''**888r'   )r   
from_elemsr   s   ` r(   r   zMsgpackDecoder._decode_mm_item  s/    #.8888C888
 
 	
r'   c                    |d         |                      |d                   |d<   |d         \  }}t          t          |          }|dk    r|                     |d                   |d<    |di |j        |d<   t          di |S )NrC   r   r   slices )r   r&   )_decode_nested_tensorsr   r   _decode_nested_slicesr   r   )rU   r3   factory_meth_namer   factory_meths        r(   r   z$MsgpackDecoder._decode_mm_field_elem  s    v;"55c&kBBCK ),G%:46GHH &&#'#=#=j>R#S#SJx #|55*55;G"))S)))r'   c                 (    t          |t          t          f          r|S t          |t                    st	          dt          |                     |r0t          |d         t                    r                     |          S  fd|D             S )Nz#Unexpected NestedTensors contents: r   c                 :    g | ]}                     |          S r&   )r   r   s     r(   r:   z9MsgpackDecoder._decode_nested_tensors.<locals>.<listcomp>  s'    <<<1++A..<<<r'   )rL   rm   r   r?   r   r.   rN   r   r   s   ` r(   r   z%MsgpackDecoder._decode_nested_tensors  s    cC<(( 	 J#t$$ 	OM$s))MMNNN 	,:c!fc** 	,&&s+++<<<<<<<<r'   c                      t          |t          t          f          sJ |r+t          |d         t          t          f          s	t          | S  fd|D             S )Nr   c                 :    g | ]}                     |          S r&   )r   r   s     r(   r:   z8MsgpackDecoder._decode_nested_slices.<locals>.<listcomp>  s'    ;;;!**1--;;;r'   )rL   r?   ry   rx   r   s   ` r(   r   z$MsgpackDecoder._decode_nested_slices  sb    #e}----- 	z#a&4-88 	#;;;;;s;;;;r'   coderC   c                     |t           k    r|S t          j        r>|t          k    rt	          j        |          S |t          k    rt          j        |          S t          d| d          )NzExtension type code z is not supported)	r   r   rb   r   r   loadsr   r   NotImplementedError)rU   r   rC   s      r(   r   zMsgpackDecoder.ext_hook  sq    '''K1 	/)))|D)))..."(...!"P"P"P"PQQQr'   )NT)$rW   r/   r0   rX   r   boolrV   r"   r   r   r.   r   rQ   r   rN   r   rs   rt   r   rp   rq   r   r@   r   r   r?   r   r   r   r   r   r   r   rm   r   r   r&   r'   r(   r   r     s/        	2 	2#* 	2 	2 	2 	2 	2"7Xg%66 "3 " " " "$ S S    "%# %- % % % %L8C= L# L# L L L L"3 "2: " " " "1# 1%, 1 1 1 1,
DcN 
7L 
 
 
 

49 
1E 
 
 
 

*c3h *<O * * * * 	=# 	=- 	= 	= 	= 	=< < < < < <
RS 
R
 
Rs 
R 
R 
R 
R 
R 
Rr'   r   methodr   .kwargsc                 4   t          |t                    r#t          t          j        |          |           }nYt          |t
                    r4	 t          | |          }n2# t          $ r t          d|d          dw xY 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)	rL   bytesr   r   r   rN   r   AttributeErrorr   )r3   r   r   r   funcs        r(   
run_methodr     s     &%   
${(00#66	FC	 	  $	3''DD 	 	 	%8&888 	
 vs##4    s   A   A?c                   \    e Zd Zedededej        fd            Zededefd            Z	dS )PydanticMsgspecMixinsource_typehandlerr+   c                 n   d t           j                            |          D             }t          |          }i }|                                D ]\  }}||         } ||          }	|j        t           j        ur3t          j        |	|j                  }
t          j	        |
          ||<   ^|j
        t           j        ur3t          j        |	|j
                  }
t          j	        |
          ||<   t          j	        |	          ||<   t          j        | j        t          j        |                    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                     i | ]
}|j         |S r&   )r   )r8   r   s     r(   r>   zEPydanticMsgspecMixin.__get_pydantic_core_schema__.<locals>.<dictcomp>  s    QQQ!&!QQQr'   )schemadefault_factory)r  default)r   structsr   r
   rA   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_schemas              r(   __get_pydantic_core_schema__z1PydanticMsgspecMixin.__get_pydantic_core_schema__  sK    RQW_-C-CK-P-PQQQ#K00
 $**,, 	J 	JJD$*40M #74==L ,G4EEE!,!@'$1$A" " "  +;NKKt&g.???!,!@')1" " "  +;NKKt  +;LIIt;!)&11
 
 	
r'   valuec                     t          ||           r|S t          |t                    r | di |S t          j        ||           S )z6Validate and convert input to msgspec.Struct instance.)r.   r&   )rL   r@   r   r   )r  r  s     r(   r  z&PydanticMsgspecMixin._validate_msgspec  sT     eS!! 	LeT"" 	 3<<<<u3////r'   N)
rW   r/   r0   classmethodr   r   r   
CoreSchemar  r  r&   r'   r(   r  r    s|        '
'
(<'
		'
 '
 '
 ['
R 0c 0c 0 0 0 [0 0 0r'   r  )Fr   r   r   collections.abcr   r   	functoolsr   inspectr   typesr   typingr   r	   r
   r   r   numpyrs   rp   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   rW   r$   r   r   r   r!   r@   r.   rN   __annotations__r   r   r   Framer"   r)   ry   r2   r7   rH   rQ   rZ   r   r   r  r&   r'   r(   <module>r,     s             . . . . . . . .                   1 1 1 1 1 1 1 1 1 1           



       ) ) ) ) ) ) % % % % % %       # # # # # #
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 > = = = = = % % % % % %	X		    8I> d4 34c9:    Y&3ci? ? ? ?  (# (%S/D0 ( ( ( (	S 	S 	 	 	 	'''+3Xc]C4H#4M+N'' ' ' '2       b  b  b  b  b  b  b  b J[R [R [R [R [R [R [R [R|!	!%K("! S/! cN	!
 	! ! ! !620 20 20 20 20 20 20 20 20 20r'   