Repository navigation
Closed
Description
Several Dask operations that utilize mean_chunk() from dask/array/reductions.py fail for Dask arrays created from non-NumPy (e.g., CuPy, sparse) arrays.
Some of the operations confirmed to fail are (including other non-core Dask projects):
dask.mean()dask.glm.algorithms.*
A sample to reproduce the issue is given below, followed by its traceback.
import cupy
import dask.array as da
x = cupy.random.random((5000, 1000))
d = da.from_array(x, chunks=(1000, 1000), asarray=False)
d.mean().compute()Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/nfs/pentschev/.local/lib/python3.5/site-packages/dask/base.py", line 156, in compute
(result,) = compute(self, traverse=False, **kwargs)
File "/home/nfs/pentschev/.local/lib/python3.5/site-packages/dask/base.py", line 398, in compute
results = schedule(dsk, keys, **kwargs)
File "/home/nfs/pentschev/.local/lib/python3.5/site-packages/dask/threaded.py", line 76, in get
pack_exception=pack_exception, **kwargs)
File "/home/nfs/pentschev/.local/lib/python3.5/site-packages/dask/local.py", line 459, in get_async
raise_exception(exc, tb)
File "/home/nfs/pentschev/.local/lib/python3.5/site-packages/dask/compatibility.py", line 112, in reraise
raise exc
File "/home/nfs/pentschev/.local/lib/python3.5/site-packages/dask/local.py", line 230, in execute_task
result = _execute_task(task, data)
File "/home/nfs/pentschev/.local/lib/python3.5/site-packages/dask/core.py", line 119, in _execute_task
return func(*args2)
File "/home/nfs/pentschev/.local/lib/python3.5/site-packages/dask/optimization.py", line 942, in __call__
dict(zip(self.inkeys, args)))
File "/home/nfs/pentschev/.local/lib/python3.5/site-packages/dask/core.py", line 149, in get
result = _execute_task(task, cache)
File "/home/nfs/pentschev/.local/lib/python3.5/site-packages/dask/core.py", line 119, in _execute_task
return func(*args2)
File "/home/nfs/pentschev/.local/lib/python3.5/site-packages/dask/compatibility.py", line 93, in apply
return func(*args, **kwargs)
File "/home/nfs/pentschev/.local/lib/python3.5/site-packages/dask/array/reductions.py", line 336, in mean_chunk
result['n'] = n
ValueError: object __array__ method not producing an arrayMetadata
Metadata
Assignees
Labels
No labels
Activity
mrocklin commentedon Feb 13, 2019
Hrm, @jcrist do you have any suggestions here? The challenge is that we're creating an
emptynumpy array and then assigning into it. This makes things hard for non-Numpy implementations like sparse or cupy.Two thoughts:
empty_likefor our dispatch rather thanempty. This would allow the__array_function__protocol to work as desired.jcrist commentedon Feb 13, 2019
We already have a dispatch for
empty, found indask.array.reductions, could cupy just register an implementation for that?empty_likewon't work, since you can't override the shape (unfortunately).mrocklin commentedon Feb 13, 2019
Right, following @jcrist 's suggestion this should be easy.
@pentschev you probably want something like the following in
dask/array/backends.pypentschev commentedon Feb 14, 2019
Yes, that probably would solve the problem, but
cupy.empty()doesn't support a list of tuples asdtype. Just as means of reproduction consider the following:I'm not sure whether this is a bug or a missing feature in CuPy. But as far as I can tell, this empty array is only used for internal Dask computing, so an alternative would be to replace it by something else, since it's only used to hold two values by their names, just like a hash table.
mrocklin commentedon Feb 14, 2019
pentschev commentedon Feb 14, 2019
@mrocklin I'm opening the issue on CuPy's tracker. If you find an appropriate way of avoiding the use of record arrays, I could also work on the implementation if you'd like, just let me know.
mrocklin commentedon Feb 14, 2019
Raised here: cupy/cupy#2031
It looks like we can resolve this on our end just by adding a
np.dtype(...)call around the list.mrocklin commentedon Feb 14, 2019
So the following change is probably needed and welcome on the dask side
pentschev commentedon Feb 14, 2019
Yes, that should work. I can try it out now and submit a PR, or are you working on this already @mrocklin ?
mrocklin commentedon Feb 14, 2019
All yours
pentschev commentedon Feb 14, 2019
Well, there's yet another CuPy problem when assigning a value to result, I guess it can only handle indices with the bracket operator:
Looking at
cupy.core._routines_indexing._simple_getitem(), this is where it fails:According to NumPy's documentation a string is a scalar, and CuPy immediately tries to convert to an int, as it seems to be the case that only indices are expected there.
mrocklin commentedon Feb 14, 2019
pentschev commentedon Feb 14, 2019
Agreed. Perhaps this could be part of your recently opened issue cupy/cupy#2031, since this is more of a follow-up case? If some fix like the one you suggested, this will definitely be next issue, so I see no reason to fix one without the other.
Modify mean chunk functions to return dicts rather than arrays
mrocklin commentedon Feb 20, 2019
I've made an attempt at this in #4513 it's not complete though. If anyone has time to investigate it further that would be welcome.
pentschev commentedon Feb 20, 2019
I had a quick look here, but it really seems that it's a more complicated issue than it seemed at first. I will try to investigate a little further.
By the way, I would say this is probably the highest priority bug on the Dask side regarding the CuPy integration. I've seen many functions breaking due to
mean_chunk(), and this is also the breaking point for Dask GLM algorithms as well. Most likely we'll uncover other bugs after fixing this as well, but we should really fix this soon.Modify mean chunk functions to return dicts rather than arrays (#4513)
Modify mean chunk functions to return dicts rather than arrays (dask#…