Skip to content

[inductor] Stop caching op packet names in kernel metadata - #200953

Closed
anijain2305 wants to merge 1 commit into
gh/anijain2305/1357/basefrom
gh/anijain2305/1357/head
Closed

anijain2305 wants to merge 1 commit into
gh/anijain2305/1357/basefrom
gh/anijain2305/1357/head

Conversation

@anijain2305

@anijain2305 anijain2305 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

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.

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 Oct 14, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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 75ea782 with merge base 207451c (image):

🟢 GREEN LIGHT - PR approved to be merged without human review:

  • Triviality
    • Behavior-neutral tweaks: drops the lru_cache helper _overloadpacket_str and inlines str(original_aten._overloadpacket) in get_kernel_metadata
    • The inlined expression is exactly the helper's body, so the metadata key string is unchanged for every op
  • Scope
    • Single hunk in torch/_inductor/utils.py; _overloadpacket_str has no other references in the checkout
    • functools is still used elsewhere in the file, so no dangling import
  • Socialization
    • Helper was module-private; no signature, default, or emitted comment format changes

reason: clean
Reviewed commit: 75ea782

Inference job

🐛 Report a wrong verdict

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.

@anijain2305

Copy link
Copy Markdown
Contributor Author

@claude review this PR

@claude

claude Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Claude finished @anijain2305's task in 29s —— View job


The cache removal looks correct and I found nothing blocking. The one open question is whether to also fix the underlying copy problem in torch/_ops.py.

Details

PR Review: #200953

Summary

The PR removes the lru_cache'd _overloadpacket_str helper from torch/_inductor/utils.py and computes str(original_aten._overloadpacket) inline in get_kernel_metadata.

I checked the PR's reasoning against torch/_ops.py:

  • OpOverload defines __deepcopy__ returning self (line 909).
  • It defines __hash__ (line 936), but not __eq__ or __copy__.
  • So a copy.copy of an op gets the same hash but compares unequal under identity, and the cache misses.

Nothing else references _overloadpacket_str, and functools is still used elsewhere in the file. The diff itself has no problems.

Code Quality

  • Root cause is left in place (torch/_ops.py:908-911). The real defect is that OpOverload handles deepcopy but not shallow copy. As a result, node.meta["original_aten"] holds a separate copy of the op on every node. That also defeats:

    • any other code that compares original_aten with is;
    • any other code that uses it as a dict or set key.

    Adding def __copy__(self): return self next to __deepcopy__ would restore the "OpOverload is unique" invariant that the existing comment already claims. This could go in a follow-up PR; removing the cache here is still right either way, since the string costs about 0.25us to build. Low confidence on whether other callers are affected today; I did not audit them.

Recommendation

Approve. This is a small removal of a cache that wasn't working, with a large measured compile-time win. No test is needed because behavior doesn't change. Please consider the __copy__ follow-up.

@anijain2305 anijain2305 added the topic: not user facing topic category label Oct 14, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #200954

1 similar comment
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #200954

pytorchmergebot pushed a commit that referenced this pull request Oct 14, 2026
`OpOverload`, `OpOverloadPacket` and `HigherOrderOperator` define
`__deepcopy__` to return `self` because each must be unique, but they did not
define `__copy__`. A shallow copy therefore built a new instance:

- FX copies node metadata such as `original_aten` with `copy.copy` when
  tracing with preserved node meta, so every node traced by AOTAutograd
  carried its own copy of its op. The copies hash like the op but compare
  unequal, which defeats any cache or identity check keyed on the op.
- `copy.copy(torch.ops.aten.add)` never finished: the half-built packet's
  `__getattr__` and `__str__` recursed through each other.

Return `self` from `__copy__`, matching `__deepcopy__`.
Pull Request resolved: #200954
Approved by: https://github.com/pytorchgreenlight
ghstack dependencies: #200953
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.

2 participants