Skip to content

torch.fx: add debug-level logging to Interpreter.run_node (#117351)#166622

Closed
mdbarnesUCSD wants to merge 3 commits intopytorch:mainfrom
mdbarnesUCSD:fx-interpreter-logging
Closed

torch.fx: add debug-level logging to Interpreter.run_node (#117351)#166622
mdbarnesUCSD wants to merge 3 commits intopytorch:mainfrom
mdbarnesUCSD:fx-interpreter-logging

Conversation

@mdbarnesUCSD
Copy link
Contributor

@mdbarnesUCSD mdbarnesUCSD commented Oct 30, 2025

Summary

Adds a debug-level logging statement to torch.fx.Interpreter.run_node, as proposed in #117351, to make FX graph execution traceable when debugging or instrumenting model transformations.

When debug logging is enabled, each executed node emits a single structured log line formatted via LazyString(lambda: n.format_node()), deferring string construction unless logging is active.

Example Output

With logging.DEBUG enabled:

run_node x = x()
run_node add = _operator.add(x, 1)
run_node clamp = torch.clamp(add, min=0.0, max=5.0)
run_node output = output(clamp)

With logging.DEBUG disabled no additional output is produced (unchanged default behavior).

Test Plan

Verified locally with Python 3.11 on macOS using a PyTorch build from source.

  • With logging.DEBUG enabled: each node emits a debug log via LazyString.
  • With logging.DEBUG disabled: no additional output.
  • Confirmed all Interpreter tests pass locally:
    pytest test/test_fx.py -k "Interpreter"

Updated the example output to reflect the new _format_fx_node helper and inclusion of kwargs.

cc @ezyang @EikanWang @jgong5 @wenzhe-nrv

@pytorch-bot
Copy link

pytorch-bot bot commented Oct 30, 2025

🔗 Helpful Links

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

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

✅ You can merge normally! (4 Unrelated Failures)

As of commit 2ce2726 with merge base a533526 (image):

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

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 Oct 30, 2025
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Oct 30, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@ezyang
Copy link
Contributor

ezyang commented Oct 30, 2025

can you do the python printing instead

@jerryzh168 jerryzh168 added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Oct 30, 2025
@mdbarnesUCSD
Copy link
Contributor Author

Thanks @ezyang, I’ve updated the PR to use python printing instead.

Each node is now logged in a format that mirrors the original Python code, including module prefixes like torch.relu and _operator.add.

Example output:

run_node x = x()
run_node add = _operator.add(x, 1)
run_node relu = torch.relu(add)
run_node output = output(relu)

Let me know if any further changes would be helpful.

f"{n.name} = "
f"{getattr(n.target, '__module__', '') + '.' if hasattr(n.target, '__module__') else ''}"
f"{getattr(n.target, '__name__', n.target)}"
f"({', '.join(map(str, n.args))})"
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we print kwargs too?

"""
log.debug(
"run_node %s",
LazyString(lambda:
Copy link
Contributor

@aorenste aorenste Oct 31, 2025

Choose a reason for hiding this comment

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

nit: This might be easier to read if the LazyString called a top-level helper function - especially if we want to eventually do more complex formatting (such as printing "call_method" as obj.method() instead of class.method(obj))
(but I don't feel strongly - so only do it if you agree)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review.

I added kwargs to the debug output and moved the string formatting into a top-level _format_fx_node helper for readability and easier future extensions.

@aorenste
Copy link
Contributor

Need to update the example output in the PR summary

@aorenste
Copy link
Contributor

This LGTM but the lintrunner error needs to be addressed (I can't imagine it will cause anything else to fail).

@mdbarnesUCSD mdbarnesUCSD force-pushed the fx-interpreter-logging branch from 9611467 to 2ce2726 Compare October 31, 2025 17:33
@mdbarnesUCSD
Copy link
Contributor Author

I ran lintrunner -a torch/fx/interpreter.py and applied the fixes for this file, and I also updated the example output in the PR summary.

I noticed that several of the remaining CI jobs are failing under the Linux Jammy configurations (e.g., the dynamo_wrapped test jobs). Are these known flakes, or is this something I should take a closer look at?

@aorenste
Copy link
Contributor

aorenste commented Nov 1, 2025

I ran lintrunner -a torch/fx/interpreter.py and applied the fixes for this file, and I also updated the example output in the PR summary.

I noticed that several of the remaining CI jobs are failing under the Linux Jammy configurations (e.g., the dynamo_wrapped test jobs). Are these known flakes, or is this something I should take a closer look at?

You should be good. If you look above (in the first "pytorch-bot" comment) it gives a status message. It currently says "You can merge normally" so once I've approved you can use torchbot to merge.

@mdbarnesUCSD
Copy link
Contributor Author

mdbarnesUCSD commented Nov 6, 2025

Thanks @aorenste, I’ll go ahead and comment @pytorchbot merge per your note above.

@mdbarnesUCSD
Copy link
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Nov 6, 2025
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request fx Merged open source release notes: fx release notes category 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.

7 participants