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

[REVIEW] Added streams and double/complex64/complex128 functionality - #31

Merged
awthomp merged 12 commits into
rapidsai:branch-0.13from
mnicely:mnicely-dev
Mar 18, 2020
Merged

awthomp merged 12 commits into
rapidsai:branch-0.13from
mnicely:mnicely-dev

Conversation

@mnicely

@mnicely mnicely commented Mar 11, 2020 •

Copy link
Copy Markdown
Contributor

I would like to determine why my Numba kernels are running so much slower than my CuPy kernels.

Using floats (Numba)
# @cuda.jit(fastmath=True) # 38 registers - 157-190us
# @cuda.jit(void(float32[:], float32[:], int32, int32, int32, int32, int32, float32[:],), fastmath=True) # 48 registers - 160-190us
@cuda.jit(void(float32[:], float32[:], int64, int64, int64, int64, int64, float32[:],), fastmath=True) # 38 registers - 157-190us
def _numba_upfirdn_1d_float(x, h_trans_flip, up, down, x_shape_a, h_per_phase, padded_len, out):

Using doubles (Numba)
# @cuda.jit(fastmath=True) # 38 registers - 157-190us
# @cuda.jit(void(float64[:], float64[:], int32, int32, int32, int32, int32, float64[:],), fastmath=True) # 48 registers - 160-190us
@cuda.jit(void(float64[:], float64[:], int64, int64, int64, int64, int64, float64[:],), fastmath=True) # 39 registers - 157-190us
def _numba_upfirdn_1d_double(x, h_trans_flip, up, down, x_shape_a, h_per_phase, padded_len, out):

Using floats (CuPy Raw Kernel)
# 21 registers - ~10us

Using doubles (CuPy Raw Kernel)
# 30 registers - ~42us

I've moved Systems profiling to a second GPU (no X-server) and I'm getting comparable results.

Time(%) Total Time Instances Average Minimum Maximum Name
------- ---------- ------- --------- --------- ------- ------------------
26.0 8041093 100 80410.9 78527 81535 _numba_upfirdn_1d_complex128$244
23.6 7277675 100 72776.7 71648 74112 _cupy_upfirdn_1d_complex_double
10.2 3142669 100 31426.7 31232 32128 _numba_upfirdn_1d_double$242
9.4 2910471 100 29104.7 28960 30207 _numba_upfirdn_1d_complex64$243
7.7 2379027 100 23790.3 23679 24704 _numba_upfirdn_1d_float$241
6.7 2077875 100 20778.8 20448 20928 _cupy_upfirdn_1d_double
4.5 1385173 100 13851.7 13728 13952 _cupy_upfirdn_1d_complex_float
4.0 1244918 100 12449.2 12352 12512 _cupy_upfirdn_1d_float

There is a significant difference between registers usage with CuPy but not Numba.

These times are from Nsight Systems, using the attached python script.
test.py.txt

Also, is it possible to overload Numba kernels? Supposedly it is with numba/numba#431. But I'm unable to make it work.~~

I've added qdrep and sqlite files. It looks like CuPy compiles are getting cached in the same manner as Numba.
cusignal.zip

As I continue to optimize, I'm noticing that even though the CuPy kernels may be 2x faster than Numba, the required launch time of the CuPy kernel take 2x longer than Numba. Making Numba the favorable option... Screenshot below. It's quite possible I'm doing something sub-optimal
numba_vs_cupy

2. Creating numba and cupy initialization functions.
3. Moving h_per_phase and padded_len into initialization function to save registers and compute.
4. Move output variable to last parameter
5. Change grid-stride size to SM * 20, from SM * 10. -> 25% improvement on CuPy raw kernel
6. Use current device versus all device 0
7. Move _init_cupy_upfirdn_1d_modules inside branch condition. No reason to compile 1d kernel for 2d execution and vice versa
8. We will leave promotions unchanged in the __init__. If users what to utilize float32 path. They will need to specify that when they create h and x.
9. Updated naming convention
@mnicely
mnicely requested a review from a team March 11, 2020 19:35
@GPUtester

Copy link
Copy Markdown
Contributor

Can one of the admins verify this patch?

@awthomp
awthomp requested a review from gmarkall March 11, 2020 19:43
@awthomp

awthomp commented Mar 12, 2020

Copy link
Copy Markdown
Member

@mnicely -- could you please write-up your latest committed changes and expected usage/initial perf? This will make the approval process easier for the reviewers and also serve as documentation to the greater community.

…s readablility

2. Added Numba/CuPy complex float/double kernels.
3. Modified number of threadsperblock for improved speed (1d kernels are memory bound)
@mnicely

mnicely commented Mar 12, 2020 •

Copy link
Copy Markdown
Contributor Author

List of changes in these commits -

  1. Removing axis default value because it will never be executed.
  2. Creating numba and cupy initialization functions.
  3. Moving h_per_phase and padded_len into initialization function to save registers and compute.
  4. Move output variable to last parameter - more of a style thing
  5. Change grid-stride size to SM * 20, from SM * 10. -> 25% improvement on CuPy raw kernel
  6. Use current device versus explicit device 0 - device_id = cp.cuda.Device()
  7. Move _init_cupy_upfirdn_1d_modules inside branch condition. No reason to compile 1d kernel for 2d execution and vice versa
  8. We will leave promotions unchanged in the init. If users what to utilize float32 path. They will need to specify that when they create h and x. - x = np.array(x, dtype=np.float32)
  9. Updated naming convention - move verbose and easier to follow, IMHO
  10. Added stream functionality - If users don't specify a stream, default stream will be used. There is no synchronization inside cuSignal. It will be the user responsibility. Must pass CuPy stream and if Numba is used, CuPy stream is copied to Numba stream. (Pre-numba 0.49/0.50)
  11. Added more information to the cupy kernel naming for better Systems readability
  12. Added Numba/CuPy complex float/double kernels.
  13. Modified number of threadsperblock (64) for improved speed (1d kernels are memory bound)

Usage:

  1. No stream & use_numba=True
    cusignal.upfirdn(c_h, c_x, 2)
    or
    cusignal.upfirdn(c_h, c_x, 2, cp_stream=cp.cuda.stream.Stream(null=True), use_numba=True)

  2. Use stream (w/ sync) & use_numba=True
    cp_stream = cp.cuda.stream.Stream()
    cusignal.upfirdn(c_h, c_x, 2, cp_stream=cp_stream)
    cp_stream.synchronize()

  3. Use stream (w/o sync) & use CuPy kernels
    cp_stream = cp.cuda.stream.Stream()
    cusignal.upfirdn(c_h, c_x, 2, cp_stream=cp_stream, use_numba=False)

Command to profile
nsys profile --sample=none --trace=cuda,nvtx --stats=true python3 <script>

To reiterate performance on test script above
Time(%) Total Time Instances Average Minimum Maximum Name
------- ---------- ------- --------- --------- ------- ------------------
26.0 8041093 100 80410.9 78527 81535 _numba_upfirdn_1d_complex128$244
23.6 7277675 100 72776.7 71648 74112 _cupy_upfirdn_1d_complex_double
10.2 3142669 100 31426.7 31232 32128 _numba_upfirdn_1d_double$242
9.4 2910471 100 29104.7 28960 30207 _numba_upfirdn_1d_complex64$243
7.7 2379027 100 23790.3 23679 24704 _numba_upfirdn_1d_float$241
6.7 2077875 100 20778.8 20448 20928 _cupy_upfirdn_1d_double
4.5 1385173 100 13851.7 13728 13952 _cupy_upfirdn_1d_complex_float
4.0 1244918 100 12449.2 12352 12512 _cupy_upfirdn_1d_float

@gmarkall

Copy link
Copy Markdown
Contributor

Also, is it possible to overload Numba kernels? Supposedly it is with numba/numba#431

That issue referred to using a device function with more than one type from within a kernel.

It should be possible to merge _numba_upfirdn_1d_double and _numba_upfirdn_1d_float into a single implementation here - just looking into this now.

@grlee77

grlee77 commented Mar 12, 2020

Copy link
Copy Markdown

Hi @mnicely, thanks for pointing me to this PR. I will try to take a look soon.

The CuPy-based upfirdn implementation in fast_upfirdn is a bit complicated because I had added some features I needed for an academic project that were not in SciPy. I ended up porting the new boundary modes to SciPy in scipy/scipy#10552, but a couple other things like variable filter origin have not been ported to SciPy but are still present in the fast_upfirdn code. The CPU implementation there is basically the same as SciPy's aside from those new features.

