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

[Fix] Move log value to cpu. #4592

Merged
merged 19 commits into from
Nov 10, 2020
Merged

[Fix] Move log value to cpu. #4592

merged 19 commits into from
Nov 10, 2020

Conversation

tchaton
Copy link
Contributor

@tchaton tchaton commented Nov 9, 2020

What does this PR do?

This PR introduces fixes for gpu leak.
It adds a move_metrics_to_cpu parameter to the Trainer to force all result objects to be moved to cpu.

Fixes #4556

Before submitting

  • Was this discussed/approved via a Github issue? (no need for typos and docs improvements)
  • 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? Otherwise, we ask you to create a separate PR for every change.
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?
  • Did you verify new and existing tests pass locally with your changes?
  • If you made a notable change (that affects users), did you update the CHANGELOG?

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 in short, see 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; Bugfixes should be including in bug-fix release milestones (m.f.X) and features should be included in (m.X.b) releases.

Did you have fun?

Make sure you had fun coding 🙃

@tchaton tchaton changed the title move value to cpu to save memory [Fix] Move log value to cpu. Nov 9, 2020
@tchaton tchaton self-assigned this Nov 9, 2020
@tchaton tchaton added the priority: 0 High priority task label Nov 9, 2020
@tchaton tchaton modified the milestones: 1.0.x, 1.1 Nov 9, 2020
@tchaton tchaton added the logger Related to the Loggers label Nov 9, 2020
@tchaton tchaton marked this pull request as ready for review November 9, 2020 18:58
@@ -136,6 +136,10 @@ def log(
if sync_dist and isinstance(value, (torch.Tensor, numbers.Number)):
value = sync_fn(value, group=sync_dist_group, reduce_op=sync_dist_op)

# no need to keep on gpu
if isinstance(value, torch.Tensor) and value.is_cuda:
value = value.cpu()
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't there also be a detach()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

        # no metrics should be logged with graphs
        if not enable_graph and isinstance(value, torch.Tensor):
            value = value.detach()

        # sync across workers when using distributed training
        sync_fn = sync_fn or sync_ddp_if_available
        if sync_dist and isinstance(value, (torch.Tensor, numbers.Number)):
            value = sync_fn(value, group=sync_dist_group, reduce_op=sync_dist_op)

        # no need to keep on gpu
        if isinstance(value, torch.Tensor) and value.is_cuda:
            value = value.cpu()

detach is called just before.

pytorch_lightning/trainer/training_loop.py Outdated Show resolved Hide resolved
@Vozf
Copy link
Contributor

Vozf commented Nov 9, 2020

Have you also checked the same thing with validation loop?

@tchaton
Copy link
Contributor Author

tchaton commented Nov 9, 2020

Hey @Vozf,

Did you join Slack ? If no, please do. So we can quickly resolve this bug :)

Best,
T.C

@Vozf
Copy link
Contributor

Vozf commented Nov 9, 2020

Joined now as Vozf. I don't have any more questions except if it works for validation and testing if it fixes the bug in general :)

@codecov
Copy link

codecov bot commented Nov 9, 2020

Codecov Report

Merging #4592 (5be63ce) into master (7e08b0d) will decrease coverage by 0%.
The diff coverage is 75%.

@@          Coverage Diff           @@
##           master   #4592   +/-   ##
======================================
- Coverage      93%     93%   -0%     
======================================
  Files         116     116           
  Lines        8883    8907   +24     
======================================
+ Hits         8278    8291   +13     
- Misses        605     616   +11     

@s-rog
Copy link
Contributor

s-rog commented Nov 10, 2020

Do we need a test for this as well? (assert tensor device cpu)

@Borda
Copy link
Member

Borda commented Nov 10, 2020

lets try the .detach() intead?

Copy link
Member

@justusschock justusschock 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 fine to me, just some minor question

pytorch_lightning/trainer/trainer.py Show resolved Hide resolved
pytorch_lightning/utilities/memory.py Outdated Show resolved Hide resolved
tchaton and others added 2 commits November 10, 2020 18:34
Co-authored-by: Justus Schock <12886177+justusschock@users.noreply.github.com>
tchaton and others added 2 commits November 10, 2020 19:38
Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
…onnector.py

Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
@SeanNaren SeanNaren merged commit 514cb22 into master Nov 10, 2020
@SeanNaren SeanNaren deleted the bugfix/4556_gpu_memory_log branch November 10, 2020 21:13
@edenlightning edenlightning modified the milestones: 1.1, 1.0.x Nov 10, 2020
@SeanNaren SeanNaren modified the milestones: 1.0.x, 1.1 Nov 11, 2020
@SeanNaren
Copy link
Contributor

@edenlightning swapping this to 1.1 since its tied to the logging refactor.

rohitgr7 pushed a commit that referenced this pull request Nov 21, 2020
* move value to cpu to save memory

* update

* move to cpu

* try something

* update

* update

* add back out_dict.update({k: v})

* add move_metrics_to_cpu

* update

* Update pytorch_lightning/utilities/memory.py

Co-authored-by: Justus Schock <12886177+justusschock@users.noreply.github.com>

* resolve comments

* Update pytorch_lightning/core/step_result.py

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

* Update pytorch_lightning/trainer/connectors/logger_connector/logger_connector.py

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

Co-authored-by: Justus Schock <12886177+justusschock@users.noreply.github.com>
Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
Co-authored-by: Sean Naren <sean.narenthiran@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
logger Related to the Loggers priority: 0 High priority task
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Gpu memory leak with self.log on_epoch=True
8 participants