Skip to content

WIP: add runtime hooks to dispatch system - #7765

Draft
eriknw wants to merge 6 commits into
networkx:mainfrom
eriknw:dispatch_hooks
Draft

eriknw wants to merge 6 commits into
networkx:mainfrom
eriknw:dispatch_hooks

Conversation

@eriknw

@eriknw eriknw commented Dec 11, 2024

Copy link
Copy Markdown
Contributor

There are many potential uses. For example, this will be useful for gathering timing of dispatch calls (including time spent in conversions, etc.).

Here's a simple example of a backend called "timing" to print how long it took to run with a backend:

def dispatch_hook_impl(hook_name, hook_token, **kwargs):
    import time

    if hook_name == "on_call_with_backend_begin":
        hook_token["timing"]["t0"] = time.time()
    elif hook_name == "on_call_with_backend_end":
        t = time.time() - hook_token["timing"]["t0"]
        backend_name = kwargs['backend_name']
        dispatch_name = hook_token.dispatchable.name
        print(f"{backend_name!r} backend ran {dispatch_name} in {t:.3g} seconds")

Here's how I organize the hooks in a way that makes sense to me:

on_load_backend_begin
on_load_backend_end

on_call_begin

    on_can_convert_end

    on_can_backend_run_begin
    on_can_backend_run_end

    on_will_call_mutate_input_end

    on_should_backend_run_begin
    on_should_backend_run_end

    on_convert_and_call_begin

        on_convert_arguments_begin

            on_convert_graph_begin

                on_get_from_cache_begin
                on_get_from_cache_end

                on_set_to_cache_begin
                on_set_to_cache_end

            on_convert_graph_end

        on_convert_arguments_end

        on_call_with_backend_begin
        on_call_with_backend_end

    on_convert_and_call_end

on_call_end

This is WIP bc it's a first pass at hooks, and it needs documented, but I don't want to document it until we discuss it. I happy to answer any questions and listen to concerns.

CC @rlratzel

There are many potential uses. For example, this will
be useful for gathering timing of dispatch calls.
@eriknw eriknw added type: Enhancements Dispatching Related to dispatching and backend support labels Dec 11, 2024
@eriknw
eriknw marked this pull request as draft December 11, 2024 01:19

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.

1 participant