Skip to content

[None][perf] Run the executor loop under a CPython frame-stack reserve - #22396

Merged
dhansen-nvidia merged 1 commit into
NVIDIA:mainfrom
dhansen-nvidia:perf/frame-stack-reserve
Nov 25, 2026
Merged

dhansen-nvidia merged 1 commit into
NVIDIA:mainfrom
dhansen-nvidia:perf/frame-stack-reserve

Conversation

@dhansen-nvidia

@dhansen-nvidia dhansen-nvidia commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

CPython 3.11+ keeps frames in per-thread data-stack chunks. When a hot call sits at a chunk boundary, every call maps a fresh chunk and every return unmaps it (gh-142183). On GB200 each munmap also pays a Grace SMMU invalidation once NCCL has peer-mapped memory, so the churn costs milliseconds per executor step: MiniMax-M3 piecewise CUDA graph prefill steps spent about half their host time in munmap, all on the executor thread. CPython fixed this in 3.13.13 and 3.14.4; 3.12 will not get the fix.

Add run_with_frame_stack_reserve() and call PyExecutor.event_loop through it, so the loop starts near the beginning of a large data-stack chunk and ordinary call depths never reach a boundary. The reserve is applied only on affected CPython versions and can be disabled with TLLM_DISABLE_FRAME_STACK_RESERVE=1. Cover the version gate, the disable switch, the churn itself and the executor wiring with cpu_only unit tests.

Background:

Dev Engineer Review

  • tensorrt_llm/_utils.py adds run_with_frame_stack_reserve(fn, /, *args, **kwargs). It forwards arguments, returns the result, and propagates exceptions unchanged.
  • At import, when _needs_frame_stack_reserve() is true, the module sets run_with_frame_stack_reserve.__code__ to a copy with co_stacksize=65536. This patches a code object at runtime and depends on CPython internals.
  • The gate covers CPython 3.11 up to 3.13.13 (exclusive) and 3.14 up to 3.14.4 (exclusive). It uses tuple comparison. If a backport uses different patch numbers, the gate gives the wrong result.
  • TLLM_DISABLE_FRAME_STACK_RESERVE is read only at import, and only the exact value "1" disables the reserve. Changing it after import has no effect.
  • The docstring states that a frame object built for the wrapper, such as one for a traceback or profiler, also allocates the reserved slots. The memory cost of this should be confirmed as acceptable.
  • py_executor.py changes one call: _event_loop_wrapper now calls run_with_frame_stack_reserve(self.event_loop). The import list is updated to match. The profiler and GC context managers are unchanged.
  • On other interpreters and on CPython versions outside the gated ranges, the wrapper is a plain call.

QA Engineer Review

Test files added:

  • tests/unittest/utils/test_frame_stack_reserve.py (+145). It covers argument and keyword forwarding, exception propagation, the version gate, import-time application, and the disable variable. On Linux, when the reserve is needed, it runs a thread-local minor-fault churn test.
  • tests/unittest/_torch/pyexecutor/test_event_loop_frame_stack_reserve.py (+48). It checks that event_loop runs directly under the reserve function's code object.

Test-list changes:

  • tests/integration/test_lists/test-db/l0_cpu.yml adds unittest/utils/test_frame_stack_reserve.py.
  • The executor wiring test is not listed in any inspected test list. CI execution of it is not confirmed.

Coverage verdict: needs follow-up. The utils tests cover the gate, disable switch, and forwarding. The executor wiring test has no list entry. The churn test runs only on affected Linux interpreters.

Per-File QA Perspective

  • tensorrt_llm/_utils.py: Verify the code-object patch applies only on gated CPython versions, and that TLLM_DISABLE_FRAME_STACK_RESERVE=1 disables it.
  • tensorrt_llm/_torch/pyexecutor/py_executor.py: Verify executor startup, shutdown, and error handling on affected CPython versions.
  • tests/integration/test_lists/test-db/l0_cpu.yml: Adds the utils test to the CPU list.
  • tests/unittest/utils/test_frame_stack_reserve.py: Verify that CI runs it.
  • tests/unittest/_torch/pyexecutor/test_event_loop_frame_stack_reserve.py: Not in any test list. Add it to a test-db/ list or confirm which list runs it.

Description

