Skip to content
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

nx-cugraph: use coverage to ensure all algorithms were run #4108

Merged
merged 18 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions ci/test_python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,33 @@ popd

rapids-logger "pytest networkx using nx-cugraph backend"
pushd python/nx-cugraph
# Use editable install to make coverage work
pip install -e . --no-deps
./run_nx_tests.sh
# run_nx_tests.sh outputs coverage data, so check that total coverage is >0.0%
# in case nx-cugraph failed to load but fallback mode allowed the run to pass.
_coverage=$(coverage report|grep "^TOTAL")
echo "nx-cugraph coverage from networkx tests: $_coverage"
echo $_coverage | awk '{ if ($NF == "0.0%") exit 1 }'
# Ensure all algorithms were called by comparing covered lines to function lines.
# Run our tests again (they're fast enough) to add their coverage, then create coverage.json
pytest \
--pyargs nx_cugraph \
--config-file=./pyproject.toml \
--cov-config=./pyproject.toml \
--cov=nx_cugraph \
--cov-append \
--cov-report=
coverage report \
--include="*/nx_cugraph/algorithms/*" \
--omit=__init__.py \
--show-missing \
--rcfile=./pyproject.toml
coverage json --rcfile=./pyproject.toml
python -m nx_cugraph.tests.ensure_algos_covered
# Exercise (and show results of) scripts that show implemented networkx algorithms
python -m nx_cugraph.scripts.print_tree --dispatch-name --plc --incomplete --different
python -m nx_cugraph.scripts.print_table
popd

rapids-logger "pytest cugraph-service (single GPU)"
Expand Down
2 changes: 1 addition & 1 deletion python/nx-cugraph/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repos:
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.15
rev: v0.16
hooks:
- id: validate-pyproject
name: Validate pyproject.toml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def generic_bfs_edges(G, source, neighbors=None, depth_limit=None, sort_neighbor
raise NotImplementedError(
"sort_neighbors argument in generic_bfs_edges is not currently supported"
)
return bfs_edges(source, depth_limit=depth_limit)
return bfs_edges(G, source, depth_limit=depth_limit)


@generic_bfs_edges._can_run
Expand Down
2 changes: 1 addition & 1 deletion python/nx-cugraph/nx_cugraph/scripts/print_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def main(path_to_info=None, *, file=sys.stdout):
if path_to_info is None:
path_to_info = get_path_to_info(version_added_sep=".")
lines = ["networkx_path,dispatch_name,version_added,plc,is_incomplete,is_different"]
lines.extend(",".join(info) for info in path_to_info.values())
lines.extend(",".join(map(str, info)) for info in path_to_info.values())
text = "\n".join(lines)
print(text, file=file)
return text
Expand Down
84 changes: 84 additions & 0 deletions python/nx-cugraph/nx_cugraph/tests/ensure_algos_covered.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# 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.
"""Ensure that all functions wrapped by @networkx_algorithm were called.

This file is run by CI and should not normally be run manually.
"""
import inspect
import json
from pathlib import Path

from nx_cugraph.interface import BackendInterface
from nx_cugraph.utils import networkx_algorithm

with Path("coverage.json").open() as f:
coverage = json.load(f)

filenames_to_executed_lines = {
"nx_cugraph/"
+ filename.rsplit("nx_cugraph/", 1)[-1]: set(coverage_info["executed_lines"])
for filename, coverage_info in coverage["files"].items()
}


def unwrap(func):
while hasattr(func, "__wrapped__"):
func = func.__wrapped__
return func


def get_func_filename(func):
return "nx_cugraph" + inspect.getfile(unwrap(func)).rsplit("nx_cugraph", 1)[-1]


def get_func_linenos(func):
lines, lineno = inspect.getsourcelines(unwrap(func))
for i, line in enumerate(lines, lineno):
if ":\n" in line:
return set(range(i + 1, lineno + len(lines)))
raise RuntimeError(f"Could not determine line numbers for function {func}")


def has_any_coverage(func):
return bool(
filenames_to_executed_lines[get_func_filename(func)] & get_func_linenos(func)
)


def main():
no_coverage = set()
for attr, func in vars(BackendInterface).items():
if not isinstance(func, networkx_algorithm):
continue
if not has_any_coverage(func):
no_coverage.add(attr)
if no_coverage:
msg = "The following algorithms have no coverage: " + ", ".join(
sorted(no_coverage)
)
# Create a border of "!"
msg = (
"\n\n"
+ "!" * (len(msg) + 6)
+ "\n!! "
+ msg
+ " !!\n"
+ "!" * (len(msg) + 6)
+ "\n"
)
raise AssertionError(msg)
print("\nSuccess: coverage determined all algorithms were called!\n")


if __name__ == "__main__":
main()
33 changes: 33 additions & 0 deletions python/nx-cugraph/nx_cugraph/tests/test_bfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 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 pytest
from packaging.version import parse

nxver = parse(nx.__version__)

if nxver.major == 3 and nxver.minor < 2:
pytest.skip("Need NetworkX >=3.2 to test clustering", allow_module_level=True)


def test_generic_bfs_edges():
# generic_bfs_edges currently isn't exercised by networkx tests
Gnx = nx.karate_club_graph()
Gcg = nx.karate_club_graph(backend="cugraph")
for depth_limit in (0, 1, 2):
for source in Gnx:
# Some ordering is arbitrary, so I think there's a chance
# this test may fail if networkx or nx-cugraph changes.
nx_result = nx.generic_bfs_edges(Gnx, source, depth_limit=depth_limit)
cg_result = nx.generic_bfs_edges(Gcg, source, depth_limit=depth_limit)
assert sorted(nx_result) == sorted(cg_result), (source, depth_limit)
14 changes: 10 additions & 4 deletions python/nx-cugraph/run_nx_tests.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
#
# Copyright (c) 2023, NVIDIA CORPORATION.
# Copyright (c) 2023-2024, NVIDIA CORPORATION.
#
# NETWORKX_GRAPH_CONVERT=cugraph
# Used by networkx versions 3.0 and 3.1
Expand Down Expand Up @@ -30,7 +30,13 @@ NETWORKX_TEST_BACKEND=cugraph \
NETWORKX_FALLBACK_TO_NX=True \
pytest \
--pyargs networkx \
--cov=nx_cugraph.algorithms \
--cov-report term-missing \
--no-cov-on-fail \
--config-file=$(dirname $0)/pyproject.toml \
--cov-config=$(dirname $0)/pyproject.toml \
--cov=nx_cugraph \
--cov-report= \
"$@"
coverage report \
--include="*/nx_cugraph/algorithms/*" \
--omit=__init__.py \
--show-missing \
--rcfile=$(dirname $0)/pyproject.toml
Loading