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

Better error message on wrong device #1056

Merged
merged 15 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

-
- Added specific runtime error when metric object is on wrong device (#1056(https://github.com/PyTorchLightning/metrics/pull/1056))

-

Expand Down
12 changes: 12 additions & 0 deletions tests/bases/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from tests.helpers import seed_all
from tests.helpers.testers import DummyListMetric, DummyMetric, DummyMetricMultiOutput, DummyMetricSum
from torchmetrics import PearsonCorrCoef
from torchmetrics.utilities.imports import _TORCH_LOWER_1_6

seed_all(42)
Expand Down Expand Up @@ -423,3 +424,14 @@ class UnsetProperty(metric_class):
match="Torchmetrics v0.9 introduced a new argument class property called.*",
):
UnsetProperty()


@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires gpu")
def test_specific_error_on_wrong_device():
metric = PearsonCorrCoef()
preds = torch.tensor(range(10), device="cuda", dtype=torch.float)
target = torch.tensor(range(10), device="cuda", dtype=torch.float)
with pytest.raises(
RuntimeError, match="This could be due to the metric class not being on the same device as input"
):
_ = metric(preds, target)
13 changes: 12 additions & 1 deletion torchmetrics/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,18 @@ def wrapped_func(*args: Any, **kwargs: Any) -> None:
self._computed = None
self._update_count += 1
with torch.set_grad_enabled(self._enable_grad):
update(*args, **kwargs)
try:
update(*args, **kwargs)
except RuntimeError as err:
if "Expected all tensors to be on" in str(err):
raise RuntimeError(
f"{str(err)}. \n"
SkafteNicki marked this conversation as resolved.
Show resolved Hide resolved
"This could be due to the metric class not being on the same device as input.\n"
"Instead of `metric=Metric()` try to do `metric=Metric().to(device)` where"
SkafteNicki marked this conversation as resolved.
Show resolved Hide resolved
" device corresponds to the device of the input."
SkafteNicki marked this conversation as resolved.
Show resolved Hide resolved
) from err
raise err

if self.compute_on_cpu:
self._move_list_states_to_cpu()

Expand Down