Repository navigation
[REVIEW] Cache raw CuPy kernels in upfirdn - #26
Conversation
Compiling a kernel on every call to _raw_apply_1d is costly - this commit creates a cache of compiled raw kernels. The cache is initialized on the first call to _raw_apply_1d, and and subsequent calls used the cached kernels. With this commit, I see the following timings for the resample_poly example from the signaltools_examples notebook: Numba: CPU times: user 288 ms, sys: 52 ms, total: 340 ms Wall time: 363 ms CuPy: CPU times: user 116 ms, sys: 4 ms, total: 120 ms Wall time: 120 ms A 3x speedup for the raw kernel compared to Numba.
|
@gmarkall and @mnicely -- This is probably me not totally understanding Python timing w.r.t. Numba and CuPy, but I'm getting the following results on a 16GB V100, SXM-2: In [4]: %%time
...: cf = cusignal.resample_poly(gy, resample_up, resample_down, window=('kai
...: ser', 0.5), use_numba=True)
...:
...:
CPU times: user 244 ms, sys: 52.8 ms, total: 297 ms
Wall time: 979 ms
In [5]: %%time
...: cf = cusignal.resample_poly(gy, resample_up, resample_down, window=('kai
...: ser', 0.5), use_numba=True)
...:
...:
CPU times: user 5.37 ms, sys: 1.13 ms, total: 6.5 ms
Wall time: 5.85 ms
In [6]: %%time
...: cf = cusignal.resample_poly(gy, resample_up, resample_down, window=('kai
...: ser', 0.5), use_numba=False)
...:
...:
CPU times: user 107 ms, sys: 4.79 ms, total: 112 ms
Wall time: 154 ms
In [7]: %%time
...: cf = cusignal.resample_poly(gy, resample_up, resample_down, window=('kai
...: ser', 0.5), use_numba=False)
...:
...:
CPU times: user 5.39 ms, sys: 4.43 ms, total: 9.83 ms
Wall time: 9.34 msFrom my reading:
This is using a local checkout of this PR. Any idea what's going on? |
|
I usually use When I run: import cupy as cp
import cusignal
from scipy import signal
import numpy as np
start = 0
stop = 10
num = int(1e8)
resample_up = 2
resample_down = 3
gx = cp.linspace(start, stop, num, endpoint=False, dtype=np.float32)
gy = cp.cos(-gx**2/6.0)
print("Timing with Numba")
%timeit cusignal.resample_poly(gy, resample_up, resample_down, window=('kaiser', 0.5), use_numba=True)
print("Timing with raw kernel")
%timeit cusignal.resample_poly(gy, resample_up, resample_down, window=('kaiser', 0.5), use_numba=False)with: I see: This is with this branch on the same GPU (16GB V100). Do you see similar numbers with |
|
I'm seeing similar performance numbers with |
|
rerun tests |
|
@gmarkall -- I've approved this PR, but it looks like the pytests are failing on resample_poly. Mind taking a look? |
|
@awthomp I shall do - will take a look tomorrow. |
Awesome! I'll look too. Looks like the |
Tests seem to fail with values that differ from
scipy.signal.resample_poly for both the Numba and raw kernels when using
a dtype of float32. This commit amends the CUDA kernel back to inferring
types (so when passed a float64 it will type the function for float64)
and the raw kernel to use doubles. This appears to fix all tests.
This commit also adds testing of the raw kernel as well as the default
Numba one.
Timings for a simple test:
start = 0
stop = 10
num = int(1e8)
resample_up = 2
resample_down = 3
gx = cp.linspace(start, stop, num, endpoint=False)
gy = cp.cos(-gx**2/6.0)
cusignal.resample_poly(gy, resample_up, resample_down,
window=('kaiser', 0.5),
use_numba=use_numba)
with this commit are:
use_numba=True::
14.3 ms ± 2.03 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
use_numba=False:
12.2 ms ± 25.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
|
It looks like it fails in all four cases:
Also I notice that I've tried changing all the Numba kernel dtypes back to being inferred (so they will be I'll pushed a commit which also adds testing of the raw kernel in addition to the Numba one. |
…upfirdn_raw_cupy_kernel
|
Also I notice that for |
|
Thanks, @gmarkall! I'm going to mark this as 'ready to merge' since the tests now pass, and I'm seeing a performance boost for raw CuPy vs Numba. Like you suspect, I assume the delta is due to a difference in precision and accumulating errors, so adjusting the tolerance to pass tests isn't necessarily the answer. Let's merge this and continue to investigate, as appropriate. |
Follows on from #25, to be merged afterwards.
Compiling a kernel on every call to
_raw_apply_1d is costly- this PR creates a cache of compiled raw kernels. The cache is initialized on the first call to _raw_apply_1d, and and subsequent calls use the cached kernels.With this PR, I see the following timings for the
resample_polyexample from the signaltools_examples notebook:A 3x speedup for the raw kernel compared to Numba.