Skip to content

Poor scheduling with flox, leading to high memory usage and eventual failure #11026

Closed

Description

@ivirshup

Describe the issue:

When performing aggregation with flox, I quickly run out of memory, regardless of the size of a chunk.

It appears that lower nodes (loading data) keep getting run while higher nodes (reduction) sit un-computed.

Opening here as requested in:

cc: @dcherian

Minimal Complete Verifiable Example:

import dask.distributed as dd
import dask.array as da
import numpy as np
import flox

cluster = 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 store
X = 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()` image
Whereas `res.compute()` ends up with something more like: image image

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)

Activity

  1. added
    needs triage
    on Mar 26, 2024
  2. fjetter commented on Mar 28, 2024

    @fjetter
    Member

    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

    image

    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.

  3. dcherian commented on Mar 28, 2024

    @dcherian
    Collaborator

    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.

  4. ivirshup commented on Mar 28, 2024

    @ivirshup
    Author

    @dcherian, adding an intermediate (computed) zarr store like this:

    M, N = 500_000, 20_000
    N_CATEGORIES = 2_000
    SCRATCH = Path("/scratch/tmp")
    X_pth = SCRATCH / "X.zarr"
    
    X_orig = da.random.normal(size=(M, N), chunks=(5_000, N))
    
    X_orig.to_zarr(X_pth, overwrite=True, compute=True)
    X = da.from_zarr(X_pth)
    by = np.random.choice(N_CATEGORIES, size=M)

    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.

    image

    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.

  5. dcherian commented on Mar 28, 2024

    @dcherian
    Collaborator

    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.

    image

    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.

  6. ivirshup commented on Mar 29, 2024

    @ivirshup
    Author

    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:

    • 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-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?

  7. dcherian commented on Mar 29, 2024

    @dcherian
    Collaborator

    Why does the groupby_sum-chunk task output an array the same size as the input?

    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?

  8. mrocklin commented on Mar 29, 2024

    @mrocklin
    Member
  9. ivirshup commented on Jun 5, 2024

    @ivirshup
    Author

    @fjetter, did you ever get a chance to look at this? I'm starting to look into it again and would appreciate any guidance!

  10. dcherian commented on Jun 5, 2024

    @dcherian
    Collaborator

    Is there a real dataset, preferably on public cloud, that we can use as an example?

  11. ivirshup commented on Jun 5, 2024

    @ivirshup
    Author

    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?

  12. dcherian commented on Jun 5, 2024

    @dcherian
    Collaborator

    I could share a link with some code to construct the Dask arrays?

    I think that would help.

  13. ivirshup commented on Jun 5, 2024

    @ivirshup
    Author

    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.

  14. dcherian commented on Jun 6, 2024

    @dcherian
    Collaborator

    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

    res, codes = flox.groupby_reduce(
        X_dense.T,
        by,
        func="sum",
        fill_value=0,
    )

    EDIT: I've moved discussion to xarray-contrib/flox#346

    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.

    res, codes = flox.groupby_reduce(
        X_dense.T,
        by,
        func="nansum",
        fill_value=0,
        engine="numbagg",
    )
  15. phofl commented on Nov 4, 2024

    @phofl
    Collaborator

    Is there anything that we can help with @dcherian ?

  16. dcherian commented on Nov 4, 2024

    @dcherian
    Collaborator

    Don't think so! but see xarray-contrib/flox#396 :)

    I think this can be closed.

  17. phofl commented on Nov 4, 2024

    @phofl
    Collaborator

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions