[MM] Further cleanup _apply_hf_processor_main (#53610)
Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk>
This commit is contained in:
@@ -45,6 +45,7 @@ from vllm.multimodal.processing import BaseDummyInputsBuilder
|
||||
from vllm.multimodal.processing.processor import (
|
||||
BaseMultiModalProcessor,
|
||||
BaseProcessingInfo,
|
||||
HFMultiModalInputs,
|
||||
PromptReplacement,
|
||||
PromptUpdate,
|
||||
ResolvedPromptUpdate,
|
||||
@@ -867,40 +868,38 @@ class Phi4MMDummyInputsBuilder(BaseDummyInputsBuilder[Phi4MMProcessingInfo]):
|
||||
|
||||
|
||||
class Phi4MMMultiModalProcessor(BaseMultiModalProcessor[Phi4MMProcessingInfo]):
|
||||
def _apply_hf_processor_main(
|
||||
def _get_hf_mm_text(self, mm_counts: Mapping[str, int]) -> str:
|
||||
return self.dummy_inputs.get_dummy_text(mm_counts)
|
||||
|
||||
def _get_hf_mm_inputs(
|
||||
self,
|
||||
mm_items: MultiModalDataItems,
|
||||
hf_processor_mm_kwargs: Mapping[str, object],
|
||||
hf_kwargs: Mapping[str, object],
|
||||
) -> HFMultiModalInputs:
|
||||
hf_inputs = super()._get_hf_mm_inputs(mm_items, hf_kwargs)
|
||||
|
||||
# The Phi4MM processor expects "audios" instead of "audio"
|
||||
hf_data = hf_inputs.hf_data
|
||||
if "audio" in hf_data:
|
||||
hf_data["audios"] = hf_data.pop("audio")
|
||||
|
||||
sr = self.info.get_feature_extractor(**hf_inputs.hf_kwargs).sampling_rate
|
||||
if audio_data := hf_data.get("audios", []):
|
||||
assert isinstance(audio_data, Sequence)
|
||||
hf_data["audios"] = [(data, sr) for data in audio_data]
|
||||
|
||||
return hf_inputs
|
||||
|
||||
def _postprocess_hf_mm_data(
|
||||
self,
|
||||
hf_data: Mapping[str, object],
|
||||
hf_kwargs: Mapping[str, object],
|
||||
processed_data: BatchFeature,
|
||||
) -> BatchFeature:
|
||||
valid_mm_items = mm_items.select(
|
||||
{k for k, c in mm_items.get_all_counts().items() if c > 0}
|
||||
)
|
||||
mm_data, passthrough_data = self._get_hf_mm_data(valid_mm_items)
|
||||
if not hf_data:
|
||||
return processed_data
|
||||
|
||||
if not mm_data:
|
||||
return BatchFeature(dict(passthrough_data))
|
||||
|
||||
prompt_text = self.dummy_inputs.get_dummy_text(mm_items.get_all_counts())
|
||||
|
||||
sr = self.info.get_feature_extractor(**hf_processor_mm_kwargs).sampling_rate
|
||||
raw_audio_data = mm_data.get("audios", [])
|
||||
assert isinstance(raw_audio_data, Sequence)
|
||||
audio_data = []
|
||||
for data in raw_audio_data:
|
||||
assert isinstance(data, (list, np.ndarray, torch.Tensor))
|
||||
audio_data.append(data)
|
||||
|
||||
processor_data = dict(text=prompt_text, **mm_data)
|
||||
if audio_data:
|
||||
processor_data["audios"] = [(data, sr) for data in audio_data]
|
||||
|
||||
processed_data = self.info.ctx.call_hf_processor(
|
||||
self.info.get_hf_processor(**hf_processor_mm_kwargs),
|
||||
processor_data,
|
||||
hf_processor_mm_kwargs,
|
||||
)
|
||||
|
||||
hf_processor = self.info.get_hf_processor(**hf_processor_mm_kwargs)
|
||||
hf_processor = self.info.get_hf_processor(**hf_kwargs)
|
||||
num_img_tokens = [
|
||||
self.info.get_num_image_tokens(
|
||||
image_width=img_size[0],
|
||||
@@ -911,21 +910,26 @@ class Phi4MMMultiModalProcessor(BaseMultiModalProcessor[Phi4MMProcessingInfo]):
|
||||
]
|
||||
processed_data["num_img_tokens"] = num_img_tokens
|
||||
|
||||
audio_features = processed_data["input_audio_embeds"]
|
||||
audios = hf_data.get("audios", [])
|
||||
assert isinstance(audios, list)
|
||||
audio_features = processed_data.get("input_audio_embeds")
|
||||
if audio_features is None:
|
||||
return processed_data
|
||||
|
||||
sr = self.info.get_feature_extractor(**hf_kwargs).sampling_rate
|
||||
feature_sizes = [
|
||||
self.info.get_audio_num_frames(len(audio), sr) for audio in audio_data
|
||||
self.info.get_audio_num_frames(len(audio), sr) for audio, _ in audios
|
||||
]
|
||||
processed_data["input_audio_embeds"] = [
|
||||
audio_features[idx, :size] for idx, size in enumerate(feature_sizes)
|
||||
]
|
||||
processed_data.update(passthrough_data)
|
||||
|
||||
return processed_data
|
||||
|
||||
def _get_mm_fields_config(
|
||||
self,
|
||||
hf_inputs: BatchFeature,
|
||||
hf_processor_mm_kwargs: Mapping[str, object],
|
||||
hf_kwargs: Mapping[str, object],
|
||||
) -> Mapping[str, MultiModalFieldConfig]:
|
||||
return dict(
|
||||
input_image_embeds=MultiModalFieldConfig.batched("image"),
|
||||
|
||||
Reference in New Issue
Block a user