Skip to content

[TRTLLM-13582][feat] Reduce context-phase host overhead in input prep - #15584

Closed
Wanli-Jiang wants to merge 1 commit into
NVIDIA:mainfrom
Wanli-Jiang:user/williamj/fix-request-gap
Closed

Wanli-Jiang wants to merge 1 commit into
NVIDIA:mainfrom
Wanli-Jiang:user/williamj/fix-request-gap

Conversation

@Wanli-Jiang

@Wanli-Jiang Wanli-Jiang commented Jun 24, 2026 •

Copy link
Copy Markdown
Collaborator

Improvement measurement

Breakdown in the code path

GPU-idle ms per host stage, rank-1 worker, Super-120B 50k CTX bs=1.
"Baseline (main)" already includes #15625's route fix; "Optimized (PR)" adds C1+C2.
Sub-stages are the NVTX ranges actually present in the trace; route+gather+bcast is
_fetch_new_requests − merge_requests (route_requests / gather_all_rank_states have no
dedicated NVTX marker in this build — the original report added those via separate
fetchinstr instrumented runs).

                              BASELINE(main)   OPTIMIZED(PR)     notes
_fetch_new_requests              1.28 ms          1.22 ms     ~flat — already route-optimized in main
  ├─ merge_requests              0.82 ms          0.82 ms     LlmRequest construction (C++ token copy) — untouched
  └─ route+gather+bcast          ~0.46ms          ~0.40ms     #15625 uses num_input_tokens accessor → route≈0
                                                              (this is the old C3 target; now in main, not the PR)
_prepare_inputs                  6.49 ms          2.06 ms     C1+C2: list-build + H2D convert + position_ids arange
  └─ attn/mamba metadata         ~0.3 ms          ~0.3 ms     already cheap (Mamba2Metadata.prepare)
prepare_resources                1.56 ms          1.50 ms     mamba/KV-cache resource prep — untouched
_handle_responses                0.33 ms          0.30 ms     (≈+KV xfer in TRUE disagg)
────────────────────────────────────────────────────────────
TOTAL host bubble               ~9.66 ms         ~5.09 ms     (−47%)
forward burst                   ~800 ms (Super) / ~785 ms (real Ultra)   [B300 — far faster than GB200]
bubble % of iteration           ~1.2% (Super) / ~0.7% (real Ultra)

vs the original report: baseline _fetch_new_requests is now 1.28 ms, not 12.2 ms —
main's #15625 replaced len(input_token_ids) with the cheap num_input_tokens accessor
(route ≈0, better than C3's 3.7 ms "1× floor"). Route no longer appears as a PR delta;
the PR's entire contribution is _prepare_inputs.

Before/after measurement

Scenario host bubble before→after dominant stage throughput
Super-120B, 50k CTX, bs=1 9.66 → 5.09 ms (−47 %) _prepare_inputs 6.49→2.06 (−68 %) +0.5 % (noise)
Real Ultra-550B, 50k CTX, bs=1 9.55 → 5.24 ms (−45 %) _prepare_inputs 6.59→2.26 (−66 %); _fetch ~flat +0.1 % (noise)
Super-120B, 8k CTX, bs=1 3.44 → 2.83 ms (−18 %) _prepare_inputs 1.71→0.97 (−43 %) +0.3 % (noise)
Super-120B, 1k CTX, bs=1 ~marginal (not re-measured) tiny host work noise
Super-120B, 1k, bs=16 not re-measured — —

Summary by CodeRabbit

  • New Features
    • Improved request scheduling by using precomputed input lengths for more consistent token-based balancing.
  • Performance
    • Reduced overhead when preparing input and position data, which may improve request processing speed.
    • Cached request length calculations to avoid repeated work during execution.

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.

@Wanli-Jiang
Wanli-Jiang requested a review from a team as a code owner June 24, 2026 09:15
@Wanli-Jiang
Wanli-Jiang requested a review from Tabrizian June 24, 2026 09:15
@coderabbitai

coderabbitai Bot commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a functools.cached_property input_length on RequestQueueItem and updates DefaultADPRouter to use it. Separately introduces a _copy_int_list_to_device helper in model_engine.py that uses array.array + torch.frombuffer for faster host-to-device int-buffer transfers, replacing two torch.tensor(list) call sites in _prepare_tp_inputs.

Changes

Scheduling and Model Engine Performance Optimizations

Layer / File(s) Summary
RequestQueueItem.input_length cached property and ADP router usage
tensorrt_llm/_torch/pyexecutor/executor_request_queue.py, tensorrt_llm/_torch/pyexecutor/scheduler/adp_router.py
RequestQueueItem gains an input_length cached property backed by functools.cached_property that calls len(request.input_token_ids) once and caches the result. DefaultADPRouter._balance_requests_across_ranks replaces its inline len(getattr(..., "input_token_ids", [])) expressions with req_item.input_length for both sorting and heap token accounting.
_copy_int_list_to_device helper and _prepare_tp_inputs call sites
tensorrt_llm/_torch/pyexecutor/model_engine.py
Adds a module-level _copy_int_list_to_device(values, dst) that converts a List[int] to an array.array('i', ...), wraps it with torch.frombuffer, and copies it into dst[:len(values)] non-blocking. The two existing torch.tensor(list, pin_memory=prefer_pinned()) + copy_ patterns for input_ids and position_ids in _prepare_tp_inputs are replaced with calls to this helper.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description omits the required short issue/solution summary and has no Test Coverage section in the template format. Add a brief Description and Test Coverage section, and keep the PR Checklist in the requested template structure.
✅ Passed checks (4 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 is concise and accurately reflects the main performance-focused change in the PR.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

@Wanli-Jiang
Wanli-Jiang force-pushed the user/williamj/fix-request-gap branch from 728bafc to 5e8eaf3 Compare June 24, 2026 09:25
@Wanli-Jiang

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #55476 [ run ] triggered by Bot. Commit: 5e8eaf3 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #55476 [ run ] completed with state SUCCESS. Commit: 5e8eaf3
/LLM/main/L0_MergeRequest_PR pipeline #44403 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

@Wanli-Jiang
Wanli-Jiang force-pushed the user/williamj/fix-request-gap branch from 5e8eaf3 to e0e197e Compare June 25, 2026 02:44
@Wanli-Jiang Wanli-Jiang changed the title [None][feat] Reduce context-phase host overhead in input prep and ADP request routing [TRTLLM-13582][feat] Reduce context-phase host overhead in input prep and ADP request routing Jun 25, 2026
@Wanli-Jiang
Wanli-Jiang force-pushed the user/williamj/fix-request-gap branch 2 times, most recently from 52a3dee to d10295f Compare June 25, 2026 05:56
@Wanli-Jiang

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #55714 [ run ] triggered by Bot. Commit: d10295f Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@Tabrizian

Copy link
Copy Markdown
Member

@Wanli-Jiang it looks similar to some of the changes in feat/deepseek_v4 branch: #15584

I wonder how we should reconcile.

@Wanli-Jiang

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #55991 [ run ] triggered by Bot. Commit: d10295f Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@Wanli-Jiang

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56051 [ run ] triggered by Bot. Commit: d10295f Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@Wanli-Jiang
Wanli-Jiang force-pushed the user/williamj/fix-request-gap branch from d10295f to e602e51 Compare June 28, 2026 06:58
@Wanli-Jiang

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56214 [ run ] triggered by Bot. Commit: e602e51 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

Reduce host-side input preparation cost in the pure-context (prefill)
path of _prepare_tp_inputs:
- Build pure-context position_ids directly on-device with torch.arange,
  skipping the Python list build and its host->device copy. Guarded to
  the case with no generation request, no MRoPE, and zero position
  offset; all other cases fall back to the list path unchanged.
- Stage input_ids/position_ids copies via array.array + torch.frombuffer
  instead of torch.tensor(<list>), faster for long sequences. The staging
  helper is a no-op on an empty list.

Verified value-preserving (bit-identical generated tokens) across decode,
in-flight batching, chunked prefill, and prefix-cache / block-reuse.

Note: the original PR NVIDIA#15584 also reduced ADP request-routing overhead,
but that optimization was independently landed on main via NVIDIA#15625
(_num_input_tokens uses the cheap num_input_tokens accessor), so the
adp_router.py / executor_request_queue.py changes are dropped on rebase.

Signed-off-by: Wanli Jiang <[email protected]>
@Wanli-Jiang
Wanli-Jiang force-pushed the user/williamj/fix-request-gap branch from e602e51 to 08467f3 Compare July 7, 2026 07:12
@Wanli-Jiang Wanli-Jiang changed the title [TRTLLM-13582][feat] Reduce context-phase host overhead in input prep and ADP request routing [TRTLLM-13582][feat] Reduce context-phase host overhead in input prep Jul 7, 2026
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