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

Avoid wrapping LightningModule in *DataParallel overrides when not fitting #8632

Closed

Conversation

ninginthecloud
Copy link
Contributor

@ninginthecloud ninginthecloud commented Jul 29, 2021

What does this PR do?

Avoid wrapping LightningModule in *DataParallel overrides when not fitting
Specifically,
- Update configure_ddp function in DDPPlugin, DDPSpawnPlugin, DDPShardedPlugin and DDPSpawnShardedPlugin by checking the state of LightningModule and avoiding wrapping LihgningModule as *DataParallel when the state is not TrainerFn.FITTING.
- Update validation_step function in DDPPlugin and DDPSpawnPlugin to use LightningModule's validation_step function if self.model is not DistributedDataParallel instance.
- Update test_step and prediction_step functions in DDPPlugin and DDPSpawnPlugin to use LightningModule's *_step functions directly.

Fixes #6977

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

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 internal minor changes/refactorings)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing make sure you have read 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 🙃

@pep8speaks
Copy link

pep8speaks commented Jul 29, 2021

Hello @ninginthecloud! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2021-08-12 18:20:15 UTC

@codecov
Copy link

codecov bot commented Jul 29, 2021

Codecov Report

Merging #8632 (4bab65f) into master (938a191) will decrease coverage by 0%.
The diff coverage is 42%.

@@          Coverage Diff           @@
##           master   #8632   +/-   ##
======================================
- Coverage      89%     89%   -0%     
======================================
  Files         176     176           
  Lines       14268   14291   +23     
======================================
+ Hits        12679   12687    +8     
- Misses       1589    1604   +15     

@justusschock
Copy link
Member

@ananthsub @SeanNaren @ninginthecloud
A general question here:
depending on how the processes are spawned and the weights are loaded (could happen only on master), wouldn't it be better to still wrap for weights syncing and use the no_sync context manager instead to disable gradient syncs?

@ninginthecloud ninginthecloud force-pushed the fix_issue6977 branch 2 times, most recently from 3caea11 to 4a15a61 Compare July 31, 2021 00:35
Copy link
Contributor

@ananthsub ananthsub left a comment

Choose a reason for hiding this comment

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

Note:
With this change,
self.model may not be a DistributedDataParallel model. therefore, in L400-410 of the DDP plugin, for training_step, validation_step, test_step, and predict_step - we must check if isinstance(self.model, DistributedDataParallel)
If it is, we should call self.model(*args, **kwargs)
This works because self.model's forward function internally routes to the LightningModule's *_step function: https://github.com/PyTorchLightning/pytorch-lightning/blob/1f01db8b303e647b102f48c36e91ddb17784414f/pytorch_lightning/overrides/base.py#L77-L99

however, if isinstance(self.model, LightningModule) then we should simply call self.model.training_step / validation_step/test_step / predict_step directly

More context on this design can be found here: #4630

@awaelchli @justusschock to double check this approach

pytorch_lightning/plugins/training_type/ddp.py Outdated Show resolved Hide resolved
pytorch_lightning/plugins/training_type/ddp.py Outdated Show resolved Hide resolved
@ananthsub
Copy link
Contributor

ananthsub commented Jul 31, 2021

depending on how the processes are spawned and the weights are loaded (could happen only on master)

@justusschock could you describe why the processes spawned would affect this? do you mean loading weights on rank 0 only and broadcasting weights via DDP's synchronization, and then running evaluation?

I think applying the no-sync wrapper regardless for validation/test/predict should be done, and that might resolve the uneven end of data in a simpler way. what's the clearest way to add the no_sync for these? is that handled by the plugin or the loops? and is that already accounted for by the no_grad context manager applied at the start of those loops?

pytorch_lightning/plugins/training_type/ddp.py Outdated Show resolved Hide resolved
pytorch_lightning/plugins/training_type/ddp.py Outdated Show resolved Hide resolved
pytorch_lightning/plugins/training_type/ddp.py Outdated Show resolved Hide resolved
pytorch_lightning/plugins/training_type/ddp.py Outdated Show resolved Hide resolved
pytorch_lightning/plugins/training_type/ddp.py Outdated Show resolved Hide resolved
pytorch_lightning/plugins/training_type/ddp.py Outdated Show resolved Hide resolved
@ninginthecloud ninginthecloud force-pushed the fix_issue6977 branch 2 times, most recently from 6fe7e55 to 705d663 Compare August 4, 2021 20:53
@ninginthecloud ninginthecloud marked this pull request as ready for review August 4, 2021 22:27
@awaelchli awaelchli changed the title Fix iss6977: Avoid wrapping LightningModule in *DataParallel overrides when not fitting Avoid wrapping LightningModule in *DataParallel overrides when not fitting Aug 4, 2021
@awaelchli awaelchli added the feature Is an improvement or enhancement label Aug 4, 2021
@awaelchli awaelchli added this to the v1.5 milestone Aug 4, 2021
@awaelchli awaelchli added the distributed Generic distributed-related topic label Aug 4, 2021
CHANGELOG.md Outdated Show resolved Hide resolved
pytorch_lightning/plugins/training_type/ddp.py Outdated Show resolved Hide resolved
@awaelchli
Copy link
Contributor

awaelchli commented Aug 11, 2021

@ninginthecloud It's because the tests are failing and if they do, the CI job won't submit coverage. Coverage is merged from all different jobs so if say the GPU code path fails coverage for that will be missing.

if tests pass the codecov bot will also update the message on this pr here.

@awaelchli awaelchli changed the title Avoid wrapping LightningModule in *DataParallel overrides when not fitting Avoid wrapping LightningModule in *DataParallel overrides when not fitting [2/2] Aug 25, 2021
@awaelchli awaelchli changed the title Avoid wrapping LightningModule in *DataParallel overrides when not fitting [2/2] Avoid wrapping LightningModule in *DataParallel overrides when not fitting Aug 25, 2021
@ananthsub
Copy link
Contributor

Closing this out in favor of #9096

@ananthsub ananthsub closed this Aug 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
distributed Generic distributed-related topic feature Is an improvement or enhancement has conflicts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid wrapping LightningModule in *DataParallel overrides when not fitting
5 participants