Repository navigation
Open
Labels
Description
Summary
- I'm struggling to figure out how to avoid OOMs in a seemingly simple shuffle on a ~6gb parquet.snappy dataset using 16 workers, each with 8gb mem, ~4gb memory limit, 1 proc, and 1 thread. I'm not persisting anything, and I'm ok with shuffle tasks spilling to disk as necessary.
- The OOMs cause the job to either fail after a while or complete after a really long while, nondeterministically.
- I decreased task size by increasing task count (128 -> 512), but I still observed OOMs with similar frequency.
- Plotting mem usage over time shows a tight distribution around
--memory-limitfor the first ~1/2 of the job and then large variance for the second ~1/2 of the job, during which time OOMs start happening (plots below). - I created more headroom for this large variance by decreasing
--memory-limit(4gb/8gb -> 2gb/8gb) and I did observe many fewer OOMs, but still 1 OOM, and moreover 2gb/8gb impedes our ability to persist data later in this pipeline for an iterative ML algo so this isn't a feasible solution. - Maybe there's something fishy on the dask side happening here, in particular in the high variance of mem usage above
--memory-limit? Or maybe I'm just making a dumb user error somewhere that's easy to fix? - Lmk if I can clarify or distill anything better!
Setup
- 16 workers (on k8s on ec2), each running in its own docker container with 8gb mem and 1 cpu
- Workers running with ~4gb mem limit, 1 proc, and 1 thread:
DASK_COMPRESSION=zlib dask-worker --nprocs 1 --nthreads 1 --memory-limit=4e9 --no-nanny <scheduler-url>
- Code looks like:
# Read from parquet (s3)
# - 238 parts in
# - ~6.5gb total
# - Part file sizes vary 10-50mb (see plot below)
ddf_no_index = dd.read_parquet(in_path)
# Pick task/part count for output
num_parts_out = ... # 128 or 512
# Reindex to a column of uniformly distributed uuid5 values with fixed, uniform divisions
# - npartitions=num_parts_out, via divisions=uniform_divisions[num_parts_out]
ddf_indexed = ddf_no_index.set_index(
uniformly_distributed_uuid5_column,
drop=False,
divisions=uniform_divisions[num_parts_out],
)
# Write to parquet (s3)
# - 128 or 512 parts out
# - ~6.6gb total (based on a successful 128-part output)
# - When 128 parts, output part files vary 54-58mb (see plot below)
# - When 512 parts, output part files should vary ~10-15mb, but I didn't let the job finish
(ddf_indexed
.astype(...)
.drop(ddf_indexed.index.name, axis=1)
.to_parquet(
out_path,
compression='snappy',
object_encoding=...,
write_index=True,
)
)- Data skew looks like:
| input parquet.snappy part file sizes 238 parts |
output parquet.snappy part file sizes 128 parts |
|---|---|
![]() |
![]() |
Trials
- Rows 1–2: my starting point was
num_parts_out=128with--memory-limit=4e9, which fails a lot of the time but actually succeeded twice with many OOMs and long runtimes - Row 3: I increased task count to
num_parts_out=512, but saw a similar frequency of OOMs and killed the job - Row 4: I decreased mem limit to
--memory-limit=2e9but still saw 1 OOM (and thus some amount of repeated work) - Col "sys metrics": check out the change in variance in mem usage partway through the job, after which OOMs start happening
- Col "task aftermath": you can see the lost workers, all due to OOMs
- Col "task counts": shows the number of shuffle tasks, for reference (~6–8k)
Versions
$ python --version
Python 3.6.0
$ cat requirements.txt | egrep 'dask|distributed|fastparquet'
git+https://github.com/dask/dask.git@a883f44
git+https://github.com/dask/fastparquet.git@d07d662
distributed==1.16.2Reacted by Anderson Banihirwe, Daniel Farrell and Jack Xu












Activity
mrocklin commentedon Jun 15, 2017
First, thank you for the detailed writeup.
As for the core cause, my first guess is that this is due to inaccurate accounting between estimating the size of the pandas dataframes and determining their true cost in memory. This often happens when we have custom objects that are large but don't implement the
__sizeof__protocol. Currently--memory-limitworks on the sum of the estimated sizes of data elements we have in memory, and not on what the process reports as memory usage. There are various reasons for this, but we could consider looking at both in the future.This provides a different possible answer. The second half of the job tends to be communication heavy, so maybe most of our data use isn't in storing data, but rather in bytes that are in flight. Dask doesn't account for bytes in active use by user code or the network layer. These bytes are much harder for us to control.
Questions
/mainpage of interest during communication heavy work.jdanbrown commentedon Jun 20, 2017
Answer 3
Hmm, no good reason—thanks for the heads up! I've switched
DASK_COMPRESSIONfrom zlib to lz4 (and I also found this useful reference on compression tradeoffs). Updated worker run command:I also went ahead and upgraded dask/distributed/fastparquet to the latest masters, at the risk of changing my repros in subtle ways. Still seeing OOMs, but fwiw the job seems to be failing faster than before (based just on the 2 trials below).
Answer 1
All text, numbers, and timestamps, and no custom python objects. Here's the distribution of types per column (206 columns total), in terms of the count of columns that are comprised of a given set of types, which there are only 8 of:
And here's a more detailed unpacking of each column with its mean size per value:
Answer 2
Hmm, I ran two more trials and collected some (partial) data from some of the worker
/mainpages, but nothing jumps out at me as fishy.KilledWorker, perhaps because I upgraded to the latest master commits for these trials.128 parts out
4g mem limit
8g mem
16 workers
~25m
KilledWorker128 parts out
4g mem limit
8g mem
16 workers
~25m
KilledWorkerQuestions
/mainpages?jdanbrown commentedon Jun 20, 2017
Another datapoint, which also answers my 3rd question:
128 parts out
4g mem limit
16g mem
7 workers
28m
success
mrocklin commentedon Jun 20, 2017
jdanbrown commentedon Jun 26, 2017
Another unsuccessful approach at solving the "4g mem limit, 8g mem, 16 workers" case:
psutilreports host metrics, not container metrics/workerspage shows "memory: 64 GiB", which is our k8s host ram, instead of the pod's container mem (one of 4/8/16 GiB in the various trials above)--memory-limit, other parts of dask are seeingpsutil.virtual_memory().total= 64GiB and allocating more than they would have otherwise, which is causing OOMs, e.g.max_buffer_sizeindistributed.comm.tcppsutilwherevirtual_memory()returns metrics from/sys/fs/cgroup/memoryinstead of/proc, and verified that I saw "memory: 8 GiB" on the/workerspagepsutilhackmrocklin commentedon Jul 3, 2017
Hey, sorry for the delay in getting to this. It dropped off of my radar in the confusion of vacation two weeks ago.
The fact that the worker-memory-plot (upper left) is red says that Dask knows that its workers are out of memory. The workers are actively sending things to disk, they're just not sending things fast enough to avoid OOM-ing. We should probably have a separate coroutine running that tracks used memory using
psutiland starts to slow things down if we get close to the upper limit. Obviously there are cases where oursizeofcomputations are failing, or we're not accounting for memory use in other components, such as data-in-flight.Something that would be interesting to try is a shuffle on purely numeric data
I would set
Nsufficiently high so that it overwhelms your available memory. This would help us to see if there is a problem with how we're accounting for data (accounting for ints is easy) or something else.avaranovich commentedon Jul 13, 2017
@jdanbrown Indeed, if you run workers as replicas in Kubernetes, each of the workers by default gets the host stats. In my case I run 10 workers, and I get my HOST RAM * 10, which is wrong. My current understanding is that dask should support docker metrics https://docs.docker.com/engine/admin/runmetrics/ , i.e. use docker API to pull the resource consumption by the container, and not the host. But I did not have time to implement this yet.
martindurant commentedon Jul 13, 2017
psutil uses standard system tools, which get the wrong answers within a container.
Can you look at
/sys/fs/cgroup/memory/memory.*inside containers to see if the values there look reasonable?martindurant commentedon Jul 13, 2017
(note that dask-kubernetes always passes
--memory-limitto each worker in a container that is a bit smaller than the container configured size, and this is probably generally good practice)jdanbrown commentedon Jul 21, 2017
@mrocklin No worries, and sorry for the delay on my end as well—been busy juggling multiple projects.
sizeofisn't the issue, since it hacks the workers'self.datato remove theirzict.Bufferentirely so they're forced to spill all keys to disk.jdanbrown commentedon Jul 21, 2017
@martindurant Yep, in my k8s docker container
/sys/fs/cgroup/memory/memory.limit_in_bytesdoes report the correct mem limit for the pod, unlike/proc/meminfowhich reports the host metrics.jdanbrown commentedon Aug 7, 2017
@mrocklin Any thoughts on my most recent comment above (#2456 (comment))? It seems to indicate this is a task-shuffle issue and not a sizeof issue.
More generally, my team is looking for ways to safely shuffle datasets that are bigger than total worker ram. Our main pipeline is an iterative model fit on training data that easily partitions across workers' ram and works great with dask, but to get that training data we have to go through a series of more traditional ETL munging steps that require shuffles (
.set_index,.merge) at various points, and that's where we've been struggling with OOMs. We've incurred a lot of incidental complexity as a result of our shuffle operations being very delicate, and overall the OOM issue has been a major drag on our development pace.Any pointers or suggestions? Are we too far off the happy path of well tread use cases? We'd love to be able to make dask a go-to tool for our broader data team.
mrocklin commentedon Aug 7, 2017
Sorry for the lack of response on this. I'll try to take a look at this either Tuesday or Wednesday this week.
mrocklin commentedon Aug 7, 2017
Looking at this again, it looks like you may be running into issues similar to @bluenote10
You might find the following issues and PRs of interest:
mrocklin commentedon Aug 7, 2017
Those issues point to a possible narrative that Pandas and the Python garbage collector sometimes leave data lying around for longer than they should. Periodically calling
gc.collect()at the right times can reduce this cost, but this can play havoc with Dask in other hard-to-pin-down ways.Another possibility is that the data that is in-flight is large enough to push things over the edge. Dask isn't able to account for this data. I wouldn't expect this to be that large though.
5 remaining items
mrocklin commentedon Aug 13, 2017
@bluenote10 are you able to correlate the jumps with any particular event occurring in the worker logs? Do you have any suspicions about what causes these jumps?
mrocklin commentedon Aug 13, 2017
@bluenote10 I'm also curious to know if anything changes if you disable work stealing. I've identified a small leak there. I wouldn't expect it to result in sharp spikes like what you're seeing here, but it would be interesting to see if there is any difference.
See dask/distributed#1325 for how to disable work stealing (either by using the config option or just deleting the right line of code.)
jdanbrown commentedon Aug 21, 2017
@mrocklin I haven't had time to test your dask.array hypothesis yet, but I'm very curious to. In the meantime, what's the setup you used to try my dask.dataframe repro? Above you said:
and I'm curious to see if I can repro your good behavior on my end.
jdanbrown commentedon Sep 11, 2017
@mrocklin Check it out—I now have an OOM repro for array, bag, and dataframe:
Eager to hear your thoughts! Here's the summary (copied from the repo's README):
Experimental setup
dask==0.15.2distributed==1.18.3--memory-limit(each worker reports 0.584g)Results
oom_ddf.py
cols=10 part_rows=157500 nparts=64cols=10 part_rows=157500 nparts=128cols=10 part_rows=157500 nparts=256cols=10 part_rows=157500 nparts=512oom_array.py
sqrt_n=64sqrt_n=96sqrt_n=112sqrt_n=120sqrt_n=128oom_bag.py
nparts=2nparts=4nparts=8nparts=32nparts=64nparts=128nparts=256nparts=512KilledWorker)mrocklin commentedon Sep 11, 2017
mrocklin commentedon Nov 1, 2017
Note that there has been some good work in avoiding memory leaking when splitting many small pandas dataframes recently. You might want to try out the master branch.
Spacerat commentedon Apr 7, 2018
I downloaded and reran
oom_ddf.pyfrom @jdanbrown 's repro today withdask==0.17.2anddistributed==1.21.5, and it still OOMs.mrocklin commentedon Apr 7, 2018
If you have time to investigate this @Spacerat that would be most welcome
Spacerat commentedon Apr 12, 2018
Although I would like to, unfortunately I do not have enough time right now; for us it was quicker to just port our problematic pipeline to use Go & some command-line tools. (Dask was probably not the right fit anyway.)
denisvlah commentedon Sep 2, 2019
Having the same issues on dask version 2.1.0.
Environment is almost the same as described by @jdanbrown.
Any plans to fix this issue?
mrocklin commentedon Sep 2, 2019
I suspect that the comments above summarize the current state of things.
Is this something that you wanted to volunteer for @denisvlah ?
jakirkham commentedon Jul 21, 2020
For those still encountering this issue, an interesting exercise may be to try Dask + Distributed 2.21.0 on Python 3.8 (or an earlier version with
pickle5installed). This leverages some fancy new features in pickle (namely out-of-band buffers), which means it will be a bit more memory efficiently when spilling data to disk.It's worth noting this may not fix the problem completely. There may still be other issues here. However it would be interesting to see what issues remain, what those issues look like, and what impact they have.
odovad commentedon Mar 15, 2022
This issue seems pretty old but I think I've experienced the same behaviour (OOM) with the latest version of dask (2021.02.1) while trying to perform a set_index on a column from an input dataset. I've used the MemorySampler to see how the memory usage looks like.
In blue, only one worker; no errors;
In orange, two workers with workers being killed even if I've a lot more memory than needed for my workers (16G)
I'm not sure why but it seems that I do not have issues with the dask 2.22.0
bryanwweber commentedon Mar 28, 2022
@odovad Can you please provide a minimal reproducer for your example? Thank you!