Repository navigation
Adding scipy.signal functions that are simple wrappers of ndimage functions - #3645
Conversation
|
Thanks for the ping, @leofang! @coderforlife - please check out cuSignal. We already have support for many of these functions (lfilter on GPU is close to impossible, though!) and would love to have you contribute. |
|
I know those are there, but I thought this scipy was supposed to be near duplication of scipy and the what would be (or is) in a scikit would be in cuSignal, cuImage, or similar. Missing them here is a bit confusing. Also, the functions in this commit are essentially freebies since they just call the ndimage functions after doing a small amount with the arguments (like checking). I have been struggling with lfilter as well... best parallelization so far is with each signal/channel. But I have a few ideas, although they may not pan out. |
I'll default to whatever the core CuPy devs want. cuSignal heavily depends on this wonderful library, so even wrapping functions core to CuPy within cuSignal is totally fine with me. As for the heavy-weight signal processing functions, like Here's our work with Here's our |
I implemented the specific IIR filters for the import cupy as cp
import numpy as np
import scipy.ndimage as ndi
from cupyimg.scipy.ndimage import spline_filter1d
d = cp.cuda.Device()
a = np.random.randn(4096, 4096).astype(np.float32)
a_gpu = cp.asarray(a)
%timeit ndi.spline_filter1d(a)
%timeit spline_filter1d(a_gpu); d.synchronize()Here the CPU time was: If I instead ravel the same arrays so that they are 1D, the CPU time becomes For reference: The spline kernel code is here and the configuration of RawKernel and block/grid is here. I think the kernel is basically the same type of code as |
|
@grlee77 My currently version only parallelizes across the signals like you suggest. @awthomp I looked through your code and I was starting to approach something similar with a few differences. It seems as though the |
|
@awthomp I have had some success with this! By having the I have now created 3 versions:
I tested with 2^20 elements and started at having an equal number of signals and samples (both 1024) and then reduced signals while increasing the samples until getting to only 1 signal with 2^20 samples. The
Overall, v2 and v3 are between 15% and 44% of the original's time. There is a strange behavior in the v2 is faster near the bottom and v3 is faster near the top. I am really curious why this is (it also has a bug so maybe those timings aren't good anyways). Also, everyone sucks at 1x however v1 actually is faster at 1x than 2x. For reference, all of these take 6.5-6.65 ms with BTW, all of these version support ND data in the same way the scipy does (one axis is considered the samples, all other axes are signals). Finally, I tried against your implementation (only the last row up there since yours only supports 1D), and yours takes 738 ms vs 352 (v1) or 156 (v2 and v3). I have no idea why yours is so much slower. But, for me, I haven't tried adding |
|
@awthomp Another benefit of the new approach is that |
|
@coderforlife so happy to see you making progress. I’ll be in and out over the next two weeks for vacation. These optimizations were all due to the great work from @mnicely, and I’m sure he’ll be able to help. |
|
I think we should move this discussion out of this PR since it has nothing to do with these first few filters. For now, just need approval of current 5 filters which all just utilize the |
|
Seems as though the signal processing experts are okay with this. It really is just mapping some |
takagi
left a comment
There was a problem hiding this comment.
Very sorry for the late review. Would you check the comments?
|
|
||
| try: | ||
| import scipy.signal # NOQA | ||
| except ImportError: | ||
| pass |
There was a problem hiding this comment.
Would you remove these lines?
| try: | |
| import scipy.signal # NOQA | |
| except ImportError: | |
| pass |
There was a problem hiding this comment.
Removing these will cause all new tests to fail because they won't be able to use cupyx.scipy.signal or scipy.signal (used as scp.signal later with scp either cupyx.scipy or scipy).
There was a problem hiding this comment.
I see. @testing.numpy_cupy_allclose imports just scipy and cupyx.scipy packages only, and scipy.signal and cupyx.scipy.signal can't be seen.
|
|
||
| import cupyx.scipy.signal # NOQA | ||
|
|
||
| try: | ||
| import scipy.signal # NOQA | ||
| except ImportError: | ||
| pass |
There was a problem hiding this comment.
Would you remove these lines?
| import cupyx.scipy.signal # NOQA | |
| try: | |
| import scipy.signal # NOQA | |
| except ImportError: | |
| pass |
There was a problem hiding this comment.
Removing these will cause all tests to fail because they won't be able to use cupyx.scipy.signal or scipy.signal (used as scp.signal later with scp either cupyx.scipy or scipy).
|
No problem, I understand it must be busy with the recent releases. I fixed all of the issues except the ones where if I remove those lines everything fails. Also, I am going to try right now to add support for complex dtypes (support actually already exists, it just isn't directly accessible). |
|
So I was able to fairly easily add complex support to both |
|
Thanks for updating the changes, and it is fine to leave complex unsupported here. |
|
pfnCI, test this please. |
|
Successfully created a job for commit 690eb26: |
|
Jenkins CI test (for commit 690eb26, target branch master) succeeded! |
|
LGTM! |
|
@coderforlife We're seeing irrelevant PRs (#3730, #3735) failing at |
|
It turns out that there is a bug in scipy. Way down in the source code for For the moment I have submitted the issue at scipy: scipy/scipy#12691. I think in the mean time we should add a check in |
|
Great, thanks for pinpointing the issue so quickly, @coderforlife! I am wondering if the errors could happen randomly? It's not always reproducible on the failing CI (for example, this PR went in just fine, and a few other PRs too). |
|
Well scipy's |
|
Yeah can't reproduced it locally... |
|
Different memory allocator or something. |
|
See #3750. |
I have implemented most of
scipy.signal.signaltools. This is the first batch that should be the most straightforward since it is just utilizing the ndimage functions which includes:wienerorder_filtermed_filtmed_filt2dsepfir2d(which is actually in bsplines.py)Next batch will be some of the utility functions and functions like
hilbert. After that will beconvolveand friends (but those are dependent on #3614). After that, there will belfilterandsosfiltand friends. By then I may have the splines completed as well.Currently there are no formal tests for these but those are coming shortly.