Skip to content

Formalize inplace operators with Tag.inplace - #181099

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

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

Conversation

@zou3519

@zou3519 zou3519 commented Apr 22, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

An inplace operator mutates its first positional argument and returns
it. For custom operators (defined via torch.library), the first argument
must be a plain Tensor(a!), must be the only mutable argument, and the
operator must return exactly that tensor. Native operators have relaxed
requirements (e.g. Tensor[] self, multiple mutable args) but this is
subject to change.

This PR adds a Tag.inplace tag, auto-applies it to all native inplace
operators via torchgen, and validates the schema when custom operators
are defined with this tag via torch.library.define.

Test Plan:

  • python test/custom_operator/test_inplace.py

Authored with Claude.

An inplace operator mutates its first positional argument and returns
it. For custom operators (defined via torch.library), the first argument
must be a plain Tensor(a!), must be the only mutable argument, and the
operator must return exactly that tensor. Native operators have relaxed
requirements (e.g. Tensor[] self, multiple mutable args) but this is
subject to change.

This PR adds a Tag.inplace tag, auto-applies it to all native inplace
operators via torchgen, and validates the schema when custom operators
are defined with this tag via torch.library.define.

Test Plan:
- python test/custom_operator/test_inplace.py

Authored with Claude.

[ghstack-poisoned]
@zou3519
zou3519 requested a review from ezyang as a code owner April 22, 2026 11:59
@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/181099

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 6e0403e 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.

Comment thread torch/library.py Outdated
Comment on lines +193 to +204
other_mutable = [
arg
for arg in args[1:]
if not arg.kwarg_only
if arg.alias_info is not None and arg.alias_info.is_write
]
kwarg_mutable = [
arg
for arg in args
if arg.kwarg_only
if arg.alias_info is not None and arg.alias_info.is_write
]

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.

nit: why not just check all args[1:]?

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.

AI slop, sorry :/

An inplace operator mutates its first positional argument and returns
it. For custom operators (defined via torch.library), the first argument
must be a plain Tensor(a!), must be the only mutable argument, and the
operator must return exactly that tensor. Native operators have relaxed
requirements (e.g. Tensor[] self, multiple mutable args) but this is
subject to change.

This PR adds a Tag.inplace tag, auto-applies it to all native inplace
operators via torchgen, and validates the schema when custom operators
are defined with this tag via torch.library.define.

Test Plan:
- python test/custom_operator/test_inplace.py

Authored with Claude.

[ghstack-poisoned]
An inplace operator mutates its first positional argument and returns
it. For custom operators (defined via torch.library), the first argument
must be a plain Tensor(a!), must be the only mutable argument, and the
operator must return exactly that tensor. Native operators have relaxed
requirements (e.g. Tensor[] self, multiple mutable args) but this is
subject to change.

This PR adds a Tag.inplace tag, auto-applies it to all native inplace
operators via torchgen, and validates the schema when custom operators
are defined with this tag via torch.library.define.

Test Plan:
- python test/custom_operator/test_inplace.py

Authored with Claude.

[ghstack-poisoned]
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #181100

1 similar comment
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #181100

pytorchmergebot pushed a commit that referenced this pull request Apr 25, 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: #181100
Approved by: https://github.com/aorenste, https://github.com/angelayi
ghstack dependencies: #181380, #180987, #181099
@github-actions
github-actions Bot deleted the gh/zou3519/5/head branch May 25, 2026 02:31
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
An inplace operator mutates its first positional argument and returns
it. For custom operators (defined via torch.library), the first argument
must be a plain Tensor(a!), must be the only mutable argument, and the
operator must return exactly that tensor. Native operators have relaxed
requirements (e.g. Tensor[] self, multiple mutable args) but this is
subject to change.

This PR adds a Tag.inplace tag, auto-applies it to all native inplace
operators via torchgen, and validates the schema when custom operators
are defined with this tag via torch.library.define.

Test Plan:
- python test/custom_operator/test_inplace.py

Authored with Claude.
Pull Request resolved: pytorch#181099
Approved by: https://github.com/angelayi
ghstack dependencies: pytorch#181380, pytorch#180987
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
An inplace operator mutates its first positional argument and returns
it. For custom operators (defined via torch.library), the first argument
must be a plain Tensor(a!), must be the only mutable argument, and the
operator must return exactly that tensor. Native operators have relaxed
requirements (e.g. Tensor[] self, multiple mutable args) but this is
subject to change.

This PR adds a Tag.inplace tag, auto-applies it to all native inplace
operators via torchgen, and validates the schema when custom operators
are defined with this tag via torch.library.define.

Test Plan:
- python test/custom_operator/test_inplace.py

Authored with Claude.
Pull Request resolved: pytorch#181099
Approved by: https://github.com/angelayi
ghstack dependencies: pytorch#181380, pytorch#180987
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
An inplace operator mutates its first positional argument and returns
it. For custom operators (defined via torch.library), the first argument
must be a plain Tensor(a!), must be the only mutable argument, and the
operator must return exactly that tensor. Native operators have relaxed
requirements (e.g. Tensor[] self, multiple mutable args) but this is
subject to change.

This PR adds a Tag.inplace tag, auto-applies it to all native inplace
operators via torchgen, and validates the schema when custom operators
are defined with this tag via torch.library.define.

Test Plan:
- python test/custom_operator/test_inplace.py

Authored with Claude.
Pull Request resolved: pytorch#181099
Approved by: https://github.com/angelayi
ghstack dependencies: pytorch#181380, pytorch#180987
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants