Repository navigation
ENH: add cupy.gradient - #3963
Merged
Merged
Conversation
Member
Author
|
Benchmark results for a GTX 1080 Ti Benchmark with default scalar spacing
Benchmark with non-constant spacing and 2nd order edges
benchmark scriptimport numpy
import cupy
from cupyx.time import repeat
print("## Benchmark with default scalar spacing")
print("shape | GPU time (ms) | CPU time (ms) | acceleration")
print("------|---------------|---------------|-------------")
for shape in [(256,), (512, 512), (2048, 4096), (192, 192, 192)]:
xg = cupy.testing.shaped_random(shape, dtype=numpy.float32)
perf_gpu = repeat(cupy.gradient, (xg,), n_warmup=20, n_repeat=100)
gpu_times = perf_gpu.gpu_times * 1000
x = cupy.asnumpy(xg)
perf_cpu = repeat(numpy.gradient, (x,), n_warmup=1, n_repeat=20)
cpu_times = perf_cpu.gpu_times * 1000
accel = cpu_times.mean() / gpu_times.mean()
print(f"{shape} | {gpu_times.mean():0.3f} +/- {gpu_times.std():0.3f} | {cpu_times.mean():0.3f} +/- {cpu_times.std():0.3f} | {accel:0.3f}")
print("\n\n")
print("## Benchmark with non-constant spacing and 2nd order edges")
print("shape | GPU time (ms) | CPU time (ms) | acceleration")
print("------|---------------|---------------|-------------")
for shape in [(256,), (512, 512), (2048, 4096), (192, 192, 192)]:
xg = cupy.testing.shaped_random(shape, dtype=numpy.float32)
spacing = tuple(cupy.arange(s, dtype=float) for s in xg.shape)
for d in range(xg.ndim):
spacing[d][5:] *= 2.0
perf_gpu = repeat(cupy.gradient, (xg,) + spacing, {'edge_order': 2}, n_warmup=20, n_repeat=100)
gpu_times = perf_gpu.gpu_times * 1000
x = cupy.asnumpy(xg)
spacing = tuple(numpy.arange(s, dtype=float) for s in xg.shape)
for d in range(xg.ndim):
spacing[d][5:] *= 2.0
perf_cpu = repeat(numpy.gradient, (x,) + spacing, {'edge_order': 2}, n_warmup=1, n_repeat=10)
cpu_times = perf_cpu.gpu_times * 1000
accel = cpu_times.mean() / gpu_times.mean()
print(f"{shape} | {gpu_times.mean():0.3f} +/- {gpu_times.std():0.3f} | {cpu_times.mean():0.3f} +/- {cpu_times.std():0.3f} | {accel:0.3f}") |
Member
|
Jenkins, test this please. |
Member
|
Jenkins CI test (for commit 92307fe, target branch master) failed with status FAILURE. |
This is done because a more recent NumPy is required to validate the uint results
Member
Author
|
I have updated to restrict the integer dtype test cases to run only on NumPy>=1.18.1 due to numpy/numpy#15207. Hopefully this resolves the test failures (I do not get any failures locally, but I have been testing against NumPy 1.19.1) |
Member
|
Jenkins, test this please. |
Member
|
Jenkins CI test (for commit 3859f07, target branch master) succeeded! |
Member
|
LGTM. |
Member
|
@grlee77 Thanks for taking over and finishing it! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #1270
This PR ports the gradient function from
cupyimgto here. It is based closely on the NumPy implementation.This had previously been started, but abandoned in #3233. The test cases added here are more comprehensive and run for all dtypes for various spacing,
axisandedge_ordercombinations.In #3233 (comment), @asi1024 suggested possibly using an
Elementwisekernel, but at least for the uniform spacing case, I don't think it is likely to be beneficial. At least, I tried timing the currentcupy.diffvs. usingElementwiseconvolution viacupy.correlate(data, [-1, 1])and this tended to be slower unless a difference of order >=4 was used.