Skip to content

[https://nvbugs/6565412][fix] Size trtllm-gen and thop decode buffers for beam search - #17014

Merged
pranav-nvidia merged 4 commits into
NVIDIA:mainfrom
pranav-nvidia:fix-trtllm-gen-beam-counter
Aug 11, 2026
Merged

[https://nvbugs/6565412][fix] Size trtllm-gen and thop decode buffers for beam search#17014
pranav-nvidia merged 4 commits into
NVIDIA:mainfrom
pranav-nvidia:fix-trtllm-gen-beam-counter

Conversation

@pranav-nvidia

@pranav-nvidia pranav-nvidia commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Description

The trtllm-gen decode kernel keeps one multi-CTA KV counter per attention head per decoder sequence, and the generation workspace is sized per decoder sequence too. Both the flashinfer sizing helper and the C++ thop attention path sized these from max_num_requests alone, omitting the beam-width expansion, so beam search rejects the counter buffer at warmup with:

TVM_FFI_CHECK(multi_ctas_kv_counter_size >= counter_bytes)

The max(..., multi_processor_count) floor hides the missing factor at small batch, so the failure appears as a threshold rather than a universal beam-search break. With 6 decoder heads on a 148-SM part at beam 2:

max_batch_size allocated max(6B, 148) required 6·B·2 result
8 148 96 pass
16 148 192 fail
32 192 384 fail

Not model-specific: any model reaching num_heads · batch · beam_width > SM count under beam search hits it. Present since the function was introduced in 487330e8a03 (2026-06-09); ships in v1.3.0rc19–rc22.

Fix

Both paths size from metadata.max_num_sequences, which the model engine sets to batch_size * max_beam_width. That is a construction-time capacity, unlike metadata.beam_width, which the model engine rewrites per step and holds at 1 during context — so it stays valid for a buffer that cannot grow under CUDA graph capture.

thop.attention gains an optional max_num_sequences, applied to reserveSemaphoreArray and getWorkspaceSizeForGeneration — whose first parameter is already named max_num_seq and expands to batch_beam. Context sizing stays on max_num_requests: beams share one prompt and fan out only at the first decode step. Callers that leave max_num_sequences unset run at beam width 1, so both paths fall back to max_num_requests.

Sizing up is safe — the kernel check is >=, and for cross-attention (K/V not beam-packed) the only cost is 4 · num_heads · batch · (beam − 1) bytes.

Test Coverage

  • tests/unittest/_torch/attention/test_fmha_page_index.py — the old sizing under-allocates for a beam-expanded batch above the SM floor, the multi-processor floor is preserved at batch 1, and prepare_workspace passes the beam-expanded bound to the sizing helper.
  • Controlled A/B on B200 (single-file read-only shim, so the sizing is the only delta) with a full concurrent batch in flight: max_batch_size 16 and 32 go fail → pass, 8 stays passing. This covered the flashinfer counter sizing; the thop change landed after that run and is not covered by it, so it rests on CI.

Why CI missed the original bug: every in-tree beam test sits in the masked region — the disagg beam test uses max_beam_width=2 at low concurrency, test_cli_flow beam tests target the legacy CLI, and the Whisper beam_search case uses max_batch_size=2.

PR Checklist

  • PR title follows [JIRA/NVBUG/None][type] summary
  • Commits are DCO signed off
  • New unit tests added
  • Filed as https://nvbugs/6565412 and title updated

Dev Engineer Review

  • Updated trtllm-gen and thop decode buffer sizing to use metadata.max_num_sequences.
  • Preserved context sizing and the fallback to metadata.max_num_requests.
  • Preserved the multi-processor allocation floor.
  • Updated the C++ and nanobind attention APIs to accept optional max_num_sequences.
  • Updated fallback FMHA calls to pass metadata.max_num_sequences.
  • Updated runner state, caching, semaphore reservation, and generation workspace sizing.
  • No configuration or test-list files changed.

QA Engineer Review

  • Added tests for beam-expanded sizing above the multi-processor floor.
  • Added tests for floor preservation at batch size 1.
  • Added a test that verifies prepare_workspace passes the beam-expanded maximum sequence count.
  • No test functions were removed.
  • The added unit tests are not listed in tests/integration/test_lists/ based on the provided changes.
  • Verdict: sufficient.

…search

The trtllm-gen decode kernel keeps one multi-CTA KV counter per attention head
per decoder sequence, but the buffer was sized from the request count alone.
Beam search expands each request into beam_width sequences, so the kernel
rejects the buffer at warmup with

  TVM_FFI_CHECK(multi_ctas_kv_counter_size >= counter_bytes)

The max(..., multi_processor_count) floor hides the missing factor at small
batch, so this only reproduces past a threshold: with 6 decoder heads and 148
SMs, beam 2 passes up to max_batch_size 8 and fails from 16 up. Every in-tree
beam-search test runs below that threshold, so CI does not cover it.

Signed-off-by: Pranav Shrestha <254760092+pranav-nvidia@users.noreply.github.com>
…ounter

Signed-off-by: Pranav Shrestha <254760092+pranav-nvidia@users.noreply.github.com>
@pranav-nvidia
pranav-nvidia marked this pull request as ready for review August 5, 2026 19:55
@pranav-nvidia
pranav-nvidia requested a review from a team as a code owner August 5, 2026 19:55
@pranav-nvidia

Copy link
Copy Markdown
Contributor Author

/bot run --disable-fail-fast

@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: e18f22c9-6681-44ca-8d8b-764ac5408f9b

📥 Commits

Reviewing files that changed from the base of the PR and between 4b69c59 and 3082eef.

📒 Files selected for processing (6)
  • cpp/tensorrt_llm/nanobind/thop/bindings.cpp
  • cpp/tensorrt_llm/thop/attentionOp.cpp
  • cpp/tensorrt_llm/thop/attentionOp.h
  • tensorrt_llm/_torch/attention_backend/fmha/fallback.py
  • tensorrt_llm/_torch/attention_backend/fmha/flashinfer_trtllm_gen.py
  • tests/unittest/_torch/attention/test_fmha_page_index.py
🚧 Files skipped from review as they are similar to previous changes (6)
  • tensorrt_llm/_torch/attention_backend/fmha/fallback.py
  • cpp/tensorrt_llm/thop/attentionOp.h
  • cpp/tensorrt_llm/nanobind/thop/bindings.cpp
  • tests/unittest/_torch/attention/test_fmha_page_index.py
  • tensorrt_llm/_torch/attention_backend/fmha/flashinfer_trtllm_gen.py
  • cpp/tensorrt_llm/thop/attentionOp.cpp

Walkthrough

The attention API now accepts a separate maximum sequence count. Runner caching, semaphore capacity, generation workspace sizing, and FlashInfer KV counter allocation use this value, with fallback to the maximum request count. Tests cover beam-expanded sizing.

Changes

Sequence-capacity attention sizing

Layer / File(s) Summary
Attention API contract
cpp/tensorrt_llm/thop/attentionOp.h, cpp/tensorrt_llm/thop/attentionOp.cpp, cpp/tensorrt_llm/nanobind/thop/bindings.cpp
The public and nanobind attention APIs accept optional max_num_sequences.
Runner capacity and workspace sizing
cpp/tensorrt_llm/thop/attentionOp.cpp
Runner state includes max_num_sequences. Semaphore reservation and generation workspace sizing use it. The value falls back to max_num_requests.
Python workspace integration and validation
tensorrt_llm/_torch/attention_backend/fmha/fallback.py, tensorrt_llm/_torch/attention_backend/fmha/flashinfer_trtllm_gen.py, tests/unittest/_torch/attention/test_fmha_page_index.py
Fallback attention passes sequence capacity. FlashInfer sizing uses sequence capacity with request-count fallback. Tests cover beam-expanded allocation and workspace preparation arguments.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested reviewers: perkzzheng, qijune

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% 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 uses the required ticket and type format and clearly identifies the beam-search buffer-sizing fix.
Description check ✅ Passed The description explains the issue, fix, test coverage, validation results, and checklist items with sufficient technical detail.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

@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.

Actionable comments posted: 1

🤖 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.

Inline comments:
In `@tests/unittest/_torch/attention/test_fmha_page_index.py`:
- Around line 36-52: Add a test covering
FlashInferTrtllmGenFmha.prepare_workspace that uses metadata with distinct
max_num_requests and beam_width values, then verifies the counter sizing
reflects their product by asserting the helper receives the expanded batch or
the allocated counter buffer has the corresponding size. Keep the existing
direct helper tests unchanged.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 8897a81e-6bf4-4dfe-aabc-04bb1b97f09f

📥 Commits

Reviewing files that changed from the base of the PR and between e5e3821 and bf36ce7.

📒 Files selected for processing (2)
  • tensorrt_llm/_torch/attention_backend/fmha/flashinfer_trtllm_gen.py
  • tests/unittest/_torch/attention/test_fmha_page_index.py

Comment thread tests/unittest/_torch/attention/test_fmha_page_index.py
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64110 [ run ] triggered by Bot. Commit: bf36ce7 Link to invocation

@pranav-nvidia pranav-nvidia changed the title [None][fix] Size the trtllm-gen multi-CTA KV counter buffer for beam search [https://nvbugs/6565412][fix] Size the trtllm-gen multi-CTA KV counter buffer for beam search Aug 5, 2026
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

Comment thread tensorrt_llm/_torch/attention_backend/fmha/flashinfer_trtllm_gen.py Outdated
Comment thread tensorrt_llm/_torch/attention_backend/fmha/flashinfer_trtllm_gen.py Outdated
Comment thread tensorrt_llm/_torch/attention_backend/fmha/flashinfer_trtllm_gen.py Outdated
…es, including thop

Take the bound from metadata.max_num_sequences instead of recomputing
max_num_requests * beam_width: beam_width is rewritten per step and is 1
during context, so it cannot size a buffer that must outlive CUDA graph
capture. The thop attention path had the same gap and now takes the same
bound for its semaphore and generation workspace sizing.

Signed-off-by: Pranav Shrestha <254760092+pranav-nvidia@users.noreply.github.com>
@pranav-nvidia pranav-nvidia changed the title [https://nvbugs/6565412][fix] Size the trtllm-gen multi-CTA KV counter buffer for beam search [https://nvbugs/6565412][fix] Size trtllm-gen and thop decode buffers for beam search Aug 6, 2026

@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)
cpp/tensorrt_llm/thop/attentionOp.cpp (1)

338-343: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Initialize RunnerBase state.

RunnerBase::max_num_sequences is a new externally visible scalar with no constructor or in-class initializer. Initialize it together with the other runner capacity fields so a default-constructed RunnerBase cannot expose indeterminate state.

As per coding guidelines, initialize externally visible class members in the constructor.

🤖 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 `@cpp/tensorrt_llm/thop/attentionOp.cpp` around lines 338 - 343, Initialize
RunnerBase::max_num_sequences in the constructor alongside beam_width,
max_num_requests, and attention_window_size, ensuring default-constructed
RunnerBase instances expose a defined value.

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 `@cpp/tensorrt_llm/thop/attentionOp.cpp`:
- Around line 338-343: Initialize RunnerBase::max_num_sequences in the
constructor alongside beam_width, max_num_requests, and attention_window_size,
ensuring default-constructed RunnerBase instances expose a defined value.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 9c0f0b25-c806-4177-badb-2fc3dc7c298c

📥 Commits

Reviewing files that changed from the base of the PR and between bf36ce7 and b9b2b13.

📒 Files selected for processing (6)
  • cpp/tensorrt_llm/nanobind/thop/bindings.cpp
  • cpp/tensorrt_llm/thop/attentionOp.cpp
  • cpp/tensorrt_llm/thop/attentionOp.h
  • tensorrt_llm/_torch/attention_backend/fmha/fallback.py
  • tensorrt_llm/_torch/attention_backend/fmha/flashinfer_trtllm_gen.py
  • tests/unittest/_torch/attention/test_fmha_page_index.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • tensorrt_llm/_torch/attention_backend/fmha/flashinfer_trtllm_gen.py
  • tests/unittest/_torch/attention/test_fmha_page_index.py

@pranav-nvidia

Copy link
Copy Markdown
Contributor Author

/bot run --disable-fail-fast

@pranav-nvidia
pranav-nvidia requested a review from yuxianq August 6, 2026 21:05
@pranav-nvidia

Copy link
Copy Markdown
Contributor Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64643 [ run ] triggered by Bot. Commit: b9b2b13 Link to invocation

@pranav-nvidia
pranav-nvidia enabled auto-merge (squash) August 7, 2026 16:14
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@pranav-nvidia

Copy link
Copy Markdown
Contributor Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64714 [ run ] triggered by Bot. Commit: b9b2b13 Link to invocation

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@pranav-nvidia

Copy link
Copy Markdown
Contributor Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64725 [ run ] triggered by Bot. Commit: 3082eef Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64725 [ run ] completed with state SUCCESS. Commit: 3082eef
/LLM/main/L0_MergeRequest_PR pipeline #52577 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

@pranav-nvidia

Copy link
Copy Markdown
Contributor Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64736 [ run ] triggered by Bot. Commit: 3082eef Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64736 [ run ] completed with state SUCCESS. Commit: 3082eef
/LLM/main/L0_MergeRequest_PR pipeline #52587 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

@pranav-nvidia

Copy link
Copy Markdown
Contributor Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #65070 [ run ] triggered by Bot. Commit: 3082eef Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #65070 [ run ] completed with state SUCCESS. Commit: 3082eef
/LLM/main/L0_MergeRequest_PR pipeline #52876 completed with status: 'UNSTABLE'

CI Report

⚠️ Multi-GPU Label Required:
Multi-GPU tests require the ci: full pre-merge approved label on this PR. Ask a member of NVIDIA/trt-llm-ci-approvers to add the label, then re-trigger CI with the same bot command (no rebase needed).

⚠️ 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

Link to invocation

@pranav-nvidia

Copy link
Copy Markdown
Contributor Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #65107 [ run ] triggered by Bot. Commit: 3082eef Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #65107 [ run ] completed with state SUCCESS. Commit: 3082eef
/LLM/main/L0_MergeRequest_PR pipeline #52907 completed with status: 'SUCCESS'
Pipeline passed with automatic retried tests. Check the rerun report for details.

CI Report

Link to invocation

@pranav-nvidia
pranav-nvidia merged commit c1a8a1c into NVIDIA:main Aug 11, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants