Skip to content

[TRTLLM-16725][refactor] Centralize PyTorch distributed initialization - #20704

Merged
yijingl-nvidia merged 1 commit into
NVIDIA:mainfrom
yijingl-nvidia:user/yijingl/torch-distributed-init
Nov 3, 2026
Merged

yijingl-nvidia merged 1 commit into
NVIDIA:mainfrom
yijingl-nvidia:user/yijingl/torch-distributed-init

Conversation

@yijingl-nvidia

@yijingl-nvidia yijingl-nvidia commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Description

This PR moves the code that starts the PyTorch process group into one new module, tensorrt_llm/_torch/distributed/initialization.py.

No functional change.

Before this PR, the two MegaMoE backends, the VisualGen executor and the Ray GPU worker each had their own copy. Consolidating into one module improves code readability and maintenance.

This PR does not depend on the other PRs in the series.

The latest push rebases on main. On main, #21225 changed the MegaMoE CuTe DSL bootstrap: rank 0 now hosts a TCPStore, and the bootstrap does not use MASTER_ADDR or MASTER_PORT. ensure_cutedsl_torch_distributed() now has this code, so the moved code is still the same as on main. init_torch_distributed() now gives store and init_method to init_process_group() only when the caller sets them, so each caller makes the same call as before.

Test Coverage

0 tests added, 0 tests changed.

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.

PRs in this series

These PRs refactor checkpoint loading and add Model Streamer loading. They merge in this order, top to bottom.

PR What it does
#20498 Change checkpoint_io_policy from plain strings to the string enum CheckpointIOPolicy.
#20703 Add the string enum CheckpointFormat for the built-in checkpoint formats, make the checkpoint I/O helpers take only LoadFormat, and make the checkpoint I/O policy resolution easier to read.
#20704 (this PR, merged) Put the PyTorch process-group setup in one module, distributed/initialization.py.
#20711 Make ModelLoader.load() shorter: move its MX, GMS, memory and model-hook helpers to new files in models/checkpoints/.
#20713 Remove the MX-only hooks from BaseCheckpointLoader.
#20717 Move Kimi K3 weight loading from modeling_kimi_linear.py to modeling_kimi_k3_loader.py.
#20781 Add checkpoint_io_policy="model_streamer", which loads a checkpoint with Run:ai Model Streamer.
#20782 Add the Model Streamer load for Kimi K3.

🤖 Generated with Claude Code

Dev Engineer Review

  • Centralizes PyTorch distributed initialization in tensorrt_llm/_torch/distributed/initialization.py.
  • Replaces duplicated initialization logic in the MegaMoE CuteDSL and DeepGEMM backends, VisualGen executor, and Ray GPU worker with shared helpers.
  • The helpers handle MPI rendezvous selection, launcher-variable validation, rank and world-size checks, CUDA device selection, and a DeepGEMM retry after RuntimeError. Verify these paths across launcher configurations and initialization failures.
  • Review severity counts are unavailable from the supplied evidence.
  • The supplied comments report failed L0_MergeRequest_PR pipelines for commits 72a73d8, a26b75e, bbede63, and 678e06b. They do not identify the failing tests.

QA Engineer Review

  • tests/integration/defs/accuracy/test_disaggregated_serving.py changed a comment only. No test functions, IDs, or selectors changed, and no tests were added or modified to cover the shared initialization helpers.
  • The test file has existing entries in CI test lists and manual-QA lists. No test-list files changed.
  • Coverage verdict: needs follow-up. The new initialization paths have no reported test additions, and the supplied CI comments do not identify failure details.

Per-File QA Perspective

  • tensorrt_llm/_torch/distributed/initialization.py: Verify process-group initialization, MPI rank and world-size validation, rendezvous selection and reuse, CUDA-device handling, and retry behavior.
  • tensorrt_llm/_torch/moe/fused_moe/mega_moe/mega_moe_cute_dsl.py: Verify that the shared helper preserves the CuteDSL backend’s initialization conditions, including single-rank and already-initialized cases.
  • tensorrt_llm/_torch/moe/fused_moe/mega_moe/mega_moe_deepgemm.py: Verify launcher-variable validation, MPI fallback rendezvous, local-rank handling, and retry behavior.
  • tensorrt_llm/_torch/visual_gen/executor.py: Verify that the shared helper preserves the existing process-group backend and initialization settings.
  • tensorrt_llm/executor/ray/gpu_worker.py: Verify master and non-master TCP-store setup and equivalent process-group initialization.
  • tests/integration/defs/accuracy/test_disaggregated_serving.py: The comment now identifies ensure_cutedsl_torch_distributed as the rendezvous implementation. Existing test selectors remain in CI and manual-QA lists; executable QA coverage did not change.

@coderabbitai

coderabbitai Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

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.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 678e06b and 864e54d.

Files selected for processing: 6
  • tensorrt_llm/_torch/distributed/initialization.py
  • tensorrt_llm/_torch/moe/fused_moe/mega_moe/mega_moe_cute_dsl.py
  • tensorrt_llm/_torch/moe/fused_moe/mega_moe/mega_moe_deepgemm.py
  • tensorrt_llm/_torch/visual_gen/executor.py
  • tensorrt_llm/executor/ray/gpu_worker.py
  • tests/integration/defs/accuracy/test_disaggregated_serving.py
Files skipped from review as they are similar to previous changes: 1
  • tests/integration/defs/accuracy/test_disaggregated_serving.py

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

Walkthrough

The change adds shared helpers for PyTorch distributed initialization. Visual generation and Ray workers use the general helper. CUTE DSL and DeepGEMM MoE initialization delegate to dedicated MPI helpers.

Changes

Distributed Initialization

Layer / File(s) Summary
Shared initialization API and workers
tensorrt_llm/_torch/distributed/initialization.py, tensorrt_llm/_torch/visual_gen/executor.py, tensorrt_llm/executor/ray/gpu_worker.py
Added init_torch_distributed to forward process-group parameters to PyTorch. Visual generation and Ray workers now use the helper.
CUTE DSL MPI initialization
tensorrt_llm/_torch/distributed/initialization.py, tensorrt_llm/_torch/moe/fused_moe/mega_moe_cute_dsl.py, tests/integration/defs/accuracy/test_disaggregated_serving.py
Added CUTE DSL MPI setup with rendezvous endpoint selection and process-group initialization. The MoE call site delegates to the helper. The integration test comment now refers to it.
DeepGEMM MPI initialization
tensorrt_llm/_torch/distributed/initialization.py, tensorrt_llm/_torch/moe/fused_moe/mega_moe_deepgemm.py
Added DeepGEMM MPI setup with launcher environment validation, local-rank selection, endpoint broadcasting, and one retry after an initialization error. The MoE call site delegates to the helper.

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

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant MPI
  participant ensure_deepgemm_torch_distributed
  participant PyTorchDistributed
  MPI->>ensure_deepgemm_torch_distributed: provide rank, world size, and local rank
  ensure_deepgemm_torch_distributed->>MPI: validate or broadcast rendezvous endpoint
  ensure_deepgemm_torch_distributed->>PyTorchDistributed: initialize NCCL process group
  PyTorchDistributed-->>ensure_deepgemm_torch_distributed: RuntimeError
  ensure_deepgemm_torch_distributed->>MPI: broadcast a new endpoint
  ensure_deepgemm_torch_distributed->>PyTorchDistributed: retry NCCL initialization
Loading

Suggested reviewers: rosong11

Merge Risk: Minimal · up to 864e5

The distributed-initialization consolidation has no established behavior regression and is mergeable after normal checks.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly identifies the refactor and the main change: centralizing PyTorch distributed initialization. It uses the expected ticket and type format.
Docstring Coverage Passed Docstring coverage is 83.33% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentati…
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.
Description check Passed The pull request includes the required Description, Test Coverage, and PR Checklist sections. It clearly explains the refactor, intended lack of functional change, affected components, test status, an…
Full details: Docstring Coverage

Explanation

Docstring coverage is 83.33% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 6 functions across 6 files.

Full details: Description check

Explanation

The pull request includes the required Description, Test Coverage, and PR Checklist sections. It clearly explains the refactor, intended lack of functional change, affected components, test status, and relevant context.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@chienchunhung

Copy link
Copy Markdown
Collaborator

/bot run --disable-fail-fast

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

LGTM. This PR consolidates PyTorch process-group initialization from MegaMoE, VisualGen, and Ray. The extraction preserves backend-specific rendezvous, device selection, and worker initialization behavior.

@chang-l , @xxi-nv can you help coordinate the reviews from VIsualGen and MoE perspectives? Thanks!

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #79392 [ run ] triggered by Bot. Commit: 72a73d8 Link to invocation

@yijingl-nvidia yijingl-nvidia changed the title [None][refactor] Centralize PyTorch distributed initialization [https://nvbugs/5812522][refactor] Centralize PyTorch distributed initialization Oct 20, 2026
@yijingl-nvidia yijingl-nvidia changed the title [https://nvbugs/5812522][refactor] Centralize PyTorch distributed initialization [TRTLLM-16725][refactor] Centralize PyTorch distributed initialization Oct 20, 2026
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #79392 [ run ] completed with state SUCCESS. Commit: 72a73d8
/LLM/main/L0_MergeRequest_PR pipeline #65385 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

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@yijingl-nvidia
yijingl-nvidia force-pushed the user/yijingl/torch-distributed-init branch from a26b75e to bbede63 Compare October 22, 2026 19:45
@yijingl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #79966 [ run ] triggered by Bot. Commit: bbede63 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@taianz-nv taianz-nv 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.

Nit: ensure_cutedsl_torch_distributed / ensure_deepgemm_torch_distributed still call dist.init_process_group directly; routing them through init_torch_distributed would make this module the single entry point it aims to be.
Nit: the reflowed comment in test_disaggregated_serving.py (~L2477) exceeds the surrounding line width; rewrap it.

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

@yijingl-nvidia
yijingl-nvidia force-pushed the user/yijingl/torch-distributed-init branch from bbede63 to 678e06b Compare October 28, 2026 21:04
@yijingl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #81260 [ run ] triggered by Bot. Commit: 678e06b Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #81260 [ run ] completed with state FAILURE. Commit: 678e06b
/LLM/main/L0_MergeRequest_PR pipeline #66950 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

@yijingl-nvidia
yijingl-nvidia force-pushed the user/yijingl/torch-distributed-init branch from 678e06b to 864e54d Compare October 30, 2026 21:28
@yijingl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run

@yijingl-nvidia

Copy link
Copy Markdown
Collaborator Author

Nit: ensure_cutedsl_torch_distributed / ensure_deepgemm_torch_distributed still call dist.init_process_group directly; routing them through init_torch_distributed would make this module the single entry point it aims to be. Nit: the reflowed comment in test_disaggregated_serving.py (~L2477) exceeds the surrounding line width; rewrap it.

Thanks for the NIT. I've addressed them

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #81756 [ run ] triggered by Bot. Commit: 864e54d Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

CI Report

Link to invocation

This makes it easier to reuse torch.dist functions in future and easier for mock in test.

Signed-off-by: Yijing Li <[email protected]>
@yijingl-nvidia
yijingl-nvidia force-pushed the user/yijingl/torch-distributed-init branch from 864e54d to 05b4ca3 Compare November 2, 2026 18:09
@yijingl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #82054 [ run ] triggered by Bot. Commit: 05b4ca3 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #82054 [ run ] completed with state FAILURE. Commit: 05b4ca3
/LLM/main/L0_MergeRequest_PR pipeline #67688 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

@yijingl-nvidia

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #82118 [ run ] triggered by Bot. Commit: 05b4ca3 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #82118 [ run ] completed with state SUCCESS. Commit: 05b4ca3
/LLM/main/L0_MergeRequest_PR pipeline #67751 completed with status: 'SUCCESS'

CI Report

Link to invocation

@yijingl-nvidia
yijingl-nvidia merged commit f71076c into NVIDIA:main Nov 3, 2026
@yijingl-nvidia
yijingl-nvidia deleted the user/yijingl/torch-distributed-init branch November 4, 2026 01:52
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.

8 participants