Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FSDPv2] Move the module to xla device #6525

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions test/spmd/test_fsdp_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ def test_fsdp_v2_global_mesh_error(self):
with self.assertRaises(ValueError):
model = FSDPv2(model)

def test_fsdp_v2_cpu_model(self):
cpu_model = self.SimpleLinear()

mesh = self._get_mesh((self.n_devices, 1), None, ('fsdp', 'tensor'))
xs.set_global_mesh(mesh)

model = FSDPv2(cpu_model)
self.assertEqual(
str(list(model._orig_module.parameters())[0].device), "xla:0")


if __name__ == '__main__':
test = unittest.main()
Expand Down
5 changes: 4 additions & 1 deletion torch_xla/experimental/spmd_fully_sharded_data_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import numpy as np

import torch_xla
import torch_xla.core.xla_model as xm
import torch_xla.distributed.spmd as spmd
from torch_xla.distributed.fsdp.wrap import recursive_wrap

Expand Down Expand Up @@ -94,7 +95,9 @@ def __init__(
)
self._auto_wrap(auto_wrap_kwargs, fsdp_kwargs)

self._orig_module = module
# Let's move the module to xla device in case it's not moved
# by the caller already.
self._orig_module = module.to(xm.xla_device())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a no-op if the model is already on xla?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so.

self._mesh = mesh

# Only handle params which are not already sharded. This enables
Expand Down
Loading