TensorRT-LLM icon indicating copy to clipboard operation
TensorRT-LLM copied to clipboard

[Bug]: Preserve per-item processed metadata when computing multimodal token lengths

Open HOYALIM opened this issue 4 months ago • 1 comments

System Info

  • TensorRT-LLM: current main at 573bd5f0b8414566bd6183f74b06f8c8a99e6a69
  • Reproduction hardware: Apple M1 Pro CPU, macOS arm64
  • Python 3.11.15, PyTorch 2.12.1
  • Backend surface: PyTorch multimodal input preprocessing/hashing contract

Information

find_mm_token_lengths accepts flattened processor-produced video_grid_thw, but it only preserves that metadata when there is one source video or exactly one grid row per video. When several source videos each expand to a variable number of processed rows, the function cannot recover item boundaries and drops video_grid_thw entirely.

That fallback is not generally equivalent: processed metadata may reflect frame sampling, segmentation, resizing, or cropping decisions that cannot be reconstructed from the raw video item. The returned lengths feed multimodal hashing, positions, masks, and layout validation.

The current source contains TODO(TRTLLM-11951) at this branch suggesting a generic per-item processed-metadata route.

Reproduction

I executed the exact current-source find_mm_token_lengths definition with a deterministic synthetic processor whose processed grid metadata is authoritative.

layout expected current result
one video, two grid rows [14] [14]
two videos, one grid row each [2, 3] [2, 3]
two videos, two grid rows each [5, 9] [-1, -1]

For the failing case, current main emits:

find_mm_token_lengths: video_grid_thw row count (4) does not match number of videos in mm_data (2); falling back to per-item recompute without video_grid_thw.

The processor receives video_grid_thw=None for both items. The result reproduced identically in three repetitions.

Expected behavior

Processor-produced multimodal metadata should retain source-item boundaries so each get_num_tokens_per_<modality> invocation receives only the metadata belonging to that item. The contract should be modality-generic rather than adding another Qwen-specific argument.

Actual behavior

Flattened multi-row video metadata loses item ownership. Current code drops it and recomputes from raw inputs, which may produce token lengths inconsistent with the already processed tensors.

Active adjacent work

I found three active PRs touching this area and do not want to overlap them:

  • #16051 introduces typed multimodal encoder item metadata for runtime scheduling;
  • #16337 introduces a prompt-order mixed-modality manifest;
  • #15766 adds model-specific temporal_ids routing in find_mm_token_lengths.

Their current patches do not partition flattened multi-row video_grid_thw by source item or remove this fallback, but one of their metadata contracts may be the right foundation. I will not implement until maintainers confirm whether this should be handled by one of those PRs or as an independent follow-up.

Proposed contribution scope

If maintainers approve, I would like to contribute a narrow, API-compatible change that:

  1. defines a generic per-item processed-metadata representation or adapter at the input-processor boundary;
  2. preserves the existing single-video and one-row-per-video behavior;
  3. covers variable rows per item, malformed boundary metadata, image/audio controls, and deterministic item ordering;
  4. adds CPU-only synthetic regression tests and leaves full-model GPU validation clearly separated.

Could a TensorRT-LLM engineer confirm the preferred metadata shape and whether this work is available for an external contribution?

HOYALIM avatar Jul 15 '26 22:07 HOYALIM

Hi @HOYALIM ,I'd like to contribute a fix for this issue. I've analyzed the codebase and here is my proposed approach:

Problem: find_mm_token_lengths in tensorrt_llm/inputs/multimodal.py:749 receives video_grid_thw as a flat (total_rows, 3) tensor from the HF processor, but loses the mapping between grid rows and source video items when multiple videos produce variable numbers of rows each.

Proposed fix:

  1. In model-specific processors (modeling_qwen3vl.py, modeling_qwen2vl.py), split the flat video_grid_thw into per-video tensors after the HF processor call, storing a List[torch.Tensor] in multimodal_data["video"]["video_grid_thw"] instead of a flat tensor
  2. Update find_mm_token_lengths to handle both the new list format (preferred) and legacy flat tensor (backward compat)
  3. Add CPU-only synthetic regression tests for the multi-video multi-row case

This is a CPU-only change all tests can run without GPU. The fix is backward-compatible and moves toward the generic per-item metadata route that TODO(TRTLLM-11951) calls for.

Could a maintainer confirm:

  1. Whether this approach aligns with your vision for the generic metadata route?
  2. Whether there would be overlap with any of the three active PRs (#16051, #16337, #15766) that I should be aware of?

Happy to adjust based on feedback before starting implementation.

DhineshPonnarasan avatar Jul 21 '26 13:07 DhineshPonnarasan