[MM] Simplify _apply_hf_processor_main (#53275)
Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk>
This commit is contained in:
@@ -190,77 +190,71 @@ class HCXVisionDummyInputsBuilder(BaseDummyInputsBuilder[HCXVisionProcessingInfo
|
||||
|
||||
|
||||
class HCXVisionMultiModalProcessor(BaseMultiModalProcessor[HCXVisionProcessingInfo]):
|
||||
def _call_hf_processor(
|
||||
def _apply_hf_processor_main(
|
||||
self,
|
||||
prompt: str,
|
||||
mm_data: Mapping[str, object],
|
||||
mm_kwargs: Mapping[str, object],
|
||||
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)
|
||||
|
||||
if not mm_data:
|
||||
return BatchFeature(dict(passthrough_data))
|
||||
|
||||
for video_idx, video_arr in enumerate(mm_data.get("videos", [])):
|
||||
if video_arr.dtype != np.uint8:
|
||||
mm_data["videos"][video_idx] = video_arr.astype(np.uint8)
|
||||
|
||||
processed_outputs = self.info.ctx.call_hf_processor(
|
||||
hf_processor=self.info.get_hf_processor(**mm_kwargs),
|
||||
images = mm_data.get("images")
|
||||
videos = mm_data.get("videos")
|
||||
|
||||
# batchify input as a single item
|
||||
processed_data = self.info.ctx.call_hf_processor(
|
||||
hf_processor=self.info.get_hf_processor(**hf_processor_mm_kwargs),
|
||||
data=dict(
|
||||
text=prompt,
|
||||
images=None,
|
||||
videos=None,
|
||||
images=None if images is None else [images],
|
||||
videos=None if videos is None else [videos],
|
||||
),
|
||||
) # text-only
|
||||
)
|
||||
|
||||
if len(mm_data) > 0:
|
||||
images = mm_data.get("images")
|
||||
videos = mm_data.get("videos")
|
||||
for k, v in processed_data.items():
|
||||
if isinstance(v, list) and len(v) > 0:
|
||||
assert len(v) == 1
|
||||
processed_data[k] = v[0]
|
||||
|
||||
# batchify input as a single item
|
||||
_processed_outputs = self.info.ctx.call_hf_processor(
|
||||
hf_processor=self.info.get_hf_processor(**mm_kwargs),
|
||||
data=dict(
|
||||
text=None,
|
||||
images=None if images is None else [images],
|
||||
videos=None if videos is None else [videos],
|
||||
),
|
||||
) # mm-only
|
||||
if images:
|
||||
processed_data["image_sizes_images"] = torch.tensor(
|
||||
processed_data["image_sizes_images"]
|
||||
)
|
||||
processed_data["vision_query_lengths_images"] = torch.tensor(
|
||||
processed_data["vision_query_lengths_images"]
|
||||
)
|
||||
|
||||
for k, v in _processed_outputs.items():
|
||||
if isinstance(v, list) and len(v) > 0:
|
||||
assert len(v) == 1
|
||||
_processed_outputs[k] = v[0]
|
||||
|
||||
if images:
|
||||
_processed_outputs["image_sizes_images"] = torch.tensor(
|
||||
_processed_outputs["image_sizes_images"]
|
||||
)
|
||||
_processed_outputs["vision_query_lengths_images"] = torch.tensor(
|
||||
_processed_outputs["vision_query_lengths_images"]
|
||||
)
|
||||
|
||||
if videos:
|
||||
_idx_per_video = [
|
||||
0,
|
||||
*accumulate(
|
||||
get_num_combined_frames(len(video)) for video in videos
|
||||
),
|
||||
if videos:
|
||||
_idx_per_video = [
|
||||
0,
|
||||
*accumulate(get_num_combined_frames(len(video)) for video in videos),
|
||||
]
|
||||
processed_data["pixel_values_videos"] = [
|
||||
processed_data["pixel_values_videos"][
|
||||
_idx_per_video[i] : _idx_per_video[i + 1]
|
||||
]
|
||||
_processed_outputs["pixel_values_videos"] = [
|
||||
_processed_outputs["pixel_values_videos"][
|
||||
for i in range(len(videos))
|
||||
]
|
||||
processed_data["vision_query_lengths_videos"] = [
|
||||
torch.tensor(
|
||||
processed_data["vision_query_lengths_videos"][
|
||||
_idx_per_video[i] : _idx_per_video[i + 1]
|
||||
]
|
||||
for i in range(len(videos))
|
||||
]
|
||||
_processed_outputs["vision_query_lengths_videos"] = [
|
||||
torch.tensor(
|
||||
_processed_outputs["vision_query_lengths_videos"][
|
||||
_idx_per_video[i] : _idx_per_video[i + 1]
|
||||
]
|
||||
)
|
||||
for i in range(len(videos))
|
||||
]
|
||||
)
|
||||
for i in range(len(videos))
|
||||
]
|
||||
|
||||
processed_outputs.update(_processed_outputs)
|
||||
processed_data.update(passthrough_data)
|
||||
|
||||
return processed_outputs
|
||||
return processed_data
|
||||
|
||||
def _get_prompt_updates(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user