Skip to content

NEP-18: mean_chunk() object __array__ method not producing an array #4481

Closed

Description

@pentschev

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 array

Activity

  1. mrocklin commented on Feb 13, 2019

    @mrocklin
    Member

    Hrm, @jcrist do you have any suggestions here? The challenge is that we're creating an empty numpy array and then assigning into it. This makes things hard for non-Numpy implementations like sparse or cupy.

    Two thoughts:

    1. We could see if dask array is returning a tuple rather than a record array. My guess is that it will get upset
    2. We could use empty_like for our dispatch rather than empty. This would allow the __array_function__ protocol to work as desired.
  2. jcrist commented on Feb 13, 2019

    @jcrist
    Member

    We already have a dispatch for empty, found in dask.array.reductions, could cupy just register an implementation for that?

    empty_like won't work, since you can't override the shape (unfortunately).

  3. mrocklin commented on Feb 13, 2019

    @mrocklin
    Member

    Right, following @jcrist 's suggestion this should be easy.

    @pentschev you probably want something like the following in dask/array/backends.py

    from .reductions import empty_lookup
    empty_lookup.register(cupy.ndarray, np.cupy)
  4. pentschev commented on Feb 14, 2019

    @pentschev
    MemberAuthor
    from .reductions import empty_lookup
    empty_lookup.register(cupy.ndarray, np.cupy)

    Yes, that probably would solve the problem, but cupy.empty() doesn't support a list of tuples as dtype. Just as means of reproduction consider the following:

    In [25]: n = cupy.array([[1000000.]])
    
    In [26]: n
    Out[26]: array([[1000000.]])
    
    In [27]: n.shape
    Out[27]: (1, 1)
    
    In [28]: cupy.empty(n.shape, dtype=[('total', total.dtype), ('n', total.dtype)])
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-28-ea9bfab84d63> in <module>
    ----> 1 cupy.empty(n.shape, dtype=[('total', total.dtype), ('n', total.dtype)])
    
    ~/.local/lib/python3.5/site-packages/cupy/creation/basic.py in empty(shape,
    dtype, order)
         20 
         21     """
    ---> 22     return cupy.ndarray(shape, dtype, order=order)
         23 
         24 
    
    ~/src/cupy/cupy/core/core.pyx in cupy.core.core.ndarray.__init__()
    
    ~/src/cupy/cupy/core/_dtype.pyx in cupy.core._dtype.get_dtype_with_itemsize()
    
    TypeError: unhashable type: 'list'

    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.

  5. mrocklin commented on Feb 14, 2019

    @mrocklin
    Member
  6. pentschev commented on Feb 14, 2019

    @pentschev
    MemberAuthor

    @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.

  7. mrocklin commented on Feb 14, 2019

    @mrocklin
    Member

    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.

  8. mrocklin commented on Feb 14, 2019

    @mrocklin
    Member

    So the following change is probably needed and welcome on the dask side

    -   result = empty(n.shape, dtype=[('total', total.dtype), ('n', n.dtype)])
    +   result = empty(n.shape, dtype=np.dtype([('total', total.dtype), ('n', n.dtype)]))
  9. pentschev commented on Feb 14, 2019

    @pentschev
    MemberAuthor

    Yes, that should work. I can try it out now and submit a PR, or are you working on this already @mrocklin ?

  10. mrocklin commented on Feb 14, 2019

    @mrocklin
    Member

    All yours

  11. pentschev commented on Feb 14, 2019

    @pentschev
    MemberAuthor

    Well, there's yet another CuPy problem when assigning a value to result, I guess it can only handle indices with the bracket operator:

    Traceback (most recent call last):
      File "empty.py", line 9, in <module>
        result['n'] = n
      File "cupy/core/core.pyx", line 1183, in cupy.core.core.ndarray.__setitem__
      File "cupy/core/_routines_indexing.pyx", line 49, in cupy.core._routines_indexing._ndarray_setitem
      File "cupy/core/_routines_indexing.pyx", line 726, in cupy.core._routines_indexing._scatter_op
      File "cupy/core/_routines_indexing.pyx", line 309, in cupy.core._routines_indexing._simple_getitem
    ValueError: invalid literal for int() with base 10: 'n'

    Looking at cupy.core._routines_indexing._simple_getitem(), this is where it fails:

    308         elif numpy.isscalar(s):
    309             ind = int(s)

    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.

  12. mrocklin commented on Feb 14, 2019

    @mrocklin
    Member
  13. pentschev commented on Feb 14, 2019

    @pentschev
    MemberAuthor

    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.

  14. mrocklin commented on Feb 20, 2019

    @mrocklin
    Member

    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.

  15. pentschev commented on Feb 20, 2019

    @pentschev
    MemberAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions