diff --git a/tests/test_handler_metrics_saver_dist.py b/tests/test_handler_metrics_saver_dist.py index 06dcbafa28..72876766e1 100644 --- a/tests/test_handler_metrics_saver_dist.py +++ b/tests/test_handler_metrics_saver_dist.py @@ -31,6 +31,7 @@ def test_content(self): self._run(tempdir) def _run(self, tempdir): + my_rank = dist.get_rank() fnames = ["aaa" * 300, "bbb" * 301, "ccc" * 302] metrics_saver = MetricsSaver( @@ -46,7 +47,7 @@ def _val_func(engine, batch): engine = Engine(_val_func) - if dist.get_rank() == 0: + if my_rank == 0: data = [{"image_meta_dict": {"filename_or_obj": [fnames[0]]}}] @engine.on(Events.EPOCH_COMPLETED) @@ -54,7 +55,7 @@ def _save_metrics0(engine): engine.state.metrics = {"metric1": 1, "metric2": 2} engine.state.metric_details = {"metric3": torch.tensor([[1, 2]]), "metric4": torch.tensor([[5, 6]])} - if dist.get_rank() == 1: + if my_rank == 1: # different ranks have different data length data = [ {"image_meta_dict": {"filename_or_obj": [fnames[1]]}}, @@ -79,7 +80,7 @@ def _all_gather(engine): metrics_saver.attach(engine) engine.run(data, max_epochs=1) - if dist.get_rank() == 0: + if my_rank == 0: # check the metrics.csv and content self.assertTrue(os.path.exists(os.path.join(tempdir, "metrics.csv"))) with open(os.path.join(tempdir, "metrics.csv")) as f: diff --git a/tests/test_integration_determinism.py b/tests/test_integration_determinism.py index 97e510d03c..64c018b4f5 100644 --- a/tests/test_integration_determinism.py +++ b/tests/test_integration_determinism.py @@ -75,7 +75,7 @@ def setUp(self): def tearDown(self): set_determinism(seed=None) - @TimedCall(seconds=150) + @TimedCall(seconds=150, skip_timing=not torch.cuda.is_available()) def test_training(self): set_determinism(seed=0) loss, step = run_test(device=self.device) diff --git a/tests/test_integration_stn.py b/tests/test_integration_stn.py index ca067c4d78..e655ff6755 100644 --- a/tests/test_integration_stn.py +++ b/tests/test_integration_stn.py @@ -103,7 +103,7 @@ def setUp(self): def tearDown(self): set_determinism(seed=None) - @TimedCall(seconds=100) + @TimedCall(seconds=100, skip_timing=not torch.cuda.is_available()) def test_training(self): """ check that the quality AffineTransform backpropagation diff --git a/tests/test_savitzky_golay_filter.py b/tests/test_savitzky_golay_filter.py index b410a641ea..0e54276533 100644 --- a/tests/test_savitzky_golay_filter.py +++ b/tests/test_savitzky_golay_filter.py @@ -25,7 +25,7 @@ torch.Tensor([1.0]).unsqueeze(0).unsqueeze(0), # Input data: Single value torch.Tensor([1 / 3]).unsqueeze(0).unsqueeze(0), # Expected output: With a window length of 3 and polyorder 1 # output should be equal to mean of 0, 1 and 0 = 1/3 (because input will be zero-padded and a linear fit performed) - 1e-15, # absolute tolerance + 1e-6, # absolute tolerance ] TEST_CASE_1D = [ @@ -35,21 +35,21 @@ .unsqueeze(0) .unsqueeze(0), # Expected output: zero padded, so linear interpolation # over length-3 windows will result in output of [2/3, 1, 2/3]. - 1e-15, # absolute tolerance + 1e-6, # absolute tolerance ] TEST_CASE_2D_AXIS_2 = [ {"window_length": 3, "order": 1}, # along default axis (2, first spatial dim) torch.ones((3, 2)).unsqueeze(0).unsqueeze(0), torch.Tensor([[2 / 3, 2 / 3], [1.0, 1.0], [2 / 3, 2 / 3]]).unsqueeze(0).unsqueeze(0), - 1e-15, # absolute tolerance + 1e-6, # absolute tolerance ] TEST_CASE_2D_AXIS_3 = [ {"window_length": 3, "order": 1, "axis": 3}, # along axis 3 (second spatial dim) torch.ones((2, 3)).unsqueeze(0).unsqueeze(0), torch.Tensor([[2 / 3, 1.0, 2 / 3], [2 / 3, 1.0, 2 / 3]]).unsqueeze(0).unsqueeze(0), - 1e-15, # absolute tolerance + 1e-6, # absolute tolerance ] # Replicated-padding trivial tests @@ -59,7 +59,7 @@ torch.Tensor([1.0]).unsqueeze(0).unsqueeze(0), # Input data: Single value torch.Tensor([1.0]).unsqueeze(0).unsqueeze(0), # Expected output: With a window length of 3 and polyorder 1 # output will be equal to mean of [1, 1, 1] = 1 (input will be nearest-neighbour-padded and a linear fit performed) - 1e-15, # absolute tolerance + 1e-6, # absolute tolerance ] TEST_CASE_1D_REP = [ @@ -67,21 +67,21 @@ torch.Tensor([1.0, 1.0, 1.0]).unsqueeze(0).unsqueeze(0), # Input data torch.Tensor([1.0, 1.0, 1.0]).unsqueeze(0).unsqueeze(0), # Expected output: zero padded, so linear interpolation # over length-3 windows will result in output of [2/3, 1, 2/3]. - 1e-15, # absolute tolerance + 1e-6, # absolute tolerance ] TEST_CASE_2D_AXIS_2_REP = [ {"window_length": 3, "order": 1, "mode": "replicate"}, # along default axis (2, first spatial dim) torch.ones((3, 2)).unsqueeze(0).unsqueeze(0), torch.Tensor([[1.0, 1.0], [1.0, 1.0], [1.0, 1.0]]).unsqueeze(0).unsqueeze(0), - 1e-15, # absolute tolerance + 1e-6, # absolute tolerance ] TEST_CASE_2D_AXIS_3_REP = [ {"window_length": 3, "order": 1, "axis": 3, "mode": "replicate"}, # along axis 3 (second spatial dim) torch.ones((2, 3)).unsqueeze(0).unsqueeze(0), torch.Tensor([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]).unsqueeze(0).unsqueeze(0), - 1e-15, # absolute tolerance + 1e-6, # absolute tolerance ] # Sine smoothing @@ -101,18 +101,18 @@ class TestSavitzkyGolayCPU(unittest.TestCase): @parameterized.expand( [TEST_CASE_SINGLE_VALUE, TEST_CASE_1D, TEST_CASE_2D_AXIS_2, TEST_CASE_2D_AXIS_3, TEST_CASE_SINE_SMOOTH] ) - def test_value(self, arguments, image, expected_data, atol): + def test_value(self, arguments, image, expected_data, atol, rtol=1e-5): result = SavitzkyGolayFilter(**arguments)(image) - np.testing.assert_allclose(result, expected_data, atol=atol) + np.testing.assert_allclose(result, expected_data, atol=atol, rtol=rtol) class TestSavitzkyGolayCPUREP(unittest.TestCase): @parameterized.expand( [TEST_CASE_SINGLE_VALUE_REP, TEST_CASE_1D_REP, TEST_CASE_2D_AXIS_2_REP, TEST_CASE_2D_AXIS_3_REP] ) - def test_value(self, arguments, image, expected_data, atol): + def test_value(self, arguments, image, expected_data, atol, rtol=1e-5): result = SavitzkyGolayFilter(**arguments)(image) - np.testing.assert_allclose(result, expected_data, atol=atol) + np.testing.assert_allclose(result, expected_data, atol=atol, rtol=rtol) @skip_if_no_cuda @@ -120,9 +120,9 @@ class TestSavitzkyGolayGPU(unittest.TestCase): @parameterized.expand( [TEST_CASE_SINGLE_VALUE, TEST_CASE_1D, TEST_CASE_2D_AXIS_2, TEST_CASE_2D_AXIS_3, TEST_CASE_SINE_SMOOTH] ) - def test_value(self, arguments, image, expected_data, atol): + def test_value(self, arguments, image, expected_data, atol, rtol=1e-5): result = SavitzkyGolayFilter(**arguments)(image.to(device="cuda")) - np.testing.assert_allclose(result.cpu(), expected_data, atol=atol) + np.testing.assert_allclose(result.cpu(), expected_data, atol=atol, rtol=rtol) @skip_if_no_cuda @@ -130,9 +130,9 @@ class TestSavitzkyGolayGPUREP(unittest.TestCase): @parameterized.expand( [TEST_CASE_SINGLE_VALUE_REP, TEST_CASE_1D_REP, TEST_CASE_2D_AXIS_2_REP, TEST_CASE_2D_AXIS_3_REP] ) - def test_value(self, arguments, image, expected_data, atol): + def test_value(self, arguments, image, expected_data, atol, rtol=1e-5): result = SavitzkyGolayFilter(**arguments)(image.to(device="cuda")) - np.testing.assert_allclose(result.cpu(), expected_data, atol=atol) + np.testing.assert_allclose(result.cpu(), expected_data, atol=atol, rtol=rtol) if __name__ == "__main__": diff --git a/tests/utils.py b/tests/utils.py index 41d905a96c..f96f659353 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -24,7 +24,7 @@ from functools import partial from subprocess import PIPE, Popen from typing import Callable, Optional, Tuple -from urllib.error import HTTPError, URLError +from urllib.error import ContentTooShortError, HTTPError, URLError import numpy as np import torch