Repository navigation
Allow dispatch machinery to fall back to networkx - #7585
Conversation
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.
Co-authored-by: Aditi Juneja <[email protected]>
Co-authored-by: Aditi Juneja <[email protected]>
Cherrypick only some code from this branch: when a call will mutate a graph, allow falling back to networkx if all graphs are instances of `nx.Graph`. I modified some of the log messages and added structure around when and how to fallback and what to log.
…o fallback_to_networkx
eriknw
left a comment
There was a problem hiding this comment.
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.
| # 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. |
There was a problem hiding this comment.
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.
| # 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. |
There was a problem hiding this comment.
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.
| 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"] |
There was a problem hiding this comment.
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.
| 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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 signalNotImplemented. 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 returnNotImplemented(or raise, or whatever signal works best).
…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.
You bet, thanks for taking a close look!
👍. The PR has been updated with your suggestions.
As we recently discussed, I change the default of
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.
Great! I like it too.
I've done my best to update the docs to reflect these changes. I don't think most users should be using
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.
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.
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.
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:
These are true regardless of this PR. I agree they are high priority, and I hope we can address them soon. |
Co-authored-by: Dan Schult <[email protected]>
MridulS
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
Allow falling back to NetworkX by converting backend graphs to nx graphs. This is enabled by adding
"networkx"tonx.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