Skip to content

Support inplace-tagged custom operators in torch.compile - #181100

Closed
zou3519 wants to merge 3 commits into
gh/zou3519/6/basefrom
gh/zou3519/6/head
Closed

zou3519 wants to merge 3 commits into
gh/zou3519/6/basefrom
gh/zou3519/6/head

Conversation

@zou3519

@zou3519 zou3519 commented Apr 22, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Allow custom operators tagged with torch.Tag.inplace to go through
auto_functionalize during torch.compile. The functional form of e.g.
foo_(Tensor(a!) self, Tensor other) -> Tensor(a!) passes self as a
base in _all_bases (with view info) so that the reinplacing pass can
do clone analysis—unlike out= ops where the mutable args are write-only
and clones are never needed.

Test Plan:

  • python test/test_inplace_tag.py

Allow custom operators tagged with torch.Tag.inplace to go through
auto_functionalize during torch.compile. The functional form of e.g.
`foo_(Tensor(a!) self, Tensor other) -> Tensor(a!)` passes self as a
base in _all_bases (with view info) so that the reinplacing pass can
do clone analysis—unlike out= ops where the mutable args are write-only
and clones are never needed.

Test Plan:
- python test/test_inplace_tag.py

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Apr 22, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ No Failures

As of commit 9069f47 with merge base a55ca57 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@pytorch-bot

pytorch-bot Bot commented Apr 22, 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.

@zou3519
zou3519 requested a review from angelayi April 22, 2026 12:04

@aorenste aorenste left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does it need a test for a duplicated base (add_(x, x))?

Approving w/ comments

return ctx.wrap_tensors(unwrapped_outs) # type: ignore[arg-type]


def _do_auto_functionalize_v2_for_inplace_operator(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe this should be merged with _do_auto_functionalize_v2_for_generic_mutable_operator? If we don't care about specific perf then I think _do_auto_functionalize_v2_for_generic_mutable_operator will just do the same thing - otherwise we could make a helper that takes lambdas to compute the details slightly more optimally for the inplace operator case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

_do_auto_functionalize_v2_for_generic_mutable_operator -> the auto_functionalized in this case returns (output_of_operator, all_cloned_mutated_inputs).

in the inplace case, this would end up returning (cloned_mutated_input, cloned_mutated_input), which is redundant.

To make it look more like the native pytorch operator situation (where the functional variant returns just (cloned_mutated_input,)) we special case it here.

NB: the code for this is fairly simple, the inplace case is only one input being mutated and returned, so I'd prefer to have the code duplication around

Allow custom operators tagged with torch.Tag.inplace to go through
auto_functionalize during torch.compile. The functional form of e.g.
`foo_(Tensor(a!) self, Tensor other) -> Tensor(a!)` passes self as a
base in _all_bases (with view info) so that the reinplacing pass can
do clone analysis—unlike out= ops where the mutable args are write-only
and clones are never needed.

Test Plan:
- python test/test_inplace_tag.py

[ghstack-poisoned]
zou3519 added a commit that referenced this pull request Apr 24, 2026
Allow custom operators tagged with torch.Tag.inplace to go through
auto_functionalize during torch.compile. The functional form of e.g.
`foo_(Tensor(a!) self, Tensor other) -> Tensor(a!)` passes self as a
base in _all_bases (with view info) so that the reinplacing pass can
do clone analysis—unlike out= ops where the mutable args are write-only
and clones are never needed.

Test Plan:
- python test/test_inplace_tag.py

ghstack-source-id: 4630767
Pull Request resolved: #181100
@zou3519

zou3519 commented Apr 24, 2026

Copy link
Copy Markdown
Contributor Author

Does it need a test for a duplicated base (add_(x, x))?

I added the test. This one is interesting because a lot of custom operators won't support this pattern. The bad case is if the kernel is clobbering values it needs to read from

Allow custom operators tagged with torch.Tag.inplace to go through
auto_functionalize during torch.compile. The functional form of e.g.
`foo_(Tensor(a!) self, Tensor other) -> Tensor(a!)` passes self as a
base in _all_bases (with view info) so that the reinplacing pass can
do clone analysis—unlike out= ops where the mutable args are write-only
and clones are never needed.

Test Plan:
- python test/test_inplace_tag.py

[ghstack-poisoned]
zou3519 added a commit that referenced this pull request Apr 24, 2026
Allow custom operators tagged with torch.Tag.inplace to go through
auto_functionalize during torch.compile. The functional form of e.g.
`foo_(Tensor(a!) self, Tensor other) -> Tensor(a!)` passes self as a
base in _all_bases (with view info) so that the reinplacing pass can
do clone analysis—unlike out= ops where the mutable args are write-only
and clones are never needed.

Test Plan:
- python test/test_inplace_tag.py

ghstack-source-id: 8e63c22
Pull Request resolved: #181100
@zou3519 zou3519 added ciflow/trunk Trigger trunk jobs on your pull request release notes: composability release notes category labels Apr 24, 2026
@zou3519

zou3519 commented Apr 25, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@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

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 mandatory check(s) failed. The first few are:

Dig deeper by viewing the failures on hud

Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

@zou3519

zou3519 commented Apr 25, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -f "unrelated failure"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

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

@github-actions
github-actions Bot deleted the gh/zou3519/6/head branch May 25, 2026 02:31
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
)

Allow custom operators tagged with torch.Tag.inplace to go through
auto_functionalize during torch.compile. The functional form of e.g.
`foo_(Tensor(a!) self, Tensor other) -> Tensor(a!)` passes self as a
base in _all_bases (with view info) so that the reinplacing pass can
do clone analysis—unlike out= ops where the mutable args are write-only
and clones are never needed.

Test Plan:
- python test/test_inplace_tag.py
Pull Request resolved: pytorch#181100
Approved by: https://github.com/aorenste, https://github.com/angelayi
ghstack dependencies: pytorch#181380, pytorch#180987, pytorch#181099
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
)

Allow custom operators tagged with torch.Tag.inplace to go through
auto_functionalize during torch.compile. The functional form of e.g.
`foo_(Tensor(a!) self, Tensor other) -> Tensor(a!)` passes self as a
base in _all_bases (with view info) so that the reinplacing pass can
do clone analysis—unlike out= ops where the mutable args are write-only
and clones are never needed.

Test Plan:
- python test/test_inplace_tag.py
Pull Request resolved: pytorch#181100
Approved by: https://github.com/aorenste, https://github.com/angelayi
ghstack dependencies: pytorch#181380, pytorch#180987, pytorch#181099
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
)

Allow custom operators tagged with torch.Tag.inplace to go through
auto_functionalize during torch.compile. The functional form of e.g.
`foo_(Tensor(a!) self, Tensor other) -> Tensor(a!)` passes self as a
base in _all_bases (with view info) so that the reinplacing pass can
do clone analysis—unlike out= ops where the mutable args are write-only
and clones are never needed.

Test Plan:
- python test/test_inplace_tag.py
Pull Request resolved: pytorch#181100
Approved by: https://github.com/aorenste, https://github.com/angelayi
ghstack dependencies: pytorch#181380, pytorch#180987, pytorch#181099
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 Merged release notes: composability release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants