Skip to content

Add stacktrace in metadata_props for rewriter created nodes #2423

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion onnxscript/ir/_tape.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import annotations

import traceback
from typing import TYPE_CHECKING, Any, Optional, Sequence

from onnx_ir import tape
Expand All @@ -16,6 +17,12 @@
UsedOpsets = set[tuple[str, Optional[int]]]


def _get_stacktrace() -> str:
"""Get the stack trace of the current execution context."""
stack = traceback.extract_stack()
return "".join(traceback.format_list(stack[:-3]))


class Builder(tape.Tape):
"""An extension of the tape that provides a more convenient API for constructing the IR."""

Expand All @@ -32,9 +39,18 @@ def _make_node(self, op_type: str, inputs: Sequence[ir.Value], kwargs: dict[str,
assert isinstance(outputs, int)
num_outputs = outputs

metadata_props = {
"pkg.onnxscript.rewriter.stacktrace": _get_stacktrace(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this always be added? Or, would it be better to do it only if the user sets some config/environment variable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it's ok as a default? For example torch.export will add metadata as well. If user wants no stack traces they can run a pass, or if there's demand we can add an option to turn it off?

Copy link
Collaborator

Choose a reason for hiding this comment

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

But who's using this? What happens when multiple rewrites happen? Trying to understand what exactly the use-case is and what the users want.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

When we look at the graph from pytorch, all nodes can be traced back from source code in the torch model, but it is unclear which rule created the nodes that are added by the rewriter. So I was trying to add this information. A node can only be created once so multiple rewrites should not affect this field

}

if num_outputs == 1:
value = super().op(
op_type, inputs=inputs, attributes=kwargs, domain=domain, version=version
op_type,
inputs=inputs,
attributes=kwargs,
domain=domain,
version=version,
metadata_props=metadata_props,
)
if isinstance(outputs, Sequence):
value.name = outputs[0]
Expand All @@ -46,6 +62,7 @@ def _make_node(self, op_type: str, inputs: Sequence[ir.Value], kwargs: dict[str,
domain=domain,
version=version,
num_outputs=num_outputs,
metadata_props=metadata_props,
)
if isinstance(outputs, Sequence):
for value, name in zip(values, outputs):
Expand Down
Loading