Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Richardson-Lucy deconvolution benchmark #790

Draft
wants to merge 1 commit into
base: branch-21.12
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions dask_cuda/benchmarks/local_cupy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@
async def _run(client, args):
if args.type == "gpu":
import cupy as xp

if args.operation == "richardson_lucy":
import cupyx.scipy.ndimage as ndimage
else:
import numpy as xp

if args.operation == "richardson_lucy":
import scipy.ndimage as ndimage

# Create a simple random array
rs = da.random.RandomState(RandomState=xp.random.RandomState)

Expand Down Expand Up @@ -133,6 +139,41 @@ async def _run(client, args):
func_args = (x, idx)

func = lambda x, idx: x[idx]
elif args.operation == "richardson_lucy":
rng = start_range(message="make array(s)", color="green")
image = rs.random((args.size,) * 3, chunks=args.chunk_size).persist()
psf = rs.random((args.filter_size,) * 3).persist()
im_deconv = (np.full_like(image, 0.5, shape=image.shape)).persist()
print(image, im_deconv)
psf_mirror = (np.flip(psf)).persist()
await wait(image)
await wait(psf)
await wait(im_deconv)
await wait(psf_mirror)
end_range(rng)

def _convolve(a, f, mode="constant"):
depth = tuple([s // 2 for s in f.shape])
return a.map_overlap(
ndimage.convolve,
depth=depth,
boundary="none",
dtype=a.dtype,
weights=f,
mode=mode,
)

def _richardson_lucy(image, psf, im_deconv, psf_mirror):
conv = _convolve(im_deconv, psf, mode="constant")
relative_blur = image / conv
im_deconv *= _convolve(relative_blur, psf_mirror, mode="constant")
return im_deconv
Comment on lines +166 to +170
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a richardson_lucy implementation in cuCIM, which we could use here

Something worth noting is Richardson-Lucy is an iterative algorithm that converges on a solution. This involves entering and leaving Fourier space repeatedly. So there is a fair bit of computation, which may affect profiling.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out, John! I was trying to reproduce https://github.com/nv-legate/cunumeric/blob/18792f3e988e3240eb10ff6de6d78de7df57d090/examples/richardson_lucy.py#L28-L41 , but I now see the mistake I've made in not iterating over im_deconv but rather overwriting it. I'll also take a closer look at the cuCIM implementation and see what I can make up of both approaches.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ofc! Yeah that makes sense. Feel free to grab that code from cuCIM if it helps.

Should add the convolve call there is using some vendored code, but that preceded CuPy adding convolve in 9.0.0. So it should be possible to use CuPy directly for that call. Everything else is also straight CuPy so that should hopefully make it easier to use.

The other interesting thing about this convolve call is it will try to do convolution in Fourier space or real space depending on which is faster (using some heuristic). If you determine one is faster for your needs, it may be worth bypassing that autodetection logic and just calling with the appropriate implementation.

One last thought since it seems in their benchmark they used a warm-up run, we might want to consider doing the same thing. After all CuPy will create the kernels on the first run. So it only seems fair to do the same thing here.


func_args = (image, psf, im_deconv, psf_mirror)
func = _richardson_lucy

# Variable name 'x' to match other operations
x = image

shape = x.shape
chunksize = x.chunksize
Expand Down Expand Up @@ -322,15 +363,24 @@ def parse_args():
"default": "10000",
"metavar": "n",
"type": int,
"help": "The array size n in n^2 (default 10000). For 'svd' operation "
"the second dimension is given by --second-size.",
"help": "The array size n in n^2 (n^3 for 'richardson_lucy'). For "
"'svd' operation the second dimension is given by --second-size. "
"Default: 10000.",
},
{
"name": ["-2", "--second-size",],
"default": "1000",
"type": int,
"help": "The second dimension size for 'svd' operation (default 1000).",
},
{
"name": ["-f", "--filter-size",],
"default": "4",
"metavar": "n",
"type": int,
"help": "The filter size n in n^3 only applicable for 'richardson_lucy'. "
"Default: 4.",
},
{
"name": ["-t", "--type",],
"choices": ["cpu", "gpu"],
Expand Down