Skip to content

Shuffle drops column "_partitions" in-place - #5657

Closed
madsbk wants to merge 1 commit into
dask:masterfrom
madsbk:inplace_drop
Closed

madsbk wants to merge 1 commit into
dask:masterfrom
madsbk:inplace_drop

Conversation

@madsbk

@madsbk madsbk commented Dec 2, 2019

Copy link
Copy Markdown
Contributor

Pandas' Dataframe support dropping a column in-place, which shuffle now uses.
Notice, cuDF will also support in-place drop: NVIDIA/cudf#3497

This PR doubles the performance of a distributed cuDF merge on a DGX1:

Before this PR (full copy)

Merge benchmark
--------------------------
Chunk-size  | 100000000
Frac-match  | 0.3
Ignore-size | 1.05 MB
Protocol    | ucx
Device(s)   | 1,2
==========================
Total time  | 2.51 s
==========================
(w1,w2)     | 25% 50% 75% (total nbytes)
--------------------------
(01,02)     | 6.77 GB/s 10.25 GB/s 10.36 GB/s (5.20 GB)
(02,01)     | 6.65 GB/s 10.06 GB/s 10.59 GB/s (5.20 GB)

After this PR (drop in-place)

Merge benchmark
--------------------------
Chunk-size  | 100000000
Frac-match  | 0.3
Ignore-size | 1.05 MB
Protocol    | ucx
Device(s)   | 1,2
==========================
Total time  | 1.26 s
==========================
(w1,w2)     | 25% 50% 75% (total nbytes)
--------------------------
(01,02)     | 9.79 GB/s 9.96 GB/s 10.91 GB/s (5.60 GB)
(02,01)     | 9.74 GB/s 9.88 GB/s 10.69 GB/s (5.60 GB)

cc @mrocklin

@madsbk
madsbk marked this pull request as ready for review December 2, 2019 15:10
@mrocklin

mrocklin commented Dec 2, 2019

Copy link
Copy Markdown
Member

Wow, it's great to see the 2x performance increase.

We should definitely find a way to do this in-place drop call, however, I don't think that we can support it reliably within the dd.DataFrame.drop method. Tasks in Dask may run twice or may run in different orders than we expect, and so we generally don't modify inputs of a task in-place.
We can still call my_cudf_dataframe.drop(..., inplace=True), but we'll have to call it immediately within some already existing task.

Alternatively, is there a way within cudf to make drop faster? Ideally calling drop would only be a change to metadata. cc @kkraus14

@kkraus14

kkraus14 commented Dec 2, 2019

Copy link
Copy Markdown
Member

Alternatively, is there a way within cudf to make drop faster? Ideally calling drop would only be a change to metadata. cc @kkraus14

Both Pandas and cudf have drop return by deep copy by default which is expensive, but we want to match the semantics.

Currently the shuffle function isn't calling drop here, it's calling del df['column']. I would assume this runs inplace similar to drop(..., inplace=True).

@kkraus14

kkraus14 commented Dec 2, 2019 •

Copy link
Copy Markdown
Member

@mrocklin

mrocklin commented Dec 2, 2019

Copy link
Copy Markdown
Member

In this particular situation yes, but in full generality, unfortunately not.

@mrocklin

mrocklin commented Dec 2, 2019

Copy link
Copy Markdown
Member

Another option here would be to do this as an optimization pass. If we ever find a blockwise operation that has a drop in it where that operation is not affecting the input then we add the inplace=True keyword. This would affect all safe uses of drop, not just the ones here.

The downside of this is that high level optimizations are somewhat new, and so there aren't many examples on how to do them. The best example of this today is probably the optimize_read_parquet_getitem optimization, which modifies read_parquet calls to select out only certain columns after the user has selected them, like this:

df = dd.read_parquet(filename)
df = df[["x", "y"]]
# becomes
df = dd.read_parquet(filename, columns=["x", "y"])

@mrocklin

mrocklin commented Dec 2, 2019

Copy link
Copy Markdown
Member

@rjzamora any interest in investigating dask dataframe optimizations?

You would want to read through

Then you would want to construct an optimization which looked through all Blockwise layers, found any that had an M.drop call and, if that call wasn't affecting inputs, added an inplace=True keyword to it.

This would force you to learn about the high level graph structure a bit more, and also into the guts of subgraph callables.

@rjzamora

rjzamora commented Dec 2, 2019

Copy link
Copy Markdown
Member

@rjzamora any interest in investigating dask dataframe optimizations?

Sounds good - Moving the inplace=True change into an optimization pass sounds like a good idea to me. I will look into it.

@mrocklin

mrocklin commented Dec 2, 2019

Copy link
Copy Markdown
Member

Thanks! Please let me know if you get stuck. That whole code path is relatively new.

@VibhuJawa

VibhuJawa commented Dec 2, 2019 •

Copy link
Copy Markdown
Contributor

FWIW, I feel the optimization pass will be super helpful across a bunch of more work-flows.

In a lot of workflows, I use column selection to work around it but a general solution will go a long way in simplifying things for us. (Please suggest if a better way is there)

n_rows = 50_000_000
n_cols = 20
With drop
df = dask_cudf.from_cudf(cudf.DataFrame({str(i):np.ones(shape=n_rows) for i in range(0,n_cols)}),npartitions=2)

st = time.time()
df = df.drop(columns=['0'])
x = len(df)
et = time.time()
print(f" time = {et - st}")
del df
time = 0.19231152534484863
With column selection
df = dask_cudf.from_cudf(cudf.DataFrame({str(i):np.ones(shape=n_rows) for i in range(0,n_cols)}),npartitions=2)

st = time.time()
df = df[[str(i)for i in range(1,n_cols)]]
x = len(df)
et = time.time()
print(f" time = {et - st}")
del df
time = 0.0229644775390625

CC: @beckernick .

@mrocklin

mrocklin commented Dec 2, 2019

Copy link
Copy Markdown
Member

That's a really motivating example @VibhuJawa ! Thanks.

Yes, hopefully this helps other workloads. Additionally, I hope that after doing this in one case we build up enough familiarity that we can do it in other cases as they arise as well.

@rjzamora

rjzamora commented Dec 2, 2019

Copy link
Copy Markdown
Member

@mrocklin - When parsing the layers of the high-level graph, the drop operations seem to have the generic "subgraph_callable" name, rather than something with "drop" in it. Do you have any advice on the correct way to compare the underlying operation with the call we are looking for? I assume we don't want to be using the key here.

@mrocklin

mrocklin commented Dec 2, 2019

Copy link
Copy Markdown
Member

Blockwise layers fuse everything into a single subgraph callable. You will need to dive within this subgraph callable object and look at its task graph (called .dsk I think). Within this graph you're going to want to search for tasks that call M.drop, look at their dependencies to verify that it's safe to insert the inplace=True keyword, and then do so, returning a new Blockwise layer with the modified subgraph callable with the modified .dsk attribute.

I'd be more than happy to walk you through this over screenshare if you're interested.

@madsbk

madsbk commented Dec 3, 2019

Copy link
Copy Markdown
Contributor Author

Created an issue to track this issue: #5664

@madsbk madsbk closed this Dec 3, 2019
@madsbk
madsbk deleted the inplace_drop branch February 16, 2021 08:59
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