Skip to content

[None][fix] Restart Router Replay decode positions after a recompute - #22392

Closed
arusli-nv wants to merge 1 commit into
NVIDIA:mainfrom
arusli-nv:ft/F11-route-capture-recompute
Closed

arusli-nv wants to merge 1 commit into
NVIDIA:mainfrom
arusli-nv:ft/F11-route-capture-recompute

Conversation

@arusli-nv

@arusli-nv arusli-nv commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Dev Engineer Review

  • tensorrt_llm/_torch/route_capture.py (+12/-8, per the diff stat): The context phase now sets the per-request decode counter to 0 with self._gen_count[rid] = 0. Previously it used setdefault(rid, 0), so the counter survived a re-prefill. This change is the fix for decode positions after a recompute pause.
  • Decode positions now use int(req.prompt_len) + self._gen_count.get(rid, 0). The old code used getattr(req, "py_prompt_len", 0). The change depends on the live prompt_len being correct after a pause. The diff does not show how prompt_len is maintained, so that dependency is unverified here.
  • The getattr default of 0 is removed. req.prompt_len is now read directly. A request object without this attribute would now raise AttributeError instead of falling back to 0. Confirm that every request type reaching this path defines prompt_len.
  • The overlap-scheduler comment is rewritten. The new comment drops the earlier KL-divergence measurement details (0.0007 to ~1.0). The behavior it describes is unchanged.
  • Beam width greater than one stays out of scope, per the PR objectives. The diff does not change beam handling.
  • Helix context parallelism: the PR description says the change is unchanged there because both lengths hold the same rank-local value. This is not verified by the diff.

QA Engineer Review

Test files changed:

  • tests/unittest/_torch/test_route_capture.py: +70 lines, no removals. Two new recompute tests, per the PR objectives and the raw summary. Test function names were not visible in the diff stat, so they are not listed here.
  • The PR reports the file passes 24 tests. It also reports the two new tests fail without the fix, and the stale-prompt test still fails with only the counter reset. These results come from the PR description, not from a CI run I could check.
  • tests/unittest/llmapi/test_return_routed_experts.py was run by the author but skipped because its model was unavailable. It is not covered.
  • Test-list entries in tests/integration/test_lists/test-db/ or qa/: not established from the diff.

Coverage verdict: needs follow-up. The new tests cover the two reported causes. The llmapi test was skipped, and CI results for the current head were not available.

Per-File QA Perspective

  • tensorrt_llm/_torch/route_capture.py: Verify that routed-expert positions restart after a pause-and-recompute on a request that has already generated tokens. Verify that routes stay complete and assemble() no longer raises ValueError in that case.
  • tests/unittest/_torch/test_route_capture.py: Covers recompute after decoding, with both a refreshed and a stale py_prompt_len. Confirm these tests are listed in the appropriate test-db CI list. Not verified here.

Description

Bug. Router Replay (RouteCapture, enable_return_routed_experts) gives each decode row of a request the
position py_prompt_len + gen_count, where gen_count counts the request's processed decode tokens. When an opted-in
request has decoded and is then paused and prefilled again, that position is wrong:

  • The counter survives the re-prefill. The re-prefilled prompt already holds the generated tokens, so every later
    decode row lands past the end of the sequence. assemble() then finds a gap and raises ValueError when the
    request finishes, which fails the request batch (non-overlap loop) or stops the executor loop (overlap loop).
  • py_prompt_len can be stale. A plain pause with KV cache manager V1 grows the C++ prompt length but does not
    refresh py_prompt_len, so the later decode rows shift onto earlier positions and the returned routes are silently
    short. No error is raised.

Who hits it. Any opted-in request that is recomputed after at least one decode step:

  • KV cache manager V2's recompute pause under KV cache pressure (V2 is the default);
  • a connector-deferred preemption;
  • recompute_active_requests after a weight update.

Fix. In RouteCapture._prepare:

  • A context phase sets the request's decode counter to 0. Decode positions restart after the prompt that this context
    phase prefills.
  • Decode positions read the live req.prompt_len instead of py_prompt_len.
  • Under helix context parallelism both lengths hold the same rank-local value, so that path is unchanged.

Out of scope. Beam width > 1: a pause there trims the tokens back to the original prompt. Router Replay landed
in #18397.

Risk. Low. Requests that are never recomputed get the same positions as before. Only recomputed requests change,
and they now return complete routes instead of failing or returning short ones.

Test Coverage

tests/unittest/_torch/test_route_capture.py (CPU), two new tests. Their fake router rows carry each token's true
position, so a shifted or skipped position is visible in the assembled routes:

  • test_recompute_restarts_decode_positions_after_the_grown_prompt: prefill, decodes, a recompute re-prefill of the
    grown prompt, more decodes. The assembled routes cover prompt + generated - 1 positions in order.
  • test_recompute_reads_the_live_prompt_length_when_py_prompt_len_is_stale: the same with a stale py_prompt_len
    (the KV cache manager V1 pause).

With and without the fix, on main's code:

  • Without the fix, both new tests fail and the other 22 tests of the file pass.
  • With only the counter reset, the stale-prompt test still fails.
  • With the fix, all 24 pass.
  • tests/unittest/llmapi/test_return_routed_experts.py was also run; it was skipped because its model is not
    available in our environment.
pytest tests/unittest/_torch/test_route_capture.py

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.

RouteCapture derives each decode row's position from the request's prompt
length plus a per-request decode counter. Two things broke that position once
an opted-in request is paused after it has decoded and later re-prefilled
(KV cache manager V2 recompute under KV pressure, connector-deferred
preemption, recompute_active_requests after a weight update):

- The counter survived the re-prefill. The re-prefilled prompt already holds
  the generated tokens, so every later decode row landed past the end of the
  sequence; assemble() then found a gap and raised ValueError when the request
  finished, which fails the request batch or stops the executor loop.
- The position read py_prompt_len, which a plain pause (KV cache manager V1)
  does not refresh while the C++ prompt length grows. The later decode rows
  were shifted onto earlier positions and the routes silently came back short.

A context phase now resets the request's decode counter, and decode positions
use the live prompt_len. Under helix context parallelism both lengths hold the
same rank-local value, so that path is unchanged.

Beam width > 1 is out of scope: a pause there trims the tokens back to the
original prompt. Router Replay itself landed in NVIDIA#18397.

Tests: a recompute after decoding, with a refreshed and with a stale
py_prompt_len; both fail without this change, and the stale case also fails
with the counter reset alone.

Signed-off-by: Andrew Rusli <[email protected]>
@arusli-nv

Copy link
Copy Markdown
Collaborator Author

/bot run

@coderabbitai

coderabbitai Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Route capture now resets per-request generation counts during context phases and calculates decode positions from the live prompt length. New tests cover route positions after pause and re-prefill, including cases where py_prompt_len is stale.

Changes

Route capture position tracking

Layer / File(s) Summary
Context reset and decode position tracking
tensorrt_llm/_torch/route_capture.py, tests/unittest/_torch/test_route_capture.py
Context phases reset each request’s generation count. Decode positions use req.prompt_len plus the generation count. Tests verify positions across prefill, decode, pause, and re-prefill, with refreshed and stale py_prompt_len.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related issues

  • Issue 17778 — The RFC calls for per-request route rows indexed by absolute position and idempotent preemption and recompute. This change corrects decode positions after recompute.

Suggested reviewers: juney-nvidia, bowenfu, shikicloud

Merge Risk: Minimal · up to ab998

This change corrects Router Replay decode positions after a request is paused and re-prefilled. Regression tests cover both the refreshed and the stale prompt-length cases. No remaining merge-blocking risk was identified.

Security architecture risk: Minimal · up to ab998

This fix restores correct results after a request is recomputed without expanding access, privileges, or exposed interfaces. No introduced or worsened security or failure-containment risk was established.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: Router Replay decode positions are restarted after a recompute, which matches the RouteCapture._prepare fix (decode counter reset on context phase, live req.prompt_len…
Description check Passed The description follows the required structure: it explains the bug and its causes, gives the fix, lists scope and risk, and covers test coverage with the exact pytest command. The author checked the …
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.
Full details: Title check

Explanation

The title names the main change: Router Replay decode positions are restarted after a recompute, which matches the RouteCapture._prepare fix (decode counter reset on context phase, live req.prompt_len for decode positions) and the new recompute tests. It follows the [None][fix] template format.

Full details: Description check

Explanation

The description follows the required structure: it explains the bug and its causes, gives the fix, lists scope and risk, and covers test coverage with the exact pytest command. The author checked the PR checklist. The template's ticket-prefixed title and the 'Description' and 'Test Coverage' headings are present. The required title format is met by the PR title itself, which uses [None][fix]. The template's HTML comments were removed, which is acceptable. No API change is declared, which is consistent with the raw summary.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between b9a6b5b and ab998f2.

Files selected for processing: 2

  • tensorrt_llm/_torch/route_capture.py
  • tests/unittest/_torch/test_route_capture.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 2 remain after this review.

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #85241 [ run ] triggered by Bot. Commit: ab998f2 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

CI Report

Link to invocation

@arusli-nv

Copy link
Copy Markdown
Collaborator Author

Superseded by #22507, which combines the runtime and serving fixes (#22381, #22382, #22384, #22386, #22392, #22393) for one review.

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.

2 participants