def test_metric_forward_cache_reset(): """test that forward cache is reset when `reset` is called.""" metric = DummyMetricSum() _ = metric(2.0) assert metric._forward_cache == 2.0 metric.reset() assert metric._forward_cache is None
def test_reset_compute(): a = DummyMetricSum() assert a.x == 0 a.update(tensor(5)) assert a.compute() == 5 a.reset() assert a.compute() == 0
def test_reset_compute(): a = DummyMetricSum() assert a.x == 0 a.update(tensor(5)) assert a.compute() == 5 a.reset() if not _LIGHTNING_AVAILABLE or _LIGHTNING_GREATER_EQUAL_1_3: assert a.compute() == 0 else: assert a.compute() == 5
def test_device_and_dtype_transfer(tmpdir): metric = DummyMetricSum() assert metric.x.is_cuda is False assert metric.x.dtype == torch.float32 metric = metric.to(device='cuda') assert metric.x.is_cuda metric = metric.double() assert metric.x.dtype == torch.float64 metric.reset() assert metric.x.dtype == torch.float64 metric = metric.half() assert metric.x.dtype == torch.float16 metric.reset() assert metric.x.dtype == torch.float16
def test_device_and_dtype_transfer(tmpdir): metric = DummyMetricSum() assert metric.x.is_cuda is False assert metric.device == torch.device("cpu") assert metric.x.dtype == torch.float32 metric = metric.to(device="cuda") assert metric.x.is_cuda assert metric.device == torch.device("cuda", index=0) metric.set_dtype(torch.double) assert metric.x.dtype == torch.float64 metric.reset() assert metric.x.dtype == torch.float64 metric.set_dtype(torch.half) assert metric.x.dtype == torch.float16 metric.reset() assert metric.x.dtype == torch.float16
def test_warning_on_compute_before_update(): metric = DummyMetricSum() # make sure everything is fine with forward with pytest.warns(None) as record: val = metric(1) assert not record metric.reset() with pytest.warns(UserWarning, match=r'The ``compute`` method of metric .*'): val = metric.compute() assert val == 0.0 # after update things should be fine metric.update(2.0) with pytest.warns(None) as record: val = metric.compute() assert not record assert val == 2.0
def test_warning_on_compute_before_update(): """test that an warning is raised if user tries to call compute before update.""" metric = DummyMetricSum() # make sure everything is fine with forward with pytest.warns(None) as record: val = metric(1) assert not record metric.reset() with pytest.warns(UserWarning, match=r"The ``compute`` method of metric .*"): val = metric.compute() assert val == 0.0 # after update things should be fine metric.update(2.0) with pytest.warns(None) as record: val = metric.compute() assert not record assert val == 2.0
def test_metric_forward_cache_reset(): metric = DummyMetricSum() _ = metric(2.0) assert metric._forward_cache == 2.0 metric.reset() assert metric._forward_cache is None