Skip to content

add PlanNd class for faster 2D and 3D FFTs - #1746

Merged
takagi merged 18 commits into
cupy:masterfrom
grlee77:cufft_plan_nd
Jan 7, 2019
Merged

takagi merged 18 commits into
cupy:masterfrom
grlee77:cufft_plan_nd

Conversation

@grlee77

@grlee77 grlee77 commented Oct 25, 2018

Copy link
Copy Markdown
Member

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, cufftMakePlanMany is 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_planning that can be set False to disable the n-dimensional planning.

The functions that benefit from this PR are:

cupy.fft.fft2
cupy.fft.ifft2
cupy.fft.fftn
cupy.fft.ifftn
cupyx.scipy.fftpack.fft2
cupyx.scipy.fftpack.ifft2
cupyx.scipy.fftpack.fftn
cupyx.scipy.fftpack.ifftn

This PR does not involve any changes to the CuPy public API aside from the addition of the cupy.fft.config module.

Caching of FFT plans can also provide an additional large performance boost, but that can be the topic of a separate PR.

@grlee77

grlee77 commented Oct 25, 2018

Copy link
Copy Markdown
Member Author

A simple script I used to benchmark cupy.fft.fftn with and without the nd planning enabled

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.

Acceleration Factors (nd vs. 1d)):
        (shape, axes, order, accel) = ((16,), (-1,), C, 1.112)
        (shape, axes, order, accel) = ((16, 16), (0,), C, 0.986)
        (shape, axes, order, accel) = ((16, 16), (1,), C, 0.993)
        (shape, axes, order, accel) = ((16, 16), (0, 1), C, 1.961)
        (shape, axes, order, accel) = ((16, 16, 16), (0,), C, 1.004)
        (shape, axes, order, accel) = ((16, 16, 16), (2,), C, 0.998)
        (shape, axes, order, accel) = ((16, 16, 16), (0, 1), C, 1.958)
        (shape, axes, order, accel) = ((16, 16, 16), (0, 2), C, 0.962)
        (shape, axes, order, accel) = ((16, 16, 16), (1, 2), C, 1.851)
        (shape, axes, order, accel) = ((16, 16, 16), (0, 1, 2), C, 3.076)
        (shape, axes, order, accel) = ((64,), (-1,), C, 1.000)
        (shape, axes, order, accel) = ((64, 64), (0,), C, 0.992)
        (shape, axes, order, accel) = ((64, 64), (1,), C, 0.991)
        (shape, axes, order, accel) = ((64, 64), (0, 1), C, 1.963)
        (shape, axes, order, accel) = ((64, 64, 64), (0,), C, 0.999)
        (shape, axes, order, accel) = ((64, 64, 64), (2,), C, 0.990)
        (shape, axes, order, accel) = ((64, 64, 64), (0, 1), C, 1.971)
        (shape, axes, order, accel) = ((64, 64, 64), (0, 2), C, 0.965)
        (shape, axes, order, accel) = ((64, 64, 64), (1, 2), C, 1.855)
        (shape, axes, order, accel) = ((64, 64, 64), (0, 1, 2), C, 2.939)
        (shape, axes, order, accel) = ((256,), (-1,), C, 1.017)
        (shape, axes, order, accel) = ((256, 256), (0,), C, 0.991)
        (shape, axes, order, accel) = ((256, 256), (1,), C, 1.000)
        (shape, axes, order, accel) = ((256, 256), (0, 1), C, 1.955)
        (shape, axes, order, accel) = ((256, 256, 256), (0,), C, 1.200)
        (shape, axes, order, accel) = ((256, 256, 256), (2,), C, 1.504)
        (shape, axes, order, accel) = ((256, 256, 256), (0, 1), C, 1.668)
        (shape, axes, order, accel) = ((256, 256, 256), (0, 2), C, 0.883)
        (shape, axes, order, accel) = ((256, 256, 256), (1, 2), C, 2.113)
        (shape, axes, order, accel) = ((256, 256, 256), (0, 1, 2), C, 0.977)
        (shape, axes, order, accel) = ((16,), (-1,), F, 4.348)
        (shape, axes, order, accel) = ((16, 16), (0,), F, 1.557)
        (shape, axes, order, accel) = ((16, 16), (1,), F, 0.987)
        (shape, axes, order, accel) = ((16, 16), (0, 1), F, 1.979)
        (shape, axes, order, accel) = ((16, 16, 16), (0,), F, 1.018)
        (shape, axes, order, accel) = ((16, 16, 16), (2,), F, 1.041)
        (shape, axes, order, accel) = ((16, 16, 16), (0, 1), F, 1.928)
        (shape, axes, order, accel) = ((16, 16, 16), (0, 2), F, 0.951)
        (shape, axes, order, accel) = ((16, 16, 16), (1, 2), F, 1.889)
        (shape, axes, order, accel) = ((16, 16, 16), (0, 1, 2), F, 2.981)
        (shape, axes, order, accel) = ((64,), (-1,), F, 1.004)
        (shape, axes, order, accel) = ((64, 64), (0,), F, 0.997)
        (shape, axes, order, accel) = ((64, 64), (1,), F, 1.009)
        (shape, axes, order, accel) = ((64, 64), (0, 1), F, 2.005)
        (shape, axes, order, accel) = ((64, 64, 64), (0,), F, 1.004)
        (shape, axes, order, accel) = ((64, 64, 64), (2,), F, 1.022)
        (shape, axes, order, accel) = ((64, 64, 64), (0, 1), F, 1.974)
        (shape, axes, order, accel) = ((64, 64, 64), (0, 2), F, 0.938)
        (shape, axes, order, accel) = ((64, 64, 64), (1, 2), F, 1.851)
        (shape, axes, order, accel) = ((64, 64, 64), (0, 1, 2), F, 2.800)
        (shape, axes, order, accel) = ((256,), (-1,), F, 0.983)
        (shape, axes, order, accel) = ((256, 256), (0,), F, 1.005)
        (shape, axes, order, accel) = ((256, 256), (1,), F, 1.006)
        (shape, axes, order, accel) = ((256, 256), (0, 1), F, 1.940)
        (shape, axes, order, accel) = ((256, 256, 256), (0,), F, 0.919)
        (shape, axes, order, accel) = ((256, 256, 256), (2,), F, 1.007)
        (shape, axes, order, accel) = ((256, 256, 256), (0, 1), F, 1.628)
        (shape, axes, order, accel) = ((256, 256, 256), (0, 2), F, 1.166)
        (shape, axes, order, accel) = ((256, 256, 256), (1, 2), F, 1.790)
        (shape, axes, order, accel) = ((256, 256, 256), (0, 1, 2), F, 2.772)

@toslunar toslunar added the st:blocked-by-another-pr Blocked by another pull-request label Nov 21, 2018
@takagi takagi self-assigned this Dec 10, 2018
@leofang

leofang commented Dec 12, 2018

Copy link
Copy Markdown
Member

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...

Comment thread cupy/fft/fft.py
return 'nd'


def _default_fft_func(a, s=None, axes=None):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The in-place operation is currently disabled because there's no overwrite_x argument here. Is this an oversight?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @grlee77, I can run successfully fftn in cupyx.scipy.fftpack with overwrite_x=True.

@leofang

leofang commented Dec 15, 2018

Copy link
Copy Markdown
Member

@grlee77 could you please make fftpack available in cupyx.scipy? Add this line in cupyx/scipy/__init__.py should suffice:

from cupyx.scipy import fftpack  # NOQA

Thanks!

@grlee77

grlee77 commented Dec 15, 2018

Copy link
Copy Markdown
Member Author

okay. I have added the import

Comment thread cupy/fft/fft.py Resolved
@takagi

takagi commented Dec 17, 2018

Copy link
Copy Markdown
Contributor

@grlee77 So sorry for my late response. I'll handle your PR to be successfully merged to CuPy master following our CI process.

@takagi takagi added the cat:feature New features/APIs label Dec 18, 2018
@grlee77

grlee77 commented Dec 18, 2018

Copy link
Copy Markdown
Member Author

now that #1745 has been merged, I rebased this on current master.
@leofang: I removed the fftpack import in cupyx.scipy __init__ as it was not in the other PR and I think the intention there was to mirror the scipy behavior (If I import scipy this does not automatically import the fftpack module)

@grlee77

grlee77 commented Dec 18, 2018

Copy link
Copy Markdown
Member Author

It looks like CI is failing, most likely related to use of cimport numpy as cnp in cufft.pyx. This was working previously when I tested locally, but I will take a look later in the day.

@leofang

leofang commented Dec 18, 2018

Copy link
Copy Markdown
Member

@leofang: I removed the fftpack import in cupyx.scipy __init__ as it was not in the other PR and I think the intention there was to mirror the scipy behavior (If I import scipy this does not automatically import the fftpack module)

OK, if mirroring is required. I would hope for easier access though...

@grlee77

grlee77 commented Dec 18, 2018

Copy link
Copy Markdown
Member Author

OK, if mirroring is required. I would hope for easier access though...

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 scipy.special also do not have imports there, so I assumed maybe it was intentional.

@grlee77

grlee77 commented Dec 18, 2018

Copy link
Copy Markdown
Member Author

Okay, I refactored the Cython code a bit so that there is no longer any cimport of NumPy and now the CI is happy.

@takagi takagi removed the st:blocked-by-another-pr Blocked by another pull-request label Dec 19, 2018
@takagi

takagi commented Dec 19, 2018

Copy link
Copy Markdown
Contributor

Jenkins, test this please.

@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit 06cd00b, target branch master) failed with status FAILURE.
(For contributors, please wait until the reviewer confirms the details of the error.)

@takagi takagi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you fix some typos?

Comment thread cupy/fft/fft.py Resolved
Comment thread cupy/fft/fft.py Resolved
@takagi

takagi commented Dec 21, 2018

Copy link
Copy Markdown
Contributor

Jenkins, test this please.

@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit 06cd00b, target branch master) failed with status FAILURE.
(For contributors, please wait until the reviewer confirms the details of the error.)

@takagi takagi added this to the v6.0.0b2 milestone Dec 21, 2018
@grlee77

grlee77 commented Jan 4, 2019

Copy link
Copy Markdown
Member Author

any details on what failed during CI on this one?

@takagi

takagi commented Jan 7, 2019

Copy link
Copy Markdown
Contributor

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).

@takagi

takagi commented Jan 7, 2019

Copy link
Copy Markdown
Contributor

LGTM! Now I will merge this PR to the master shortly.

@takagi
takagi merged commit 74fa049 into cupy:master Jan 7, 2019
@leofang

leofang commented Jan 7, 2019

Copy link
Copy Markdown
Member

Thanks @grlee77 @takagi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat:feature New features/APIs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants