Skip to content

Conversation

chang-l
Copy link
Collaborator

@chang-l chang-l commented Jul 22, 2025

[TRTLLM-6654][feat] Support external multimodal embeddings as the input to LLM decoder

This PR adds support for external embedding tensors as an additional attribute of PromptInput, which is then passed to the lead worker via the shared tensor utility (#5396). Currently, only LLaMA4 and LLaVA models are supported.

Note:
Unlike typical multimodal models, llama4's multimodal embeddings are not represented as a contiguous token block per image. Therefore, users must also provide image_special_tokens and image_special_offsets to correctly align the embeddings with the text input. See here for how image token ids are generated in input_ids: https://github.com/huggingface/transformers/blob/73869f2e81467db8422cbb4831cce9a7bdc85c4b/src/transformers/models/llama4/processing_llama4.py#L121-L135

Note:
PR dependency: #6254

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Added support for directly attaching externally computed multimodal (e.g., image) embeddings to text prompts, enabling advanced multimodal input handling.
    • Enhanced input loader to accept and process both raw media and precomputed multimodal embeddings.
    • Introduced conversion methods for efficient inter-process communication of multimodal tensor data.
    • Improved backend processing to handle multimodal embeddings seamlessly.
  • Bug Fixes

    • Improved handling and validation of multimodal data and embeddings during input processing.
  • Tests

    • Introduced unit tests to verify correct conversion between tensor data and handle representations within multimodal parameters.

Description

Test Coverage

  • Unit test to cover to_tensor and to_handle functions

GitHub Bot Help

/bot [-h] ['run', 'kill', 'skip', 'reuse-pipeline'] ...

Provide a user friendly way for developers to interact with a Jenkins server.

Run /bot [-h|--help] to print this help message.

See details below for each supported subcommand.

run [--reuse-test (optional)pipeline-id --disable-fail-fast --skip-test --stage-list "A10-PyTorch-1, xxx" --gpu-type "A30, H100_PCIe" --test-backend "pytorch, cpp" --add-multi-gpu-test --only-multi-gpu-test --disable-multi-gpu-test --post-merge --extra-stage "H100_PCIe-TensorRT-Post-Merge-1, xxx" --detailed-log --debug(experimental)]

Launch build/test pipelines. All previously running jobs will be killed.

--reuse-test (optional)pipeline-id (OPTIONAL) : Allow the new pipeline to reuse build artifacts and skip successful test stages from a specified pipeline or the last pipeline if no pipeline-id is indicated. If the Git commit ID has changed, this option will be always ignored. The DEFAULT behavior of the bot is to reuse build artifacts and successful test results from the last pipeline.

--disable-reuse-test (OPTIONAL) : Explicitly prevent the pipeline from reusing build artifacts and skipping successful test stages from a previous pipeline. Ensure that all builds and tests are run regardless of previous successes.

--disable-fail-fast (OPTIONAL) : Disable fail fast on build/tests/infra failures.

--skip-test (OPTIONAL) : Skip all test stages, but still run build stages, package stages and sanity check stages. Note: Does NOT update GitHub check status.

--stage-list "A10-PyTorch-1, xxx" (OPTIONAL) : Only run the specified test stages. Examples: "A10-PyTorch-1, xxx". Note: Does NOT update GitHub check status.

--gpu-type "A30, H100_PCIe" (OPTIONAL) : Only run the test stages on the specified GPU types. Examples: "A30, H100_PCIe". Note: Does NOT update GitHub check status.

--test-backend "pytorch, cpp" (OPTIONAL) : Skip test stages which don't match the specified backends. Only support [pytorch, cpp, tensorrt, triton]. Examples: "pytorch, cpp" (does not run test stages with tensorrt or triton backend). Note: Does NOT update GitHub pipeline status.

--only-multi-gpu-test (OPTIONAL) : Only run the multi-GPU tests. Note: Does NOT update GitHub check status.

--disable-multi-gpu-test (OPTIONAL) : Disable the multi-GPU tests. Note: Does NOT update GitHub check status.

--add-multi-gpu-test (OPTIONAL) : Force run the multi-GPU tests in addition to running L0 pre-merge pipeline.

--post-merge (OPTIONAL) : Run the L0 post-merge pipeline instead of the ordinary L0 pre-merge pipeline.

--extra-stage "H100_PCIe-TensorRT-Post-Merge-1, xxx" (OPTIONAL) : Run the ordinary L0 pre-merge pipeline and specified test stages. Examples: --extra-stage "H100_PCIe-TensorRT-Post-Merge-1, xxx".

--detailed-log (OPTIONAL) : Enable flushing out all logs to the Jenkins console. This will significantly increase the log volume and may slow down the job.

--debug (OPTIONAL) : Experimental feature. Enable access to the CI container for debugging purpose. Note: Specify exactly one stage in the stage-list parameter to access the appropriate container environment. Note: Does NOT update GitHub check status.

For guidance on mapping tests to stage names, see docs/source/reference/ci-overview.md
and the scripts/test_to_stage_mapping.py helper.

kill

kill

Kill all running builds associated with pull request.

skip

skip --comment COMMENT

Skip testing for latest commit on pull request. --comment "Reason for skipping build/test" is required. IMPORTANT NOTE: This is dangerous since lack of user care and validation can cause top of tree to break.

reuse-pipeline

reuse-pipeline

Reuse a previous pipeline to validate current commit. This action will also kill all currently running builds associated with the pull request. IMPORTANT NOTE: This is dangerous since lack of user care and validation can cause top of tree to break.

Copy link
Contributor

coderabbitai bot commented Jul 22, 2025

Walkthrough

This update introduces support for directly attaching externally computed multimodal (e.g., image) embeddings into the text processing pipeline for Llama4 and LlavaNext models. It adds conversion utilities for multimodal data between tensor and handle representations, enhances multimodal input handling in the API and executor, and includes comprehensive unit tests for the new conversion logic.

Changes

File(s) Change Summary
tensorrt_llm/_torch/models/modeling_llama.py Added attach_multimodal_embeddings to Llama4InputProcessor for injecting multimodal embeddings; updated constructor with image token constants.
tensorrt_llm/_torch/models/modeling_llava_next.py Added attach_multimodal_embeddings to LlavaNextInputProcessor to support direct multimodal embedding input.
tensorrt_llm/inputs/multimodal.py Added to_handle and to_tensor methods to MultimodalParams for tensor/handle conversion.
tensorrt_llm/inputs/utils.py Expanded multimodal input loader to handle both raw media and embeddings; updated type annotations and logic accordingly.
tensorrt_llm/llmapi/llm.py Modified generate_async to handle multimodal embeddings and invoke new processor methods; updated data flow for embeddings.
tensorrt_llm/executor/worker.py Added multimodal data conversion to tensor in executor request handling for PyTorch backend.
tests/unittest/_torch/multimodal/test_share_multiparams.py New unit tests for MultimodalParams handle/tensor conversion, including nested and empty data cases.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant API as LLM API
    participant Processor as InputProcessor
    participant Executor
    participant Model

    User->>API: Submit prompt with multimodal embeddings
    API->>Processor: attach_multimodal_embeddings(prompt, embeddings)
    Processor->>Processor: Process prompt, merge embeddings, tokenize
    Processor-->>API: token_ids, extra_inputs
    API->>Executor: Prepare request (convert embeddings if needed)
    Executor->>Model: Forward pass with tokens and embeddings
    Model-->>Executor: Output
    Executor-->>API: Output
    API-->>User: Output
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Suggested reviewers

  • yechank-nvidia
  • Naveassaf
  • amukkara
  • QiJune

Poem

A rabbit hopped with glee today,
For Llama and Llava now both can play—
With images and tokens, side by side,
Embeddings attached, no need to hide!
Data flows smooth, conversions are neat,
With tests to ensure the circle’s complete.
🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 916b0f7 and acf1542.

📒 Files selected for processing (3)
  • tensorrt_llm/_torch/models/modeling_llama.py (3 hunks)
  • tensorrt_llm/_torch/models/modeling_llava_next.py (2 hunks)
  • tensorrt_llm/llmapi/llm.py (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • tensorrt_llm/llmapi/llm.py
  • tensorrt_llm/_torch/models/modeling_llava_next.py
🧰 Additional context used
📓 Path-based instructions (2)
**/*.py

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

**/*.py: The code developed for TensorRT-LLM should conform to Python 3.8+.
Indent Python code with 4 spaces. Do not use tabs.
Always maintain the namespace when importing in Python, even if only one class or function from a module is used.
Python filenames should use snake_case (e.g., some_file.py).
Python classes should use PascalCase (e.g., class SomeClass).
Python functions and methods should use snake_case (e.g., def my_awesome_function():).
Python local variables should use snake_case. Prefix k for variable names that start with a number (e.g., k_99th_percentile = ...).
Python global variables should use upper snake_case and prefix G (e.g., G_MY_GLOBAL = ...).
Python constants should use upper snake_case (e.g., MY_CONSTANT = ...).
Avoid shadowing variables declared in an outer scope in Python.
Initialize all externally visible members of a Python class in the constructor.
For interfaces that may be used outside a file, prefer docstrings over comments in Python.
Comments in Python should be reserved for code within a function, or interfaces that are local to a file.
Use Google style docstrings for Python classes and functions, which can be parsed by Sphinx.
Attributes and variables in Python can be documented inline; attribute docstrings will be rendered under the docstring for the class.
Avoid using reflection in Python when functionality can be easily achieved without it.
When using try-except blocks in Python, limit the except to the smallest set of errors possible.
When using try-except blocks to handle multiple possible variable types in Python, keep the body of the try as small as possible, using the else block to implement the logic.

Files:

  • tensorrt_llm/_torch/models/modeling_llama.py
**/*.{cpp,h,hpp,cc,cxx,cu,py}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

All TensorRT-LLM Open Source Software code should contain an NVIDIA copyright header that includes the current year. The block should be prepended to the top of all files, including .cpp, .h, .cu, .py, and any other source files which are compiled or interpreted.

Files:

  • tensorrt_llm/_torch/models/modeling_llama.py
🧠 Learnings (2)
📓 Common learnings
Learnt from: yechank-nvidia
PR: NVIDIA/TensorRT-LLM#6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.726Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using `from_shared_tensor()` is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call `strip_for_generation()` to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.
tensorrt_llm/_torch/models/modeling_llama.py (1)

Learnt from: yechank-nvidia
PR: #6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.726Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using from_shared_tensor() is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call strip_for_generation() to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.

🪛 Ruff (0.12.2)
tensorrt_llm/_torch/models/modeling_llama.py

835-835: Line too long (122 > 120)

(E501)


836-836: Line too long (121 > 120)

(E501)

🔇 Additional comments (3)
tensorrt_llm/_torch/models/modeling_llama.py (3)

808-811: Constructor changes look good.

The new attributes properly initialize constants needed for multimodal embedding processing from the appropriate config sources.


842-894: Input validation is comprehensive and well-structured.

The method properly validates all required inputs including text prompt presence, dictionary structure, modality support, and embedding dimensions. The error messages are clear and actionable.


921-940: Text processing and tokenization logic is well-implemented.

The method correctly processes the prompt by combining text parts with image token sequences, applies proper tokenization parameters, and prepares the multimodal data structure for downstream processing. The out-of-vocabulary token replacement aligns with the multimodal processing pipeline.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (5)
tests/unittest/_torch/multimodal/test_share_multiparams.py (1)

1-95: Consider adding error condition tests and different tensor configurations.

The test suite provides excellent coverage of the happy path scenarios. Consider enhancing with:

  • Error condition tests (invalid keys, malformed data)
  • Different tensor dtypes and devices
  • Performance benchmarks for large tensors
tensorrt_llm/inputs/multimodal.py (2)

258-272: Consider handling nested dictionaries within lists.

The _to_tensor_handle function handles lists of tensors but doesn't recursively process dictionaries that might be inside lists.

Consider enhancing the list handling to support nested structures:

                 elif isinstance(v, list):
                     for i, item in enumerate(v):
                         if isinstance(item, torch.Tensor):
                             handle = SharedTensorContainer.from_tensor(
                                 item).dump_to_dict()
                             v[i] = handle
+                        elif isinstance(item, dict):
+                            _to_tensor_handle(item)

336-347: Consider handling nested dictionaries within lists (same as to_handle).

Similar to the to_handle method, the _to_tensor function should handle dictionaries within lists for consistency.

Consider enhancing the list handling:

                 elif isinstance(v, list):
                     for i, item in enumerate(v):
                         if isinstance(item, dict) and 'method_key' in item:
                             try:
                                 tensor = SharedTensorContainer.from_dict(
                                     item).get_local_view()
                                 v[i] = tensor
                             except Exception as e:
                                 raise ValueError(
                                     f"Failed to convert handle to tensor in list at index {i}: {e}"
                                 )
+                        elif isinstance(item, dict):
+                            _to_tensor(item)
tensorrt_llm/_torch/models/modeling_llama.py (2)

808-812: Track the TODO for obtaining special tokens from tokenizer.

The hardcoded special tokens should ideally come from the tokenizer to ensure consistency.

Would you like me to help implement the logic to obtain these special tokens from the tokenizer or open an issue to track this TODO?


844-943: Consider refactoring this method for better maintainability.

This method is quite long (100+ lines) with complex logic. Consider breaking it down into smaller helper methods for better readability and maintainability.

Consider extracting helper methods:

def _validate_multimodal_embedding(self, multimodal_embedding: Dict[str, List[Dict[str, Any]]]) -> None:
    """Validate the structure and content of multimodal embeddings."""
    if not isinstance(multimodal_embedding, dict):
        raise ValueError("multimodal_embedding must be a dictionary")
    
    if 'image' not in multimodal_embedding:
        raise ValueError("Only image modality is supported for now")
    
    mm_embedding_info = multimodal_embedding['image']
    if not mm_embedding_info or not isinstance(mm_embedding_info[0], dict):
        raise ValueError("Llama4 image embedding must contain special token information")

def _extract_embedding_components(self, mm_embedding_info: List[Dict[str, Any]]) -> Tuple[List, List, List]:
    """Extract embedding components from the embedding info."""
    try:
        mm_embeddings = [mm_embedding['mm_embeddings'] for mm_embedding in mm_embedding_info]
        mm_embedding_special_tokens = [mm_embedding['image_special_tokens'] for mm_embedding in mm_embedding_info]
        mm_embedding_special_offsets = [mm_embedding['image_special_token_offsets'] for mm_embedding in mm_embedding_info]
        return mm_embeddings, mm_embedding_special_tokens, mm_embedding_special_offsets
    except KeyError as e:
        raise ValueError(f"Missing required key in multimodal embedding: {e}")

def _validate_embedding_dimensions(self, mm_embeddings: List[torch.Tensor]) -> None:
    """Validate that embedding dimensions match model requirements."""
    model_hidden_size = self.model_config.text_config.hidden_size
    for i, embedding in enumerate(mm_embeddings):
        if embedding.shape[-1] != model_hidden_size:
            raise ValueError(
                f"Multimodal embedding {i} hidden size {embedding.shape[-1]} "
                f"must match model hidden size {model_hidden_size}"
            )

Then use these helpers in the main method to reduce its complexity.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ff99639 and 109ce0a.

📒 Files selected for processing (7)
  • tensorrt_llm/_torch/models/modeling_llama.py (3 hunks)
  • tensorrt_llm/_torch/models/modeling_llava_next.py (2 hunks)
  • tensorrt_llm/executor/worker.py (1 hunks)
  • tensorrt_llm/inputs/multimodal.py (1 hunks)
  • tensorrt_llm/inputs/utils.py (4 hunks)
  • tensorrt_llm/llmapi/llm.py (3 hunks)
  • tests/unittest/_torch/multimodal/test_share_multiparams.py (1 hunks)
🧠 Learnings (7)
📓 Common learnings
Learnt from: yechank-nvidia
PR: NVIDIA/TensorRT-LLM#6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using `from_shared_tensor()` is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call `strip_for_generation()` to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.
tensorrt_llm/executor/worker.py (1)

Learnt from: yechank-nvidia
PR: #6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using from_shared_tensor() is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call strip_for_generation() to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.

tensorrt_llm/inputs/multimodal.py (1)

Learnt from: yechank-nvidia
PR: #6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using from_shared_tensor() is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call strip_for_generation() to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.

tensorrt_llm/_torch/models/modeling_llama.py (1)

Learnt from: yechank-nvidia
PR: #6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using from_shared_tensor() is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call strip_for_generation() to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.

tensorrt_llm/llmapi/llm.py (1)

Learnt from: yechank-nvidia
PR: #6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using from_shared_tensor() is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call strip_for_generation() to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.

tensorrt_llm/inputs/utils.py (1)

Learnt from: yechank-nvidia
PR: #6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using from_shared_tensor() is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call strip_for_generation() to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.

tensorrt_llm/_torch/models/modeling_llava_next.py (1)

Learnt from: yechank-nvidia
PR: #6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using from_shared_tensor() is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call strip_for_generation() to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.

🪛 Ruff (0.12.2)
tensorrt_llm/_torch/models/modeling_llama.py

837-837: Line too long (122 > 120)

(E501)


838-838: Line too long (121 > 120)

(E501)

🧰 Additional context used
🧠 Learnings (7)
📓 Common learnings
Learnt from: yechank-nvidia
PR: NVIDIA/TensorRT-LLM#6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using `from_shared_tensor()` is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call `strip_for_generation()` to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.
tensorrt_llm/executor/worker.py (1)

Learnt from: yechank-nvidia
PR: #6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using from_shared_tensor() is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call strip_for_generation() to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.

tensorrt_llm/inputs/multimodal.py (1)

Learnt from: yechank-nvidia
PR: #6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using from_shared_tensor() is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call strip_for_generation() to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.

tensorrt_llm/_torch/models/modeling_llama.py (1)

Learnt from: yechank-nvidia
PR: #6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using from_shared_tensor() is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call strip_for_generation() to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.

tensorrt_llm/llmapi/llm.py (1)

Learnt from: yechank-nvidia
PR: #6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using from_shared_tensor() is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call strip_for_generation() to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.

tensorrt_llm/inputs/utils.py (1)

Learnt from: yechank-nvidia
PR: #6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using from_shared_tensor() is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call strip_for_generation() to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.

tensorrt_llm/_torch/models/modeling_llava_next.py (1)

Learnt from: yechank-nvidia
PR: #6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using from_shared_tensor() is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call strip_for_generation() to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.

🪛 Ruff (0.12.2)
tensorrt_llm/_torch/models/modeling_llama.py

837-837: Line too long (122 > 120)

(E501)


838-838: Line too long (121 > 120)

(E501)

🔇 Additional comments (13)
tensorrt_llm/executor/worker.py (1)

500-505: LGTM: Tensor conversion logic is correctly implemented for multimodal embeddings.

The conditional tensor conversion properly handles the deserialization of multimodal embeddings from shared tensor handles back to PyTorch tensors for the PyTorch backend. The conditions appropriately check for PyTorch backend, multimodal parameters existence, and multimodal data presence before conversion.

tensorrt_llm/llmapi/llm.py (3)

344-347: LGTM: Condition properly expanded to include multimodal embeddings.

The updated condition correctly triggers VLM reprocessing for both existing multi_modal_data and new multi_modal_embeddings scenarios, maintaining backward compatibility while supporting the new feature.


397-399: LGTM: Handle conversion optimizes IPC for multimodal embeddings.

The conversion to shared tensor handle with key "multimodal_embedding" correctly optimizes inter-process communication by serializing tensor data. This pairs with the inverse to_tensor operation in the worker for efficient multimodal data transfer.


380-384: attch_multimodal_embeddings implementation verified

The attch_multimodal_embeddings method is present in both model-specific input processors, matching the new branch’s call site:

  • tensorrt_llm/_torch/models/modeling_llama.py
  • tensorrt_llm/_torch/models/modeling_llava_next.py

No further action required.

tests/unittest/_torch/multimodal/test_share_multiparams.py (4)

11-30: LGTM: Well-structured test setup with comprehensive multimodal data.

The setUp method creates realistic test fixtures covering various multimodal data types (embeddings, mrope config, image data) using CPU tensors, which is appropriate for unit testing since CUDA IPC requires separate processes.


31-50: LGTM: Thorough edge case testing for None and empty data.

The test properly validates behavior with None and empty multimodal data, ensuring the handle conversion methods are robust. The test also verifies that MultimodalInput objects are preserved correctly when they don't contain tensor data.


51-64: LGTM: Basic round-trip conversion test validates data integrity.

The test correctly verifies that tensor data survives the handle conversion round-trip (to_handle followed by to_tensor) while maintaining type and value integrity using torch.allclose.


65-91: LGTM: Comprehensive nested data conversion test ensures full functionality.

The test validates that complex nested multimodal data structures with multiple tensor types (embeddings, mrope configs, image data) are correctly preserved through the full conversion cycle. The individual tensor comparisons ensure all data types are handled properly.

tensorrt_llm/_torch/models/modeling_llava_next.py (2)

3-3: LGTM!

The addition of Dict import is appropriate for the new method's type annotations.


218-229: Implementation looks correct.

The method correctly:

  • Validates the presence of 'image' key in multimodal_embedding
  • Tokenizes the prompt
  • Uses the existing _postprocess method to handle embedding expansion
  • Returns the expected tuple format
tensorrt_llm/_torch/models/modeling_llama.py (1)

2-2: LGTM!

The addition of Dict import is appropriate for the new method's type annotations.

tensorrt_llm/inputs/utils.py (2)

325-325: LGTM!

The expanded type annotation correctly supports multiple media data formats including embeddings.


483-613: Well-implemented support for multimodal embeddings.

The changes correctly:

  • Add optional mm_embeddings parameter with proper type hints
  • Extend convert_to_conversation_message to handle embeddings
  • Maintain backward compatibility with raw media inputs
  • Use distinct dictionary keys to differentiate between embeddings and raw data

The implementation is clean and maintains the existing functionality while adding the new capability.

@chang-l chang-l marked this pull request as ready for review July 22, 2025 16:21
@chang-l chang-l requested review from a team as code owners July 22, 2025 16:21
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (1)
tensorrt_llm/_torch/models/modeling_llama.py (1)

820-841: Fix the typo in method name and address line length issues.

The method name has a typo and there are line length violations.

🧹 Nitpick comments (1)
tensorrt_llm/_torch/models/modeling_llama.py (1)

935-935: Validate the out-of-vocabulary token index.

Using self.vocab_size + 1 as a hardcoded out-of-vocabulary token index could lead to conflicts. Consider making this configurable or validating that this index is reserved for this purpose.

-        token_ids[token_ids == self.image_token_index] = self.vocab_size + 1
+        # Use a well-defined OOV token index, potentially from model config
+        oov_token_index = getattr(self.model_config, 'oov_token_index', self.vocab_size + 1)
+        token_ids[token_ids == self.image_token_index] = oov_token_index
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 109ce0a and 916b0f7.

📒 Files selected for processing (1)
  • tensorrt_llm/_torch/models/modeling_llama.py (3 hunks)
🧠 Learnings (2)
📓 Common learnings
Learnt from: yechank-nvidia
PR: NVIDIA/TensorRT-LLM#6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using `from_shared_tensor()` is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call `strip_for_generation()` to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.
tensorrt_llm/_torch/models/modeling_llama.py (1)

Learnt from: yechank-nvidia
PR: #6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using from_shared_tensor() is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call strip_for_generation() to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.

🪛 Ruff (0.12.2)
tensorrt_llm/_torch/models/modeling_llama.py

835-835: Line too long (122 > 120)

(E501)


836-836: Line too long (121 > 120)

(E501)

🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: yechank-nvidia
PR: NVIDIA/TensorRT-LLM#6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using `from_shared_tensor()` is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call `strip_for_generation()` to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.
tensorrt_llm/_torch/models/modeling_llama.py (1)

Learnt from: yechank-nvidia
PR: #6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using from_shared_tensor() is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call strip_for_generation() to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.

🪛 Ruff (0.12.2)
tensorrt_llm/_torch/models/modeling_llama.py

835-835: Line too long (122 > 120)

(E501)


836-836: Line too long (121 > 120)

(E501)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Pre-commit Check
🔇 Additional comments (2)
tensorrt_llm/_torch/models/modeling_llama.py (2)

2-2: Import addition looks good.

The Any type import is necessary for the new method's type annotations.


808-811: Constants definition looks good.

The new image token constants are well-defined and provide clear abstractions for multimodal token processing.

Copy link
Collaborator

@yechank-nvidia yechank-nvidia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for the work.
Can you add the usage under here and here?

@chang-l
Copy link
Collaborator Author

chang-l commented Jul 24, 2025

Thx for the work. Can you add the usage under here and here?

Hi @yechank-nvidia , would it be okay to add trtllm-serve support in a follow-up PR? I plan to integrate this feature into trtllm-serve as part of the EPD integration work (#5000).

@chang-l
Copy link
Collaborator Author

chang-l commented Jul 25, 2025

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #12909 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #12909 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #9625 completed with status: 'FAILURE'

@chang-l
Copy link
Collaborator Author

chang-l commented Jul 25, 2025

/bot run

1 similar comment
@ZhanruiSunCh
Copy link
Collaborator

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #13028 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #13028 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #9732 completed with status: 'FAILURE'

@chang-l
Copy link
Collaborator Author

chang-l commented Jul 26, 2025

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Collaborator

PR_Github #13056 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #13056 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #9756 completed with status: 'FAILURE'

@chang-l
Copy link
Collaborator Author

chang-l commented Jul 29, 2025

/bot run --disable-fail-fast

@chang-l chang-l enabled auto-merge (squash) July 29, 2025 04:37
@tensorrt-cicd
Copy link
Collaborator

PR_Github #13292 [ run ] triggered by Bot

@chang-l
Copy link
Collaborator Author

chang-l commented Jul 29, 2025

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Collaborator

PR_Github #13404 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #13292 [ run ] completed with state ABORTED

@chang-l
Copy link
Collaborator Author

chang-l commented Jul 30, 2025

/bot run

@chang-l
Copy link
Collaborator Author

chang-l commented Jul 30, 2025

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #13475 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #13404 [ run ] completed with state ABORTED

@tensorrt-cicd
Copy link
Collaborator

PR_Github #13475 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #10089 completed with status: 'SUCCESS'

@chang-l chang-l merged commit b4065d8 into NVIDIA:main Jul 30, 2025
3 checks passed
lancelly pushed a commit to lancelly/TensorRT-LLM that referenced this pull request Aug 6, 2025
jain-ria pushed a commit to jain-ria/TensorRT-LLM that referenced this pull request Aug 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants