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

[REVIEW] Implementation of Raw CuPy Module for upfirdn - #25

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

awthomp merged 11 commits into
rapidsai:branch-0.13from
awthomp:upfirdn_raw_cupy_kernel

Conversation

@awthomp

@awthomp awthomp commented Mar 6, 2020

Copy link
Copy Markdown
Member

This PR closes #16

CC: @mnicely

WIP implementation comparing the performance of a raw CuPy CUDA kernel via RawModule versus the legacy Numba CUDA kernel. This currently only addresses the 1D case for upfirdn and adds an additional parameter for apply_filter where:

  • use_numba=True uses the legacy upfirdn CUDA kernel and
  • use_numba=False uses the CuPy RawModule

This is also exposed in resample_poly. As an example:

import cusignal
import cupy as cp
 
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)
 
gf = cusignal.resample_poly(gy, resample_up, resample_down, window=('kaiser', 0.5), use_numba=False)

Matt has a separate test script that has been profiled via NSIGHT; it's getting about a 4x perf increase. When timed Python, however, I'm getting a ~2x speed decrease in resample_poly.

There are also Python formatting fixes in this PR and an update to the relevant Jupyter notebook showing the option to use the Numba/Raw CuPy kernel.

@awthomp
awthomp requested a review from a team March 6, 2020 22:09
@awthomp awthomp changed the title Implementation of Raw CuPy Module for upfirdn [WIP] Implementation of Raw CuPy Module for upfirdn Mar 6, 2020
@gmarkall

gmarkall commented Mar 9, 2020

Copy link
Copy Markdown
Contributor

I think compiling the CuPy kernel for _raw_apply_1d on every call is unnecessary and will be expensive - I've tried caching the kernels in gmarkall@e3b854c - on my test machine (Tesla V100-SXM2-16GB) this makes the call to resample_poly with the raw kernel approximately 3x faster than the Numba one by wall time.

use_numba=True:

     CPU times: user 288 ms, sys: 52 ms, total: 340 ms
     Wall time: 363 ms
    
use_numba=False:
    
     CPU times: user 116 ms, sys: 4 ms, total: 120 ms
     Wall time: 120 ms

@awthomp

awthomp commented Mar 9, 2020

Copy link
Copy Markdown
Member Author

Looks great, @gmarkall! Both @mnicely and I didn't realize CuPy wasn't caching the compiled kernel after the first run. Your experiment looks straight forward, and I'm good with rolling it into the baseline code. Can you commit to this PR, or do you need to create a separate one that merges after this?

@gmarkall

gmarkall commented Mar 9, 2020

Copy link
Copy Markdown
Contributor

I can't commit to the PR branch as I don't have write access to github.com/awthomp/cusignal-dev repo - I'll create a separate PR that merges after this one.

@gmarkall

gmarkall commented Mar 9, 2020

Copy link
Copy Markdown
Contributor

PR #26 created for the merge afterwards.

@gmarkall

gmarkall commented Mar 9, 2020

Copy link
Copy Markdown
Contributor

It's also worth considering that at the moment the notebook example is comparing a raw kernel operating on 32-bit types with a Numba kernel operating on 64-bit types - for example, gy in the notebook is a float64. Passing this to the raw CuPy kernel appears to initiate a copy from float64 to float32, whereas Numba will accept the float64 and do all operations on float64 data, which carries a performance penalty. Modifying the Numba kernel so that it only accepts float32 (to ensure that nothing is accidentally run with a float64 while I'm testing) with an explicit signature:

diff --git a/python/cusignal/_upfirdn.py b/python/cusignal/_upfirdn.py
index 54f887c..aacab2f 100644
--- a/python/cusignal/_upfirdn.py
+++ b/python/cusignal/_upfirdn.py
@@ -12,7 +12,7 @@
 # limitations under the License.
 
 import cupy as cp
-from numba import cuda
+from numba import cuda, float32, int32, void
 import math
 
 
@@ -94,7 +94,7 @@ def _apply(x, h_trans_flip, out, up, down, axis=-1):
             h_idx += 1
 
 
-@cuda.jit(fastmath=True)
+@cuda.jit(void(float32[:], float32[:], float32[:], int32, int32, int32), fastmath=True)
 def _apply_1d(x, h_trans_flip, out, up, down, axis=-1):
 
     X = cuda.grid(1)

and then changing the declaration of gx and gy in the notebook:

gx = cp.linspace(start, stop, num, endpoint=False, dtype=np.float32)
gy = cp.cos(-gx**2/6.0)

results in the following times:

Numba:
    CPU times: user 4 ms, sys: 20 ms, total: 24 ms
    Wall time: 21.5 ms

CuPy:
    CPU times: user 8 ms, sys: 4 ms, total: 12 ms
    Wall time: 10.5 ms

which closes the gap to 2x performance from CuPy - still some more improvement to do on the Numba side :-)

@awthomp

awthomp commented Mar 9, 2020

Copy link
Copy Markdown
Member Author

Since this PR gives the user the option to roll back to the default Numba CUDA kernel (in fact, it's enabled by default), I'm going to recommend merging this PR.

@awthomp awthomp changed the title [WIP] Implementation of Raw CuPy Module for upfirdn [REVIEW] Implementation of Raw CuPy Module for upfirdn Mar 9, 2020
@awthomp awthomp added 3 - Ready for Review Ready for review by team 4 - Needs Reviewer Waiting for reviewer to review or respond labels Mar 9, 2020
@awthomp

awthomp commented Mar 10, 2020 •

Copy link
Copy Markdown
Member Author

@mnicely -- this PR contains the int raw CuPy kernel. Do you want it removed? Actually -- do you mind taking a look at the files changed and see if I adequately captured your work?

@mnicely

mnicely commented Mar 10, 2020 •

Copy link
Copy Markdown
Contributor

@awthomp If it's not too late, I suggest we remove the int kernel from the Raw Module. It is technically dead code, because it will never be reached as all int inputs are promoted to float.

@awthomp
awthomp merged commit aea170e into rapidsai:branch-0.13 Mar 10, 2020
@awthomp
awthomp deleted the upfirdn_raw_cupy_kernel branch March 10, 2020 22:13
@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

3 - Ready for Review Ready for review by team 4 - Needs Reviewer Waiting for reviewer to review or respond

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEA] Analyze performance of writing custom CUDA kernels via CuPy RawKernel versus Numba.CUDA

4 participants