Skip to content

Improve ndimage convolve/correlate - #3179

Merged
mergify[bot] merged 2 commits into
cupy:masterfrom
anaruse:improve_ndimage_convolve
Mar 12, 2020
Merged

mergify[bot] merged 2 commits into
cupy:masterfrom
anaruse:improve_ndimage_convolve

Conversation

@anaruse

@anaruse anaruse commented Mar 10, 2020

Copy link
Copy Markdown
Contributor

This PR improves performance of convolve and correlate in cupyx.scipy.ndimage.filter.py.

I was investing why the performance of convolve and correlate was slower than expected, and it seems one of the causes is that raw array access via CArray is slow.. The issue will be addressed with this PR.

Performance (convolution, fp32)

[sec] scipy convolve cupyx convolve (current) cupyx convolve (this PR)
x.shape = (4000, 4000), w.shape = (3,3) 0.225978 0.002195 0.000670
x.shape = (4000, 4000), w.shape = (5,5) 0.390014 0.005525 0.001229
x.shape = (4000, 4000), w.shape = (7,7) 0.761595 0.011325 0.002043
x.shape = (300, 300, 300), w.shape = (3, 3, 3) 0.702484 0.028360 0.002025
  • Quadro GV100
  • CUDA 10.2

@grlee77

grlee77 commented Mar 10, 2020

Copy link
Copy Markdown
Member

That is a very nice speedup for such a small change!

Not directly related to this PR, but for cases when the kernel is small enough to fit, do you think it would help to copy it to shared memory? I suppose with a RawModule you could also keep the kernel in global/constant memory via get_global, but I don't think there is a way to do the equivalent with an ElementwiseKernel.

@leofang

leofang commented Mar 10, 2020

Copy link
Copy Markdown
Member

This is a very good catch, @anaruse! Do you think the CArray class is too abstract to provide good performance? I've had a hard time understanding it...

@coderforlife

Copy link
Copy Markdown
Contributor

Note that I currently have a similar solution but then also generalizes to all of ndimage.filters. I too did notice the slow CArray lookups and then when using custom handling of indices it was way faster.

@coderforlife

Copy link
Copy Markdown
Contributor

Your current version does always force the input array to be C contiguous and if it isn't copies the entire array. With a little extra work as done in mine (which I am going to submit a PR for in just a bit) it doesn't require non-contiguous arrays to be copied first.

grlee77 added a commit to mritools/cupyimg that referenced this pull request Mar 10, 2020
Comment thread cupyx/scipy/ndimage/filters.py Outdated
out_params = 'Y y'

ops = []
ops.append('X* _x = (X*)&(x[0]);')

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.

Can you change the variable prefix?
We are working on some coding guidelines for CuPy and one of them will be not allowing '_' to prefix local variables.
Thanks!

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.

Got it!
BTW, is it OK to use '_' as suffix (^^).

@emcastillo emcastillo self-assigned this Mar 11, 2020
@grlee77

grlee77 commented Mar 11, 2020

Copy link
Copy Markdown
Member

Your current version does always force the input array to be C contiguous and if it isn't copies the entire array. With a little extra work as done in mine (which I am going to submit a PR for in just a bit) it doesn't require non-contiguous arrays to be copied first.

I agree that this is a limitation, but it could potentially be worth always enforcing contiguity for performance? We would have to benchmark on various contiguous and non-contiguous inputs to be sure. In memory-limited scenarios, avoiding the copy would obviously be preferable, though.

In any case, contiguity was already being enforced prior to this PR, so at least it is not a new requirement:

input = cupy.ascontiguousarray(input)
weights = cupy.ascontiguousarray(weights, cupy.float64)

@coderforlife

Copy link
Copy Markdown
Contributor

@grlee77 I did some very basic timing tests on my machine and having the non-contiguous helping of the input image (but not the weights, I still always make that contiguous since it should be relatively small) did not make a time difference with my code as in the other PR. I didn't actually test with any non-contiguous array, but the lack of speed difference isn't surprising since the current code essentially multiples by the stride right now anyways:

ops.append(' ix_{j} *= sx_{j};'.format(j=j))

It just computes the sx_j variables (the strides) from the shape instead of just using x.strides()[{j}].

Here are the times for some convolutions, first line is not assuming contiguous arrays and second assumes contiguous (but doesn't actually do the check). Sometimes assuming contiguous is slower.

(4000, 4000) ⊗ (3, 3)
1.32 ms ± 1.34 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
1.31 ms ± 6.88 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) # assuming contiguous

(4000, 4000) ⊗ (5, 5)
2.91 ms ± 6.05 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
2.95 ms ± 9.63 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) # assuming contiguous

(4000, 4000) ⊗ (7, 7)
5.15 ms ± 2.04 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
5.25 ms ± 6.88 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) # assuming contiguous

(300, 300, 300) ⊗ (3, 3, 3)
17.1 ms ± 2.87 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
17.1 ms ± 4.89 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) # assuming contiguous

@coderforlife

Copy link
Copy Markdown
Contributor

@leofang The problem with CArray is that if you use a linear index it incurs several divisions/modulos to compute the new index (even if that index will end up being the same exact same index since it is a contiguous array). That means every single lookup or set has a fairly large unexpected overhead.

I understand why it is doing this - it is to protect ElementwiseKernel authors from having to worry about non-contiguous array access. But it would be nice if:

  • The array was contiguous then CArray directly used the linear index (and no division/modulo/multiplication)
  • The ElementwiseKernel author could optionally turn off linear index conversion (even for non-contiguous arrays)

I can't remember if the tuple-index lookup has the same division problem, but I suspect not since I think it should only need the multiplications.

@emcastillo

Copy link
Copy Markdown
Member

The array was contiguous then CArray directly used the linear index (and no division/modulo/multiplication)

This should be fairly easy to do

@emcastillo

Copy link
Copy Markdown
Member

I will allow this PR to go to the master branch while I review #3184. This will allow having some performance benefits right now.

Jenkins, test this please

@pfn-ci-bot

Copy link
Copy Markdown
Collaborator

Successfully created a job for commit f151fc7:

@emcastillo emcastillo added st:test-and-merge (deprecated) Ready to merge after test pass. cat:performance Performance in terms of speed or memory consumption labels Mar 12, 2020
@emcastillo emcastillo added this to the v8.0.0b1 milestone Mar 12, 2020
@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit f151fc7, target branch master) succeeded!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat:performance Performance in terms of speed or memory consumption st:test-and-merge (deprecated) Ready to merge after test pass.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants