Skip to content

Compress payload during shuffle operation - #6259

Closed
fjetter wants to merge 9 commits into
dask:mainfrom
fjetter:shuffle_compress_payload
Closed

fjetter wants to merge 9 commits into
dask:mainfrom
fjetter:shuffle_compress_payload

Conversation

@fjetter

@fjetter fjetter commented May 29, 2020

Copy link
Copy Markdown
Member

For jobs which process a dataframes with a significant amount of payload columns, shuffle operations can be incredibly memory consuming. Especially due to the multi stage algorithm, multiple copies of the data are in fact intended. Due to the multi stage shuffle we also need to (de)serialize these columns many times. However, for the shuffle itself these columns are effectively dead weight.

This change proposes to serialize (and more importantly compress) the payload columns during the shuffle operation which allows for a significant reduction in memory usage. Not only is the memory usage reduced (less spill to disk), but this also reduces the amount of IO (disk or network depending on the shuffle mode) and the overhead of (de)serializing "garbage". We've seen significant speed ups in runtime when testing this; even with reduced cluster sizes (I don't have proper benchmarks prepared yet but can follow up with some, if desired).

This is not ready to merge since it still fails for multiple environments (especially older pandas versions; The py3.8 job is successful with the exception of a failed doctest) and I will address these build failures if we are willing to accept this contribution.

To compress and serialize the payload, I'm using the distributed.protocol assuming that it is stable, fast, handles compression on the fly and allows for customization if desired (e.g. to use different serializers, arrow/parquet come to my mind but I haven't tested this lately). If there are other options for this format, I'm open for suggestions since atm this would obviously only work if distributed is installed.

The code itself is a bit messier than I would like to. I needed to use the DataFrameGroupy.apply which has the obvious downsides of being slow. I would argue, however, that this slowdown is acceptable, weighing in the performance gains I expect from this change.
On top of this, I observe funny/unstable behaviour of this function (It improves with more recent pandas versions but I consider it still unstable).
If somebody has a suggestion how to express this logic without using apply, I'd appreciate the input.

Thanks @crepererum for this cool idea

For completeness and transparency, we're building this currently into kartothek where we need to deal with fewer edge cases (see JDASoftwareGroup/kartothek#286 ) but we would obviously prefer this to be part of dask itself.

@TomAugspurger

Copy link
Copy Markdown
Member

Haven't looked closely yet, but I believe that the rapids folks (@rjzamora and @madsbk, maybe others) have been looking closely at this area too.

@rjzamora

Copy link
Copy Markdown
Member

cc @jakirkham

@crepererum

Copy link
Copy Markdown
Contributor

@fjetter thank you very much for integrating and polishing my rough idea. It's really good to see it swimming upstream and also how small the actual code change it 👍

Please note that a similar optimization could be done for the tasks-based Bag.groupby. This can (and should) probably be done in a separate PR, so we can first discuss and decide here if the approach makes sense.

Also as a side note on shuffling: This can of course be improved / tuned further. Shuffling (both for the DF and for the Bag) are basically a (distributed) shuffle with two components K (the key) and V (the passive payload, sometimes K needs to calculated from V or is a part of the payload, but let's keep it simple for now). At the beginning, we start with elements Iterable[Tuple[K, V]] we want to end with Dict[K, Pack[V] (Pack can be a Bag partition, or a concatenated DF, or just a list/iterable; and Dict here is more an iterable where K is unique, but you get the idea). Basically, V is "dead weight" and does not need to be touched during the shuffle. This can be exploited in multiple ways (non-exhaustive list):

  1. cheap (de-)serialize: do not pickle/unpickle, compress/decompress, ... V while in-flight, but use a format that is cheap to move around (bytes for example).
  2. (stronger) compression: since the task is heavily IO-bound, it can makes sense to apply a more expensive but stronger (in terms of ratio) compression before feeding the data through the shuffle network (that's what @fjetter also illustrates in the idea to use Parquet).
  3. out-of-band transmission: in certain extreme scenarios, it can even be helpful to move V out of the cluster (e.g. to disk or to an external storage system like S3 or ABS), because it can be faster to do this pre-shuffle transmission and post-shuffle retrieval (1 send, 1 retrieve, both more expensive then a worker-to-worker transmission) instead of moving the data through the network many times.

The PR implements (1) and (2) for DFs. (3) (or any further options) are out of scope here but interested parties might try it out.

@jakirkham

Copy link
Copy Markdown
Member

FWIW @fjetter Pandas serialization hasn't been particularly efficient historically ( dask/distributed#614 ). PR ( dask/distributed#3784 ) changed that in 2.17.0 as it now handles out-of-band serialization when pickling (if supported), which works on Pandas objects. I haven't looked into compression in that case, but suspect it may be happening as well. Maybe worth updating to the latest version and exploring workloads if you haven't already. Would be good to have an update on issue ( dask/distributed#614 ) with anything you find. 🙂

@fjetter

fjetter commented Jun 2, 2020

Copy link
Copy Markdown
Member Author

@jakirkham Yes, I am aware of your work and am very excited to test this. Unfortunately, we're still stuck on py3.6 for a while which makes it hard to test this at the moment since pickle5 is not supported. Once I get the opportunity to properly test this, I'll leave a note in the ticket.

I haven't looked into compression in that case, but suspect it may be happening as well.

Yes, distributed automatically compresses the frames if it yields favourable compression rates, see https://github.com/dask/distributed/blob/1fe50c24792d74643772268c188decd3c1ab76ee/distributed/protocol/compression.py#L160 This is what I effectively use here as well.


Irrelevant of the performance of the serialization, I believe this PR makes a big difference, assuming the shuffled dataframe has a significant amount of payload data since this PR proposes to (de-)serialize and compress this payload data only once instead of doing it on every submission.
Additionally, the current staged implementation intentionally holds multiple copies of the data on the cluster which is much cheaper if the payload is compressed. This has a significant impact on the memory usage (I'll try to prepare a few measurements to support this claim). So just to make the point very clear, this PR is more concerned with memory compression. The serialisation performance (by doing less) is just the cherry on top imo.

Another note to the argument @crepererum mad above about the K, V split: I currently store the V in the dataframe itself which might not be the most performant solution but it is rather elegant since we don not need to touch much. That could be obviously refined at a later stage since even point 1 and 2 can probably be expressed more efficiently with a more specialized implementation.


Yet another benefit of this approach I want to mention since it falls a bit through the cracks if not looked into closely.

The pack_payload column performs a groupby.apply which effectively eliminates a certain redundancy in the data by reducing the number of rows to the essential, yet again benefiting IO, serialization and memory

Example

In [2]: import pandas as pd
   ...: import dask.dataframe as dd
   ...: from dask.dataframe.shuffle import pack_payload
   ...:
   ...: df = pd.DataFrame({"A": [1, 1] * 2 + [2, 2] * 2 + [3, 3] * 2, "B": range(12)})
   ...: ddf = dd.from_pandas(df, npartitions=2)

In [3]: ddf.partitions[0].compute()
Out[3]:
   A  B
0  1  0
1  1  1
2  1  2
3  1  3
4  2  4
5  2  5

In [4]: pack_payload(ddf, "A").partitions[0].compute()
Out[4]:
   A                               __dask_payload_bytes
0  1  b'\x03\x00\x00\x00\x00\x00\x00\x00)\x00\x00\x0...
1  2  b'\x03\x00\x00\x00\x00\x00\x00\x00)\x00\x00\x0...

@fjetter fjetter mentioned this pull request Jun 2, 2020
@jakirkham

Copy link
Copy Markdown
Member

No worries. We are looking at the long tail as well ( dask/distributed#2495 ) 🙂

@fjetter
fjetter force-pushed the shuffle_compress_payload branch from 5501788 to b9742b9 Compare June 3, 2020 07:19
@fjetter fjetter changed the title RFC Compress payload during shuffle operation Compress payload during shuffle operation Jun 3, 2020
@jakirkham

Copy link
Copy Markdown
Member

Should add there are similarities to this and @madsbk's PR ( dask/distributed#3624 ) to transmit serialized data. @mrocklin pointed out in that PR that we might benefit from having a MutableMapping that for spilling, which would contain spilled, compressed, in-memory data ( dask/distributed#3656 ). We could then leverage this to transmit this compressed data between workers. Certainly useful for shuffling. Though probably useful for other things as well.

@fjetter

fjetter commented Jun 24, 2020

Copy link
Copy Markdown
Member Author

The auto compression MutableMapping would also be interesting but we'd be faced with the problem of having the payload and the group keys be compressed into the same byte string, i.e. every time I want to work with the data I'd have to decompress everything although I'm only interested in the group key. However, with a mutable mapping like this, one could think about splitting the payload and group key into two tasks/data objects... that would probably fit nicely with the idea of #6164


While I was not, yet, able to perform proper benchmarks, what I could observer already is that this general approach presented here will not work since the groupby.apply is too granular and for some data constellations causes major performance issues. In a nutshell, grouping on the group key directly creates too many groups and the apply + compression is too slow and in some edge cases even uses up more memory. This implementation would need to group on the final partition ID (I know the _partition column was recently removed for a lot of shuffle operations) instead since there are much fewer groups... I will try to produce a few numbers to make my point

Base automatically changed from master to main March 8, 2021 20:19
@fjetter

fjetter commented May 26, 2021

Copy link
Copy Markdown
Member Author

Closing this since obviously stale and I currently don't have time to update. The idea is still valid if somebody wants to pick this up

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants