Skip to content

Add "networkx" backend for dispatching - #7496

Closed
eriknw wants to merge 12 commits into
networkx:mainfrom
eriknw:networkx_backend
Closed

eriknw wants to merge 12 commits into
networkx:mainfrom
eriknw:networkx_backend

Conversation

@eriknw

@eriknw eriknw commented Jun 13, 2024 •

Copy link
Copy Markdown
Contributor

backend="networkx" is now valid when calling functions, which will automatically convert backend graphs to networkx graphs.

"networkx" may also be given in nx.config.backend_priority even though this is kind of silly right now (it's silly b/c it is equivalent to deleting everything to the right of "networkx" and "networkx" from the config). If "networkx" is a valid (if still special) backend name, then it's reasonable to support it in nx.config.backend_priority and allowing it to be used may be a useful pattern in the future.

I'm not sure whether to include "networkx" in nx.config.backends.

I also updated some error messages and variable names.

I tested this locally via

Details
import networkx as nx
import graphblas_algorithms as ga

G = nx.complete_graph(3)
rv1 = nx.pagerank(G, backend="networkx")
rv2 = nx.pagerank(G, backend="graphblas")
rv3 = nx.pagerank(G)
assert rv1 == rv2 == rv3

Ggb = ga.Graph.from_networkx(G)
rv4 = nx.pagerank(Ggb, backend="networkx")
rv5 = nx.pagerank(Ggb, backend="graphblas")
rv6 = nx.pagerank(Ggb)
assert rv1 == rv4 == rv5 == rv6

nx.config.backend_priority = ["networkx"]
rv4 = nx.pagerank(Ggb, backend="networkx")
rv5 = nx.pagerank(Ggb, backend="graphblas")
rv6 = nx.pagerank(Ggb)
assert rv1 == rv4 == rv5 == rv6

but should probably add a test or two. Oops. Test and doc added.

CC @rlratzel

This also enables automatically converting backend graphs to networkx graphs
@eriknw eriknw added type: Enhancements Dispatching Related to dispatching and backend support labels Jun 13, 2024

@Schefflera-Arboricola Schefflera-Arboricola left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't backend=None makes more sense than backend="networkx"?

It seems more intuitive to use backend=None instead of backend="networkx" because NetworkX is the primary/main library and not a backend in the traditional sense. Using None as the default value can clearly indicate that the main NetworkX implementation will be used. But, having "networkx" instead of None in the backend_priority list does make more sense, but as you mentioned above, including "networkx" in the nx.config.backend_priority list essentially truncates the list, making everything to the right of "networkx" irrelevant. So, why even allow it to be added in the middle? A more elegant alternative to handle this could be to have a flag like fallback_to_nx(I'm not sure if currently this can be used with backend_priority) that will indicate if a user wants to fallback to networkx's implementation or get a NetworkXNotImplemented error at the end of trying all backends in the backend_priority list. And, again "networkx" is not a backend so we probably shouldn't allow it to be passed in the "backend"_priority list but instead have a fallback option. But, I would really like to understand if there are any specific use cases that motivate this change.

Thank you :)

@eriknw

eriknw commented Jun 13, 2024

Copy link
Copy Markdown
Contributor Author

Thanks for the comment and questions @Schefflera-Arboricola! Several great discussion points.

Doesn't backend=None makes more sense than backend="networkx"?

Using backend=None is more-or-less what we've used up to this point, but it is limited. Consider you have a graph Gspam from backend "spam". How should we interpret the following cases?

nx.pagerank(Gspam)  # 1
nx.pagerank(Gspam, backend="networkx")  # 2
nx.pagerank(Gspam, backend=None)  # 3
nx.pagerank(Gspam, backend="eggs")  # 4

#1 will use the "spam" backend.
#2 will be use networkx implementation (and convert to--and cache if enabled!--a networkx graph)
#3 is unclear. What is meant? What should we do? To me, backend=None means the same as #1, which basically means "do default dispatching" (which is what happens).
#4 does not yet work, but the meaning is clear: convert from "spam" backend to "eggs" backend and cook with eggs.

Hence, I think "networkx" makes a lot of sense as a backend name even if it may occasionally be special. Right now it's special because we know how to convert backend graphs to it.

You are astute to bring up fallback behavior. This deserves careful consideration. Currently, fallback behavior only exists when testing with a backend. We could introduce fallback behavior in this or a subsequent PR by falling back to NetworkX if "networkx" is in backend_priority. This implies the possibility of falling back to other backends once we enable backend-to-backend conversion. This more general approach "feels" right to me, but it's not fully designed, and I'm open to other ideas. One of the main reasons we don't expose the current fallback_to_nx option and behavior to end users is we (@jim22k is part of "we" here--he helped design this when we began) is we weren't sure what the target UX is and we wanted to have fallback be more generic if possible.

What would people like to see? I can add "fallback to networkx" behavior to this PR since it's related, or I can wait to add it to a new PR.

@eriknw

eriknw commented Jul 1, 2024

Copy link
Copy Markdown
Contributor Author

Update doc and added a test 😄

if not backends:
# Fast path if no backends are installed
if backend is not None:
if backend is not None and backend != "networkx":

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MridulS, here is the fix for the issue you called out today. And to reiterate: if no backends are installed (not counting networkx), the fast path of lines 776-779 is still taken.

eriknw added a commit to eriknw/networkx that referenced this pull request Aug 22, 2024
@eriknw
eriknw marked this pull request as draft August 25, 2024 16:59
@eriknw

eriknw commented Aug 25, 2024

Copy link
Copy Markdown
Contributor Author

I converted this to Draft to prevent merging. #7585 has changes in this PR along with a refactor that would make merging the two PRs challenging.

We can still discuss the changes in functional, user-facing terms in this PR, but #7585 is now the preferred implementation (imho) and PR.

@eriknw

eriknw commented Oct 1, 2024

Copy link
Copy Markdown
Contributor Author

Closing. Part of and superseded by #7585

@eriknw eriknw closed this Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Dispatching Related to dispatching and backend support type: Enhancements

Development

Successfully merging this pull request may close these issues.

3 participants