Skip to content

Add nx.config.backend option - #7485

Open
eriknw wants to merge 13 commits into
networkx:mainfrom
eriknw:config_backend
Open

eriknw wants to merge 13 commits into
networkx:mainfrom
eriknw:config_backend

Conversation

@eriknw

@eriknw eriknw commented Jun 4, 2024

Copy link
Copy Markdown
Contributor

This is like doing backend=... when calling a dispatchable function.

I think this will be especially nice when combined with #7363, which will let us set it within a context:

with nx.config(backend="mybackend"):
    ...  # No need to use `backend="mybackend"` when calling dispatchable networkx functions!

Thanks to @seberg for this idea!

CC @rlratzel

This is like doing `backend=...` when calling a dispatchable function.
@rlratzel

rlratzel commented Jun 5, 2024

Copy link
Copy Markdown
Contributor

I'm in favor of this idea since it makes it easy for a user to essentially say "I want strict and exclusive use of this one backend, and an error if that can't be done" without changing each algo call to specify that using backend=.

However, I'm having a bit of trouble envisioning myself explaining the difference between the config options backend and backend_priority to a new user. I wonder if we need a different name for one or both?

@eriknw eriknw added type: Enhancements Dispatching Related to dispatching and backend support labels Jun 5, 2024
@eriknw

eriknw commented Jun 11, 2024

Copy link
Copy Markdown
Contributor Author

However, I'm having a bit of trouble envisioning myself explaining the difference between the config options backend and backend_priority to a new user. I wonder if we need a different name for one or both?

Suggestions? I can't think of anything better. @jim22k any ideas?

@Schefflera-Arboricola

Copy link
Copy Markdown
Member

Suggestions?

We can just have one backend config option and if the value passed to it is a str type then we can assume that the user wants to use only one backend and if they pass a list of backends (i.e. type(backend)==list[str]) then we can assume they have given us the priority order of the backends. Pls LMK if there is anything wrong with this approach. Thanks!

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

I have been thinking about these context managers and how we could potentially combine the context manager introduced in PR #7363 with this one. This combination could simplify the usage of nx.config in a more cohesive manner.

So, something like this:

with nx.config(backend=backend_name, *backend_args, **backend_kwargs):
    # nx code

This approach leverages the flexibility introduced in PR #7363 and could make setting configurations more intuitive, I think, especially when dealing with different backends. Your thoughts on this would be greatly appreciated. Thank you :)

@eriknw

eriknw commented Jun 27, 2024

Copy link
Copy Markdown
Contributor Author

Interesting and fun idea @Schefflera-Arboricola!

So, this can be a generalization to add keyword arguments to dispatchable calls such that adding a specific backend config option isn't necessary--it could get passed via **kwargs. Thus, one could do any of the following:

with nx.config(backend=backend_name):
    ...

with nx.config(backend="parallel", get_chunks=chunks):
    ...

with nx.config(parallel_kwargs={"get_chunks": chunks}, cugraph_kwargs={"dtype": "float32"}):
    ...

The latter example requires #7297. This is pretty nice.

I don't think baking in *args makes as much sense, but doing so would allow code like this:

with nx.config(G):
    cc = nx.connected_components()
    pr = nx.pagerank()

The main pro of baking in **kwargs above is that it is more general. The main cons are that it's a bit magical and using it is less discoverable (for example, the ability to use backend like this isn't discoverable if you look at the repr of nx.config or to tab-completion nx.config.<TAB>). So, we would need to make this discoverable by adding it to the docstring of the config and the online documentation, which is totally okay.

Another pro is that this may in some ways avoid the confusion that @rlratzel raised by having both backend and backend_priority configs.

Another con (for the purists!) may be that this usage mixes configuration items with keyword arguments. In practice, merging name spaces like this is often pragmatic and fine.

Two more question:

  1. how can you find the extra **kwargs that were baked in?
  2. can you bake in the extra **kwargs as e.g. a global config (i.e., not within a context)?

For example for (1):

with nx.config(chunks=7) as cfg:
    assert cfg.kwargs == {"chunks": 7}  # ??? or some other way?

@eriknw

eriknw commented Jun 27, 2024

Copy link
Copy Markdown
Contributor Author

I began experimenting with this in #7528. Fortunately, argmap actually prevents weird shenanigans (don't ask me how!) such as:

with nx.config(G=G):
    cc = nx.connected_components()  # raises, hooray!
    pr = nx.pagerank()

so this really ought to be used for backend kwargs as originally suggested.