There is also a separate, faster code path for the case where there is no down or up sampling. I had done that to use for convolution prior to the introduction of cupyx.scipy.ndimage.convolve in CuPy. At the moment, I think the convolution in cupyx.scipy.ndimage.convolve is probably faster for basic convolutions, but the boundary modes supported are a bit different.

To handle nd data, the implementation always transposes the axis to be filtered to be the contiguous axis and then applies a batch 1D transform.

My personal use case is primarily for volumetric medical imaging data, possibly collected at multiple timepoints or contrasts (i.e. 3D + time data). I have not focused on optimizing for 1D signals, but would be interested to see how it compares.

@gmarkall

Copy link
Copy Markdown
Contributor

It turns out that multiple signatures cannot be specified for a CUDA JIT kernel: numba/numba#3226.

I tried merging all the Numba kernels into one that infers types. The typing of arguments when there is no signature specified is:

x T[:]
h_trans_flip: T[:]
up: int64
down: int64
x_shape_a: int64
h_per_phase: int64
padded_len: int64
out: T[:]

where T is either float32, float64, complex64, or complex128 depending on the type of the argument. IIUC, we're seeing some extra register usage with Numba in this case because of the typing of integer variables as int64 when they could have been int32.

@mnicely

mnicely commented Mar 12, 2020 •

Copy link
Copy Markdown
Contributor Author

@gmarkall I noticed 10 additional registers being used when I use int32 instead of int64...
I'll rerun my test tomorrow and post register pressure.

@mnicely

mnicely commented Mar 12, 2020 •

Copy link
Copy Markdown
Contributor Author

@gmarkall Here are results from Nsight Compute (with 64 threads per block)
nv-nsight-cu-cli --metrics launch__registers_per_thread -k "_numba" python3 test.py

Using int64
  cudapy::cusignal::_upfirdn::_numba_upfirdn_1d_double$242(Array<double,int=1,A,mutable,aligned>,Array<double,int=1,A,mutable,aligned>,__int64,__int64,__int64,__int64,__int64,Array<double,int=1,A,mutable,aligned>), 2020-Mar-12 18:42:18, Context 2, Stream 15
    Section: Command line profiler metrics
    ---------------------------------------------------------------------- --------------- ------------------------------
    launch__registers_per_thread                                           register/thread                             39
    ---------------------------------------------------------------------- --------------- ------------------------------

  cudapy::cusignal::_upfirdn::_numba_upfirdn_1d_float$241(Array<float,int=1,A,mutable,aligned>,Array<float,int=1,A,mutable,aligned>,__int64,__int64,__int64,__int64,__int64,Array<float,int=1,A,mutable,aligned>), 2020-Mar-12 18:42:18, Context 2, Stream 15
    Section: Command line profiler metrics
    ---------------------------------------------------------------------- --------------- ------------------------------
    launch__registers_per_thread                                           register/thread                             38
    ---------------------------------------------------------------------- --------------- ------------------------------

  cudapy::cusignal::_upfirdn::_numba_upfirdn_1d_complex64$243(Array<complex64,int=1,C,mutable,aligned>,Array<complex64,int=1,C,mutable,aligned>,__int64,__int64,__int64,__int64,__int64,Array<complex64,int=1,C,mutable,aligned>), 2020-Mar-12 18:42:18, Context 2, Stream 15
    Section: Command line profiler metrics
    ---------------------------------------------------------------------- --------------- ------------------------------
    launch__registers_per_thread                                           register/thread                             38
    ---------------------------------------------------------------------- --------------- ------------------------------

  cudapy::cusignal::_upfirdn::_numba_upfirdn_1d_complex128$244(Array<complex128,int=1,C,mutable,aligned>,Array<complex128,int=1,C,mutable,aligned>,__int64,__int64,__int64,__int64,__int64,Array<complex128,int=1,C,mutable,aligned>), 2020-Mar-12 18:42:19, Context 2, Stream 15
    Section: Command line profiler metrics
    ---------------------------------------------------------------------- --------------- ------------------------------
    launch__registers_per_thread                                           register/thread                             38
    ---------------------------------------------------------------------- --------------- ------------------------------

Using int32

  cudapy::cusignal::_upfirdn::_numba_upfirdn_1d_double$242(Array<double,int=1,A,mutable,aligned>,Array<double,int=1,A,mutable,aligned>,int,int,int,int,int,Array<double,int=1,A,mutable,aligned>), 2020-Mar-12 18:45:01, Context 2, Stream 15
    Section: Command line profiler metrics
    ---------------------------------------------------------------------- --------------- ------------------------------
    launch__registers_per_thread                                           register/thread                             48
    ---------------------------------------------------------------------- --------------- ------------------------------

  cudapy::cusignal::_upfirdn::_numba_upfirdn_1d_float$241(Array<float,int=1,A,mutable,aligned>,Array<float,int=1,A,mutable,aligned>,int,int,int,int,int,Array<float,int=1,A,mutable,aligned>), 2020-Mar-12 18:45:01, Context 2, Stream 15
    Section: Command line profiler metrics
    ---------------------------------------------------------------------- --------------- ------------------------------
    launch__registers_per_thread                                           register/thread                             48
    ---------------------------------------------------------------------- --------------- ------------------------------

  cudapy::cusignal::_upfirdn::_numba_upfirdn_1d_complex64$243(Array<complex64,int=1,A,mutable,aligned>,Array<complex64,int=1,A,mutable,aligned>,int,int,int,int,int,Array<complex64,int=1,A,mutable,aligned>), 2020-Mar-12 18:45:01, Context 2, Stream 15
    Section: Command line profiler metrics
    ---------------------------------------------------------------------- --------------- ------------------------------
    launch__registers_per_thread                                           register/thread                             48
    ---------------------------------------------------------------------- --------------- ------------------------------

  cudapy::cusignal::_upfirdn::_numba_upfirdn_1d_complex128$244(Array<complex128,int=1,A,mutable,aligned>,Array<complex128,int=1,A,mutable,aligned>,int,int,int,int,int,Array<complex128,int=1,A,mutable,aligned>), 2020-Mar-12 18:45:01, Context 2, Stream 15
    Section: Command line profiler metrics
    ---------------------------------------------------------------------- --------------- ------------------------------
    launch__registers_per_thread                                           register/thread                             50
    ---------------------------------------------------------------------- --------------- ------------------------------

@grlee77

grlee77 commented Mar 12, 2020

Copy link
Copy Markdown

I did some basic tests of 1d upfirdn across the two variants here as well as cupyimg.

For relatively small sizes, cupyimg always wins, apparently due to lower cpu-side overhead. At larger sizes, the cusignal (cupy) variant was always fastest. The numba-based cusignal implementation has more of a performance penalty when using complex dtypes than the cupy-based ones. In some large data cases, GPU-based implementations are > 100 times faster than base SciPy (for a GTX 1080 Ti GPU and Intel Skylake X CPU).

Times in the table are in seconds for upfirdn with down=1, up=2 and the size/dtype indicated.

cusignal (numba) cusignal (cupy) cupyimg scipy
x.size=40000, h.size=8, dtype=float32 0.00041 0.00069 0.00008 0.00101
x.size=40000, h.size=8, dtype=float64 0.00042 0.00069 0.00008 0.00100
x.size=40000, h.size=8, dtype=complex64 0.00070 0.00070 0.00008 0.00135
x.size=40000, h.size=8, dtype=complex128 0.00071 0.00071 0.00009 0.00137
x.size=400000, h.size=8, dtype=float32 0.00049 0.00071 0.00019 0.00980
x.size=400000, h.size=8, dtype=float64 0.00056 0.00072 0.00020 0.01125
x.size=400000, h.size=8, dtype=complex64 0.00098 0.00071 0.00020 0.01258
x.size=400000, h.size=8, dtype=complex128 0.00104 0.00080 0.00029 0.01545
x.size=4000000, h.size=8, dtype=float32 0.00117 0.00092 0.00134 0.10478
x.size=4000000, h.size=8, dtype=float64 0.00188 0.00099 0.00150 0.12774
x.size=4000000, h.size=8, dtype=complex64 0.00358 0.00099 0.00152 0.12493
x.size=4000000, h.size=8, dtype=complex128 0.00423 0.00174 0.00243 0.19120
x.size=40000000, h.size=8, dtype=float32 0.00845 0.00345 0.01295 1.12230
x.size=40000000, h.size=8, dtype=float64 0.01624 0.00501 0.01451 1.26718
x.size=40000000, h.size=8, dtype=complex64 0.03058 0.00505 0.01466 1.53360
x.size=40000000, h.size=8, dtype=complex128 0.03854 0.01376 0.02375 1.88744

The script to produce the above table is:

import cupy as cp
import numpy as np
import pandas as pd
import scipy.signal

import cusignal

import cupyimg.scipy.signal
from cupyimg.time import repeat

df = pd.DataFrame(
    columns=['cusignal (numba)', 'cusignal (cupy)', 'cupyimg', 'scipy']
)

name = 'upfirdn'
h_size = 8
down = 1
up = 2
for x_size in [int(4e4), int(4e5), int(4e6), int(4e7)]:
    for dtype in [np.float32, np.float64, np.complex64, np.complex128]:
        x_dtype = h_dtype = dtype
        h = np.random.randn(h_size).astype(h_dtype)
        n = int(x_size)
        x = np.random.randn(n).astype(x_dtype)

        hd = cp.asarray(h)
        xd = cp.asarray(x)

        index = f"x.size={x.size}, h.size={h.size}, dtype={np.dtype(dtype).name}"

        perf = repeat(
            cusignal.upfirdn, (hd, xd, up, down), kwargs=dict(use_numba=True), 
            n_warmup=10, n=100
        )
        t_numba = perf.gpu_times.mean()
        df.at[index, 'cusignal (numba)'] = t_numba
        perf = repeat(
            cusignal.upfirdn, (hd, xd, 2), kwargs=dict(use_numba=False),
            n_warmup=10, n=100
        )
        t_cupy = perf.gpu_times.mean()
        df.at[index, 'cusignal (cupy)'] = t_cupy

        perf = repeat(
            cupyimg.scipy.signal.upfirdn, (hd, xd, up, down), n_warmup=10,
            n=100
        )
        t_cupyimg = perf.gpu_times.mean()
        df.at[index, 'cupyimg'] = t_cupyimg

        perf = repeat(
            scipy.signal.upfirdn, (h, x, up, down), n_warmup=0, n=2
        )
        t_scipy = perf.gpu_times.mean()
        df.at[index, 'scipy'] = t_scipy
print(df.to_markdown(floatfmt=".6f", numalign="right"))

@gmarkall

Copy link
Copy Markdown
Contributor

@gmarkall I noticed 10 additional registers being used when I use int32 instead of int64...
I'll rerun my test tomorrow and post register pressure.

Looking into this a bit more, typing of the function with int32 arguments is quickly resulting in widths being extended up to 64 bits because Numba has a strong preference for using the widest integer type it can (as outlined in NBEP 1: https://gmarkall.github.io/numba-nbep7-docs/proposals/integer-typing.html). The coexistence of some values as 32-bit (e.g. arguments) and others as 64-bit is probably preventing some registers being reused, and therefore adding to the total number of registers.

NBEP 1 did consider the performance impact of going from semantics where typing always tried to start with the smallest width result and widening as necessary, to starting with the widest type possible and being consistent. Unfortunately it looks like the reasoning was mostly based around what happens on CPU architectures, where the impact of wider types is much less than in CUDA.

For now I think the route to go for the Numba kernels is to use int64 - even with lots of casting in the kernel, I couldn't persuade the type inference to use int32 exclusively.

@gmarkall

Copy link
Copy Markdown
Contributor

In gmarkall@c99e5a3 I've replaced the repetition of the kernel code 4 times with a single definition that is compiled 4 times for different signatures, with some "manual" dispatch based on the dtype of out - it's the best workaround I can think of for overloading a CUDA kernel at the moment - what do you think of it?

I presently have CUDA toolkit 9.2, where I see each variant uses 37 registers, except for the complex128 one, which uses 40. All tests appear to pass with this commit.

@mnicely

mnicely commented Mar 13, 2020

Copy link
Copy Markdown
Contributor Author

@grlee77 Thanks for the performance analysis! I have a few more improvements that I will try once this PR get approved.

Also, I'll look into alternative techniques to improve performance when the data set is small.

@mnicely

mnicely commented Mar 13, 2020

Copy link
Copy Markdown
Contributor Author

@gmarkall Thanks for the update! I feel a lot better knowing I'm not crazy :)
I really like you're implementation and will use it from here out for conciseness.
What is the best way to commit those changes to this PR?

@leofang

leofang commented Mar 13, 2020 •

Copy link
Copy Markdown
Member

Guys, I saw @mnicely brought up the issue cupy/cupy#3193 and was directed from there. Not sure if this is raised earlier in the discussion (or if it's relevant at all 😅), but it is a known issue that a Numba kernels is suboptimal compared to its CUDA C counterpart (and then compiled by CuPy's RawKernel/RawModule): numba/numba#4647. There're (non-Anaconda) people working on it AFAIK.

@gmarkall

Copy link
Copy Markdown
Contributor

@gmarkall Thanks for the update! I feel a lot better knowing I'm not crazy :)
I really like you're implementation and will use it from here out for conciseness.
What is the best way to commit those changes to this PR?

Perhaps to cherry-pick the commit into your local branch then push to your cusignal repo? Assuming you have the mnicely-dev branch checked out and your cusignal repo on Github is origin, something like:

git remote add gmarkall [email protected]:gmarkall/cusignal.git
git fetch gmarkall
git cherry-pick c99e5a310e4aaba069cb688ce722e05b6cc31331
git push origin mnicely-dev

@mnicely mnicely changed the title [WIP] Understanding difference in CuPy and Numba [REVIEW] Understanding difference in CuPy and Numba Mar 16, 2020
@mnicely

mnicely commented Mar 16, 2020

Copy link
Copy Markdown
Contributor Author

@gmarkall Added your changes from gmarkall@c99e5a3! Everything looks/works as expected. I'm seeing one or two more registers than you, but that is expected because I'm using 10.2.

@mnicely

mnicely commented Mar 16, 2020 •

Copy link
Copy Markdown
Contributor Author

Hi @grlee77, I reran your test scripts on the latest update on my i7-8700K @ 4.2GHz and Titan RTX. You should notice that cusignal (cupy) is performing better at smaller datasets and Numba is doing much better for complex numbers.

|                                             |   cusignal (numba) |   cusignal (cupy) |   cupyimg |    scipy |
|:--------------------------------------------|-------------------:|------------------:|----------:|---------:|
| x.size=40000, h.size=8, dtype=float32       |           0.000393 |          0.000155 |  0.000077 | 0.000876 |
| x.size=40000, h.size=8, dtype=float64       |           0.000386 |          0.000155 |  0.000077 | 0.000885 |
| x.size=40000, h.size=8, dtype=complex64     |           0.000390 |          0.000157 |  0.000080 | 0.001119 |
| x.size=40000, h.size=8, dtype=complex128    |           0.000395 |          0.000168 |  0.000168 | 0.002342 |
| x.size=400000, h.size=8, dtype=float32      |           0.000525 |          0.000171 |  0.000112 | 0.008507 |
| x.size=400000, h.size=8, dtype=float64      |           0.000442 |          0.000169 |  0.000126 | 0.008574 |
| x.size=400000, h.size=8, dtype=complex64    |           0.000401 |          0.000165 |  0.000121 | 0.010926 |
| x.size=400000, h.size=8, dtype=complex128   |           0.000482 |          0.000263 |  0.000309 | 0.013798 |
| x.size=4000000, h.size=8, dtype=float32     |           0.000614 |          0.000308 |  0.000494 | 0.090381 |
| x.size=4000000, h.size=8, dtype=float64     |           0.000650 |          0.000422 |  0.000580 | 0.112420 |
| x.size=4000000, h.size=8, dtype=complex64   |           0.000656 |          0.000422 |  0.000529 | 0.117459 |
| x.size=4000000, h.size=8, dtype=complex128  |           0.001177 |          0.001127 |  0.001890 | 0.167315 |
| x.size=40000000, h.size=8, dtype=float32    |           0.002679 |          0.001796 |  0.003551 | 0.991915 |
| x.size=40000000, h.size=8, dtype=float64    |           0.004131 |          0.003403 |  0.005130 | 1.141730 |
| x.size=40000000, h.size=8, dtype=complex64  |           0.004249 |          0.003388 |  0.004984 | 1.378713 |
| x.size=40000000, h.size=8, dtype=complex128 |           0.010472 |          0.010226 |  0.018526 | 1.712302 |

@awthomp

awthomp commented Mar 16, 2020

Copy link
Copy Markdown
Member

@mnicely -- when you're ready for a review (i.e. when you stop committing!) -- could you change the title of your PR to reflect the work encapsulated here. Further, could you provide a comment with the list of changes (e.g. introduce complex support and float32 support). Thanks!

@mnicely mnicely changed the title [REVIEW] Understanding difference in CuPy and Numba [REVIEW] Added streams and double/complex64/complex128 functionality Mar 17, 2020
@mnicely

mnicely commented Mar 17, 2020

Copy link
Copy Markdown
Contributor Author

Major Features/Enhancements/Changes added in this PR

  1. Removed _raw_apply_1d_int. It is dead code because of type promotion in _UpFIRDn.__init__
  2. Created specialized Numba functions for float/double/complex64/complex128 to minimize register pressure. Specialization greatly reduces redundant code.
  3. Added stream functionality. Users can now pass a CuPy stream into function and use CuPy stream sync afterwards. If no stream is passed, default stream will be used. The CuPy stream is passed to a Numba stream, for Numba kernels using stream_cupy_to_numba. This function will should be replaced when future versions of Numba allows for passing CuPy stream straight to Numba kernels.
  4. Refactored code to be more modular, with separate CuPy and Numba initializations.
  5. Added CuPy kernels for double/complex64/complex128. Also added a dictionary to cache compiled kernels to speed up repetitive calls to the same kernels.

Comment thread python/cusignal/_upfirdn.py Resolved
use_numba : bool, optional
Option to use Numba CUDA kernel or raw CuPy kernel. Raw CuPy
can yield performance gains over Numba. Default is True.
Returns

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.

We need to add documentation for cp_stream (in a similar way we added documentation for use_numba)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How's this?

cp_stream : CuPy stream, optional
		Option allows upfirdn to run in a non-default stream. The use
		of multiple non-default streams allow multiple kernels to 
		run concurrently. Default is cp.cuda.stream.Stream(null=True)
		or default stream.

h_per_phase,
padded_len,
out,),
)

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.

Let's add an else and raise a NotImplementedError if dtype isn't complex64/128 or float32/64

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this wording work?

NotImplementedError: Out type - dtype('int32') not supported

@awthomp awthomp 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.

  • Remove profiling in cupy cp.prof -- unless I'm misunderstanding the functionality here.
  • Raise NotImplementedError if numerical dtype is unsupported
  • Document cp_stream parameter

@BradReesWork

Copy link
Copy Markdown
Member

@mnicely your PR looks good, but I have one questions. You removed x.shape[axis] from the subfunctions and instead make that a requirement on the top level function - which still passes the x.shape[axis] results. Are you expecting users to enter a different shape value?

@mnicely mnicely left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mnicely your PR looks good, but I have one questions. You removed x.shape[axis] from the subfunctions and instead make that a requirement on the top level function - which still passes the x.shape[axis] results. Are you expecting users to enter a different shape value?

The idea was to minimize register usage and compute cycles in the Numba kernels themselves. Now

x_shape_a = x.shape[axis]
h_per_phase = len(h_trans_flip) // up
padded_len = x.shape[axis] + h_per_phase - 1

are computed on the CPU and passed as arguments to constant memory.

Another reason, is that I need to do that for the CuPy kernels anyways because shape and len don't exist in C.

Honestly, having them in _cupy_init and _numba_init is redundant. In a future PR, it might be worthwhile to calculate them in apply_filter, append them to a list and pass them along.

1. Remove profiling in cupy cp.prof -- unless I'm misunderstanding the functionality here.
2. Raise NotImplementedError if numerical dtype is unsupported
3. Document cp_stream parameter
@mnicely
mnicely requested a review from awthomp March 18, 2020 18:31
@BradReesWork

Copy link
Copy Markdown
Member

Rerun tests

@mnicely

mnicely commented Mar 18, 2020

Copy link
Copy Markdown
Contributor Author

Updated CHANGELOG.md and converted tabs to spaces.

@awthomp

awthomp commented Mar 18, 2020

Copy link
Copy Markdown
Member

rerun tests

@awthomp
awthomp merged commit f720118 into rapidsai:branch-0.13 Mar 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants