Skip to content

Support rebuild-on-import for scikit-build-core editable installs - #180249

Closed
zklaus wants to merge 66 commits into
gh/zklaus/55/basefrom
gh/zklaus/55/head
Closed

zklaus wants to merge 66 commits into
gh/zklaus/55/basefrom
gh/zklaus/55/head

Conversation

@zklaus

@zklaus zklaus commented Apr 13, 2026 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

scikit-build-core's editable install can optionally rebuild the project
on the first import torch in a process, so source edits are picked up
without an explicit reinstall. It is off by default (editable.rebuild = false, set in #180247); developers opt in with
SKBUILD_EDITABLE_REBUILD=true.

_patch_skbuild_editable_rebuild() makes that opt-in path usable. It
wraps the ScikitBuildRedirectingFinder's rebuild() to: (1) restore
the real stderr so a pytest --capture=sys run does not crash the
subprocess.run(stdout=sys.stderr) call; (2) propagate
SKBUILD_EDITABLE_SKIP into the current process so subsequent wheel-file
imports do not each trigger a fresh rebuild cascade; and (3) alias
MAX_JOBS to CMAKE_BUILD_PARALLEL_LEVEL so the import-time rebuild
honors PyTorch's build-parallelism knob.

The editable artifact-location shims that make a plain (non-rebuilding)
editable install importable live in the base migration commit (#180247);
this commit only adds the opt-in rebuild-on-import convenience on top.

Rebuild-on-import log (opt-in editable.rebuild = true)

When rebuild-on-import is opted in (SKBUILD_EDITABLE_REBUILD=true), the
redirect finder runs cmake --build & --install on the first import torch in a process, so edits are picked up without an explicit
reinstall. Captured on a CUDA box (scikit-build-core 0.12.x, CUDA 12.9).

Steady state (no source change): a near no-op -- only
version-file/compile-commands regen, no compilation.

$ SKBUILD_EDITABLE_VERBOSE=1 python -c "import torch; print(torch.__version__)"
[0/2] Re-checking globbed directories...
[1/3] Merging compile_commands.json...
[2/3] Regenerating version file...
-- Install configuration: "Release"
Running cmake --build & --install in /path/to/pytorch/build
2.14.0a0+git2a4e636

After editing one source file (c10/core/AllocatorConfig.cpp): the
finder recompiles the changed TU and relinks the dependent libraries
before import proceeds.

$ SKBUILD_EDITABLE_VERBOSE=1 python -c "import torch; print(torch.__version__)"
[0/2] Re-checking globbed directories...
[1/15] Building CXX object c10/CMakeFiles/c10.dir/core/AllocatorConfig.cpp.o
[2/15] Merging compile_commands.json...
[3/15] Linking CXX shared library lib/libc10.so
[4/15] Regenerating version file...
[5/15] Linking CXX shared library lib/libc10_cuda.so
[6/15] Linking CXX shared library lib/libtorch_cpu.so
[7/15] Linking CXX shared library lib/libshm.so
[8/15] Linking CXX executable bin/torch_shm_manager
[9/15] Linking CXX shared library lib/libtorch_cuda.so
[10/15] Linking CXX shared library lib/libtorch.so
[11/15] Linking CXX shared library lib/libtorch_cuda_linalg.so
[12/15] Linking CXX shared library lib/libtorch_python.so
[13/15] Linking C shared module torch/_C.cpython-310-x86_64-linux-gnu.so
[14/15] Linking CXX shared library lib/libnnapi_backend.so
-- Install configuration: "Release"
-- Set non-toolchain portion of runtime path of ".../torch/lib/libc10.so" to "$ORIGIN"
   (... rpath fixups for each installed library ...)
Running cmake --build & --install in /path/to/pytorch/build
2.14.0a0+git2a4e636

(Linker Unused direct dependencies warnings elided for brevity.)

Test plan

  • No-op unless opted in: _patch_skbuild_editable_rebuild() only
    acts when a ScikitBuildRedirectingFinder is on sys.meta_path (an
    editable install) that exposes a rebuild; a wheel install or a
    non-rebuild editable install is unaffected. Confirmed by green wheel CI.
  • Editable rebuild smoke test (opt-in): install with
    SKBUILD_EDITABLE_REBUILD=true, then edit a cmake-generated file's
    source and re-import torch -- exactly one rebuild runs, with no
    pytest --capture=sys stderr crash. Exercised locally / by the macOS
    job (ciflow/mps); not part of default PR CI.

Co-Generated with Claude Code

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Apr 13, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 6 New Failures, 31 Unclassified Failures

As of commit 06d1df4 with merge base 3043bfb (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:

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

@pytorch-bot

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

zklaus added a commit to zklaus/pytorch that referenced this pull request Apr 13, 2026
Add compatibility patches to torch/__init__.py and _utils_internal.py
so that editable installs (redirect mode) can locate CMake-built
artifacts alongside source-tree Python files.

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


ghstack-source-id: 07bd4ef
Pull-Request: pytorch#180249
@zklaus
zklaus marked this pull request as ready for review April 14, 2026 18:33
@zklaus zklaus added ciflow/binaries Trigger all binary build and upload jobs on the PR release notes: releng release notes category labels Apr 14, 2026
zklaus added a commit to zklaus/pytorch that referenced this pull request Apr 15, 2026
Add compatibility patches to torch/__init__.py and _utils_internal.py
so that editable installs (redirect mode) can locate CMake-built
artifacts alongside source-tree Python files.

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


ghstack-source-id: 07bd4ef
Pull-Request: pytorch#180249
zklaus added a commit to zklaus/pytorch that referenced this pull request Apr 17, 2026
Add compatibility patches to torch/__init__.py and _utils_internal.py
so that editable installs (redirect mode) can locate CMake-built
artifacts alongside source-tree Python files.

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


ghstack-source-id: 07bd4ef
Pull-Request: pytorch#180249
zklaus added 2 commits April 20, 2026 13:41
[ghstack-poisoned]
[ghstack-poisoned]
zklaus added 14 commits May 24, 2026 13:16
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
Comment thread pyproject.toml Resolved
Comment thread pyproject.toml Outdated
# rebuild-on-import here no longer re-runs cmake/ninja on every import.
mode = "redirect"
rebuild = false
rebuild = true

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.

How widely adopted is this flag for users of scikit-build-core?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@henryiii, could you shed some light on this?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's been problematic in the past. I think it's better now in the (unreleased) 1.0, though if you don't set a rebuild-dir (new setting in 1.0), then it's still on the package to ensure no absolute paths are referenced, those break because you need a reconfigure (which we don't provide), the dir gets moved after building it in editable installs.

(also new in 1.0 will be <pkg>.__loader__.rebuild(), manual rebuilds)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also, since mid last year I can't process all the pyproject.toml's on PyPI anymore, the project I was using was abandoned (site is now a spam site). So I can't track usage like I used to be able to.

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.

Ok, let's go with disabled by default for now and properly document how to enable this in our CONTRIBUTING.md

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Sounds good -- that's where the stack landed. As of the latest push, editable.rebuild = false is the default, so a plain editable install does no rebuild-on-import. I've documented how to opt in in CONTRIBUTING.md: SKBUILD_EDITABLE_REBUILD=true spin develop (or -C editable.rebuild=true with a raw pip install), plus the SKBUILD_EDITABLE_VERBOSE knob. Thanks both!

zklaus added 4 commits June 26, 2026 13:03
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
zklaus added a commit that referenced this pull request Jul 2, 2026
Add compatibility patches to torch/__init__.py and _utils_internal.py
so that editable installs (redirect mode) can locate CMake-built
artifacts alongside source-tree Python files.

Rebuild-on-import stays off by default (`editable.rebuild = false` in
#180247); the rebuild-once guard here only makes that path sane for
developers who opt in (`SKBUILD_EDITABLE_REBUILD=true`).

## Rebuild-on-import log (opt-in `editable.rebuild = true`)

When rebuild-on-import is opted in (`SKBUILD_EDITABLE_REBUILD=true`), the
redirect finder runs `cmake --build & --install` on the first `import
torch` in a process, so edits are picked up without an explicit
reinstall. Captured on a CUDA box (scikit-build-core 0.12.x, CUDA 12.9).

**Steady state (no source change):** a near no-op -- only
version-file/compile-commands regen, no compilation.

```
$ SKBUILD_EDITABLE_VERBOSE=1 python -c "import torch; print(torch.__version__)"
[0/2] Re-checking globbed directories...
[1/3] Merging compile_commands.json...
[2/3] Regenerating version file...
-- Install configuration: "Release"
Running cmake --build & --install in /path/to/pytorch/build
2.14.0a0+git2a4e636
```

**After editing one source file (`c10/core/AllocatorConfig.cpp`):** the
finder recompiles the changed TU and relinks the dependent libraries
before import proceeds.

```
$ SKBUILD_EDITABLE_VERBOSE=1 python -c "import torch; print(torch.__version__)"
[0/2] Re-checking globbed directories...
[1/15] Building CXX object c10/CMakeFiles/c10.dir/core/AllocatorConfig.cpp.o
[2/15] Merging compile_commands.json...
[3/15] Linking CXX shared library lib/libc10.so
[4/15] Regenerating version file...
[5/15] Linking CXX shared library lib/libc10_cuda.so
[6/15] Linking CXX shared library lib/libtorch_cpu.so
[7/15] Linking CXX shared library lib/libshm.so
[8/15] Linking CXX executable bin/torch_shm_manager
[9/15] Linking CXX shared library lib/libtorch_cuda.so
[10/15] Linking CXX shared library lib/libtorch.so
[11/15] Linking CXX shared library lib/libtorch_cuda_linalg.so
[12/15] Linking CXX shared library lib/libtorch_python.so
[13/15] Linking C shared module torch/_C.cpython-310-x86_64-linux-gnu.so
[14/15] Linking CXX shared library lib/libnnapi_backend.so
-- Install configuration: "Release"
-- Set non-toolchain portion of runtime path of ".../torch/lib/libc10.so" to "$ORIGIN"
   (... rpath fixups for each installed library ...)
Running cmake --build & --install in /path/to/pytorch/build
2.14.0a0+git2a4e636
```

(Linker `Unused direct dependencies` warnings elided for brevity.)

## Test plan

- [x] **No-op for wheel installs**: the four redirect-mode shims
  (rebuild patch, `_load_global_deps` fallback, `__path__` extension,
  `_utils_internal` fallback) are guarded on the
  `ScikitBuildRedirectingFinder` meta-path type and on the source-tree
  paths being absent, so they short-circuit in non-editable installs.
  Confirmed by green wheel CI.
- [ ] **Editable smoke test** (redirect mode): `pip install -e . -v
  --no-build-isolation` then `import torch` resolves native-lib +
  `torch._C` + `bin/`. With rebuild-on-import opted in
  (`SKBUILD_EDITABLE_REBUILD=true`), touching a cmake-generated file's
  source and re-importing triggers a single rebuild with no
  `pytest --capture=sys` stderr crash. Exercised by the macOS test job
  (`ciflow/mps`) or locally -- not part of default PR CI.

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

ghstack-source-id: 0e51d64
Pull-Request: #180249
[ghstack-poisoned]
zklaus added a commit that referenced this pull request Jul 3, 2026
Add compatibility patches to torch/__init__.py and _utils_internal.py
so that editable installs (redirect mode) can locate CMake-built
artifacts alongside source-tree Python files.

Rebuild-on-import stays off by default (`editable.rebuild = false` in
#180247); the rebuild-once guard here only makes that path sane for
developers who opt in (`SKBUILD_EDITABLE_REBUILD=true`).

## Rebuild-on-import log (opt-in `editable.rebuild = true`)

When rebuild-on-import is opted in (`SKBUILD_EDITABLE_REBUILD=true`), the
redirect finder runs `cmake --build & --install` on the first `import
torch` in a process, so edits are picked up without an explicit
reinstall. Captured on a CUDA box (scikit-build-core 0.12.x, CUDA 12.9).

**Steady state (no source change):** a near no-op -- only
version-file/compile-commands regen, no compilation.

```
$ SKBUILD_EDITABLE_VERBOSE=1 python -c "import torch; print(torch.__version__)"
[0/2] Re-checking globbed directories...
[1/3] Merging compile_commands.json...
[2/3] Regenerating version file...
-- Install configuration: "Release"
Running cmake --build & --install in /path/to/pytorch/build
2.14.0a0+git2a4e636
```

**After editing one source file (`c10/core/AllocatorConfig.cpp`):** the
finder recompiles the changed TU and relinks the dependent libraries
before import proceeds.

```
$ SKBUILD_EDITABLE_VERBOSE=1 python -c "import torch; print(torch.__version__)"
[0/2] Re-checking globbed directories...
[1/15] Building CXX object c10/CMakeFiles/c10.dir/core/AllocatorConfig.cpp.o
[2/15] Merging compile_commands.json...
[3/15] Linking CXX shared library lib/libc10.so
[4/15] Regenerating version file...
[5/15] Linking CXX shared library lib/libc10_cuda.so
[6/15] Linking CXX shared library lib/libtorch_cpu.so
[7/15] Linking CXX shared library lib/libshm.so
[8/15] Linking CXX executable bin/torch_shm_manager
[9/15] Linking CXX shared library lib/libtorch_cuda.so
[10/15] Linking CXX shared library lib/libtorch.so
[11/15] Linking CXX shared library lib/libtorch_cuda_linalg.so
[12/15] Linking CXX shared library lib/libtorch_python.so
[13/15] Linking C shared module torch/_C.cpython-310-x86_64-linux-gnu.so
[14/15] Linking CXX shared library lib/libnnapi_backend.so
-- Install configuration: "Release"
-- Set non-toolchain portion of runtime path of ".../torch/lib/libc10.so" to "$ORIGIN"
   (... rpath fixups for each installed library ...)
Running cmake --build & --install in /path/to/pytorch/build
2.14.0a0+git2a4e636
```

(Linker `Unused direct dependencies` warnings elided for brevity.)

## Test plan

- [x] **No-op for wheel installs**: the four redirect-mode shims
  (rebuild patch, `_load_global_deps` fallback, `__path__` extension,
  `_utils_internal` fallback) are guarded on the
  `ScikitBuildRedirectingFinder` meta-path type and on the source-tree
  paths being absent, so they short-circuit in non-editable installs.
  Confirmed by green wheel CI.
- [ ] **Editable smoke test** (redirect mode): `pip install -e . -v
  --no-build-isolation` then `import torch` resolves native-lib +
  `torch._C` + `bin/`. With rebuild-on-import opted in
  (`SKBUILD_EDITABLE_REBUILD=true`), touching a cmake-generated file's
  source and re-importing triggers a single rebuild with no
  `pytest --capture=sys` stderr crash. Exercised by the macOS test job
  (`ciflow/mps`) or locally -- not part of default PR CI.

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

ghstack-source-id: 9340421
Pull-Request: #180249
@zklaus
zklaus requested a review from albanD July 3, 2026 09:44
[ghstack-poisoned]
zklaus added a commit that referenced this pull request Jul 6, 2026
scikit-build-core's editable install can optionally rebuild the project
on the first `import torch` in a process, so source edits are picked up
without an explicit reinstall. It is off by default (`editable.rebuild =
false`, set in #180247); developers opt in with
`SKBUILD_EDITABLE_REBUILD=true`.

`_patch_skbuild_editable_rebuild()` makes that opt-in path usable. It
wraps the `ScikitBuildRedirectingFinder`'s `rebuild()` to: (1) restore
the real stderr so a `pytest --capture=sys` run does not crash the
`subprocess.run(stdout=sys.stderr)` call; (2) propagate
`SKBUILD_EDITABLE_SKIP` into the current process so subsequent wheel-file
imports do not each trigger a fresh rebuild cascade; and (3) alias
`MAX_JOBS` to `CMAKE_BUILD_PARALLEL_LEVEL` so the import-time rebuild
honors PyTorch's build-parallelism knob.

The editable artifact-location shims that make a plain (non-rebuilding)
editable install importable live in the base migration commit (#180247);
this commit only adds the opt-in rebuild-on-import convenience on top.

## Rebuild-on-import log (opt-in `editable.rebuild = true`)

When rebuild-on-import is opted in (`SKBUILD_EDITABLE_REBUILD=true`), the
redirect finder runs `cmake --build & --install` on the first `import
torch` in a process, so edits are picked up without an explicit
reinstall. Captured on a CUDA box (scikit-build-core 0.12.x, CUDA 12.9).

**Steady state (no source change):** a near no-op -- only
version-file/compile-commands regen, no compilation.

```
$ SKBUILD_EDITABLE_VERBOSE=1 python -c "import torch; print(torch.__version__)"
[0/2] Re-checking globbed directories...
[1/3] Merging compile_commands.json...
[2/3] Regenerating version file...
-- Install configuration: "Release"
Running cmake --build & --install in /path/to/pytorch/build
2.14.0a0+git2a4e636
```

**After editing one source file (`c10/core/AllocatorConfig.cpp`):** the
finder recompiles the changed TU and relinks the dependent libraries
before import proceeds.

```
$ SKBUILD_EDITABLE_VERBOSE=1 python -c "import torch; print(torch.__version__)"
[0/2] Re-checking globbed directories...
[1/15] Building CXX object c10/CMakeFiles/c10.dir/core/AllocatorConfig.cpp.o
[2/15] Merging compile_commands.json...
[3/15] Linking CXX shared library lib/libc10.so
[4/15] Regenerating version file...
[5/15] Linking CXX shared library lib/libc10_cuda.so
[6/15] Linking CXX shared library lib/libtorch_cpu.so
[7/15] Linking CXX shared library lib/libshm.so
[8/15] Linking CXX executable bin/torch_shm_manager
[9/15] Linking CXX shared library lib/libtorch_cuda.so
[10/15] Linking CXX shared library lib/libtorch.so
[11/15] Linking CXX shared library lib/libtorch_cuda_linalg.so
[12/15] Linking CXX shared library lib/libtorch_python.so
[13/15] Linking C shared module torch/_C.cpython-310-x86_64-linux-gnu.so
[14/15] Linking CXX shared library lib/libnnapi_backend.so
-- Install configuration: "Release"
-- Set non-toolchain portion of runtime path of ".../torch/lib/libc10.so" to "$ORIGIN"
   (... rpath fixups for each installed library ...)
Running cmake --build & --install in /path/to/pytorch/build
2.14.0a0+git2a4e636
```

(Linker `Unused direct dependencies` warnings elided for brevity.)

## Test plan

- [x] **No-op unless opted in**: `_patch_skbuild_editable_rebuild()` only
  acts when a `ScikitBuildRedirectingFinder` is on `sys.meta_path` (an
  editable install) that exposes a `rebuild`; a wheel install or a
  non-rebuild editable install is unaffected. Confirmed by green wheel CI.
- [ ] **Editable rebuild smoke test** (opt-in): install with
  `SKBUILD_EDITABLE_REBUILD=true`, then edit a cmake-generated file's
  source and re-import `torch` -- exactly one rebuild runs, with no
  `pytest --capture=sys` stderr crash. Exercised locally / by the macOS
  job (`ciflow/mps`); not part of default PR CI.

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

ghstack-source-id: dc7c2c1
Pull-Request: #180249
@zklaus zklaus changed the title Add editable install support for scikit-build-core Support rebuild-on-import for scikit-build-core editable installs Jul 6, 2026
@zklaus

zklaus commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator Author

With scikit-build-core-1.0.0 this is now obsolete, so closing in favor of the single migration in #180247.

@zklaus zklaus closed this Jul 7, 2026
@github-actions
github-actions Bot deleted the gh/zklaus/55/head branch August 7, 2026 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/binaries Trigger all binary build and upload jobs on the PR open source release notes: releng release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants