[MM] Further cleanup _apply_hf_processor_main (#53610)
Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk>
This commit is contained in:
@@ -1122,6 +1122,9 @@ class MolmoDummyInputsBuilder(BaseDummyInputsBuilder[MolmoProcessingInfo]):
|
||||
|
||||
|
||||
class MolmoMultiModalProcessor(BaseMultiModalProcessor[MolmoProcessingInfo]):
|
||||
def _get_hf_mm_text(self, mm_counts: Mapping[str, int]) -> str:
|
||||
return self.dummy_inputs.get_dummy_text(mm_counts)
|
||||
|
||||
def _postprocess_prompt(self, prompt: list[int]) -> list[int]:
|
||||
processor = self.info.get_hf_processor()
|
||||
|
||||
@@ -1140,28 +1143,28 @@ class MolmoMultiModalProcessor(BaseMultiModalProcessor[MolmoProcessingInfo]):
|
||||
dict(tokens=tokens),
|
||||
)["input_ids"].tolist()
|
||||
|
||||
def _apply_hf_processor_main(
|
||||
def _call_hf_processor(
|
||||
self,
|
||||
mm_items: MultiModalDataItems,
|
||||
hf_processor_mm_kwargs: Mapping[str, object],
|
||||
hf_data: Mapping[str, object],
|
||||
hf_kwargs: Mapping[str, object],
|
||||
) -> BatchFeature:
|
||||
mm_counts = mm_items.get_all_counts()
|
||||
|
||||
valid_mm_items = mm_items.select({k for k, c in mm_counts.items() if c > 0})
|
||||
processor_data, passthrough_data = self._get_hf_mm_data(valid_mm_items)
|
||||
|
||||
if not processor_data:
|
||||
return BatchFeature(dict(passthrough_data))
|
||||
|
||||
hf_processor = self.info.get_hf_processor(**hf_processor_mm_kwargs)
|
||||
prompt_text = self.dummy_inputs.get_dummy_text(mm_counts)
|
||||
|
||||
processed_data = self.info.ctx.call_hf_processor(
|
||||
hf_processor = self.info.get_hf_processor(**hf_kwargs)
|
||||
return self.info.ctx.call_hf_processor(
|
||||
hf_processor.process,
|
||||
dict(text=prompt_text, **processor_data),
|
||||
hf_processor_mm_kwargs,
|
||||
hf_data,
|
||||
hf_kwargs,
|
||||
)
|
||||
|
||||
def _postprocess_hf_mm_data(
|
||||
self,
|
||||
hf_data: Mapping[str, object],
|
||||
hf_kwargs: Mapping[str, object],
|
||||
processed_data: BatchFeature,
|
||||
) -> BatchFeature:
|
||||
if not hf_data:
|
||||
return processed_data
|
||||
|
||||
hf_processor = self.info.get_hf_processor(**hf_kwargs)
|
||||
tokenizer = hf_processor.tokenizer
|
||||
image_patch_id = tokenizer.vocab[IMAGE_PATCH_TOKEN]
|
||||
|
||||
@@ -1169,7 +1172,7 @@ class MolmoMultiModalProcessor(BaseMultiModalProcessor[MolmoProcessingInfo]):
|
||||
|
||||
processed_data.pop("input_ids")
|
||||
|
||||
if (images := processor_data.get("images")) is not None:
|
||||
if (images := hf_data.get("images")) is not None:
|
||||
mm_items = self.info.parse_mm_data({"image": images}, validate=False)
|
||||
parsed_images = mm_items.get_items("image", ImageProcessorItems)
|
||||
image_sizes = [
|
||||
@@ -1193,8 +1196,6 @@ class MolmoMultiModalProcessor(BaseMultiModalProcessor[MolmoProcessingInfo]):
|
||||
processed_data["num_crops"] = num_crops
|
||||
processed_data["img_patch_id"] = image_patch_id
|
||||
|
||||
processed_data.update(passthrough_data)
|
||||
|
||||
return processed_data
|
||||
|
||||
def _get_mm_fields_config(
|
||||
|
||||
Reference in New Issue
Block a user