Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[runtime][python] Add debug sink to bindings #19013

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

sogartar
Copy link
Contributor

@sogartar sogartar commented Nov 4, 2024

We don't support custom debug sinks in the Runtime Python bindings. In particular the ability to register a custom callback when tracing tensors.

This change makes it possible to create a HAL module with a Python function as a callback.
A special care is taken to avoid leaking due to circular references where the callback reference the HAL module itself.

We don't support custom debug sinks in the Runtime Python bindings.
In particular the ability to register a custom callback when tracing
tensors.

This change makes it possible to create a HAL module with a Python
function as a callback.
A special care is taken to avoid leaking due to circular references
where the callback reference the HAL module itself.

Signed-off-by: Boian Petkantchin <[email protected]>
@sogartar
Copy link
Contributor Author

sogartar commented Nov 4, 2024

This PR plumbs through the Python bindings the trace callback recently added by #18966.

@sogartar sogartar force-pushed the hal-module-sink-python-bindings branch from 8b2ec54 to c060bb2 Compare November 4, 2024 19:41
@sogartar sogartar requested review from benvanik and stellaraccident and removed request for stellaraccident November 4, 2024 20:42
@sogartar sogartar marked this pull request as draft November 4, 2024 20:48
Signed-off-by: Boian Petkantchin <[email protected]>
@sogartar sogartar marked this pull request as ready for review November 4, 2024 21:23
@sogartar
Copy link
Contributor Author

sogartar commented Nov 5, 2024

In its current state, I found out that one needs to hold the HAL module python object in order for the debug sink context to not get deleted, which is not what I wanted. I wanted once registered in IREE, the HAL module to continue be a shared owner of the debug sink. I guess the C++ objects for the bindings gets destroyed and created on demand from the underlying IREE runtime objects.
I will need to rethink how to handle this.

@sogartar sogartar marked this pull request as draft November 5, 2024 19:50
// Retain a reference. We want the callback to be valid after
// the user has dropped its reference and not burden the user
// with lifetime management.
vm_module.SetHalModuleDebugSink(*debug_sink);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Need to think a bit carefully about this: The underlying VM module can have a lifetime that exceeds the Python level wrapper for it. Reviewing the rest of the patch and will see if I can spot a better way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I came up with 2 solutions.

  1. Burden the user to guarantee that the callback function outlives the HAL module by holding a reference to it. In general not a good solution in Python.
  2. In order to bind the lifetime of the Python callback object to the HAL module we need to be able to register hooks with the module where it gets called on destruction. This burdens the IREE runtime with more stuff. Maybe not desirable. Instead of doing this for every VM module maybe at least the IREE runtime instance can have on-destruction hooks. The general problem is that we don't know when stuff gets destroyed.

Copy link
Contributor Author

@sogartar sogartar Nov 6, 2024

Choose a reason for hiding this comment

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

Another option is to add ownership of the debug sink to the VM context when the HAL module is registered there.
Then as long as the Python object of the VM context lives the debug sink will live.
This solution suffers from the problem of interop between the the Python bindings and some other interaction with the runtime. For example if we take the native handle from the Python VM context object and start using it while we destroy the Python object. I am not sure currently if the the bindings support this interaction, but I may be a future use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants