Skip to content

Fix FSDP device mesh initialization on CPU#21816

Open
Yonghui-Lee wants to merge 2 commits into
Lightning-AI:masterfrom
Yonghui-Lee:fsdp-device-mesh-cpu-fix
Open

Fix FSDP device mesh initialization on CPU#21816
Yonghui-Lee wants to merge 2 commits into
Lightning-AI:masterfrom
Yonghui-Lee:fsdp-device-mesh-cpu-fix

Conversation

@Yonghui-Lee

@Yonghui-Lee Yonghui-Lee commented Jul 9, 2026

Copy link
Copy Markdown

What does this PR do?

Fixes #21814

Initialize device mesh with the strategy's root device type instead of hardcoded "cuda", allowing FSDP to run on CPU-only environments.

Before submitting
  • Was this discussed/agreed via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:

Reviewer checklist
  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

Initialize device mesh with the strategy's root device type instead of hardcoded "cuda", allowing FSDP to run on CPU-only environments.

Copilot AI 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.

Pull request overview

This PR fixes FSDP DeviceMesh initialization to support CPU-only environments by using the strategy’s root_device.type instead of a hardcoded "cuda", and adds regression tests to validate CPU device-mesh initialization in both Lightning and Fabric.

Changes:

  • Initialize DeviceMesh using self.root_device.type in both lightning.pytorch and lightning.fabric FSDP strategies.
  • Add CPU regression tests ensuring init_device_mesh is called with "cpu" when running with CPU parallel devices.
  • Add missing RunIf import in Fabric FSDP tests.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/lightning/pytorch/strategies/fsdp.py Use root_device.type for init_device_mesh instead of hardcoded "cuda"
src/lightning/fabric/strategies/fsdp.py Use root_device.type for init_device_mesh instead of hardcoded "cuda"
tests/tests_pytorch/strategies/test_fsdp.py Add regression test for CPU device-mesh initialization
tests/tests_fabric/strategies/test_fsdp.py Add regression test (and RunIf import) for CPU device-mesh initialization

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@RunIf(min_torch="2.2")
@mock.patch("torch.distributed.device_mesh.init_device_mesh")
@mock.patch("torch.distributed.init_process_group")
def test_device_mesh_initialization_cpu(init_process_group_mock, init_device_mesh_mock):
@RunIf(min_torch="2.2")
@mock.patch("torch.distributed.device_mesh.init_device_mesh")
@mock.patch("torch.distributed.init_process_group")
def test_device_mesh_initialization_cpu(init_process_group_mock, init_device_mesh_mock):
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.

Device mesh initialization fails when running FSDP on CPU

3 participants