Skip to content

Scope third-party install rules out of the wheel via CMake components - #184873

Closed
zklaus wants to merge 1 commit into
gh/zklaus/60/basefrom
gh/zklaus/60/head
Closed

zklaus wants to merge 1 commit into
gh/zklaus/60/basefrom
gh/zklaus/60/head

Conversation

@zklaus

@zklaus zklaus commented May 22, 2026 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

Move all install() rules onto named components so scikit-build-core
can restrict the wheel to the subset that actually belongs there.
The default component is "libtorch" (shared libraries, headers,
runtime data); explicit "torch" tags cover Python-side artifacts
(libtorch_python, the _C extension, version.py, type stubs, yaml /
jinja templates, torchgen packaged inputs, etc.); the existing "dev"
tag for find_package(Torch) configs is kept; and a new "third_party"
sentinel absorbs install rules from vendored subprojects so they
become no-ops at install time.

cmake/InstallComponents.cmake defines two helpers used at every
vendored subproject site:

pytorch_add_thirdparty_subdirectory() wraps add_subdirectory()
and temporarily overrides CMAKE_INSTALL_DEFAULT_COMPONENT_NAME
to "third_party" so the subproject's untagged install() rules
land in the suppressed component.

pytorch_install_thirdparty_headers() mirrors the subset of a
subproject's public headers that the wheel actually needs
(since we just suppressed the upstream install()), tagged with
"libtorch". This keeps libfbgemm.a, libkineto.a, libcpuinfo.a,
libtorch_xpu_ops.a, and the corresponding lib64/cmake/* configs
out of the wheel while preserving the include surface that
torch's public headers and downstream cpp_extension consumers
depend on.

Each add_subdirectory(third_party/...) site is rewritten to use the
wrap macro and, where the subproject contributes public headers, an
immediately-following pytorch_install_thirdparty_headers() (or, for
sleef's generated sleef.h and torch-xpu-ops' sycltla .so libraries,
an explicit install() rule). pyproject.toml selects the wheel
contents via install.components = ["libtorch", "torch", "dev"].

Co-Generated with Claude Code

cc @gujinghui @PenghuiCheng @XiaobingSuper @jianyuh @jgong5 @mingfeima @sanchitintel @ashokei @jingxu10 @min-jean-cho @yanbing-j @Guobing-Chen @Xia-Weiwen @snadampal

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented May 22, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 4 New Failures, 12 Unrelated Failures, 2 Unclassified Failures

As of commit a29741f with merge base 04d232f (image):

NEW FAILURES - The following jobs have failed:

UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:

  • Check mergeability of ghstack PR / ghstack-mergeability-check (gh) (this job did not run on the merge base, so DrCI cannot tell whether the failure is pre-existing)
    RuntimeError: PR 180243 is out of sync with the corresponding revision ede170a73635e5dff9349ab052e758cea7ffdca0 on branch gh/zklaus/49/orig that would be merged into main. This usually happens because there is a non ghstack change in the PR. Please sync them and try again (ex. make the changes on origin/gh/zklaus/60/orig and run ghstack).
  • pull / linux-jammy-rocm-py3.10 / build (gh) (this job did not run on the merge base, so DrCI cannot tell whether the failure is pre-existing)
    ##[error]The operation was canceled.

UNSTABLE - The following jobs are marked as unstable, possibly due to flakiness on trunk:

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

@pytorch-bot pytorch-bot Bot added the module: mkldnn Related to Intel IDEEP or oneDNN (a.k.a. mkldnn) integration label May 22, 2026
@pytorch-bot

pytorch-bot Bot commented May 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.

@github-actions

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions Bot added the Stale label Jul 21, 2026
zklaus added a commit to zklaus/pytorch that referenced this pull request Jul 29, 2026
Since pytorch#180247 a standalone `cmake --install` scatters Python package
directories (_dynamo/, _export/, _inductor/, csrc/, testing/, utils/) across
CMAKE_INSTALL_PREFIX alongside bin/, include/, lib/ and share/, and writes
into the source checkout besides. Reported as pytorch#191102.

The root cause is that PyTorch has two install roots but names only one. The
C++ distribution (bin/, lib/, include/, share/cmake/) goes to
CMAKE_INSTALL_PREFIX. The Python packages (torch/, torchgen/) go to the
interpreter's import root, which is one level up -- `wheel.install-dir =
"torch"` makes the prefix the torch package directory, so the two coincide
only when scikit-build-core drives the build. Having no name for the second
root, its rules borrowed the first: destinations relative to
CMAKE_INSTALL_PREFIX (correct only under scikit-build-core) or absolute
against SKBUILD_PLATLIB_DIR with a PROJECT_SOURCE_DIR fallback (which is what
wrote into the checkout).

This adds TORCH_PYTHON_SITE_DIR for the Python root and routes all of it --
cmake/PackageData.cmake, cmake/FileMirroring.cmake, and the _C, _nccl_ep and
version.py rules in torch/CMakeLists.txt -- through it. It defaults to
SKBUILD_PLATLIB_DIR, which scikit-build-core sets and nothing else does, so
a wheel build gets the staging platlib and a standalone install gets an empty
value meaning "no Python payload". That restores the pre-2.13 contract: a
standalone cmake install is the C++ distribution, because setup.py owned all
the Python packaging.

I first tried gating the rules behind an INSTALL_PYTHON_PACKAGE option
(the previous version of this commit). It works but is per-rule whack-a-mole,
and it missed two of the five `DESTINATION "."` sites -- version.py, which
sits outside the BUILD_PYTHON block and leaked even into libtorch builds, and
_nccl_ep. Naming the root fixes the whole class instead of one rule at a time.
The reporter suggested a named install COMPONENT excluded from the default
install; that is orthogonal and already in flight as pytorch#184873 for wheel
scoping, but it would not fix this on its own, since CMake runs every
install() rule on a component-less `cmake --install` regardless of its tag.

`wheel.install-dir` stays as it is. Dropping it would make the prefix the
platlib root and require a torch/ prefix on ~90 C++ destinations, ~40 of them
raw literals not routed through TORCH_INSTALL_LIB_DIR and friends, and would
invert the conditional: standalone C++ installs would then need torch/
suppressed everywhere.

Two things are enforced rather than assumed. The extension modules resolve
the C++ tree through an $ORIGIN/lib install rpath and torch/__init__.py
resolves lib/ and bin/ off its own __file__, so the prefix must be
<site>/torch; a mismatch is now a configure-time FATAL_ERROR naming both
paths instead of an install that only fails at import. And the default is
deliberately not a cache entry: scikit-build-core stages each build in a
fresh temporary directory while reusing the build directory, so a cached
default goes stale on the next rebuild -- an explicit -D still caches and
wins.

Note that tools/build_libtorch.py loses the torchgen/packaged mirroring, as
it builds with BUILD_PYTHON=OFF and no longer produces a Python payload. The
only consumer of those files is torchgen/gen_backend_stubs.py at runtime in
an installed torchgen, which is not in the libtorch path.

Test Plan:

All on Linux/CPU unless noted, against the reporter's exact configure from
the issue:

```
cmake -S . -B build-standalone -GNinja \
  -DCMAKE_INSTALL_PREFIX=/tmp/prefix \
  -DATEN_NO_TEST=ON -DBUILD_BINARY=ON -DBUILD_CUSTOM_PROTOBUF=OFF \
  -DBUILD_PYTHON=ON -DBUILD_TEST=OFF -DUSE_OPENMP=ON \
  -DCMAKE_INSTALL_LIBDIR=lib -DUSE_DISTRIBUTED=OFF -DUSE_CUDA=OFF

grep -rhoE '\$\{CMAKE_INSTALL_PREFIX\}/[A-Za-z_./]*' \
  --include=cmake_install.cmake build-standalone/ \
  | sed 's|${CMAKE_INSTALL_PREFIX}/||' | cut -d/ -f1 | sort -u
grep -rh --include=cmake_install.cmake 'CMAKE_INSTALL_PREFIX}/\."' build-standalone/
grep -rh --include=cmake_install.cmake "DESTINATION \"$PWD" build-standalone/
```

Before: `. bin csrc _dynamo _export _inductor include lib share testing
utils`, plus seven rules with destinations inside the source checkout. After:
`bin include lib share`, and both greps empty. The greps must recurse over
every cmake_install.cmake, not just the top-level one -- that is how the
version.py rule escaped the first attempt.

Same configure with -DUSE_CUDA=ON -DUSE_DISTRIBUTED=ON -DUSE_NCCL_EP=ON
-DUSE_SYSTEM_NCCL=ON: the _nccl_ep target is still built (it appears in
build.ninja, producing torch/_nccl_ep.cpython-310-x86_64-linux-gnu.so) but
contributes no install rule, and the prefix root stays clean.

The nesting guard, both directions:

```
cmake -S . -B b-bad  -DCMAKE_INSTALL_PREFIX=/tmp/elsewhere \
                     -DTORCH_PYTHON_SITE_DIR=/tmp/site
cmake -S . -B b-good -DCMAKE_INSTALL_PREFIX=/tmp/site/torch \
                     -DTORCH_PYTHON_SITE_DIR=/tmp/site
```

The first fails with the FATAL_ERROR; the second configures, puts all 16
Python destinations under /tmp/site, and leaves the prefix root at
`bin include lib share`.

Wheel contents, via scikit-build-core:

```
pip install -e . -v --no-build-isolation   # twice, consecutively
python -c "import torch, torchgen.packaged, functorch; print(torch.__version__)"
```

Both builds succeed, each configure picking up its own staging directory
(the second run is the case a cached default would have broken). The
installed file list under site-packages is identical to a pre-change build
of the same base: 9679 files, none gained or lost, including the mirrored
copies that exist only in the install tree (valgrind.h/callgrind.h,
torchgen/packaged with native_functions.yaml, tags.yaml, 42 templates and 16
autograd files) and the generated version.py, _C, 43 .pyi stubs and 20 jinja
templates. import and a matmul both work.

Lint:

```
lintrunner -a --take CMAKE,NEWLINE,SPACES,TABS,CODESPELL,COPYRIGHT,EXEC \
  CMakeLists.txt cmake/FileMirroring.cmake cmake/PackageData.cmake \
  cmake/Summary.cmake torch/CMakeLists.txt
```

Not covered: Windows and macOS were not exercised. The Windows-specific rule
here is the _C import library, which keeps its existing prefix-relative
`ARCHIVE DESTINATION "lib"` since it is a C++ artifact.

This change was authored with the assistance of an AI coding assistant.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
zklaus added a commit to zklaus/pytorch that referenced this pull request Jul 29, 2026
Since pytorch#180247 a standalone `cmake --install` scatters Python package
directories (_dynamo/, _export/, _inductor/, csrc/, testing/, utils/) across
CMAKE_INSTALL_PREFIX alongside bin/, include/, lib/ and share/, and writes
into the source checkout besides. Reported as pytorch#191102.

scikit-build-core models a wheel as several trees -- platlib, purelib, data,
headers, scripts, metadata -- and exports each to CMake as SKBUILD_<TREE>_DIR.
`wheel.install-dir` selects which tree, plus optional subpath, becomes
CMAKE_INSTALL_PREFIX; it defaults to empty, which makes the prefix the platlib
root itself. We set it to "torch" because the C++ tree has to sit at torch/lib
for the $ORIGIN/lib install rpath and torch/__init__.py's __file__ lookups to
resolve, and that pushes the prefix one level below platlib.

Most of our Python-side install rules were nonetheless written relative to the
prefix, using it as a proxy for the platlib tree. That only holds while
scikit-build-core is driving and install-dir is exactly "torch". A standalone
cmake install has a conventional prefix and no platlib at all, so the same
rules deposit torch/ package data at the prefix root. cmake/FileMirroring.cmake
did address the tree properly via SKBUILD_PLATLIB_DIR, but fell back to
PROJECT_SOURCE_DIR when it was undefined, which is how a standalone install
came to write into the checkout.

This is not an edge case: docs/libtorch.rst documents building libtorch with
`cmake --build . --target install` into a CMAKE_INSTALL_PREFIX of your choice,
which is the flow the report used. The layout that install should produce is
nowhere documented, which is a gap worth closing separately.

The rules now address SKBUILD_PLATLIB_DIR directly -- cmake/PackageData.cmake,
cmake/FileMirroring.cmake, and the _C, _nccl_ep and version.py rules in
torch/CMakeLists.txt -- guarded on SKBUILD, the sentinel scikit-build-core sets
for exactly this question. A build with no wheel behind it (libtorch,
tools/build_libtorch.py, downstream cmake --install) installs the C++
distribution alone, which restores the pre-2.13 contract: setup.py owned all
the Python packaging back when a standalone install was C++ only.

Two alternatives are worth naming. The reporter suggested a named install
COMPONENT held out of the default install; that is orthogonal, and already in
flight as pytorch#184873 for wheel scoping, but it would not fix this on its own,
because CMake runs every install() rule on a component-less `cmake --install`
whatever its tag. Dropping `wheel.install-dir` instead would make the prefix
the platlib root and coincide the two, but it needs a torch/ prefix on ~90 C++
destinations, ~40 of them raw literals not routed through TORCH_INSTALL_LIB_DIR
and friends, and it inverts the conditional: standalone C++ installs would then
need torch/ suppressed everywhere.

The prefix-must-be-<platlib>/torch coupling is now enforced rather than
assumed. install-dir is what makes the two agree, so changing it silently
produces an install that only fails at import; a mismatch is now a
configure-time FATAL_ERROR naming both paths and pointing at pyproject.toml.

Note that tools/build_libtorch.py loses the torchgen/packaged mirroring, as it
builds with BUILD_PYTHON=OFF and no longer produces a Python payload. The only
consumer of those files is torchgen/gen_backend_stubs.py at runtime in an
installed torchgen, which is not in the libtorch path.

Test Plan:

All on Linux/CPU unless noted, against the reporter's exact configure from the
issue:

```
cmake -S . -B build-standalone -GNinja \
  -DCMAKE_INSTALL_PREFIX=/tmp/prefix \
  -DATEN_NO_TEST=ON -DBUILD_BINARY=ON -DBUILD_CUSTOM_PROTOBUF=OFF \
  -DBUILD_PYTHON=ON -DBUILD_TEST=OFF -DUSE_OPENMP=ON \
  -DCMAKE_INSTALL_LIBDIR=lib -DUSE_DISTRIBUTED=OFF -DUSE_CUDA=OFF

grep -rhoE '\$\{CMAKE_INSTALL_PREFIX\}/[A-Za-z_./]*' \
  --include=cmake_install.cmake build-standalone/ \
  | sed 's|${CMAKE_INSTALL_PREFIX}/||' | cut -d/ -f1 | sort -u
grep -rh --include=cmake_install.cmake 'CMAKE_INSTALL_PREFIX}/\."' build-standalone/
grep -rh --include=cmake_install.cmake "DESTINATION \"$PWD" build-standalone/
grep -rhE --include=cmake_install.cmake 'DESTINATION "(/torch|/torchgen)' build-standalone/
```

Before: `. bin csrc _dynamo _export _inductor include lib share testing utils`,
plus seven rules with destinations inside the source checkout. After:
`bin include lib share`, and the other three greps empty. The last one checks
that an unset SKBUILD_PLATLIB_DIR has not turned a destination into an absolute
path at the filesystem root. The greps must recurse over every
cmake_install.cmake, not just the top-level one, since several of the offending
rules live in subdirectory scopes.

Same configure with -DUSE_CUDA=ON -DUSE_DISTRIBUTED=ON -DUSE_NCCL=ON
-DUSE_NCCL_EP=ON -DUSE_SYSTEM_NCCL=ON: the _nccl_ep target is still built (it
appears in build.ninja, producing torch/_nccl_ep.cpython-310-x86_64-linux-gnu.so)
but contributes no install rule, and the prefix root stays clean.

The guard, exercised the way it can actually regress -- by changing install-dir
in pyproject.toml to something other than "torch" and building a wheel -- fails
configure with the FATAL_ERROR, reporting platlib, the expected prefix and the
actual one.

Wheel contents, via scikit-build-core:

```
pip install -e . -v --no-build-isolation   # twice, consecutively
python -c "import torch, torchgen.packaged, functorch; print(torch.__version__)"
```

Both builds succeed, each configure picking up its own staging directory. The
installed file list under site-packages is identical to a pre-change build of
the same base: 9679 files, none gained or lost, including the mirrored copies
that exist only in the install tree (valgrind.h/callgrind.h, torchgen/packaged
with native_functions.yaml, tags.yaml, 42 templates and 16 autograd files) and
the generated version.py, _C, 43 .pyi stubs and 20 jinja templates. import and
a matmul both work.

Lint:

```
lintrunner -a --take CMAKE,NEWLINE,SPACES,TABS,CODESPELL,COPYRIGHT,EXEC \
  CMakeLists.txt cmake/FileMirroring.cmake cmake/PackageData.cmake \
  cmake/Summary.cmake torch/CMakeLists.txt
```

Not covered: Windows and macOS were not exercised. The Windows-specific rule
here is the _C import library, which keeps its existing prefix-relative
`ARCHIVE DESTINATION "lib"` since it is a C++ artifact.

This change was authored with the assistance of an AI coding assistant.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
zklaus added a commit to zklaus/pytorch that referenced this pull request Jul 29, 2026
Since pytorch#180247 a standalone `cmake --install` scatters Python package
directories (_dynamo/, _export/, _inductor/, csrc/, testing/, utils/) across
CMAKE_INSTALL_PREFIX alongside bin/, include/, lib/ and share/, and writes
into the source checkout besides. Reported as pytorch#191102.

scikit-build-core models a wheel as several trees -- platlib, purelib, data,
headers, scripts, metadata -- and exports each to CMake as SKBUILD_<TREE>_DIR.
`wheel.install-dir` selects which tree, plus optional subpath, becomes
CMAKE_INSTALL_PREFIX; it defaults to empty, which makes the prefix the platlib
root itself. We set it to "torch" because the C++ tree has to sit at torch/lib
for the $ORIGIN/lib install rpath and torch/__init__.py's __file__ lookups to
resolve, and that pushes the prefix one level below platlib.

Most of our Python-side install rules were nonetheless written relative to the
prefix, using it as a proxy for the platlib tree. That only holds while
scikit-build-core is driving and install-dir is exactly "torch". A standalone
cmake install has a conventional prefix and no platlib at all, so the same
rules deposit torch/ package data at the prefix root. cmake/FileMirroring.cmake
did address the tree properly via SKBUILD_PLATLIB_DIR, but fell back to
PROJECT_SOURCE_DIR when it was undefined, which is how a standalone install
came to write into the checkout.

This is not an edge case: docs/libtorch.rst documents building libtorch with
`cmake --build . --target install` into a CMAKE_INSTALL_PREFIX of your choice,
which is the flow the report used. The layout that install should produce is
nowhere documented, which is a gap worth closing separately.

The rules now address SKBUILD_PLATLIB_DIR directly -- cmake/PackageData.cmake,
cmake/FileMirroring.cmake, and the _C, _nccl_ep and version.py rules in
torch/CMakeLists.txt -- guarded on SKBUILD, the sentinel scikit-build-core sets
for exactly this question. A build with no wheel behind it (libtorch,
tools/build_libtorch.py, downstream cmake --install) installs the C++
distribution alone, which restores the pre-2.13 contract: setup.py owned all
the Python packaging back when a standalone install was C++ only.

Two alternatives are worth naming. The reporter suggested a named install
COMPONENT held out of the default install; that is orthogonal, and already in
flight as pytorch#184873 for wheel scoping, but it would not fix this on its own,
because CMake runs every install() rule on a component-less `cmake --install`
whatever its tag. Dropping `wheel.install-dir` instead would make the prefix
the platlib root and coincide the two, but it needs a torch/ prefix on ~90 C++
destinations, ~40 of them raw literals not routed through TORCH_INSTALL_LIB_DIR
and friends, and it inverts the conditional: standalone C++ installs would then
need torch/ suppressed everywhere.

The prefix-must-be-<platlib>/torch coupling is now enforced rather than
assumed. install-dir is what makes the two agree, so changing it silently
produces an install that only fails at import; a mismatch is now a
configure-time FATAL_ERROR naming both paths and pointing at pyproject.toml.

Note that tools/build_libtorch.py loses the torchgen/packaged mirroring, as it
builds with BUILD_PYTHON=OFF and no longer produces a Python payload. The only
consumer of those files is torchgen/gen_backend_stubs.py at runtime in an
installed torchgen, which is not in the libtorch path.

Test Plan:

All on Linux/CPU unless noted, against the reporter's exact configure from the
issue:

```
cmake -S . -B build-standalone -GNinja \
  -DCMAKE_INSTALL_PREFIX=/tmp/prefix \
  -DATEN_NO_TEST=ON -DBUILD_BINARY=ON -DBUILD_CUSTOM_PROTOBUF=OFF \
  -DBUILD_PYTHON=ON -DBUILD_TEST=OFF -DUSE_OPENMP=ON \
  -DCMAKE_INSTALL_LIBDIR=lib -DUSE_DISTRIBUTED=OFF -DUSE_CUDA=OFF

grep -rhoE '\$\{CMAKE_INSTALL_PREFIX\}/[A-Za-z_./]*' \
  --include=cmake_install.cmake build-standalone/ \
  | sed 's|${CMAKE_INSTALL_PREFIX}/||' | cut -d/ -f1 | sort -u
grep -rh --include=cmake_install.cmake 'CMAKE_INSTALL_PREFIX}/\."' build-standalone/
grep -rh --include=cmake_install.cmake "DESTINATION \"$PWD" build-standalone/
grep -rhE --include=cmake_install.cmake 'DESTINATION "(/torch|/torchgen)' build-standalone/
```

Before: `. bin csrc _dynamo _export _inductor include lib share testing utils`,
plus seven rules with destinations inside the source checkout. After:
`bin include lib share`, and the other three greps empty. The last one checks
that an unset SKBUILD_PLATLIB_DIR has not turned a destination into an absolute
path at the filesystem root. The greps must recurse over every
cmake_install.cmake, not just the top-level one, since several of the offending
rules live in subdirectory scopes.

Same configure with -DUSE_CUDA=ON -DUSE_DISTRIBUTED=ON -DUSE_NCCL=ON
-DUSE_NCCL_EP=ON -DUSE_SYSTEM_NCCL=ON: the _nccl_ep target is still built (it
appears in build.ninja, producing torch/_nccl_ep.cpython-310-x86_64-linux-gnu.so)
but contributes no install rule, and the prefix root stays clean.

The guard, exercised the way it can actually regress -- by changing install-dir
in pyproject.toml to something other than "torch" and building a wheel -- fails
configure with the FATAL_ERROR, reporting platlib, the expected prefix and the
actual one.

Wheel contents, via scikit-build-core:

```
pip install -e . -v --no-build-isolation   # twice, consecutively
python -c "import torch, torchgen.packaged, functorch; print(torch.__version__)"
```

Both builds succeed, each configure picking up its own staging directory. The
installed file list under site-packages is identical to a pre-change build of
the same base: 9679 files, none gained or lost, including the mirrored copies
that exist only in the install tree (valgrind.h/callgrind.h, torchgen/packaged
with native_functions.yaml, tags.yaml, 42 templates and 16 autograd files) and
the generated version.py, _C, 43 .pyi stubs and 20 jinja templates. import and
a matmul both work.

Lint:

```
lintrunner -a --take CMAKE,NEWLINE,SPACES,TABS,CODESPELL,COPYRIGHT,EXEC \
  CMakeLists.txt cmake/FileMirroring.cmake cmake/PackageData.cmake \
  cmake/Summary.cmake torch/CMakeLists.txt
```

Not covered: Windows and macOS were not exercised. The Windows-specific rule
here is the _C import library, which keeps its existing prefix-relative
`ARCHIVE DESTINATION "lib"` since it is a C++ artifact.

This change was authored with the assistance of an AI coding assistant.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
zklaus added a commit to zklaus/pytorch that referenced this pull request Jul 29, 2026
Since v2.13.0 a standalone `cmake --install` scatters Python package
directories (_dynamo/, _export/, _inductor/, csrc/, testing/, utils/) across
CMAKE_INSTALL_PREFIX alongside bin/, include/, lib/ and share/, and writes
into the source checkout besides. Reported as pytorch#191102. The prefix-root
scattering comes from pytorch#177643, which added cmake/PackageData.cmake and its
unconditional include; the source-checkout writes from pytorch#177642, which did the
same for cmake/FileMirroring.cmake.

scikit-build-core models a wheel as several trees -- platlib, purelib, data,
headers, scripts, metadata -- and exports each to CMake as SKBUILD_<TREE>_DIR.
`wheel.install-dir` selects which tree, plus optional subpath, becomes
CMAKE_INSTALL_PREFIX; it defaults to empty, which makes the prefix the platlib
root itself. We set it to "torch" because the C++ tree has to sit at torch/lib
for the $ORIGIN/lib install rpath and torch/__init__.py's __file__ lookups to
resolve, and that pushes the prefix one level below platlib.

Most of our Python-side install rules were nonetheless written relative to the
prefix, using it as a proxy for the platlib tree. That only holds while
scikit-build-core is driving and install-dir is exactly "torch". A standalone
cmake install has a conventional prefix and no platlib at all, so the same
rules deposit torch/ package data at the prefix root. cmake/FileMirroring.cmake
did address the tree properly via SKBUILD_PLATLIB_DIR, but fell back to
PROJECT_SOURCE_DIR when it was undefined, which is how a standalone install
came to write into the checkout.

This is not an edge case: docs/libtorch.rst documents building libtorch with
`cmake --build . --target install` into a CMAKE_INSTALL_PREFIX of your choice,
which is the flow the report used. The layout that install should produce is
nowhere documented, which is a gap worth closing separately.

The rules now address SKBUILD_PLATLIB_DIR directly -- cmake/PackageData.cmake,
cmake/FileMirroring.cmake, and the _C, _nccl_ep and version.py rules in
torch/CMakeLists.txt -- guarded on SKBUILD, the sentinel scikit-build-core sets
for exactly this question. A build with no wheel behind it (libtorch,
tools/build_libtorch.py, downstream cmake --install) installs the C++
distribution alone, which restores the pre-2.13 contract: setup.py owned all
the Python packaging back when a standalone install was C++ only.

Two alternatives are worth naming. The reporter suggested a named install
COMPONENT held out of the default install; that is orthogonal, and already in
flight as pytorch#184873 for wheel scoping, but it would not fix this on its own,
because CMake runs every install() rule on a component-less `cmake --install`
whatever its tag. Dropping `wheel.install-dir` instead would make the prefix
the platlib root and coincide the two, but it needs a torch/ prefix on ~90 C++
destinations, ~40 of them raw literals not routed through TORCH_INSTALL_LIB_DIR
and friends, and it inverts the conditional: standalone C++ installs would then
need torch/ suppressed everywhere.

The prefix-must-be-<platlib>/torch coupling is now enforced rather than
assumed. install-dir is what makes the two agree, so changing it silently
produces an install that only fails at import; a mismatch is now a
configure-time FATAL_ERROR naming both paths and pointing at pyproject.toml.

Note that tools/build_libtorch.py loses the torchgen/packaged mirroring, as it
builds with BUILD_PYTHON=OFF and no longer produces a Python payload. The only
consumer of those files is torchgen/gen_backend_stubs.py at runtime in an
installed torchgen, which is not in the libtorch path.

Test Plan:

All on Linux/CPU unless noted, against the reporter's exact configure from the
issue:

```
cmake -S . -B build-standalone -GNinja \
  -DCMAKE_INSTALL_PREFIX=/tmp/prefix \
  -DATEN_NO_TEST=ON -DBUILD_BINARY=ON -DBUILD_CUSTOM_PROTOBUF=OFF \
  -DBUILD_PYTHON=ON -DBUILD_TEST=OFF -DUSE_OPENMP=ON \
  -DCMAKE_INSTALL_LIBDIR=lib -DUSE_DISTRIBUTED=OFF -DUSE_CUDA=OFF

grep -rhoE '\$\{CMAKE_INSTALL_PREFIX\}/[A-Za-z_./]*' \
  --include=cmake_install.cmake build-standalone/ \
  | sed 's|${CMAKE_INSTALL_PREFIX}/||' | cut -d/ -f1 | sort -u
grep -rh --include=cmake_install.cmake 'CMAKE_INSTALL_PREFIX}/\."' build-standalone/
grep -rh --include=cmake_install.cmake "DESTINATION \"$PWD" build-standalone/
grep -rhE --include=cmake_install.cmake 'DESTINATION "(/torch|/torchgen)' build-standalone/
```

Before: `. bin csrc _dynamo _export _inductor include lib share testing utils`,
plus seven rules with destinations inside the source checkout. After:
`bin include lib share`, and the other three greps empty. The last one checks
that an unset SKBUILD_PLATLIB_DIR has not turned a destination into an absolute
path at the filesystem root. The greps must recurse over every
cmake_install.cmake, not just the top-level one, since several of the offending
rules live in subdirectory scopes.

Same configure with -DUSE_CUDA=ON -DUSE_DISTRIBUTED=ON -DUSE_NCCL=ON
-DUSE_NCCL_EP=ON -DUSE_SYSTEM_NCCL=ON: the _nccl_ep target is still built (it
appears in build.ninja, producing torch/_nccl_ep.cpython-310-x86_64-linux-gnu.so)
but contributes no install rule, and the prefix root stays clean.

The guard, exercised the way it can actually regress -- by changing install-dir
in pyproject.toml to something other than "torch" and building a wheel -- fails
configure with the FATAL_ERROR, reporting platlib, the expected prefix and the
actual one.

Wheel contents, via scikit-build-core:

```
pip install -e . -v --no-build-isolation   # twice, consecutively
python -c "import torch, torchgen.packaged, functorch; print(torch.__version__)"
```

Both builds succeed, each configure picking up its own staging directory. The
installed file list under site-packages is identical to a pre-change build of
the same base: 9679 files, none gained or lost, including the mirrored copies
that exist only in the install tree (valgrind.h/callgrind.h, torchgen/packaged
with native_functions.yaml, tags.yaml, 42 templates and 16 autograd files) and
the generated version.py, _C, 43 .pyi stubs and 20 jinja templates. import and
a matmul both work.

Lint:

```
lintrunner -a --take CMAKE,NEWLINE,SPACES,TABS,CODESPELL,COPYRIGHT,EXEC \
  CMakeLists.txt cmake/FileMirroring.cmake cmake/PackageData.cmake \
  cmake/Summary.cmake torch/CMakeLists.txt
```

Not covered: Windows and macOS were not exercised. The Windows-specific rule
here is the _C import library, which keeps its existing prefix-relative
`ARCHIVE DESTINATION "lib"` since it is a C++ artifact.

This change was authored with the assistance of an AI coding assistant.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
zklaus added a commit to zklaus/pytorch that referenced this pull request Aug 18, 2026
Since v2.13.0 a standalone `cmake --install` scatters Python package
directories (_dynamo/, _export/, _inductor/, csrc/, testing/, utils/) across
CMAKE_INSTALL_PREFIX alongside bin/, include/, lib/ and share/, and writes
into the source checkout besides. Reported as pytorch#191102. The prefix-root
scattering comes from pytorch#177643, which added cmake/PackageData.cmake and its
unconditional include; the source-checkout writes from pytorch#177642, which did the
same for cmake/FileMirroring.cmake.

scikit-build-core models a wheel as several trees -- platlib, purelib, data,
headers, scripts, metadata -- and exports each to CMake as SKBUILD_<TREE>_DIR.
`wheel.install-dir` selects which tree, plus optional subpath, becomes
CMAKE_INSTALL_PREFIX; it defaults to empty, which makes the prefix the platlib
root itself. We set it to "torch" because the C++ tree has to sit at torch/lib
for the $ORIGIN/lib install rpath and torch/__init__.py's __file__ lookups to
resolve, and that pushes the prefix one level below platlib.

Most of our Python-side install rules were nonetheless written relative to the
prefix, using it as a proxy for the platlib tree. That only holds while
scikit-build-core is driving and install-dir is exactly "torch". A standalone
cmake install has a conventional prefix and no platlib at all, so the same
rules deposit torch/ package data at the prefix root. cmake/FileMirroring.cmake
did address the tree properly via SKBUILD_PLATLIB_DIR, but fell back to
PROJECT_SOURCE_DIR when it was undefined, which is how a standalone install
came to write into the checkout.

This is not an edge case: docs/libtorch.rst documents building libtorch with
`cmake --build . --target install` into a CMAKE_INSTALL_PREFIX of your choice,
which is the flow the report used. The layout that install should produce is
nowhere documented, which is a gap worth closing separately.

The rules now address SKBUILD_PLATLIB_DIR directly -- cmake/PackageData.cmake,
cmake/FileMirroring.cmake, and the _C, _nccl_ep and version.py rules in
torch/CMakeLists.txt -- guarded on SKBUILD, the sentinel scikit-build-core sets
for exactly this question. A build with no wheel behind it (libtorch,
tools/build_libtorch.py, downstream cmake --install) installs the C++
distribution alone, which restores the pre-2.13 contract: setup.py owned all
the Python packaging back when a standalone install was C++ only.

Two alternatives are worth naming. The reporter suggested a named install
COMPONENT held out of the default install; that is orthogonal, and already in
flight as pytorch#184873 for wheel scoping, but it would not fix this on its own,
because CMake runs every install() rule on a component-less `cmake --install`
whatever its tag. Dropping `wheel.install-dir` instead would make the prefix
the platlib root and coincide the two, but it needs a torch/ prefix on ~90 C++
destinations, ~40 of them raw literals not routed through TORCH_INSTALL_LIB_DIR
and friends, and it inverts the conditional: standalone C++ installs would then
need torch/ suppressed everywhere.

The prefix-must-be-<platlib>/torch coupling is now enforced rather than
assumed. install-dir is what makes the two agree, so changing it silently
produces an install that only fails at import; a mismatch is now a
configure-time FATAL_ERROR naming both paths and pointing at pyproject.toml.

Note that tools/build_libtorch.py loses the torchgen/packaged mirroring, as it
builds with BUILD_PYTHON=OFF and no longer produces a Python payload. The only
consumer of those files is torchgen/gen_backend_stubs.py at runtime in an
installed torchgen, which is not in the libtorch path.

Test Plan:

All on Linux/CPU unless noted, against the reporter's exact configure from the
issue:

```
cmake -S . -B build-standalone -GNinja \
  -DCMAKE_INSTALL_PREFIX=/tmp/prefix \
  -DATEN_NO_TEST=ON -DBUILD_BINARY=ON -DBUILD_CUSTOM_PROTOBUF=OFF \
  -DBUILD_PYTHON=ON -DBUILD_TEST=OFF -DUSE_OPENMP=ON \
  -DCMAKE_INSTALL_LIBDIR=lib -DUSE_DISTRIBUTED=OFF -DUSE_CUDA=OFF

grep -rhoE '\$\{CMAKE_INSTALL_PREFIX\}/[A-Za-z_./]*' \
  --include=cmake_install.cmake build-standalone/ \
  | sed 's|${CMAKE_INSTALL_PREFIX}/||' | cut -d/ -f1 | sort -u
grep -rh --include=cmake_install.cmake 'CMAKE_INSTALL_PREFIX}/\."' build-standalone/
grep -rh --include=cmake_install.cmake "DESTINATION \"$PWD" build-standalone/
grep -rhE --include=cmake_install.cmake 'DESTINATION "(/torch|/torchgen)' build-standalone/
```

Before: `. bin csrc _dynamo _export _inductor include lib share testing utils`,
plus seven rules with destinations inside the source checkout. After:
`bin include lib share`, and the other three greps empty. The last one checks
that an unset SKBUILD_PLATLIB_DIR has not turned a destination into an absolute
path at the filesystem root. The greps must recurse over every
cmake_install.cmake, not just the top-level one, since several of the offending
rules live in subdirectory scopes.

Same configure with -DUSE_CUDA=ON -DUSE_DISTRIBUTED=ON -DUSE_NCCL=ON
-DUSE_NCCL_EP=ON -DUSE_SYSTEM_NCCL=ON: the _nccl_ep target is still built (it
appears in build.ninja, producing torch/_nccl_ep.cpython-310-x86_64-linux-gnu.so)
but contributes no install rule, and the prefix root stays clean.

The guard, exercised the way it can actually regress -- by changing install-dir
in pyproject.toml to something other than "torch" and building a wheel -- fails
configure with the FATAL_ERROR, reporting platlib, the expected prefix and the
actual one.

Wheel contents, via scikit-build-core:

```
pip install -e . -v --no-build-isolation   # twice, consecutively
python -c "import torch, torchgen.packaged, functorch; print(torch.__version__)"
```

Both builds succeed, each configure picking up its own staging directory. The
installed file list under site-packages is identical to a pre-change build of
the same base: 9679 files, none gained or lost, including the mirrored copies
that exist only in the install tree (valgrind.h/callgrind.h, torchgen/packaged
with native_functions.yaml, tags.yaml, 42 templates and 16 autograd files) and
the generated version.py, _C, 43 .pyi stubs and 20 jinja templates. import and
a matmul both work.

Lint:

```
lintrunner -a --take CMAKE,NEWLINE,SPACES,TABS,CODESPELL,COPYRIGHT,EXEC \
  CMakeLists.txt cmake/FileMirroring.cmake cmake/PackageData.cmake \
  cmake/Summary.cmake torch/CMakeLists.txt
```

Not covered: Windows and macOS were not exercised. The Windows-specific rule
here is the _C import library, which keeps its existing prefix-relative
`ARCHIVE DESTINATION "lib"` since it is a C++ artifact.

This change was authored with the assistance of an AI coding assistant.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
zklaus added a commit to zklaus/pytorch that referenced this pull request Aug 18, 2026
Since v2.13.0 a standalone `cmake --install` scatters Python package
directories (_dynamo/, _export/, _inductor/, csrc/, testing/, utils/) across
CMAKE_INSTALL_PREFIX alongside bin/, include/, lib/ and share/, and writes
into the source checkout besides. Reported as pytorch#191102. The prefix-root
scattering comes from pytorch#177643, which added cmake/PackageData.cmake and its
unconditional include; the source-checkout writes from pytorch#177642, which did the
same for cmake/FileMirroring.cmake.

scikit-build-core models a wheel as several trees -- platlib, purelib, data,
headers, scripts, metadata -- and exports each to CMake as SKBUILD_<TREE>_DIR.
`wheel.install-dir` selects which tree, plus optional subpath, becomes
CMAKE_INSTALL_PREFIX; it defaults to empty, which makes the prefix the platlib
root itself. We set it to "torch" because the C++ tree has to sit at torch/lib
for the $ORIGIN/lib install rpath and torch/__init__.py's __file__ lookups to
resolve, and that pushes the prefix one level below platlib.

Most of our Python-side install rules were nonetheless written relative to the
prefix, using it as a proxy for the platlib tree. That only holds while
scikit-build-core is driving and install-dir is exactly "torch". A standalone
cmake install has a conventional prefix and no platlib at all, so the same
rules deposit torch/ package data at the prefix root. cmake/FileMirroring.cmake
did address the tree properly via SKBUILD_PLATLIB_DIR, but fell back to
PROJECT_SOURCE_DIR when it was undefined, which is how a standalone install
came to write into the checkout.

This is not an edge case: docs/libtorch.rst documents building libtorch with
`cmake --build . --target install` into a CMAKE_INSTALL_PREFIX of your choice,
which is the flow the report used. The layout that install should produce is
nowhere documented, which is a gap worth closing separately.

The rules now address SKBUILD_PLATLIB_DIR directly -- cmake/PackageData.cmake,
cmake/FileMirroring.cmake, and the _C, _nccl_ep and version.py rules in
torch/CMakeLists.txt -- guarded on SKBUILD, the sentinel scikit-build-core sets
for exactly this question. A build with no wheel behind it (libtorch,
tools/build_libtorch.py, downstream cmake --install) installs the C++
distribution alone, which restores the pre-2.13 contract: setup.py owned all
the Python packaging back when a standalone install was C++ only.

Two alternatives are worth naming. The reporter suggested a named install
COMPONENT held out of the default install; that is orthogonal, and already in
flight as pytorch#184873 for wheel scoping, but it would not fix this on its own,
because CMake runs every install() rule on a component-less `cmake --install`
whatever its tag. Dropping `wheel.install-dir` instead would make the prefix
the platlib root and coincide the two, but it needs a torch/ prefix on ~90 C++
destinations, ~40 of them raw literals not routed through TORCH_INSTALL_LIB_DIR
and friends, and it inverts the conditional: standalone C++ installs would then
need torch/ suppressed everywhere.

The prefix-must-be-<platlib>/torch coupling is now enforced rather than
assumed. install-dir is what makes the two agree, so changing it silently
produces an install that only fails at import; a mismatch is now a
configure-time FATAL_ERROR naming both paths and pointing at pyproject.toml.

Note that tools/build_libtorch.py loses the torchgen/packaged mirroring, as it
builds with BUILD_PYTHON=OFF and no longer produces a Python payload. The only
consumer of those files is torchgen/gen_backend_stubs.py at runtime in an
installed torchgen, which is not in the libtorch path.

Test Plan:

All on Linux/CPU unless noted, against the reporter's exact configure from the
issue:

```
cmake -S . -B build-standalone -GNinja \
  -DCMAKE_INSTALL_PREFIX=/tmp/prefix \
  -DATEN_NO_TEST=ON -DBUILD_BINARY=ON -DBUILD_CUSTOM_PROTOBUF=OFF \
  -DBUILD_PYTHON=ON -DBUILD_TEST=OFF -DUSE_OPENMP=ON \
  -DCMAKE_INSTALL_LIBDIR=lib -DUSE_DISTRIBUTED=OFF -DUSE_CUDA=OFF

grep -rhoE '\$\{CMAKE_INSTALL_PREFIX\}/[A-Za-z_./]*' \
  --include=cmake_install.cmake build-standalone/ \
  | sed 's|${CMAKE_INSTALL_PREFIX}/||' | cut -d/ -f1 | sort -u
grep -rh --include=cmake_install.cmake 'CMAKE_INSTALL_PREFIX}/\."' build-standalone/
grep -rh --include=cmake_install.cmake "DESTINATION \"$PWD" build-standalone/
grep -rhE --include=cmake_install.cmake 'DESTINATION "(/torch|/torchgen)' build-standalone/
```

Before: `. bin csrc _dynamo _export _inductor include lib share testing utils`,
plus seven rules with destinations inside the source checkout. After:
`bin include lib share`, and the other three greps empty. The last one checks
that an unset SKBUILD_PLATLIB_DIR has not turned a destination into an absolute
path at the filesystem root. The greps must recurse over every
cmake_install.cmake, not just the top-level one, since several of the offending
rules live in subdirectory scopes.

Same configure with -DUSE_CUDA=ON -DUSE_DISTRIBUTED=ON -DUSE_NCCL=ON
-DUSE_NCCL_EP=ON -DUSE_SYSTEM_NCCL=ON: the _nccl_ep target is still built (it
appears in build.ninja, producing torch/_nccl_ep.cpython-310-x86_64-linux-gnu.so)
but contributes no install rule, and the prefix root stays clean.

The guard, exercised the way it can actually regress -- by changing install-dir
in pyproject.toml to something other than "torch" and building a wheel -- fails
configure with the FATAL_ERROR, reporting platlib, the expected prefix and the
actual one.

Wheel contents, via scikit-build-core:

```
pip install -e . -v --no-build-isolation   # twice, consecutively
python -c "import torch, torchgen.packaged, functorch; print(torch.__version__)"
```

Both builds succeed, each configure picking up its own staging directory. The
installed file list under site-packages is identical to a pre-change build of
the same base: 9679 files, none gained or lost, including the mirrored copies
that exist only in the install tree (valgrind.h/callgrind.h, torchgen/packaged
with native_functions.yaml, tags.yaml, 42 templates and 16 autograd files) and
the generated version.py, _C, 43 .pyi stubs and 20 jinja templates. import and
a matmul both work.

Lint:

```
lintrunner -a --take CMAKE,NEWLINE,SPACES,TABS,CODESPELL,COPYRIGHT,EXEC \
  CMakeLists.txt cmake/FileMirroring.cmake cmake/PackageData.cmake \
  cmake/Summary.cmake torch/CMakeLists.txt
```

Not covered: Windows and macOS were not exercised. The Windows-specific rule
here is the _C import library, which keeps its existing prefix-relative
`ARCHIVE DESTINATION "lib"` since it is a C++ artifact.

This change was authored with the assistance of an AI coding assistant.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
@github-actions github-actions Bot closed this Aug 20, 2026
zklaus added a commit to zklaus/pytorch that referenced this pull request Aug 28, 2026
Since v2.13.0 a standalone `cmake --install` scatters Python package
directories (_dynamo/, _export/, _inductor/, csrc/, testing/, utils/) across
CMAKE_INSTALL_PREFIX alongside bin/, include/, lib/ and share/, and writes
into the source checkout besides. Reported as pytorch#191102. The prefix-root
scattering comes from pytorch#177643, which added cmake/PackageData.cmake and its
unconditional include; the source-checkout writes from pytorch#177642, which did the
same for cmake/FileMirroring.cmake.

scikit-build-core models a wheel as several trees -- platlib, purelib, data,
headers, scripts, metadata -- and exports each to CMake as SKBUILD_<TREE>_DIR.
`wheel.install-dir` selects which tree, plus optional subpath, becomes
CMAKE_INSTALL_PREFIX; it defaults to empty, which makes the prefix the platlib
root itself. We set it to "torch" because the C++ tree has to sit at torch/lib
for the $ORIGIN/lib install rpath and torch/__init__.py's __file__ lookups to
resolve, and that pushes the prefix one level below platlib.

Most of our Python-side install rules were nonetheless written relative to the
prefix, using it as a proxy for the platlib tree. That only holds while
scikit-build-core is driving and install-dir is exactly "torch". A standalone
cmake install has a conventional prefix and no platlib at all, so the same
rules deposit torch/ package data at the prefix root. cmake/FileMirroring.cmake
did address the tree properly via SKBUILD_PLATLIB_DIR, but fell back to
PROJECT_SOURCE_DIR when it was undefined, which is how a standalone install
came to write into the checkout.

This is not an edge case: docs/libtorch.rst documents building libtorch with
`cmake --build . --target install` into a CMAKE_INSTALL_PREFIX of your choice,
which is the flow the report used. The layout that install should produce is
nowhere documented, which is a gap worth closing separately.

The rules now address SKBUILD_PLATLIB_DIR directly -- cmake/PackageData.cmake,
cmake/FileMirroring.cmake, and the _C, _nccl_ep and version.py rules in
torch/CMakeLists.txt -- guarded on SKBUILD, the sentinel scikit-build-core sets
for exactly this question. A build with no wheel behind it (libtorch,
tools/build_libtorch.py, downstream cmake --install) installs the C++
distribution alone, which restores the pre-2.13 contract: setup.py owned all
the Python packaging back when a standalone install was C++ only.

Two alternatives are worth naming. The reporter suggested a named install
COMPONENT held out of the default install; that is orthogonal, and already in
flight as pytorch#184873 for wheel scoping, but it would not fix this on its own,
because CMake runs every install() rule on a component-less `cmake --install`
whatever its tag. Dropping `wheel.install-dir` instead would make the prefix
the platlib root and coincide the two, but it needs a torch/ prefix on ~90 C++
destinations, ~40 of them raw literals not routed through TORCH_INSTALL_LIB_DIR
and friends, and it inverts the conditional: standalone C++ installs would then
need torch/ suppressed everywhere.

The prefix-must-be-<platlib>/torch coupling is now enforced rather than
assumed. install-dir is what makes the two agree, so changing it silently
produces an install that only fails at import; a mismatch is now a
configure-time FATAL_ERROR naming both paths and pointing at pyproject.toml.

Note that tools/build_libtorch.py loses the torchgen/packaged mirroring, as it
builds with BUILD_PYTHON=OFF and no longer produces a Python payload. The only
consumer of those files is torchgen/gen_backend_stubs.py at runtime in an
installed torchgen, which is not in the libtorch path.

Test Plan:

All on Linux/CPU unless noted, against the reporter's exact configure from the
issue:

```
cmake -S . -B build-standalone -GNinja \
  -DCMAKE_INSTALL_PREFIX=/tmp/prefix \
  -DATEN_NO_TEST=ON -DBUILD_BINARY=ON -DBUILD_CUSTOM_PROTOBUF=OFF \
  -DBUILD_PYTHON=ON -DBUILD_TEST=OFF -DUSE_OPENMP=ON \
  -DCMAKE_INSTALL_LIBDIR=lib -DUSE_DISTRIBUTED=OFF -DUSE_CUDA=OFF

grep -rhoE '\$\{CMAKE_INSTALL_PREFIX\}/[A-Za-z_./]*' \
  --include=cmake_install.cmake build-standalone/ \
  | sed 's|${CMAKE_INSTALL_PREFIX}/||' | cut -d/ -f1 | sort -u
grep -rh --include=cmake_install.cmake 'CMAKE_INSTALL_PREFIX}/\."' build-standalone/
grep -rh --include=cmake_install.cmake "DESTINATION \"$PWD" build-standalone/
grep -rhE --include=cmake_install.cmake 'DESTINATION "(/torch|/torchgen)' build-standalone/
```

Before: `. bin csrc _dynamo _export _inductor include lib share testing utils`,
plus seven rules with destinations inside the source checkout. After:
`bin include lib share`, and the other three greps empty. The last one checks
that an unset SKBUILD_PLATLIB_DIR has not turned a destination into an absolute
path at the filesystem root. The greps must recurse over every
cmake_install.cmake, not just the top-level one, since several of the offending
rules live in subdirectory scopes.

Same configure with -DUSE_CUDA=ON -DUSE_DISTRIBUTED=ON -DUSE_NCCL=ON
-DUSE_NCCL_EP=ON -DUSE_SYSTEM_NCCL=ON: the _nccl_ep target is still built (it
appears in build.ninja, producing torch/_nccl_ep.cpython-310-x86_64-linux-gnu.so)
but contributes no install rule, and the prefix root stays clean.

The guard, exercised the way it can actually regress -- by changing install-dir
in pyproject.toml to something other than "torch" and building a wheel -- fails
configure with the FATAL_ERROR, reporting platlib, the expected prefix and the
actual one.

Wheel contents, via scikit-build-core:

```
pip install -e . -v --no-build-isolation   # twice, consecutively
python -c "import torch, torchgen.packaged, functorch; print(torch.__version__)"
```

Both builds succeed, each configure picking up its own staging directory. The
installed file list under site-packages is identical to a pre-change build of
the same base: 9679 files, none gained or lost, including the mirrored copies
that exist only in the install tree (valgrind.h/callgrind.h, torchgen/packaged
with native_functions.yaml, tags.yaml, 42 templates and 16 autograd files) and
the generated version.py, _C, 43 .pyi stubs and 20 jinja templates. import and
a matmul both work.

Lint:

```
lintrunner -a --take CMAKE,NEWLINE,SPACES,TABS,CODESPELL,COPYRIGHT,EXEC \
  CMakeLists.txt cmake/FileMirroring.cmake cmake/PackageData.cmake \
  cmake/Summary.cmake torch/CMakeLists.txt
```

Not covered: Windows and macOS were not exercised. The Windows-specific rule
here is the _C import library, which keeps its existing prefix-relative
`ARCHIVE DESTINATION "lib"` since it is a C++ artifact.

This change was authored with the assistance of an AI coding assistant.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
pytorchmergebot pushed a commit that referenced this pull request Aug 28, 2026
…#191494)

Since v2.13.0 a standalone `cmake --install` scatters Python package
directories (_dynamo/, _export/, _inductor/, csrc/, testing/, utils/) across
CMAKE_INSTALL_PREFIX alongside bin/, include/, lib/ and share/, and writes
into the source checkout besides. Reported as #191102. The prefix-root
scattering comes from #177643, which added cmake/PackageData.cmake and its
unconditional include; the source-checkout writes from #177642, which did the
same for cmake/FileMirroring.cmake.

scikit-build-core models a wheel as several trees -- platlib, purelib, data,
headers, scripts, metadata -- and exports each to CMake as SKBUILD_<TREE>_DIR.
`wheel.install-dir` selects which tree, plus optional subpath, becomes
CMAKE_INSTALL_PREFIX; it defaults to empty, which makes the prefix the platlib
root itself. We set it to "torch" because the C++ tree has to sit at torch/lib
for the $ORIGIN/lib install rpath and torch/__init__.py's __file__ lookups to
resolve, and that pushes the prefix one level below platlib.

Most of our Python-side install rules were nonetheless written relative to the
prefix, using it as a proxy for the platlib tree. That only holds while
scikit-build-core is driving and install-dir is exactly "torch". A standalone
cmake install has a conventional prefix and no platlib at all, so the same
rules deposit torch/ package data at the prefix root. cmake/FileMirroring.cmake
did address the tree properly via SKBUILD_PLATLIB_DIR, but fell back to
PROJECT_SOURCE_DIR when it was undefined, which is how a standalone install
came to write into the checkout.

This is not an edge case: docs/libtorch.rst documents building libtorch with
`cmake --build . --target install` into a CMAKE_INSTALL_PREFIX of your choice,
which is the flow the report used. The layout that install should produce is
nowhere documented, which is a gap worth closing separately.

The rules now address SKBUILD_PLATLIB_DIR directly -- cmake/PackageData.cmake,
cmake/FileMirroring.cmake, and the _C, _nccl_ep, version.py and _rocm_init.py
rules in torch/CMakeLists.txt -- guarded on SKBUILD, the sentinel
scikit-build-core sets for exactly this question. A build scikit-build-core does
not drive (libtorch, tools/build_libtorch.py, downstream cmake --install)
installs the C++
distribution alone, which restores the pre-2.13 contract: setup.py owned all
the Python packaging back when a standalone install was C++ only.

Two alternatives are worth naming. The reporter suggested a named install
COMPONENT held out of the default install; that is orthogonal, and already in
flight as #184873 for wheel scoping, but it would not fix this on its own,
because CMake runs every install() rule on a component-less `cmake --install`
whatever its tag. Dropping `wheel.install-dir` instead would make the prefix
the platlib root and coincide the two, but it needs a torch/ prefix on ~90 C++
destinations, ~40 of them raw literals not routed through TORCH_INSTALL_LIB_DIR
and friends, and it inverts the conditional: standalone C++ installs would then
need torch/ suppressed everywhere.

The prefix-must-be-<platlib>/torch coupling is now enforced rather than
assumed. install-dir is what makes the two agree, so changing it silently
produces an install that only fails at import; a mismatch is now a
configure-time FATAL_ERROR naming both paths and pointing at pyproject.toml.

Note that tools/build_libtorch.py loses the torchgen/packaged mirroring, as it
builds with BUILD_PYTHON=OFF and no longer produces a Python payload. The only
consumer of those files is torchgen/gen_backend_stubs.py at runtime in an
installed torchgen, which is not in the libtorch path.

Three things surfaced while reviewing the above and are folded in. #191625 added
an install rule for the gitignored torch/_rocm_init.py while this was open, in
the same prefix-relative form; it was the last remaining DESTINATION "." in the
tree and is now guarded and addressed like its neighbours. The _C rule's ARCHIVE
clause is dropped: _C is a MODULE library, so no Windows import library is
produced and none ships -- the 2026-08-18 cp312 win_amd64 nightlies carry 13 to
16 .lib files across the cpu, cu126 and xpu variants, every one a genuine C++
import library and no _C.lib among them. It was also the one prefix-relative
destination left in a rule whose other two were absolute.

A lint rule is added so this class of regression is rejected when it is written
rather than after a release. #191625 is the evidence that it recurs: it landed
three weeks after this fix and nothing caught it, because no CI job installs
standalone and inspects the result -- tools/build_libtorch.py is the only
standalone-install path exercised in CI and it configures with BUILD_PYTHON=OFF,
so it never evaluates these rules. CMAKE_INSTALL_PREFIX_ROOT forbids a
destination of "." across the build system; CMAKE_PLATLIB_DESTINATION
additionally requires every destination in the two package-data files to name
the wheel tree, which catches the prefix-relative variant. Both reuse the
existing grep_linter adapter and are registered in VERY_FAST_LINTERS in
.spin/cmds.py, which spin requires of every linter. This is a text check, not a
behavioural one: a destination that reaches the prefix root through a variable,
or a new rule that uses the wheel tree but omits its SKBUILD guard, still gets
through.

Test Plan:

All on Linux/CPU unless noted, against the reporter's exact configure from the
issue:

```
cmake -S . -B build-standalone -GNinja \
  -DCMAKE_INSTALL_PREFIX=/tmp/prefix \
  -DATEN_NO_TEST=ON -DBUILD_BINARY=ON -DBUILD_CUSTOM_PROTOBUF=OFF \
  -DBUILD_PYTHON=ON -DBUILD_TEST=OFF -DUSE_OPENMP=ON \
  -DCMAKE_INSTALL_LIBDIR=lib -DUSE_DISTRIBUTED=OFF -DUSE_CUDA=OFF

grep -rhoE '\$\{CMAKE_INSTALL_PREFIX\}/[A-Za-z_./]*' \
  --include=cmake_install.cmake build-standalone/ \
  | sed 's|${CMAKE_INSTALL_PREFIX}/||' | cut -d/ -f1 | sort -u
grep -rh --include=cmake_install.cmake 'CMAKE_INSTALL_PREFIX}/\."' build-standalone/
grep -rh --include=cmake_install.cmake "DESTINATION \"$PWD" build-standalone/
grep -rhE --include=cmake_install.cmake 'DESTINATION "(/torch|/torchgen)' build-standalone/
```

Before: `. bin csrc _dynamo _export _inductor include lib share testing utils`,
plus seven rules with destinations inside the source checkout. After:
`bin include lib share`, and the other three greps empty. The last one checks
that an unset SKBUILD_PLATLIB_DIR has not turned a destination into an absolute
path at the filesystem root. The greps must recurse over every
cmake_install.cmake, not just the top-level one, since several of the offending
rules live in subdirectory scopes.

Same configure with -DUSE_CUDA=ON -DUSE_DISTRIBUTED=ON -DUSE_NCCL=ON
-DUSE_NCCL_EP=ON -DUSE_SYSTEM_NCCL=ON: the _nccl_ep target is still built (it
appears in build.ninja, producing torch/_nccl_ep.cpython-310-x86_64-linux-gnu.so)
but contributes no install rule, and the prefix root stays clean.

The guard, exercised the way it can actually regress -- by changing install-dir
in pyproject.toml to something other than "torch" and building a wheel -- fails
configure with the FATAL_ERROR, reporting platlib, the expected prefix and the
actual one.

Wheel contents, via scikit-build-core:

```
pip install -e . -v --no-build-isolation   # twice, consecutively
python -c "import torch, torchgen.packaged, functorch; print(torch.__version__)"
```

Both builds succeed, each configure picking up its own staging directory. The
installed file list under site-packages is identical to a pre-change build of
the same base: 9679 files, none gained or lost, including the mirrored copies
that exist only in the install tree (valgrind.h/callgrind.h, torchgen/packaged
with native_functions.yaml, tags.yaml, 42 templates and 16 autograd files) and
the generated version.py, _C, 43 .pyi stubs and 20 jinja templates. import and
a matmul both work.

The new lint rules, checked in both directions -- clean on this tree, and
flagging the real regressions on the pre-fix tree:

```
spin lint -- --take CMAKE_INSTALL_PREFIX_ROOT,CMAKE_PLATLIB_DESTINATION
```

CMAKE_INSTALL_PREFIX_ROOT flags 6 lines on the pre-fix tree -- torch/CMakeLists.txt
474, 475, 517, 548 and 555, plus cmake/PackageData.cmake:21 -- where 555 is the
#191625 _rocm_init.py rule, so the check demonstrably rejects the regression that
actually happened. CMAKE_PLATLIB_DESTINATION flags 12.

Lint:

```
lintrunner -a --take CMAKE,NEWLINE,SPACES,TABS,CODESPELL,COPYRIGHT,EXEC \
  CMakeLists.txt cmake/FileMirroring.cmake cmake/PackageData.cmake \
  cmake/Summary.cmake torch/CMakeLists.txt .lintrunner.toml .spin/cmds.py
```

Not covered: Windows and macOS were not exercised locally; ciflow/binaries is
what covers them.

This change was authored with the assistance of an AI coding assistant.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Fixes #191102

Pull Request resolved: #191494
Approved by: https://github.com/atalman
orrangetabby17 pushed a commit to orrangetabby17/pytorch that referenced this pull request Sep 2, 2026
…pytorch#191494)

Since v2.13.0 a standalone `cmake --install` scatters Python package
directories (_dynamo/, _export/, _inductor/, csrc/, testing/, utils/) across
CMAKE_INSTALL_PREFIX alongside bin/, include/, lib/ and share/, and writes
into the source checkout besides. Reported as pytorch#191102. The prefix-root
scattering comes from pytorch#177643, which added cmake/PackageData.cmake and its
unconditional include; the source-checkout writes from pytorch#177642, which did the
same for cmake/FileMirroring.cmake.

scikit-build-core models a wheel as several trees -- platlib, purelib, data,
headers, scripts, metadata -- and exports each to CMake as SKBUILD_<TREE>_DIR.
`wheel.install-dir` selects which tree, plus optional subpath, becomes
CMAKE_INSTALL_PREFIX; it defaults to empty, which makes the prefix the platlib
root itself. We set it to "torch" because the C++ tree has to sit at torch/lib
for the $ORIGIN/lib install rpath and torch/__init__.py's __file__ lookups to
resolve, and that pushes the prefix one level below platlib.

Most of our Python-side install rules were nonetheless written relative to the
prefix, using it as a proxy for the platlib tree. That only holds while
scikit-build-core is driving and install-dir is exactly "torch". A standalone
cmake install has a conventional prefix and no platlib at all, so the same
rules deposit torch/ package data at the prefix root. cmake/FileMirroring.cmake
did address the tree properly via SKBUILD_PLATLIB_DIR, but fell back to
PROJECT_SOURCE_DIR when it was undefined, which is how a standalone install
came to write into the checkout.

This is not an edge case: docs/libtorch.rst documents building libtorch with
`cmake --build . --target install` into a CMAKE_INSTALL_PREFIX of your choice,
which is the flow the report used. The layout that install should produce is
nowhere documented, which is a gap worth closing separately.

The rules now address SKBUILD_PLATLIB_DIR directly -- cmake/PackageData.cmake,
cmake/FileMirroring.cmake, and the _C, _nccl_ep, version.py and _rocm_init.py
rules in torch/CMakeLists.txt -- guarded on SKBUILD, the sentinel
scikit-build-core sets for exactly this question. A build scikit-build-core does
not drive (libtorch, tools/build_libtorch.py, downstream cmake --install)
installs the C++
distribution alone, which restores the pre-2.13 contract: setup.py owned all
the Python packaging back when a standalone install was C++ only.

Two alternatives are worth naming. The reporter suggested a named install
COMPONENT held out of the default install; that is orthogonal, and already in
flight as pytorch#184873 for wheel scoping, but it would not fix this on its own,
because CMake runs every install() rule on a component-less `cmake --install`
whatever its tag. Dropping `wheel.install-dir` instead would make the prefix
the platlib root and coincide the two, but it needs a torch/ prefix on ~90 C++
destinations, ~40 of them raw literals not routed through TORCH_INSTALL_LIB_DIR
and friends, and it inverts the conditional: standalone C++ installs would then
need torch/ suppressed everywhere.

The prefix-must-be-<platlib>/torch coupling is now enforced rather than
assumed. install-dir is what makes the two agree, so changing it silently
produces an install that only fails at import; a mismatch is now a
configure-time FATAL_ERROR naming both paths and pointing at pyproject.toml.

Note that tools/build_libtorch.py loses the torchgen/packaged mirroring, as it
builds with BUILD_PYTHON=OFF and no longer produces a Python payload. The only
consumer of those files is torchgen/gen_backend_stubs.py at runtime in an
installed torchgen, which is not in the libtorch path.

Three things surfaced while reviewing the above and are folded in. pytorch#191625 added
an install rule for the gitignored torch/_rocm_init.py while this was open, in
the same prefix-relative form; it was the last remaining DESTINATION "." in the
tree and is now guarded and addressed like its neighbours. The _C rule's ARCHIVE
clause is dropped: _C is a MODULE library, so no Windows import library is
produced and none ships -- the 2026-08-18 cp312 win_amd64 nightlies carry 13 to
16 .lib files across the cpu, cu126 and xpu variants, every one a genuine C++
import library and no _C.lib among them. It was also the one prefix-relative
destination left in a rule whose other two were absolute.

A lint rule is added so this class of regression is rejected when it is written
rather than after a release. pytorch#191625 is the evidence that it recurs: it landed
three weeks after this fix and nothing caught it, because no CI job installs
standalone and inspects the result -- tools/build_libtorch.py is the only
standalone-install path exercised in CI and it configures with BUILD_PYTHON=OFF,
so it never evaluates these rules. CMAKE_INSTALL_PREFIX_ROOT forbids a
destination of "." across the build system; CMAKE_PLATLIB_DESTINATION
additionally requires every destination in the two package-data files to name
the wheel tree, which catches the prefix-relative variant. Both reuse the
existing grep_linter adapter and are registered in VERY_FAST_LINTERS in
.spin/cmds.py, which spin requires of every linter. This is a text check, not a
behavioural one: a destination that reaches the prefix root through a variable,
or a new rule that uses the wheel tree but omits its SKBUILD guard, still gets
through.

Test Plan:

All on Linux/CPU unless noted, against the reporter's exact configure from the
issue:

```
cmake -S . -B build-standalone -GNinja \
  -DCMAKE_INSTALL_PREFIX=/tmp/prefix \
  -DATEN_NO_TEST=ON -DBUILD_BINARY=ON -DBUILD_CUSTOM_PROTOBUF=OFF \
  -DBUILD_PYTHON=ON -DBUILD_TEST=OFF -DUSE_OPENMP=ON \
  -DCMAKE_INSTALL_LIBDIR=lib -DUSE_DISTRIBUTED=OFF -DUSE_CUDA=OFF

grep -rhoE '\$\{CMAKE_INSTALL_PREFIX\}/[A-Za-z_./]*' \
  --include=cmake_install.cmake build-standalone/ \
  | sed 's|${CMAKE_INSTALL_PREFIX}/||' | cut -d/ -f1 | sort -u
grep -rh --include=cmake_install.cmake 'CMAKE_INSTALL_PREFIX}/\."' build-standalone/
grep -rh --include=cmake_install.cmake "DESTINATION \"$PWD" build-standalone/
grep -rhE --include=cmake_install.cmake 'DESTINATION "(/torch|/torchgen)' build-standalone/
```

Before: `. bin csrc _dynamo _export _inductor include lib share testing utils`,
plus seven rules with destinations inside the source checkout. After:
`bin include lib share`, and the other three greps empty. The last one checks
that an unset SKBUILD_PLATLIB_DIR has not turned a destination into an absolute
path at the filesystem root. The greps must recurse over every
cmake_install.cmake, not just the top-level one, since several of the offending
rules live in subdirectory scopes.

Same configure with -DUSE_CUDA=ON -DUSE_DISTRIBUTED=ON -DUSE_NCCL=ON
-DUSE_NCCL_EP=ON -DUSE_SYSTEM_NCCL=ON: the _nccl_ep target is still built (it
appears in build.ninja, producing torch/_nccl_ep.cpython-310-x86_64-linux-gnu.so)
but contributes no install rule, and the prefix root stays clean.

The guard, exercised the way it can actually regress -- by changing install-dir
in pyproject.toml to something other than "torch" and building a wheel -- fails
configure with the FATAL_ERROR, reporting platlib, the expected prefix and the
actual one.

Wheel contents, via scikit-build-core:

```
pip install -e . -v --no-build-isolation   # twice, consecutively
python -c "import torch, torchgen.packaged, functorch; print(torch.__version__)"
```

Both builds succeed, each configure picking up its own staging directory. The
installed file list under site-packages is identical to a pre-change build of
the same base: 9679 files, none gained or lost, including the mirrored copies
that exist only in the install tree (valgrind.h/callgrind.h, torchgen/packaged
with native_functions.yaml, tags.yaml, 42 templates and 16 autograd files) and
the generated version.py, _C, 43 .pyi stubs and 20 jinja templates. import and
a matmul both work.

The new lint rules, checked in both directions -- clean on this tree, and
flagging the real regressions on the pre-fix tree:

```
spin lint -- --take CMAKE_INSTALL_PREFIX_ROOT,CMAKE_PLATLIB_DESTINATION
```

CMAKE_INSTALL_PREFIX_ROOT flags 6 lines on the pre-fix tree -- torch/CMakeLists.txt
474, 475, 517, 548 and 555, plus cmake/PackageData.cmake:21 -- where 555 is the
pytorch#191625 _rocm_init.py rule, so the check demonstrably rejects the regression that
actually happened. CMAKE_PLATLIB_DESTINATION flags 12.

Lint:

```
lintrunner -a --take CMAKE,NEWLINE,SPACES,TABS,CODESPELL,COPYRIGHT,EXEC \
  CMakeLists.txt cmake/FileMirroring.cmake cmake/PackageData.cmake \
  cmake/Summary.cmake torch/CMakeLists.txt .lintrunner.toml .spin/cmds.py
```

Not covered: Windows and macOS were not exercised locally; ciflow/binaries is
what covers them.

This change was authored with the assistance of an AI coding assistant.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Fixes pytorch#191102

Pull Request resolved: pytorch#191494
Approved by: https://github.com/atalman
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
…pytorch#191494)

Since v2.13.0 a standalone `cmake --install` scatters Python package
directories (_dynamo/, _export/, _inductor/, csrc/, testing/, utils/) across
CMAKE_INSTALL_PREFIX alongside bin/, include/, lib/ and share/, and writes
into the source checkout besides. Reported as pytorch#191102. The prefix-root
scattering comes from pytorch#177643, which added cmake/PackageData.cmake and its
unconditional include; the source-checkout writes from pytorch#177642, which did the
same for cmake/FileMirroring.cmake.

scikit-build-core models a wheel as several trees -- platlib, purelib, data,
headers, scripts, metadata -- and exports each to CMake as SKBUILD_<TREE>_DIR.
`wheel.install-dir` selects which tree, plus optional subpath, becomes
CMAKE_INSTALL_PREFIX; it defaults to empty, which makes the prefix the platlib
root itself. We set it to "torch" because the C++ tree has to sit at torch/lib
for the $ORIGIN/lib install rpath and torch/__init__.py's __file__ lookups to
resolve, and that pushes the prefix one level below platlib.

Most of our Python-side install rules were nonetheless written relative to the
prefix, using it as a proxy for the platlib tree. That only holds while
scikit-build-core is driving and install-dir is exactly "torch". A standalone
cmake install has a conventional prefix and no platlib at all, so the same
rules deposit torch/ package data at the prefix root. cmake/FileMirroring.cmake
did address the tree properly via SKBUILD_PLATLIB_DIR, but fell back to
PROJECT_SOURCE_DIR when it was undefined, which is how a standalone install
came to write into the checkout.

This is not an edge case: docs/libtorch.rst documents building libtorch with
`cmake --build . --target install` into a CMAKE_INSTALL_PREFIX of your choice,
which is the flow the report used. The layout that install should produce is
nowhere documented, which is a gap worth closing separately.

The rules now address SKBUILD_PLATLIB_DIR directly -- cmake/PackageData.cmake,
cmake/FileMirroring.cmake, and the _C, _nccl_ep, version.py and _rocm_init.py
rules in torch/CMakeLists.txt -- guarded on SKBUILD, the sentinel
scikit-build-core sets for exactly this question. A build scikit-build-core does
not drive (libtorch, tools/build_libtorch.py, downstream cmake --install)
installs the C++
distribution alone, which restores the pre-2.13 contract: setup.py owned all
the Python packaging back when a standalone install was C++ only.

Two alternatives are worth naming. The reporter suggested a named install
COMPONENT held out of the default install; that is orthogonal, and already in
flight as pytorch#184873 for wheel scoping, but it would not fix this on its own,
because CMake runs every install() rule on a component-less `cmake --install`
whatever its tag. Dropping `wheel.install-dir` instead would make the prefix
the platlib root and coincide the two, but it needs a torch/ prefix on ~90 C++
destinations, ~40 of them raw literals not routed through TORCH_INSTALL_LIB_DIR
and friends, and it inverts the conditional: standalone C++ installs would then
need torch/ suppressed everywhere.

The prefix-must-be-<platlib>/torch coupling is now enforced rather than
assumed. install-dir is what makes the two agree, so changing it silently
produces an install that only fails at import; a mismatch is now a
configure-time FATAL_ERROR naming both paths and pointing at pyproject.toml.

Note that tools/build_libtorch.py loses the torchgen/packaged mirroring, as it
builds with BUILD_PYTHON=OFF and no longer produces a Python payload. The only
consumer of those files is torchgen/gen_backend_stubs.py at runtime in an
installed torchgen, which is not in the libtorch path.

Three things surfaced while reviewing the above and are folded in. pytorch#191625 added
an install rule for the gitignored torch/_rocm_init.py while this was open, in
the same prefix-relative form; it was the last remaining DESTINATION "." in the
tree and is now guarded and addressed like its neighbours. The _C rule's ARCHIVE
clause is dropped: _C is a MODULE library, so no Windows import library is
produced and none ships -- the 2026-08-18 cp312 win_amd64 nightlies carry 13 to
16 .lib files across the cpu, cu126 and xpu variants, every one a genuine C++
import library and no _C.lib among them. It was also the one prefix-relative
destination left in a rule whose other two were absolute.

A lint rule is added so this class of regression is rejected when it is written
rather than after a release. pytorch#191625 is the evidence that it recurs: it landed
three weeks after this fix and nothing caught it, because no CI job installs
standalone and inspects the result -- tools/build_libtorch.py is the only
standalone-install path exercised in CI and it configures with BUILD_PYTHON=OFF,
so it never evaluates these rules. CMAKE_INSTALL_PREFIX_ROOT forbids a
destination of "." across the build system; CMAKE_PLATLIB_DESTINATION
additionally requires every destination in the two package-data files to name
the wheel tree, which catches the prefix-relative variant. Both reuse the
existing grep_linter adapter and are registered in VERY_FAST_LINTERS in
.spin/cmds.py, which spin requires of every linter. This is a text check, not a
behavioural one: a destination that reaches the prefix root through a variable,
or a new rule that uses the wheel tree but omits its SKBUILD guard, still gets
through.

Test Plan:

All on Linux/CPU unless noted, against the reporter's exact configure from the
issue:

```
cmake -S . -B build-standalone -GNinja \
  -DCMAKE_INSTALL_PREFIX=/tmp/prefix \
  -DATEN_NO_TEST=ON -DBUILD_BINARY=ON -DBUILD_CUSTOM_PROTOBUF=OFF \
  -DBUILD_PYTHON=ON -DBUILD_TEST=OFF -DUSE_OPENMP=ON \
  -DCMAKE_INSTALL_LIBDIR=lib -DUSE_DISTRIBUTED=OFF -DUSE_CUDA=OFF

grep -rhoE '\$\{CMAKE_INSTALL_PREFIX\}/[A-Za-z_./]*' \
  --include=cmake_install.cmake build-standalone/ \
  | sed 's|${CMAKE_INSTALL_PREFIX}/||' | cut -d/ -f1 | sort -u
grep -rh --include=cmake_install.cmake 'CMAKE_INSTALL_PREFIX}/\."' build-standalone/
grep -rh --include=cmake_install.cmake "DESTINATION \"$PWD" build-standalone/
grep -rhE --include=cmake_install.cmake 'DESTINATION "(/torch|/torchgen)' build-standalone/
```

Before: `. bin csrc _dynamo _export _inductor include lib share testing utils`,
plus seven rules with destinations inside the source checkout. After:
`bin include lib share`, and the other three greps empty. The last one checks
that an unset SKBUILD_PLATLIB_DIR has not turned a destination into an absolute
path at the filesystem root. The greps must recurse over every
cmake_install.cmake, not just the top-level one, since several of the offending
rules live in subdirectory scopes.

Same configure with -DUSE_CUDA=ON -DUSE_DISTRIBUTED=ON -DUSE_NCCL=ON
-DUSE_NCCL_EP=ON -DUSE_SYSTEM_NCCL=ON: the _nccl_ep target is still built (it
appears in build.ninja, producing torch/_nccl_ep.cpython-310-x86_64-linux-gnu.so)
but contributes no install rule, and the prefix root stays clean.

The guard, exercised the way it can actually regress -- by changing install-dir
in pyproject.toml to something other than "torch" and building a wheel -- fails
configure with the FATAL_ERROR, reporting platlib, the expected prefix and the
actual one.

Wheel contents, via scikit-build-core:

```
pip install -e . -v --no-build-isolation   # twice, consecutively
python -c "import torch, torchgen.packaged, functorch; print(torch.__version__)"
```

Both builds succeed, each configure picking up its own staging directory. The
installed file list under site-packages is identical to a pre-change build of
the same base: 9679 files, none gained or lost, including the mirrored copies
that exist only in the install tree (valgrind.h/callgrind.h, torchgen/packaged
with native_functions.yaml, tags.yaml, 42 templates and 16 autograd files) and
the generated version.py, _C, 43 .pyi stubs and 20 jinja templates. import and
a matmul both work.

The new lint rules, checked in both directions -- clean on this tree, and
flagging the real regressions on the pre-fix tree:

```
spin lint -- --take CMAKE_INSTALL_PREFIX_ROOT,CMAKE_PLATLIB_DESTINATION
```

CMAKE_INSTALL_PREFIX_ROOT flags 6 lines on the pre-fix tree -- torch/CMakeLists.txt
474, 475, 517, 548 and 555, plus cmake/PackageData.cmake:21 -- where 555 is the
pytorch#191625 _rocm_init.py rule, so the check demonstrably rejects the regression that
actually happened. CMAKE_PLATLIB_DESTINATION flags 12.

Lint:

```
lintrunner -a --take CMAKE,NEWLINE,SPACES,TABS,CODESPELL,COPYRIGHT,EXEC \
  CMakeLists.txt cmake/FileMirroring.cmake cmake/PackageData.cmake \
  cmake/Summary.cmake torch/CMakeLists.txt .lintrunner.toml .spin/cmds.py
```

Not covered: Windows and macOS were not exercised locally; ciflow/binaries is
what covers them.

This change was authored with the assistance of an AI coding assistant.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Fixes pytorch#191102

Pull Request resolved: pytorch#191494
Approved by: https://github.com/atalman
@github-actions
github-actions Bot deleted the gh/zklaus/60/head branch September 20, 2026 01:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: mkldnn Related to Intel IDEEP or oneDNN (a.k.a. mkldnn) integration open source Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants