Skip to content

Formalize out= operators and custom operators definition - #180851

Closed
zou3519 wants to merge 10 commits into
gh/zou3519/1/basefrom
gh/zou3519/1/head
Closed

zou3519 wants to merge 10 commits into
gh/zou3519/1/basefrom
gh/zou3519/1/head

Conversation

@zou3519

@zou3519 zou3519 commented Apr 20, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

An out= operator must:

  • have the "Tag::out" tag.
  • All of the mutable arguments must be write-only buffers (not read
    before write)
  • it must return all mutable arguments in order.
  • all of the mutable arguments must be kwarg-only arguments.

The last three restrictions are already restrictions that torchgen
places on native pytorch out= operators.

This PR:

  • renames "Tag::out_variant" to "Tag::out". This is not being used in the wild
    so there are no BC-concerns. We added this API in PyTorch 2.10 but
    didn't have a good user. I prefer not calling it a "variant" because a
    user may just have a custom operator that is an "out operator" and not
    have a functional variant of the operator, so I don't want to call it
    an "out variant".
  • makes it so that all native out=operators get the out tag too.
  • torch.library.define (the Python api) validates the schema for the
    above three constraints.

Test Plan:

  • existing and new tests
  • previously we did allow Tag::out_variant ops to return None/void.
    Again, this is not actually being used in the wild, so I'm breaking BC
    here.

Authored with Claude.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

An out= operator must:
- have the "Tag::out" tag.
- All of the mutable arguments must be write-only buffers (not read
  before write)
- it must return all mutable arguments in order.
- all of the mutable arguments must be kwarg-only arguments.

The last three restrictions are already restrictions that torchgen
places on native pytorch out= operators.

This PR:
- renames "Tag::out_variant" to "Tag::out". This is not being used in the wild
  so there are no BC-concerns. We added this API in PyTorch 2.10 but
  didn't have a good user. I prefer not calling it a "variant" because a
  user may just have a custom operator that is an "out operator" and not
  have a functional variant of the operator, so I don't want to call it
  an "out variant".
- makes it so that all native out=operators get the out tag too.
- torch.library.define (the Python api) validates the schema for the
  above three constraints.

Test Plan:
- existing and new tests
- previously we did allow `Tag::out_variant` ops to return None/void.
  Again, this is not actually being used in the wild, so I'm breaking BC
  here.

Authored with Claude.

[ghstack-poisoned]
@zou3519
zou3519 requested a review from ezyang as a code owner April 20, 2026 14:43
@pytorch-bot

pytorch-bot Bot commented Apr 20, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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 70a5f06 with merge base b72c0ac (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 20, 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.

An out= operator must:
- have the "Tag::out" tag.
- All of the mutable arguments must be write-only buffers (not read
  before write)
- it must return all mutable arguments in order.
- all of the mutable arguments must be kwarg-only arguments.

The last three restrictions are already restrictions that torchgen
places on native pytorch out= operators.

This PR:
- renames "Tag::out_variant" to "Tag::out". This is not being used in the wild
  so there are no BC-concerns. We added this API in PyTorch 2.10 but
  didn't have a good user. I prefer not calling it a "variant" because a
  user may just have a custom operator that is an "out operator" and not
  have a functional variant of the operator, so I don't want to call it
  an "out variant".
- makes it so that all native out=operators get the out tag too.
- torch.library.define (the Python api) validates the schema for the
  above three constraints.

Test Plan:
- existing and new tests
- previously we did allow `Tag::out_variant` ops to return None/void.
  Again, this is not actually being used in the wild, so I'm breaking BC
  here.

Authored with Claude.

[ghstack-poisoned]
@zou3519
zou3519 requested review from albanD, angelayi and eellison April 20, 2026 15:02
This tag indicates that an operator performs a reduction operation, computing aggregate values
(sum, mean, max, min, etc.) across one or more dimensions of the input tensor(s).
- tag: out_variant
- tag: out

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we have a linter we can run to check if this tag is missing on the op when it should be there?

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.

I'm not convinced a linter is able to detect this. There is testing in this PR.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yeah, I was thinking if we could check that it a kwarg out= implies it should have this tag.

Comment thread torch/library.py Resolved
zou3519 added 3 commits April 21, 2026 06:16
An out= operator must:
- have the "Tag::out" tag.
- All of the mutable arguments must be write-only buffers (not read
  before write)
- it must return all mutable arguments in order.
- all of the mutable arguments must be kwarg-only arguments.

The last three restrictions are already restrictions that torchgen
places on native pytorch out= operators.

This PR:
- renames "Tag::out_variant" to "Tag::out". This is not being used in the wild
  so there are no BC-concerns. We added this API in PyTorch 2.10 but
  didn't have a good user. I prefer not calling it a "variant" because a
  user may just have a custom operator that is an "out operator" and not
  have a functional variant of the operator, so I don't want to call it
  an "out variant".
- makes it so that all native out=operators get the out tag too.
- torch.library.define (the Python api) validates the schema for the
  above three constraints.

Test Plan:
- existing and new tests
- previously we did allow `Tag::out_variant` ops to return None/void.
  Again, this is not actually being used in the wild, so I'm breaking BC
  here.

Authored with Claude.

[ghstack-poisoned]
An out= operator must:
- have the "Tag::out" tag.
- All of the mutable arguments must be write-only buffers (not read
  before write)
- it must return all mutable arguments in order.
- all of the mutable arguments must be kwarg-only arguments.

The last three restrictions are already restrictions that torchgen
places on native pytorch out= operators.

This PR:
- renames "Tag::out_variant" to "Tag::out". This is not being used in the wild
  so there are no BC-concerns. We added this API in PyTorch 2.10 but
  didn't have a good user. I prefer not calling it a "variant" because a
  user may just have a custom operator that is an "out operator" and not
  have a functional variant of the operator, so I don't want to call it
  an "out variant".
- makes it so that all native out=operators get the out tag too.
- torch.library.define (the Python api) validates the schema for the
  above three constraints.

Test Plan:
- existing and new tests
- previously we did allow `Tag::out_variant` ops to return None/void.
  Again, this is not actually being used in the wild, so I'm breaking BC
  here.

Authored with Claude.

[ghstack-poisoned]
An out= operator must:
- have the "Tag::out" tag.
- All of the mutable arguments must be write-only buffers (not read
  before write)
- it must return all mutable arguments in order.
- all of the mutable arguments must be kwarg-only arguments.

The last three restrictions are already restrictions that torchgen
places on native pytorch out= operators.

This PR:
- renames "Tag::out_variant" to "Tag::out". This is not being used in the wild
  so there are no BC-concerns. We added this API in PyTorch 2.10 but
  didn't have a good user. I prefer not calling it a "variant" because a
  user may just have a custom operator that is an "out operator" and not
  have a functional variant of the operator, so I don't want to call it
  an "out variant".
- makes it so that all native out=operators get the out tag too.
- torch.library.define (the Python api) validates the schema for the
  above three constraints.

Test Plan:
- existing and new tests
- previously we did allow `Tag::out_variant` ops to return None/void.
  Again, this is not actually being used in the wild, so I'm breaking BC
  here.

Authored with Claude.

[ghstack-poisoned]
@albanD

albanD commented Apr 21, 2026

Copy link
Copy Markdown
Collaborator

@claude review this

@claude

claude Bot commented Apr 21, 2026 •

Copy link
Copy Markdown

Claude finished @albanD's task in 4m 33s —— View job


PR Review: #180851

Summary

This PR formalizes the definition of out= operators by renaming Tag::out_variant to Tag::out, adding schema validation at Library.define() time, and auto-tagging all native out= ops. The design is clean and the validation logic is thorough. I found one bug (stale reference to old tag name in another test file) and a few items worth discussing.

Code Quality

  1. torch/library.py:117 — zip should use strict=True (agreeing with Skylion007's review comment). The length check on line 111 guarantees equal lengths, so strict=True is purely defensive, but it's good practice and costs nothing:

    for i, (ret, arg) in enumerate(zip(returns, mutable_args, strict=True)):
  2. torch/_library/_out_variant.py:65-82 — _has_valid_out_variant_returns still accepts void returns. This function allows len(schema.returns) == 0 (line 70-71), while the new _validate_out_schema rejects it. Since _has_valid_out_variant_returns is called from to_out_variant() (line 128), and to_out_variant only considers ops with torch.Tag.out, the divergence is unlikely to cause real issues — any op defined via Library.define() with the out tag will have already passed the stricter validation. But it would be cleaner to update _has_valid_out_variant_returns to reject void returns too, so the invariants are consistent everywhere. Consider removing the early-return for zero returns now that _validate_out_schema guarantees it won't happen for tagged ops.

  3. torch/library.py:237 — duplicate name = schema.split("(")[0]. This line is identical to line 227 above it. Not introduced by this PR (line 237 was already there), but worth noting since the diff touches adjacent code.

Testing

  1. test/inductor/test_custom_op_out_lowering.py:29,72 — still references torch.Tag.out_variant. This file was not updated in the PR and uses the old tag name in two places. This will break at runtime since the out_variant tag no longer exists in tags.yaml. These need to be updated to torch.Tag.out.
    Fix this →

  2. test/custom_operator/test_out_variant.py — author's TODO at line 238 (review comment) for torchgen branch tests. zou3519 already flagged this. The new auto-tagging logic in torchgen/model.py and torchgen/native_function_generation.py is tested indirectly via test_is_out_native, which verifies that torch.ops.aten.abs.out and torch.ops.aten.randn_like.out have the tag. That covers both the hand-written and auto-generated paths. More targeted torchgen unit tests would be ideal but the runtime coverage is reasonable.

  3. test_compile_out_variant (line 197-224) — void-returning out= op deliberately avoids the tag. The TODO comment explains this is blocked on torch.compile support for aliased returns. This is fine as-is, but it highlights a design gap: out= operators used with torch.compile currently cannot use the out tag if they return void. Worth tracking as a follow-up.

API Design

  1. The is_out utility (torch/_library/utils.py:79-82) is a thin wrapper over tag checking. This is appropriate — it provides a semantic name for the check and centralizes it. However, the docstring says "write-only output buffers that are not read from" which describes the semantic contract but is not enforced by the tag itself (the tag only validates schema structure, not runtime behavior). Consider softening to "indicates out= semantics" or document that write-only behavior is the caller's responsibility.

Backward Compatibility

  1. Renaming Tag::out_variant → Tag::out is a breaking change. The PR description acknowledges this and states the API is not used in the wild (added in PyTorch 2.10). However, the existence of test/inductor/test_custom_op_out_lowering.py using the old name within this very repo suggests it is used internally. There is no backwards compatibility shim (Tag.out_variant = Tag.out). Given this was only added recently and the PR author states it's not used externally, this is acceptable, but the inductor test file needs updating (see item 4).

Recommendation

Request Changes

The test/inductor/test_custom_op_out_lowering.py file still references torch.Tag.out_variant which will break since the tag was removed from tags.yaml. This must be fixed. The zip(..., strict=True) suggestion is minor but aligned with PyTorch conventions.


An out= operator must:
- have the "Tag::out" tag.
- All of the mutable arguments must be write-only buffers (not read
  before write)
- it must return all mutable arguments in order.
- all of the mutable arguments must be kwarg-only arguments.

The last three restrictions are already restrictions that torchgen
places on native pytorch out= operators.

This PR:
- renames "Tag::out_variant" to "Tag::out". This is not being used in the wild
  so there are no BC-concerns. We added this API in PyTorch 2.10 but
  didn't have a good user. I prefer not calling it a "variant" because a
  user may just have a custom operator that is an "out operator" and not
  have a functional variant of the operator, so I don't want to call it
  an "out variant".
- makes it so that all native out=operators get the out tag too.
- torch.library.define (the Python api) validates the schema for the
  above three constraints.

Test Plan:
- existing and new tests
- previously we did allow `Tag::out_variant` ops to return None/void.
  Again, this is not actually being used in the wild, so I'm breaking BC
  here.

Authored with Claude.

[ghstack-poisoned]
An out= operator must:
- have the "Tag::out" tag.
- All of the mutable arguments must be write-only buffers (not read
  before write)
- it must return all mutable arguments in order.
- all of the mutable arguments must be kwarg-only arguments.

The last three restrictions are already restrictions that torchgen
places on native pytorch out= operators.

This PR:
- renames "Tag::out_variant" to "Tag::out". This is not being used in the wild
  so there are no BC-concerns. We added this API in PyTorch 2.10 but
  didn't have a good user. I prefer not calling it a "variant" because a
  user may just have a custom operator that is an "out operator" and not
  have a functional variant of the operator, so I don't want to call it
  an "out variant".
- makes it so that all native out=operators get the out tag too.
- torch.library.define (the Python api) validates the schema for the
  above three constraints.

Test Plan:
- existing and new tests
- previously we did allow `Tag::out_variant` ops to return None/void.
  Again, this is not actually being used in the wild, so I'm breaking BC
  here.

Authored with Claude.

[ghstack-poisoned]
@pytorch-bot pytorch-bot Bot added the ciflow/torchtitan Run TorchTitan integration tests label Apr 21, 2026
Comment thread torch/library.py Outdated
f"Got: {schema}"
)
for i, (ret, arg) in enumerate(zip(returns, mutable_args, strict=True)):
if ret.alias_info is None or arg.alias_info is None:

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: since we already know all of the mutable_args have alias_info is not None?

Suggested change
if ret.alias_info is None or arg.alias_info is None:
if ret.alias_info is None:

zou3519 added 3 commits April 21, 2026 11:03
An out= operator must:
- have the "Tag::out" tag.
- All of the mutable arguments must be write-only buffers (not read
  before write)
- it must return all mutable arguments in order.
- all of the mutable arguments must be kwarg-only arguments.

The last three restrictions are already restrictions that torchgen
places on native pytorch out= operators.

This PR:
- renames "Tag::out_variant" to "Tag::out". This is not being used in the wild
  so there are no BC-concerns. We added this API in PyTorch 2.10 but
  didn't have a good user. I prefer not calling it a "variant" because a
  user may just have a custom operator that is an "out operator" and not
  have a functional variant of the operator, so I don't want to call it
  an "out variant".
- makes it so that all native out=operators get the out tag too.
- torch.library.define (the Python api) validates the schema for the
  above three constraints.

Test Plan:
- existing and new tests
- previously we did allow `Tag::out_variant` ops to return None/void.
  Again, this is not actually being used in the wild, so I'm breaking BC
  here.

Authored with Claude.

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy kadeng muchulee8 amjames chauhang aakhundov coconutruben jataylo

[ghstack-poisoned]
An out= operator must:
- have the "Tag::out" tag.
- All of the mutable arguments must be write-only buffers (not read
  before write)
- it must return all mutable arguments in order.
- all of the mutable arguments must be kwarg-only arguments.

The last three restrictions are already restrictions that torchgen
places on native pytorch out= operators.

This PR:
- renames "Tag::out_variant" to "Tag::out". This is not being used in the wild
  so there are no BC-concerns. We added this API in PyTorch 2.10 but
  didn't have a good user. I prefer not calling it a "variant" because a
  user may just have a custom operator that is an "out operator" and not
  have a functional variant of the operator, so I don't want to call it
  an "out variant".
- makes it so that all native out=operators get the out tag too.
- torch.library.define (the Python api) validates the schema for the
  above three constraints.

Test Plan:
- existing and new tests
- previously we did allow `Tag::out_variant` ops to return None/void.
  Again, this is not actually being used in the wild, so I'm breaking BC
  here.

Authored with Claude.

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy kadeng muchulee8 amjames chauhang aakhundov coconutruben jataylo

[ghstack-poisoned]
An out= operator must:
- have the "Tag::out" tag.
- All of the mutable arguments must be write-only buffers (not read
  before write)
- it must return all mutable arguments in order.
- all of the mutable arguments must be kwarg-only arguments.

The last three restrictions are already restrictions that torchgen
places on native pytorch out= operators.

This PR:
- renames "Tag::out_variant" to "Tag::out". This is not being used in the wild
  so there are no BC-concerns. We added this API in PyTorch 2.10 but
  didn't have a good user. I prefer not calling it a "variant" because a
  user may just have a custom operator that is an "out operator" and not
  have a functional variant of the operator, so I don't want to call it
  an "out variant".
- makes it so that all native out=operators get the out tag too.
- torch.library.define (the Python api) validates the schema for the
  above three constraints.

Test Plan:
- existing and new tests
- previously we did allow `Tag::out_variant` ops to return None/void.
  Again, this is not actually being used in the wild, so I'm breaking BC
  here.

Authored with Claude.

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy kadeng muchulee8 amjames chauhang aakhundov coconutruben jataylo

[ghstack-poisoned]
@zou3519 zou3519 added the release notes: composability release notes category label Apr 22, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #180987

pytorchmergebot pushed a commit that referenced this pull request Apr 22, 2026
Allow custom operators tagged with torch.Tag.out to go through
auto_functionalize during torch.compile. Previously these were
rejected because their returns have alias annotations.

- the functional form of e.g. `foo_out(Tensor x, *, Tensor(a!) out) -> Tensor(a!)`
  is auto_functionalized(foo, x, out_shape, out_stride, out_dtype, out_device).
  The semantics of this operator are
  `foo(Tensor x) -> Tensor`. This is because the out= args are
  write-only, and we need to store the metadata to know how to allocate
  the output.
- Re-inplacing this operator is easy. We just use empty_like to allocate
  the output tensor and call foo_out.

Authored with Claude.

Pull Request resolved: #180852
Approved by: https://github.com/angelayi
ghstack dependencies: #180851
pytorchmergebot pushed a commit that referenced this pull request Apr 22, 2026
Operators tagged with Tag.out have well-defined fake kernel semantics:
return the out= arguments in declaration order. This extends the
existing auto-fake-kernel mechanism (which handles mutable ops with no
returns) to also cover Tag.out ops, removing the need for users to
manually write trivial fake/meta kernels for these operators.

Also warns when users manually register a Meta kernel for a non-builtin
Tag.out operator, since the automatic registration is preferred and the
manual version is easy to get wrong.

Authored with Claude.
Pull Request resolved: #180987
Approved by: https://github.com/angelayi
ghstack dependencies: #180851, #180852
pytorchmergebot pushed a commit that referenced this pull request Apr 23, 2026
…81098)

