Skip to content

Commit

Permalink
forgot to push these files...
Browse files Browse the repository at this point in the history
  • Loading branch information
justusschock committed Apr 3, 2020
1 parent fd3332a commit df2b46d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
22 changes: 8 additions & 14 deletions pytorch_lightning/metrics/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import torch
import torch.distributed

from pytorch_lightning.metrics.convertors import tensor_metric, numpy_metric
from pytorch_lightning.metrics.converters import tensor_metric, numpy_metric
from pytorch_lightning.utilities.apply_func import apply_to_collection

__all__ = ['Metric', 'TensorMetric', 'NumpyMetric']
Expand Down Expand Up @@ -35,7 +35,7 @@ def dtype(self) -> Union[str, torch.dtype]:

@dtype.setter
def dtype(self, new_dtype: Union[str, torch.dtype]):
# Necessary to avoid infinite recursion
# necessary to avoid infinite recursion
raise RuntimeError('Cannot set the dtype explicitly. Please use metric.to(new_dtype).')

@property
Expand Down Expand Up @@ -107,25 +107,19 @@ def to(self, *args, **kwargs) -> torch.nn.Module:
tensor([[0.4963, 0.7682, 0.0885, 0.1320],
[0.3074, 0.6341, 0.4901, 0.8964],
[0.4556, 0.6323, 0.3489, 0.4017]])
>>> metric.to(torch.double)
>>> metric.to(torch.double) #doctest: +ELLIPSIS
ExampleMetric()
>>> metric.weight
tensor([[0.4963, 0.7682, 0.0885, 0.1320],
[0.3074, 0.6341, 0.4901, 0.8964],
[0.4556, 0.6323, 0.3489, 0.4017]], dtype=torch.float64)
tensor([[...]], dtype=torch.float64)
>>> cpu = torch.device('cpu')
>>> metric.to(cpu, dtype=torch.half, non_blocking=True)
ExampleMetric()
>>> metric.weight
tensor([[0.4963, 0.7682, 0.0885, 0.1320],
[0.3074, 0.6341, 0.4901, 0.8964],
[0.4556, 0.6323, 0.3489, 0.4017]], dtype=torch.float16)
>>> metric.weight #doctest: +ELLIPSIS
tensor([[...]], dtype=torch.float16)
>>> metric.to(cpu)
ExampleMetric()
>>> metric.weight
tensor([[0.4963, 0.7682, 0.0885, 0.1320],
[0.3074, 0.6341, 0.4901, 0.8964],
[0.4556, 0.6323, 0.3489, 0.4017]], dtype=torch.float16)
>>> metric.weight #doctest: +ELLIPSIS
tensor([[...]], dtype=torch.float16)
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import torch.distributed as dist

import tests.base.utils as tutils
from pytorch_lightning.metrics.convertors import (
from pytorch_lightning.metrics.converters import (
_apply_to_inputs, _apply_to_outputs, _convert_to_tensor, _convert_to_numpy,
_numpy_metric_conversion, _tensor_metric_conversion, _sync_ddp, tensor_metric, numpy_metric)
_numpy_metric_conversion, _tensor_metric_conversion, _sync_ddp_if_available, tensor_metric, numpy_metric)


@pytest.mark.parametrize(['args', 'kwargs'],
Expand Down Expand Up @@ -109,7 +109,7 @@ def test_sync_reduce_ddp():

tensor = torch.tensor([1.], device='cuda:0')

reduced_tensor = _sync_ddp(tensor)
reduced_tensor = _sync_ddp_if_available(tensor)

assert reduced_tensor.item() == dist.get_world_size(), \
'Sync-Reduce does not work properly with DDP and Tensors'
Expand All @@ -121,7 +121,7 @@ def test_sync_reduce_simple():
"""Make sure sync-reduce works without DDP"""
tensor = torch.tensor([1.], device='cpu')

reduced_tensor = _sync_ddp(tensor)
reduced_tensor = _sync_ddp_if_available(tensor)

assert torch.allclose(tensor, reduced_tensor), \
'Sync-Reduce does not work properly without DDP and Tensors'
Expand Down

0 comments on commit df2b46d

Please sign in to comment.