[MM] Further cleanup _apply_hf_processor_main (#53610)
Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk>
This commit is contained in:
@@ -595,22 +595,25 @@ class Gemma4DummyInputsBuilder(BaseDummyInputsBuilder[Gemma4ProcessingInfo]):
|
||||
|
||||
|
||||
class Gemma4MultiModalProcessor(BaseMultiModalProcessor[Gemma4ProcessingInfo]):
|
||||
def _get_hf_mm_text(self, mm_counts: Mapping[str, int]) -> str:
|
||||
return self.dummy_inputs.get_dummy_text(mm_counts)
|
||||
|
||||
def _apply_hf_processor_main(
|
||||
self,
|
||||
mm_items: MultiModalDataItems,
|
||||
hf_processor_mm_kwargs: Mapping[str, object],
|
||||
hf_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}
|
||||
hf_data, hf_kwargs, passthrough_data = self._get_hf_mm_inputs(
|
||||
mm_items, hf_kwargs
|
||||
)
|
||||
mm_data, passthrough_data = self._get_hf_mm_data(valid_mm_items)
|
||||
|
||||
if not mm_data:
|
||||
return BatchFeature(dict(passthrough_data))
|
||||
if not hf_data:
|
||||
return self._finalize_hf_mm_data(hf_data, hf_kwargs, passthrough_data)
|
||||
|
||||
prompt_text = self.dummy_inputs.get_dummy_text(mm_items.get_all_counts())
|
||||
prompt_text = hf_data.pop("text")
|
||||
assert isinstance(prompt_text, str)
|
||||
|
||||
merged_kwargs = self.info.ctx.get_merged_mm_kwargs(hf_processor_mm_kwargs)
|
||||
merged_kwargs = self.info.ctx.get_merged_mm_kwargs(hf_kwargs)
|
||||
val, is_top_level_max_soft_tokens = _get_max_soft_tokens(merged_kwargs)
|
||||
|
||||
if val is not None and val not in _SUPPORTED_SOFT_TOKENS:
|
||||
@@ -619,14 +622,12 @@ class Gemma4MultiModalProcessor(BaseMultiModalProcessor[Gemma4ProcessingInfo]):
|
||||
f"Valid values are {_SUPPORTED_SOFT_TOKENS}."
|
||||
)
|
||||
|
||||
mm_data = dict(mm_data)
|
||||
|
||||
# ---- VIDEO HANDLING ----
|
||||
# Gemma4 decomposes video into timestamped image frames.
|
||||
# Each frame is processed with max_soft_tokens=70 through the
|
||||
# same vision tower, matching transformers processing_gemma4.py.
|
||||
video_outputs: dict[str, Any] = {}
|
||||
if videos := mm_data.pop("videos", []):
|
||||
if videos := hf_data.pop("videos", []):
|
||||
assert isinstance(videos, list)
|
||||
processor = self.info.get_hf_processor()
|
||||
|
||||
@@ -656,7 +657,7 @@ class Gemma4MultiModalProcessor(BaseMultiModalProcessor[Gemma4ProcessingInfo]):
|
||||
timestamps = [idx / fps for idx in frame_indices]
|
||||
|
||||
# Process frames as images with max_soft_tokens=70
|
||||
video_mm_kwargs = dict(hf_processor_mm_kwargs)
|
||||
video_mm_kwargs = dict(hf_kwargs)
|
||||
# Override configured image options too: these inputs are video
|
||||
# frames, whose cache keys exclude images_kwargs.
|
||||
video_mm_kwargs["images_kwargs"] = {}
|
||||
@@ -726,18 +727,14 @@ class Gemma4MultiModalProcessor(BaseMultiModalProcessor[Gemma4ProcessingInfo]):
|
||||
"video_timestamps": video_timestamps_per_video,
|
||||
}
|
||||
|
||||
# The processor accepts 'audio' not 'audios'.
|
||||
if "audios" in mm_data:
|
||||
mm_data["audio"] = mm_data.pop("audios")
|
||||
|
||||
# Warn if any audio waveform exceeds the model's max duration.
|
||||
if "audio" in mm_data:
|
||||
if "audio" in hf_data:
|
||||
processor = self.info.get_hf_processor()
|
||||
sr = processor.feature_extractor.sampling_rate
|
||||
max_tokens = processor.audio_seq_length
|
||||
ms_per_tok = processor.audio_ms_per_token
|
||||
max_duration_s = max_tokens * ms_per_tok / 1000.0
|
||||
audios = mm_data["audio"]
|
||||
audios = hf_data["audio"]
|
||||
if not isinstance(audios, (list, tuple)):
|
||||
audios = [audios]
|
||||
for i, waveform in enumerate(audios):
|
||||
@@ -760,13 +757,13 @@ class Gemma4MultiModalProcessor(BaseMultiModalProcessor[Gemma4ProcessingInfo]):
|
||||
# NOTE: This requires a corresponding type annotation on the
|
||||
# HF side (Gemma4ProcessorKwargs.images_kwargs) so that
|
||||
# _merge_kwargs routes max_soft_tokens into images_kwargs.
|
||||
patched_mm_kwargs = dict(hf_processor_mm_kwargs)
|
||||
patched_mm_kwargs = dict(hf_kwargs)
|
||||
if val is not None and is_top_level_max_soft_tokens:
|
||||
patched_mm_kwargs["max_soft_tokens"] = val
|
||||
|
||||
processed_data = self.info.ctx.call_hf_processor(
|
||||
self.info.get_hf_processor(**patched_mm_kwargs),
|
||||
dict(text=prompt_text, **mm_data),
|
||||
dict(text=prompt_text, **hf_data),
|
||||
patched_mm_kwargs,
|
||||
)
|
||||
|
||||
@@ -797,9 +794,9 @@ class Gemma4MultiModalProcessor(BaseMultiModalProcessor[Gemma4ProcessingInfo]):
|
||||
processed_data["input_features_mask"] = unpadded_masks
|
||||
|
||||
processed_data.update(video_outputs)
|
||||
processed_data.update(passthrough_data)
|
||||
|
||||
return processed_data
|
||||
return self._finalize_hf_mm_data(
|
||||
hf_data, hf_kwargs, passthrough_data, processed_data
|
||||
)
|
||||
|
||||
def _get_mm_fields_config(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user