Skip to content

fix(dist): rebind Megatron's import-time torch.distributed collectives after monkey_patch_torch_dist - #2453

Merged
zhuzilin merged 2 commits into
THUDM:mainfrom
ruizhang0101:fix/rebind-megatron-collectives-after-patch
Oct 10, 2026
Merged

zhuzilin merged 2 commits into
THUDM:mainfrom
ruizhang0101:fix/rebind-megatron-collectives-after-patch

Conversation

@ruizhang0101

Copy link
Copy Markdown
Contributor

Fixes #2452.

Problem

monkey_patch_torch_dist() replaces the torch.distributed collectives with wrappers that unwrap ReloadableProcessGroup to the inner communicator before calling torch. Megatron does not look those functions up at call time. Three modules bind them when they are imported:

  • megatron/core/distributed/param_and_grad_buffer.py: dist_reduce_scatter_func, dist_all_gather_func, _coalescing_manager
  • megatron/core/tensor_parallel/mappings.py: dist_reduce_scatter_func, dist_all_gather_func
  • megatron/core/timers.py: dist_all_gather_func

slime/backends/megatron_utils/actor.py imports megatron.core at module level and applies the patch later, in MegatronTrainRayActor.init, so Megatron keeps the unpatched originals. Every DDP gradient reduce-scatter, distributed-optimizer all-gather and tensor-parallel collective therefore hands the Python subclass group straight to torch, which dispatches it through the PyProcessGroup trampoline instead of the inner NCCL group. torch 2.11 tolerates that detour. torch 2.13 renamed the collectives (reduce_scatter_single, all_gather_single, *_single_coalesced), which the wrapper does not override, and the first optimizer step segfaults in c10d::PyProcessGroup::PyWorkHolder::wait (details and log in #2452).

Change

After the patch has replaced the torch.distributed functions, monkey_patch_torch_dist() now walks the already-imported megatron.* modules and redirects any attribute that is identical to a replaced original to its wrapper. The mapping is taken from a snapshot of torch.distributed made before patching, so it stays correct if the patched set changes. Modules imported after the patch bind the wrappers on their own and need nothing.

The behavior for runs that do not call monkey_patch_torch_dist() (no --offload-train) is unchanged. Megatron's call sites are untouched.

Test

tests/test_reloadable_process_group_megatron_bindings.py registers a fake megatron.core.distributed.param_and_grad_buffer module holding the original functions, applies the patch in a spawned process, and checks that the three bindings now point at the wrappers while an unrelated attribute and a non-megatron module keep the originals.

Verification

  • pre-commit run --files on both files: passed (ruff, autoflake, isort, black).
  • pytest tests/test_reloadable_process_group_megatron_bindings.py tests/test_reloadable_process_group_memory_check.py: 4 passed.
  • GPU: 1x H200 trainer + 1 rollout engine, Megatron Bridge, Qwen2.5-0.5B-Instruct, --use-critic, --offload-train, --update-weight-transport disk, torch 2.13.0+cu130, Slime at 41014d1 with this patch applied to reloadable_process_group.py and no other change. Without the patch the critic actor segfaulted at its first optimizer step in 3 of 3 runs. With it, 3 training steps completed (175 s, 137 s, 144 s), 4 weight updates reached the rollout engine, no segfault. Not covered by that run: tensor parallel > 1 (tensor_parallel/mappings.py holds the same kind of bindings and is rebound by the same loop).

…s after monkey_patch_torch_dist

Signed-off-by: Rui Zhang <zrfishnoodles@gmail.com>
Signed-off-by: Rui Zhang <zrfishnoodles@gmail.com>
@zhuzilin
zhuzilin merged commit 642dcfd into THUDM:main Oct 10, 2026
90 checks passed
aoshen02 added a commit to aoshen02/vime that referenced this pull request Oct 10, 2026
Source PRs: THUDM/slime#2453, #2450, #2458, #2457. Preserve approved Vime overlays and restore the omitted Source Straw error assertions. Native TITO, replay/SC, MTP and Buildkite adapters use the fixed 43b4 engine stack.

Validation: Slurm2366 301CPU/1skip,2376 Python3.11 agent step163pass/2optional skips,2377 Straw7+88pass,2378 nine precommit hooks. Native sandbox SSD prerequisites2373 pass; full new8GPU/model and final-image gates remain pending. No latest or release promotion.

Slime-commit: 58b0878679de339ec7844b45d884a87955a134be

Co-authored-by: Codex <noreply@openai.com>
Signed-off-by: aoshen02 <aoshen@inferact.ai>
aoshen02 added a commit to vllm-project/vime that referenced this pull request Oct 12, 2026
* sync: update from Slime through #2393

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* fix: complete vLLM sync translations

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* sync: complete Slime update through #2394

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* Sync Slime through score centering

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* fix: rebase vLLM patches onto current nightly

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* fix: remove stale async CI entry and obsolete metrics fallbacks

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* feat: transport sampling-mask logprobs for score centering

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* fix(vllm): enable scale-out endpoint for rollout TITO

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* fix(rollout): keep requests during sleep and enable external PD TITO

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* fix(ci): enable TITO on external OPD teacher

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* test: enable scale-out render endpoint in Geo3K e2e

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* feat: mirror Slime persistent rollout queue and distributed fully async

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* test: exercise straw transport in mirrored GPU suites

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* style: match repository formatter for straw sync

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* ci: install straw in CPU contract container

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* sync: mirror Slime #2427 straw checkpoint rollback

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* ci: install straw dependency for sync tests

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* test: pin vllm generation config for score centering

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* fix: bind straw coordinator in vllm rollout

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* docs: record full GPU gate procedure

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* sync: mirror Slime #2432 Straw queue and replay changes

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* ci: quote Straw requirement in Buildkite shell steps

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Sync] Mirror Slime PipelineRL and reference top-p fix

Port #2437 flush scheduling and #2434 reference log-prob replay fix. Replace request-bound TITO weight version with per-output propagation through vLLM #53199, and preserve exact top-p score-centering support.

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [CI] Add flush interval to weight updater test fixtures

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [CI] Accept versionless terminal abort chunk in PipelineRL probe

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Sync] Mirror concurrent rollout and HTTP regression tests

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Sync] Preserve buffered-token weight version on abort

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Sync] Avoid zero-KL assertion for top-p actor/ref mismatch

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Sync] Backport AuxOutput reset fix from vLLM #59060

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Sync] Stamp synthetic abort outputs with committed version

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Sync] Preserve coalesced rollout metadata and validate sampler probabilities

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Sync] Remove superseded per-file spill and mirror Straw references

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [CI] Inherit the available file descriptor limit for GPU jobs

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Sync] Backport terminal token-stream finish reasons

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Test] Cover terminal output with real prompt token metadata

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Sync] Mirror latest Slime and validate nightly Python/Rust adapters

Mirror Slime through 2f231865 with approved native overlays and Source test registration. Pin validated43b nightly, install tested tensor-dump/tracing Python and Rust patches, and parse native integer-ID top-k responses. Preserve original Straw assertions and saving policy. Keep profiler deferred and latest unchanged.

AI assistance: OpenAI Codex. Scoped validation171CPU tests, full precommit and11patch/98path installation proof pass; full remote CI and remaining candidate GPU validation pending.

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Sync] Mirror Slime external PD delta artifact assertion

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* fix: repair verified recovery and initial-policy CI contracts

Preserve training loss, numerical thresholds and checkpoint strategy. Add indexed precision dumps, offline-first manager setup, scoped reference load, same-distribution CI KL diagnostic, canonical optimizer masters and NIXL peer retirement.

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* fix: keep initial-model diagnostic optional for direct loss callers

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* fix: give the PD rollout debug dump a unique per-rollout path

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Sync] Mirror Slime v0.4.0 training stack and release updates

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Sync] Mirror latest Slime documentation and native engine contracts

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Sync] Restore upstream documentation release checks

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* fix(sync): mirror active DeepEP recipes and docs CI contracts

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* docs(sync): mirror Slime environment entry and preserve sampler parameters

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Sync] Preserve upstream export explanation and tokenization attribution

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Sync] Inherit Slime documentation deletions and navigation

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* Remove opt-in DeepEP auto switching

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [RL] Release native weight-transfer communicators and expose lifecycle APIs

Include fork vLLM #119 on the fixed 43b4 baseline, including explicit communicator destruction and matching Python/Rust public controls. Keep the other sixteen-patch capabilities intact.

Assisted-by: OpenAI Codex
Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Sync] Mirror Slime agent and collective updates through 58b08786

Source PRs: THUDM/slime#2453, #2450, #2458, #2457. Preserve approved Vime overlays and restore the omitted Source Straw error assertions. Native TITO, replay/SC, MTP and Buildkite adapters use the fixed 43b4 engine stack.

Validation: Slurm2366 301CPU/1skip,2376 Python3.11 agent step163pass/2optional skips,2377 Straw7+88pass,2378 nine precommit hooks. Native sandbox SSD prerequisites2373 pass; full new8GPU/model and final-image gates remain pending. No latest or release promotion.

Slime-commit: 58b0878679de339ec7844b45d884a87955a134be

Co-authored-by: Codex <noreply@openai.com>
Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Build] Package native ARM Sunabako without changing x86 installation

Preserve Slime SDK0.1.1 contracts and the original x86 build target. Build a verified native ARM wheel with matching seccomp/syscall/ABI and driver paths before requirements resolution.

Source-Slime: 58b0878679de339ec7844b45d884a87955a134be
Co-authored-by: Codex <noreply@openai.com>
Signed-off-by: aoshen02 <aoshen@inferact.ai>

* Mirror rollout weight-group lifecycle and preserve draft state across sleep

Restore Source connect/disconnect layout and Agent MTP translation. Reuse vLLM parameter retention for draft-private weights instead of adding a second backup switch.

Co-authored-by: Codex <noreply@openai.com>
Signed-off-by: aoshen02 <aoshen@inferact.ai>

* sync: align draft retention patch with reviewed native selector

Mirror the simplified native proposal, including an empty fully-shared draft. No trainer, saving, or inference-path policy change.

Co-authored-by: Codex <noreply@openai.com>
Signed-off-by: aoshen02 <aoshen@inferact.ai>

* [Sync] Mirror router health-check ownership with native gateway patch

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* fix(sync): mirror source documentation and MTP verification budgets

Co-authored-by: Codex <noreply@openai.com>
Signed-off-by: aoshen02 <aoshen@inferact.ai>

* fix(rust): mirror multimodal render-to-generate support

Keep the pinned vLLM base and existing Rust overlays; apply the independent multimodal bridge after them. Preserve explicit UUIDs, native validation and metadata-only EC support. Remove the GB10 launcher resurrected after the approved platform deletion.

Signed-off-by: aoshen02 <aoshen@inferact.ai>

* fix(epd): forward producer transfer metadata and preserve media identity

Use the existing rollout endpoint metadata instead of inert SGLang-only engine keys. Preserve ordinary/streaming placement, complete media parts and caller UUIDs. Do not alias language_only to the unrelated modality-limit flag.

Validation: frozen700-path CPU2502 passes92 cases; real two-host ECCPU/NIXL2503 passes both frontends with four actual transfers and baseline-exact tokens/UUID metadata-only. Explicit connector/body assembly, not default deployment or all-model certification.

Co-authored-by: Codex <noreply@openai.com>
Signed-off-by: aoshen02 <aoshen@inferact.ai>

* fix(epd): mirror encoder deployment with native EC transfer

Keep Slime deployment sequencing and consumer rendering. Use native EC transport and DP endpoint ownership, preserve decoder input processing while omitting encoder weights, and supply resolved engine config and embedding support to Rust. Reuse existing upstream authentication fix; retain test assertions.

Validation: complete 23-patch stack on43b;83CLI/468server and all510Rust paths; installed experimentalfefcf78runtime/705sourcepaths/92CPU; no-overlay original3GPU E-P-D2552 passes Python/Rust. One-hour2554 is running, not claimed complete. Native review worktrees remain uncommitted. AI assistance used.

Co-authored-by: Codex <noreply@openai.com>
Signed-off-by: aoshen02 <aoshen@inferact.ai>

---------

Signed-off-by: aoshen02 <aoshen@inferact.ai>
Co-authored-by: Codex <noreply@openai.com>
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.

[Bug] monkey_patch_torch_dist() runs after Megatron binds torch.distributed collectives; segfault in PyWorkHolder::wait on torch 2.13

2 participants