Repository navigation
[REVIEW] Added streams and double/complex64/complex128 functionality - #31
Conversation
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
|
Can one of the admins verify this patch? |
|
@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)
|
List of changes in these commits -
Usage:
Command to profile To reiterate performance on test script above |
That issue referred to using a device function with more than one type from within a kernel. It should be possible to merge |
|
Hi @mnicely, thanks for pointing me to this PR. I will try to take a look soon. The CuPy-based 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 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. |
|
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: where |
|
@gmarkall I noticed 10 additional registers being used when I use int32 instead of int64... |
|
@gmarkall Here are results from Nsight Compute (with 64 threads per block) |
|
I did some basic tests of 1d For relatively small sizes, Times in the table are in seconds for
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")) |
Looking into this a bit more, typing of the function with 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 |
|
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 I presently have CUDA toolkit 9.2, where I see each variant uses 37 registers, except for the |
|
@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. |
|
@gmarkall Thanks for the update! I feel a lot better knowing I'm not crazy :) |
|
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. |
Perhaps to cherry-pick the commit into your local branch then push to your cusignal repo? Assuming you have the |
|
@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. |
|
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. |
|
@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! |
|
Major Features/Enhancements/Changes added in this PR
|
| 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 |
There was a problem hiding this comment.
We need to add documentation for cp_stream (in a similar way we added documentation for use_numba)
There was a problem hiding this comment.
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,), | ||
| ) |
There was a problem hiding this comment.
Let's add an else and raise a NotImplementedError if dtype isn't complex64/128 or float32/64
There was a problem hiding this comment.
Does this wording work?
NotImplementedError: Out type - dtype('int32') not supported
awthomp
left a comment
There was a problem hiding this comment.
- Remove profiling in cupy
cp.prof-- unless I'm misunderstanding the functionality here. - Raise
NotImplementedErrorif numerical dtype is unsupported - Document cp_stream parameter
|
@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
left a comment
There was a problem hiding this comment.
@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
Removed `from cupy import prof`
|
Rerun tests |
|
Updated CHANGELOG.md and converted tabs to spaces. |
|
rerun tests |
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-190usdef _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-190usdef _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 - ~10usUsing doubles (CuPy Raw Kernel)# 30 registers - ~42usI'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$24423.6 7277675 100 72776.7 71648 74112 _cupy_upfirdn_1d_complex_double10.2 3142669 100 31426.7 31232 32128 _numba_upfirdn_1d_double$2429.4 2910471 100 29104.7 28960 30207 _numba_upfirdn_1d_complex64$2437.7 2379027 100 23790.3 23679 24704 _numba_upfirdn_1d_float$2416.7 2077875 100 20778.8 20448 20928 _cupy_upfirdn_1d_double4.5 1385173 100 13851.7 13728 13952 _cupy_upfirdn_1d_complex_float4.0 1244918 100 12449.2 12352 12512 _cupy_upfirdn_1d_floatThere 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
