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鈥檒l 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 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions tests/bases/test_metric.py
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)
15 changes: 14 additions & 1 deletion torchmetrics/metric.py
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
import functools
import inspect
import traceback
from abc import ABC, abstractmethod
from contextlib import contextmanager
from copy import deepcopy
Expand Down Expand Up @@ -377,7 +378,19 @@ 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):
traceback.print_exc()
SkafteNicki marked this conversation as resolved.
Show resolved Hide resolved
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
)
raise err

if self.compute_on_cpu:
self._move_list_states_to_cpu()

Expand Down