Skip to content

rewrite cupyx.scipy.ndimage.interpolation using ElementwiseKernel - #3166

Merged
asi1024 merged 13 commits into
cupy:masterfrom
grlee77:interpolation_refactor
Apr 27, 2020
Merged

asi1024 merged 13 commits into
cupy:masterfrom
grlee77:interpolation_refactor

Conversation

@grlee77

@grlee77 grlee77 commented Mar 6, 2020

Copy link
Copy Markdown
Member

This PR improves the performance and reduces memory usage for the cupyx.scipy.ndimage.interpolation module. There should be no change in behavior from the prior implementation.

Memory overhead in various functions like affine_transform is greatly reduced by not explicitly generating dense n-dimensional coordinate arrays.

closes #2607
also resolves some issues with performance that were previously reported in: #1168, #2345, #2844

Will post the benchmarks in a follow-up comment, but performance is greatly improved on all cases I tested.

The main downside to this PR is that such template-based code generation is a bit harder to read than the previous implementations, but I tried to add some comments describing what is going on.

Memory overhead in affine_transform is greatly reduced by not explicitly
generating nd coordinate arrays.

The performance of all functions are also improved.
@grlee77

grlee77 commented Mar 6, 2020

Copy link
Copy Markdown
Member Author

The following timings were measured for this PR vs. current master (8.0.0a1) for various operations applied to a 2D float32 image of shape (4096, 4096).

The case labeled affine_transform_1dmat corresponds to a different code path in affine_transform for the case when the matrix is 1d (corresponding to a zoom).

The next-to-last column gives the acceleration factor for the PR vs. current master, showing from between 4x and 26x improvement!

SciPy (1.4.1) CuPy (8.0.0a1) CuPy (proposed) accel. vs. 8.0.0a1 accel. vs. SciPy
rotate (constant, order=0) 0.8071 0.0327 0.0042 7.8077 192.8056
rotate (nearest, order=0) 1.0220 0.0295 0.0042 7.0459 243.6998
rotate (constant, order=1) 1.0053 0.1229 0.0052 23.7408 194.2177
rotate (nearest, order=1) 1.5673 0.1183 0.0069 17.1905 227.7342
shift (constant, order=0) 0.1681 0.0167 0.0012 13.7808 138.9603
shift (nearest, order=0) 0.1661 0.0153 0.0009 17.6277 191.7065
shift (constant, order=1) 0.3448 0.0411 0.0021 19.4609 163.4667
shift (nearest, order=1) 0.3636 0.0396 0.0019 21.1493 194.3028
zoom (constant, order=0) 0.1746 0.0164 0.0012 13.2128 140.5852
zoom (nearest, order=0) 0.1673 0.0152 0.0008 18.3022 201.7883
zoom (constant, order=1) 0.3637 0.0635 0.0029 21.6818 124.1133
zoom (nearest, order=1) 0.3581 0.0623 0.0027 23.0291 132.3654
affine_transform (constant, order=0) 0.3592 0.0174 0.0021 8.1698 168.8159
affine_transform (nearest, order=0) 0.6480 0.0152 0.0023 6.5670 279.7564
affine_transform (constant, order=1) 0.4340 0.0664 0.0025 26.0903 170.6035
affine_transform (nearest, order=1) 0.9803 0.0626 0.0036 17.5132 274.3002
map_coordinates (constant, order=0) 0.4296 0.0106 0.0022 4.7879 194.5049
map_coordinates (nearest, order=0) 0.4323 0.0091 0.0021 4.2682 201.6946
map_coordinates (constant, order=1) 0.6561 0.0577 0.0035 16.6911 189.9143
map_coordinates (nearest, order=1) 0.6641 0.0564 0.0033 16.9896 199.9157
affine_transform_1dmat (constant, order=0) 0.1360 0.0167 0.0014 12.1260 98.6344
affine_transform_1dmat (nearest, order=0) 0.1680 0.0151 0.0012 12.8153 142.2724
affine_transform_1dmat (constant, order=1) 0.2351 0.0487 0.0018 26.6065 128.5304
affine_transform_1dmat (nearest, order=1) 0.4457 0.0471 0.0022 21.8071 206.5435

@leofang

leofang commented Mar 6, 2020

Copy link
Copy Markdown
Member

Thanks a lot, Gregory! This is excellent work! I confirmed that this PR eliminates the excessive memory usage we reported in #2607.

cc: @jqyin @dxm447

@debangshu-mukherjee

Copy link
Copy Markdown

Really great!

@grlee77

grlee77 commented Mar 10, 2020

Copy link
Copy Markdown
Member Author

I suppose the same type of indexing changes made in #3179 should also help here. I can take a look at that when I get a chance.

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

I know very little about the ndimage module, so can't comment much, but I noticed that we almost always need to do memoize() for each device.

Comment thread cupyx/scipy/ndimage/_interp_kernels.py Resolved
Comment thread cupyx/scipy/ndimage/_interp_kernels.py Resolved
Comment thread cupyx/scipy/ndimage/_interp_kernels.py Resolved
Comment thread cupyx/scipy/ndimage/_interp_kernels.py Resolved
Comment thread cupyx/scipy/ndimage/_interp_kernels.py Resolved
@grlee77

grlee77 commented Mar 18, 2020

Copy link
Copy Markdown
Member Author

#3184 managed to remove xshape from the parameters for convolve which would help reduce the number of combinations for memoize if a similar thing is possible here.

I did try making indexing changes as in #3179 and most functions stayed about the same, although map coordinates did show a substantial improvement. This was prior to the merging of #3191, though which may have given a similar benefit. I will take a look again and see.

Update cupyx/scipy/ndimage/_interp_kernels.py
@grlee77
grlee77 force-pushed the interpolation_refactor branch from 607cdf9 to 3dc0824 Compare March 20, 2020 03:02
@emcastillo emcastillo added this to the v8.0.0b2 milestone Mar 23, 2020
@grlee77

grlee77 commented Mar 27, 2020

Copy link
Copy Markdown
Member Author

I was able to remove xshape from the parameterized parameters.

#3191 seems to have made contiguous indexing fast enough that it was not necessarily to make specific indexing changes as done in #3179.

Comment thread cupyx/scipy/ndimage/_interp_kernels.py Outdated
import cupy
import cupy.core.internal

from .filters import _generate_boundary_condition_ops

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.

Relative imports are prohibited.

Comment thread cupyx/scipy/ndimage/_interp_kernels.py Outdated
Comment on lines +56 to +58
"""
W c_{j} = zoom[{j}] * ((W)in_coord[{j}] - shift[{j}]);
""".format(j=j))

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.

This string includes 2 newline characters.

Comment thread cupyx/scipy/ndimage/interpolation.py Outdated
import cupy
import numpy

from ._interp_kernels import (

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.

Importing non-module symbols is prohibited.

offset = matrix[:-1, -1]
matrix = matrix[:-1, :-1]
if matrix.ndim not in [1, 2]:
raise RuntimeError('no proper affine matrix provided')

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.

Could you add a test for this exception?

Comment thread cupyx/scipy/ndimage/interpolation.py Outdated
ret[:] = map_coordinates(input, coordinates, ret.dtype, order, mode, cval,
prefilter).reshape(output_shape)
return ret
matrix = matrix.astype(float, copy=False)

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.

Please use cupy.float64 for the readability.

Suggested change
matrix = matrix.astype(float, copy=False)
matrix = matrix.astype(cupy.float64, copy=False)

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.

BTW, Is this typecast needed? A float64 array looks to be returned without this typecast.

Comment thread cupyx/scipy/ndimage/interpolation.py Resolved
Comment thread cupyx/scipy/ndimage/interpolation.py Resolved
Comment thread cupyx/scipy/ndimage/interpolation.py Resolved
Comment thread cupyx/scipy/ndimage/interpolation.py Resolved
Comment thread cupyx/scipy/ndimage/interpolation.py Resolved
@asi1024

asi1024 commented Apr 22, 2020

Copy link
Copy Markdown
Member

@grlee77 Could you check the travis failure?

@grlee77
grlee77 force-pushed the interpolation_refactor branch from 3ac86d1 to aabf746 Compare April 22, 2020 06:58
@grlee77

grlee77 commented Apr 22, 2020

Copy link
Copy Markdown
Member Author

Okay, Travis is happy now.

@asi1024 asi1024 modified the milestones: v8.0.0b2, v8.0.0b3 Apr 23, 2020
@asi1024

asi1024 commented Apr 27, 2020

Copy link
Copy Markdown
Member

Jenkins, test this please.

@pfn-ci-bot

Copy link
Copy Markdown
Collaborator

Successfully created a job for commit aabf746:

@asi1024 asi1024 added the cat:performance Performance in terms of speed or memory consumption label Apr 27, 2020
@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit aabf746, target branch master) failed with status FAILURE.

@asi1024

asi1024 commented Apr 27, 2020

Copy link
Copy Markdown
Member

@grlee77 Could you check Jenkins failure?

@grlee77

grlee77 commented Apr 27, 2020

Copy link
Copy Markdown
Member Author

@grlee77 Could you check Jenkins failure?

Please try again now. (It looks like SciPy prior to 1.3 raised IndexError instead of ValueError on out of range axes in rotate).

@asi1024

asi1024 commented Apr 27, 2020

Copy link
Copy Markdown
Member

Jenkins, test this please.

@pfn-ci-bot

Copy link
Copy Markdown
Collaborator

Successfully created a job for commit 048d0c0:

@chainer-ci

Copy link
Copy Markdown
Member

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

@asi1024

asi1024 commented Apr 27, 2020

Copy link
Copy Markdown
Member

LGTM. Thanks for your PR!

@asi1024
asi1024 merged commit 768f99a into cupy:master Apr 27, 2020
@grlee77
grlee77 deleted the interpolation_refactor branch September 9, 2020 15:12
grlee77 added a commit to grlee77/cupy that referenced this pull request Oct 13, 2020
grlee77 added a commit to grlee77/cupy that referenced this pull request Oct 13, 2020
This was inadvertently removed in cupygh-3166

fix mode opencv for rotate and shift as well
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cupyx.scipy.ndimage.rotate() saturates P100's 16G memory for 1G input data

7 participants