Skip to content

[TRTLLM-10030][perf] avoid syncs in beam search + other improvements#11349

Merged
ixlmar merged 2 commits intoNVIDIA:mainfrom
ixlmar:perf/beam-search-3
Feb 9, 2026
Merged

[TRTLLM-10030][perf] avoid syncs in beam search + other improvements#11349
ixlmar merged 2 commits intoNVIDIA:mainfrom
ixlmar:perf/beam-search-3

Conversation

@ixlmar
Copy link
Collaborator

@ixlmar ixlmar commented Feb 6, 2026

Description

Note: Commits up to and including 15087b5 are shared with #11341.

Details:

  • defer beam-history construction decision until update_requests (avoids sync in sample_async)
  • do not access device tensors in update_requests (avoids sync)
  • remove obsolete code from _convert_logprobs_tensor_to_list
  • minor optimizations / improvements

Test Coverage

n/a

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)

  • 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

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

Details

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.

Summary by CodeRabbit

Release Notes

  • Refactor
    • Optimized CUDA memory handling using pinned buffers to improve data transfer efficiency.
    • Refactored beam history construction to use deferred initialization for improved resource management.

@ixlmar ixlmar requested review from Funatiq and stnie February 6, 2026 15:33
@ixlmar ixlmar requested a review from a team as a code owner February 6, 2026 15:33
@ixlmar
Copy link
Collaborator Author

ixlmar commented Feb 6, 2026

/bot run --disable-multi-gpu-test

@ixlmar
Copy link
Collaborator Author

ixlmar commented Feb 6, 2026

/bot run

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 6, 2026

📝 Walkthrough

Walkthrough

This PR introduces lazy beam history construction in the torch sampler using a builder pattern, where BeamHistoryBuilder callables replace eager BeamHistory objects. Additionally, tensor creation in model_engine.py is optimized to use CPU-pinned memory before CUDA transfer.

Changes

Cohort / File(s) Summary
Tensor Creation Optimization
tensorrt_llm/_torch/pyexecutor/model_engine.py
Modified tensor creation path to construct CPU-pinned tensor before non-blocking CUDA transfer instead of directly creating CUDA tensor.
Beam History Builder Pattern
tensorrt_llm/_torch/pyexecutor/sampler.py
Introduced BeamHistoryBuilder type alias and refactored beam history construction from eager to deferred. Renamed _create_beam_history to _prepare_beam_history to return a builder callable. Updated _get_logprobs_from_request signature with pin_memory and preallocate_extra_steps parameters. Changed beam_histories to beam_history_builders in SampleStateTorch. Added _maybe_build_beam_history helper and asynchronous finish reason handling.
Test Adaptation
tests/unittest/_torch/sampler/test_beam_search.py
Updated test to use new builder pattern: call _prepare_beam_history to get builder, then invoke it. Adjusted tensor shape expectations and added device-correctness fix for cumulative logprobs comparison.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 52.38% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The PR title clearly summarizes the main change: avoiding synchronizations in beam search with other improvements, matching the primary objectives.
Description check ✅ Passed The PR description provides concrete implementation details (defer beam-history construction, avoid device tensor access, remove obsolete code) but lacks comprehensive test coverage details.

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

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: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
tensorrt_llm/_torch/pyexecutor/sampler.py (1)

1745-1756: ⚠️ Potential issue | 🟠 Major

Fix slicing logic to handle preallocate_extra_steps=0 correctly.

When preallocate_extra_steps=0, the slice logprobs_tensor_full[:, :-0, :] produces an empty tensor (equivalent to [:, :0, :]), causing the filling loop to be skipped and the function to return uninitialized tensors. Although the current sole caller passes preallocate_extra_steps=1 explicitly, the default value of 0 in the function signature creates a correctness trap for future use.

Apply the proposed fix to check if preallocate_extra_steps > 0 before slicing:

Fix
-        logprobs_tensor = logprobs_tensor_full[:, :-preallocate_extra_steps, :]
-        logprobs_indices_tensor = logprobs_indices_tensor_full[:, :-preallocate_extra_steps, :]
+        if preallocate_extra_steps > 0:
+            logprobs_tensor = logprobs_tensor_full[:, :-preallocate_extra_steps, :]
+            logprobs_indices_tensor = logprobs_indices_tensor_full[:, :-preallocate_extra_steps, :]
+        else:
+            logprobs_tensor = logprobs_tensor_full
+            logprobs_indices_tensor = logprobs_indices_tensor_full
🧹 Nitpick comments (1)
tensorrt_llm/_torch/pyexecutor/sampler.py (1)

1280-1280: Static analysis: zip() without strict= parameter.

Ruff flags this zip(token_list[beam_idx], logprobs_list[beam_idx]) as missing an explicit strict= parameter. Since both lists are derived from tensors of the same shape, strict=True would be appropriate and would catch shape mismatches early.

Proposed fix
-            for topk_token, topk_logprob in zip(token_list[beam_idx], logprobs_list[beam_idx]):
+            for topk_token, topk_logprob in zip(token_list[beam_idx], logprobs_list[beam_idx], strict=True):

@tensorrt-cicd
Copy link
Collaborator

PR_Github #35114 [ run ] triggered by Bot. Commit: 077b567

@tensorrt-cicd
Copy link
Collaborator

PR_Github #35115 [ run ] triggered by Bot. Commit: 077b567

@tensorrt-cicd
Copy link
Collaborator

PR_Github #35114 [ run ] completed with state ABORTED. Commit: 077b567

@ixlmar
Copy link
Collaborator Author

ixlmar commented Feb 6, 2026

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Collaborator

PR_Github #35139 [ run ] triggered by Bot. Commit: 077b567

@tensorrt-cicd
Copy link
Collaborator

PR_Github #35115 [ run ] completed with state ABORTED. Commit: 077b567
LLM/main/L0_MergeRequest_PR #27108 (Blue Ocean) completed with status: ABORTED

@tensorrt-cicd
Copy link
Collaborator

PR_Github #35139 [ run ] completed with state SUCCESS. Commit: 077b567
/LLM/main/L0_MergeRequest_PR pipeline #27130 completed with status: 'FAILURE'

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

Copy link
Collaborator

@Funatiq Funatiq left a comment

Choose a reason for hiding this comment

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

remove obsolete code from _convert_logprobs_tensor_to_list

Is this independent of the beam-history changes? It would have been easier to review if these were separate commits.

Could you please add some perf numbers for this improvement?

Signed-off-by: ixlmar <206748156+ixlmar@users.noreply.github.com>
@ixlmar ixlmar force-pushed the perf/beam-search-3 branch from 077b567 to 918164e Compare February 9, 2026 09:30
@ixlmar
Copy link
Collaborator Author

ixlmar commented Feb 9, 2026

In benchmarks using a 1B parameter model and short output sequence lengths, the performance advantage of TorchSampler over TRTLLMSampler increased from 16 % to 31 % upon incorporating those changes. For long output sequences, a performance advantage of 13 % was observed for the particular configuration tested.

Signed-off-by: ixlmar <206748156+ixlmar@users.noreply.github.com>
@ixlmar
Copy link
Collaborator Author

ixlmar commented Feb 9, 2026

/bot run --disable-fail-fast

@ixlmar ixlmar requested a review from Funatiq February 9, 2026 09:58
@tensorrt-cicd
Copy link
Collaborator

PR_Github #35318 [ run ] triggered by Bot. Commit: 5d26128

@ixlmar
Copy link
Collaborator Author

ixlmar commented Feb 9, 2026

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Collaborator

PR_Github #35320 [ run ] triggered by Bot. Commit: 5d26128

@ixlmar
Copy link
Collaborator Author

ixlmar commented Feb 9, 2026

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Collaborator

PR_Github #35320 [ run ] completed with state SUCCESS. Commit: 5d26128
/LLM/main/L0_MergeRequest_PR pipeline #27275 completed with status: 'FAILURE'

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

@tensorrt-cicd
Copy link
Collaborator

PR_Github #35342 [ run ] triggered by Bot. Commit: 5d26128

@tensorrt-cicd
Copy link
Collaborator

PR_Github #35342 [ run ] completed with state SUCCESS. Commit: 5d26128
/LLM/main/L0_MergeRequest_PR pipeline #27294 completed with status: 'SUCCESS'

@ixlmar ixlmar merged commit 196d94a into NVIDIA:main Feb 9, 2026
5 checks passed
@ixlmar ixlmar deleted the perf/beam-search-3 branch February 9, 2026 15:14
inciaf pushed a commit to inciaf/trtllm-energy-monitoring that referenced this pull request Feb 18, 2026
…VIDIA#11349)

Signed-off-by: ixlmar <206748156+ixlmar@users.noreply.github.com>
Signed-off-by: Ahmet Inci <ainci@nvidia.com>
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

Comments