Skip to content

[TRTLLM-14692][feat] Overlap LoRA and base model computations - #16951

Open
AlessioNetti wants to merge 8 commits into
NVIDIA:mainfrom
AlessioNetti:lora-cuda-stream-overlap
Open

[TRTLLM-14692][feat] Overlap LoRA and base model computations#16951
AlessioNetti wants to merge 8 commits into
NVIDIA:mainfrom
AlessioNetti:lora-cuda-stream-overlap

Conversation

@AlessioNetti

@AlessioNetti AlessioNetti commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Dev Engineer Review

  • Added LoraConfig.overlap_lora_and_base: bool = False to control whether CUDA-graph LoRA execution overlaps with base projection computation via a secondary CUDA stream.
  • Extended CudaGraphLoraManager and PyTorchModelEngine wiring to propagate overlap_lora_and_base into CUDA-graph LoRA parameters.
  • Implemented LoraLayer.forward_with_base(...) to optionally run base forward and a tuple of LoRA layers in parallel (aux stream + CUDA events) when CUDA-graph-compatible conditions are met (do_multi_stream(), not under torch compile, and LoRA entries exist for layer_idx); otherwise it falls back to sequential execution.
  • Combined base and LoRA outputs using in-place add_ on the base output buffer (when LoRA outputs are non-None).
  • Updated CUDA-graph output restoration to use torch.empty_like(...) instead of torch.zeros_like(...) prior to index_copy_.
  • Refactored LoRA call sites to delegate composition to LoraLayer.forward_with_base(...) for LoRA-enabled paths in:
    • attention (QKV)
    • mlp (up projection)
    • gated_mlp (gate/up projection)
    • linear (GEMM path)
  • Ensured eager execution remains sequential.
  • Updated TorchLlmArgs golden manifest schema to include lora_config.overlap_lora_and_base.

QA Engineer Review

No test changes.

Description

As of TRT-LLM 1.3.0rc22, LoRA computations are enqueued in the same default CUDA stream as the base model, which results in sequential execution. In an effort to improve performance, this PR changes this by introducing a secondary CUDA stream just for LoRA operations, which results in base model projections happening in parallel with LoRA, hiding most of the latency of the latter.

Below you can find a latency comparison for the new changes, benchmarked using a Qwen3 32B FP8 model, 256 ISL/OSL and a rank 32 LoRA adapter (gate_up_proj, down_proj, qkv_proj and o_proj modules), on H100. It can be seen that the changes improve latency significantly, especially in low-concurrency regimes (~1s or ~15% latency reduction at concurrency 1).

plot_optim_par_streams

The new behavior can be disabled via the new overlap_lora_and_base flag added to LoraConfig - this is disabled by default. Some caveats regarding the changes:

  • Two additional minor optimizations are included in this PR: using in-place add_ to sum LoRA outputs to base projection outputs, and switching from zeros_like to empty_like when allocating the LoRA restored output buffer;
  • This PR covers all LoRA use happening via the MLP, gated MLP, attention and Linear modules, but does not cover MoE LoRA, which follows a separate code path;
  • Parallel execution can occur only in the CUDA graph path, while the eager path remains sequential.

Test Coverage

Focused tests were added under tests/unittest/_torch/modules/tests_lora_modules/test_qwen3_sanity.py, in the TestQwen3LoRA class.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
tensorrt_llm/_torch/peft/lora/layer.py (2)

177-228: 🎯 Functional Correctness | 🔵 Trivial | 🏗️ Heavy lift

Add overlap parity coverage.

