Skip to content

Conversation

@SeanNaren
Copy link
Contributor

@SeanNaren SeanNaren commented Jun 23, 2022

What does this PR do?

Related #12985.

The reason for this PR is because FSDP has overridden the apply method as seen here: https://github.com/pytorch/pytorch/blob/master/torch/distributed/fsdp/fully_sharded_data_parallel.py#L1192

For model parallel correctness, the parameters are first gathered from all processes, the apply function is run, and then the processes are scattered. The issue here is that in this function we do not actually move the modules to the cuda device, so we run into a distributed error when this code is run.

To circumvent this, I modified the code to not use self.apply.

Does your PR introduce any breaking changes? If yes, please list them.

None

Before submitting

  • Was this discussed/approved 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:

  • 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

Did you have fun?

Make sure you had fun coding 🙃

cc @carmocca @justusschock @awaelchli @Borda @ananthsub @ninginthecloud @jjenniferdai @rohitgr7

…FSDP from calling distributed communications
@SeanNaren SeanNaren added bug Something isn't working lightningmodule pl.LightningModule strategy: fsdp Fully Sharded Data Parallel labels Jun 23, 2022
@SeanNaren SeanNaren added this to the pl:1.7 milestone Jun 23, 2022
@SeanNaren SeanNaren self-assigned this Jun 23, 2022
# TODO: Find why `isinstance(module, DeviceDtypeModuleMixin)` doesn't
# work when using `init_meta_context`.
for m in self.modules():
if isinstance(m, (DeviceDtypeModuleMixin, pl.LightningModule)):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if isinstance(m, (DeviceDtypeModuleMixin, pl.LightningModule)):
if isinstance(m, DeviceDtypeModuleMixin):

should be sufficient I guess as LightningModule inherits from DeviceDtypeModuleMixin:

https://github.com/Lightning-AI/lightning/blob/511f1a651506af2e12e15346de0a3715fed7e814/src/pytorch_lightning/core/module.py#L60

if isinstance(m, (DeviceDtypeModuleMixin, pl.LightningModule)):
apply_fn(m)

apply_fn(self)
Copy link
Member

Choose a reason for hiding this comment

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

if we immediately apply this on self only, there is no reason to have this defined as a local function :)

@SeanNaren
Copy link
Contributor Author

So, turns out I can do the same fix as #13350 which means not touching scary internals of the LightningModule. Going to put this into the related PR :)

thanks @justusschock for taking a look regardless!

@SeanNaren SeanNaren closed this Jun 23, 2022
@SeanNaren SeanNaren deleted the fix/fsdp_1n branch June 23, 2022 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working lightningmodule pl.LightningModule strategy: fsdp Fully Sharded Data Parallel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants