Skip to content

da.diagonal is broken #5661

Closed
Closed

Description

@TAdeJong

While working on #2726, I found that da.diagonal() is broken in a way that I do not understand.
If both of the dimensions of a 2D input contains more than one chunk, the resulting graph can still be computed, but any composing that graph with another graph containing something else than itself, can not be computed, failing with:

"UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U46'), dtype('<U46')) -> dtype('<U46')"

In code:

x = da.ones((8,8), chunks=(4,4))
dd = da.ones((8,8), chunks=(4,4))
d = da.diagonal(dd)
print(d.compute())  # works
print((d+d).compute())  # also works
A = (d + x)
A.compute() # broken.
Details
---------------------------------------------------------------------------
UFuncTypeError                            Traceback (most recent call last)
<ipython-input-106-1caaff1e195d> in <module>
      5 print((d+d).compute())  # also works
      6 A = (d + x)
----> 7 A.compute() # broken.

~/code/dask-dev/dask/dask/base.py in compute(self, **kwargs)
    163         dask.base.compute
    164         """
--> 165         (result,) = compute(self, traverse=False, **kwargs)
    166         return result
    167 

~/code/dask-dev/dask/dask/base.py in compute(*args, **kwargs)
    434     keys = [x.__dask_keys__() for x in collections]
    435     postcomputes = [x.__dask_postcompute__() for x in collections]
--> 436     results = schedule(dsk, keys, **kwargs)
    437     return repack([f(r, *a) for r, (f, a) in zip(results, postcomputes)])
    438 

~/code/dask-dev/dask/dask/threaded.py in get(dsk, result, cache, num_workers, pool, **kwargs)
     79         get_id=_thread_get_id,
     80         pack_exception=pack_exception,
---> 81         **kwargs
     82     )
     83 

~/code/dask-dev/dask/dask/local.py in get_async(apply_async, num_workers, dsk, result, cache, get_id, rerun_exceptions_locally, pack_exception, raise_exception, callbacks, dumps, loads, **kwargs)
    484                         _execute_task(task, data)  # Re-execute locally
    485                     else:
--> 486                         raise_exception(exc, tb)
    487                 res, worker_id = loads(res_info)
    488                 state["cache"][key] = res

~/code/dask-dev/dask/dask/local.py in reraise(exc, tb)
    314     if exc.__traceback__ is not tb:
    315         raise exc.with_traceback(tb)
--> 316     raise exc
    317 
    318 

~/code/dask-dev/dask/dask/local.py in execute_task(key, task_info, dumps, loads, get_id, pack_exception)
    220     try:
    221         task, data = loads(task_info)
--> 222         result = _execute_task(task, data)
    223         id = get_id()
    224         result = dumps((result, id))

~/code/dask-dev/dask/dask/core.py in _execute_task(arg, cache, dsk)
    117         func, args = arg[0], arg[1:]
    118         args2 = [_execute_task(a, cache) for a in args]
--> 119         return func(*args2)
    120     elif not ishashable(arg):
    121         return arg

~/code/dask-dev/dask/dask/optimization.py in __call__(self, *args)
    980         if not len(args) == len(self.inkeys):
    981             raise ValueError("Expected %d args, got %d" % (len(self.inkeys), len(args)))
--> 982         return core.get(self.dsk, self.outkey, dict(zip(self.inkeys, args)))
    983 
    984     def __reduce__(self):

~/code/dask-dev/dask/dask/core.py in get(dsk, out, cache)
    147     for key in toposort(dsk):
    148         task = dsk[key]
--> 149         result = _execute_task(task, cache)
    150         cache[key] = result
    151     result = _execute_task(out, cache)

~/code/dask-dev/dask/dask/core.py in _execute_task(arg, cache, dsk)
    117         func, args = arg[0], arg[1:]
    118         args2 = [_execute_task(a, cache) for a in args]
--> 119         return func(*args2)
    120     elif not ishashable(arg):
    121         return arg

UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U46'), dtype('<U46')) -> dtype('<U46')

Replacing dd by da.ones((8,8), chunks=(8,4)) or da.ones((8,8), chunks=(4,8)) does not expose the problem.

Activity

  1. TAdeJong commented on Dec 2, 2019

    @TAdeJong
    ContributorAuthor

    I would be happy to work on this and make a pull request together with my work on #2726, but I am a bit at a loss at what is happening here/ how to debug.

  2. jacobtomlinson commented on Dec 3, 2019

    @jacobtomlinson
    Member
  3. mrocklin commented on Dec 3, 2019

    @mrocklin
    Member

    I'm able to reproduce the problem. Based on the dtype error my guess is that key values are leaking out into the actual values. I haven't investigated any further though. If someone wanted to dive further I would recommend running the computation with compute(scheduler=single-threaded), and then use pdb to see what is going on when the error occurs.

  4. TomAugspurger commented on Feb 6, 2020

    @TomAugspurger
    Member

    A bit smaller reproducer

        a = da.ones((8, 8), chunks=(4, 4))
        result = a.rechunk(((4, 4), (4, 0, 0, 4)))
        result.compute()

    Will try to figure this out today.

  5. TomAugspurger commented on Feb 6, 2020

    @TomAugspurger
    Member

    Going to have to table this for a bit, but this seems a bit strange in dask.array.rechunk._compute_rechunk

    -> for new_idx, cross1 in zip(new_index, crossed):
    (Pdb) pp len(list(new_index)), len(list(crossed))
    (8, 4)

    The length-0 blocks are throwing some computation off

    (Pdb) pp new_index
    [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3)]
    (Pdb) pp crossed
    [(((0, slice(0, 4, None)), (0, slice(0, 4, None))),),
     (((0, slice(0, 4, None)), (1, slice(0, 4, None))),),
     (((1, slice(0, 4, None)), (0, slice(0, 4, None))),),
     (((1, slice(0, 4, None)), (1, slice(0, 4, None))),)]
  6. TomAugspurger commented on Feb 11, 2020

    @TomAugspurger
    Member

    Here's some currently-failing tests. Some of the expected values may be debatable, but I think getting those passing would (help) get this issue fixed.

    diff --git a/dask/array/tests/test_rechunk.py b/dask/array/tests/test_rechunk.py
    index a3a775a7a..55876addb 100644
    --- a/dask/array/tests/test_rechunk.py
    +++ b/dask/array/tests/test_rechunk.py
    @@ -745,3 +745,57 @@ def test_rechunk_bad_keys():
             x.rechunk({-100: 4})
     
         assert "-100" in str(info.value)
    +
    +
    +def test_rechunk_with_empty():
    +    a = da.ones((8, 8), chunks=(4, 4))
    +    result = a.rechunk(((4, 4), (4, 0, 0, 4)))
    +    expected = da.ones((8, 8), chunks=((4, 4), (4, 0, 0, 4)))
    +    assert_eq(result, expected)
    +
    +
    +def test_intersect_chunks_nonempty():
    +    from dask.array.rechunk import intersect_chunks
    +
    +    old = ((4, 4), (2,))
    +    new = ((8,), (1, 1))
    +    result = list(intersect_chunks(old, new))
    +    expected = [
    +        (
    +            ((0, slice(0, 4, None)), (0, slice(0, 1, None))),
    +            ((1, slice(0, 4, None)), (0, slice(0, 1, None))),
    +        ),
    +        (
    +            ((0, slice(0, 4, None)), (0, slice(1, 2, None))),
    +            ((1, slice(0, 4, None)), (0, slice(1, 2, None))),
    +        ),
    +    ]
    +    assert result == expected
    +
    +
    +def test_intersect_chunks_empty():
    +    from dask.array.rechunk import intersect_chunks
    +
    +    old = ((4, 4), (2,))
    +    new = ((4, 0, 0, 4), (1, 1))
    +    result = list(intersect_chunks(old, new))
    +    expected = [
    +        (
    +            ((0, slice(0, 4, None)), (0, slice(0, 1, None))),
    +            ((1, slice(0, 4, None)), (0, slice(0, 1, None))),
    +        ),
    +        (
    +            ((0, slice(0, 4, None)), (0, slice(1, 2, None))),
    +            ((1, slice(0, 4, None)), (0, slice(1, 2, None))),
    +        ),
    +    ]
    +    assert result == expected
    +
    +
    +def test_old_to_new_empty():
    +    from dask.array.rechunk import _old_to_new
    +    old = ((4, 4,),)
    +    new = ((4, 0, 4),)
    +    result = _old_to_new(old, new)
    +    expected = [[[(0, slice(0, 4))], [(0, slice(0, 0))], [(1, slice(0, 4))]]]
    +    assert result == expected

    Alternatively, rechunk could filter out 0 chunks, but that seems unwise.

  7. self-assigned this
    on Mar 3, 2020
  8. martindurant commented on Apr 7, 2020

    @martindurant
    Member

    @jsignell , do you have any thoughts of a timeline on this, how much effort it would take and whether you will have that effort to spare?

  9. removed their assignment
    on Apr 8, 2020
  10. jsignell commented on Apr 8, 2020

    @jsignell
    Member

    Thanks for the ping @martindurant. I don't really have an estimate for this. If no one has gotten to it in a week or so I'll probably have more time then to take another look.

  11. heerad commented on Oct 5, 2020

    @heerad

    Hi, just wanted to note that I'm running into a similar error:

    "UFuncTypeError: ufunc 'multiply' did not contain a loop with signature matching types (dtype('<U46'), dtype('<U46')) -> dtype('<U46')"

    Note that my error involves the ufunc multiply rather than add by OP @TAdeJong. I'm not explicitly calling da.diagonal anywhere in my code, and am rather using dask implicitly via xarray.

  12. TAdeJong commented on Feb 9, 2022

    @TAdeJong
    ContributorAuthor

    Stumbled back upon this via #8689. As of 2022.01.1+20.gad3b688f, the bug is still there, although the error is different now:

    KeyError: ('rechunk-merge-e63040be2c9c9793b8231bdea30520b8', 1, 0)

    Details
    >>> a = da.ones((8, 8), chunks=(4, 4))
    >>> result = a.rechunk(((4, 4), (4, 0, 0, 4)))
    >>> result.compute()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/tobias/code/dask-dev/dask2/dask/dask/base.py", line 288, in compute
        (result,) = compute(self, traverse=False, **kwargs)
      File "/home/tobias/code/dask-dev/dask2/dask/dask/base.py", line 571, in compute
        results = schedule(dsk, keys, **kwargs)
      File "/home/tobias/code/dask-dev/dask2/dask/dask/threaded.py", line 79, in get
        results = get_async(
      File "/home/tobias/code/dask-dev/dask2/dask/dask/local.py", line 521, in get_async
        return nested_get(result, state["cache"])
      File "/home/tobias/code/dask-dev/dask2/dask/dask/local.py", line 298, in nested_get
        return tuple(nested_get(i, coll) for i in ind)
      File "/home/tobias/code/dask-dev/dask2/dask/dask/local.py", line 298, in <genexpr>
        return tuple(nested_get(i, coll) for i in ind)
      File "/home/tobias/code/dask-dev/dask2/dask/dask/local.py", line 298, in nested_get
        return tuple(nested_get(i, coll) for i in ind)
      File "/home/tobias/code/dask-dev/dask2/dask/dask/local.py", line 298, in <genexpr>
        return tuple(nested_get(i, coll) for i in ind)
      File "/home/tobias/code/dask-dev/dask2/dask/dask/local.py", line 298, in nested_get
        return tuple(nested_get(i, coll) for i in ind)
      File "/home/tobias/code/dask-dev/dask2/dask/dask/local.py", line 298, in <genexpr>
        return tuple(nested_get(i, coll) for i in ind)
      File "/home/tobias/code/dask-dev/dask2/dask/dask/local.py", line 300, in nested_get
        return coll[ind]
    KeyError: ('rechunk-merge-e63040be2c9c9793b8231bdea30520b8', 1, 0)
    >>> dask.__version__
    '2022.01.1+20.gad3b688f'
    
  13. ParticularMiner commented on Feb 9, 2022

    @ParticularMiner
    Contributor

    Hi @TAdeJong ,

    I think the problem here is that the chunks attribute of diagonal()'s output 1D-dask array has a mixture of chunks of zero and nonzero size, something that dask.array seems to be allergic to (as I've come to learn from experience).

    The following code extract from diagonal():

    dask/dask/array/creation.py

    Lines 656 to 675 in 510bbc3

    dsk = {}
    idx_set = set(range(a.ndim)) - {axis1, axis2}
    n1 = len(a.chunks[axis1])
    n2 = len(a.chunks[axis2])
    for idx in product(*(range(len(a.chunks[i])) for i in idx_set)):
    for i, (i1, i2) in enumerate(product(range(n1), range(n2))):
    tsk = reduce(getitem, idx[:axis1], a.__dask_keys__())[i1]
    tsk = reduce(getitem, idx[axis1 : axis2 - 1], tsk)[i2]
    tsk = reduce(getitem, idx[axis2 - 1 :], tsk)
    k = chunk_offsets[i1][i2]
    dsk[(name,) + idx + (i,)] = (np.diagonal, tsk, k, axis1, axis2)
    left_shape = tuple(a.shape[i] for i in idx_set)
    right_shape = (_diag_len(a.shape[axis1], a.shape[axis2], offset),)
    shape = left_shape + right_shape
    left_chunks = tuple(a.chunks[i] for i in idx_set)
    right_shape = (tuple(diag_chunks),)
    chunks = left_chunks + right_shape

    could be changed in the following way to make your code snippet above run successfully:

        dsk = {}
        j = 0
        idx_set = set(range(a.ndim)) - {axis1, axis2}
        n1 = len(a.chunks[axis1])
        n2 = len(a.chunks[axis2])
        for idx in product(*(range(len(a.chunks[i])) for i in idx_set)):
            for i, (i1, i2) in enumerate(product(range(n1), range(n2))):
                tsk = reduce(getitem, idx[:axis1], a.__dask_keys__())[i1]
                tsk = reduce(getitem, idx[axis1 : axis2 - 1], tsk)[i2]
                tsk = reduce(getitem, idx[axis2 - 1 :], tsk)
                k = chunk_offsets[i1][i2]
                if len(idx) == 0 and diag_chunks[i] > 0:
                    dsk[(name,) + idx + (j,)] = (np.diagonal, tsk, k, axis1, axis2)
                    j += 1
                else:
                    dsk[(name,) + idx + (i,)] = (np.diagonal, tsk, k, axis1, axis2)
    
        left_shape = tuple(a.shape[i] for i in idx_set)
        right_shape = (_diag_len(a.shape[axis1], a.shape[axis2], offset),)
        shape = left_shape + right_shape
    
        left_chunks = tuple(a.chunks[i] for i in idx_set)
        right_shape = (tuple(d for d in diag_chunks if d > 0 or len(idx_set) != 0),)
        chunks = left_chunks + right_shape
  14. TAdeJong commented on Feb 9, 2022

    @TAdeJong
    ContributorAuthor

    Heey @ParticularMiner,

    Thanks for looking into this. It seems you are right about the zero and nonzero chunks, and now finally @TomAugspurger's comments here make sense to me, (and make my rereporting of this of limited use, I'm sorry) :-)

    I guess we could fix diagonal() in the way you suggest, but that would essentially only be symptom relief and I guess it would be preferable to really handle the zero size chunks better or at least try to throw a sensible warning.

    I for certain do not have the overview to make decisions here. Also I do not know how far @jsignell got at the time with resolving this.

  15. ParticularMiner commented on Feb 9, 2022

    @ParticularMiner
    Contributor

    @TAdeJong

    I guess it would be preferable to really handle the zero size chunks better or at least try to throw a sensible warning

    I completely agree.

    As a side note, I've just discovered that my "fix" above allows your code snippet to pass, but not the main test_diagonal() in test_creation.py. So there is something else going on here.

  16. ParticularMiner commented on Feb 9, 2022

    @ParticularMiner
    Contributor

    @TAdeJong

    It seems the problem only occurs when the input array has exactly two dimensions. Really weird. So I've amended the "fix" above (see the edit). This will cause both the main test_diagonal() and your code snippet to run successfully.

    But like you said before, this can only be a tentative patch-up solution awaiting a more comprehensive one.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions