Skip to content

Use of da.where before da.einsum on a chunked array produces incorrectly sized result #8131

Closed

Description

@timothymillar

What happened:

Using dask.array.where prior to dask.array.einsum on some chunked arrays results in an incorrectly sized array. This seems to only occur if the array is chunked in the dimensions that are retained by einsum. Using einsum without first using 'where' produces the correct result. Note that this occurs even when 'where' does not change any values within the array.

What you expected to happen:

Use of dask.array.where shouldn't affect the result shape of dask.array.einsum.

Minimal Complete Verifiable Example:

import numpy as np
import dask.array as da
a = da.asarray(
    [
        [1. , 0. ],
        [0. , 1. ],
        [0.5, 0.5],
    ], 
    chunks=((2,1), (2,))
)
b = da.where(a == -1, a, a)
assert a.chunks == b.chunks
np.testing.assert_array_equal(a, b)
da.einsum('xj,yj->xy', a, a).compute()
array([[1. , 0. , 0.5],
       [0. , 1. , 0.5],
       [0.5, 0.5, 0.5]])
da.einsum('xj,yj->xy', b, b).compute()
array([[1. , 0. , 1. , 0. ],
       [0. , 1. , 0. , 1. ],
       [0.5, 0.5, 0.5, 0.5],
       [0.5, 0.5, 0.5, 0.5]])

Environment:

  • Dask version: 2021.9.0 (pypi)
  • Python version: 3.9.6 (conda)
  • Operating System: Ubuntu 21.04
  • Install method: pip within conda env

Activity

  1. jsignell commented on Sep 10, 2021

    @jsignell
    Member

    Thanks for taking the time to write this up. I can reproduce, but I'm kind of stumped. I went through line by line and it seems like it's this sum:

    return result.sum(
    axis=list(range(size, size + ncontract_inds)), split_every=split_every
    )

    The input array seems right, but after the sum the dask array has the right shape, but when you call compute on it the numpy array has the wrong shape. So I'm a little worried that this has to do more with sum than with einsum. I'm still trying to test that theory though.

    @rjzamora in case you have any thoughts since this is kind or related to blockwise work.

  2. timothymillar commented on Sep 13, 2021

    @timothymillar
    Author

    Thanks for looking into it.
    Playing around with this shows it's not always a shape error, sometimes the shape is correct but the values are wrong and sometimes dask raises a ValueError:

    E.g. wrong values are returned:

    import numpy as np
    import dask.array as da
    a = da.asarray(
        [
            [1. , 0. ],
            [0. , 1. ],
            [0.5, 0.5],
            [1. , 0. ],
            [0. , 1. ],
            [0.5, 0.5],
        ], 
        chunks=((2,2,2), (2,)) 
    )
    b = da.where(a == -1, a, a)
    assert a.chunks == b.chunks
    np.testing.assert_array_equal(a, b)
    da.einsum('xj,yj->xy', a, a).compute() - da.einsum('xj,yj->xy', b, b).compute()
    array([[1. , 0. , 1. , 0. , 1. , 0. ],
           [0. , 1. , 0. , 1. , 0. , 1. ],
           [0.5, 0.5, 0.5, 0.5, 0.5, 0.5],
           [0.5, 1. , 0.5, 1. , 0.5, 1. ],
           [1. , 0.5, 1. , 0.5, 1. , 0.5],
           [0.5, 0.5, 0.5, 0.5, 0.5, 0.5]])
    
    • If I re-run this example with chunks=((3,3), (2,)) I get the correct answer.
    • If I re-run this example with chunks=((4,1,1), (2,)) einsum produces the wrong shape (12,12) for array b
    • If I re-run this example with chunks=((4,2), (2,)) einsum on array b raises ValueError: operands could not be broadcast together with shapes (2,2) (4,2) (4,2)
  3. timothymillar commented on Sep 13, 2021

    @timothymillar
    Author

    Ah, the shape errors seem to be a result of broadcasting. Any (non-leading) 1s are getting treated as the leading value. So chunks=(2,1,1,1,1) results in an array of shape (10, 10) and chunks=(2,2,1,1) results in an array of shape (8, 8).

    If there is a mixture of chunk sizes other than 1s (or if the first value is 1) then you get the ValueError related to broadcasting.

  4. GenevieveBuckley commented on Sep 16, 2021

    @GenevieveBuckley
    Contributor

    Just want to draw your attention to this change Klaus made in order to properly calculate array shapes using blockwise

    I suspect what needs to happen with broadcasting here might be different, but I wanted to link it in case anyone does need to do something similar.

  5. GenevieveBuckley commented on Sep 16, 2021

    @GenevieveBuckley
    Contributor

    Drilling a little deeper into which specific line shows the problem, this blockwise call in the reduction looks problematic:

    tmp = blockwise(
    chunk, inds, x, inds, axis=axis, keepdims=True, token=name, dtype=dtype or float
    )

    If you pop a breakpoint in there, x.compute() works fine but tmp.compute() produces a broadcast error:

    ipdb>  blockwise(np.sum, inds, x, inds, axis=axis, keepdims=True, token=name, dtype=dtype).compute()
    
    *** ValueError: operands could not be broadcast together with shapes (2,2) (4,2) (4,2)

    What's baffling to me is why it's only different when there's a where function somewhere in the Dask task graph. I don't see why that should affect the shapes for broadcasting (and as far as I can tell, it doesn't - all the shapes seem to look the same)

  6. jcrist commented on Mar 1, 2022

    @jcrist
    Member

    I'm not sure what PR fixed this (there have been a few touching this code recently), but the test example passes on main now:

    import numpy as np
    import dask.array as da
    a = da.asarray(
        [
            [1. , 0. ],
            [0. , 1. ],
            [0.5, 0.5],
        ], 
        chunks=((2,1), (2,))
    )
    b = da.where(a == -1, a, a)
    assert a.chunks == b.chunks
    np.testing.assert_array_equal(a, b)

    The issue seems to be resolved, closing.

  7. timothymillar commented on Apr 8, 2022

    @timothymillar
    Author

    Just for reference, the fix appears to be in dask 2022.01.0

    Edit: digging a little deeper it was fixed in #8542 and seems to have been a symptom of duplicate dependency names in rewrite_blockwise, though I can't quite grok the details.

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