I'm still torn whether to have backend be in the config or to simply apply backend via the generic backend kwargs (both are possible and equally clean to implement).

@Schefflera-Arboricola

Schefflera-Arboricola commented Jun 28, 2024 •

Copy link
Copy Markdown
Member

Really sorry, I didn't mean this

So, something like this:

with nx.config(backend=backend_name, *backend_args, **backend_kwargs):
    # nx code

I wanted to suggest

with nx.config(backend=backend_name, backend_configs):
    # nx code

So, this way the backend and the backend configs will be in the same context manager. And if you want to include the suggestion above . This one:

We can just have one backend config option and if the value passed to it is a str type then we can assume that the user wants to use only one backend and if they pass a list of backends (i.e. type(backend)==list[str]) then we can assume they have given us the priority order of the backends. Pls LMK if there is anything wrong with this approach. Thanks!

Then backend_configs will be either a dict or list of dicts or a dict of dicts

with nx.config(backend=parallel, backend_configs={"n_jobs":6}):
    # nx code

Or

with nx.config(backend=[cugraph, parallel], backend_configs=[{"cugraph_config_param": "xyz"},{"n_jobs"=6}]:
    # nx code

Or

with nx.config(backend=[cugraph, parallel], backend_configs={{"cugraph" : {"cugraph_config_param": "xyz"}, "parallel" : {"n_jobs"=6}}:
    # nx code

Really sorry for the confusion.

@eriknw

eriknw commented Jun 28, 2024

Copy link
Copy Markdown
Contributor Author

I see, thanks for clarifying @Schefflera-Arboricola. I think I understand now.

Let me respond to your previous suggestion first, which we also discussed briefly in our weekly meetings:

We can just have one backend config option and if the value passed to it is a str type then we can assume that the user wants to use only one backend and if they pass a list of backends (i.e. type(backend)==list[str]) then we can assume they have given us the priority order of the backends.

I'm -1 on this. I think this introduces complexity. I don't like having different behaviors based on the type of something. Generally, I would prefer to have two simple things than one complicated thing. And I think backend and backend_priority are different things--they are not similar enough to be merged imho. I think it's better to document and use each separately. Combining them also makes it more difficult for either of them to evolve. For example, we are proposing evolving backend_priority in #7502, so we should also consider the implications of having the backend behavior you proposed when the argument is a string instead of a list of strings.

If I understand your latest idea, you are discussing adding backend_configs configuration that may be a dict, dict-of-dicts, or list-of-dicts, and how it's used may depend on backend_priority config. I would try to simplify this--again, I don't like having different types for minimal benefit. Syntactic sugar can be sweet (and occasionally worth it!), but adding too much sugar too early can fall into the YAGNI principle. If we're not adding new functionality, programmers are often okay with jumping through the single available hoop.

Naming things is also hard, and backend_configs is very close to the configs for backends that we already have: nx.config.backends.

So, if I may, I'd like to respond to my own criticisms and compare two approaches:

  1. Use a new extra_kwargs (or backend_kwargs) config that may only be a dict
  2. Use the approach in WIP: save backend kwargs when using config as context #7528 so that any extra keyword arguments when using nx.config as a context manager are treated as extra kwargs
with nx.config(backend="parallel", extra_kwargs={"n_jobs": 16}):
    ...  # 1

with nx.config(backend="parallel", n_jobs=16):
    ...  # 2

and

with nx.config(
    backend_priority=["cugraph", "parallel"],
    extra_kwargs={
        "cugraph_kwargs": {"dtype": "float32"},
        "parallel_kwargs": {"n_jobs": 16},
    },
):
    ...  # 1

with nx.config(
    backend_priority=["cugraph", "parallel"],
    cugraph_kwargs={"dtype": "float32"},
    parallel_kwargs={"n_jobs": 16},
):
    ...  # 2

I really appreciate this discussion @Schefflera-Arboricola. In general, I'm glad we've been taking a careful, incremental approach around dispatching. We all have different perspectives and sensibilities, and I think we build better things when we consider each other and explore possibilities!

@eriknw

eriknw commented Jul 1, 2024

Copy link
Copy Markdown
Contributor Author

Continuing to experiment...

#7528 now has a backend_kwargs config (better name?) that can be used directly or set via extra keyword arguments when config is used as a context manager, so this largely has "all of the above" behavior. Also, it removed config.backend and relies on backend_kwargs for doing e.g. with nx.config(backend=backend):. We can document the behavior of backend_kwargs in the nx.config docstring and the online docs.

This branch has not been deployed

No deployments
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