Skip to content

[None][fix] Fix max_context_length value for attention workspace sizing#15156

Merged
yuxianq merged 1 commit into
NVIDIA:mainfrom
pengbowang-nv:dev-fix-visual-gen-attn-workspace-oob-access
Jun 12, 2026
Merged

[None][fix] Fix max_context_length value for attention workspace sizing#15156
yuxianq merged 1 commit into
NVIDIA:mainfrom
pengbowang-nv:dev-fix-visual-gen-attn-workspace-oob-access

Conversation

@pengbowang-nv

@pengbowang-nv pengbowang-nv commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

TRTLLM attention used max_context_length only to estimate context workspace size. For VisualGen, every token is a context token so max_context_length should be equal to max_seq_len.

This PR is to address a small hidden OOB write of attention workspace.

Summary by CodeRabbit

  • Bug Fixes
    • Adjusted maximum context window calculation to utilize one additional token for attention processing across both attention backends.

Description

Test Coverage

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.

@pengbowang-nv
pengbowang-nv requested a review from a team as a code owner June 9, 2026 09:48
@pengbowang-nv
pengbowang-nv requested a review from brb-nv June 9, 2026 09:48
@pengbowang-nv

Copy link
Copy Markdown
Collaborator Author

/bot run

@coderabbitai

coderabbitai Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR removes an off-by-one subtraction from context length calculations in the TensorRT-LLM attention backend. The TrtllmAttentionMetadata.max_context_length property and the FlashInferTrtllmGenAttention.attention method are both updated to compute the effective maximum context as min(max_seq_len, max_num_tokens) instead of subtracting 1 from max_seq_len.

Changes

Context length off-by-one removal

Layer / File(s) Summary
Max context length calculation
tensorrt_llm/_torch/attention_backend/trtllm.py, tensorrt_llm/_torch/attention_backend/trtllm_gen.py
TrtllmAttentionMetadata.max_context_length property and FlashInferTrtllmGenAttention.attention context clamping both remove the -1 offset from max_seq_len, increasing the effective maximum context length by one token in metadata queries and workspace sizing.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% 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
Title check ✅ Passed The title accurately describes the main fix: removing the '-1' offset from max_context_length computation to correct workspace sizing for attention operations.
Description check ✅ Passed The description explains the issue (VisualGen needs max_context_length equal to max_seq_len) and the fix, but the template structure is incomplete with empty Description and Test Coverage sections.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

@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 (1)
tensorrt_llm/_torch/attention_backend/trtllm_gen.py (1)

741-742: ⚡ Quick win

Use metadata.max_context_length to avoid contract drift.

Line 741 duplicates the metadata contract already defined in TrtllmAttentionMetadata.max_context_length. Reusing the property here keeps both paths locked to one source of truth.

♻️ Proposed refactor
-        max_context_length = min(metadata.max_seq_len, metadata.max_num_tokens)
+        max_context_length = metadata.max_context_length
🤖 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/attention_backend/trtllm_gen.py` around lines 741 - 742,
Replace the ad-hoc computation of context length with the metadata property to
avoid contract drift: set max_context_length from metadata.max_context_length
(instead of min(metadata.max_seq_len, metadata.max_num_tokens)) and use
metadata.max_context_length as the fallback for attention_window_size when
forward_args.attention_window_size is falsy (i.e., change attention_window_size
to use forward_args.attention_window_size or metadata.max_context_length);
update references in this block (max_context_length and attention_window_size)
to rely on TrtllmAttentionMetadata.max_context_length as the single source of
truth.
🤖 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/attention_backend/trtllm_gen.py`:
- Around line 741-742: Replace the ad-hoc computation of context length with the
metadata property to avoid contract drift: set max_context_length from
metadata.max_context_length (instead of min(metadata.max_seq_len,
metadata.max_num_tokens)) and use metadata.max_context_length as the fallback
for attention_window_size when forward_args.attention_window_size is falsy
(i.e., change attention_window_size to use forward_args.attention_window_size or
metadata.max_context_length); update references in this block
(max_context_length and attention_window_size) to rely on
TrtllmAttentionMetadata.max_context_length as the single source of truth.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: a69ef961-72fb-495c-886f-08efe3ca69d4

📥 Commits

Reviewing files that changed from the base of the PR and between 45e2523 and ee70236.

📒 Files selected for processing (2)
  • tensorrt_llm/_torch/attention_backend/trtllm.py
  • tensorrt_llm/_torch/attention_backend/trtllm_gen.py

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #53044 [ run ] triggered by Bot. Commit: ee70236 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #53044 [ run ] completed with state SUCCESS. Commit: ee70236
/LLM/main/L0_MergeRequest_PR pipeline #42260 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

@pengbowang-nv
pengbowang-nv force-pushed the dev-fix-visual-gen-attn-workspace-oob-access branch from ee70236 to a95d292 Compare June 10, 2026 02:37
@pengbowang-nv

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #53244 [ run ] triggered by Bot. Commit: a95d292 Link to invocation

@pengbowang-nv
pengbowang-nv force-pushed the dev-fix-visual-gen-attn-workspace-oob-access branch from a95d292 to f5fefc3 Compare June 10, 2026 09:06
@pengbowang-nv

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #53283 [ run ] triggered by Bot. Commit: f5fefc3 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #53244 [ run ] completed with state ABORTED. Commit: a95d292

Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #53283 [ run ] completed with state SUCCESS. Commit: f5fefc3
/LLM/main/L0_MergeRequest_PR pipeline #42473 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

@pengbowang-nv

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #53312 [ run ] triggered by Bot. Commit: f5fefc3 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #53312 [ run ] completed with state SUCCESS. Commit: f5fefc3
/LLM/main/L0_MergeRequest_PR pipeline #42499 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

@pengbowang-nv
pengbowang-nv force-pushed the dev-fix-visual-gen-attn-workspace-oob-access branch from f5fefc3 to 86c4352 Compare June 11, 2026 03:02
TRTLLM attention used max_context_length only to estimate context workspace size.
For visualGen, every token is a context token so max_context_length should be equal to max_seq_len.

Signed-off-by: Pengbo Wang <221450789+pengbowang-nv@users.noreply.github.com>
@pengbowang-nv
pengbowang-nv force-pushed the dev-fix-visual-gen-attn-workspace-oob-access branch from 86c4352 to 6a68216 Compare June 11, 2026 07:43
@pengbowang-nv

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #53521 [ run ] triggered by Bot. Commit: 6a68216 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #53521 [ run ] completed with state SUCCESS. Commit: 6a68216
/LLM/main/L0_MergeRequest_PR pipeline #42678 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

@pengbowang-nv

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #53591 [ run ] triggered by Bot. Commit: 6a68216 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #53591 [ run ] completed with state SUCCESS. Commit: 6a68216
/LLM/main/L0_MergeRequest_PR pipeline #42737 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

@pengbowang-nv

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #53759 [ run ] triggered by Bot. Commit: 6a68216 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #53759 [ run ] completed with state SUCCESS. Commit: 6a68216
/LLM/main/L0_MergeRequest_PR pipeline #42879 completed with status: 'SUCCESS'

CI Report

Link to invocation

@yuxianq
yuxianq merged commit 2b3af6d into NVIDIA:main Jun 12, 2026
7 checks passed
@pengbowang-nv
pengbowang-nv deleted the dev-fix-visual-gen-attn-workspace-oob-access branch June 12, 2026 04:34
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.

3 participants