Test Coverage

  • tests/unittest/utils/test_frame_stack_reserve.py (cpu_only, added to l0_cpu.yml) covers:
    • argument and keyword forwarding (fn is positional-only), the return value, and unchanged exception propagation;
    • the version gate at each boundary (3.10, 3.11, 3.13.12/3.13.13, 3.14.3/3.14.4, 3.15, a full sys.version_info tuple, non-CPython);
    • the reserve being applied at import exactly when the gate holds, and TLLM_DISABLE_FRAME_STACK_RESERVE=1 turning it off at import (one subprocess);
    • a minor-page-fault sweep showing data-stack churn without the reserve and none with it (runs only on affected interpreters on Linux; counts per-thread faults, so host load does not affect it).
  • tests/unittest/_torch/pyexecutor/test_event_loop_frame_stack_reserve.py (cpu_only, picked up by the existing unittest/_torch/pyexecutor entry in l0_cpu.yml) checks that the real reserve frame is the event loop's direct caller.
  • GPU A/B validation on GB200 within one build (reserve on vs. TLLM_DISABLE_FRAME_STACK_RESERVE=1):
    • greedy-output equality for Llama-3.1-8B and DeepSeek-V3-Lite on the eager, PCG and BCG arms (c1: 32/32 identical, logprob delta 0);
    • a Llama-8B PCG serving A/B: TTFT p50 -48% at ISL 256 / OSL 128, unchanged at ISL 3072;
    • a MiniMax-M3 TP4 same-node ABBA prefill sweep on PCG and BCG;
    • RSS, thread-count and per-thread minor-fault sampling.

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.

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

A utility wrapper conditionally reserves frame-stack capacity based on the CPython version and an environment variable. PyExecutor._event_loop_wrapper calls self.event_loop through that wrapper. CPU-only tests cover the wrapper and executor call path.

Changes

Frame-stack reserve

Layer / File(s) Summary
Add and validate the reserve wrapper
tensorrt_llm/_utils.py, tests/unittest/utils/test_frame_stack_reserve.py
Adds a wrapper that forwards arguments and returns results. Version checks and an environment variable control whether import-time code sets the wrapper’s stack size to 65,536. Tests cover call behavior, version gating, disabling the reserve, and Linux minor-fault measurements.
Invoke the event loop through the wrapper
tensorrt_llm/_torch/pyexecutor/py_executor.py, tests/unittest/_torch/pyexecutor/test_event_loop_frame_stack_reserve.py, tests/integration/test_lists/test-db/l0_cpu.yml
The executor invokes self.event_loop through the wrapper while retaining the existing profiler and garbage-collection contexts. A CPU-only test checks the call path, and the CPU test list includes the utility test.

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

Change: Refactor

Merge Risk: Minimal · up to edeeb

This change runs the executor event loop through a small wrapper that reserves frame-stack space on affected CPython versions, and it can be disabled with an environment variable. No concrete merge-blocking risk was identified.

Security architecture risk: Minimal · up to edeeb

The change adjusts stack allocation on the existing executor thread without adding a request entrypoint or changing who can invoke the executor. Existing error handling and cleanup remain in place, and operators can disable the reserve before the process loads it.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The effective reserve is scoped to calls through the helper, with one production call site in the inspected package. Import changes the helper's code object but does not reserve stack capacity for unrelated calls.

Trust Boundaries and Controls

  • observed — The executor supplies its own bound event-loop method, not a callable selected from request input. Reserve selection reads the process environment and interpreter version at import, not request data.

Resilience and Maintainability Implications

  • observed — The enclosing worker still distinguishes incomplete execution from errors after normal completion, preserves the separate interruption path, and runs its existing cleanup. The new helper does not take over failure handling or retry the loop.
Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the executor loop now runs under a CPython frame-stack reserve. It follows the repository's [ticket][type] Summary format with [None][perf]. The title is concise and s…
Description check Passed The description follows the repository template. It has a Description section that explains the CPython data-stack churn and the reserve fix, and it names the disable switch. It has a detailed Test Co…
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: the executor loop now runs under a CPython frame-stack reserve. It follows the repository's [ticket][type] Summary format with [None][perf]. The title is concise and specific, and it matches the summary and objectives.

Full details: Description check

Explanation

The description follows the repository template. It has a Description section that explains the CPython data-stack churn and the reserve fix, and it names the disable switch. It has a detailed Test Coverage section that lists the cpu_only unit tests and the reported GPU validation. The PR Checklist is present and checked. The template's Description section is brief, and the description does not state an API-change label decision. These are minor gaps. The GPU results are author-reported and were not verified here.

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 7a843f6 and edeeb33.

