[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:
@@ -29,6 +29,7 @@ from vllm.model_executor.layers.fused_moe.routed_experts_capturer import (
|
||||
)
|
||||
from vllm.multimodal import MULTIMODAL_REGISTRY, MultiModalRegistry
|
||||
from vllm.multimodal.encoder_budget import MultiModalBudget
|
||||
from vllm.multimodal.utils import get_mm_features_in_window
|
||||
from vllm.v1.core.encoder_cache_manager import (
|
||||
EncoderCacheManager,
|
||||
EncoderDecoderCacheManager,
|
||||
@@ -1163,22 +1164,23 @@ class Scheduler(SchedulerInterface):
|
||||
# trackers for accounting at the encoder input level.
|
||||
mm_hashes_to_schedule = set()
|
||||
num_embeds_to_schedule = 0
|
||||
for i, mm_feature in enumerate(mm_features):
|
||||
|
||||
lo, hi = get_mm_features_in_window(
|
||||
mm_features,
|
||||
start=num_computed_tokens,
|
||||
end=num_computed_tokens + num_new_tokens + shift_computed_tokens,
|
||||
)
|
||||
# For encoder-decoder, all inputs sit at start_pos=0, so lo=0 always.
|
||||
if self.is_encoder_decoder:
|
||||
lo = 0
|
||||
|
||||
for i in range(lo, hi):
|
||||
mm_feature = mm_features[i]
|
||||
start_pos = mm_feature.mm_position.offset
|
||||
num_encoder_tokens = mm_feature.mm_position.length
|
||||
num_encoder_embeds = mm_feature.mm_position.get_num_embeds()
|
||||
item_identifier = mm_feature.identifier
|
||||
|
||||
# The encoder output is needed if the two ranges overlap:
|
||||
# [num_computed_tokens, num_computed_tokens + num_new_tokens) and
|
||||
# [start_pos, start_pos + num_encoder_tokens)
|
||||
if (
|
||||
start_pos
|
||||
>= num_computed_tokens + num_new_tokens + shift_computed_tokens
|
||||
):
|
||||
# The encoder input is not needed in this step.
|
||||
break
|
||||
|
||||
if self.is_encoder_decoder and num_computed_tokens > 0:
|
||||
assert start_pos == 0, (
|
||||
"Encoder input should be processed at the beginning of "
|
||||
@@ -1194,10 +1196,6 @@ class Scheduler(SchedulerInterface):
|
||||
# decoder tokens (num_computed_tokens > 0), then we know we
|
||||
# already calculated encoder inputs and can skip here.
|
||||
continue
|
||||
elif start_pos + num_encoder_tokens <= num_computed_tokens:
|
||||
# The encoder input is already computed and stored
|
||||
# in the decoder's KV cache.
|
||||
continue
|
||||
|
||||
if not self.is_encoder_decoder:
|
||||
# We are not using the encoder cache for encoder-decoder models,
|
||||
|
||||
Reference in New Issue
Block a user