[Perf] Improve multimodal item handling from O(n) to O(log n) per step (#44212)
Signed-off-by: Andy Lo <andy@mistral.ai>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
import bisect
|
||||
import mimetypes
|
||||
from collections import defaultdict
|
||||
from collections.abc import Generator, Sequence
|
||||
@@ -18,6 +19,7 @@ from vllm.utils.import_utils import LazyLoader
|
||||
from .hasher import MultiModalHasher
|
||||
from .inputs import (
|
||||
BatchedTensorInputs,
|
||||
MultiModalFeatureSpec,
|
||||
MultiModalFieldElem,
|
||||
MultiModalKwargsItem,
|
||||
MultiModalSharedField,
|
||||
@@ -109,6 +111,29 @@ def encode_video_url(
|
||||
return f"data:{mimetype};base64,{video_b64}"
|
||||
|
||||
|
||||
def get_mm_features_in_window(
|
||||
mm_features: list[MultiModalFeatureSpec],
|
||||
start: int,
|
||||
end: int,
|
||||
) -> tuple[int, int]:
|
||||
"""Return (lo, hi) indices for features overlapping [start, end).
|
||||
|
||||
Assumes mm_features are sorted by offset and non-overlapping, so
|
||||
offset + length is also sorted.
|
||||
"""
|
||||
lo = bisect.bisect_left(
|
||||
mm_features,
|
||||
start + 1,
|
||||
key=lambda f: f.mm_position.offset + f.mm_position.length,
|
||||
)
|
||||
hi = bisect.bisect_left(
|
||||
mm_features,
|
||||
end,
|
||||
key=lambda f: f.mm_position.offset,
|
||||
)
|
||||
return lo, hi
|
||||
|
||||
|
||||
def argsort_mm_positions(
|
||||
mm_positions: MultiModalPlaceholders,
|
||||
) -> list[tuple[str, int]]:
|
||||
|
||||
Reference in New Issue
Block a user