Skip to content

Add graph optimization pass for in-place drop - #5669

Closed
rjzamora wants to merge 9 commits into
dask:masterfrom
rjzamora:drop-opt
Closed

rjzamora wants to merge 9 commits into
dask:masterfrom
rjzamora:drop-opt

Conversation

@rjzamora

@rjzamora rjzamora commented Dec 4, 2019

Copy link
Copy Markdown
Member

Closes #5664

Adds optimize_drop function to add inplace=True to tasks with M.drop. The inplace change is not performed in the case that the corresponding tasks has no dependencies within the Blockwise subgraph (to avoid modifying input data).

Note: The requirement to avoid modifying tasks that depend on subgraph inputs is currently skipping the optimization for merge operations (the original motivation for the in-place drop). For this reason, I added a force argument to effectively ignore this criteria. For example, the optimization can be used by adding these lines to the dask-cuda merge benchmark (just before the ddf_join.persist() call):

# Add graph optimizations
ddf_join.dask = optimize_drop(
    optimize_blockwise(ddf_join.dask), force=True
)

Before Optimization:

Merge benchmark
--------------------------
Chunk-size  | 100000000
Frac-match  | 0.3
Ignore-size | 1.05 MB
Protocol    | ucx
Device(s)   | 0,1
==========================
Total time  | 2.40 s
==========================
(w1,w2)     | 25% 50% 75% (total nbytes)
--------------------------
(00,01)     | 9.72 GB/s 15.72 GB/s 16.05 GB/s (5.20 GB)
(01,00)     | 9.20 GB/s 14.69 GB/s 16.87 GB/s (5.20 GB)

After Optimization:

Merge benchmark
--------------------------
Chunk-size  | 100000000
Frac-match  | 0.3
Ignore-size | 1.05 MB
Protocol    | ucx
Device(s)   | 0,1
==========================
Total time  | 970.67 ms
==========================
(w1,w2)     | 25% 50% 75% (total nbytes)
--------------------------
(00,01)     | 15.98 GB/s 16.02 GB/s 17.64 GB/s (5.20 GB)
(01,00)     | 15.31 GB/s 15.62 GB/s 17.50 GB/s (5.20 GB)

This performance difference is a clear motivation to find a way to capture the "merge" situation (using somethin better/safer than the force=True hack/workaround).

  • Tests added / passed
  • Passes black dask / flake8 dask

@mrocklin

mrocklin commented Dec 4, 2019

Copy link
Copy Markdown
Member

Ah, so in this case the optimization doesn't trigger because the layer just before the drop isn't a blockwise operation. Is that right?

@mrocklin

mrocklin commented Dec 4, 2019

Copy link
Copy Markdown
Member

Hrm, as an alternative, we might try the following:

  1. We keep on with this approach in general, but not targetting the merge case in particular. It has general utility.
  2. For the merge case we try to use df = df[[c for c in df.columns if c != "_partitions"]]. I think that this doesn't incur a copy and should have the same effect (although someone should probably verify this). This would be a separate PR.

We can also look at the shuffle code, and see if the layers of the join just preceding the drop call can be made into blockwise. After taking a brief look though it seems like they are too complex.

Thoughts on that plan?

@TomAugspurger

Copy link
Copy Markdown
Member

cc @eriknw.

@TomAugspurger

TomAugspurger commented Dec 4, 2019 •

Copy link
Copy Markdown
Member

df = df[[c for c in df.columns if c != "_partitions"]].

I think this may still trigger a copy. I believe that triggers fancy indexing, which always copies?

In [69]: a = np.random.random((100, 4))

In [70]: b = np.random.random((100, 4)).astype(int)

In [71]: df = pd.concat([pd.DataFrame(a), pd.DataFrame(b)], axis=1, ignore_index=True, copy=False)

In [72]: df._data.blocks[0].values.base is a
Out[72]: True

In [73]: np.may_share_memory(df[[0, 1, 2, 3, 4, 5]]._data.blocks[0].values, a)
Out[73]: False

@rjzamora

rjzamora commented Dec 4, 2019

Copy link
Copy Markdown
Member Author

Ah, so in this case the optimization doesn't trigger because the layer just before the drop isn't a blockwise operation. Is that right?

This is where my understanding of HighLevelGraph and Blockwise is a bit fuzzy, and this question makes me believe I may have made some wrong assumptions. Currently, the optimization pass assumes that a drop call will correspond to a single task within the sub-graph of a larger Blockwise operation. When a drop operation is detected, the inplace argument is only added iff that corresponding task has a dependency. In the merge case, the optimization is skipped, because get_deps(<dask-graph-of-Blockwise-layer>)[0][<key-of-drop-task>] returns set()

@mrocklin

mrocklin commented Dec 4, 2019

Copy link
Copy Markdown
Member

Right, that means that the drop call is at the very beginning of the Blockwise set of calls. It directly affects the input. In this case we can't safely add the inplace keyword in general.

@mrocklin

mrocklin commented Dec 4, 2019

Copy link
Copy Markdown
Member

@TomAugspurger is there a way in pandas to return a new dataframe with a column missing that doesn't copy the underlying data?

@rjzamora

rjzamora commented Dec 4, 2019

Copy link
Copy Markdown
Member Author

I think this may still trigger a copy. I believe that triggers fancy indexing, which always copies?

I believe that @VibhuJawa used this approach to get a speedup, see his comment. Is there any reason this would improve performance if it wasn't avoiding a copy?

@mrocklin

mrocklin commented Dec 4, 2019

Copy link
Copy Markdown
Member

(we probably should have asked you about this a while ago :) )

@mrocklin

mrocklin commented Dec 4, 2019

Copy link
Copy Markdown
Member

I believe that @VibhuJawa used this approach to get a speedup, see his comment. Is there any reason this would improve performance if it wasn't avoiding a copy?

Perhaps cudf and pandas have different behaviors here?

@TomAugspurger

Copy link
Copy Markdown
Member

@TomAugspurger is there a way in pandas to return a new dataframe with a column missing that doesn't copy the underlying data?

Today, something like del df[col] or df.drop(columns=col, inplace=True) is probably the best bet, but that can't guarantee that it's not a copy.

I believe that whether a copy needs to be made depends on whether the column is part of a larger Block. IIRC, the motivating case here was dropping a column _partitions which we added as part of a shuffle? With a bit of work, we could probably trick pandas into not consolidating that newly added column.

It's quite likely that cudf doesn't have the same limitation.

@mrocklin

mrocklin commented Dec 4, 2019

Copy link
Copy Markdown
Member

Well, it would be nice if this improved performance on the Pandas side as well. cuDF is a bit more flexible than Pandas, so I'd prefer to find a solution that works well with Pandas and then ensure that that also works with cudf.

Perhaps what I want is a df.copy(shallow=True). Then I could do something like ...

df = df.copy(shallow=True)
del df["_partitions"]

This doesn't appear to exist though.

I would hope that even if we did need to copy the block that we would only need to copy that one block and not all of the others. Also, my hope is that the _partitions column will be an odd type, like uint16 and so might be able to avoid colliding with too many other columns.

@rjzamora

rjzamora commented Dec 4, 2019

Copy link
Copy Markdown
Member Author

Well, it would be nice if this improved performance on the Pandas side as well. cuDF is a bit more flexible than Pandas, so I'd prefer to find a solution that works well with Pandas and then ensure that that also works with cudf.

Is there good reason to avoid using

return df3[[c for c in df.columns if c != "_partitions"]]

ranther than

del df3["_partitions"]
return df3

just because it improves cudf-based performance and not pandas-based performance? I understand that we want to improve both cases in the long run, but I am not understanding if there is a good reason to avoid this change for now.

@mrocklin

mrocklin commented Dec 4, 2019

Copy link
Copy Markdown
Member

I'm fine with that personally. It isn't significantly more complex and doesn't degrade performance elsewhere. Want to throw up a PR?

@rjzamora

rjzamora commented Dec 4, 2019

Copy link
Copy Markdown
Member Author

I'm fine with that personally. It isn't significantly more complex and doesn't degrade performance elsewhere. Want to throw up a PR?

Sounds good to me, but I most likely won't get to it until later tonight.

@dhirschfeld

Copy link
Copy Markdown

Perhaps what I want is a df.copy(shallow=True)

In [56]: df = pd.DataFrame(randn(10,3), columns=list('ABC'))

In [57]: df
Out[57]: 
          A         B         C
0  0.114722  1.558930 -2.126745
1  0.766587  0.046552  1.090657
2  0.702984  0.344245 -0.236198
3  0.682041 -0.047099  0.091260
4  0.347412 -0.679305  0.915347
5  0.127372  0.339934 -0.590698
6 -1.008189 -0.037308 -0.158385
7  0.237671  0.589957 -1.010068
8 -1.426825 -1.557856  0.389637
9  0.285834 -1.140209 -0.350228

In [58]: df2 = df.copy(deep=False)

In [59]: del df2['B']

In [60]: df2
Out[60]: 
          A         C
0  0.114722 -2.126745
1  0.766587  1.090657
2  0.702984 -0.236198
3  0.682041  0.091260
4  0.347412  0.915347
5  0.127372 -0.590698
6 -1.008189 -0.158385
7  0.237671 -1.010068
8 -1.426825  0.389637
9  0.285834 -0.350228

In [61]: df
Out[61]: 
          A         B         C
0  0.114722  1.558930 -2.126745
1  0.766587  0.046552  1.090657
2  0.702984  0.344245 -0.236198
3  0.682041 -0.047099  0.091260
4  0.347412 -0.679305  0.915347
5  0.127372  0.339934 -0.590698
6 -1.008189 -0.037308 -0.158385
7  0.237671  0.589957 -1.010068
8 -1.426825 -1.557856  0.389637
9  0.285834 -1.140209 -0.350228

@mrocklin

mrocklin commented Dec 5, 2019

Copy link
Copy Markdown
Member

Indeed ...

In [8]: df = pd.DataFrame({"x": np.arange(10000000)})

In [9]: %timeit df.copy()
18.4 ms ± 278 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [10]: %timeit df.copy(deep=False)
34.3 µs ± 1.44 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

@mrocklin

mrocklin commented Dec 5, 2019

Copy link
Copy Markdown
Member

So it seems like we should maybe change the definition of dd.DataFrame.drop to call a function that calls copy(deep=False) and then del df[columns]?

@rjzamora

rjzamora commented Dec 5, 2019

Copy link
Copy Markdown
Member Author

I'm thinking we can close this in favor of #5675?

@rjzamora rjzamora closed this Dec 5, 2019
@mrocklin

mrocklin commented Dec 5, 2019

Copy link
Copy Markdown
Member

This is still an interesting approach that I suspect will be useful in the future. @eriknw , I suspect that you in particular might be interested in this. If so, is there a way in which you would like this to be preserved?

@eriknw

eriknw commented Dec 6, 2019

Copy link
Copy Markdown
Member

Thanks for the ping! This would be a good optimization to perform (when applicable).

@rjzamora
rjzamora deleted the drop-opt branch May 21, 2024 00:04
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.

[FEA] In-place DataFrame.drops()

5 participants