Skip to content

[model] fix: Preserve distributed context during export - #5696

Merged
yaoyu-33 merged 1 commit into
mainfrom
bugfix/export-ckpt-distributed-lifecycle-20260819
Aug 20, 2026
Merged

yaoyu-33 merged 1 commit into
mainfrom
bugfix/export-ckpt-distributed-lifecycle-20260819

Conversation

@yaoyu-33

Copy link
Copy Markdown
Contributor

Summary

  • preserve a caller-owned distributed context in AutoBridge.export_ckpt
  • retain the existing temporary Gloo lifecycle for standalone export
  • add a real world-size-one Gloo regression test

Supported trigger and impact

AutoBridge.export_ckpt is a documented one-call export API. If a supported in-process caller already owns the default process group, export unconditionally tries to create another default group and fails before checkpoint loading or Hugging Face saving with:

ValueError: trying to initialize the default process group twice!

The focused validated case is world-size-one Gloo, which the downstream CPU checkpoint load path supports. This PR does not claim new multi-rank export topology support.

Root cause and fix

export_ckpt always entered temporary_distributed_context, even though adjacent import_ckpt and load_megatron_model paths reuse caller-owned distributed state. The fix selects nullcontext() when distributed is already initialized and otherwise keeps the existing temporary Gloo context.

Regression evidence

A deterministic private reproducer AST-loaded the exact production definitions and used real PyTorch Gloo while mocking only checkpoint load/save and MCore initialization beyond the failing boundary:

uv run --no-sync python repro_export_ckpt_dist.py .
  • Before: exit 1 with the duplicate-default-group ValueError before load/save.
  • After: exit 0; existing caller state remained initialized and load/save were reached.
  • Negative control: the same run exercised standalone export and confirmed its temporary group was cleaned up.

The committed unit test encodes the caller-owned-state contract with a real world-size-one Gloo group. Local collection of the full test module was blocked before test execution by unrelated CPU optional-package imports that require CUDA; the exact-source reproducer above was used for local runtime evidence.

Validation

Passed:

uv run --no-sync python -m py_compile src/megatron/bridge/models/conversion/auto_bridge.py tests/unit_tests/models/test_auto_bridge.py
uv run --no-sync ruff check src/megatron/bridge/models/conversion/auto_bridge.py tests/unit_tests/models/test_auto_bridge.py
uv run --no-sync ruff format --check src/megatron/bridge/models/conversion/auto_bridge.py tests/unit_tests/models/test_auto_bridge.py
git diff --check
uv run --no-sync pre-commit run --all-files

No dependency, public signature, CI workflow, checkpoint-format, or multi-rank behavior changes are included.

Signed-off-by: Yu Yao <yaoyu.094@gmail.com>
@copy-pr-bot

copy-pr-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@yaoyu-33

Copy link
Copy Markdown
Contributor Author

/ok to test 6bf4f9d

@claude

claude Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

LGTM

The export_ckpt fix correctly reuses caller-owned distributed state via nullcontext() when dist.is_initialized(), avoiding a re-init crash. This matches the identical pattern already used in import_ckpt (line 1468) and export_adapter_ckpt (line 1695), so the three conversion entry points are now consistent. The new test_export_ckpt_preserves_existing_distributed_context test exercises the caller-owned path and tears the process group down in a finally.

Suggested test cases

  • test_export_ckpt_preserves_existing_distributed_context (added test - verify it passes)
  • No perf tests impacted.

@yaoyu-33 yaoyu-33 added area:ckpt Checkpoint conversion, loading, export, and save paths bug Something isn't working needs-more-tests Requires additional L0 and L1 test coverage before merge needs-review PR is ready for code review and waiting on a reviewer labels Aug 19, 2026
@yaoyu-33
yaoyu-33 merged commit 922872b into main Aug 20, 2026
90 checks passed
@yaoyu-33
yaoyu-33 deleted the bugfix/export-ckpt-distributed-lifecycle-20260819 branch August 20, 2026 00:30

This branch was previously deployed

2 inactive deployments
public — 6bf4f9df Deployed Aug 19, 2026 by copy-pr-bot[bot] via release / finalize / publish-docs #4811
test — 6bf4f9df Deployed Aug 19, 2026 by copy-pr-bot[bot] via cicd-wait-in-queue #20787
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ckpt Checkpoint conversion, loading, export, and save paths bug Something isn't working needs-more-tests Requires additional L0 and L1 test coverage before merge needs-review PR is ready for code review and waiting on a reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant