Skip to content

Commit

Permalink
Dispatch for e.g. nx.Graph(backend="cugraph")
Browse files Browse the repository at this point in the history
  • Loading branch information
eriknw committed Feb 14, 2025
1 parent 7c9c868 commit c006da2
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 14 deletions.
5 changes: 0 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ repos:
- flake8==7.1.1
- flake8-bugbear==24.12.12
- flake8-simplify==0.21.0
- repo: https://github.com/asottile/yesqa
rev: v1.5.0
hooks:
- id: yesqa
additional_dependencies: *flake8_dependencies
- repo: https://github.com/codespell-project/codespell
rev: v2.4.1
hooks:
Expand Down
6 changes: 6 additions & 0 deletions _nx_cugraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"descendants",
"descendants_at_distance",
"diamond_graph",
"digraph__new__",
"dijkstra_path",
"dijkstra_path_length",
"dodecahedral_graph",
Expand All @@ -94,6 +95,7 @@
"from_scipy_sparse_array",
"frucht_graph",
"generic_bfs_edges",
"graph__new__",
"has_path",
"heawood_graph",
"hits",
Expand Down Expand Up @@ -122,6 +124,8 @@
"louvain_communities",
"lowest_common_ancestor",
"moebius_kantor_graph",
"multidigraph__new__",
"multigraph__new__",
"node_connected_component",
"null_graph",
"number_connected_components",
Expand Down Expand Up @@ -340,6 +344,8 @@ def update_env_var(varname):
update_env_var("NETWORKX_AUTOMATIC_BACKENDS") # For NetworkX 3.2
# Automatically create nx-cugraph Graph from graph generators
update_env_var("NETWORKX_BACKEND_PRIORITY_GENERATORS")
# And for graph classes such as `nx.Graph()` for NetworkX >=3.5
update_env_var("NETWORKX_BACKEND_PRIORITY_CLASSES")
# Run default NetworkX implementation (in >=3.4) if not implemented by nx-cugraph
if (varname := "NETWORKX_FALLBACK_TO_NX") not in os.environ:
os.environ[varname] = "true"
Expand Down
22 changes: 19 additions & 3 deletions nx_cugraph/classes/digraph.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2023-2024, NVIDIA CORPORATION.
# Copyright (c) 2023-2025, 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
Expand All @@ -25,8 +25,8 @@

import nx_cugraph as nxcg

from ..utils import index_dtype
from .graph import CudaGraph, Graph
from ..utils import index_dtype, networkx_algorithm
from .graph import CudaGraph, Graph, _GraphCache

if TYPE_CHECKING: # pragma: no cover
from nx_cugraph.typing import AttrKey
Expand Down Expand Up @@ -106,6 +106,22 @@ def to_cudagraph_class(cls) -> type[CudaDiGraph]:
def to_networkx_class(cls) -> type[nx.DiGraph]:
return nx.DiGraph

@networkx_algorithm(name="digraph__new__", version_added="25.04")
def __new__(cls, incoming_graph_data=None, **attr):
return object.__new__(DiGraph)

@__new__._can_run
def _(cls, incoming_graph_data=None, **attr): # noqa: N805
if cls not in {nx.DiGraph, DiGraph}:
return "Unknown subclasses of nx.DiGraph are not supported."
return True

del _

def __init__(self, incoming_graph_data=None, **attr):
super().__init__(incoming_graph_data, **attr)
self.__networkx_cache__ = _GraphCache(self)

##########################
# Networkx graph methods #
##########################
Expand Down
14 changes: 13 additions & 1 deletion nx_cugraph/classes/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import nx_cugraph as nxcg
from nx_cugraph import _nxver

from ..utils import index_dtype
from ..utils import index_dtype, networkx_algorithm

if TYPE_CHECKING: # pragma: no cover
from collections.abc import Iterable, Iterator
Expand Down Expand Up @@ -291,6 +291,18 @@ def to_networkx_class(cls) -> type[nx.Graph]:
def to_undirected_class(cls) -> type[Graph]:
return Graph

@networkx_algorithm(name="graph__new__", version_added="25.04")
def __new__(cls, incoming_graph_data=None, **attr):
return object.__new__(Graph)

@__new__._can_run
def _(cls, incoming_graph_data=None, **attr): # noqa: N805
if cls not in {nx.Graph, Graph}:
return "Unknown subclasses of nx.Graph are not supported."
return True

del _

def __init__(self, incoming_graph_data=None, **attr):
super().__init__(incoming_graph_data, **attr)
self.__networkx_cache__ = _GraphCache(self)
Expand Down
21 changes: 19 additions & 2 deletions nx_cugraph/classes/multidigraph.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2023-2024, NVIDIA CORPORATION.
# Copyright (c) 2023-2025, 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
Expand All @@ -16,8 +16,9 @@

import nx_cugraph as nxcg

from ..utils import networkx_algorithm
from .digraph import CudaDiGraph, DiGraph
from .graph import Graph
from .graph import Graph, _GraphCache
from .multigraph import CudaMultiGraph, MultiGraph

__all__ = ["CudaMultiDiGraph", "MultiDiGraph"]
Expand Down Expand Up @@ -51,6 +52,22 @@ def to_cudagraph_class(cls) -> type[CudaMultiDiGraph]:
def to_networkx_class(cls) -> type[nx.MultiDiGraph]:
return nx.MultiDiGraph

@networkx_algorithm(name="multidigraph__new__", version_added="25.04")
def __new__(cls, incoming_graph_data=None, multigraph_input=None, **attr):
return object.__new__(MultiDiGraph)

@__new__._can_run
def _(cls, incoming_graph_data=None, multigraph_input=None, **attr): # noqa: N805
if cls not in {nx.MultiDiGraph, MultiDiGraph}:
return "Unknown subclasses of nx.MultiDiGraph are not supported."
return True

del _

def __init__(self, incoming_graph_data=None, multigraph_input=None, **attr):
super().__init__(incoming_graph_data, multigraph_input, **attr)
self.__networkx_cache__ = _GraphCache(self)

##########################
# Networkx graph methods #
##########################
Expand Down
16 changes: 14 additions & 2 deletions nx_cugraph/classes/multigraph.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2023-2024, NVIDIA CORPORATION.
# Copyright (c) 2023-2025, 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
Expand All @@ -21,7 +21,7 @@

import nx_cugraph as nxcg

from ..utils import index_dtype
from ..utils import index_dtype, networkx_algorithm
from .graph import CudaGraph, Graph, _GraphCache

if TYPE_CHECKING:
Expand Down Expand Up @@ -73,6 +73,18 @@ def to_networkx_class(cls) -> type[nx.MultiGraph]:
def to_undirected_class(cls) -> type[MultiGraph]:
return MultiGraph

@networkx_algorithm(name="multigraph__new__", version_added="25.04")
def __new__(cls, incoming_graph_data=None, multigraph_input=None, **attr):
return object.__new__(MultiGraph)

@__new__._can_run
def _(cls, incoming_graph_data=None, multigraph_input=None, **attr): # noqa: N805
if cls not in {nx.MultiGraph, MultiGraph}:
return "Unknown subclasses of nx.MultiGraph are not supported."
return True

del _

def __init__(self, incoming_graph_data=None, multigraph_input=None, **attr):
super().__init__(incoming_graph_data, multigraph_input, **attr)
self.__networkx_cache__ = _GraphCache(self)
Expand Down
5 changes: 4 additions & 1 deletion scripts/update_readme.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# Copyright (c) 2024, NVIDIA CORPORATION.
# Copyright (c) 2024-2025, 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
Expand Down Expand Up @@ -110,6 +110,9 @@ def main(readme_file, objects_filename):
def get_payload(info, **kwargs):
path = "networkx." + info.networkx_path
subpath, name = path.rsplit(".", 1)
if "__" in name:
# Don't include e.g. Graph.__new__
return None
# Many objects are referred to in modules above where they are defined.
while subpath:
path = f"{subpath}.{name}"
Expand Down

0 comments on commit c006da2

Please sign in to comment.