[MM] Further cleanup _apply_hf_processor_main (#53610)

Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk>
This commit is contained in:
Cyrus Leung
2026-09-16 10:21:00 -07:00
committed by GitHub
parent 6ca2b23e22
commit 0b9e018fb9
74 changed files with 1048 additions and 1323 deletions
+50 -57
View File
@@ -71,6 +71,7 @@ from vllm.multimodal.processing import (
PromptUpdate,
cached_encode,
)
from vllm.multimodal.processing.processor import HFMultiModalInputs
from vllm.sequence import IntermediateTensors
from vllm.utils.gpu_sync_debug import gpu_sync_allowed
from vllm.utils.tensor_schema import TensorSchema, TensorShape
@@ -1112,34 +1113,32 @@ class Ernie4_5VLMultiModalProcessor(BaseMultiModalProcessor[Ernie4_5_VLProcessin
pixel_values = pixel_values.to(hf_config.dtype)
return pixel_values
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],
) -> 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)
hf_kwargs: Mapping[str, object],
) -> HFMultiModalInputs:
hf_inputs = super()._get_hf_mm_inputs(mm_items, hf_kwargs)
hf_data = hf_inputs.hf_data
if "images" not in mm_data and "videos" not in mm_data:
return BatchFeature(dict(passthrough_data))
if "images" not in hf_data and "videos" not in hf_data:
return hf_inputs
prompt_text = self.dummy_inputs.get_dummy_text(mm_items.get_all_counts())
mm_data = dict(mm_data)
if "images" not in mm_data:
mm_data["images"] = []
if "videos" not in mm_data:
mm_data["videos"] = []
prompt_text = hf_data["text"]
hf_data["text"] = [prompt_text]
hf_data.setdefault("images", [])
hf_data.setdefault("videos", [])
# Check if HF processor supports video metadata
hf_processor = self.info.get_hf_processor(**hf_processor_mm_kwargs)
hf_processor = self.info.get_hf_processor(**hf_inputs.hf_kwargs)
supports_video_metadata = getattr(
hf_processor, "supports_video_metadata", False
)
videos = mm_data["videos"]
videos = hf_data["videos"]
assert isinstance(videos, Sequence)
if videos and not supports_video_metadata:
# Old HF processor, unwrap tuple to pure frames
@@ -1147,49 +1146,43 @@ class Ernie4_5VLMultiModalProcessor(BaseMultiModalProcessor[Ernie4_5_VLProcessin
"HF processor doesn't support video metadata. "
"Timestamps will NOT be rendered. Please upgrade the model."
)
mm_data["videos"] = [v[0] if isinstance(v, tuple) else v for v in videos]
hf_data["videos"] = [v[0] if isinstance(v, tuple) else v for v in videos]
processor_output = self.info.ctx.call_hf_processor(
hf_processor,
dict(
text=[prompt_text], images=mm_data["images"], videos=mm_data["videos"]
),
hf_processor_mm_kwargs,
)
return hf_inputs
def _postprocess_hf_mm_data(
self,
hf_data: Mapping[str, object],
hf_kwargs: Mapping[str, object],
processed_data: BatchFeature,
) -> BatchFeature:
if "images" not in hf_data and "videos" not in hf_data:
return processed_data
# Divide the processor_output into two modalities: image and video.
if processor_output is not None:
pixel_values = processor_output["images"]
if pixel_values is not None:
processor_output["images"] = self._pixel_values_norm(
pixel_values, hf_processor_mm_kwargs
)
for key in list(processor_output.keys()):
if processor_output[key] is None:
del processor_output[key]
continue
if key == "grid_thw":
grid_thw = processor_output["grid_thw"]
pixel_values_all = processor_output["images"]
# Identify elements where the first
# dimension is greater than 1 and
# treat them as the video modality
mask = grid_thw[:, 0] > 1
processor_output["video_grid_thw"] = grid_thw[mask]
processor_output["image_grid_thw"] = grid_thw[~mask]
image_patch_num = (
processor_output["image_grid_thw"].prod(dim=1).sum()
)
processor_output["pixel_values"] = pixel_values_all[
:image_patch_num
]
processor_output["pixel_values_videos"] = pixel_values_all[
image_patch_num:
]
del processor_output["images"]
pixel_values = processed_data["images"]
if pixel_values is not None:
processed_data["images"] = self._pixel_values_norm(pixel_values, hf_kwargs)
for key in list(processed_data.keys()):
if processed_data[key] is None:
del processed_data[key]
continue
if key == "grid_thw":
grid_thw = processed_data["grid_thw"]
pixel_values_all = processed_data["images"]
# Identify elements where the first
# dimension is greater than 1 and
# treat them as the video modality
mask = grid_thw[:, 0] > 1
processed_data["video_grid_thw"] = grid_thw[mask]
processed_data["image_grid_thw"] = grid_thw[~mask]
image_patch_num = processed_data["image_grid_thw"].prod(dim=1).sum()
processed_data["pixel_values"] = pixel_values_all[:image_patch_num]
processed_data["pixel_values_videos"] = pixel_values_all[
image_patch_num:
]
del processed_data["images"]
processed_data = processor_output
processed_data.update(passthrough_data)
return processed_data
def _get_prompt_updates(