From 83ab0ba0161ea78e24146a034d967abc37ccaebe Mon Sep 17 00:00:00 2001 From: Michael Eickenberg Date: Thu, 5 Oct 2023 00:11:04 -0400 Subject: [PATCH 1/4] FIX tighten the forward tests according to observed error wrt array size --- tests/test_1d/test_forward_1d.py | 10 ++++++++ tests/test_2d/test_forward_2d.py | 39 ++++++++++++-------------------- tests/test_3d/test_forward_3d.py | 23 ++++++++++++++----- 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/tests/test_1d/test_forward_1d.py b/tests/test_1d/test_forward_1d.py index 1bbd8d0..9050c52 100644 --- a/tests/test_1d/test_forward_1d.py +++ b/tests/test_1d/test_forward_1d.py @@ -67,6 +67,16 @@ def test_1d_t1_forward_CPU(values: torch.Tensor) -> None: ) == pytest.approx(0, abs=1e-06) + abs_errors = torch.abs(finufft1D1_out - against_torch) + l_inf_error = abs_errors.max() + l_2_error = torch.sqrt(torch.sum(abs_errors**2)) + l_1_error = torch.sum(abs_errors) + + assert l_inf_error < 3.5e-3 * N ** .6 + assert l_2_error < 7.5e-4 * N ** 1.1 + assert l_1_error < 5e-4 * N ** 1.6 + + @pytest.mark.parametrize("targets", cases) def test_1d_t2_forward_CPU(targets: torch.Tensor): """ diff --git a/tests/test_2d/test_forward_2d.py b/tests/test_2d/test_forward_2d.py index 3a61938..5cd74dd 100644 --- a/tests/test_2d/test_forward_2d.py +++ b/tests/test_2d/test_forward_2d.py @@ -45,28 +45,14 @@ def test_2d_t1_forward_CPU(N: int) -> None: against_torch = torch.fft.fft2(values.reshape(g[0].shape)) - assert abs((finufft_out - against_torch).sum()) / (N**3) == pytest.approx( - 0, abs=1e-6 - ) + abs_errors = torch.abs(finufft_out - against_torch) + l_inf_error = abs_errors.max() + l_2_error = torch.sqrt(torch.sum(abs_errors**2)) + l_1_error = torch.sum(abs_errors) - values = torch.randn(*x.shape, dtype=torch.complex64) - - finufft_out = pytorch_finufft.functional.finufft2D1.apply( - torch.from_numpy(x).to(torch.float32), - torch.from_numpy(y).to(torch.float32), - values, - N, - ) - - against_torch = torch.fft.fft2(values.reshape(g[0].shape)) - - # NOTE -- the below tolerance is set to 1e-5 instead of -6 due - # to the occasional failing case that seems to be caused by - # the randomness of the test cases in addition to the expected - # accruation of numerical inaccuracies - assert abs((finufft_out - against_torch).sum()) / (N**3) == pytest.approx( - 0, abs=1e-5 - ) + assert l_inf_error < 5e-5 * N + assert l_2_error < 1e-5 * N ** 2 + assert l_1_error < 1e-5 * N ** 3 @pytest.mark.parametrize("N", Ns) @@ -109,9 +95,14 @@ def test_2d_t2_forward_CPU(N: int) -> None: against_torch = torch.fft.ifft2(values) - assert abs((finufft_out - against_torch).sum()) / (N**3) == pytest.approx( - 0, abs=1e-6 - ) + abs_errors = torch.abs(finufft_out - against_torch) + l_inf_error = abs_errors.max() + l_2_error = torch.sqrt(torch.sum(abs_errors**2)) + l_1_error = torch.sum(abs_errors) + + assert l_inf_error < 1e-5 * N + assert l_2_error < 1e-5 * N ** 2 + assert l_1_error < 1e-5 * N ** 3 # @pytest.mark.parametrize("N", Ns) diff --git a/tests/test_3d/test_forward_3d.py b/tests/test_3d/test_forward_3d.py index e470abc..ae8f5f6 100644 --- a/tests/test_3d/test_forward_3d.py +++ b/tests/test_3d/test_forward_3d.py @@ -41,9 +41,15 @@ def test_3d_t1_forward_CPU(N: int) -> None: against_torch = torch.fft.fftn(values.reshape(g[0].shape)) - assert abs((finufft_out - against_torch).sum()) / (N**4) == pytest.approx( - 0, abs=1e-6 - ) + abs_errors = torch.abs(finufft_out - against_torch) + l_inf_error = abs_errors.max() + l_2_error = torch.sqrt(torch.sum(abs_errors**2)) + l_1_error = torch.sum(abs_errors) + + assert l_inf_error < 2e-5 * N ** 1.5 + assert l_2_error < 1e-5 * N ** 3 + assert l_1_error < 1e-5 * N ** 4.5 + @pytest.mark.parametrize("N", Ns) @@ -69,6 +75,11 @@ def test_3d_t2_forward_CPU(N: int) -> None: against_torch = torch.fft.ifftn(values) - assert (abs((finufft_out - against_torch).sum())) / (N**4) == pytest.approx( - 0, abs=1e-6 - ) + abs_errors = torch.abs(finufft_out - against_torch) + l_inf_error = abs_errors.max() + l_2_error = torch.sqrt(torch.sum(abs_errors**2)) + l_1_error = torch.sum(abs_errors) + + assert l_inf_error < 1e-5 * N ** 1.5 + assert l_2_error < 1e-5 * N ** 3 + assert l_1_error < 1e-5 * N ** 4.5 From 4f58a586ed690844e543dc7404c4d07fc6518421 Mon Sep 17 00:00:00 2001 From: Michael Eickenberg Date: Thu, 5 Oct 2023 14:32:48 -0400 Subject: [PATCH 2/4] FIX add remaining test case, 1d type 2 --- tests/test_1d/test_forward_1d.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_1d/test_forward_1d.py b/tests/test_1d/test_forward_1d.py index 9050c52..404abf8 100644 --- a/tests/test_1d/test_forward_1d.py +++ b/tests/test_1d/test_forward_1d.py @@ -105,6 +105,14 @@ def test_1d_t2_forward_CPU(targets: torch.Tensor): 0, abs=1e-05 ) + abs_errors = torch.abs(finufft_out - against_torch) + l_inf_error = abs_errors.max() + l_2_error = torch.sqrt(torch.sum(abs_errors**2)) + l_1_error = torch.sum(abs_errors) + + assert l_inf_error < 3.5e-3 * N ** .6 + assert l_2_error < 7.5e-4 * N ** 1.1 + assert l_1_error < 5e-4 * N ** 1.6 # @pytest.mark.parametrize("values", cases) # def test_1d_t3_forward_CPU(values: torch.Tensor) -> None: From 31030916c973785809dd7893d71fc26ef16213fc Mon Sep 17 00:00:00 2001 From: Michael Eickenberg Date: Thu, 5 Oct 2023 14:47:15 -0400 Subject: [PATCH 3/4] FIX remove redundant previous tests --- tests/test_1d/test_forward_1d.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/tests/test_1d/test_forward_1d.py b/tests/test_1d/test_forward_1d.py index 404abf8..09245cf 100644 --- a/tests/test_1d/test_forward_1d.py +++ b/tests/test_1d/test_forward_1d.py @@ -59,13 +59,6 @@ def test_1d_t1_forward_CPU(values: torch.Tensor) -> None: assert finufft1D1_out.dtype is torch.complex128 assert against_torch.dtype == values.dtype - assert ( - torch.linalg.norm(finufft1D1_out - against_torch) / N**2 - ) == pytest.approx(0, abs=1e-06) - assert ( - torch.linalg.norm(finufft1D1_out - against_scipy) / N**2 - ) == pytest.approx(0, abs=1e-06) - abs_errors = torch.abs(finufft1D1_out - against_torch) l_inf_error = abs_errors.max() @@ -98,13 +91,6 @@ def test_1d_t2_forward_CPU(targets: torch.Tensor): / N ) - assert torch.norm(finufft_out - np.array(targets)) / N**2 == pytest.approx( - 0, abs=1e-05 - ) - assert torch.norm(finufft_out - against_torch) / N**2 == pytest.approx( - 0, abs=1e-05 - ) - abs_errors = torch.abs(finufft_out - against_torch) l_inf_error = abs_errors.max() l_2_error = torch.sqrt(torch.sum(abs_errors**2)) From 690ae836a2a3548742578b894c9a1125b99768ea Mon Sep 17 00:00:00 2001 From: Michael Eickenberg Date: Thu, 5 Oct 2023 14:51:18 -0400 Subject: [PATCH 4/4] FIX slightly relax a 2d test --- tests/test_2d/test_forward_2d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_2d/test_forward_2d.py b/tests/test_2d/test_forward_2d.py index 5cd74dd..4d902ea 100644 --- a/tests/test_2d/test_forward_2d.py +++ b/tests/test_2d/test_forward_2d.py @@ -50,7 +50,7 @@ def test_2d_t1_forward_CPU(N: int) -> None: l_2_error = torch.sqrt(torch.sum(abs_errors**2)) l_1_error = torch.sum(abs_errors) - assert l_inf_error < 5e-5 * N + assert l_inf_error < 6e-5 * N assert l_2_error < 1e-5 * N ** 2 assert l_1_error < 1e-5 * N ** 3