[Bugfix] Make MM batching more robust (#33817)
Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk>
This commit is contained in:
+32
-20
@@ -424,8 +424,9 @@ class BaseMultiModalField(ABC):
|
||||
|
||||
keep_on_cpu: bool = False
|
||||
"""
|
||||
If `True`, then this field is excluded from being moved to the accelerator
|
||||
when `MultiModalKwargsItems.get_data()` is called to batch the data.
|
||||
If `True`, then this field is excluded from being moved to the accelerator when
|
||||
[`group_and_batch_mm_items`][vllm.multimodal.utils.group_and_batch_mm_items]
|
||||
is called to batch the data.
|
||||
"""
|
||||
|
||||
def _field_factory(self):
|
||||
@@ -1006,27 +1007,38 @@ class MultiModalKwargsItems(UserDict[str, Sequence[_I]]):
|
||||
pin_memory: bool = False,
|
||||
) -> BatchedTensorInputs:
|
||||
"""Construct a dictionary of keyword arguments to pass to the model."""
|
||||
elems_by_key = defaultdict[str, list[MultiModalFieldElem]](list)
|
||||
for modality, items in self.items():
|
||||
for i, item in enumerate(items):
|
||||
if item is None:
|
||||
raise RuntimeError(
|
||||
f"Cannot build data from empty mm_items[{modality}][{i}]"
|
||||
)
|
||||
from .utils import group_and_batch_mm_items
|
||||
|
||||
for key, elem in item.items():
|
||||
elems_by_key[key].append(elem)
|
||||
|
||||
data = {
|
||||
key: elems[0].field.reduce_data(
|
||||
elems,
|
||||
device=device,
|
||||
pin_memory=pin_memory,
|
||||
)
|
||||
for key, elems in elems_by_key.items()
|
||||
items_by_modality = self.require_data()
|
||||
batches_by_modality = {
|
||||
modality: [
|
||||
data
|
||||
for _, data in group_and_batch_mm_items(
|
||||
items,
|
||||
device=device,
|
||||
pin_memory=pin_memory,
|
||||
)
|
||||
]
|
||||
for modality, items in items_by_modality.items()
|
||||
if len(items) > 0
|
||||
}
|
||||
|
||||
return data
|
||||
out_data: BatchedTensorInputs = {}
|
||||
for _, batches in batches_by_modality.items():
|
||||
if len(batches) != 1:
|
||||
num_batches_by_modality = {
|
||||
modality: len(batches)
|
||||
for modality, batches in batches_by_modality.items()
|
||||
}
|
||||
|
||||
raise RuntimeError(
|
||||
f"Some modalities cannot be merged into a single batch "
|
||||
f"({num_batches_by_modality=})"
|
||||
)
|
||||
|
||||
out_data.update(batches[0])
|
||||
|
||||
return out_data
|
||||
|
||||
|
||||
MultiModalKwargsOptionalItems: TypeAlias = (
|
||||
|
||||
Reference in New Issue
Block a user