Add CUDA-graph tests that compare sequential and auxiliary-stream outputs, verify overlap_lora_and_base=False, and cover the supported Attention, MLP, GatedMLP, and Linear paths.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tensorrt_llm/_torch/peft/lora/layer.py` around lines 177 - 228, Add
CUDA-graph test coverage for LoraLayer.forward_with_base that compares
sequential execution with auxiliary-stream overlap outputs. Parameterize or
otherwise cover the supported Attention, MLP, GatedMLP, and Linear paths, and
explicitly verify that overlap_lora_and_base=False uses the sequential path
while preserving matching outputs.

164-201: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use concrete Python 3.10 types for the new helper.

Any, Dict/List/Optional, and the unannotated nested callable obscure the tensor-only add_() contract. Use concrete tensor return types, built-in generics/|, and a precise LoRA-params type. As per coding guidelines, “Annotate every function, use None for non-returning functions, avoid Any … [and] prefer built-in generic types and |.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tensorrt_llm/_torch/peft/lora/layer.py` around lines 164 - 201, Update
LoraLayer.forward_with_base and its nested lora_forward helper to use concrete
tensor-based annotations instead of Any and an unannotated callable. Replace
List/Dict/Optional and tuple typing with Python 3.10 built-in generics and |,
and use the precise existing LoRA-params type for lora_params. Annotate every
callable, including non-returning paths with None where applicable, while
preserving the current tensor/tuple-of-tensors return behavior.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@tensorrt_llm/_torch/peft/lora/layer.py`:
- Around line 177-228: Add CUDA-graph test coverage for
LoraLayer.forward_with_base that compares sequential execution with
auxiliary-stream overlap outputs. Parameterize or otherwise cover the supported
Attention, MLP, GatedMLP, and Linear paths, and explicitly verify that
overlap_lora_and_base=False uses the sequential path while preserving matching
outputs.
- Around line 164-201: Update LoraLayer.forward_with_base and its nested
lora_forward helper to use concrete tensor-based annotations instead of Any and
an unannotated callable. Replace List/Dict/Optional and tuple typing with Python
3.10 built-in generics and |, and use the precise existing LoRA-params type for
lora_params. Annotate every callable, including non-returning paths with None
where applicable, while preserving the current tensor/tuple-of-tensors return
behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 83223af9-06d8-4b0f-b4ed-4b4985b44fe6

📥 Commits

Reviewing files that changed from the base of the PR and between c82ae76 and 2447474.

📒 Files selected for processing (9)
  • tensorrt_llm/_torch/modules/attention.py
  • tensorrt_llm/_torch/modules/gated_mlp.py
  • tensorrt_llm/_torch/modules/linear.py
  • tensorrt_llm/_torch/modules/mlp.py
  • tensorrt_llm/_torch/peft/lora/cuda_graph_lora_manager.py
  • tensorrt_llm/_torch/peft/lora/layer.py
  • tensorrt_llm/_torch/pyexecutor/model_engine.py
  • tensorrt_llm/lora_helper.py
  • tensorrt_llm/usage/llm_args_golden_manifest.json

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

LoRA configuration now controls optional overlap between base and LoRA computation. Projection paths use a shared helper for execution, stream scheduling, and output merging.

Changes

LoRA overlap execution

Layer / File(s) Summary
Overlap configuration propagation
tensorrt_llm/lora_helper.py, tensorrt_llm/usage/llm_args_golden_manifest.json, tensorrt_llm/_torch/pyexecutor/model_engine.py, tensorrt_llm/_torch/peft/lora/cuda_graph_lora_manager.py
Adds the overlap_lora_and_base configuration field and propagates it into CUDA-graph LoRA parameters.
Centralized LoRA forward orchestration
tensorrt_llm/_torch/peft/lora/layer.py
Adds forward_with_base with optional auxiliary-stream execution, event synchronization, LoRA result merging, and updated CUDA-graph output restoration.
Projection path integration
tensorrt_llm/_torch/modules/attention.py, tensorrt_llm/_torch/modules/gated_mlp.py, tensorrt_llm/_torch/modules/mlp.py, tensorrt_llm/_torch/modules/linear.py
Routes attention, MLP, gated MLP, and linear LoRA execution through the shared helper.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant AttentionOrMLP
  participant LoraLayer
  participant QuantMethod
  participant AuxStream
  participant LoRAProjection
  AttentionOrMLP->>LoraLayer: call forward_with_base
  LoraLayer->>QuantMethod: execute base projection
  LoraLayer->>AuxStream: optionally schedule LoRA computation
  AuxStream->>LoRAProjection: compute LoRA output
  LoraLayer->>LoraLayer: merge LoRA output into base output
  LoraLayer-->>AttentionOrMLP: return combined output
Loading

Suggested reviewers: qijune, arysef, yihuilu512, nv-xtf, bo-nv

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 42.86% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the LoRA and base model overlap feature and follows the repository's ticket and type format.
Description check ✅ Passed The description explains the problem, solution, scope, caveats, benchmark results, and focused test coverage; the checklist is not included but is non-critical.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@AlessioNetti
AlessioNetti force-pushed the lora-cuda-stream-overlap branch 2 times, most recently from 48861ee to dea6573 Compare July 28, 2026 14:15
@Funatiq Funatiq added the api-compatible Accepted LLM API contract change that is backwards-compatible label Jul 28, 2026
@Funatiq

Funatiq commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #62207 [ run ] triggered by Bot. Commit: dea6573 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #62207 [ run ] completed with state FAILURE. Commit: dea6573
/LLM/main/L0_MergeRequest_PR pipeline #50377 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

@AlessioNetti
AlessioNetti force-pushed the lora-cuda-stream-overlap branch from dea6573 to 9b59e76 Compare July 29, 2026 10:05
@Funatiq

Funatiq commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #62511 [ run ] triggered by Bot. Commit: 9b59e76 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #62511 [ run ] completed with state SUCCESS. Commit: 9b59e76
/LLM/main/L0_MergeRequest_PR pipeline #50659 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

@Funatiq

Funatiq commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #62533 [ run ] triggered by Bot. Commit: 9b59e76 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #62533 [ run ] completed with state FAILURE. Commit: 9b59e76
/LLM/main/L0_MergeRequest_PR pipeline #50676 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

@AlessioNetti
AlessioNetti force-pushed the lora-cuda-stream-overlap branch from 9b59e76 to 1904bcf Compare July 31, 2026 08:29
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63080 [ run ] triggered by Bot. Commit: aee41f8 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63080 [ run ] completed with state FAILURE. Commit: aee41f8
/LLM/main/L0_MergeRequest_PR pipeline #51171 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

@AlessioNetti

Copy link
Copy Markdown
Contributor Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63389 [ run ] triggered by Bot. Commit: aee41f8 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63389 [ run ] completed with state FAILURE. Commit: aee41f8
/LLM/main/L0_MergeRequest_PR pipeline #51366 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

@AlessioNetti

Copy link
Copy Markdown
Contributor Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63434 [ run ] triggered by Bot. Commit: aee41f8 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63434 [ run ] completed with state FAILURE. Commit: aee41f8

Link to invocation

@AlessioNetti

Copy link
Copy Markdown
Contributor Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63440 [ run ] triggered by Bot. Commit: aee41f8 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63440 [ run ] completed with state FAILURE. Commit: aee41f8
/LLM/main/L0_MergeRequest_PR pipeline #51412 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

@AlessioNetti
AlessioNetti force-pushed the lora-cuda-stream-overlap branch from aee41f8 to 44578d2 Compare August 3, 2026 13:57
@AlessioNetti

Copy link
Copy Markdown
Contributor Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63461 [ run ] triggered by Bot. Commit: 44578d2 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63461 [ run ] completed with state FAILURE. Commit: 44578d2
/LLM/main/L0_MergeRequest_PR pipeline #51432 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

Signed-off-by: Alessio Netti <26897207+AlessioNetti@users.noreply.github.com>
Signed-off-by: Alessio Netti <26897207+AlessioNetti@users.noreply.github.com>
…o empty_like() for init

Signed-off-by: Alessio Netti <26897207+AlessioNetti@users.noreply.github.com>
Signed-off-by: Alessio Netti <26897207+AlessioNetti@users.noreply.github.com>
Signed-off-by: Alessio Netti <26897207+AlessioNetti@users.noreply.github.com>
Signed-off-by: Alessio Netti <26897207+AlessioNetti@users.noreply.github.com>
Signed-off-by: Alessio Netti <26897207+AlessioNetti@users.noreply.github.com>
Signed-off-by: Alessio Netti <26897207+AlessioNetti@users.noreply.github.com>
@AlessioNetti
AlessioNetti force-pushed the lora-cuda-stream-overlap branch from 44578d2 to 93acdfb Compare August 3, 2026 16:16
@AlessioNetti

Copy link
Copy Markdown
Contributor Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63491 [ run ] triggered by Bot. Commit: 93acdfb Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63491 [ run ] completed with state SUCCESS. Commit: 93acdfb
/LLM/main/L0_MergeRequest_PR pipeline #51460 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

@AlessioNetti

Copy link
Copy Markdown
Contributor Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63532 [ run ] triggered by Bot. Commit: 93acdfb Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63532 [ run ] completed with state FAILURE. Commit: 93acdfb
/LLM/main/L0_MergeRequest_PR pipeline #51498 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

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.

4 participants