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

New example for preserve_node_meta #128681

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

TharinduRusira
Copy link
Contributor

@TharinduRusira TharinduRusira commented Jun 14, 2024

Docstring updated with a new example as suggested by @ezyang and @angelayi

Fixes #127915

cc @svekars @brycebortree @ezyang @SherlockNoMad @EikanWang @jgong5 @wenzhe-nrv

Docstring updated with a new example as suggested by @ezyang and @angelayi

Fixes pytorch#127915
Copy link

pytorch-bot bot commented Jun 14, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/128681

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit b9613f5 with merge base eff01bc (image):

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot bot added the release notes: fx release notes category label Jun 14, 2024
@github-actions github-actions bot added docathon-h1-2024 medium Label for medium docathon tasks module: docs Related to our documentation, both in docs/ and docblocks triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Jun 14, 2024
@svekars svekars requested review from angelayi and ezyang June 17, 2024 19:56
>>> for node in fxg.graph.nodes:
>>> print(node, node.meta)

>>> def simple_transform(g: torch.fx.Graph) -> torch.fx.Graph:
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, this is not really the transformation I was referring to. It should be with fx.Transformer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @angelayi, a quick clarification, please see the code below. When I do a node traversal with run_node, it does preserve the meta data.
However, if I do SimpleTransform(fxg).transform(), it overwrites the existing meta data. I understand transform() returns a new graph but is this the expected behavior of preserve_node_meta when called against transform()?

(tested with Pytorch built from commit eff01bc)


class SimpleAdd(torch.nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, x, y):
        return x + y

class SimpleTransform(torch.fx.Transformer):
    def __init__(self, module):
        super().__init__(module)
    
    def run_node(self, n: torch.fx.Node):
        n.meta["node_op"] = n.op
        return super().run_node(n)
    
model = SimpleAdd()

with torch.fx.traceback.preserve_node_meta():
    fxg = torch.fx.symbolic_trace(model)

    # node meta prior to transform
    """
    x {'seq_nr': -1}
    y {'seq_nr': -1} 
    add {'seq_nr': -1}
    output {'seq_nr': -1}
    """
    for node in fxg.graph.nodes:
        print(node, node.meta)

    # node traversal to add new meta
    SimpleTransform(fxg).run()

    """
    x {'seq_nr': -1, 'node_op': 'placeholder'}
    y {'seq_nr': -1, 'node_op': 'placeholder'}
    add {'seq_nr': -1, 'node_op': 'call_function'}
    output {'seq_nr': -1, 'node_op': 'output'}
    """
    for node in fxg.graph.nodes:
        print(node, node.meta)

    # call super().transform()
    fxgn = SimpleTransform(fxg).transform()

    # node meta after transform
    """
    x {}
    y {}
    add {'from_node': [('add', <built-in function add>)], 'seq_nr': -1}
    output {}
    """
    for node in fxgn.graph.nodes:
        print(node, node.meta)

Thanks!

Copy link

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions bot added the Stale label Aug 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docathon-h1-2024 medium Label for medium docathon tasks module: docs Related to our documentation, both in docs/ and docblocks oncall: fx open source release notes: fx release notes category Stale triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document the torch.fx.traceback.preserve_node_meta function
4 participants