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
{{ message }}
Repository navigation
Use of da.where before da.einsum on a chunked array produces incorrectly sized result #8131
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:
importnumpyasnpimportdask.arrayasdaa=da.asarray(
[
[1. , 0. ],
[0. , 1. ],
[0.5, 0.5],
],
chunks=((2,1), (2,))
)
b=da.where(a==-1, a, a)
asserta.chunks==b.chunksnp.testing.assert_array_equal(a, b)
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.
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:
importnumpyasnpimportdask.arrayasdaa=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)
asserta.chunks==b.chunksnp.testing.assert_array_equal(a, b)
da.einsum('xj,yj->xy', a, a).compute() -da.einsum('xj,yj->xy', b, b).compute()
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)
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.
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)
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.
Activity
jsignell commentedon Sep 10, 2021
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:
dask/dask/array/einsumfuncs.py
Lines 247 to 249 in e1974bf
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
sumthan witheinsum. 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.
timothymillar commentedon Sep 13, 2021
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:
chunks=((3,3), (2,))I get the correct answer.chunks=((4,1,1), (2,))einsum produces the wrong shape (12,12) for arraybchunks=((4,2), (2,))einsum on arraybraisesValueError: operands could not be broadcast together with shapes (2,2) (4,2) (4,2)timothymillar commentedon Sep 13, 2021
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) andchunks=(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
ValueErrorrelated to broadcasting.GenevieveBuckley commentedon Sep 16, 2021
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.
GenevieveBuckley commentedon Sep 16, 2021
Drilling a little deeper into which specific line shows the problem, this blockwise call in the reduction looks problematic:
dask/dask/array/reductions.py
Lines 158 to 160 in 478a794
If you pop a breakpoint in there,
x.compute()works fine buttmp.compute()produces a broadcast error: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)
jcrist commentedon Mar 1, 2022
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:
The issue seems to be resolved, closing.
timothymillar commentedon Apr 8, 2022
Just for reference, the fix appears to be in dask
2022.01.0Edit: 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.