[MM] Simplify _apply_hf_processor_main (#53275)
Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk>
This commit is contained in:
@@ -750,46 +750,40 @@ class GlmAsrMultiModalProcessor(BaseMultiModalProcessor["GlmAsrProcessingInfo"])
|
||||
chunk_counts.append(min(n_chunks, max_windows))
|
||||
return chunk_counts
|
||||
|
||||
def _call_hf_processor(
|
||||
def _get_hf_processor_text(self, mm_counts: Mapping[str, int]) -> str:
|
||||
return self.dummy_inputs.get_dummy_text(mm_counts)
|
||||
|
||||
def _preprocess_hf_mm_data(
|
||||
self,
|
||||
prompt: str,
|
||||
mm_data: dict[str, object],
|
||||
mm_kwargs: Mapping[str, Any],
|
||||
) -> BatchFeature:
|
||||
# Normalize input: handle deprecated key and list conversion.
|
||||
mm_data: Mapping[str, object],
|
||||
hf_processor_mm_kwargs: Mapping[str, object],
|
||||
) -> tuple[Mapping[str, object], Mapping[str, object]]:
|
||||
mm_data = dict(mm_data)
|
||||
if "audios" in mm_data:
|
||||
mm_data["audio"] = mm_data.pop("audios")
|
||||
|
||||
audio = mm_data.get("audio", [])
|
||||
audio_list = [audio] if audio and not isinstance(audio, list) else audio
|
||||
|
||||
# Early return for text-only.
|
||||
if not audio_list:
|
||||
prompt_ids = self.info.get_tokenizer().encode(prompt)
|
||||
prompt_ids = self._apply_hf_processor_tokens_only(prompt_ids)
|
||||
return BatchFeature(dict(input_ids=[prompt_ids]), tensor_type="pt")
|
||||
|
||||
# Handle sampling_rate
|
||||
feature_extractor = self.info.get_feature_extractor(**mm_kwargs)
|
||||
mm_kwargs = dict(
|
||||
**mm_kwargs,
|
||||
feature_extractor = self.info.get_feature_extractor(**hf_processor_mm_kwargs)
|
||||
hf_processor_mm_kwargs = dict(
|
||||
**hf_processor_mm_kwargs,
|
||||
sampling_rate=feature_extractor.sampling_rate,
|
||||
)
|
||||
|
||||
# Call parent method
|
||||
outputs = super()._call_hf_processor(
|
||||
prompt=prompt,
|
||||
mm_data=mm_data,
|
||||
mm_kwargs=mm_kwargs,
|
||||
)
|
||||
return mm_data, hf_processor_mm_kwargs
|
||||
|
||||
# Postprocess: rename mask and add chunk counts
|
||||
def _postprocess_hf_mm_data(
|
||||
self,
|
||||
mm_data: Mapping[str, object],
|
||||
hf_processor_mm_kwargs: Mapping[str, object],
|
||||
processed_data: BatchFeature,
|
||||
) -> BatchFeature:
|
||||
# Handle different key names from different transformers versions
|
||||
if "input_features_mask" in outputs:
|
||||
outputs["feature_attention_mask"] = outputs.pop("input_features_mask")
|
||||
elif "input_features_mask" not in outputs and "input_features" in outputs:
|
||||
if "input_features_mask" in processed_data:
|
||||
processed_data["feature_attention_mask"] = processed_data.pop(
|
||||
"input_features_mask"
|
||||
)
|
||||
elif "input_features" in processed_data:
|
||||
# If no mask is provided, create one from input_features
|
||||
input_features = outputs["input_features"]
|
||||
input_features = processed_data["input_features"]
|
||||
if isinstance(input_features, torch.Tensor):
|
||||
# Create a mask of all ones matching the sequence length
|
||||
mask = torch.ones(
|
||||
@@ -797,18 +791,22 @@ class GlmAsrMultiModalProcessor(BaseMultiModalProcessor["GlmAsrProcessingInfo"])
|
||||
input_features.shape[-1],
|
||||
dtype=torch.long,
|
||||
)
|
||||
outputs["feature_attention_mask"] = mask
|
||||
processed_data["feature_attention_mask"] = mask
|
||||
|
||||
# Get processor for chunk counts calculation
|
||||
processor = self.info.get_hf_processor(**mm_kwargs)
|
||||
audio = mm_data.get("audio", [])
|
||||
audio_list = [audio] if audio and not isinstance(audio, list) else audio
|
||||
if audio_list:
|
||||
processor = self.info.get_hf_processor(**hf_processor_mm_kwargs)
|
||||
|
||||
# Override chunk counts calculation with GLM-ASR specific logic
|
||||
chunk_counts = self._calculate_chunk_counts(
|
||||
audio_list, processor.feature_extractor, processor
|
||||
)
|
||||
outputs["chunk_counts"] = torch.tensor(chunk_counts, dtype=torch.long)
|
||||
# Override chunk counts calculation with GLM-ASR specific logic
|
||||
chunk_counts = self._calculate_chunk_counts(
|
||||
audio_list, processor.feature_extractor, processor
|
||||
)
|
||||
processed_data["chunk_counts"] = torch.tensor(
|
||||
chunk_counts, dtype=torch.long
|
||||
)
|
||||
|
||||
return outputs
|
||||
return processed_data
|
||||
|
||||
def _get_mm_fields_config(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user