Skip to content

[dynamo] Reuse the stack trace when consecutive fx nodes share one - #193764

Closed
anijain2305 wants to merge 9 commits into
gh/anijain2305/1315/basefrom
gh/anijain2305/1315/head
Closed

anijain2305 wants to merge 9 commits into
gh/anijain2305/1315/basefrom
gh/anijain2305/1315/head

Conversation

@anijain2305

@anijain2305 anijain2305 commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Every fx node records the user stack that produced it, and producing it is not
cheap: a FrameSummary per frame on the tracing stack, then StackSummary.format(),
which reads the source line and works out the 3.11+ anchor columns for each one.
That is roughly ten frames for every node created.

Almost all of that is repeated. A transformer traces the same lines once per
layer, so the same stack recurs constantly - on a Qwen3-8B prefill trace, 4837
nodes produce far fewer distinct stacks, and 48k FrameSummary objects get built
and formatted to say the same handful of things.

Key on what the answer actually depends on - the frames' (file, line, name,
columns) - and build the string only when it is new. The key is read straight off
the tracing stack, so a hit skips constructing the FrameSummary objects as well
as formatting them. The cache lives on the SubgraphTracer, so it lasts exactly as
long as the graph being built.

Also hoists the torch/nn/modules regex in is_co_filename_from_nn_modules out of
the function; it was recompiled on every frame of every node.

Dynamo tracing on that model goes from 5.30s to 5.01s, and time attributed to
create_proxy from 0.72s to 0.44s.

Test plan:

python test/dynamo/test_misc.py

865 tests, OK. Separately checked that the stack traces themselves do not
change: traced a small module with nested calls and a dynamic dimension, dumped
node.stack_trace for all 43 nodes with and without this change, and compared -
byte identical.

This PR was authored with the assistance of an AI coding agent.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @jataylo @azahed98

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Aug 17, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 New Failure, 1 Unrelated Failure, 1 Unclassified Failure

As of commit 6ad8871 with merge base 6a0eb75 (image):

NEW FAILURE - The following job has failed:

  • Lint / lintrunner-pyrefly-partial / lint (gh)
    >>> Lint for torch/_dynamo/output_graph.py:

    AI verdict: AI verdict: related

    The PR introduced a stack trace caching optimization in torch/_dynamo/output_graph.py that uses **({"colno": ..., "end_colno": ...} if condition else {}) to pass optional kwargs to traceback.FrameSummary.__init__. Pyrefly flags this as bad-argument-type because it cannot resolve the unpacked conditional expression's type correctly.

    Full reasoning on HUD →

UNCLASSIFIED FAILURE - DrCI could not classify the following job because the workflow did not run on the merge base. The failure may be pre-existing on trunk or introduced by this PR:

  • Check mergeability of ghstack PR / ghstack-mergeability-check (gh) (this job did not run on the merge base, so DrCI cannot tell whether the failure is pre-existing)
    cherry-pick

    AI verdict: AI verdict: probably related

    The ghstack mergeability check fails because a commit in this PR's stack (241a9eb - "[c10] Don't build the contiguity predicate when the answer is computed concretely") has a merge conflict in test/test_dynamic_shapes.py when cherry-picked onto the target branch. This is inherent to the PR's stack state.

    Full reasoning on HUD →

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 ciflow/torchtitan Run TorchTitan integration tests module: dynamo labels Aug 17, 2026
@pytorch-bot

pytorch-bot Bot commented Aug 17, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@github-actions

Copy link
Copy Markdown
Contributor

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 Oct 24, 2026
@github-actions github-actions Bot closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant