Skip to content

ENH: add cupy.gradient - #3963

Merged
asi1024 merged 4 commits into
cupy:masterfrom
grlee77:gradient
Sep 9, 2020
Merged

asi1024 merged 4 commits into
cupy:masterfrom
grlee77:gradient

Conversation

@grlee77

@grlee77 grlee77 commented Sep 8, 2020

Copy link
Copy Markdown
Member

closes #1270

This PR ports the gradient function from cupyimg to 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, axis and edge_order combinations.

In #3233 (comment), @asi1024 suggested possibly using an Elementwise kernel, but at least for the uniform spacing case, I don't think it is likely to be beneficial. At least, I tried timing the current cupy.diff vs. using Elementwise convolution via cupy.correlate(data, [-1, 1]) and this tended to be slower unless a difference of order >=4 was used.

@grlee77

grlee77 commented Sep 8, 2020 •

Copy link
Copy Markdown
Member Author

Benchmark results for a GTX 1080 Ti

Benchmark with default scalar spacing

shape GPU time (ms) CPU time (ms) acceleration
(256,) 0.117 +/- 0.006 0.023 +/- 0.004 0.198
(512, 512) 0.245 +/- 0.018 0.721 +/- 0.018 2.944
(2048, 4096) 1.109 +/- 0.005 29.845 +/- 0.106 26.917
(192, 192, 192) 1.452 +/- 0.005 36.301 +/- 0.272 24.994

Benchmark with non-constant spacing and 2nd order edges

shape GPU time (ms) CPU time (ms) acceleration
(256,) 0.507 +/- 0.021 0.050 +/- 0.003 0.099
(512, 512) 0.996 +/- 0.054 2.735 +/- 0.047 2.747
(2048, 4096) 4.573 +/- 0.024 135.849 +/- 0.500 29.705
(192, 192, 192) 6.350 +/- 0.012 167.089 +/- 0.467 26.313
benchmark script
import 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}")

@kmaehashi kmaehashi added cat:feature New features/APIs prio:medium labels Sep 8, 2020
@asi1024 asi1024 modified the milestone: v9.0.0a1 Sep 8, 2020
@asi1024

asi1024 commented Sep 8, 2020

Copy link
Copy Markdown
Member

Jenkins, test this please.

@chainer-ci

Copy link
Copy Markdown
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
@grlee77

grlee77 commented Sep 8, 2020

Copy link
Copy Markdown
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)

@asi1024

asi1024 commented Sep 9, 2020

Copy link
Copy Markdown
Member

Jenkins, test this please.

@chainer-ci

Copy link
Copy Markdown
Member

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

@asi1024

asi1024 commented Sep 9, 2020

Copy link
Copy Markdown
Member

LGTM.

@asi1024
asi1024 merged commit 4c81aeb into cupy:master Sep 9, 2020
@leofang

leofang commented Sep 9, 2020

Copy link
Copy Markdown
Member

@grlee77 Thanks for taking over and finishing it!

@grlee77
grlee77 deleted the gradient branch September 9, 2020 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

implement cupy.gradient

5 participants