You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here's what convolution would look like if someone rolled their own today using CuPy 7.0.0 and Dask 2.9.1. There are a few rough edges, but we could improve on this over time both in dask-image and upstream.
Also we may be able to use __array_function__ to dispatch over different operations (like convolve here). Some discussion along these lines in issue ( scipy/scipy#10204 ).
Yeah I’ve seen that. There’s also some work to add equivalent functions to CuPy.
The long term solution would be to use __array_function__ to dispatch. Maybe a short to medium term solution would be to provide our own dispatching mechanism.
I've been thinking about implementing some sort of dispatching solution. Maybe that gives us something useful in the near term to medium term. I'll try to put a POC together when I have a chance so we can take a look.
@jakirkham is it worth us having a quick discussion about the still to be decided dispatch mechanism? (Been trying and failing to get my thoughts coherent enough for text for a couple of months now)
I'm interested in putting in some of my own effort into helping things along. I've been doing a lot of reading around NEP 18, 37, etc. but I'm still pretty unclear about what things should look like for us here. Presumably you're a lot clearer on this in your own mind.
Yeah I think that is a great idea! Was thinking about suggesting that as well. Am on US Pacific timezone now so should be easier to come up with a time that works. Should we try to schedule something for next week?
So it seems GPU support for our ndfourier and ndmeasure modules will be significantly more complicated to implement. The extra difficulty here is because these modules don't have the same implementation pattern using map_overlap like we have in ndfilters etc.
One of the main blockers as I understand it is that there isn't currently any support for switching array backends for functions like dask.array.ones(), dask.array.arange() etc. These wrap numpy directly (see here for an example) and get used a lot - both in our code and in dask functions like dask.fft.fftfreq.
Worth noting: there is an open issue at dask/dask#6118 suggesting some problems with dask fourier transforms of cupy arrays might be solved with cupy release 8.0.0 this September. However, this won't fix all our problems because dask-image uses dask.fft.fftfreq which also relies on dask arange, which suffers the problem described in the previous paragraph.
We also use some numpy features that don't have equivalents in cupy - in particular we use the numpy.ndindex iterator for label comprehensions (which all of the ndmeasure module relies on), but there is no cupy.ndindex that exists.
Yeah the general array creation problem is a known issue, which is what Peter has been working on with PR ( numpy/numpy#16935 ). I think we will still need some updates to Dask. Though there have been a few people poking at that problem too.
There may be other ways to work around this shorter term. Happy to think about that as well.
Given what NumPy's ndindex is doing, I don't think we need a CuPy implementation per se. Though feel free to point out the issues you are seeing and would be happy to take a look 🙂
Given what NumPy's ndindex is doing, I don't think we need a CuPy implementation per se. Though feel free to point out the issues you are seeing and would be happy to take a look 🙂
I agree, all we need is a way to iterate over the thing, so that should be doable.
There may be other ways to work around this shorter term. Happy to think about that as well.
Maybe we should plan to catch up in two weeks or so after your break? Synchronous or asynchronous. At the moment I don't have a clear plan for GPU ndfourier and ndmeasure.
One trick that has been handy for these has been using da.ones_like where appropriate and building off of that. Here's one for arange. Though this could then be extended to handle other details like offsets, scalings, or other operations that can be constructed from a linear scaling.
importcupyascpimportnumpyasnpimportdask.arrayasdaa=da.from_array(cp.random.random((10,)), chunks=2)
r=da.cumsum(da.ones_like(a)) -1# `da.arange` now with the type of `a`
Note: If we need other flexibility like overriding the shape in da.ones_like, that is possible as long as we have NumPy 1.17.0+ ( numpy/numpy#13046 ) (probably a pretty reasonable requirement at this point) and Dask 2.19.0+ ( dask/dask#6064 ).
It turns out that binary_dilation and binary_erosion cupy functions do not exist right now. We'll need that for GPU support of ndmorph (the actual implementation will be pretty straightforward once those functions exist).
There are cupy functions for grey_dilation and grey_erosion that do exist, which have slightly different function signatures.
I went ahead and ported the binary morphology operations from cupyimg over to CuPy in cupy/cupy#3907. I don't think these will appear in CuPy 8.0, though, as 8.0.0rc1 was already released.
To be clear, ndfourier and ndmeasure are left still to do because they are a bit harder. They don't have that nice easy pattern with map_overlap like all the ndfilters functions. It's probably worth sitting down and working out what's best to tackle next.
Yeah, if we can get the fourier utils working with cupy, I think that's the bulk of the work there done. There are a few numpy array creation calls, so this is a good chance to try out some of Peter's new work. Labels also has bunch of similar stuff going on.
Yeah I think there we need to get da.arange to use the like= parameter. Don't think this is covered yet. This would allow us to use like= in fftfreq. Not too bad, but some work. Though this is just in Dask.
Yeah label is a bit more complicated as it also uses sparse matrices. There is some support for this in CuPy and Dask. I don't think we will get __array_function__ into SciPy though ( scipy/scipy#10362 ). So we might need to do some dispatching here ourselves. It's possible more recent iterations of array dispatching would be able to handle this somehow. We also need some things like scipy.sparse.csgraph.connected_components, which don't exist in CuPy yet, but may in the future ( cupy/cupy#4054 ).
Yeah I think there we need to get da.arange to use the like= parameter. Don't think this is covered yet. This would allow us to use like= in fftfreq. Not too bad, but some work. Though this is just in Dask.
I'm not right now, but I hope I can do that later in March. If you have a list of functions that would immediately benefit from it, I can certainly prioritize that. Do you see anything besides da.arange at the moment?
Yeah the rest of those functions should already be fine as they are taking arrays as input as opposed to creating new ones. So it sounds like just arange. This comes up in other places as well Peter.
Idk to what extent people use other things (like linspace) with Dask, but we might want to look at those as well. Though these can usually be generated from arange.
Yep I think that was covered in the latter half of this comment ( #133 (comment) ). Happy to follow up on anything from that which may have been missed, needs more info/discussion, etc.
It seems that all those functions, with the exception of exp are already tested in https://github.com/dask/dask/blob/master/dask/array/tests/test_cupy.py, so they are probably working fine. If you know you're using some functions that aren't covered there, I would appreciate adding tests to that file too, just copy the already existing Dask tests for NumPy-backed arrays in there with a few modifications to use CuPy and that's it, if they're not working yet today, marking them as xfail is also helpful so that we know they've been tested but aren't working today. 😄
I don't see scipy.sparse.csgraph.connected_components in that list
Besides what John already said, there's some exploratory work in plugging cuGraph into CuPy in cupy/cupy#4219 .
Just FYI, it's also being backported to CuPy 9.0 (planned for release April 22nd) in cupy/cupy#5113 . Assuming conda-forge/staged-recipes#14546 gets merged until then, it may be usable just after CuPy 9 release by installing libcugraph from conda-forge.
Reacted by Akira Naruse, Genevieve Buckley and jakirkham
Activity
jakirkham commentedon Jan 6, 2020
Here's what convolution would look like if someone rolled their own today using CuPy 7.0.0 and Dask 2.9.1. There are a few rough edges, but we could improve on this over time both in dask-image and upstream.
GenevieveBuckley commentedon Jan 6, 2020
Linking to this blog post from Matt about mixing dask and cupy arrays, since I found it helpful to read and it's relevant for anyone who comes across this issue: https://matthewrocklin.com/blog/work/2019/01/03/dask-array-gpus-first-steps
jakirkham commentedon Jan 7, 2020
Also we may be able to use
__array_function__to dispatch over different operations (likeconvolvehere). Some discussion along these lines in issue ( scipy/scipy#10204 ).GenevieveBuckley commentedon May 7, 2020
I haven't had a chance to look much into cupyimg (BSD-3 licensed), but that could also be useful to look at here: https://github.com/mritools/cupyimg
jakirkham commentedon May 7, 2020
Yeah I’ve seen that. There’s also some work to add equivalent functions to CuPy.
The long term solution would be to use
__array_function__to dispatch. Maybe a short to medium term solution would be to provide our own dispatching mechanism.jakirkham commentedon May 20, 2020
I've been thinking about implementing some sort of dispatching solution. Maybe that gives us something useful in the near term to medium term. I'll try to put a POC together when I have a chance so we can take a look.
GenevieveBuckley commentedon Jul 24, 2020
@jakirkham is it worth us having a quick discussion about the still to be decided dispatch mechanism? (Been trying and failing to get my thoughts coherent enough for text for a couple of months now)
I'm interested in putting in some of my own effort into helping things along. I've been doing a lot of reading around NEP 18, 37, etc. but I'm still pretty unclear about what things should look like for us here. Presumably you're a lot clearer on this in your own mind.
jakirkham commentedon Jul 24, 2020
Yeah I think that is a great idea! Was thinking about suggesting that as well. Am on US Pacific timezone now so should be easier to come up with a time that works. Should we try to schedule something for next week?
GenevieveBuckley commentedon Jul 25, 2020
Sounds good @jakirkham
I've just sent you an email, so we don't clog up the thread here with conversation about scheduling.
GenevieveBuckley commentedon Aug 26, 2020
So it seems GPU support for our
ndfourierandndmeasuremodules will be significantly more complicated to implement. The extra difficulty here is because these modules don't have the same implementation pattern usingmap_overlaplike we have inndfiltersetc.One of the main blockers as I understand it is that there isn't currently any support for switching array backends for functions like
dask.array.ones(),dask.array.arange()etc. These wrap numpy directly (see here for an example) and get used a lot - both in our code and in dask functions like dask.fft.fftfreq.Worth noting: there is an open issue at dask/dask#6118 suggesting some problems with dask fourier transforms of cupy arrays might be solved with cupy release 8.0.0 this September. However, this won't fix all our problems because dask-image uses
dask.fft.fftfreqwhich also relies on daskarange, which suffers the problem described in the previous paragraph.We also use some numpy features that don't have equivalents in cupy - in particular we use the
numpy.ndindexiterator for label comprehensions (which all of the ndmeasure module relies on), but there is nocupy.ndindexthat exists.jakirkham commentedon Aug 26, 2020
Thanks for looking into these 😄
Yeah the general array creation problem is a known issue, which is what Peter has been working on with PR ( numpy/numpy#16935 ). I think we will still need some updates to Dask. Though there have been a few people poking at that problem too.
There may be other ways to work around this shorter term. Happy to think about that as well.
Given what NumPy's
ndindexis doing, I don't think we need a CuPy implementation per se. Though feel free to point out the issues you are seeing and would be happy to take a look 🙂GenevieveBuckley commentedon Aug 26, 2020
I agree, all we need is a way to iterate over the thing, so that should be doable.
Maybe we should plan to catch up in two weeks or so after your break? Synchronous or asynchronous. At the moment I don't have a clear plan for GPU
ndfourierandndmeasure.jakirkham commentedon Aug 27, 2020
That sounds like a good plan.
One trick that has been handy for these has been using
da.ones_likewhere appropriate and building off of that. Here's one forarange. Though this could then be extended to handle other details like offsets, scalings, or other operations that can be constructed from a linear scaling.Note: If we need other flexibility like overriding the
shapeinda.ones_like, that is possible as long as we have NumPy 1.17.0+ ( numpy/numpy#13046 ) (probably a pretty reasonable requirement at this point) and Dask 2.19.0+ ( dask/dask#6064 ).GenevieveBuckley commentedon Aug 31, 2020
It turns out that
binary_dilationandbinary_erosioncupy functions do not exist right now. We'll need that for GPU support of ndmorph (the actual implementation will be pretty straightforward once those functions exist).There are cupy functions for
grey_dilationandgrey_erosionthat do exist, which have slightly different function signatures.grlee77 commentedon Sep 1, 2020
I went ahead and ported the binary morphology operations from
cupyimgover to CuPy in cupy/cupy#3907. I don't think these will appear in CuPy 8.0, though, as8.0.0rc1was already released.13 remaining items
GenevieveBuckley commentedon Mar 2, 2021
To be clear,
ndfourierandndmeasureare left still to do because they are a bit harder. They don't have that nice easy pattern withmap_overlaplike all the ndfilters functions. It's probably worth sitting down and working out what's best to tackle next.jakirkham commentedon Mar 2, 2021
Thanks for the update Genevieve! 😄
Yeah I'd need to look at the
ndfourieragain. It may be that Peter's recent work unblocks these (namely we have better tools for array creation).With
ndmeasure, havinglabelin particular would be really useful. There are other things in there that would be useful, but may be trickier to port.I think we've talked about the value of
regionprops. Though that's kind of its own issue outside of this one.GenevieveBuckley commentedon Mar 2, 2021
Yeah, if we can get the fourier utils working with cupy, I think that's the bulk of the work there done. There are a few numpy array creation calls, so this is a good chance to try out some of Peter's new work. Labels also has bunch of similar stuff going on.
jakirkham commentedon Mar 2, 2021
Yeah I think there we need to get
da.arangeto use thelike=parameter. Don't think this is covered yet. This would allow us to uselike=infftfreq. Not too bad, but some work. Though this is just in Dask.Yeah
labelis a bit more complicated as it also uses sparse matrices. There is some support for this in CuPy and Dask. I don't think we will get__array_function__into SciPy though ( scipy/scipy#10362 ). So we might need to do some dispatching here ourselves. It's possible more recent iterations of array dispatching would be able to handle this somehow. We also need some things likescipy.sparse.csgraph.connected_components, which don't exist in CuPy yet, but may in the future ( cupy/cupy#4054 ).GenevieveBuckley commentedon Mar 2, 2021
Is that something that @pentschev is working on?
pentschev commentedon Mar 2, 2021
I'm not right now, but I hope I can do that later in March. If you have a list of functions that would immediately benefit from it, I can certainly prioritize that. Do you see anything besides
da.arangeat the moment?GenevieveBuckley commentedon Mar 3, 2021
I'm not sure how compatible or not things already are, but to give you a sense of what our fourier module is using, we have:
dask.array.meshgridanddask.array.fft.fftfreqin the _utilsdask-image/dask_image/ndfourier/_utils.py
Lines 20 to 25 in 91c4955
... and also get used here are:
dask.array.tensordotdask.array.expdask.array.sincdask.array.prodHopefully some of these will be fine without any changes (fingers crossed)
jakirkham commentedon Mar 3, 2021
Yeah the rest of those functions should already be fine as they are taking arrays as input as opposed to creating new ones. So it sounds like just
arange. This comes up in other places as well Peter.Idk to what extent people use other things (like
linspace) with Dask, but we might want to look at those as well. Though these can usually be generated fromarange.GenevieveBuckley commentedon Mar 3, 2021
Labels rely a lot on sparse matrices.
I found this table explaining what sparse matrix support cupy has: https://docs.cupy.dev/en/stable/reference/sparse.html
I don't see
scipy.sparse.csgraph.connected_componentsin that listdask-image/dask_image/ndmeasure/_utils/_label.py
Line 23 in 91c4955
That's the only thing that stands out to me there.
jakirkham commentedon Mar 3, 2021
Yep I think that was covered in the latter half of this comment ( #133 (comment) ). Happy to follow up on anything from that which may have been missed, needs more info/discussion, etc.
pentschev commentedon Mar 4, 2021
It seems that all those functions, with the exception of
expare already tested in https://github.com/dask/dask/blob/master/dask/array/tests/test_cupy.py, so they are probably working fine. If you know you're using some functions that aren't covered there, I would appreciate adding tests to that file too, just copy the already existing Dask tests for NumPy-backed arrays in there with a few modifications to use CuPy and that's it, if they're not working yet today, marking them as xfail is also helpful so that we know they've been tested but aren't working today. 😄Besides what John already said, there's some exploratory work in plugging cuGraph into CuPy in cupy/cupy#4219 .
jakirkham commentedon Mar 4, 2021
Included
expin those tests with PR ( dask/dask#7322 )GenevieveBuckley commentedon Apr 20, 2021
FYI: connected components has been merged into the cupy master branch (should be included in the cupy 10.0.0 release): cupy/cupy#4940
This might be something we need to have GPU support for
labelpentschev commentedon Apr 20, 2021
Just FYI, it's also being backported to CuPy 9.0 (planned for release April 22nd) in cupy/cupy#5113 . Assuming conda-forge/staged-recipes#14546 gets merged until then, it may be usable just after CuPy 9 release by installing
libcugraphfrom conda-forge.jakirkham commentedon May 20, 2021
Yeah, on the cuGraph point, this is supported in the CuPy 9 build in conda-forge