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
Start trying out Numpy syntax, and see what breaks
x.sum().compute()
x.mean().compute()
(x+x.T).sum(axis=1).compute()
... # there are many other things that we could do hereu, s, v=da.linalg.svd(x)
s.compute()
Probably after this we will learn about many flaws either in Dask Array, CuPy, or both.
I know the example given in the description is just a reference, but since some corrections are necessary, below is a fully functional sample (after the PR mentioned above is merged):
That's pretty exciting! I'll add a couple comments to the CuPy PR.
Some follow ups:
We'll also need to call .compute() at the end of the other computations like x.mean() as well. This triggers the actual computation and verifies that things work at runtime.
A, whoops. @pentschev mentioned in side-channels that I probably meant d.mean().compute() above. The .compute() method is a dask method that turns a lazy Dask thing into a concrete Numpy or CuPy thing.
It looks like we have our own custom empty_dispatch object, which will probably end up creating a numpy array. Then when we assign a cupy array into that array cupy complains because it doesn't like coercing itself into Numpy. The empty_dispatch dispatcher was probably made before NEP-18 arrived.
In principle, we probably want to use np.empty_like instead, which takes an array object and dispatches based on that (either numpy or cupy or sparse or whatever) and the NEP-18 dispatching magic can take over.
However this requires some finesse because we still need to support non-NEP-18 enabled installs (which are currently the majority). I'm not yet sure what the right approach is here yet.
@mrocklin thanks for the detailed description. I will try to dive a bit into the code and see if I can suggest a fix that covers both cases. Is it safe to assume that existing tests cover the pre-NEP-18 case?
I also wouldn't dive too deeply into the mean issue if it's proving to be a rabbit hole. I think it's probably more valuable to search broadly first, uncover many issues, and then we can think a bit more holistically about how to handle things. There are others we can also rely on when diving in deeply here.
Here is a minimal example with dask-glm, which I think serves as a good general application here.
importnumpyasnpimportdask.arrayasdaX=np.random.random((1000, 10)) # ideally we swap this out with cupydX=da.from_array(X, chunks=(100, 10))
y=np.ones(1000) # ideally we swap this out with cupydy=da.from_array(y, chunks=(100,))
importdask_glm.algorithmsdask_glm.algorithms.admm(dX, dy, max_iter=5)
dask_glm.algorithms.proximal_grad(dX, dy, max_iter=5)
After NEP-18, Dask will be capable of working on arrays from NumPy-like libraries, e.g., CuPy. For such unimplemented methods, such as argtopk() and take_along_axis() (a dependency of the former), a TypeError will simply be emitted:
I see two general approaches to situations like these:
Implement take_along_axis in cupy, either by asking them to implementing it, or implementing it ourselves. Raising it as an issue is probably useful regardless
Change Dask code to avoid specialized functions in favor of more generic and commonly implemented ones. If there is not a significant performance or complexity concern then I suspect that this is fine.
Please, don't get too attached to the example I presented, it was randomly chosen. Implementing all missing functions is probably out of question, there's hundreds of them:
Understood. The two approaches above are probably the same two approaches for other cases as well. If it's easy to add to cupy then lets do it. Otherwise lets see if we can easily avoid the operation in Dask Array. Either approach is probably slightly useful outside of this endeavor as well.
From my latest bug hunt I've found some new issues, starting with those directly related to lack of fully NumPy-compliant implementations in CuPy:
cupy.einsum(): doesn’t implement things like order= and casting=, Dask sets them and CuPy therefore fails for not recognizing such arguments
cupy.histogram(): doesn’t implement weights= and fail due to Dask passing it. Additionally, several other arguments don’t exist, making it a capped implementation of NumPy
For the two issues mentioned above, the options I see for now are:
Open issues on GitHub (but they are likely not to be implemented in quite some time)
Implement them ourselves, eventually (may take a lot of time as well)
Warn that Dask+CuPy doesn’t work for those functions
dask.array.eye() , dask.array.fromfunction: should we add *_like() (as in empty_like()) to allow the creation of Dask arrays based on CuPy?
Please note that I'm mentioning CuPy only here because that's what I've been working on mostly, but all these issues will eventually happen when we want to work with other NumPy-like libraries. Also, all issues mentioned above are not an extensive list, just the ones I've found so far, they will most likely happen for other functions too.
On a more general note, for me personally I think it may be helpful to set milestones for Dask's NEP-18 support. At first, it seemed that there wouldn't be so many issues, but unfortunately we have many due to lack of features that Dask implements and CuPy does not, as well as various bugs in both libraries.
My suggestion is that we come up with subsets of functions (probably ordered by importance) and tackle them by that order, to prevent us from working only on least important issues and forgetting in the short-term about the important ones. Since I'm relatively new to Dask, I don't think I'm qualified to come up with such a list, so any input here is very welcome.
@jakirkham supposing there was a resize() on all libraries, what I don't like from your proposal is the arr.copy(), for large arrays this would be very prohibitive. But if we add specializations for different libraries (as you did for masked_array), we could always check the input array and create it a new one based on its type. Sure, it would be sub-optimal from a code perspective, as we would need to explicitly add support for each one, but would work, plus there aren't too many libraries supporting __array_function__ yet.
If doing this is acceptable until we get shape added to _like() functions, then it would indeed help us in some cases.
That's a fair complaint. Here's why the copy is there.
It would be nice if the resize implementation did not mutate the input array.
resize requires that the data be owned by the object used.
If we didn't care about 1, we could replace this with arr = numpy.require(arr, requirements="O"), which will only copy if the array doesn't own the data.
@jakirkham sorry, maybe I wasn't clear with what I meant. I do understand why copy was there, but I think that's too prohibitive. In fact, avoiding a copy was the whole reason we're pushing to have a shape argument in the *_like() functions.
As to the specializations, I think we didn't want to go down that road for a permanent solution. Maybe we could reconsider for a temporary one?
I was attempting to avoid this even as a temporary one, but if that's acceptable, we could go down that road. There's no need to make it a permanent solution anyway, I believe that once we can get like arrays with shape, we could solve that issue for good, at least I don't currently see any other issues that may prevent that from being a permanent solution.
The error I am getting is: ValueError: object __array__ method not producing an array
You need NumPy 1.16.x and environment variable NUMPY_EXPERIMENTAL_ARRAY_FUNCTION=1. Starting with NumPy 1.17, the environment variable won't be necessary anymore.
Activity
pentschev commentedon Feb 11, 2019
This should work after my PR cupy/cupy#2026
I know the example given in the description is just a reference, but since some corrections are necessary, below is a fully functional sample (after the PR mentioned above is merged):
mrocklin commentedon Feb 11, 2019
That's pretty exciting! I'll add a couple comments to the CuPy PR.
Some follow ups:
.compute()at the end of the other computations likex.mean()as well. This triggers the actual computation and verifies that things work at runtime.mrocklin commentedon Feb 11, 2019
A, whoops. @pentschev mentioned in side-channels that I probably meant
d.mean().compute()above. The.compute()method is a dask method that turns a lazy Dask thing into a concrete Numpy or CuPy thing.pentschev commentedon Feb 11, 2019
Currently,
d.mean().compute()fails:mrocklin commentedon Feb 11, 2019
So the relevant function is here:
It looks like we have our own custom
empty_dispatchobject, which will probably end up creating a numpy array. Then when we assign a cupy array into that array cupy complains because it doesn't like coercing itself into Numpy. Theempty_dispatchdispatcher was probably made before NEP-18 arrived.In principle, we probably want to use
np.empty_likeinstead, which takes an array object and dispatches based on that (either numpy or cupy or sparse or whatever) and the NEP-18 dispatching magic can take over.However this requires some finesse because we still need to support non-NEP-18 enabled installs (which are currently the majority). I'm not yet sure what the right approach is here yet.
pentschev commentedon Feb 11, 2019
@mrocklin thanks for the detailed description. I will try to dive a bit into the code and see if I can suggest a fix that covers both cases. Is it safe to assume that existing tests cover the pre-NEP-18 case?
mrocklin commentedon Feb 11, 2019
You should make sure that you also have the
sparselibrary installed, which is another numpy-like array that we like to supportmrocklin commentedon Feb 12, 2019
I also wouldn't dive too deeply into the
meanissue if it's proving to be a rabbit hole. I think it's probably more valuable to search broadly first, uncover many issues, and then we can think a bit more holistically about how to handle things. There are others we can also rely on when diving in deeply here.mrocklin commentedon Feb 12, 2019
Here is a minimal example with dask-glm, which I think serves as a good general application here.
pentschev commentedon Feb 13, 2019
After NEP-18, Dask will be capable of working on arrays from NumPy-like libraries, e.g., CuPy. For such unimplemented methods, such as
argtopk()andtake_along_axis()(a dependency of the former), a TypeError will simply be emitted:I think a discussion on handling this case is important for the near future.
mrocklin commentedon Feb 13, 2019
I see two general approaches to situations like these:
take_along_axisin cupy, either by asking them to implementing it, or implementing it ourselves. Raising it as an issue is probably useful regardlesspentschev commentedon Feb 13, 2019
Please, don't get too attached to the example I presented, it was randomly chosen. Implementing all missing functions is probably out of question, there's hundreds of them:
https://docs-cupy.chainer.org/en/stable/reference/comparison.html
mrocklin commentedon Feb 13, 2019
Understood. The two approaches above are probably the same two approaches for other cases as well. If it's easy to add to cupy then lets do it. Otherwise lets see if we can easily avoid the operation in Dask Array. Either approach is probably slightly useful outside of this endeavor as well.
pentschev commentedon Feb 19, 2019
From my latest bug hunt I've found some new issues, starting with those directly related to lack of fully NumPy-compliant implementations in CuPy:
cupy.einsum(): doesn’t implement things likeorder=andcasting=, Dask sets them and CuPy therefore fails for not recognizing such argumentscupy.histogram(): doesn’t implementweights=and fail due to Dask passing it. Additionally, several other arguments don’t exist, making it a capped implementation of NumPyFor the two issues mentioned above, the options I see for now are:
dask.array.eye(),dask.array.fromfunction: should we add*_like()(as inempty_like()) to allow the creation of Dask arrays based on CuPy?Please note that I'm mentioning CuPy only here because that's what I've been working on mostly, but all these issues will eventually happen when we want to work with other NumPy-like libraries. Also, all issues mentioned above are not an extensive list, just the ones I've found so far, they will most likely happen for other functions too.
@shoyer @jakirkham this may interest you.
pentschev commentedon Feb 19, 2019
On a more general note, for me personally I think it may be helpful to set milestones for Dask's NEP-18 support. At first, it seemed that there wouldn't be so many issues, but unfortunately we have many due to lack of features that Dask implements and CuPy does not, as well as various bugs in both libraries.
My suggestion is that we come up with subsets of functions (probably ordered by importance) and tackle them by that order, to prevent us from working only on least important issues and forgetting in the short-term about the important ones. Since I'm relatively new to Dask, I don't think I'm qualified to come up with such a list, so any input here is very welcome.
6 remaining items
pentschev commentedon Mar 15, 2019
@jakirkham supposing there was a
resize()on all libraries, what I don't like from your proposal is thearr.copy(), for large arrays this would be very prohibitive. But if we add specializations for different libraries (as you did formasked_array), we could always check the input array and create it a new one based on its type. Sure, it would be sub-optimal from a code perspective, as we would need to explicitly add support for each one, but would work, plus there aren't too many libraries supporting__array_function__yet.If doing this is acceptable until we get
shapeadded to_like()functions, then it would indeed help us in some cases.jakirkham commentedon Mar 15, 2019
That's a fair complaint. Here's why the
copyis there.resizeimplementation did not mutate the input array.resizerequires that the data be owned by the object used.If we didn't care about 1, we could replace this with
arr = numpy.require(arr, requirements="O"), which will only copy if the array doesn't own the data.pentschev commentedon Mar 15, 2019
@jakirkham sorry, maybe I wasn't clear with what I meant. I do understand why
copywas there, but I think that's too prohibitive. In fact, avoiding a copy was the whole reason we're pushing to have ashapeargument in the*_like()functions.pentschev commentedon Mar 15, 2019
In other words: I agree what you suggested would be a nice workaround if we could avoid this, potentially huge, overhead of data duplication.
jakirkham commentedon Mar 15, 2019
No worries. Just wanted to make sure we were on the same page.
If we go the
numpy.requireroute, we would sometimes avoid the copy. Am not sure how often "sometimes" is currently.It's worth thinking about. Let me know if you have any ideas. ;)
jakirkham commentedon Mar 15, 2019
As to the specializations, I think we didn't want to go down that road for a permanent solution. Maybe we could reconsider for a temporary one?
pentschev commentedon Mar 15, 2019
I was attempting to avoid this even as a temporary one, but if that's acceptable, we could go down that road. There's no need to make it a permanent solution anyway, I believe that once we can get
likearrays with shape, we could solve that issue for good, at least I don't currently see any other issues that may prevent that from being a permanent solution.system123 commentedon Jul 17, 2019
The following example is failing for me with CuPy 6.1.0 and Master branch of Dask:
The error I am getting is:
ValueError: object __array__ method not producing an arraypentschev commentedon Jul 17, 2019
You need NumPy 1.16.x and environment variable
NUMPY_EXPERIMENTAL_ARRAY_FUNCTION=1. Starting with NumPy 1.17, the environment variable won't be necessary anymore.jrbourbeau commentedon Jul 17, 2019
@system123 does setting the environment variable
NUMPY_EXPERIMENTAL_ARRAY_FUNCTION=1as @pentschev suggested fix your issue?system123 commentedon Jul 17, 2019
Just got around to testing it and it is working after setting the env variable.
jrbourbeau commentedon Jul 17, 2019
Great, glad to hear it. Thanks for the fix @pentschev
quasiben commentedon Sep 16, 2020
I think this is largely supported now and we can close. If there are edge cases we should open new issues and resolve