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

[bugfix] Add mechanism to prevent deadlock for DDP on Exception Trigger #8167

Merged
merged 19 commits into from
Jun 28, 2021

Conversation

tchaton
Copy link
Contributor

@tchaton tchaton commented Jun 28, 2021

What does this PR do?

This PR introduces a mechanism to prevent deadlock.

Mechanism:

  • On ddp instantiation, make all processes aware of pids
  • Share a temporary directory used to save some synchronisation state

On failure for a given process:

  • Save a dummy object to the temporary
  • Wait a little bit
  • Check if the other processes have actually wrote to the folder.
  • If the num files == world_size, all good !
  • else: kill all process and raised DeadLock Exception.

Other approaches tried:

  • Use timetout for TCPStore -> result in cuda failure
  • Use signal -> Very flicky and un-stable

Fixes #8130

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 update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

Anyone in the community is free to review the PR once the tests have passed.
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 Jun 28, 2021

Hello @tchaton! Thanks for updating this PR.

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

Comment last updated at 2021-06-28 17:45:42 UTC

@tchaton tchaton self-assigned this Jun 28, 2021
@tchaton tchaton added this to the v1.3.x milestone Jun 28, 2021
@tchaton tchaton added bug Something isn't working distributed Generic distributed-related topic labels Jun 28, 2021
@codecov
Copy link

codecov bot commented Jun 28, 2021

Codecov Report

Merging #8167 (650a4fe) into master (c3065c5) will decrease coverage by 0%.
The diff coverage is 92%.

@@           Coverage Diff           @@
##           master   #8167    +/-   ##
=======================================
- Coverage      93%     93%    -0%     
=======================================
  Files         211     211            
  Lines       13440   13607   +167     
=======================================
+ Hits        12450   12602   +152     
- Misses        990    1005    +15     

CHANGELOG.md Outdated Show resolved Hide resolved
Copy link
Contributor

@kaushikb11 kaushikb11 left a comment

Choose a reason for hiding this comment

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

LGTM!

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/trainer/trainer.py Outdated Show resolved Hide resolved
pytorch_lightning/trainer/trainer.py Outdated Show resolved Hide resolved
pytorch_lightning/trainer/trainer.py Outdated Show resolved Hide resolved
@awaelchli
Copy link
Member

@tchaton thanks for working on this.
What is the reason that this distributed-specific code is in the Trainer? With accelerators and plugins we aimed at abstracting this away. Can we consider implementing such a sync function in the parallel plugin / ddp plugin?

@tchaton
Copy link
Contributor Author

tchaton commented Jun 28, 2021

@tchaton thanks for working on this.
What is the reason that this distributed-specific code is in the Trainer? With accelerators and plugins we aimed at abstracting this away. Can we consider implementing such a sync function in the parallel plugin / ddp plugin?

Everything has been moved to the plugin. Just wanted to confirm the logic :)

Best,
T.C

@tchaton tchaton marked this pull request as ready for review June 28, 2021 11:03
@tchaton tchaton added the ready PRs ready to be merged label Jun 28, 2021
CHANGELOG.md 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
Copy link
Member

@awaelchli awaelchli left a comment

Choose a reason for hiding this comment

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

overall LGTM just unsure about how the test works

pl_examples/basic_examples/autoencoder.py Show resolved Hide resolved
tests/trainer/test_trainer.py Outdated Show resolved Hide resolved
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.

This is an interesting approach @tchaton !

How does this work if the trainer is running with torchelastic? and as @ethanwharris points out, what happens in the multinode case?

@tchaton
Copy link
Contributor Author

tchaton commented Jun 28, 2021

This is an interesting approach @tchaton !

How does this work if the trainer is running with torchelastic? and as @ethanwharris points out, what happens in the multinode case?

Hey @ananthsub. Thanks for checking this out !

In the case of multinode or TorchElastic, we can check that the saved files are matching the node size.
We will add support for those in following PRs.
If you can help us test this feature on multinode or TorchElastic and help us add support for them,
this would be really appreciated !

Best,
T.C

pytorch_lightning/plugins/training_type/parallel.py Outdated Show resolved Hide resolved
pytorch_lightning/utilities/exceptions.py Outdated Show resolved Hide resolved
tchaton and others added 2 commits June 28, 2021 18:43
Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
@tchaton tchaton enabled auto-merge (squash) June 28, 2021 17:45
@tchaton tchaton merged commit c521624 into master Jun 28, 2021
@tchaton tchaton deleted the ddp_kill_on_deadlock branch June 28, 2021 19:26
@tchaton tchaton mentioned this pull request Jun 29, 2021
11 tasks
for pid in self._pids:
if pid != os.getpid():
os.kill(pid, signal.SIGKILL)
shutil.rmtree(sync_dir)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this clean-up command be part of __del__ so we make sure it always gets removed? Or have it run in acceleratotr.on_train_end

Copy link
Member

Choose a reason for hiding this comment

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

no, we don't want to kill the processes.
the ddp processes must survive until the end of the script.

that is, unless there was an exception and we want to exit the program, which is what @tchaton tries to handle here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm referring specifically to the line

shutil.rmtree(sync_dir)

Copy link
Contributor Author

@tchaton tchaton Jun 29, 2021

Choose a reason for hiding this comment

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

Hey @carmocca,

Yes, this will make it cleaner.

awaelchli added a commit that referenced this pull request Jun 29, 2021
…er (#8167)

* add mechanism to prevent deadlock

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* resolve flake8 + update changelog

* update on comments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* update

* remove space

* resolve bugs

* overwrite config

* update on comments

* update on comments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* update

* update

* update test with comments

* Update pytorch_lightning/plugins/training_type/parallel.py

Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>

* update on comments

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
awaelchli added a commit that referenced this pull request Jun 29, 2021
…er (#8167)

* add mechanism to prevent deadlock

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* resolve flake8 + update changelog

* update on comments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* update

* remove space

* resolve bugs

* overwrite config

* update on comments

* update on comments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* update

* update

* update test with comments

* Update pytorch_lightning/plugins/training_type/parallel.py

Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>

* update on comments

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
awaelchli added a commit that referenced this pull request Jun 30, 2021
…er (#8167)

* add mechanism to prevent deadlock

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* resolve flake8 + update changelog

* update on comments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* update

* remove space

* resolve bugs

* overwrite config

* update on comments

* update on comments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* update

* update

* update test with comments

* Update pytorch_lightning/plugins/training_type/parallel.py

Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>

* update on comments

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
awaelchli added a commit that referenced this pull request Jul 1, 2021
…er (#8167)

* add mechanism to prevent deadlock

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* resolve flake8 + update changelog

* update on comments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* update

* remove space

* resolve bugs

* overwrite config

* update on comments

* update on comments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* update

* update

* update test with comments

* Update pytorch_lightning/plugins/training_type/parallel.py

Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>

* update on comments

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
lexierule pushed a commit that referenced this pull request Jul 1, 2021
…er (#8167)

* add mechanism to prevent deadlock

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* resolve flake8 + update changelog

* update on comments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* update

* remove space

* resolve bugs

* overwrite config

* update on comments

* update on comments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* update

* update

* update test with comments

* Update pytorch_lightning/plugins/training_type/parallel.py

Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>

* update on comments

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working distributed Generic distributed-related topic ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DDP script hangs forever if one child process dies.
9 participants