Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions vllm_ascend/worker/model_runner_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import copy
import gc
import inspect
import itertools
import math
import re
Expand Down Expand Up @@ -1394,11 +1395,19 @@ def _prepare_inputs(
# embeddings), we always use embeddings (rather than token ids)
# as input to the multimodal model, even when the input is text.
input_ids = self.input_ids[:total_num_scheduled_tokens]
inputs_embeds = self.model.get_input_embeddings(
input_ids,
multimodal_embeddings=mm_embeds,
is_multimodal=is_mm_embed,
)
has_is_multimodal = 'is_multimodal' in inspect.signature(self.model.get_input_embeddings).parameters
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have checked that is_multimodal is not included in vLLM v0.11.0, thus only the custom Qwen2_5_vl in vllm-ascend have this arg. Maybe we can just check the model_type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

if has_is_multimodal:
inputs_embeds = self.model.get_input_embeddings(
input_ids,
multimodal_embeddings=mm_embeds,
is_multimodal=is_mm_embed,
)
else:
if mm_embeds:
inputs_embeds = self.model.get_input_embeddings(
input_ids, mm_embeds)
else:
inputs_embeds = self.model.get_input_embeddings(input_ids)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using inspect.signature inside _prepare_inputs can introduce a performance overhead, as this method is on a hot path and reflection is generally slow. This check should be performed only once and the result should be cached.

A better approach would be to perform this check once and store the result in an instance attribute. You can add a new attribute, for example _has_is_multimodal_in_get_input_embeddings, and compute it once within _prepare_inputs using lazy initialization.

Suggested change
has_is_multimodal = 'is_multimodal' in inspect.signature(self.model.get_input_embeddings).parameters
if has_is_multimodal:
inputs_embeds = self.model.get_input_embeddings(
input_ids,
multimodal_embeddings=mm_embeds,
is_multimodal=is_mm_embed,
)
else:
if mm_embeds:
inputs_embeds = self.model.get_input_embeddings(
input_ids, mm_embeds)
else:
inputs_embeds = self.model.get_input_embeddings(input_ids)
if not hasattr(self, "_has_is_multimodal_in_get_input_embeddings"):
self._has_is_multimodal_in_get_input_embeddings = (
'is_multimodal' in inspect.signature(
self.model.get_input_embeddings).parameters)
if self._has_is_multimodal_in_get_input_embeddings:
inputs_embeds = self.model.get_input_embeddings(
input_ids,
multimodal_embeddings=mm_embeds,
is_multimodal=is_mm_embed,
)
else:
if mm_embeds:
inputs_embeds = self.model.get_input_embeddings(
input_ids, mm_embeds)
else:
inputs_embeds = self.model.get_input_embeddings(input_ids)

# TODO(woosuk): Avoid the copy. Optimize.
self.inputs_embeds[:total_num_scheduled_tokens].copy_(
inputs_embeds)
Expand Down
Loading