Skip to content

[inductor] Reuse formatted origin nodes across kernel comments - #194010

Closed
anijain2305 wants to merge 7 commits into
gh/anijain2305/1318/basefrom
gh/anijain2305/1318/head
Closed

anijain2305 wants to merge 7 commits into
gh/anijain2305/1318/basefrom
gh/anijain2305/1318/head

Conversation

@anijain2305

@anijain2305 anijain2305 commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Every generated kernel carries a comment naming the graph fragment it came
from, and building it re-formats each of the kernel's origin nodes with
Node.format_node(include_tensor_metadata=True). The same node is an origin of
many kernels, so on a large graph this rebuilds the same strings over and over:
258k format_node calls to emit comments for 1266 kernels on one production
model, which cProfile puts at a larger share of compile time than
GraphLowering.run.

The graph is fully built by the time we are emitting code, so a node's
formatting cannot change. Cache the split lines on the graph, exactly as the
topological index map a few lines above already does, so the lifetime is the
graph's. Also memoize str(op._overloadpacket), which was called once per node
per kernel - 271k times - on singleton op objects.

Cold compile of a reduced training replay of model3 goes from 47.3s to 43.5s
(median of three).

Test plan:

python test/inductor/test_torchinductor.py -k GPU
python test/inductor/test_provenance_tracing.py

The thing to establish is that the comments do not change, and the generated
code cannot be diffed directly: two runs of unmodified code differ by 656 lines
because the repro feeds random data and node numbering shifts. So the cache was
checked from the inside instead - recompute on every hit and assert the cached
lines match - and a full compile of that model passed with zero mismatches
across all ~258k lookups.

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 @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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 2fb5e88 with merge base 9ebc67a (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.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Aug 28, 2026
Every generated kernel carries a comment naming the graph fragment it came
from, and building it re-formats each of the kernel's origin nodes with
Node.format_node(include_tensor_metadata=True). The same node is an origin of
many kernels, so on a large graph this rebuilds the same strings over and over:
258k format_node calls to emit comments for 1266 kernels on one production
model, which cProfile puts at a larger share of compile time than
GraphLowering.run.

The graph is fully built by the time we are emitting code, so a node's
formatting cannot change. Cache the split lines on the graph, exactly as the
topological index map a few lines above already does, so the lifetime is the
graph's. Also memoize str(op._overloadpacket), which was called once per node
per kernel - 271k times - on singleton op objects.

Cold compile of a reduced training replay of model3 goes from 47.3s to 43.5s
(median of three).

Test plan:

```
python test/inductor/test_torchinductor.py -k GPU
python test/inductor/test_provenance_tracing.py
```

The thing to establish is that the comments do not change, and the generated
code cannot be diffed directly: two runs of unmodified code differ by 656 lines
because the repro feeds random data and node numbering shifts. So the cache was
checked from the inside instead - recompute on every hit and assert the cached
lines match - and a full compile of that model passed with zero mismatches
across all ~258k lookups.

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

ghstack-source-id: efd1fe8
Pull-Request: #194010
anijain2305 added a commit that referenced this pull request Aug 28, 2026
Every generated kernel carries a comment naming the graph fragment it came
from, and building it re-formats each of the kernel's origin nodes with
Node.format_node(include_tensor_metadata=True). The same node is an origin of
many kernels, so on a large graph this rebuilds the same strings over and over:
258k format_node calls to emit comments for 1266 kernels on one production
model, which cProfile puts at a larger share of compile time than
GraphLowering.run.

The graph is fully built by the time we are emitting code, so a node's
formatting cannot change. Cache the split lines on the graph, exactly as the
topological index map a few lines above already does, so the lifetime is the
graph's. Also memoize str(op._overloadpacket), which was called once per node
per kernel - 271k times - on singleton op objects.

Cold compile of a reduced training replay of model3 goes from 47.3s to 43.5s
(median of three).

Test plan:

```
python test/inductor/test_torchinductor.py -k GPU
python test/inductor/test_provenance_tracing.py
```

The thing to establish is that the comments do not change, and the generated
code cannot be diffed directly: two runs of unmodified code differ by 656 lines
because the repro feeds random data and node numbering shifts. So the cache was
checked from the inside instead - recompute on every hit and assert the cached
lines match - and a full compile of that model passed with zero mismatches
across all ~258k lookups.

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

ghstack-source-id: efd1fe8
Pull-Request: #194010
@anijain2305

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Aug 28, 2026
@pytorchmergebot

Copy link
Copy Markdown
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

orrangetabby17 pushed a commit to orrangetabby17/pytorch that referenced this pull request Sep 2, 2026
…ch#194010)

Every generated kernel carries a comment naming the graph fragment it came
from, and building it re-formats each of the kernel's origin nodes with
Node.format_node(include_tensor_metadata=True). The same node is an origin of
many kernels, so on a large graph this rebuilds the same strings over and over:
258k format_node calls to emit comments for 1266 kernels on one production
model, which cProfile puts at a larger share of compile time than
GraphLowering.run.

The graph is fully built by the time we are emitting code, so a node's
formatting cannot change. Cache the split lines on the graph, exactly as the
topological index map a few lines above already does, so the lifetime is the
graph's. Also memoize str(op._overloadpacket), which was called once per node
per kernel - 271k times - on singleton op objects.

Cold compile of a reduced training replay of model3 goes from 47.3s to 43.5s
(median of three).

Test plan:

```
python test/inductor/test_torchinductor.py -k GPU
python test/inductor/test_provenance_tracing.py
```

The thing to establish is that the comments do not change, and the generated
code cannot be diffed directly: two runs of unmodified code differ by 656 lines
because the repro feeds random data and node numbering shifts. So the cache was
checked from the inside instead - recompute on every hit and assert the cached
lines match - and a full compile of that model passed with zero mismatches
across all ~258k lookups.

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

Pull Request resolved: pytorch#194010
Approved by: https://github.com/eellison
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
…ch#194010)

Every generated kernel carries a comment naming the graph fragment it came
from, and building it re-formats each of the kernel's origin nodes with
Node.format_node(include_tensor_metadata=True). The same node is an origin of
many kernels, so on a large graph this rebuilds the same strings over and over:
258k format_node calls to emit comments for 1266 kernels on one production
model, which cProfile puts at a larger share of compile time than
GraphLowering.run.

The graph is fully built by the time we are emitting code, so a node's
formatting cannot change. Cache the split lines on the graph, exactly as the
topological index map a few lines above already does, so the lifetime is the
graph's. Also memoize str(op._overloadpacket), which was called once per node
per kernel - 271k times - on singleton op objects.

Cold compile of a reduced training replay of model3 goes from 47.3s to 43.5s
(median of three).

Test plan:

```
python test/inductor/test_torchinductor.py -k GPU
python test/inductor/test_provenance_tracing.py
```

The thing to establish is that the comments do not change, and the generated
code cannot be diffed directly: two runs of unmodified code differ by 656 lines
because the repro feeds random data and node numbering shifts. So the cache was
checked from the inside instead - recompute on every hit and assert the cached
lines match - and a full compile of that model passed with zero mismatches
across all ~258k lookups.

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

Pull Request resolved: pytorch#194010
Approved by: https://github.com/eellison
pytorchmergebot pushed a commit that referenced this pull request Oct 14, 2026
https://www.internalfb.com/intern/paste/P2550697715/

#194010 memoized `str(op._overloadpacket)` in `get_kernel_metadata` with an
`lru_cache` keyed on the `OpOverload`, assuming ops are singletons. They are
not in `node.meta["original_aten"]`: FX copies that field with `copy.copy`,
and `OpOverload` defines `__deepcopy__` but not `__copy__`, so every traced
node carries its own copy of its op. The copies hash alike but compare
unequal, so the cache fills with colliding keys and almost always misses.

On a large training backward graph (1,544 kernels, up to 2,699
origin nodes per kernel) the cache recorded 2.53M misses and 18.8K hits, and
`get_kernel_metadata` took 98s of a 293s cold compile. Computing the string
directly costs about 0.25us; with this change the function takes 2.5s and the
compile 201s.

Pull Request resolved: #200953
Approved by: https://github.com/pytorchgreenlight
@github-actions
github-actions Bot deleted the gh/anijain2305/1318/head branch November 11, 2026 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request Merged module: inductor topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants