-
-
Notifications
You must be signed in to change notification settings - Fork 657
[BC-breaking] Make Metrics accumulate values on device specified by user (#1232) #1238
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
695c2d8
Make Metrics accumulate values on device specified by user (#1232)
n2cholas b8a4355
Improved and fixed accuracy tests
vfdev-5 15bb9ab
autopep8 fix
1b82de0
Merge branch 'master' into metrics_impl
sdesrozis 9973de2
update docs and docstrings for updated metrics (#1239)
n2cholas b37bf17
Merge branch 'master' into metrics_impl
sdesrozis 1013230
Merge branch 'master' into metrics_impl
sdesrozis fa11e2a
Merge branch 'master' into metrics_impl
sdesrozis 4d6d66c
Merge branch 'master' into metrics_impl
sdesrozis 4f3c767
Updates to metrics_impl (#1266)
n2cholas bd8fcad
Merge branch 'master' into metrics_impl
vfdev-5 674d13a
Update metrics.rst
vfdev-5 1b7306a
Update metrics.rst
vfdev-5 61e5041
Fix TPU tests for metrics_impl branch (#1277)
n2cholas 0e3cde5
metrics_impl fix 2 gpu hvd tests and ensure consistent detaching (#1280)
n2cholas 1a5dc76
Merge branch 'master' into metrics_impl
vfdev-5 73cbd6a
Fixes failing test on TPUs
vfdev-5 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import numbers | ||
from typing import Any, Callable, Optional, Union | ||
from typing import Any, Callable, Union | ||
|
||
import torch | ||
|
||
|
@@ -31,14 +31,19 @@ class VariableAccumulation(Metric): | |
:class:`~ignite.engine.engine.Engine`'s ``process_function``'s output into the | ||
form expected by the metric. This can be useful if, for example, you have a multi-output model and | ||
you want to compute the metric with respect to one of the outputs. | ||
device (str of torch.device, optional): optional device specification for internal storage. | ||
device (str or torch.device): specifies which device updates are accumulated on. Setting the metric's | ||
device to be the same as your ``update`` arguments ensures the ``update`` method is non-blocking. By | ||
default, CPU. | ||
|
||
""" | ||
|
||
_required_output_keys = None | ||
|
||
def __init__( | ||
self, op: Callable, output_transform: Callable = lambda x: x, device: Optional[Union[str, torch.device]] = None | ||
self, | ||
op: Callable, | ||
output_transform: Callable = lambda x: x, | ||
device: Union[str, torch.device] = torch.device("cpu"), | ||
): | ||
if not callable(op): | ||
raise TypeError("Argument op should be a callable, but given {}".format(type(op))) | ||
|
@@ -61,12 +66,13 @@ def _check_output_type(self, output: Union[Any, torch.Tensor, numbers.Number]) - | |
def update(self, output: Union[Any, torch.Tensor, numbers.Number]) -> None: | ||
self._check_output_type(output) | ||
|
||
if self._device is not None: | ||
# Put output to the metric's device | ||
if isinstance(output, torch.Tensor) and (output.device != self._device): | ||
if isinstance(output, torch.Tensor): | ||
output = output.detach() | ||
if output.device != self._device: | ||
output = output.to(self._device) | ||
|
||
self.accumulator = self._op(self.accumulator, output) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we detach if self._device is not None:
# Put output to the metric's device
if isinstance(output, torch.Tensor):
output = output.detach()
if (output.device != self._device):
output = output.to(self._device)
self.accumulator = self._op(self.accumulator, output) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we should, thanks for catching this. |
||
if hasattr(output, "shape"): | ||
self.num_examples += output.shape[0] if len(output.shape) > 1 else 1 | ||
else: | ||
|
@@ -111,11 +117,14 @@ class Average(VariableAccumulation): | |
:class:`~ignite.engine.engine.Engine`'s ``process_function``'s output into the | ||
form expected by the metric. This can be useful if, for example, you have a multi-output model and | ||
you want to compute the metric with respect to one of the outputs. | ||
device (str of torch.device, optional): optional device specification for internal storage. | ||
|
||
device (str or torch.device): specifies which device updates are accumulated on. Setting the metric's | ||
device to be the same as your ``update`` arguments ensures the ``update`` method is non-blocking. By | ||
default, CPU. | ||
""" | ||
|
||
def __init__(self, output_transform: Callable = lambda x: x, device: Optional[Union[str, torch.device]] = None): | ||
def __init__( | ||
self, output_transform: Callable = lambda x: x, device: Union[str, torch.device] = torch.device("cpu") | ||
): | ||
def _mean_op(a, x): | ||
if isinstance(x, torch.Tensor) and x.ndim > 1: | ||
x = x.sum(dim=0) | ||
|
@@ -155,11 +164,15 @@ class GeometricAverage(VariableAccumulation): | |
:class:`~ignite.engine.engine.Engine`'s ``process_function``'s output into the | ||
form expected by the metric. This can be useful if, for example, you have a multi-output model and | ||
you want to compute the metric with respect to one of the outputs. | ||
device (str of torch.device, optional): optional device specification for internal storage. | ||
device (str or torch.device): specifies which device updates are accumulated on. Setting the metric's | ||
device to be the same as your ``update`` arguments ensures the ``update`` method is non-blocking. By | ||
default, CPU. | ||
|
||
""" | ||
|
||
def __init__(self, output_transform: Callable = lambda x: x, device: Optional[Union[str, torch.device]] = None): | ||
def __init__( | ||
self, output_transform: Callable = lambda x: x, device: Union[str, torch.device] = torch.device("cpu") | ||
): | ||
def _geom_op(a: torch.Tensor, x: Union[Any, numbers.Number, torch.Tensor]) -> torch.Tensor: | ||
if not isinstance(x, torch.Tensor): | ||
x = torch.tensor(x) | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.