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
Following #6846 the memory usage of tensordot (and dot, which delegates to tensordot in Dask) is much higher than before, and grows as a function of array size.
What you expected to happen:
The memory usage should be related to chunk size (as it was previously), not the size of the array. This was achieved previously by avoiding concatenate=True in the call to blockwise from tensordot. As a general rule, concatenate=True should be avoided since it causes these memory issues for very large inputs.
Minimal Complete Verifiable Example:
The "Multiplication Only" part of this notebook - with X.T @ Y replaced with da.dot(X.T, Y) - demonstrates the problem. The memory usage should be flat, not growing with the size of the array.
Anything else we need to know?:
#6874 is a related issue that aims to remove the same memory issue from matmul.
Note that there seems to have been a hidden bug in dask's tensordot for a long time that was coincidentally resolved by #6846. For a few more details see #6907.
We could have a special case for sparse array (based on type) that introduces the contraction in _tensordot only for sparse arrays? But I wonder if there is a more generic solution 🤔
Afaiu #6924 also adds a special case but just for {1,2}d. Should I give it a try to produce a PR for the sparse array special case, to compare? Wdyt @tomwhite and the rest?
r1 is wrong and r2 is correct (with a dask version prior to fbccc4e). This is not due to special values; when I take the problematic values and create an array from a numpy array with them, the problem does not occur. The problem is also not with the (tensor)dot implementation of the blocks, but rather that they get fed wrong blocks. But I was not able yet to track down why and therefore I am not sure how to set up the test. I think having random stuff in tests should rather be avoided.
What do you think? Include a test with random as above? Track down the problem in more detail before writing the test?
Sorry I didn't mention this here before, but in the full script I had a dask.config.set(scheduler='single-threaded') at the beginning, so the error does occur with the single-threaded scheduler. (I now edited the snippet above to include the scheduler change.)
@GenevieveBuckley and I ran into this today. My apologies for only understanding what had happened until now.
Short term, we can revert the previous change. I think that tensordot on numpy is more important than tensordot on *sparse.
A solution that it a little better would be to look at the _meta of the input arrays and special casing those with sparse in the type name.
However a best solution would be to find a way to use the sum trick with sparse arrays. I think that this trick is necessary for memory efficient operation at large scale.
I think that @GenevieveBuckley is taking a look at this now. Genevieve, I recommending spending an hour or two to see if you can respect the current concatenate choice for sparse arrays, and if not then just revert the previous commit.
Yes, I am looking at it (well, eating lunch and then back to it). What you've commented above is what I had in mind. I think it should be relatively straightforward to keep the current situation with sparse arrays, and use sum for other array types. Once that's done we can look at a better long term plan using the sum trick for sparse arrays too.
Activity
zklaus commentedon Dec 1, 2020
Note that there seems to have been a hidden bug in dask's tensordot for a long time that was coincidentally resolved by #6846. For a few more details see #6907.
jakirkham commentedon Dec 3, 2020
Good to know. Maybe we should include a test then? Would you be willing to submit a PR? 🙂
ravwojdyla commentedon Dec 3, 2020
From #6846 (comment):
Afaiu #6924 also adds a special case but just for {1,2}d. Should I give it a try to produce a PR for the sparse array special case, to compare? Wdyt @tomwhite and the rest?
zklaus commentedon Dec 4, 2020
I would. The problem is that I am not completely sure what is going on. In
r1is wrong andr2is correct (with a dask version prior to fbccc4e). This is not due to special values; when I take the problematic values and create an array from a numpy array with them, the problem does not occur. The problem is also not with the (tensor)dot implementation of the blocks, but rather that they get fed wrong blocks. But I was not able yet to track down why and therefore I am not sure how to set up the test. I think havingrandomstuff in tests should rather be avoided.What do you think? Include a test with random as above? Track down the problem in more detail before writing the test?
mrocklin commentedon Dec 4, 2020
Can I ask folks to try this with the single-threaded scheduler
.compute(scheduler="single-threaded")If it's fine there then I suspect that this is the numpy/glibc threaded leak issue.
zklaus commentedon Dec 4, 2020
Sorry I didn't mention this here before, but in the full script I had a
dask.config.set(scheduler='single-threaded')at the beginning, so the error does occur with the single-threaded scheduler. (I now edited the snippet above to include the scheduler change.)mrocklin commentedon Aug 3, 2021
@GenevieveBuckley and I ran into this today. My apologies for only understanding what had happened until now.
Short term, we can revert the previous change. I think that tensordot on numpy is more important than tensordot on *sparse.
A solution that it a little better would be to look at the
_metaof the input arrays and special casing those with sparse in the type name.However a best solution would be to find a way to use the
sumtrick with sparse arrays. I think that this trick is necessary for memory efficient operation at large scale.mrocklin commentedon Aug 3, 2021
I think that @GenevieveBuckley is taking a look at this now. Genevieve, I recommending spending an hour or two to see if you can respect the current concatenate choice for sparse arrays, and if not then just revert the previous commit.
GenevieveBuckley commentedon Aug 3, 2021
Yes, I am looking at it (well, eating lunch and then back to it). What you've commented above is what I had in mind. I think it should be relatively straightforward to keep the current situation with sparse arrays, and use sum for other array types. Once that's done we can look at a better long term plan using the sum trick for sparse arrays too.
Quick fix for unbounded memory usage in tensordot (#7980)
dcherian commentedon Mar 24, 2022
I think this can be closed now.