Repository navigation
add PlanNd class for faster 2D and 3D FFTs - #1746
Conversation
|
A simple script I used to benchmark import cupy
import numpy as np
import time
from cupy.fft import config
print("Acceleration Factors (nd vs. 1d)):")
for order in ['C', 'F']:
for sz in (16, 64, 256):
for ndim in [1, 2, 3]:
shape = (sz, ) * ndim
if np.prod(shape) < 1e3:
nruns = 4000
if np.prod(shape) < 1e6:
nruns = 400
else:
nruns = 40
if ndim == 1:
_axes = [(-1,)]
if ndim == 2:
_axes = [(0,), (1,), (0, 1)]
if ndim == 3:
_axes = [(0,), (2,), (0, 1), (0, 2), (1, 2), (0, 1, 2)]
for axes in _axes:
a = cupy.random.randn(*shape).astype(np.complex64)
if order == 'F':
a = cupy.asfortranarray(a)
cupy.fft.fftn(a, axes=axes)
config.enable_nd_planning = False
tstart = time.time()
for t in range(nruns):
cupy.fft.fftn(a, axes=axes)
dur_1d = (time.time() - tstart) / nruns
config.enable_nd_planning = True
tstart = time.time()
for t in range(nruns):
cupy.fft.fftn(a, axes=axes)
dur_nd = (time.time() - tstart) / nruns
print("\t(shape, axes, order, accel) = ({}, {}, {}, {:0.3f})".format(shape, axes, order, dur_1d / dur_nd))which gives the following result on my system. The last number on each line is the acceleration factor for the nd planning case. It is never substantially slower and is in some cases 2-3 times faster than separable 1D planning. |
da987b9 to
be791c3
Compare
|
I modified @grlee77's script to test the most relevant scenario in our app. Not bad for uncached plan: # use cuda event for timing
import cupy
import numpy as np
from cupy.fft import config
print("Acceleration Factors (nd vs. 1d)):")
for order in ['C']:
for sz in (512, 1024, 2048, 4096, 16000):
ndim = 3
for dim in (128, 192, 256, 320):
shape = [sz, ] * ndim
for i in [1, 2]:
shape[i] = dim
shape = tuple(shape)
nruns = 20
_axes = [(1, 2)]
for axes in _axes:
a = cupy.random.randn(*shape).astype(np.complex64)
if order == 'F':
a = cupy.asfortranarray(a)
else:
a = cupy.ascontiguousarray(a)
cupy.fft.fftn(a, axes=axes)
b = a.copy()
start = cupy.cuda.Event()
end = cupy.cuda.Event()
config.enable_nd_planning = False
dur_1d = 0.
for t in range(nruns):
start.record()
cupy.fft.fftn(a, axes=axes)
end.record()
end.synchronize()
dur_1d += cupy.cuda.get_elapsed_time(start, end)
dur_1d /= nruns
config.enable_nd_planning = True
dur_nd = 0.
for t in range(nruns):
start.record()
cupy.fft.fftn(b, axes=axes)
end.record()
end.synchronize()
dur_nd += cupy.cuda.get_elapsed_time(start, end)
dur_nd /= nruns
speedup = dur_1d / dur_nd
print("\t(shape, axes, order, dur_1d, dur_nd, accel) = ({}, {}, {}, {:0.3f} ms, {:0.3f} ms, {:0.3f})".format(shape, axes, order, dur_1d, dur_nd, speedup))Results: Acceleration Factors (nd vs. 1d)):
(shape, axes, order, dur_1d, dur_nd, accel) = ((512, 128, 128), (1, 2), C, 2.466 ms, 1.566 ms, 1.574)
(shape, axes, order, dur_1d, dur_nd, accel) = ((512, 192, 192), (1, 2), C, 2.705 ms, 2.075 ms, 1.304)
(shape, axes, order, dur_1d, dur_nd, accel) = ((512, 256, 256), (1, 2), C, 3.740 ms, 2.531 ms, 1.478)
(shape, axes, order, dur_1d, dur_nd, accel) = ((512, 320, 320), (1, 2), C, 5.847 ms, 3.373 ms, 1.733)
(shape, axes, order, dur_1d, dur_nd, accel) = ((1024, 128, 128), (1, 2), C, 2.451 ms, 1.806 ms, 1.357)
(shape, axes, order, dur_1d, dur_nd, accel) = ((1024, 192, 192), (1, 2), C, 4.067 ms, 2.556 ms, 1.591)
(shape, axes, order, dur_1d, dur_nd, accel) = ((1024, 256, 256), (1, 2), C, 7.335 ms, 3.747 ms, 1.957)
(shape, axes, order, dur_1d, dur_nd, accel) = ((1024, 320, 320), (1, 2), C, 20.474 ms, 4.984 ms, 4.108)
(shape, axes, order, dur_1d, dur_nd, accel) = ((2048, 128, 128), (1, 2), C, 3.509 ms, 2.584 ms, 1.358)
(shape, axes, order, dur_1d, dur_nd, accel) = ((2048, 192, 192), (1, 2), C, 8.039 ms, 3.892 ms, 2.065)
(shape, axes, order, dur_1d, dur_nd, accel) = ((2048, 256, 256), (1, 2), C, 14.477 ms, 6.184 ms, 2.341)
(shape, axes, order, dur_1d, dur_nd, accel) = ((2048, 320, 320), (1, 2), C, 23.074 ms, 8.980 ms, 2.570)
(shape, axes, order, dur_1d, dur_nd, accel) = ((4096, 128, 128), (1, 2), C, 6.846 ms, 3.438 ms, 1.991)
(shape, axes, order, dur_1d, dur_nd, accel) = ((4096, 192, 192), (1, 2), C, 24.936 ms, 7.006 ms, 3.559)
(shape, axes, order, dur_1d, dur_nd, accel) = ((4096, 256, 256), (1, 2), C, 28.899 ms, 11.565 ms, 2.499)
(shape, axes, order, dur_1d, dur_nd, accel) = ((4096, 320, 320), (1, 2), C, 73.326 ms, 17.473 ms, 4.197)
(shape, axes, order, dur_1d, dur_nd, accel) = ((16000, 128, 128), (1, 2), C, 34.545 ms, 11.103 ms, 3.111)
(shape, axes, order, dur_1d, dur_nd, accel) = ((16000, 192, 192), (1, 2), C, 68.365 ms, 23.713 ms, 2.883)
...rest is out of memory... |
| return 'nd' | ||
|
|
||
|
|
||
| def _default_fft_func(a, s=None, axes=None): |
There was a problem hiding this comment.
The in-place operation is currently disabled because there's no overwrite_x argument here. Is this an oversight?
There was a problem hiding this comment.
I don't think so. _default_fft_func is just a utility to decide whether to use an nD or a 1D plan. The plan itself is the same whether or not overwrite_x is being used so we don't need to know that to choose the plan type. It is just the number and position of the axes that determine if an n-d plan is feasible.
I did not add overwrite_x to the numpy functions because that is not part of the numpy API, but I did add use of nD plans to the scipy.fftpack interfaces as well. See for example the lines here:
https://github.com/cupy/cupy/pull/1746/files#diff-9b4df5bba4da521be6f03098a67016a8R137
There was a problem hiding this comment.
Thanks, @grlee77, I can run successfully fftn in cupyx.scipy.fftpack with overwrite_x=True.
|
@grlee77 could you please make from cupyx.scipy import fftpack # NOQAThanks! |
|
okay. I have added the import |
|
@grlee77 So sorry for my late response. I'll handle your PR to be successfully merged to CuPy master following our CI process. |
add matching check_output_array method to Plan1d
_cook_shape: preserve dtype if requested
add tests for _default_plan_type
ffb4d6d to
21d530d
Compare
|
It looks like CI is failing, most likely related to use of |
OK, if mirroring is required. I would hope for easier access though... |
revert changes to the CI scripts
I am not sure if there is a strict policy one way or the other (I am a fairly new contributor here). It doesn't bother me to have it, but it looks like some of the other submodules like |
|
Okay, I refactored the Cython code a bit so that there is no longer any |
|
Jenkins, test this please. |
|
Jenkins CI test (for commit 06cd00b, target branch master) failed with status FAILURE. |
takagi
left a comment
There was a problem hiding this comment.
Would you fix some typos?
|
Jenkins, test this please. |
|
Jenkins CI test (for commit 06cd00b, target branch master) failed with status FAILURE. |
Co-Authored-By: grlee77 <[email protected]>
|
any details on what failed during CI on this one? |
|
I checked the CI result and the failure was not relevant to the change of this PR (caused by a nondeterministic test and its tolerance in a regression test with Chainer). |
|
LGTM! Now I will merge this PR to the master shortly. |
This PR is based on top of #1745, so that one should be reviewed first.
For n-dimensional data where 2 or 3 axes are transformed,
cufftMakePlanManyis faster than applying separable 1D plans. This PR implements this capability. This benefits of this were previously discussed in #1669.The primary downside of an n-dimensional plan vs separable 1d plans is increased GPU memory usage for the plan itself. For this reason, I made a global configuration flag at
cupy.fft.config.enabled_nd_planningthat can be set False to disable the n-dimensional planning.The functions that benefit from this PR are:
This PR does not involve any changes to the CuPy public API aside from the addition of the
cupy.fft.configmodule.Caching of FFT plans can also provide an additional large performance boost, but that can be the topic of a separate PR.