-
Notifications
You must be signed in to change notification settings - Fork 309
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
Further optimize from_pandas_edgelist
with cudf
#4528
Merged
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e56ca08
Further optimize `from_pandas_edgelist` with cudf
eriknw 6261969
Oops fix copyright (and install/run pre-commit)
eriknw 8676c9b
Merge branch 'branch-24.08' into df_avoid_copies
eriknw 768b44d
Add basic tests that smoked out a couple issues :)
eriknw 530b482
Test `create_using` too
eriknw 20ad596
Merge branch 'branch-24.08' into df_avoid_copies
eriknw 2b18d39
Merge branch 'branch-24.08' into df_avoid_copies
eriknw 82d076d
Merge branch 'branch-24.08' into df_avoid_copies
eriknw f397d7f
Merge branch 'branch-24.08' into df_avoid_copies
eriknw eb981ef
Merge branch 'df_avoid_copies' of github.com:eriknw/cugraph into df_a…
eriknw 3b047ec
Add comments about not sharing ownership of arrays. NetworkX doesn't …
eriknw 7a3f74c
Merge branch 'branch-24.08' into df_avoid_copies
nv-rliu 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import networkx as nx | ||
import pandas as pd | ||
import pytest | ||
|
||
import nx_cugraph as nxcg | ||
from nx_cugraph.utils import _cp_iscopied_asarray | ||
|
||
try: | ||
import cudf | ||
except ModuleNotFoundError: | ||
cudf = None | ||
|
||
|
||
DATA = [ | ||
{"source": [0, 1], "target": [1, 2]}, # nodes are 0, 1, 2 | ||
{"source": [0, 1], "target": [1, 3]}, # nodes are 0, 1, 3 (need renumbered!) | ||
{"source": ["a", "b"], "target": ["b", "c"]}, # nodes are 'a', 'b', 'c' | ||
] | ||
CREATE_USING = [nx.Graph, nx.DiGraph, nx.MultiGraph, nx.MultiDiGraph] | ||
|
||
|
||
@pytest.mark.skipif("not cudf") | ||
@pytest.mark.parametrize("data", DATA) | ||
@pytest.mark.parametrize("create_using", CREATE_USING) | ||
def test_from_cudf_edgelist(data, create_using): | ||
df = cudf.DataFrame(data) | ||
nxcg.from_pandas_edgelist(df, create_using=create_using) # Basic smoke test | ||
source = df["source"] | ||
if source.dtype == int: | ||
is_copied, src_array = _cp_iscopied_asarray(source) | ||
assert is_copied is False | ||
is_copied, src_array = _cp_iscopied_asarray(source.to_cupy()) | ||
assert is_copied is False | ||
is_copied, src_array = _cp_iscopied_asarray(source, orig_object=source) | ||
assert is_copied is False | ||
is_copied, src_array = _cp_iscopied_asarray( | ||
source.to_cupy(), orig_object=source | ||
) | ||
assert is_copied is False | ||
# to numpy | ||
is_copied, src_array = _cp_iscopied_asarray(source.to_numpy()) | ||
assert is_copied is True | ||
is_copied, src_array = _cp_iscopied_asarray( | ||
source.to_numpy(), orig_object=source | ||
) | ||
assert is_copied is True | ||
else: | ||
with pytest.raises(TypeError): | ||
_cp_iscopied_asarray(source) | ||
with pytest.raises(TypeError): | ||
_cp_iscopied_asarray(source.to_cupy()) | ||
with pytest.raises(ValueError, match="Unsupported dtype"): | ||
_cp_iscopied_asarray(source.to_numpy()) | ||
with pytest.raises(ValueError, match="Unsupported dtype"): | ||
_cp_iscopied_asarray(source.to_numpy(), orig_object=source) | ||
|
||
|
||
@pytest.mark.parametrize("data", DATA) | ||
@pytest.mark.parametrize("create_using", CREATE_USING) | ||
def test_from_pandas_edgelist(data, create_using): | ||
df = pd.DataFrame(data) | ||
nxcg.from_pandas_edgelist(df, create_using=create_using) # Basic smoke test | ||
source = df["source"] | ||
if source.dtype == int: | ||
is_copied, src_array = _cp_iscopied_asarray(source) | ||
assert is_copied is True | ||
is_copied, src_array = _cp_iscopied_asarray(source, orig_object=source) | ||
assert is_copied is True | ||
is_copied, src_array = _cp_iscopied_asarray(source.to_numpy()) | ||
assert is_copied is True | ||
is_copied, src_array = _cp_iscopied_asarray( | ||
source.to_numpy(), orig_object=source | ||
) | ||
assert is_copied is True |
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.
Maybe I'm missing something obvious, but couldn't the same behavior be achieved by just doing the following?
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 you meant:
which should work as expected.
I found if-else branches more clear to use here, and easy for us to do given that we already have the booleans around.
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.
And once again, if you didn't catch it from one of my previous comments in previous PRs, numpy 2 changed semantics:
https://numpy.org/doc/stable/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword
so I think it's okay to be extra clear here.
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.
Thanks for the explanation, but no, actually that snippet wasn't what I was thinking. But after re-reading again I think there's some necessary side effects that I was missing, but I'd rather not assume I know what they are. Let's chat offline and I'll update the comment afterwards.
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 my questioning of why not just use
copy=False
and therefore why_cp_iscopied_asarray
is needed at all is because I didn't initially realize this function was intentionally making copies of the incoming dataframe series for the graph to own. That makes sense and I confirmed this with Erik offline (although we should probably think about a future improvement to prevent yet another copy happening when the PLC graph is made, but that can be for later) so my only request is a comment mentioning that. Perhaps something like this here:but maybe something even shorter and better, maybe just in the docstring for the function could simply be
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.
Thanks, code comments added. NetworkX doesn't share ownership with input objects, so I figure neither should we.