Skip to content

Move at::Tag to headeronly, add library.def override to allow tags (#180190) - #180190

Closed
janeyx99 wants to merge 1 commit into
mainfrom
move-at-tags
Closed

janeyx99 wants to merge 1 commit into
mainfrom
move-at-tags

Conversation

@janeyx99

@janeyx99 janeyx99 commented Apr 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary:
Custom ops benefit from using tags. Let's make them usable--they're already headeronly (it's an enum) but the major pain of this change is needing to codev it and fight internal. Claude was v helpful tho!

This PR moves at::Tag to torch::headeronly::Tag and adds a shim for stable Library.def to be able to take in tags.

Differential Revision: D100546620

Pulled By: janeyx99

@pytorch-bot

pytorch-bot Bot commented Apr 12, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 2 New Failures, 6 Unrelated Failures

As of commit 398f987 with merge base 5c3f8fd (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

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

@github-actions

Copy link
Copy Markdown
Contributor

Attention! PyTorch one of the C-stable API file was changed

You MUST NOT change existing function declarations in this, as this header defines a stable C ABI. If you need to change the signature for a function, introduce a new v2 version of the function and modify code generation to target the new version of the function.


Caused by:

@meta-codesync

meta-codesync Bot commented Apr 12, 2026

Copy link
Copy Markdown

@janeyx99 has imported this pull request. If you are a Meta employee, you can view this in D100546620.

@meta-codesync meta-codesync Bot changed the title Move at::Tag to headeronly, add test for using them [pytorch][PR] Move at::Tag to headeronly, add test for using them Apr 13, 2026
@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Apr 13, 2026
@meta-codesync

meta-codesync Bot commented Apr 13, 2026

Copy link
Copy Markdown

@janeyx99 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D100546620.

@janeyx99 janeyx99 changed the title [pytorch][PR] Move at::Tag to headeronly, add test for using them Move at::Tag to headeronly, add test for using them Apr 14, 2026
Comment on lines +29 to +30
aoti_torch_dtype_float4_e2m1fn_x2: TORCH_VERSION_2_12_0
aoti_torch_dtype_float8_e8m0fnu: TORCH_VERSION_2_12_0

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.

These are from a previous commit #176496 that our linter wasn't triggered on

@janeyx99
janeyx99 marked this pull request as ready for review April 14, 2026 20:11
@janeyx99
janeyx99 requested review from albanD and zou3519 April 14, 2026 20:11
@janeyx99 janeyx99 changed the title Move at::Tag to headeronly, add test for using them Move at::Tag to headeronly, add library.def override to allow tags Apr 14, 2026
Comment thread torchgen/gen.py
@@ -2101,6 +2101,7 @@ def gen_headers(
static_dispatch_idx: list[BackendIndex],
selector: SelectiveBuilder,
backend_indices: dict[DispatchKey, BackendIndex],
headeronly_fm: FileManager,

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.

These changes look reasonable to me but I am not sure who the best reviewer would be. Also my inclination is that we don't care about the BC lint here?

@albanD albanD left a comment

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.

Small nit on test, but sounds good otherwise!


STABLE_TORCH_LIBRARY_FRAGMENT(STABLE_LIB_NAME, m) {
m.def("tagged_identity(Tensor t) -> Tensor",
{torch::headeronly::Tag::pointwise});

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.

Can you do a test with multiple tags?

@meta-codesync meta-codesync Bot changed the title Move at::Tag to headeronly, add library.def override to allow tags Move at::Tag to headeronly, add test for using them (#180190) Apr 15, 2026
meta-codesync Bot pushed a commit that referenced this pull request Apr 15, 2026
Pulled By:
janeyx99


janeyx99

Differential Revision: D100546620
@janeyx99 janeyx99 changed the title Move at::Tag to headeronly, add test for using them (#180190) Move at::Tag to headeronly, add library.def override to allow tags Apr 15, 2026
@meta-codesync meta-codesync Bot changed the title Move at::Tag to headeronly, add library.def override to allow tags Move at::Tag to headeronly, add library.def override to allow tags (#180190) Apr 16, 2026
meta-codesync Bot pushed a commit that referenced this pull request Apr 16, 2026
…180190)

Pulled By:
janeyx99


janeyx99


janeyx99

Differential Revision: D100546620
meta-codesync Bot pushed a commit that referenced this pull request Apr 16, 2026
…180190)

Pulled By:
janeyx99


janeyx99


janeyx99

Differential Revision: D100546620
pytorchmergebot pushed a commit that referenced this pull request Apr 24, 2026
Copilot AI pushed a commit to intel/torch-xpu-ops that referenced this pull request Apr 27, 2026
The best scenario is to not have a different copy here, but this fix
will unblock my PR's broken XPU build.
pytorch/pytorch#180190
Co-authored-by: chuanqi129 <[email protected]>
@janeyx99

Copy link
Copy Markdown
Contributor Author

Landed in #181161

@janeyx99 janeyx99 closed this Apr 27, 2026
malfet pushed a commit that referenced this pull request Apr 28, 2026
* [pytorch][PR] Reland D100546620: Move at::Tag to headeronly, add library.def override to allow tags (#181161)

Summary:
Reland of D100546620 which was reverted by D101431332 due to breaking libhammer.so.

Custom ops benefit from using tags. This PR moves at::Tag to torch::headeronly::Tag and adds a shim for stable Library.def to be able to take in tags.

Note: The two BUILD.bazel changes from the original diff were already present on master (the revert did not touch them), so this reland omits those files.

Old PR was: #180190

Differential Revision: D102031366

Pull Request resolved: #181161
Approved by: https://github.com/seemethere, https://github.com/atalman

* Change default of torchgen headeronly-install-dir to depend on install-dir (#181434)

This is a "forward fix" of #181161 and will need to be cherrypicked to make sure xpu builds still work. To describe the issue:
- torch-xpu-ops has its own version of tags.yaml
- the natural default of headeronly-install-dir had the XPU build overriding the enum_tags.h, and since their tag.yaml weren't the same as ours, we get build issues
- I tried fixing this before by just aligning the torch-xpu-ops version of tags.yaml but we're still adding more tags so that problem will come back
- I finally decided the fix is just to make the the default doesn't have the install locations colliding even if it makes the default for torchgen a tad more complicated.

Test plan:
make sure the XPU build on the PR stacked on top of this one passes.

Pull Request resolved: #181434
Approved by: https://github.com/EikanWang, https://github.com/wdvr
jafraustro pushed a commit to jafraustro/torch-xpu-ops that referenced this pull request Apr 29, 2026
The best scenario is to not have a different copy here, but this fix
will unblock my PR's broken XPU build.
pytorch/pytorch#180190
@github-actions
github-actions Bot deleted the move-at-tags branch May 28, 2026 02:27
pytorchmergebot pushed a commit that referenced this pull request Jun 13, 2026
…ml (#181233)

# Motivation
Currently, PyTorch XPU ATen ops are split across two locations:
- operations that call oneDNN (e.g., GEMM, convolution) reside in [ATen/native/mkldnn/xpu](https://github.com/pytorch/pytorch/tree/main/aten/src/ATen/native/mkldnn/xpu);
- while those implemented with SYCL are located [in torch-xpu-ops](https://github.com/intel/torch-xpu-ops/tree/main/src/ATen/native/xpu).

The operations in PyTorch are declared and registered in [pytorch/aten/src/ATen/native/native_functions.yaml](https://github.com/pytorch/pytorch/blob/main/aten/src/ATen/native/native_functions.yaml#L651), whereas the SYCL-based operations in torch-xpu-ops are declared and registered in [yaml/native/native_functions.yaml](https://github.com/intel/torch-xpu-ops/blob/main/yaml/native/native_functions.yaml#L87).

This setup introduces a complication:
When building PyTorch with XPU support, `torch-gen` needs to be run twice:
- once during the build of torch-xpu at [here](https://github.com/pytorch/pytorch/blob/5aac4cfce409ae24f7f0fe3e95f9a3cf5bdf4a28/cmake/Codegen.cmake#L232-L241) and
- again during the build of torch-xpu-ops at [here](https://github.com/intel/torch-xpu-ops/blob/8d373ba272f9fed348c7684bac4a0c2663844bbd/cmake/Codegen.cmake#L99-L125).

This solution introduces two issues (for more details, see [Details](#details)):
1.	We need to maintain an additional `native_functions.yaml` in torch-xpu-ops, which may lead to unexpected behavior if it becomes out of sync with the one in PyTorch. For example, changes to `codegen` (e.g., #180190) may break XPU CI because XPU ATen kernel registrations are out-of-tree. This creates additional maintenance overhead for both PyTorch and torch-xpu-ops.
2.	It creates a build dependency; torch-xpu-ops must wait for the torch-cpu build to finish, which increases overall build time and complexity. It also retriggers the re-compile XPU (depends on libtorch_cpu.so) due to any CPU code change.

To resolve the issues mentioned above, we would like to migrate XPU ATen kernel registration to `native_functions.yaml` in PyTorch and reuse its codegen fully.

# Solution
If we could move the ops declarations from torch-xpu-ops into torch’s native_functions.yaml, both problems would be resolved.
The key benefits:
1. Single code generation pass – run torch-gen only once instead of twice, simple and more efficient build process
2. Eliminate build dependency – enable parallel builds of torch-cpu and torch-xpu-ops
3. Single source of truth – maintain only one `native_functions.yaml` file, prevent XPU CI break by torchgen and YAML changes.

# Additional Context
## Details
<img width="1661" height="966" alt="image" src="https://github.com/user-attachments/assets/06574feb-1038-4d9a-bfdd-f4bb0444ecd2" />

We have to maintain a very complicated build flow:
- first `torchgen` in PyTorch, the left one of the picture, it will generate XPUFunctions_inl.h, oneDNN-based dispatch headers (e.g., `addmm_xpu_dispatch.h`), and some partial native declarations (e.g., `dot_native.h`). At this stage, SYCL-based ops (e.g., `dot`) are not included, and corresponding dispatch headers and declarations are not generated.
- second `torchgen` in torch-xpu-ops, the right one of the picture, generates SYCL-based dispatch headers (e.g., `dot_xpu_dispatch.h`) and corresponding native declarations (e.g., `dot_native.h`).
- After 2nd `torchgen`, we copy the newly generated files and declarations back into `<Aten/ops>` folders, and update the corresponding native headers, for example, `dot_xpu_dispatch.h` is copied into `<Aten/ops>` folder and update `dot_native.h` and `XPUFunctions_inl.h` accordingly.

We do this for two reasons:
1. fully reusing torchgen;
2. exposing Aten xpu ops header to public. So developers can include headers like `<Aten/ops/dot_xpu_dispatch.h>` when building C++ extensions (similar to CPU and CUDA).

However, building `libtorch_cpu.so` depends on `dot_native.h`, which is also updated during the second `torchgen`. As a result, we must wait for `libtorch_cpu.so` to finish building before running the second `torchgen`, otherwise, a read-write race may happen if `dot_native.h` is modified while still being used. This can lead to a build failure, especially on Windows. It also introduces an implicit dependency, causing unnecessary recompilation of XPU whenever CPU code changes.

## Deprecated
- _empty_affine_quantized
- _empty_per_channel_affine_quantized
- quantized_max_pool2d_xpu
- _safe_softmax_xpu

Pull Request resolved: #181233
Approved by: https://github.com/EikanWang, https://github.com/albanD
zxd1997066 pushed a commit to daisyden/pytorch that referenced this pull request Jun 18, 2026
…ml (pytorch#181233)

Currently, PyTorch XPU ATen ops are split across two locations:
- operations that call oneDNN (e.g., GEMM, convolution) reside in [ATen/native/mkldnn/xpu](https://github.com/pytorch/pytorch/tree/main/aten/src/ATen/native/mkldnn/xpu);
- while those implemented with SYCL are located [in torch-xpu-ops](https://github.com/intel/torch-xpu-ops/tree/main/src/ATen/native/xpu).

The operations in PyTorch are declared and registered in [pytorch/aten/src/ATen/native/native_functions.yaml](https://github.com/pytorch/pytorch/blob/main/aten/src/ATen/native/native_functions.yaml#L651), whereas the SYCL-based operations in torch-xpu-ops are declared and registered in [yaml/native/native_functions.yaml](https://github.com/intel/torch-xpu-ops/blob/main/yaml/native/native_functions.yaml#L87).

This setup introduces a complication:
When building PyTorch with XPU support, `torch-gen` needs to be run twice:
- once during the build of torch-xpu at [here](https://github.com/pytorch/pytorch/blob/5aac4cfce409ae24f7f0fe3e95f9a3cf5bdf4a28/cmake/Codegen.cmake#L232-L241) and
- again during the build of torch-xpu-ops at [here](https://github.com/intel/torch-xpu-ops/blob/8d373ba272f9fed348c7684bac4a0c2663844bbd/cmake/Codegen.cmake#L99-L125).

This solution introduces two issues (for more details, see [Details](#details)):
1.	We need to maintain an additional `native_functions.yaml` in torch-xpu-ops, which may lead to unexpected behavior if it becomes out of sync with the one in PyTorch. For example, changes to `codegen` (e.g., pytorch#180190) may break XPU CI because XPU ATen kernel registrations are out-of-tree. This creates additional maintenance overhead for both PyTorch and torch-xpu-ops.
2.	It creates a build dependency; torch-xpu-ops must wait for the torch-cpu build to finish, which increases overall build time and complexity. It also retriggers the re-compile XPU (depends on libtorch_cpu.so) due to any CPU code change.

To resolve the issues mentioned above, we would like to migrate XPU ATen kernel registration to `native_functions.yaml` in PyTorch and reuse its codegen fully.

If we could move the ops declarations from torch-xpu-ops into torch’s native_functions.yaml, both problems would be resolved.
The key benefits:
1. Single code generation pass – run torch-gen only once instead of twice, simple and more efficient build process
2. Eliminate build dependency – enable parallel builds of torch-cpu and torch-xpu-ops
3. Single source of truth – maintain only one `native_functions.yaml` file, prevent XPU CI break by torchgen and YAML changes.

<img width="1661" height="966" alt="image" src="https://github.com/user-attachments/assets/06574feb-1038-4d9a-bfdd-f4bb0444ecd2" />

We have to maintain a very complicated build flow:
- first `torchgen` in PyTorch, the left one of the picture, it will generate XPUFunctions_inl.h, oneDNN-based dispatch headers (e.g., `addmm_xpu_dispatch.h`), and some partial native declarations (e.g., `dot_native.h`). At this stage, SYCL-based ops (e.g., `dot`) are not included, and corresponding dispatch headers and declarations are not generated.
- second `torchgen` in torch-xpu-ops, the right one of the picture, generates SYCL-based dispatch headers (e.g., `dot_xpu_dispatch.h`) and corresponding native declarations (e.g., `dot_native.h`).
- After 2nd `torchgen`, we copy the newly generated files and declarations back into `<Aten/ops>` folders, and update the corresponding native headers, for example, `dot_xpu_dispatch.h` is copied into `<Aten/ops>` folder and update `dot_native.h` and `XPUFunctions_inl.h` accordingly.

We do this for two reasons:
1. fully reusing torchgen;
2. exposing Aten xpu ops header to public. So developers can include headers like `<Aten/ops/dot_xpu_dispatch.h>` when building C++ extensions (similar to CPU and CUDA).

However, building `libtorch_cpu.so` depends on `dot_native.h`, which is also updated during the second `torchgen`. As a result, we must wait for `libtorch_cpu.so` to finish building before running the second `torchgen`, otherwise, a read-write race may happen if `dot_native.h` is modified while still being used. This can lead to a build failure, especially on Windows. It also introduces an implicit dependency, causing unnecessary recompilation of XPU whenever CPU code changes.

- _empty_affine_quantized
- _empty_per_channel_affine_quantized
- quantized_max_pool2d_xpu
- _safe_softmax_xpu

Pull Request resolved: pytorch#181233
Approved by: https://github.com/EikanWang, https://github.com/albanD

(cherry picked from commit 7b04342)
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
…ytorch#180190) (pytorch#180190)

Summary:
Custom ops benefit from using tags. Let's make them usable--they're already headeronly (it's an enum) but the major pain of this change is needing to codev it and fight internal. Claude was v helpful tho!

This PR moves at::Tag to torch::headeronly::Tag and adds a shim for stable Library.def to be able to take in tags.

Differential Revision: D100546620

Pulled By: janeyx99

Pull Request resolved: pytorch#180190
Approved by: https://github.com/albanD
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
…w tags (pytorch#180190) (pytorch#180190)"

This reverts commit 85c505b.

Reverted pytorch#180190 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#180190 (comment)))
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
…ary.def override to allow tags (pytorch#181161)

Summary:
Reland of D100546620 which was reverted by D101431332 due to breaking libhammer.so.

Custom ops benefit from using tags. This PR moves at::Tag to torch::headeronly::Tag and adds a shim for stable Library.def to be able to take in tags.

Note: The two BUILD.bazel changes from the original diff were already present on master (the revert did not touch them), so this reland omits those files.

Old PR was: pytorch#180190

Differential Revision: D102031366

Pull Request resolved: pytorch#181161
Approved by: https://github.com/seemethere
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
…ary.def override to allow tags (pytorch#181161)

Summary:
Reland of D100546620 which was reverted by D101431332 due to breaking libhammer.so.

Custom ops benefit from using tags. This PR moves at::Tag to torch::headeronly::Tag and adds a shim for stable Library.def to be able to take in tags.

Note: The two BUILD.bazel changes from the original diff were already present on master (the revert did not touch them), so this reland omits those files.

Old PR was: pytorch#180190

Differential Revision: D102031366

Pull Request resolved: pytorch#181161
Approved by: https://github.com/seemethere, https://github.com/atalman
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
…ytorch#180190) (pytorch#180190)

Summary:
Custom ops benefit from using tags. Let's make them usable--they're already headeronly (it's an enum) but the major pain of this change is needing to codev it and fight internal. Claude was v helpful tho!

This PR moves at::Tag to torch::headeronly::Tag and adds a shim for stable Library.def to be able to take in tags.

Differential Revision: D100546620

Pulled By: janeyx99

Pull Request resolved: pytorch#180190
Approved by: https://github.com/albanD
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…w tags (pytorch#180190) (pytorch#180190)"

This reverts commit 85c505b.

Reverted pytorch#180190 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#180190 (comment)))
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…ary.def override to allow tags (pytorch#181161)

Summary:
Reland of D100546620 which was reverted by D101431332 due to breaking libhammer.so.

Custom ops benefit from using tags. This PR moves at::Tag to torch::headeronly::Tag and adds a shim for stable Library.def to be able to take in tags.

Note: The two BUILD.bazel changes from the original diff were already present on master (the revert did not touch them), so this reland omits those files.

Old PR was: pytorch#180190

Differential Revision: D102031366

Pull Request resolved: pytorch#181161
Approved by: https://github.com/seemethere
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…ary.def override to allow tags (pytorch#181161)

Summary:
Reland of D100546620 which was reverted by D101431332 due to breaking libhammer.so.

Custom ops benefit from using tags. This PR moves at::Tag to torch::headeronly::Tag and adds a shim for stable Library.def to be able to take in tags.

Note: The two BUILD.bazel changes from the original diff were already present on master (the revert did not touch them), so this reland omits those files.

Old PR was: pytorch#180190

Differential Revision: D102031366

Pull Request resolved: pytorch#181161
Approved by: https://github.com/seemethere, https://github.com/atalman
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…ml (pytorch#181233)

# Motivation
Currently, PyTorch XPU ATen ops are split across two locations:
- operations that call oneDNN (e.g., GEMM, convolution) reside in [ATen/native/mkldnn/xpu](https://github.com/pytorch/pytorch/tree/main/aten/src/ATen/native/mkldnn/xpu);
- while those implemented with SYCL are located [in torch-xpu-ops](https://github.com/intel/torch-xpu-ops/tree/main/src/ATen/native/xpu).

The operations in PyTorch are declared and registered in [pytorch/aten/src/ATen/native/native_functions.yaml](https://github.com/pytorch/pytorch/blob/main/aten/src/ATen/native/native_functions.yaml#L651), whereas the SYCL-based operations in torch-xpu-ops are declared and registered in [yaml/native/native_functions.yaml](https://github.com/intel/torch-xpu-ops/blob/main/yaml/native/native_functions.yaml#L87).

This setup introduces a complication:
When building PyTorch with XPU support, `torch-gen` needs to be run twice:
- once during the build of torch-xpu at [here](https://github.com/pytorch/pytorch/blob/5aac4cfce409ae24f7f0fe3e95f9a3cf5bdf4a28/cmake/Codegen.cmake#L232-L241) and
- again during the build of torch-xpu-ops at [here](https://github.com/intel/torch-xpu-ops/blob/8d373ba272f9fed348c7684bac4a0c2663844bbd/cmake/Codegen.cmake#L99-L125).

This solution introduces two issues (for more details, see [Details](#details)):
1.	We need to maintain an additional `native_functions.yaml` in torch-xpu-ops, which may lead to unexpected behavior if it becomes out of sync with the one in PyTorch. For example, changes to `codegen` (e.g., pytorch#180190) may break XPU CI because XPU ATen kernel registrations are out-of-tree. This creates additional maintenance overhead for both PyTorch and torch-xpu-ops.
2.	It creates a build dependency; torch-xpu-ops must wait for the torch-cpu build to finish, which increases overall build time and complexity. It also retriggers the re-compile XPU (depends on libtorch_cpu.so) due to any CPU code change.

To resolve the issues mentioned above, we would like to migrate XPU ATen kernel registration to `native_functions.yaml` in PyTorch and reuse its codegen fully.

# Solution
If we could move the ops declarations from torch-xpu-ops into torch’s native_functions.yaml, both problems would be resolved.
The key benefits:
1. Single code generation pass – run torch-gen only once instead of twice, simple and more efficient build process
2. Eliminate build dependency – enable parallel builds of torch-cpu and torch-xpu-ops
3. Single source of truth – maintain only one `native_functions.yaml` file, prevent XPU CI break by torchgen and YAML changes.

# Additional Context
## Details
<img width="1661" height="966" alt="image" src="https://github.com/user-attachments/assets/06574feb-1038-4d9a-bfdd-f4bb0444ecd2" />

We have to maintain a very complicated build flow:
- first `torchgen` in PyTorch, the left one of the picture, it will generate XPUFunctions_inl.h, oneDNN-based dispatch headers (e.g., `addmm_xpu_dispatch.h`), and some partial native declarations (e.g., `dot_native.h`). At this stage, SYCL-based ops (e.g., `dot`) are not included, and corresponding dispatch headers and declarations are not generated.
- second `torchgen` in torch-xpu-ops, the right one of the picture, generates SYCL-based dispatch headers (e.g., `dot_xpu_dispatch.h`) and corresponding native declarations (e.g., `dot_native.h`).
- After 2nd `torchgen`, we copy the newly generated files and declarations back into `<Aten/ops>` folders, and update the corresponding native headers, for example, `dot_xpu_dispatch.h` is copied into `<Aten/ops>` folder and update `dot_native.h` and `XPUFunctions_inl.h` accordingly.

We do this for two reasons:
1. fully reusing torchgen;
2. exposing Aten xpu ops header to public. So developers can include headers like `<Aten/ops/dot_xpu_dispatch.h>` when building C++ extensions (similar to CPU and CUDA).

However, building `libtorch_cpu.so` depends on `dot_native.h`, which is also updated during the second `torchgen`. As a result, we must wait for `libtorch_cpu.so` to finish building before running the second `torchgen`, otherwise, a read-write race may happen if `dot_native.h` is modified while still being used. This can lead to a build failure, especially on Windows. It also introduces an implicit dependency, causing unnecessary recompilation of XPU whenever CPU code changes.

## Deprecated
- _empty_affine_quantized
- _empty_per_channel_affine_quantized
- quantized_max_pool2d_xpu
- _safe_softmax_xpu

Pull Request resolved: pytorch#181233
Approved by: https://github.com/EikanWang, https://github.com/albanD
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…ytorch#180190) (pytorch#180190)

Summary:
Custom ops benefit from using tags. Let's make them usable--they're already headeronly (it's an enum) but the major pain of this change is needing to codev it and fight internal. Claude was v helpful tho!

This PR moves at::Tag to torch::headeronly::Tag and adds a shim for stable Library.def to be able to take in tags.

Differential Revision: D100546620

Pulled By: janeyx99

Pull Request resolved: pytorch#180190
Approved by: https://github.com/albanD
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…w tags (pytorch#180190) (pytorch#180190)"

This reverts commit 85c505b.

Reverted pytorch#180190 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#180190 (comment)))
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…ary.def override to allow tags (pytorch#181161)

Summary:
Reland of D100546620 which was reverted by D101431332 due to breaking libhammer.so.

Custom ops benefit from using tags. This PR moves at::Tag to torch::headeronly::Tag and adds a shim for stable Library.def to be able to take in tags.

Note: The two BUILD.bazel changes from the original diff were already present on master (the revert did not touch them), so this reland omits those files.

Old PR was: pytorch#180190

Differential Revision: D102031366

Pull Request resolved: pytorch#181161
Approved by: https://github.com/seemethere
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…ary.def override to allow tags (pytorch#181161)

Summary:
Reland of D100546620 which was reverted by D101431332 due to breaking libhammer.so.

Custom ops benefit from using tags. This PR moves at::Tag to torch::headeronly::Tag and adds a shim for stable Library.def to be able to take in tags.

Note: The two BUILD.bazel changes from the original diff were already present on master (the revert did not touch them), so this reland omits those files.

Old PR was: pytorch#180190

Differential Revision: D102031366

Pull Request resolved: pytorch#181161
Approved by: https://github.com/seemethere, https://github.com/atalman
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…ml (pytorch#181233)

# Motivation
Currently, PyTorch XPU ATen ops are split across two locations:
- operations that call oneDNN (e.g., GEMM, convolution) reside in [ATen/native/mkldnn/xpu](https://github.com/pytorch/pytorch/tree/main/aten/src/ATen/native/mkldnn/xpu);
- while those implemented with SYCL are located [in torch-xpu-ops](https://github.com/intel/torch-xpu-ops/tree/main/src/ATen/native/xpu).

The operations in PyTorch are declared and registered in [pytorch/aten/src/ATen/native/native_functions.yaml](https://github.com/pytorch/pytorch/blob/main/aten/src/ATen/native/native_functions.yaml#L651), whereas the SYCL-based operations in torch-xpu-ops are declared and registered in [yaml/native/native_functions.yaml](https://github.com/intel/torch-xpu-ops/blob/main/yaml/native/native_functions.yaml#L87).

This setup introduces a complication:
When building PyTorch with XPU support, `torch-gen` needs to be run twice:
- once during the build of torch-xpu at [here](https://github.com/pytorch/pytorch/blob/5aac4cfce409ae24f7f0fe3e95f9a3cf5bdf4a28/cmake/Codegen.cmake#L232-L241) and
- again during the build of torch-xpu-ops at [here](https://github.com/intel/torch-xpu-ops/blob/8d373ba272f9fed348c7684bac4a0c2663844bbd/cmake/Codegen.cmake#L99-L125).

This solution introduces two issues (for more details, see [Details](#details)):
1.	We need to maintain an additional `native_functions.yaml` in torch-xpu-ops, which may lead to unexpected behavior if it becomes out of sync with the one in PyTorch. For example, changes to `codegen` (e.g., pytorch#180190) may break XPU CI because XPU ATen kernel registrations are out-of-tree. This creates additional maintenance overhead for both PyTorch and torch-xpu-ops.
2.	It creates a build dependency; torch-xpu-ops must wait for the torch-cpu build to finish, which increases overall build time and complexity. It also retriggers the re-compile XPU (depends on libtorch_cpu.so) due to any CPU code change.

To resolve the issues mentioned above, we would like to migrate XPU ATen kernel registration to `native_functions.yaml` in PyTorch and reuse its codegen fully.

# Solution
If we could move the ops declarations from torch-xpu-ops into torch’s native_functions.yaml, both problems would be resolved.
The key benefits:
1. Single code generation pass – run torch-gen only once instead of twice, simple and more efficient build process
2. Eliminate build dependency – enable parallel builds of torch-cpu and torch-xpu-ops
3. Single source of truth – maintain only one `native_functions.yaml` file, prevent XPU CI break by torchgen and YAML changes.

# Additional Context
## Details
<img width="1661" height="966" alt="image" src="https://github.com/user-attachments/assets/06574feb-1038-4d9a-bfdd-f4bb0444ecd2" />

We have to maintain a very complicated build flow:
- first `torchgen` in PyTorch, the left one of the picture, it will generate XPUFunctions_inl.h, oneDNN-based dispatch headers (e.g., `addmm_xpu_dispatch.h`), and some partial native declarations (e.g., `dot_native.h`). At this stage, SYCL-based ops (e.g., `dot`) are not included, and corresponding dispatch headers and declarations are not generated.
- second `torchgen` in torch-xpu-ops, the right one of the picture, generates SYCL-based dispatch headers (e.g., `dot_xpu_dispatch.h`) and corresponding native declarations (e.g., `dot_native.h`).
- After 2nd `torchgen`, we copy the newly generated files and declarations back into `<Aten/ops>` folders, and update the corresponding native headers, for example, `dot_xpu_dispatch.h` is copied into `<Aten/ops>` folder and update `dot_native.h` and `XPUFunctions_inl.h` accordingly.

We do this for two reasons:
1. fully reusing torchgen;
2. exposing Aten xpu ops header to public. So developers can include headers like `<Aten/ops/dot_xpu_dispatch.h>` when building C++ extensions (similar to CPU and CUDA).

However, building `libtorch_cpu.so` depends on `dot_native.h`, which is also updated during the second `torchgen`. As a result, we must wait for `libtorch_cpu.so` to finish building before running the second `torchgen`, otherwise, a read-write race may happen if `dot_native.h` is modified while still being used. This can lead to a build failure, especially on Windows. It also introduces an implicit dependency, causing unnecessary recompilation of XPU whenever CPU code changes.

## Deprecated
- _empty_affine_quantized
- _empty_per_channel_affine_quantized
- quantized_max_pool2d_xpu
- _safe_softmax_xpu

Pull Request resolved: pytorch#181233
Approved by: https://github.com/EikanWang, https://github.com/albanD
minmingzhu pushed a commit to minmingzhu/torch-xpu-ops that referenced this pull request Sep 15, 2026
The best scenario is to not have a different copy here, but this fix
will unblock my PR's broken XPU build.
pytorch/pytorch#180190
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td Do not run TD on this PR ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request fb-exported Merged meta-exported release notes: inductor (aoti) Reverted suppress-bc-linter Suppresses the failures of API backward-compatibility linter (Lint/bc_linter)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants