Skip to content

Add scipy.fft to cupyx - #2355

Merged
toslunar merged 17 commits into
cupy:masterfrom
peterbell10:scipy_fft
Dec 3, 2019
Merged

toslunar merged 17 commits into
cupy:masterfrom
peterbell10:scipy_fft

Conversation

@peterbell10

@peterbell10 peterbell10 commented Aug 1, 2019 •

Copy link
Copy Markdown
Contributor

SciPy version 1.4 will have a new submodule scipy.fft with an interface very similar to numpy.fft. This adds a subset of that interface to cupyx.scipy and also adds uarray-based backend support allowing cupy FFTs to be called through the scipy.fft interface e.g.

import cupy
import cupyx.scipy.fft as cu_fft
import scipy.fft

x = cupy.arange(10)
scipy.fft.fft(x)  # ValueError: object __array__ method not producing an array

with scipy.fft.set_backend(cu_fft):
    scipy.fft.fft(x)  # Calls into cupyx, returning a cupy array

Tests are currently failing but at least some of the failures seem to be bugs in cupy.fft. I need to investigate further.

@grlee77

grlee77 commented Aug 1, 2019

Copy link
Copy Markdown
Member

Thanks, Peter. I will take a look soon.

Some additional context for the CuPy team: This work is associated with a SciPy Google Summer of Code project. Any feedback on the implementation is greatly appreciated. Relevant SciPy PRs for additional details are:
new scipy.fft module: scipy/scipy#10238
uarray-based backend support: scipy/scipy#10383

Starting with SciPy 1.4.0, the recommendation to users is to call FFTs via the new scipy.fft module in preference to scipy.fftpack. The fftpack interface will continue to be available for backwards compatibility.

@leofang

leofang commented Aug 1, 2019

Copy link
Copy Markdown
Member

Hi @grlee77 I suppose it'd work like cupy.fft in which 1D/ND cuFFT plans are generated under the hood?

@grlee77

grlee77 commented Aug 1, 2019 •

Copy link
Copy Markdown
Member

Yes, this is using the same underlying implementations from cupy.fft so is not adding too much new complexity here. There are a few differences in API from NumPy/scipy.fftpack. Off the top of my head, scipy.fft supports an overwrite_x argument like scipy.fftpack but the R2C/C2R transforms operate more like the ones in numpy.fft than the scipy.fftpack ones. This PR provides the new API for CuPy users and allows CuPy to become an FFT backend for SciPy as in the example Peter gave above.

@peterbell10

Copy link
Copy Markdown
Contributor Author

Just to add to that. The _fft and _fftn functions in cupy.fft all support overwrite_x already, so this is just a slightly different interface to those same functions.

@leofang

leofang commented Aug 1, 2019

Copy link
Copy Markdown
Member

I see, thanks for the clarification, guys.

Do you know what's the official long term plan for scipy.fftpack? Would it be deprecated after scipy.fft becomes polished? I wonder if it's possible to add a plan argument to scipy.fft (as we did before, see #1942, #2033), so that explicit cuFFT plans can still be used. Does scipy.fft.set_backend set up any rules against changes like this in the scipy.fft API?

@grlee77

grlee77 commented Aug 1, 2019

Copy link
Copy Markdown
Member

also including @larsoner here (not sure if he already follows this repo)

@peterbell10

Copy link
Copy Markdown
Contributor Author

Do you know what's the official long term plan for scipy.fftpack?

There are no plans to deprecate it currently. It will be documented as "long term legacy".

I wonder if it's possible to add a plan argument to scipy.fft (as we did before, see #1942, #2033), so that explicit cuFFT plans can still be used. Does scipy.fft.set_backend set up any rules against changes like this in the scipy.fft API?

You won't be able to pass a plan argument through scipy.fft unless the argument is added to scipy itself.

@leofang

leofang commented Aug 1, 2019

Copy link
Copy Markdown
Member

You won't be able to pass a plan argument through scipy.fft unless the argument is added to scipy itself.

OK, perhaps it's better to move the further discussion on plan to a separate issue: #2356.

peterbell10 and others added 3 commits August 5, 2019 14:22
By sorting the axes before applying _cook_shape it would resize the wrong axes.
* API: add missing norm kwarg to irfft
* BUG: _fftn handle axes=None case when shape is specified
Comment thread docs/source/reference/scipy_fft.rst Resolved
@peterbell10

Copy link
Copy Markdown
Contributor Author

I still have some test failures locally for rfft and hfft but I've confirmed that the cupy.fft tests are failing in the same way on master so it can't be my code. @grlee77 has also confirmed that these tests pass on his machine.

Question: Should I add tests for the scipy.fft backend functionality, given that it would require a recent weekly build of SciPy to run?

@larsoner

larsoner commented Aug 7, 2019

Copy link
Copy Markdown
Contributor

Question: Should I add tests for the scipy.fft backend functionality, given that it would require a recent weekly build of SciPy to run?

FWIW many other repositories I know of and/or work on use a line like the following in some Travis build:

pip install -f "https://7933911d6844c6c53a7d-47bd50c35cd79bd838daf386af554a83.ssl.cf2.rackcdn.com" --pre numpy scipy;

It helps ensure that there are no unhappy surprises leading up to new NumPy and SciPy releases.

It helps to add this functionality in a PR separate from whatever functionality actually needs it, though, since using the weekly (?) builds themselves already sometimes exposes issues.

@grlee77

grlee77 commented Aug 7, 2019

Copy link
Copy Markdown
Member

Question: Should I add tests for the scipy.fft backend functionality, given that it would require a recent weekly build of SciPy to run?

I would skip that for now unless one of the core devs here expresses support for adding it. I did not test the backend part locally yet, but can do that soon.

As is, the automated CI on Travis/Appveyor does not actually run any tests on the GPU. Once you think this is ready, you can ping the maintainers here to trigger a build on their internal Jenkins CI which tests on actual GPU hardware.

@leofang

leofang commented Nov 22, 2019

Copy link
Copy Markdown
Member

I am wondering whether it makes sense or not to expose cupyx.scipy.fftpack.get_fft_plan() to the cupyx.scipy.fft namespace. Thoughts?

@toslunar

Copy link
Copy Markdown
Member

It looks good to make cupyx.scipy.fft.get_fft_plan available as an experimental feature.

@toslunar

Copy link
Copy Markdown
Member

Jenkins, test this please.

@pfn-ci-bot

Copy link
Copy Markdown
Collaborator

Successfully created a job for commit 3095913:

@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit 3095913, target branch master) failed with status FAILURE.

@leofang

leofang commented Nov 25, 2019

Copy link
Copy Markdown
Member

Just confirmed that this new feature can work seamlessly with the multi-GPU support in #2644 🎉

import cupy
import cupyx.scipy.fft as cu_fft
import scipy.fft

x = cupy.arange(64)
cupy.fft.config.use_multi_gpus = True  # from #2644
cupy.fft.config.set_cufft_gpus(2)      # from #2644

with scipy.fft.set_backend(cu_fft):
    scipy.fft.fft(x)  # This runs on 2 GPUs!

@hameerabbasi

Copy link
Copy Markdown

We were proposing a change to uarray to remove the __ua_convert__ protocol, but we evaluated that everything that one had to do with that protocol could be done by CuPy manually without too much effort. Would that be okay?

---------------------------
1. The boolean switch ``cupy.fft.config.enable_nd_planning`` also affects the FFT functions in this module, see :doc:`./fft`. Moreover, as with other FFT modules in CuPy, FFT functions in this module can take advantage of an existing cuFFT plan (returned by :func:`cupyx.scipy.fftpack.get_fft_plan`) when used as a context manager.

2. Like in ``scipy.fft``, all FFT functions in this module have an optional argument ``overwrite_x`` (default is ``False``), which has the same semantics as in ``scipy.fft``: when it is set to ``True``, the input array ``x`` *can* (not *will*) be overwritten arbitrary. This is not an in-place FFT, the user should always use the return value from the functions, e.g. ``x = cupyx.scipy.fft.fft(x, ..., overwrite_x=True, ...)``.

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.

typo arbitrary -> arbitrarily

@grlee77

grlee77 commented Nov 26, 2019

Copy link
Copy Markdown
Member

We were proposing a change to uarray to remove the ua_convert protocol, but we evaluated that everything that one had to do with that protocol could be done by CuPy manually without too much effort. Would that be okay?

You mean remove that protocol from uarray itself, not just this PR?

@grlee77

grlee77 commented Nov 26, 2019 •

Copy link
Copy Markdown
Member

To make things a bit more concrete regarding coerce for those unfamiliar with uarray.

@peterbell10 & @hameerabbasi, please correct if I have gotten anything wrong here

As this currently is in the PR, with the following setup:

import numpy as np
import cupy
import cupyx.scipy.fft as cu_fft
import scipy.fft

x_np = np.arange(10)
x_cp = cupy.asarray(x_np)

case 1: (numpy input, no coercion or fallback allowed)

with scipy.fft.set_backend(cu_fft, coerce=False, only=True):
    z = scipy.fft.fft(x_np)

raises BackendNotImplementedError because coercion from NumPy to CuPy is not allowed and the only kwarg tells scipy.fft that it is not allowed to fall back to the pocketfft based implementation.

case 2: (no coercian, but fallback is allowed)

with scipy.fft.set_backend(cu_fft, coerce=False, only=False):
    z = scipy.fft.fft(x_np)

This will succeed, but will have actually used pocketfft rather than cupy and z will be a NumPy array

case 3: (coercion allowed)

with scipy.fft.set_backend(cu_fft, coerce=True, only=True):
    z = scipy.fft.fft(x_np)

Now with coerce=True, x will get converted to a cupy.ndarray and then the cuFFT implementation will be used. z will be a cupy.ndarray

case 4: (no coercion or fallback is necessary or allowed)

with scipy.fft.set_backend(cu_fft, coerce=False, only=True):
    z = scipy.fft.fft(x_cp)

In this case the input is already a cupy.ndarray so computation will be with cuFFT and the output is a cupy.ndarray.

@grlee77

grlee77 commented Nov 26, 2019

Copy link
Copy Markdown
Member

In CuPy functions typically do not support numpy array inputs and will raise an error in that case. So I think the behavior for case 4 in my pervious comment (no coercion allowed) would not be surprising to CuPy users. Is that what you have in mind @hameerabbasi?

@hameerabbasi

Copy link
Copy Markdown

The changes for the end-user would be that the coerce= argument would be removed completely. And the behaviour for devs would be that they'd have to manually do what __ua_convert__ was doing.

@hameerabbasi

hameerabbasi commented Nov 27, 2019 •

Copy link
Copy Markdown

Inside SciPy, the typical function would look like this:

@ua.create_multimethod("numpy.scipy.fft") # Or similar
def func(signature):
    pass

@toslunar

toslunar commented Dec 3, 2019

Copy link
Copy Markdown
Member

Jenkins, test this please.

@pfn-ci-bot

Copy link
Copy Markdown
Collaborator

Successfully created a job for commit df54d36:

@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit df54d36, target branch master) succeeded!

@toslunar toslunar left a comment

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.

LGTM

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.

9 participants