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
Incorrect maths with tensordot for sparse arrays constructed with map_blocks #6907
I'm not sure if this is a problem with dask or with sparse, though the fact that the behaviour changes when constructing the array with map_blocks vs converting a sparse array to dask makes me think that dask may be involved. Environment:
This might not even be related to sparse. In the following snippet, the same problem appears. At least I think it is the same problem. Applying persist to the input makes it work.
Produced wrong results for many releases of dask (I tested 2.30.0, 2.20.0, 2.10.0, 2.0.0, and 1.0.0) this seems to have been fixed with fbccc4e .
Seeing as this is a rather serious bug, silently producing wrong results for da.dot and da.tensordot under certain conditions, I wonder if a bug-fix release might be in order?
I suspect that since the bug has been around for so long, this isn't something that people run into very often. But I'll raise it with @dask/maintenance to see if this should be a candidate for a bug fix release. If not, then maybe a time estimate for the next release would be helpful.
#!/usr/bin/env python# coding: utf-8importnumpyasnpimportdaskimportdask.arrayasdadask.config.set(scheduler='single-threaded')
numpy_data=np.random.random((4,4))
numpy_data[numpy_data<0.65] =0# if this statement happens in numpy, there is no difference whether we persist the dask input array or not before passing it to tensordotx=da.random.random((4,4), chunks=(2,2))
y=x.persist()
z=x.compute()
r1=da.dot(x, x).compute()
r2=da.dot(y, y).compute()
rr=np.dot(z, z)
assertnp.allclose(rr, r1)
assertnp.allclose(rr, r2)
This doesn't work
#!/usr/bin/env python# coding: utf-8importnumpyasnpimportdaskimportdask.arrayasdadask.config.set(scheduler='single-threaded')
x=da.random.random((4,4), chunks=(2,2))
x[x<0.65] =0# if this statement happens in dask, then we get different results from tensordot depending on whether we persist the dask input array or noty=x.persist()
z=x.compute()
r1=da.dot(x, x).compute()
r2=da.dot(y, y).compute()
rr=np.dot(z, z)
assertnp.allclose(rr, r1)
assertnp.allclose(rr, r2)
Why do we care?
We want to fix the memory problems with tensordot reported here #6916. It seemed like the simplest path for a quick fix would be to revert the change which introduced the memory bottleneck... but that's the same change that inadvertantly fixed this issue. So, I want to solve both problems at once.
I've narrowed it down the circumstances in which this occurs a bit further (although I still don't know the reason it happens).
I can reproduce this if:
The two input arrays have the same numeric values in all locations (note: they don't have to be the same dask array, they just need to contain the same numeric values), AND...
There is a conditional "where" done on the same value/location(s), AND...
...something very specific about the way the chunks are aligned/arranged - I haven't worked this bit out yet
importdaskimportdask.arrayasdaimportnumpyasnpdask.config.set(scheduler='single-threaded')
x=da.from_array(np.arange(16).reshape((4, 4)), chunks=(2,2))
x[x==0] =0x2=da.from_array(np.arange(16).reshape((4, 4)), chunks=(2,2))
x2[x2==0] =0# change this line to "x2[x2 == 1] = 1" and the bug is not triggeredy=x.persist()
y2=x2.persist()
z=x.compute()
z2=x2.compute()
r1=da.dot(x, x2)
r2=da.dot(y, y2)
rr=np.dot(z, z2)
assertnp.allclose(rr, r1.compute()) # failsassertnp.allclose(rr, r2.compute())
I thought maybe it might have something to do with the insert trick used in _tensordot to expand the dimensions, followed by the sum reduction, but perhaps not. I haven't been able to make a simpler reproducible example.
Investigating the intermediate named arrays for both situations in :
The values in the arrays are the same
The chunk sizes/shapes don't match, according to da.assert_eq. But I don't fully understand why that's happening, because both the shape & chunksize in the array HTML reprs match, and when you iterate through the array blocks one by one they also look like they match.
I redid my experiment with the Dask get entry point, after my conversation with Peter. The results were the same as last time, but I thought it's worthwhile sharing here regardless.
If you .compute() the input arrays, they appear correct.
All of the intermediate task results look identical.
...but if we .compute() the final result there's a difference 😞
There are a couple of extra keys for tasks in the array that has the where statement, but they all come at the very beginning so I think we should at least see some errors being propagated through the steps after them, but we don't until the very final compute.
Activity
zklaus commentedon Nov 30, 2020
This might not even be related to sparse. In the following snippet, the same problem appears. At least I think it is the same problem. Applying
persistto the input makes it work.jsignell commentedon Dec 1, 2020
I can't reproduce either of these on latest master:
Please reopen if you update to latest master and are able to reproduce.
zklaus commentedon Dec 1, 2020
This does seem to be fixed in master (more on that below). However, the following simple program
Produced wrong results for many releases of dask (I tested 2.30.0, 2.20.0, 2.10.0, 2.0.0, and 1.0.0) this seems to have been fixed with fbccc4e .
Seeing as this is a rather serious bug, silently producing wrong results for
da.dotandda.tensordotunder certain conditions, I wonder if a bug-fix release might be in order?jsignell commentedon Dec 1, 2020
I suspect that since the bug has been around for so long, this isn't something that people run into very often. But I'll raise it with @dask/maintenance to see if this should be a candidate for a bug fix release. If not, then maybe a time estimate for the next release would be helpful.
martindurant commentedon Dec 1, 2020
We ought to be nearing a full normal release, I believe. It would be good to have that timeline, I agree.
GenevieveBuckley commentedon Aug 5, 2021
I can narrow down on the weirdness a bit further.
or...
This works
This doesn't work
Why do we care?
We want to fix the memory problems with tensordot reported here #6916. It seemed like the simplest path for a quick fix would be to revert the change which introduced the memory bottleneck... but that's the same change that inadvertantly fixed this issue. So, I want to solve both problems at once.
GenevieveBuckley commentedon Aug 18, 2021
Stuff I did:
getentry point (link to docs) to examine all intermediate task resultsI think this might have something to do with how arrays/chunks get hashed. I'll need to go look at how exactly that part is done.(perhaps not?)GenevieveBuckley commentedon Aug 18, 2021
I've narrowed it down the circumstances in which this occurs a bit further (although I still don't know the reason it happens).
I can reproduce this if:
GenevieveBuckley commentedon Aug 18, 2021
More investigation into the chunks reveals:
GenevieveBuckley commentedon Aug 18, 2021
I thought maybe it might have something to do with the insert trick used in
_tensordotto expand the dimensions, followed by the sum reduction, but perhaps not. I haven't been able to make a simpler reproducible example.Investigating the
intermediatenamed arrays for both situations in :da.assert_eq. But I don't fully understand why that's happening, because both the shape & chunksize in the array HTML reprs match, and when you iterate through the array blocks one by one they also look like they match.GenevieveBuckley commentedon Sep 9, 2021
I redid my experiment with the Dask get entry point, after my conversation with Peter. The results were the same as last time, but I thought it's worthwhile sharing here regardless.
.compute()the input arrays, they appear correct..compute()the final result there's a difference 😞There are a couple of extra keys for tasks in the array that has the where statement, but they all come at the very beginning so I think we should at least see some errors being propagated through the steps after them, but we don't until the very final compute.
jsignell commentedon Sep 10, 2021
Having just looked at #8131 this is looking very similar. @GenevieveBuckley do you think they could be related?
GenevieveBuckley commentedon Sep 14, 2021
Hm, maybe! Thanks for highlighting that here, Julia