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
Poor scheduling with flox, leading to high memory usage and eventual failure #11026
importdask.distributedasddimportdask.arrayasdaimportnumpyasnpimportfloxcluster=dd.LocalCluster(n_workers=3)
client=dd.Client(cluster)
M, N=1_000_000, 20_000# Note that it doesn't seem to matter where X comes from.# In my real-world use case, it is either generated from `map_blocks` or # read directly from a zarr storeX=da.random.normal(size=(M, N), chunks=(5_000, N))
by=np.random.choice(2_000, size=M)
res, codes=flox.groupby_reduce(
X.T,
by,
func="sum",
fill_value=0,
method="map-reduce",
reindex=True,
)
res_comp=res.compute()
Anything else we need to know?:
As an example, here's how my task graph completes if I run `X.sum(axis=0).compute()` Whereas `res.compute()` ends up with something more like:
Environment:
Dask version: '2024.3.1'
Python version: Python 3.11.7 | packaged by conda-forge | (main, Dec 23 2023, 14:43:09) [GCC 12.3.0]
Operating System: ubuntu (but can replicate on mac)
Install method (conda, pip, source): pip (in conda env)
I only very briefly looked into this so far but it looks like the very first groupby tasks generate very large partitions, pretty much regardless of how the initial input partitions look like
I get many, many of those 600MiB chunks and this is clogging the cluster. From what I can tell, scheduling is working as intended. The reason why this isn't moving along is because some of those chunks end up on different workers and have to be moved around using network, i.e. the final reducer tasks are delayed since the workers have to fetch that data. While that is being fetched, the worker is attempting to run other tasks.
I'm a little surprised to see these large intermediate results on a map-reduce workflow but I don't have sufficient knowledge about flox to judge this properly.
The input chunk sizes are large here, so that's fixable in theory. I did notice that no root tasks were identified in the dashboard. @ivirshup's comment suggest that this is happening with Zarr inputs as well, not just random. Do you see this too?
EDIT: Those chunk tasks are blockwise reduction tasks fused with the random data generation task.
I'm not familiar enough with dask to know what my expectations should be of the high level graph representations, but some stuff there does seem a little off.
My expectation would be that each groupby-sum chunk would be (N, N_CATEGORIES) with a total shape of (N, N_CATEGORIES * len(X.chunks[1]). Each partial aggregation layer after that reports chunk sizes to be (20000, 1) which also seems wrong. Not sure if this is related though.
Each partial aggregation layer after that reports chunk sizes to be (20000, 1) which also seems wrong.
Yes this is (intentionally) wrong.
Number of outputs is more indicative of the reduction tree. You start with 200 chunks -> 50 -> 13 -> 4 -> 1 (The default "split-size" for the tree reduction is 4, so at each stage 4 outputs get combined to 1).
I think Florian's image is a result of large initial chunk sizes. It's certaily matches the input chunk size.
I'll also note that number of categories ~ chunksize of input along the reduction dimension, so the blockwise reduction isn't too effective at reducing memory. It is doing a factor of 2 reduction in your latest example (5000->2000).
Note: You could try passing reindex=False, this would control memory at the expense of slower stages during the tree reduction. But make sure to do this for your actual data, it shouldn't show too much difference (on average) with purely random groups, but will definitely be slower.
Number of outputs is more indicative of the reduction tree. You start with 200 chunks -> 50 -> 13 -> 4 -> 1 (The default "split-size" for the tree reduction is 4, so at each stage 4 outputs get combined to 1).
That makes sense, but I guess I would have expected to have arrays that look like:
Why does the groupby_sum-chunk task output an array the same size as the input?
FWIW, I tried this with the threading scheduler and it seemed to work great with no memory issues. So definitely speeks to high cost of transfer.
I was under the impression that the scheduler would try to reduce the number of data transfers necessary. Is this explicitly done, or is this just expected to happen due to last-in-first-out task distribution?
It would require a little bit of manipulation. Most of the "real data" cases easily available I can think go off the top of my head are in a sparse format in either hdf5 or tiledb. I could share a link with some code to construct the Dask arrays?
I was able to get a little further today, and was able to get a few use-cases to actually run, but I am on a larger machine than I was using previously.
EDIT3: Memory issues can be controlled by using numbagg and nansum. It's also 5X faster (approx). I guess we should look in to how to make flox just handle sparse matrices directly.
Activity
fjetter commentedon Mar 28, 2024
I only very briefly looked into this so far but it looks like the very first groupby tasks generate very large partitions, pretty much regardless of how the initial input partitions look like
I get many, many of those 600MiB chunks and this is clogging the cluster. From what I can tell, scheduling is working as intended. The reason why this isn't moving along is because some of those chunks end up on different workers and have to be moved around using network, i.e. the final reducer tasks are delayed since the workers have to fetch that data. While that is being fetched, the worker is attempting to run other tasks.
I'm a little surprised to see these large intermediate results on a map-reduce workflow but I don't have sufficient knowledge about flox to judge this properly.
dcherian commentedon Mar 28, 2024
The input chunk sizes are large here, so that's fixable in theory. I did notice that no root tasks were identified in the dashboard. @ivirshup's comment suggest that this is happening with Zarr inputs as well, not just
random. Do you see this too?EDIT: Those
chunktasks areblockwisereduction tasks fused with therandomdata generation task.ivirshup commentedon Mar 28, 2024
@dcherian, adding an intermediate (computed) zarr store like this:
Has the same behaviour for me.
@fjetter, what is that task representation?
I'm not familiar enough with dask to know what my expectations should be of the high level graph representations, but some stuff there does seem a little off.
My expectation would be that each groupby-sum chunk would be
(N, N_CATEGORIES)with a total shape of(N, N_CATEGORIES * len(X.chunks[1]). Each partial aggregation layer after that reports chunk sizes to be(20000, 1)which also seems wrong. Not sure if this is related though.dcherian commentedon Mar 28, 2024
Yes this is (intentionally) wrong.
Number of outputs is more indicative of the reduction tree. You start with 200 chunks -> 50 -> 13 -> 4 -> 1 (The default "split-size" for the tree reduction is 4, so at each stage 4 outputs get combined to 1).
I think Florian's image is a result of large initial chunk sizes. It's certaily matches the input chunk size.
I'll also note that number of categories ~ chunksize of input along the reduction dimension, so the blockwise reduction isn't too effective at reducing memory. It is doing a factor of 2 reduction in your latest example (5000->2000).
Note: You could try passing
reindex=False, this would control memory at the expense of slower stages during the tree reduction. But make sure to do this for your actual data, it shouldn't show too much difference (on average) with purely random groups, but will definitely be slower.ivirshup commentedon Mar 29, 2024
That makes sense, but I guess I would have expected to have arrays that look like:
shape=(N_FEATURES, N_CATEGORIES, N_REDUCTIONS), chunks=(FEATURE_CHUNKS, N_CATEGORIES, 1)Instead of:
shape=(N_FEATURES, N_REDUCTIONS), chunks=(FEATURE_CHUNKS, 1)Why does the
groupby_sum-chunktask output an array the same size as the input?FWIW, I tried this with the threading scheduler and it seemed to work great with no memory issues. So definitely speeks to high cost of transfer.
I was under the impression that the scheduler would try to reduce the number of data transfers necessary. Is this explicitly done, or is this just expected to happen due to last-in-first-out task distribution?
dcherian commentedon Mar 29, 2024
This is interesting, I don't actually update the sizes.
@fjetter would the scheduling algorithm work better if the size/shapes were more accurate at the blockwise step?
mrocklin commentedon Mar 29, 2024
ivirshup commentedon Jun 5, 2024
@fjetter, did you ever get a chance to look at this? I'm starting to look into it again and would appreciate any guidance!
dcherian commentedon Jun 5, 2024
Is there a real dataset, preferably on public cloud, that we can use as an example?
ivirshup commentedon Jun 5, 2024
It would require a little bit of manipulation. Most of the "real data" cases easily available I can think go off the top of my head are in a sparse format in either hdf5 or tiledb. I could share a link with some code to construct the Dask arrays?
dcherian commentedon Jun 5, 2024
I think that would help.
ivirshup commentedon Jun 5, 2024
Here is a gist with examples using both hdf5 and tiledb: https://gist.github.com/ivirshup/eb4f5beb1bb33724b8c11bd0eacf03a6
I was able to get a little further today, and was able to get a few use-cases to actually run, but I am on a larger machine than I was using previously.
dcherian commentedon Jun 6, 2024
TLDR This isn't a dask problem. The core "array"/read tasks are treated as root-ish.
This works on my laptop with 32GB RAM with some spilling
EDIT: I've moved discussion to xarray-contrib/flox#346
EDIT3: Memory issues can be controlled by using
numbaggandnansum. It's also 5X faster (approx). I guess we should look in to how to makefloxjust handle sparse matrices directly.phofl commentedon Nov 4, 2024
Is there anything that we can help with @dcherian ?
dcherian commentedon Nov 4, 2024
Don't think so! but see xarray-contrib/flox#396 :)
I think this can be closed.
phofl commentedon Nov 4, 2024
Ah yikes, I missed that ping. This sounds like a shuffle - map_blocks problem now that we have a shuffle. I'll take a closer look though