Skip to content

Allow dispatch machinery to fall back to networkx - #7585

Merged
dschult merged 90 commits into
networkx:mainfrom
eriknw:fallback_to_networkx
Oct 1, 2024
Merged

dschult merged 90 commits into
networkx:mainfrom
eriknw:fallback_to_networkx

Conversation

@eriknw

@eriknw eriknw commented Jul 31, 2024

Copy link
Copy Markdown
Contributor

Allow falling back to NetworkX by converting backend graphs to nx graphs. This is enabled by adding "networkx" to nx.config.backend_priority.

This PR builds off of #7496, #7499, #7502 (WIP), and #7568. I have done a little bit of refactoring, but I would like to do more refactoring to simplify the code after more PRs are merged. Feel free to review and merge the four other PRs separately, or to review this PR as a whole.

I am working on a notebook that exercises many of the branches and log/error messages, and I bet I/we will continue to refine the messages. Perhaps we could turn this into an example that gets run in CI. Hopefully the log/error messages can help make the dispatch code easier to follow (if one is comfortable reading all that text!).

CC @rlratzel

eriknw and others added 30 commits June 13, 2024 14:21
This also enables automatically converting backend graphs to networkx graphs
`can_run` and `should_run` may not be able to capture everything,
so might as well try the next backend.

@eriknw eriknw left a comment

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.

Thanks everybody for the discussions and sharing your ideas! I'm glad this provoked some thought. Hearing your suggestions gives me hope we may be able to find a suitable path forward to continue improving dispatching beyond networkx v3.4.

I would like to focus on being practical right now and try to get this PR reviewed, accepted, and merged. I've spent a lot of time using this branch the last few weeks and have continued to refine log messages and fix bugs. I think this is a very important PR to get in.

Please let me know if there is more I can do to help make this more reviewable. As it is a combination of 4 other PRs and a refactor, it has gotten larger than is typically recommended for an PR on an open source package with volunteer reviewers. I will do what I can to help.

I am working on adding better ways to test the dispatch machinery, and I plan to port the example notebooks from the first comment as tests. These could be part of this PR or as a new PR. Either way, I plan to use them to demonstrate continuity and changes pre- and post- this PR.

I left a few comments to draw attention to a few important things.

Comment on lines +1081 to +1088
# The current behavior for functions that don't mutate input graphs:
#
# 1. If backend is specified by `backend=` keyword, use it (done above).
# 2. If input is from a backend other than "networkx", try to use it.
# - Note: if present, "networkx" graphs will be converted to the backend.
# 3. If input is from "networkx" (or no backend), try to use backends from
# `backend_priority` before running with the default "networkx" implementation.
# 4. If configured, "fall back" and run with the default "networkx" implementation.

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.

I added code comments to explain how dispatching works today. Items 1-3 are what we have come up with together over the last couple of years. This PR adds item 4: falling back to networkx. The goal of the refactor was to help clean things up and have more maintainable code.

Comment on lines +977 to +982
# The current behavior for functions that mutate input graphs:
#
# 1. If backend is specified by `backend=` keyword, use it (done above).
# 2. If inputs are from one backend, try to use it.
# 3. If all input graphs are instances of `nx.Graph`, then run with the
# default "networkx" implementation.

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.

Similarly, this describes how functions that mutate inputs behave today. This PR adds item 3, which is important to fully enable "zero code change" workflows.

Comment on lines +1059 to +1063
if config.fallback_to_nx or not graph_backend_names:
# Use "networkx" by default if there are no inputs from backends.
# For example, graph generators should probably return NetworkX graphs
# instead of raising NotImplementedError.
backend_fallback = ["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.

Note that we treat "networkx" as a special backend here by using it by default (instead of failing) for functions that create graphs (i.e., the not graph_backend_names clause). I think this is probably the expected behavior most of the time.

Comment thread networkx/utils/configs.py Outdated
Comment on lines +308 to +311
fallback_to_nx : bool
If True, then "fall back" and run with the default "networkx" implementation
for dispatchable functions not implemented by backends of input graphs.
Default is True.

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.

As was proposed and discussed, I added a nx.config.fallback_to_nx configuration that is enabled by default. I think it's okay if "networkx" is always a privileged backend since it's the primary library, so it may be reasonable to keep this config option around even if we generalize things further in the future.

Any suggestions for a better name?

@dschult dschult 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.

I think I have gone through the aspects of this. I was able to use the draft PR #7499 to get the ideas for that in a smaller setting. So thanks for that!

I like the refactoring in general and the updates on the config systems. New names for the difference val variables are minor but nicely helpful too.

I'm not sure I like the idea of automatically converting other backends to networkx graphs (which I think is the meaning of the phrase "fall back"... it's not just that we use networkx functions, its that we convert_and_run other graph objects to nx graphs and then run nx functions on them). But I'm still learning. What is the expected use-case here and how can we avoid surprises? It's a fair amount of extra cruft compared to just raising an exception and having the user replace G with nx.Graph(G).

I support backend-to-backend conversions, but I don't like the approach of automatically choosing which to which. It's too implicit. What are the advantages of automatic conversion over user-directed explicit conversion?

I do think we should allow the keyword arg backend="networkx". That is, this is the explicit way to say we want the original networkx functionality even if we have backend_priority set.

Can we update the Backends docs page to reflect these changes? For example, it would be nice to tell readers interested in details that backend_info holds as keys the backends that can be used. There are places that say backend_info is optional. Is that still true? (I think so) I guess I don't understand whether and why backend_info holds as keys the backend names instead of whereever it used to be... (which was backends?) We should also reproduce the Rules for choice of backend from comments in the code to the docs.

I'm beginning to think that type-based dispatching is pretty restrictive. In some sense, I'd like to see the code that is dispatched to be one choice and the object used by that code as a separate choice. So libraries could support more than one type (by converting and caching internally or by making code that works across types), and also more than one library can support a specific type (like a number of libraries which work directly with the nx graphs). But I haven't thought about how to implement that.

To finish up here with this comment:

  • what aspects of this do you see as needed for v3.4?
  • Do we need the conversion of other types to networkx graphs upon fallback? What does this feature enable?
  • Is this going to lock us into the five groups of priorities described in lines 1100-1107? It is very appealing to just have backend_priority, and go down that list and pick the first one that doesn't signal NotImplemented. Backend-to-backend conversion can still happen when the dispatched-to library can handle/convert the incoming graph type. And if it can't then return NotImplemented (or raise, or whatever signal works best).

Comment thread networkx/__init__.py Resolved
Comment thread networkx/utils/tests/test_backends.py Resolved
Comment thread networkx/utils/backends.py Resolved
Comment thread networkx/utils/backends.py Resolved
rapids-bot Bot pushed a commit to rapidsai/cugraph that referenced this pull request Sep 24, 2024
…workX Graph classes, needed for zero code change graph generators (#4629)

This is an alternative approach to #4558 for enabling GPU-accelerated NetworkX to "just work". It has similarities to #4558. I opted to make separate classes such as `ZeroGraph`, which I think makes for cleaner separation and gives us and users more control.

There are a few lingering TODOs and code comments to tidy up, but I don't think there are any show-stoppers. I have not updated methods (such as `number_of_nodes`) to optimistically try to use GPU if possible, b/c this is not strictly necessary, but we should update these soon.

I have run NetworkX tests with these classes using networkx/networkx#7585 and networkx/networkx#7600. We need the behavior in 7585, and 7600 is only useful for testing.

There are only 3 new failing tests, and 3 tests that hang (I'll run them overnight to see if they finish). Here's a test summary:
```
5548 passed, 24 skipped, 16 xfailed, 25 xpassed
```
Note that 25 tests that were failing now pass. I have not investigated test failures, xfails, or xpasses yet. I would like to add tests too.

We rely heavily on the networkx cache. I think this is preferred.

It is late for me. I will describe and show how and why this works later.

I opted for `zero=` and `ZeroGraph`, because I find them delightful! Renaming is trivial if other terms are preferred.

CC @quasiben

Authors:
  - Erik Welch (https://github.com/eriknw)

Approvers:
  - Bradley Dice (https://github.com/bdice)
  - Rick Ratzel (https://github.com/rlratzel)

URL: #4629
Also, update docstring and comments to try to make it more clear
Just run it in `backends.py`. Also, rename `val` to `graph` in caching functions.
@eriknw

eriknw commented Sep 30, 2024

Copy link
Copy Markdown
Contributor Author

I think I have gone through the aspects of this. I was able to use the draft PR #7499 to get the ideas for that in a smaller setting. So thanks for that!

You bet, thanks for taking a close look!

I like the refactoring in general and the updates on the config systems. New names for the difference val variables are minor but nicely helpful too.

👍. The PR has been updated with your suggestions.

I'm not sure I like the idea of automatically converting other backends to networkx graphs (which I think is the meaning of the phrase "fall back"... it's not just that we use networkx functions, its that we convert_and_run other graph objects to nx graphs and then run nx functions on them). But I'm still learning. What is the expected use-case here and how can we avoid surprises? It's a fair amount of extra cruft compared to just raising an exception and having the user replace G with nx.Graph(G).

As we recently discussed, I change the default of nx.config.fallback_to_nx to False. I also updated the config docstring to try to make it more clear. Falling back to networkx implementation allows nx-cugraph to achieve "zero code change" for virtually all of networkx, although this comes at the cost of creating the networkx graph data structures (_adj, etc.). Conversion is optional; we call the backend interface convert_to_nx with the function that it's being converted for, and the backend may choose to not convert if the graph is already duck-type compatible as a networkx graph (this is what nx-cugraph does).

I support backend-to-backend conversions, but I don't like the approach of automatically choosing which to which. It's too implicit. What are the advantages of automatic conversion over user-directed explicit conversion?

Gotcha. I think this is a conversation for later. This PR does not enable generic backend-to-backend conversions, and I agree it would be great to allow user-directed explicit conversions.

I do think we should allow the keyword arg backend="networkx". That is, this is the explicit way to say we want the original networkx functionality even if we have backend_priority set.

Great! I like it too.

Can we update the Backends docs page to reflect these changes? For example, it would be nice to tell readers interested in details that backend_info holds as keys the backends that can be used. There are places that say backend_info is optional. Is that still true? (I think so) I guess I don't understand whether and why backend_info holds as keys the backend names instead of whereever it used to be... (which was backends?) We should also reproduce the Rules for choice of backend from comments in the code to the docs.

I've done my best to update the docs to reflect these changes. I don't think most users should be using backend_info; nx.config.backends has the installed backends.

I'm beginning to think that type-based dispatching is pretty restrictive. In some sense, I'd like to see the code that is dispatched to be one choice and the object used by that code as a separate choice. So libraries could support more than one type (by converting and caching internally or by making code that works across types), and also more than one library can support a specific type (like a number of libraries which work directly with the nx graphs). But I haven't thought about how to implement that.

I'm similarly surprised we've been able to get as far as we have with our current dispatching! I like this chain of thought, so let's try to continue it later.

To finish up here with this comment:

  • what aspects of this do you see as needed for v3.4?

All of it.

It has all the things we need, and some of the things we want. There are bug fixes, and log messages are greatly improved. It is systematically tested. The code should be easier to maintain and more adaptable to change. It does not lock us into an approach for the "backend-to-backend" story.

  • Do we need the conversion of other types to networkx graphs upon fallback? What does this feature enable?

It allows for a smoother experience when users have backend graph objects. Just like they can opt in to automatically converting a networkx graph to a backend graph, this allows the reverse. Also, as I said above, it supports the "no code change" use case for nx-cugraph.

  • Is this going to lock us into the five groups of priorities described in lines 1100-1107? It is very appealing to just have backend_priority, and go down that list and pick the first one that doesn't signal NotImplemented. Backend-to-backend conversion can still happen when the dispatched-to library can handle/convert the incoming graph type. And if it can't then return NotImplemented (or raise, or whatever signal works best).

Not at all! In fact, I think it's the opposite: now that we've identified the five groups, it's much easier to change. The refactor is much more adaptable. Currently, we do not do backend-to-backend stuff. My bad for discussing it so much. Should I delete some code comments that discuss future possibilities?

Finally, two broader issues have come up:

  1. we would like better testing of dispatch machinery.
  2. we would like better maintainer documentation of dispatching, perhaps as a NXEP.

These are true regardless of this PR. I agree they are high priority, and I hope we can address them soon.

Comment thread networkx/utils/backends.py Resolved

@MridulS MridulS 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.

I was about to add a bunch of littered comments all around but decided not to do that.

we would like better testing of dispatch machinery.
we would like better maintainer documentation of dispatching, perhaps as a NXEP.

Most of the comments were around these 2 points (as pointed out by @eriknw) that we need to work on in the near future with dispatch machinery.

Let's get this in!

@dschult dschult 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.

I, like @MridulS, have a smattering of changes, mostly about docs and tests. I will put them into a separate PR(s).
I'm still looking for a simple explanation of how the backends are determined, and I am pretty sure the current description is not complete. But with this PR the code is easier to read and the decision framework described in the comments seems like it could be turned into some good docs.

So: "Once more unto the breach, dear friends"
Let's merge and pick up any stray pieces as we find them.

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 run:benchmark type: Enhancements

Development

Successfully merging this pull request may close these issues.

6 participants