@@ -52,8 +52,8 @@ class MsgpackEncoder:
52
52
See: https://github.com/vllm-project/vllm/issues/16185
53
53
"""
54
54
55
- def __init__ (self , size_threshold = None ):
56
- if ( size_threshold is None ) :
55
+ def __init__ (self , size_threshold : Optional [ int ] = None ):
56
+ if size_threshold is None :
57
57
size_threshold = envs .VLLM_MSGPACK_ZERO_COPY_THRESHOLD
58
58
self .encoder = msgpack .Encoder (enc_hook = self .enc_hook )
59
59
# This is used as a local stash of buffers that we can then access from
@@ -84,27 +84,22 @@ def enc_hook(self, obj: Any) -> Any:
84
84
if isinstance (obj , np .ndarray ) and obj .dtype .kind not in ('O' , 'V' ):
85
85
return self ._encode_ndarray (obj )
86
86
87
- if isinstance (obj , MultiModalKwargs ):
88
- mm : MultiModalKwargs = obj
89
- if mm .modalities :
90
- # ignore the main dict, it will be re-indexed.
91
- # pass a list of MultiModalKwargsItem, then see below
92
- # Any tensors *not* indexed by modality will be ignored.
93
- return list (mm ._items_by_modality .values ())
94
- # just return the main dict if there are no modalities
87
+ if isinstance (obj , MultiModalKwargs ):
88
+ mm : MultiModalKwargs = obj
89
+ if not mm .modalities :
90
+ # just return the main dict if there are no modalities.
95
91
return dict (mm )
96
-
97
- if isinstance (obj , MultiModalKwargsItem ):
98
- ret = []
99
- for elem in obj .values ():
100
- # Encode as plain dictionary + special handling for .field
101
- ret .append ({
102
- "modality" : elem .modality ,
103
- "key" : elem .key ,
104
- "data" : self ._encode_nested_tensors (elem .data ),
105
- "field" : self ._encode_field (elem .field ),
106
- })
107
- return ret
92
+
93
+ # ignore the main dict, it will be re-indexed.
94
+ # Encode a list of MultiModalKwargsItems as plain dicts
95
+ # + special handling for .field.
96
+ # Any tensors *not* indexed by modality will be ignored.
97
+ return [{
98
+ "modality" : elem .modality ,
99
+ "key" : elem .key ,
100
+ "data" : self ._encode_nested_tensors (elem .data ),
101
+ "field" : self ._encode_field (elem .field ),
102
+ } for item in mm ._items_by_modality .values () for elem in item ]
108
103
109
104
if isinstance (obj , FunctionType ):
110
105
# `pickle` is generally faster than cloudpickle, but can have
0 commit comments