test/custom_operator/** tests are not actually run in CI (except for one
of them...)
Pull Request resolved: #181098
Approved by: https://github.com/angelayi
ghstack dependencies: #180851, #180852, #180987
@github-actions
github-actions Bot deleted the gh/zou3519/1/head branch May 23, 2026 02:28
Comment thread torch/_inductor/ir.py
# Try multi-output .out() lowering for custom ops with the out tag.
if (
isinstance(kernel, torch._ops.OpOverload)
and not torch._library.utils.is_builtin(kernel)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@zou3519 Why are builtins disabled from this pass? If its to preserve existing functional behaviour could we add a config option (I'm happy to add this if its okay)? For example we can use the memory pool functionality supported in inductor with out= aten overloads

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.

this was just to preserve existing intended behavior, which was that this code only applies to custom operators

aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
)

An out= operator must:
- have the "Tag::out" tag.
- All of the mutable arguments must be write-only buffers (not read
  before write)
- it must return all mutable arguments in order.
- all of the mutable arguments must be kwarg-only arguments.

The last three restrictions are already restrictions that torchgen
places on native pytorch out= operators.

This PR:
- renames "Tag::out_variant" to "Tag::out". This is not being used in the wild
  so there are no BC-concerns. We added this API in PyTorch 2.10 but
  didn't have a good user. I prefer not calling it a "variant" because a
  user may just have a custom operator that is an "out operator" and not
  have a functional variant of the operator, so I don't want to call it
  an "out variant".
- makes it so that all native out=operators get the out tag too.
- torch.library.define (the Python api) validates the schema for the
  above three constraints.

Test Plan:
- existing and new tests
- previously we did allow `Tag::out_variant` ops to return None/void.
  Again, this is not actually being used in the wild, so I'm breaking BC
  here.

Authored with Claude.

Pull Request resolved: pytorch#180851
Approved by: https://github.com/angelayi
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
Allow custom operators tagged with torch.Tag.out to go through
auto_functionalize during torch.compile. Previously these were
rejected because their returns have alias annotations.

- the functional form of e.g. `foo_out(Tensor x, *, Tensor(a!) out) -> Tensor(a!)`
  is auto_functionalized(foo, x, out_shape, out_stride, out_dtype, out_device).
  The semantics of this operator are
  `foo(Tensor x) -> Tensor`. This is because the out= args are
  write-only, and we need to store the metadata to know how to allocate
  the output.
- Re-inplacing this operator is easy. We just use empty_like to allocate
  the output tensor and call foo_out.

Authored with Claude.

Pull Request resolved: pytorch#180852
Approved by: https://github.com/angelayi
ghstack dependencies: pytorch#180851
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
Operators tagged with Tag.out have well-defined fake kernel semantics:
return the out= arguments in declaration order. This extends the
existing auto-fake-kernel mechanism (which handles mutable ops with no
returns) to also cover Tag.out ops, removing the need for users to
manually write trivial fake/meta kernels for these operators.

Also warns when users manually register a Meta kernel for a non-builtin
Tag.out operator, since the automatic registration is preferred and the
manual version is easy to get wrong.

Authored with Claude.
Pull Request resolved: pytorch#180987
Approved by: https://github.com/angelayi
ghstack dependencies: pytorch#180851, pytorch#180852
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
)

An out= operator must:
- have the "Tag::out" tag.
- All of the mutable arguments must be write-only buffers (not read
  before write)
- it must return all mutable arguments in order.
- all of the mutable arguments must be kwarg-only arguments.

The last three restrictions are already restrictions that torchgen
places on native pytorch out= operators.

This PR:
- renames "Tag::out_variant" to "Tag::out". This is not being used in the wild
  so there are no BC-concerns. We added this API in PyTorch 2.10 but
  didn't have a good user. I prefer not calling it a "variant" because a
  user may just have a custom operator that is an "out operator" and not
  have a functional variant of the operator, so I don't want to call it
  an "out variant".
- makes it so that all native out=operators get the out tag too.
- torch.library.define (the Python api) validates the schema for the
  above three constraints.

Test Plan:
- existing and new tests
- previously we did allow `Tag::out_variant` ops to return None/void.
  Again, this is not actually being used in the wild, so I'm breaking BC
  here.

Authored with Claude.

Pull Request resolved: pytorch#180851
Approved by: https://github.com/angelayi
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
Allow custom operators tagged with torch.Tag.out to go through
auto_functionalize during torch.compile. Previously these were
rejected because their returns have alias annotations.

- the functional form of e.g. `foo_out(Tensor x, *, Tensor(a!) out) -> Tensor(a!)`
  is auto_functionalized(foo, x, out_shape, out_stride, out_dtype, out_device).
  The semantics of this operator are
  `foo(Tensor x) -> Tensor`. This is because the out= args are
  write-only, and we need to store the metadata to know how to allocate
  the output.
- Re-inplacing this operator is easy. We just use empty_like to allocate
  the output tensor and call foo_out.

Authored with Claude.

Pull Request resolved: pytorch#180852
Approved by: https://github.com/angelayi
ghstack dependencies: pytorch#180851
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
Operators tagged with Tag.out have well-defined fake kernel semantics:
return the out= arguments in declaration order. This extends the
existing auto-fake-kernel mechanism (which handles mutable ops with no
returns) to also cover Tag.out ops, removing the need for users to
manually write trivial fake/meta kernels for these operators.

Also warns when users manually register a Meta kernel for a non-builtin
Tag.out operator, since the automatic registration is preferred and the
manual version is easy to get wrong.

Authored with Claude.
Pull Request resolved: pytorch#180987
Approved by: https://github.com/angelayi
ghstack dependencies: pytorch#180851, pytorch#180852
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
)

An out= operator must:
- have the "Tag::out" tag.
- All of the mutable arguments must be write-only buffers (not read
  before write)
- it must return all mutable arguments in order.
- all of the mutable arguments must be kwarg-only arguments.

The last three restrictions are already restrictions that torchgen
places on native pytorch out= operators.

This PR:
- renames "Tag::out_variant" to "Tag::out". This is not being used in the wild
  so there are no BC-concerns. We added this API in PyTorch 2.10 but
  didn't have a good user. I prefer not calling it a "variant" because a
  user may just have a custom operator that is an "out operator" and not
  have a functional variant of the operator, so I don't want to call it
  an "out variant".
- makes it so that all native out=operators get the out tag too.
- torch.library.define (the Python api) validates the schema for the
  above three constraints.

Test Plan:
- existing and new tests
- previously we did allow `Tag::out_variant` ops to return None/void.
  Again, this is not actually being used in the wild, so I'm breaking BC
  here.

Authored with Claude.

Pull Request resolved: pytorch#180851
Approved by: https://github.com/angelayi
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.out to go through
auto_functionalize during torch.compile. Previously these were
rejected because their returns have alias annotations.

- the functional form of e.g. `foo_out(Tensor x, *, Tensor(a!) out) -> Tensor(a!)`
  is auto_functionalized(foo, x, out_shape, out_stride, out_dtype, out_device).
  The semantics of this operator are
  `foo(Tensor x) -> Tensor`. This is because the out= args are
  write-only, and we need to store the metadata to know how to allocate
  the output.
- Re-inplacing this operator is easy. We just use empty_like to allocate
  the output tensor and call foo_out.

Authored with Claude.

Pull Request resolved: pytorch#180852
Approved by: https://github.com/angelayi
ghstack dependencies: pytorch#180851
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Operators tagged with Tag.out have well-defined fake kernel semantics:
return the out= arguments in declaration order. This extends the
existing auto-fake-kernel mechanism (which handles mutable ops with no
returns) to also cover Tag.out ops, removing the need for users to
manually write trivial fake/meta kernels for these operators.

Also warns when users manually register a Meta kernel for a non-builtin
Tag.out operator, since the automatic registration is preferred and the
manual version is easy to get wrong.

Authored with Claude.
Pull Request resolved: pytorch#180987
Approved by: https://github.com/angelayi
ghstack dependencies: pytorch#180851, pytorch#180852
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 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.

6 participants