Skip to content
This repository was archived by the owner on Sep 25, 2023. It is now read-only.

[REVIEW] Cache raw CuPy kernels in upfirdn - #26

Merged
awthomp merged 8 commits into
rapidsai:branch-0.13from
gmarkall:upfirdn_raw_cupy_kernel
Mar 11, 2020
Merged

awthomp merged 8 commits into
rapidsai:branch-0.13from
gmarkall:upfirdn_raw_cupy_kernel

Conversation

@gmarkall

@gmarkall gmarkall commented Mar 9, 2020

Copy link
Copy Markdown
Contributor

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

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
gmarkall requested a review from a team March 9, 2020 15:11
@awthomp awthomp added the 3 - Ready for Review Ready for review by team label Mar 9, 2020
@awthomp

awthomp commented Mar 9, 2020

Copy link
Copy Markdown
Member

@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 ms

From my reading:

  • Using raw time magic cells, the first execution of resample_poly with Numba CUDA kernels instead of raw CuPy kernels is ~8x slower (979 ms vs 154 ms)
  • On second execution, once the Numba CUDA kernel has been compiled and cached, it looks like Numba is 2x faster than CuPy raw kernels (5.85 ms vs 9.34 ms)

This is using a local checkout of this PR. Any idea what's going on?

@gmarkall

gmarkall commented Mar 9, 2020

Copy link
Copy Markdown
Contributor Author

I usually use %timeit instead, which runs the timed code multiple times and computes an average - I can't tell from the docs, but maybe %time only runs once and prints the time? https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-time - if this is the case I'd expect Numba to be slow on the first run, as you identified.

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:

ipython test.ipy

I see:

Timing with Numba
17.1 ms ± 15.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Timing with raw kernel
9.4 ms ± 27.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

This is with this branch on the same GPU (16GB V100). Do you see similar numbers with %timeit?

@awthomp

awthomp commented Mar 10, 2020

Copy link
Copy Markdown
Member

I'm seeing similar performance numbers with %%timeit. I think our confusion starts with %%time. I know we talked offline about the trades between time versus timeit and realistic perf metrics that can be expected by Python/cuSignal users using these libraries to build E2E workflows, but I think an in-depth study and blog post highlighting our assumptions and recommendations with @mnicely is a great next-step!

@awthomp

awthomp commented Mar 10, 2020

Copy link
Copy Markdown
Member

rerun tests

@awthomp

awthomp commented Mar 10, 2020

Copy link
Copy Markdown
Member

@gmarkall -- I've approved this PR, but it looks like the pytests are failing on resample_poly. Mind taking a look?

@gmarkall

Copy link
Copy Markdown
Contributor Author

@awthomp I shall do - will take a look tomorrow.

@awthomp

awthomp commented Mar 11, 2020

Copy link
Copy Markdown
Member

@awthomp I shall do - will take a look tomorrow.

Awesome! I'll look too. Looks like the resample_poly test is failing if one specifies both use_numba=True and use_numba=False along with explicitly setting the dtype to float32.

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)
@gmarkall

gmarkall commented Mar 11, 2020 •

Copy link
Copy Markdown
Contributor Author

It looks like it fails in all four cases:

  • use_numba=True, float32
  • use_numba=False, float32
  • use_numba=True, float64
  • use_numba=False, float64

Also I notice that scipy.signal.resample_poly returns a float64 array even if the input is float32, and the output differs slightly from the Numba and raw kernels. I'm not familiar with the maths here, but is it possible that float32 does not give enough precision?

I've tried changing all the Numba kernel dtypes back to being inferred (so they will be float64) and the raw kernel to use double arrays, and this seems to pass the tests for both the Numba and raw kernel again.

I'll pushed a commit which also adds testing of the raw kernel in addition to the Numba one.

@gmarkall

Copy link
Copy Markdown
Contributor Author

Also I notice that for float64, the gap between Numba and raw kernels closes quite a lot (from the commit message recently pushed):

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)

@awthomp

awthomp commented Mar 11, 2020

Copy link
Copy Markdown
Member

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.

@awthomp awthomp added 5 - Ready to Merge Testing and reviews complete, ready to merge and removed 3 - Ready for Review Ready for review by team labels Mar 11, 2020
@awthomp
awthomp merged commit d64df2f into rapidsai:branch-0.13 Mar 11, 2020
@BradReesWork BradReesWork added this to the 0.13 milestone Mar 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

5 - Ready to Merge Testing and reviews complete, ready to merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants