-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
[Feat] Cleanup ModelCheckpoint / EarlyStopping by moving logic to LoggerConnector #5218
Merged
Merged
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a48ca18
[bug-fix] Metric reduction with Logging (#5150)
tchaton 884a454
iupdate
f30de4c
resolve bugs
d6bae34
add test back
303e85d
correct flake8
tchaton c6ef2f1
Merge branch 'release/1.2-dev' into cleanup/metrics_2
tchaton 3af20fe
resolve flake8
tchaton 5d3d7ce
update on comments
tchaton fe95959
update tests
bf2e78e
add a test
89f901e
add test
tchaton f2ffa52
update to Callable
tchaton 97c9e51
Update pytorch_lightning/trainer/connectors/logger_connector/metrics_…
tchaton b922c73
Merge branch 'release/1.2-dev' into cleanup/metrics_2
tchaton 2696cf1
Merge branch 'release/1.2-dev' into cleanup/metrics_2
tchaton 46de65b
add changelog
tchaton 3b37aa6
Merge branch 'cleanup/metrics_2' of https://github.com/PyTorchLightni…
tchaton 2650ae2
resolve flake8
tchaton a957d19
Merge branch 'release/1.2-dev' into cleanup/metrics_2
tchaton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
pytorch_lightning/trainer/connectors/logger_connector/metrics_holder.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Copyright The PyTorch Lightning team. | ||
tchaton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import numbers | ||
from typing import Any | ||
|
||
import torch | ||
|
||
from pytorch_lightning.metrics.metric import Metric | ||
from pytorch_lightning.utilities import _TPU_AVAILABLE | ||
|
||
|
||
class MetricsHolder: | ||
tchaton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
""" | ||
This class acts as a dictonary holder. | ||
It holds metris and implement convertion functions. | ||
tchaton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Those functions will be triggered within LoggerConnector | ||
when the property is being requested from the user. | ||
""" | ||
|
||
def __init__(self, to_float: bool = False): | ||
self.metrics = {} | ||
self._to_float = to_float | ||
|
||
def update(self, metrics): | ||
self.metrics.update(metrics) | ||
|
||
def pop(self, key, default): | ||
return self.metrics.pop(key, default) | ||
|
||
def reset(self, metrics): | ||
self.metrics = metrics | ||
|
||
def convert(self, use_tpu: bool, device: torch.device): | ||
for key, value in self.metrics.items(): | ||
self.metrics[key] = self._convert(value, use_tpu, device) | ||
|
||
def _convert(self, current: Any, use_tpu: bool, device: torch.device): | ||
if self._to_float: | ||
return self._convert_to_float(current, use_tpu, device) | ||
return self._convert_to_tensor(current, use_tpu, device) | ||
|
||
def _convert_to_float(self, current, use_tpu: bool, device: torch.device): | ||
tchaton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if isinstance(current, Metric): | ||
current = current.compute().detach() | ||
|
||
if isinstance(current, torch.Tensor): | ||
current = float(current.item()) | ||
|
||
elif isinstance(current, int): | ||
current = float(current) | ||
|
||
return current | ||
|
||
def _convert_to_tensor(self, current: Any, use_tpu: bool, device: torch.device): | ||
if current is not None: | ||
if isinstance(current, Metric): | ||
current = current.compute().detach() | ||
|
||
elif isinstance(current, numbers.Number): | ||
if device is None: | ||
current = torch.tensor(current, dtype=torch.float) | ||
else: | ||
current = torch.tensor(current, device=device, dtype=torch.float) | ||
|
||
if use_tpu and _TPU_AVAILABLE: | ||
current = current.cpu() | ||
|
||
return current |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this always be on cpu? or should it be on
current.device
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great question ! I am not sure. What do you think ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO it should live on current.device, since all the other tensors (especially current if not nan) also live on this device.