Skip to content

Running out of memory due to task prioritization by scheduler #2470

Closed

Description

@olivier-lacroix

Hi,

I am using dask (0.14.3) (Python 3.6.1 :: Anaconda 4.4.0 (64-bit)) and have run into an issue using either the distributed or multiprocessing scheduler on a single machine.

I am applying a function through a map_partitions on a large dask dataframe (read from several h5 files) that returns a result with a small RAM footprint from a relatively large dask partition, and then compute the result.

Doing this, the memory footprint increases until the system runs out of it and the kernel kills a couple of workers. Looking at task progress with the distributed scheduler, a lot of partitions are being read in advance (2-3 per worker), and then wait in RAM for the function to be applied, and thus filling up the RAM unnecessarily.

Is there a way to avoid this suicidal behaviour from the scheduler(s)?

Note: the only way around it I found was reducing the number of workers, which is not ideal.

Thanks

Olivier

Activity

  1. mrocklin commented on Jun 17, 2017

    @mrocklin
    Member

    This Stack overflow example may interest you: https://stackoverflow.com/questions/44344171/understanding-memory-behavior-of-dask-distributed

    Also, I wouldn't expect the scheduler to run out of RAM. If you are loading data on workers then the scheduler should never have any data load. The workers will share among each other. Perhaps you're putting data directly into your graph?

    I recommend producing a minimal example if possible.

  2. olivier-lacroix commented on Jun 19, 2017

    @olivier-lacroix
    Author

    Thank you Matt,

    Yes, that looks a lot like it.

    Sorry if it was not clear: I am suspecting the workers to fill the RAM, and not the scheduler. All data load is done at the worker level by reading from disk.

    Each dask partition is around 50Mo (from pandas.dataframe.info() ), and other input data loaded by the function applied through the map_partition is also around 50 Mo.

    I was therefore surprised to see the system run out of RAM with 40 workers on a 40 cores system and 160Go of RAM.

    What is the best way to look into workers' data & memory footprint?

    Thanks,

    Olivier

  3. mrocklin commented on Jun 27, 2017

    @mrocklin
    Member

    The diagnostic web pages show both the expected memory use per worker on the upper left plot of the status/ page and the memory use reported by psutil on the workers/ page

  4. mrocklin commented on Jul 3, 2017

    @mrocklin
    Member

    @olivier-lacroix is there anything else to do here or can we close this?

  5. ogrisel commented on Oct 20, 2017

    @ogrisel
    Contributor

    @mrocklin I have faced what I believed is the same issue. I can reproduce the problem on a minimal example:

    import dask.dataframe as dd
    import dask.array as da
    from dask.distributed import Client
    c = Client('localhost:8786')
    
    array = da.random.normal(size=(int(1e10), 10), chunks=(int(1e6), 10))
    df = dd.from_array(array, columns=[str(i) for i in range(10)])
    df.to_parquet('test.parquet')

    The above workflow should be able to "stream" blocks of 80MB (10e6 float64 per chunk) into the parquet file on the disk. However if I launch a single worker with 1GB of working memory:

    dask-worker --nthreads 1 --nprocs 1 --memory-limit 1e9  localhost:8786
    

    I get after a minute or so:

    distributed.nanny - WARNING - Worker exceeded 95% memory budget.  Restarting
    distributed.nanny - WARNING - Worker process 52463 was killed by unknown signal
    

    I believe that the scheduler is keeping in memory the intermediate chunks because the df and array variables are still the local scope of the client Python program. I don't know how to tell the scheduler that there is no need to keep this data around in memory (nor even to spill it to disk because we do not need to access df nor array anymore once df has been serialized into the parquet file).

    Edit: this is not related to parquet: you can replace the last line by df.mean(axis=0).compute() and you can stil get random worker OOM and restart by nanny.

  6. ogrisel commented on Oct 20, 2017

    @ogrisel
    Contributor

    I just found the compute=False option of to_parquet. Here is a new version of my code that does not seem to have the afore mentionned problem anymore:

    import dask.dataframe as dd
    import dask.array as da
    import dask
    from dask.distributed import Client
    c = Client('localhost:8786')
    
    
    def dump_data(filename):
        array = da.random.normal(size=(int(1e10), 10), chunks=(int(1e6), 10))
        df = dd.from_array(array, columns=[str(i) for i in range(10)])
        return df.to_parquet(filename, compute=False)
    
    
    dask.compute(dump_data('test.parquet'))

    EDIT: this is actually wrong. I can also reproduce the memory exhaustion with this version of the code as well.

  7. mrocklin commented on Oct 20, 2017

    @mrocklin
    Member

    On an eight-core machine I run the following:

    import dask.dataframe as dd
    import dask.array as da
    from dask.distributed import Client
    c = Client('localhost:8786')
    
    array = da.random.normal(size=(int(1e10), 10), chunks=(int(1e6), 10))
    df = dd.from_array(array, columns=[str(i) for i in range(10)])
    df.to_parquet('test.parquet')

    And it takes up about a GB of RAM when running but no more. This is consistent with having 8 80GB chunks (640MB) + some ongoing or very recent results. Data does not build up over time. 1GB of RAM for a worker is somewhat small, importing numpy, pandas, and others, can take a few hundred MB.

    I believe that the scheduler is keeping in memory the intermediate chunks because the df and array variables are still the local scope of the client Python program. I don't know how to tell the scheduler that there is no need to keep this data around in memory (nor even to spill it to disk because we do not need to access df nor array anymore once df has been serialized into the parquet file).

    Dask is definitely deleting intermediate values when no longer necessary, at least here on my machine. It might take a second or two for Python's GC to kick in. You can observe this by looking at the progress bars at http://localhost:8787/status . Color on the bar means finished tasks. Slightly more transparent alpha values mean released. I see that only a very small piece of the bar on the right is full color (in memory) while most of the bar is somewhat-transparent (released).

    If you were to increase the RAM or decrease the partition size or number of cores I suspect that things would go smoothly.

  8. ogrisel commented on Oct 20, 2017

    @ogrisel
    Contributor

    Hum alright. I set the limit to 1GB voluntarily to make it faster to trigger the problem but I think I had seen a similar problem with workers with a much larger limit (but it was also on larger chunks). Maybe it was just bad luck then. The problem is that it's too expensive to reproduce the original failure to investigate.

  9. ogrisel commented on Oct 20, 2017

    @ogrisel
    Contributor

    Also in the example script I gave, I use a single thread, so there is only one single chunk of 80MB of data that is required to be loaded at a time. 1GB should be enough even if several chunks are eagerly loaded by the scheduler ahead of conversion and dumping into the parquet sink.

  10. ogrisel commented on Oct 20, 2017

    @ogrisel
    Contributor

    But I agree that blaming the variable scoping as I did in #2470 (comment) is wrong as I reproduced the problem with that version of the code as well.

  11. ogrisel commented on Oct 20, 2017

    @ogrisel
    Contributor

    Aggressively triggering GC when detecting high memory usage in the worker seem to fix the issue when using the 1GB boundary. I will submit a PR.

  12. mrocklin commented on Dec 19, 2017

    @mrocklin
    Member

    I believe that this has been resolved.

  13. gabrielelanaro commented on Apr 13, 2018

    @gabrielelanaro
    Contributor

    I was recently having this same issue (dask 0.17.2)(distributed 1.21.6(

    For some reason the following code shows that the memory of the pyarrow tasks doesn't get cleared. I suspect it has to do with the scheduler.

    import pandas as pd
    import dask.dataframe as dd
    import numpy as np
    from dask.distributed import Client
    import time
    
    
    client = Client()
    val= np.random.rand(3000)
    df = dd.from_pandas(pd.DataFrame(val), npartitions=1000)
    
    def wait(df):
        time.sleep(1)
        return df
    
    df = df.map_partitions(wait, meta=df)
    df.to_parquet('/tmp/test', engine='pyarrow')
    

    Is it possible to reopen the issue?

  14. ogrisel commented on Apr 13, 2018

    @ogrisel
    Contributor

    Maybe open a new issue with your reproduction script and the version numbers of all the libraries. It could be:

    • a memory leak in pyarrow
    • a delayed GC of objects with reference cycles

    When you have a reference cycle I have the feeling that memory is not immediately freed even when you do an explicit gc.collect(). There can be some delay and this does not seem to be controllable. Maybe @pitrou can shed some light here.

  15. pitrou commented on Apr 13, 2018

    @pitrou
    Member

    If there is a memory leak in pyarrow, it would be nice to have a reproducer that doesn't involve dask :-)
    As for gc.collect(), it should always reciaim dead reference cycles. The only exception is, if another garbage collection is underway (e.g. in a different thread), gc.collect() doesn't do anything.

  16. mrocklin commented on Apr 13, 2018

    @mrocklin
    Member
  17. ogrisel commented on Apr 13, 2018

    @ogrisel
    Contributor

    @gabrielelanaro is the memory released once you call del df in the main script? It might just be the case that the intermediate results are kept in workers' memory because you still hold a reference in the driver / client program.

  18. gabrielelanaro commented on Apr 13, 2018

    @gabrielelanaro
    Contributor

    Thanks everyone for your help! I'll further troubleshoot the issue and find out if this indeed related to pyarrow or something else. I'll write more if I find anything.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions