Skip to content

Commit 12c9d8b

Browse files
p88hnjhill
andcommitted
Apply suggestions from code review
Co-authored-by: Nick Hill <[email protected]> Signed-off-by: Staszek Pasko <[email protected]>
1 parent 7cf5492 commit 12c9d8b

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

vllm/v1/serial_utils.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class MsgpackEncoder:
5252
See: https://github.com/vllm-project/vllm/issues/16185
5353
"""
5454

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:
5757
size_threshold = envs.VLLM_MSGPACK_ZERO_COPY_THRESHOLD
5858
self.encoder = msgpack.Encoder(enc_hook=self.enc_hook)
5959
# 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:
8484
if isinstance(obj, np.ndarray) and obj.dtype.kind not in ('O', 'V'):
8585
return self._encode_ndarray(obj)
8686

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.
9591
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]
108103

109104
if isinstance(obj, FunctionType):
110105
# `pickle` is generally faster than cloudpickle, but can have

0 commit comments

Comments
 (0)