Skip to content

[None][feat] Support externally provided MPI sessions with explicit ownership - #16053

Merged
sunnyqgg merged 1 commit into
NVIDIA:mainfrom
sunnyqgg:mpi_session_ownership
Jul 13, 2026
Merged

[None][feat] Support externally provided MPI sessions with explicit ownership#16053
sunnyqgg merged 1 commit into
NVIDIA:mainfrom
sunnyqgg:mpi_session_ownership

Conversation

@sunnyqgg

@sunnyqgg sunnyqgg commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Summary

Core MPI session lifecycle only (split out of #15777 per review):

  • Accept an externally created MPI session: GenerationExecutorProxy / GenerationExecutorRpcProxy adopt a caller-provided MpiPoolSession; its size is validated against the model world size via a shared validate_session_world_size() in mpi_session.py (an external session must match exactly, else the wrong number of executor workers would start).
  • Owned vs borrowed: _owns_mpi_session in llm.py, proxy.py, rpc_proxy.py. Borrowed sessions are never shut down by the LLM or the executor — on normal or exceptional paths; shutdown_abort is equally gated. The rpc_proxy non-owning shutdown blocks on outstanding worker futures for parity with the owned path.
  • Minimal communicator change for reused workers: init_pp_comm reuses the existing PPCommNCCL when world size is unchanged, and warns when replacing a live comm of a different world size.

No test-specific optimization and no weight-loader behavior in this PR.

Companion PRs

Each is independently mergeable; together they supersede #15777.

Test plan

  • Exercised end-to-end by the test-side companion PR's validation on B200 (multi-GPU suite 15 passed with shared sessions; DeepSeek-V3-Lite accuracy 4 layouts at reference)
  • CI

Summary by CodeRabbit

  • Bug Fixes
    • Improved MPI session handling so existing sessions are reused more safely during model startup and shutdown.
    • Added validation to catch mismatches between session size and model size earlier, preventing incorrect executor launches.
    • Fixed cleanup behavior so externally managed sessions are no longer shut down unexpectedly.
    • Improved shutdown coordination for worker processes to reduce hangs and deadlock risk.

@coderabbitai

coderabbitai Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds MPI session ownership tracking (_owns_mpi_session) across llm.py, executor.py, proxy.py, and rpc_proxy.py, with a new validate_session_world_size helper in mpi_session.py to validate externally supplied sessions, gating shutdown calls on ownership. Separately, init_pp_comm gains logic to reuse an existing PPCommNCCL communicator when world size matches.

Changes

MPI session ownership tracking

Layer / File(s) Summary
World-size validation helper and docstring cleanup
tensorrt_llm/llmapi/mpi_session.py
Adds validate_session_world_size(mpi_session, model_world_size) raising ValueError on n_workers mismatch; multiple docstrings standardized to triple-double-quote format and some log statements converted from f-strings.
LLM constructor/shutdown ownership tracking
tensorrt_llm/llmapi/llm.py
BaseLLM.__init__ retains mpi_session, clears args.mpi_session, and computes _owns_mpi_session; failure-path and shutdown() only shut down the session when owned.
Executor create() session passthrough
tensorrt_llm/executor/executor.py
Non-Windows path forwards the given mpi_session instead of forcing None; Windows path sets _owns_mpi_session = True on the created executor.
GenerationExecutorProxy ownership and validation
tensorrt_llm/executor/proxy.py
Imports and uses validate_session_world_size for externally provided sessions, tracks _owns_mpi_session, and gates shutdown_abort/mpi_session.shutdown() calls on ownership; docstring reflows.
RpcProxy ownership, futures tracking and shutdown
tensorrt_llm/executor/rpc_proxy.py
Sets _owns_mpi_session for created vs. externally adopted sessions with validation, retains launch_workers() futures in self.worker_futures, and on shutdown waits on those futures instead of shutting the shared pool when the session is externally owned; some log statements converted from f-strings.

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

PP communicator reuse

Layer / File(s) Summary
init_pp_comm reuse/replace logic
tensorrt_llm/_torch/distributed/communicator.py
Adds a branch reusing an existing PPCommNCCL when world_size matches, updating only mapping; otherwise logs a warning before replacing the communicator with a different world size.

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

Sequence Diagram(s)

sequenceDiagram
  participant LLM as BaseLLM
  participant Proxy as GenerationExecutorProxy/RpcProxy
  participant Session as MpiSession
  participant Validator as validate_session_world_size

  LLM->>LLM: compute _owns_mpi_session from external session presence
  LLM->>Proxy: pass mpi_session
  Proxy->>Validator: validate_session_world_size(mpi_session, model_world_size)
  Validator-->>Proxy: raise ValueError or pass
  Proxy->>Proxy: set _owns_mpi_session (True/False)
  Note over Proxy,Session: On shutdown()
  alt owns session
    Proxy->>Session: shutdown()
  else externally owned
    Proxy->>Proxy: wait on stored worker_futures
  end
Loading

Suggested reviewers: hchings, yibinl-nvidia, JunyiXu-nv

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 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 matches the main change and follows the required [None][feat] format.
Description check ✅ Passed The description covers the core summary and test plan, with only template-specific sections omitted.
✨ 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

🧹 Nitpick comments (2)
tensorrt_llm/executor/executor.py (1)

664-667: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Setting a "private" attribute from outside the owning class.

executor._owns_mpi_session = True reaches into GenerationExecutorProxy's internal state from the factory method after construction (where the constructor itself would have computed False since a non-None session was passed in). Consider exposing this via an explicit constructor parameter (e.g. owns_mpi_session: bool = ...) threaded through _create_ipc_executor, which would be less fragile to future refactors of GenerationExecutorProxy.__init__.

🤖 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 `@tensorrt_llm/executor/executor.py` around lines 664 - 667, The factory is
mutating GenerationExecutorProxy’s internal state by setting the private
_owns_mpi_session attribute after construction, which is fragile. Update
GenerationExecutorProxy.__init__ to accept an explicit owns_mpi_session
parameter and thread it through _create_ipc_executor so the ownership decision
is established during निर्माण instead of being patched afterward. Make sure the
proxy constructor and its call sites use this parameter consistently, preserving
the current behavior when the session is created locally.
tensorrt_llm/executor/rpc_proxy.py (1)

280-297: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Broad except Exception flagged by static analysis; likely acceptable here.

Ruff BLE001 flags for future in getattr(self, "worker_futures", []): try: future.result() except Exception as e: Catching broadly is reasonable here since arbitrary worker-task exceptions must not abort shutdown, mirroring the existing bare except: pattern in proxy.py's shutdown(). No action likely needed, but flagging per the coding guideline to "limit the except to the smallest set of errors possible" for completeness.

🤖 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 `@tensorrt_llm/executor/rpc_proxy.py` around lines 280 - 297, The shutdown path
in rpc_proxy.py is catching Exception broadly around worker_futures.result(),
which triggers the Ruff BLE001 warning. Update the shutdown logic in the
executor’s teardown flow to handle only the expected future/task failure cases,
or if swallowing all worker-task errors is intentional, add an explicit local
lint suppression at that catch site while keeping the warning log. Keep the
behavior in shutdown and the mpi_session shutdown path unchanged otherwise.

Sources: Coding guidelines, Linters/SAST tools

🤖 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 `@tensorrt_llm/executor/rpc_proxy.py`:
- Around line 308-326: In _create_mpi_session, initialize the instance state
before calling validate_session_world_size(): set self.mpi_session to None and
self._owns_mpi_session to False up front so a failed validation does not leave a
partially constructed RpcProxy that later crashes in shutdown() with an
AttributeError. Then keep the existing create/adopt branching intact, updating
self.mpi_session only after successful creation or validation.

---

Nitpick comments:
In `@tensorrt_llm/executor/executor.py`:
- Around line 664-667: The factory is mutating GenerationExecutorProxy’s
internal state by setting the private _owns_mpi_session attribute after
construction, which is fragile. Update GenerationExecutorProxy.__init__ to
accept an explicit owns_mpi_session parameter and thread it through
_create_ipc_executor so the ownership decision is established during निर्माण
instead of being patched afterward. Make sure the proxy constructor and its call
sites use this parameter consistently, preserving the current behavior when the
session is created locally.

In `@tensorrt_llm/executor/rpc_proxy.py`:
- Around line 280-297: The shutdown path in rpc_proxy.py is catching Exception
broadly around worker_futures.result(), which triggers the Ruff BLE001 warning.
Update the shutdown logic in the executor’s teardown flow to handle only the
expected future/task failure cases, or if swallowing all worker-task errors is
intentional, add an explicit local lint suppression at that catch site while
keeping the warning log. Keep the behavior in shutdown and the mpi_session
shutdown path unchanged otherwise.
🪄 Autofix (Beta)

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: 2cf75c6d-09b2-4033-bf65-a1ff946f98f2

📥 Commits

Reviewing files that changed from the base of the PR and between 4d1cb8f and f40a9ea.

📒 Files selected for processing (6)
  • tensorrt_llm/_torch/distributed/communicator.py
  • tensorrt_llm/executor/executor.py
  • tensorrt_llm/executor/proxy.py
  • tensorrt_llm/executor/rpc_proxy.py
  • tensorrt_llm/llmapi/llm.py
  • tensorrt_llm/llmapi/mpi_session.py

Comment thread tensorrt_llm/executor/rpc_proxy.py
@sunnyqgg

sunnyqgg commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #57984 [ run ] triggered by Bot. Commit: f40a9ea Link to invocation

Comment thread tensorrt_llm/executor/rpc_proxy.py
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@sunnyqgg

sunnyqgg commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58147 [ run ] triggered by Bot. Commit: f40a9ea Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@sunnyqgg

sunnyqgg commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58417 [ run ] triggered by Bot. Commit: f40a9ea Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@sunnyqgg

sunnyqgg commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58462 [ run ] triggered by Bot. Commit: f40a9ea Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@sunnyqgg

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58545 [ run ] triggered by Bot. Commit: f40a9ea Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@sunnyqgg

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58568 [ run ] triggered by Bot. Commit: f40a9ea Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

…wnership

Allow an LLM to adopt an externally created MpiPoolSession instead of
always spawning its own, so callers (e.g. test infrastructure) can share
one worker pool across several LLM instances:

- proxy/rpc_proxy accept an external session and validate its size via a
  shared validate_session_world_size() in mpi_session.py (an external
  session must match the model world size exactly, else the wrong number
  of executor workers would start).
- _owns_mpi_session distinguishes owned from borrowed sessions in llm.py,
  proxy.py and rpc_proxy.py: borrowed sessions are never shut down by the
  LLM or the executor, on normal or exceptional paths; shutdown_abort is
  equally gated. The rpc_proxy non-owning shutdown blocks on outstanding
  worker futures for parity with the owned path.
- communicator.py: init_pp_comm reuses the existing PPCommNCCL when the
  world size is unchanged (reused worker processes would otherwise
  re-create NCCL comms), and warns when replacing a live comm of a
  different world size - the minimum needed for reused workers.

Split out of NVIDIA#15777 per review (core MPI session lifecycle only; the HF
weight-loader cache and the test-side automatic reuse are companion PRs).

Signed-off-by: qgai <qgai@nvidia.com>
@sunnyqgg

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@sunnyqgg
sunnyqgg force-pushed the mpi_session_ownership branch from f40a9ea to 3c53d96 Compare July 10, 2026 15:26
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58675 [ run ] triggered by Bot. Commit: 3c53d96 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58675 [ run ] completed with state SUCCESS. Commit: 3c53d96
/LLM/main/L0_MergeRequest_PR pipeline #47261 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

@sunnyqgg

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58739 [ run ] triggered by Bot. Commit: 3c53d96 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58739 [ run ] completed with state FAILURE. Commit: 3c53d96

Link to invocation

@sunnyqgg

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58770 [ run ] triggered by Bot. Commit: 3c53d96 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58770 [ run ] completed with state SUCCESS. Commit: 3c53d96
/LLM/main/L0_MergeRequest_PR pipeline #47352 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

@sunnyqgg

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58782 [ run ] triggered by Bot. Commit: 3c53d96 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58782 [ run ] completed with state SUCCESS. Commit: 3c53d96
/LLM/main/L0_MergeRequest_PR pipeline #47364 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

@sunnyqgg

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58792 [ run ] triggered by Bot. Commit: 3c53d96 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58792 [ run ] completed with state DISABLED
Pipeline is freezed and top-1 instance is under maintenance.

Link to invocation

Copy link
Copy Markdown
Collaborator

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58826 [ run ] triggered by Bot. Commit: 3c53d96 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58826 [ run ] completed with state SUCCESS. Commit: 3c53d96
/LLM/main/L0_MergeRequest_PR pipeline #47376 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

@QiJune

QiJune commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58848 [ run ] triggered by Bot. Commit: 3c53d96 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58848 [ run ] completed with state SUCCESS. Commit: 3c53d96
/LLM/main/L0_MergeRequest_PR pipeline #47400 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

@sunnyqgg

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58878 [ run ] triggered by Bot. Commit: 3c53d96 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58878 [ run ] completed with state SUCCESS. Commit: 3c53d96
/LLM/main/L0_MergeRequest_PR pipeline #47421 completed with status: 'SUCCESS'

CI Report

Link to invocation

@sunnyqgg
sunnyqgg merged commit 6c43b3e into NVIDIA:main Jul 13, 2026
8 checks passed
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.

6 participants