Skip to content

[TRTLLM-12869][feat] Encode images and video without a prompt, and prefill from the encoded items - #20902

Draft
yechank-nvidia wants to merge 7 commits into
NVIDIA:mainfrom
yechank-nvidia:refactor/mm-handoff-compatible-20261023
Draft

yechank-nvidia wants to merge 7 commits into
NVIDIA:mainfrom
yechank-nvidia:refactor/mm-handoff-compatible-20261023

Conversation

@yechank-nvidia

@yechank-nvidia yechank-nvidia commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Why

An application with a cached image and a new video should be able to encode just the video and use both items in a later prompt. The standalone encoder currently needs the full prompt, and video rows alone do not identify the frame times or positions that prefill needs.

What

  • Qwen2.5-VL and Qwen3-VL can encode media without a prompt through the Python MultimodalEncoder. Each exported item carries its grid, video timing where applicable, and a fresh hash for its encoded rows.
  • Prefill takes the real prompt and either attached tensors in the existing {"mm_embeddings": tensor, **metadata} form or encoder handles in prompt order. It rebuilds the prompt IDs, mRoPE positions and KV spans. It accepts Qwen image-pad spacing and the Qwen3 bare-video spelling. The pixel-only SM103 target rejects attached rows at admission.
  • Retain the owned rows, metadata and hash to reuse one saved item across prompts. Consume an encoder handle once, then create a fresh export for each handle delivery. Omit all hashes for fresh identities on submission. When migrating a cache, discard old raw-media hashes or re-encode the items.
  • Prompt-free encoding accepts RGB PIL images and channels-last uint8 NumPy frames. Qwen3 videos with fps or num_frames require an explicit do_sample_frames setting across the request. Qwen2.5 needs homogeneous scheduled encoder batches. A shared encoder can set max_batch_size=1 and accept one modality per request.
  • Qwen2.5 validates the rotary positions of external video timing and the local output budget. If generation runs on another worker, its caller must allow enough rotary positions for the planned output length.

Validation

Validated revision 5dc8913d4e82 is based on 33d42fadb9a6, which includes #22328.

The reviewed head reproduced both cache problems on a shared GB200. A blue video submitted after a red one reused four KV blocks and answered red. Changing image normalization for the same UUID reused seven blocks and returned the previous output. On the fixed Qwen3-VL head, those inputs and a raw-to-attached normalization test all matched their independent cold controls. Reuse stopped at the first media token (10 common prefix tokens for video and four for images). Submitting one saved video twice through fresh CUDA exports reused 128 tokens on each call and preserved the raw result.

The broad CPU run had 6,738 passes and nine failures. All 6,855 shared cases had the same outcome on the reviewed control. All 18 added cases passed. The nine failures reproduced on the controls. With the final test expectations applied to both revisions, all 20 targeted regressions failed before the fixes and passed after them. Another 99 V2, encoder, SM103 and serving ownership cases and 38 protected LLM API tests passed.

On a shared GB200 at TP1 and BF16, attached tensors and handles matched the full raw prompt and all 32 greedy output tokens in 44/44 comparisons (24 Qwen3-VL-8B and 20 Qwen2.5-VL-7B). All 13 encoder comparisons were bitwise equal. These comparisons used CPU-staged handles with block reuse disabled. The separate warm-cache tests above used default V2 and fresh CUDA exports. Qwen2.5 concurrent image/video submissions matched their solo encodes in both orders at batch size one. Tests used the 8ba86b8b native build with its matching operator registrations. The SM103 admission test ran on CPU.

PR Checklist

Please review the following before submitting your PR.

  • PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.

  • PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.

  • Test cases are provided for new code paths (see test instructions)

  • If PR introduces API changes, an appropriate PR label is added - either api-compatible or api-breaking. For api-breaking, include BREAKING in the PR title.

  • Any new dependencies have been scanned for license and vulnerabilities

  • CODEOWNERS updated if ownership changes

  • Documentation updated as needed

  • Update tava architecture diagram if there is a significant design change in PR.

  • The reviewers assigned automatically/manually are appropriate for the PR.

  • Please check this after reviewing the above items as appropriate for this PR.

GitHub Bot Help

To see a list of available CI bot commands, please comment /bot help.

@yechank-nvidia yechank-nvidia added the api-compatible Accepted LLM API contract change that is backwards-compatible label Oct 23, 2026 — with ChatGPT Codex Connector
@yechank-nvidia yechank-nvidia changed the title [TRTLLM-12869][refactor] reuse encoder layouts with a compatible handoff [TRTLLM-12869][feat] Encode multimodal items without a prompt and prefill from item results Nov 16, 2026
@yechank-nvidia
yechank-nvidia force-pushed the refactor/mm-handoff-compatible-20261023 branch from 1c70c4b to b2ce284 Compare November 16, 2026 10:46
@yechank-nvidia yechank-nvidia changed the title [TRTLLM-12869][feat] Encode multimodal items without a prompt and prefill from item results [TRTLLM-12869][feat] Encode images and video without a prompt, and prefill from the encoded items Nov 16, 2026
Problem
-------
A caller with cached image or video embeddings must send the full
prompt to encode the remaining items. A synthetic prompt's positions
cannot be reused in another request.

Solution
--------
Let processors opt in to an item-only encoder request. Build one
placeholder per item and keep the synthetic layout and positions out
of the result. Release the unused position handles. Preserve each
item's hash on its handle, including across a cache hit. Give each
item its own random key when the encoder has no content hash.

Validation
----------
CPU tests cover pixel and grid parity, two-step preprocessing, hash
precedence and release of synthetic position handles.

Signed-off-by: Yechan Kim <[email protected]>
Problem
-------
The video grid alone cannot reproduce the temporal positions of
Qwen2.5-VL or the frame timestamps in a Qwen3-VL prompt.

Solution
--------
Keep the sampled video timing beside its host-built grid. Export
seconds per grid step for Qwen2.5-VL and the processor's frame-group
timestamps for Qwen3-VL. Keep each value with its video when the
encoder selects a subset.

Validation
----------
Processor tests reconstruct a video prompt from the exported timing
and check that each video keeps its metadata in prompt order.

Signed-off-by: Yechan Kim <[email protected]>
Problem
-------
Prefill cannot combine cached images with newly encoded videos when
the encoder has never seen the final prompt.

Solution
--------
Expand the real prompt using each item's grid and video timing, then
tokenize it once. Rebuild its mRoPE positions and KV-cache spans.
Accept attached tensors grouped by modality or handles in prompt
order. Keep the existing handoff for handles with a prompt layout or
mRoPE. Validate each item's geometry against its embedding rows.

Validation
----------
Compare prompt IDs, positions, spans and hashes against raw media for
both prefill entry points. Cover images, video, mixed item order,
partial cache hits and inconsistent metadata.

Signed-off-by: Yechan Kim <[email protected]>
Problem
-------
A typed caller cannot submit the documented media-only input to the
encoder. Its asynchronous method also excludes preprocessed inputs.

Solution
--------
Give the standalone encoder a media-only dictionary type. Use the
existing single-input check for batched requests and pass each request
through the asynchronous entry point.

Validation
----------
Type-check single and batched media, asynchronous generation and
preprocessing followed by submission. The caller passes all cases.

Signed-off-by: Yechan Kim <[email protected]>
Problem
-------
The SM103 vision target accepts attached rows but only runs its
embedding splice for raw pixels. The decoder can consume pad tokens
in place of the encoded video.

Solution
--------
Reject precomputed embeddings at request admission on this target.

Validation
----------
The CPU test builds a real video item through the Qwen processor.
Admission now raises a request error for its attached rows.

Signed-off-by: Yechan Kim <[email protected]>
Problem
-------
Splitting a Qwen request can change video sampling or pixel rescaling.
An absent media bucket creates a placeholder, and an invalid item
order can send the wrong media to the processor.

Solution
--------
Require a sampling decision for prompt-free videos with a frame target.
Accept raw RGB frames with consistent rescaling across calls. Skip
absent buckets and validate the order within each modality.

Validation
----------
CPU tests reproduce all six failures before the fix. Compare the
pixels and frame times of two separate encodes with a full request
under each explicit sampling policy.

Signed-off-by: Yechan Kim <[email protected]>
Problem
-------
The item matcher rejects valid Qwen prompts. External video timing
can exceed the rotary table. Two videos with the same layout or two
encodes with different normalization can reuse the wrong KV blocks.

Solution
--------
Match Qwen's pad tokens and video wrappers. Validate frame times and
rotary positions on the host. Store timestamps as ordinary floats.
Give each encoder export and each hashless submission a fresh item
identity. Validate supplied digests before constructing KV inputs.
Document how to cache owned rows and schedule single-modality batches.

Validation
----------
Compare raw, attached and handle prompts and positions. Check video
timing, safe reload, digest bounds and output length on CPU. On GB200
the old head returned the red clip's answer for a blue clip after
four block hits, and reused seven blocks for different normalization.

Signed-off-by: Yechan Kim <[email protected]>
@yechank-nvidia
yechank-nvidia force-pushed the refactor/mm-handoff-compatible-20261023 branch from b2ce284 to 5dc8913 Compare November 23, 2026 02:55
@yechank-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #86632 [ run ] triggered by Bot. Commit: 5dc8913 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #86632 [ run ] completed with state SUCCESS. Commit: 5dc8913
/LLM/main/L0_MergeRequest_PR pipeline #71812 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api-compatible Accepted LLM API contract change that is backwards-compatible

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants