-
-
Notifications
You must be signed in to change notification settings - Fork 720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DNM] Scatter shuffle proof-of-concept #5473
Closed
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d895141
Consistent worker Client instance in `get_client`
gjoseph92 5e8fcd4
Check for Futures from the wrong Client in `gather`
gjoseph92 0cd61df
POC for scatter-based shuffle
gjoseph92 30af07e
Set current client in worker while deserializing dependencies
gjoseph92 3399830
Don't report back locally-scattered keys
gjoseph92 555be9b
also don't report to workers (is this right?)
gjoseph92 8b2d7b0
scatter docstrings + comments
gjoseph92 1898673
faster names to key_split
gjoseph92 90e1c47
Preserve contextvars during comm offload
gjoseph92 6d9d090
Remove maybe-superfluous message in setstate
gjoseph92 673ea24
FIXME remove address coercion in update_data
gjoseph92 4369028
no enforce_metadata or transform_divisions
gjoseph92 d485eaa
shuffle-split -> shuffle-shards
gjoseph92 23b1e65
REVERTME no-report cancellation
gjoseph92 a46c507
REVERTME remove cancel logs
gjoseph92 78c337f
REVERTME don't cancel
gjoseph92 aefe78a
HACK don't inform on deserialized keys
gjoseph92 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Generic, TypeVar | ||
|
||
from dask.base import tokenize | ||
from dask.dataframe import DataFrame | ||
from dask.dataframe.core import _concat | ||
from dask.dataframe.shuffle import shuffle_group | ||
from dask.highlevelgraph import HighLevelGraph | ||
from dask.sizeof import sizeof | ||
|
||
from distributed import Future, get_client | ||
|
||
if TYPE_CHECKING: | ||
import pandas as pd | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
|
||
class QuickSizeof(Generic[T]): | ||
"Wrapper to bypass slow `sizeof` calls" | ||
|
||
def __init__(self, obj: T, size: int) -> None: | ||
self.obj = obj | ||
self.size = size | ||
|
||
def __sizeof__(self) -> int: | ||
return self.size | ||
|
||
|
||
def split( | ||
df: pd.DataFrame, | ||
column: str, | ||
npartitions_output: int, | ||
ignore_index: bool, | ||
name: str, | ||
row_size_estimate: int, | ||
partition_info: dict[str, int] = None, | ||
) -> dict[int, Future]: | ||
"Split input partition into shards per output group; scatter shards and return Futures referencing them." | ||
assert isinstance(partition_info, dict), "partition_info is not a dict" | ||
client = get_client() | ||
|
||
shards: dict[int, pd.DataFrame] = shuffle_group( | ||
df, | ||
cols=column, | ||
stage=0, | ||
k=npartitions_output, | ||
npartitions=npartitions_output, | ||
ignore_index=ignore_index, | ||
nfinal=npartitions_output, | ||
) | ||
input_partition_i = partition_info["number"] | ||
# Change keys to be unique among all tasks---the dict keys here end up being | ||
# the task keys on the scheduler. | ||
# Also wrap in `QuickSizeof` to significantly speed up the worker storing each | ||
# shard in its zict buffer. | ||
shards_rekeyed = { | ||
# NOTE: this name is optimized to be easy for `key_split` to process | ||
f"{name}-{input_partition_i}-{output_partition_i}": QuickSizeof( | ||
shard, len(shard) * row_size_estimate | ||
) | ||
for output_partition_i, shard in shards.items() | ||
} | ||
# NOTE: `scatter` called within a task has very different (undocumented) behavior: | ||
# it writes the keys directly to the current worker, then informs the scheduler | ||
# that these keys exist on the current worker. No communications to other workers ever. | ||
futures: dict[str, Future] = client.scatter(shards_rekeyed) | ||
# Switch keys back to output partition numbers so they're easier to select | ||
return dict(zip(shards, futures.values())) | ||
|
||
|
||
def gather_regroup(i: int, all_futures: list[dict[int, Future]]) -> pd.DataFrame: | ||
"Given Futures for all shards, select Futures for this output partition, gather them, and concat." | ||
client = get_client() | ||
futures = [fs[i] for fs in all_futures if i in fs] | ||
for f in futures: | ||
# HACK: we disabled informing on deserialized futures, so manually mark them as finished | ||
if not f.done(): | ||
f._state.finish() | ||
shards: list[QuickSizeof[pd.DataFrame]] = client.gather(futures, direct=True) | ||
# # Since every worker holds a reference to all futures until the very last task completes, | ||
# # forcibly cancel these futures now to allow memory to be released eagerly. | ||
# # This is safe because we're only cancelling futures for this output partition, | ||
# # and there's exactly one task for each output partition. | ||
# client.cancel(futures, force=True, _report=False) | ||
|
||
return _concat([s.obj for s in shards]) | ||
|
||
|
||
def rearrange_by_column_scatter( | ||
df: DataFrame, column: str, npartitions=None, ignore_index=False | ||
) -> DataFrame: | ||
token = tokenize(df, column) | ||
|
||
npartitions = npartitions or df.npartitions | ||
row_size_estimate = sizeof(df._meta_nonempty) // len(df._meta_nonempty) | ||
splits = df.map_partitions( | ||
split, | ||
column, | ||
npartitions, | ||
ignore_index, | ||
f"shuffle-shards-{token}", | ||
row_size_estimate, | ||
meta=df, | ||
enforce_metadata=False, | ||
transform_divisions=False, | ||
) | ||
|
||
all_futures = splits.__dask_keys__() | ||
name = f"shuffle-regroup-{token}" | ||
dsk = {(name, i): (gather_regroup, i, all_futures) for i in range(npartitions)} | ||
return DataFrame( | ||
HighLevelGraph.from_collections(name, dsk, [splits]), | ||
name, | ||
df._meta, | ||
[None] * (npartitions + 1), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is about updating the
who_wants
on scheduler side. however, I don't know for sure