Repository navigation
add cupyx.scipy.ndimage.fourier_shift, fourier_gaussian, fourier_uniform - #3654
Conversation
|
Benchmarking indicates that it is up to ~200 times faster than SciPy for sufficiently large inputs. import numpy as np
import cupy as cp
import scipy.ndimage as ndi
from cupyx.scipy.ndimage import fourier_shift
from cupyx.time import repeat
print(" case | timing | acceleration")
print("------|--------|-------------")
for dtype in [np.complex64, np.complex128]:
for shape in [(16, 16), (512, 512), (4096, 4096), (256, 256, 256), (64, 64, 64, 64)]:
a = cp.random.randn(*shape).astype(dtype)
a = a + 1j * cp.random.randn(*shape).astype(dtype)
a_cpu = a.get()
perf_cpu = repeat(
ndi.fourier_shift, (a_cpu, 5), n_warmup=1, n_repeat=10
)
# print(f"shape={shape, np.dtype(dtype).name}, CPU | {perf_cpu}")
perf_gpu = repeat(fourier_shift, (a, 5), n_warmup=20, n_repeat=100)
accel = perf_cpu.cpu_times.mean() / perf_gpu.gpu_times.mean()
print(f"shape={shape, np.dtype(dtype).name}, GPU | {perf_gpu} | {accel}")
|
|
cc @coderforlife for awareness: I had already done this function for |
|
@grlee77 I have never actually used any of the |
|
I went ahead and added The test structure is nearly the same too. Mainly it is just the function name and the second argument name/range that is different across these three functions. |
|
These all seem way simpler than the scipy implementations (which I always thought was overkill). |
leofang
left a comment
There was a problem hiding this comment.
Looks like at least a large portion of fourier_uniform and fourier_gaussian can be factored out and reused, but I am not sure if it improves readability...
Yeah, actually these implementations when using NumPy instead of CuPy are faster than SciPy's in most cases. SciPy's C implementation wins in 1D, but for 2D/3D this separable approach can be 3-5 times faster. I think the SciPy approach is more justified for the elliptical filter case (not implemented in this PR). It is the only case that is non-separable along the axes (you have to compute the Euclidean distance from the f=0 coordinate at each frequency location). |
|
FTI, |
takagi
left a comment
There was a problem hiding this comment.
Thanks for implementing these functions! Would you check some minor comments?
| arr *= -2j * np.pi * shiftk / n | ||
| else: | ||
| arr = cupy.fft.fftfreq(ax_size) | ||
| arr = arr * (-2j * np.pi * shiftk) |
There was a problem hiding this comment.
| arr = arr * (-2j * np.pi * shiftk) | |
| arr *= (-2j * np.pi * shiftk) |
There was a problem hiding this comment.
In-place won't work here because arr is real-valued prior to the multiplication, but complex afterwards.
We can make it slightly more efficient if we duplicate some internals of fftfreq rather than calling fftfreq, but I thought that was probably not worth the added complexity. I did do that for the real-valued case above where rfftfreq basically just corresponds to arange, so wasn't complicated to replace.
|
pfnCI, test this please. |
|
Successfully created a job for commit 1ff2a91: |
|
Jenkins CI test (for commit 1ff2a91, target branch master) failed with status FAILURE. |
|
This should now be ready to test |
|
pfnCI, test this please. |
|
Successfully created a job for commit 0825801: |
|
Thanks! Merging will be done after the CI passes. |
|
Jenkins CI test (for commit 0825801, target branch master) failed with status FAILURE. |
|
I see the reason for the failure. The tests as written require scipy >= 1.4 to run due to use of the newer |
this is to support scipy < 1.4 the behavior of rfft/irfft is not identical for fftpack, so just skip the real-valued cases unless scipy >= 1.4.
|
Hopefully the most recent commit will fix the previous CI failure. I didn't see a really easy way to run the real-valued tests on older scipy because the behavior of the real-valued FFTs in scipy.fft behave like numpy.fft and not scipy.fftpack. We also can't use numpy.fft instead of scipy.fft because it always returns double precision results while scipy's does not. |
|
pfnCI, test this please. |
|
Successfully created a job for commit 52a0891: |
|
pfnCI, test this please. |
|
Successfully created a job for commit 52a0891: |
|
Jenkins CI test (for commit 52a0891, target branch master) succeeded! |
This adds the
fourier_shiftfunction from ndimage. These functions take a Fourier-domain image and apply the complex exponentials that would be needed to give a (circular) shift in the spatial domain. (i.e. based on the Fourier shift theorem).It seems like we might already have a utility like
_reshape_ndhere somewhere else, but I'm not sure. If desired we could move it tocupy.core.internal._get_output_fourier_complexwas copied from SciPyThe SciPy implementation is in C and could be emulated via an
ElementwiseKernel. This separable implementation only has to take the complex exponential of a 1d array along each axis rather than at every n-d element, though, so I think it might be faster.