Files selected for processing: 3

  • tensorrt_llm/_torch/pyexecutor/py_executor.py
  • tensorrt_llm/_utils.py
  • tests/integration/test_lists/test-db/l0_cpu.yml

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

@dhansen-nvidia
dhansen-nvidia force-pushed the perf/frame-stack-reserve branch from c9576df to e3bdad2 Compare November 17, 2026 20:38
@dhansen-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #85556 [ run ] triggered by Bot. Commit: e3bdad2 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@chienchunhung chienchunhung left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the PR!

@dhansen-nvidia
dhansen-nvidia force-pushed the perf/frame-stack-reserve branch from e3bdad2 to 75314c3 Compare November 18, 2026 13:51
@dhansen-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #85780 [ run ] triggered by Bot. Commit: 75314c3 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #85780 [ run ] completed with state SUCCESS. Commit: 75314c3
/LLM/main/L0_MergeRequest_PR pipeline #71026 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

@dhansen-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #85838 [ run ] triggered by Bot. Commit: 75314c3 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #85838 [ run ] completed with state SUCCESS. Commit: 75314c3
/LLM/main/L0_MergeRequest_PR pipeline #71082 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

@dhansen-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #86112 [ run ] triggered by Bot. Commit: 7a843f6 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #86112 [ run ] completed with state SUCCESS. Commit: 7a843f6
/LLM/main/L0_MergeRequest_PR pipeline #71323 completed with status: 'UNSTABLE'

CI Report

⚠️ Multi-GPU Label Required:
Multi-GPU tests require the ci: full pre-merge approved label on this PR. Either:

  • Wait for the PR to be fully approved — the label is added automatically once approval is complete. Having unresolved open comments is fine, or
  • If needed, ask a member of NVIDIA/trt-llm-ci-approvers to add the label manually.
    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

@mikeiovine mikeiovine left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@github-actions

Copy link
Copy Markdown

Automatically added "ci: full pre-merge approved" because this PR has satisfied the required GitHub review approvals. Unresolved review conversations and other required checks remain independent merge requirements.

CPython 3.11+ keeps frames in per-thread data-stack chunks. When a hot
call sits at a chunk boundary, every call maps a fresh chunk and every
return unmaps it (gh-142183). On GB200 each munmap also pays a Grace SMMU
invalidation once NCCL has peer-mapped memory, so the churn costs
milliseconds per executor step: MiniMax-M3 piecewise CUDA graph prefill
steps spent about half their host time in munmap, all on the executor
thread. CPython fixed this in 3.13.13 and 3.14.4; 3.12 will not get the
fix.

Add run_with_frame_stack_reserve() and call PyExecutor.event_loop through
it, so the loop starts near the beginning of a large data-stack chunk and
ordinary call depths never reach a boundary. The reserve is applied only on
affected CPython versions and can be disabled with
TLLM_DISABLE_FRAME_STACK_RESERVE=1. Cover the version gate, the disable
switch, the churn itself and the executor wiring with cpu_only unit tests.

Background:
- python/cpython#142183 (the issue)
- python/cpython#145789 (the fix; backports
  #145829 / #146123 for 3.13, #145828 for 3.14)
- python/cpython#88756 (3.11 change that moved
  frames onto the data stack)
- sgl-project/sglang-torchtpu#1037 (same technique)

Signed-off-by: Dan Hansen <[email protected]>
@dhansen-nvidia
dhansen-nvidia force-pushed the perf/frame-stack-reserve branch from 7a843f6 to edeeb33 Compare November 23, 2026 21:25
@dhansen-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #86808 [ run ] triggered by Bot. Commit: edeeb33 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@dhansen-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #87269 [ run ] triggered by Bot. Commit: edeeb33 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #87269 [ run ] completed with state SUCCESS. Commit: edeeb33
/LLM/main/L0_MergeRequest_PR pipeline #72405 completed with status: 'SUCCESS'

CI Report

Link to invocation

@dhansen-nvidia
dhansen-nvidia added this pull request to the merge queue Nov 25, 2026
Merged via the queue into NVIDIA:main with commit 1cb4054 Nov 25, 2026
15 checks passed
@dhansen-nvidia
dhansen-nvidia deleted the perf/frame-stack-reserve branch November 25, 2026 21:43
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.

7 participants