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
Large memory increase and processing slowness during graph creation #7851
When creating a graph consisting of delayed dataframes created from 20k image blocks, the memory climbs into 10+ gigabytes and it takes several minutes to create. No execution of graph is done. I experimented with fewer blocks, and the performance is quite non-linear. The processing time and memory taken up (measured via GKE jupyterhub pod memory usage) by number of blocks:
I expected 20k blocks this to fit under a minute and under a gigabyte. I also expected linear performance with number of blocks, as the blocks are independent (as can be verified by .visualize(..) on a single dataframe)
Minimal Complete Verifiable Example:
Scaled down example with 8k blocks, which is enough to demonstrate the memory growth. My actual dataset has 20k blocks, roughly 3TB divided into 150MB blocks -- but the problem seems to be purely a function of block count, not datasize.
@rikturr Thanks for pointing out the issue. In my case there is no scheduler involved -- I was running into OOM Kills on my client job during graph construction. I now give my client jobs 45GB of memory per core!
@GenevieveBuckley Given your expertise in HighLevelGraphs and your interest in alternatives when creating non-arrays from chunked arrays, I thought this would be of interest. Do you have any tips in how we might avoid this behavior when returning delayed dataframes?
Similar code that returns delayed arrays does not have this problem. I suspect something can be done in dd.from_delayed with the merge, but I'm not familiar with what is happening here.
My first suggestion was going to be to remove the delayed within a delayed, since that's something recommended against in the docs (you have image.to_delayed() passed into another delayed function later on). But when I change that in a notebook I still see an exponential increase in time taken to construct the graph, so that can't be the whole story.
I don't see the same memory climb as you do (I'm running Ubuntu 20.04). Watching the system monitor, the memory and swap history stays pretty consistent, although during graph creation I do see a single CPU thread max itself out for a bit, then drop back to zero while another CPU thread maxes out for a bit, etc.
It's definitely the from_delayed part that's causing the trouble. I'd try moving that out of the loop.
When I do this the memory leak is observable. If the chunk size is changed to 1000 instead of 1 it runs (there are many fewer chunks overall in that scenario)
importoperatorimportfunctoolsimportdaskimportdask.arrayasdaimportdask.bagasdbimportdask.dataframeasddimportnumpyasnpimportpandasaspdimage=da.zeros(8000, dtype=np.uint16, chunks=1)
block_iter=zip(
np.ndindex(*image.numblocks),
map(functools.partial(operator.getitem, image),
da.core.slices_from_chunks(image.chunks))
)
ddf_all=np.empty(image.numblocks, dtype=object)
foridx_chunk, chunkinblock_iter:
ddf_delayed=dask.delayed(lambdax: pd.DataFrame(np.random.random(5)))(chunk)
ddf_all[idx_chunk] =ddf_delayedbag=db.from_sequence(ddf_all)
result=bag.map(dd.from_delayed, meta=[(0, np.float64)])
# things seem ok until now...# memory leak observableforiinresult:
print(i)
Thanks for helping out. I see what you are saying about the recommendation of not using delayed-in-delayed, though I'm not sure if the problem I'm seeing is related to that recommendation (though it may be).
I have this pattern (of creating dataframes from chunks) several places in my codebase, and most times I do defer the from_delayed until outside the loop. In those cases, I want the dataframes concatenated. But in this case I do actually need each dataframe separate and stored in the array structure, as each one is rejoined later to chunks laid out in the same pattern. (It's actually more complex that than -- the array is 5 dimensional, and the frames along one dimension of the array are concatenated before the rejoin)
The final graph here is very simple (it is separable by chunk, and each chunk's subgraph is just a few tasks), so I believe this is something that should be possible without so much time/memory. My understanding of the code is that there is a quadratic effect where each chunk carries information about all other chunks -- and the HLG merging seems to be doing a double loop while looking up keys.
If it's not obvious what the problem may be, I can look and see if my code can be restructured. The restructuring would be unfortunate, as it may make the APIs a bit messy, but it would be worth it for performance. I just don't know if that restructuring might have similar performance issues.
Thanks for pointing out those issues, @jsignell . It looks like #7922 is related to arrays iterating via blocks, which is similar to this, but here the iterate is via delayed (or in following @GenevieveBuckley , using numpy slicing). And #8042 is requesting some sort of new functionality in computation, while this issue is around graph construction only -- not computation.
My investigation has led me to realize that a block of an array seems to have a pointer back to the high-level-graph of the original array, which has all the keys for all the chunks. When the dd.from_delayed happens on a chunk, it is trying to merge the HLGs. This leads to a loop over all those underlying keys. Because this is done for each chunk, this leads to an N^2 behavior -- hence you see some functions being called 10s or 100s of millions of times in the profiles.
The HLG getitem is falling down to the O(n) lookup because the keys have a "delayed-" prefix, whereas the layer keys do not have this -- it has to go into the values themselves to find the "delayed-" prefixed items.
I think this requires someone with knowledge of delayed (in particular, dataframes) and HLGs, and how they should be interacting.
Ah that makes sense, every from_delayed probably had to materialize and merge the full un-culled graph of the array. Nice! I had no idea I was fixing this.
Activity
rikturr commentedon Jul 2, 2021
I think this is the same thing happening in #7860
chrisroat commentedon Jul 2, 2021
@rikturr Thanks for pointing out the issue. In my case there is no scheduler involved -- I was running into OOM Kills on my client job during graph construction. I now give my client jobs 45GB of memory per core!
chrisroat commentedon Aug 13, 2021
I verified this is still true in 2021.7.2.
There are an enormous number of calls to highlevelgraph getitem, and I suspect it may be falling back to the O(n) branch. From
prun:And from
pyinstrument.chrisroat commentedon Aug 14, 2021
@GenevieveBuckley Given your expertise in HighLevelGraphs and your interest in alternatives when creating non-arrays from chunked arrays, I thought this would be of interest. Do you have any tips in how we might avoid this behavior when returning delayed dataframes?
Similar code that returns delayed arrays does not have this problem. I suspect something can be done in
dd.from_delayedwith the merge, but I'm not familiar with what is happening here.GenevieveBuckley commentedon Aug 16, 2021
My first suggestion was going to be to remove the delayed within a delayed, since that's something recommended against in the docs (you have
image.to_delayed()passed into another delayed function later on). But when I change that in a notebook I still see an exponential increase in time taken to construct the graph, so that can't be the whole story.I don't see the same memory climb as you do (I'm running Ubuntu 20.04). Watching the system monitor, the memory and swap history stays pretty consistent, although during graph creation I do see a single CPU thread max itself out for a bit, then drop back to zero while another CPU thread maxes out for a bit, etc.
It's definitely the
from_delayedpart that's causing the trouble. I'd try moving that out of the loop.GenevieveBuckley commentedon Aug 16, 2021
@chrisroat take a look at the way I've structured https://github.com/dask/dask-image/pull/240/files and see if that helps you here (less pleasantly, perhaps you can tell us if we'll run into problems with large numbers of chunks with that PR too)
GenevieveBuckley commentedon Aug 16, 2021
When I do this the memory leak is observable. If the chunk size is changed to 1000 instead of 1 it runs (there are many fewer chunks overall in that scenario)
chrisroat commentedon Aug 16, 2021
Thanks for helping out. I see what you are saying about the recommendation of not using delayed-in-delayed, though I'm not sure if the problem I'm seeing is related to that recommendation (though it may be).
I have this pattern (of creating dataframes from chunks) several places in my codebase, and most times I do defer the from_delayed until outside the loop. In those cases, I want the dataframes concatenated. But in this case I do actually need each dataframe separate and stored in the array structure, as each one is rejoined later to chunks laid out in the same pattern. (It's actually more complex that than -- the array is 5 dimensional, and the frames along one dimension of the array are concatenated before the rejoin)
The final graph here is very simple (it is separable by chunk, and each chunk's subgraph is just a few tasks), so I believe this is something that should be possible without so much time/memory. My understanding of the code is that there is a quadratic effect where each chunk carries information about all other chunks -- and the HLG merging seems to be doing a double loop while looking up keys.
If it's not obvious what the problem may be, I can look and see if my code can be restructured. The restructuring would be unfortunate, as it may make the APIs a bit messy, but it would be worth it for performance. I just don't know if that restructuring might have similar performance issues.
GenevieveBuckley commentedon Aug 16, 2021
Maintainers, do we know who our experts are on dask delayed? (I don't think there's a project team list I can ping here)
jsignell commentedon Aug 19, 2021
Hmm, not sure if there is a specific expert, but there have been some roughly related issues. Specifically #7922 and #8042
chrisroat commentedon Aug 20, 2021
Thanks for pointing out those issues, @jsignell . It looks like #7922 is related to arrays iterating via blocks, which is similar to this, but here the iterate is via delayed (or in following @GenevieveBuckley , using numpy slicing). And #8042 is requesting some sort of new functionality in computation, while this issue is around graph construction only -- not computation.
My investigation has led me to realize that a block of an array seems to have a pointer back to the high-level-graph of the original array, which has all the keys for all the chunks. When the
dd.from_delayedhappens on a chunk, it is trying to merge the HLGs. This leads to a loop over all those underlying keys. Because this is done for each chunk, this leads to an N^2 behavior -- hence you see some functions being called 10s or 100s of millions of times in the profiles.The HLG getitem is falling down to the O(n) lookup because the keys have a "delayed-" prefix, whereas the layer keys do not have this -- it has to go into the values themselves to find the "delayed-" prefixed items.
I think this requires someone with knowledge of delayed (in particular, dataframes) and HLGs, and how they should be interacting.
chrisroat commentedon Sep 27, 2021
The fix by #8174 by @gjoseph92 fixed this issue. In my testing, the 8000 block reproducer finished in <8s with minimal memory usage.
gjoseph92 commentedon Sep 27, 2021
Ah that makes sense, every
from_delayedprobably had to materialize and merge the full un-culled graph of the array. Nice! I had no idea I was fixing this.