Skip to content

[BE] Build all macOS Python wheels on a single runner - #181171

Closed
malfet wants to merge 10 commits into
gh/malfet/849/basefrom
gh/malfet/849/head
Closed

malfet wants to merge 10 commits into
gh/malfet/849/basefrom
gh/malfet/849/head

Conversation

@malfet

@malfet malfet commented Apr 22, 2026 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

Build all macOS arm64 wheels on a single runner, cutting macos-26-xlarge runner-minutes ~75%. Leverages the cmake.py Python-cache-invalidation fix from #181147 so iterations 2..N in the shared build/ only recompile libtorch_python + _C (~7 min each), not the full ~35 min build.

What changed

  • wheel-build is now a single macos-26-xlarge job. It installs uv via astral-sh/setup-uv@v6, pre-fetches every CPython in DESIRED_PYTHONS in parallel, then runs .ci/wheel/build_all_macos_wheels.sh which loops through the versions and calls .ci/wheel/build_wheel.sh with the right interpreter first on PATH.
  • Delocate moved to its own parallelized step (.ci/wheel/delocate_wheels.sh) that runs delocate-wheel across all wheels via xargs -P $(sysctl -n hw.ncpu). delocate-listdeps (pure diagnostic, was ~11 min/wheel) is dropped.
  • New reusable workflow .github/workflows/_binary-test-macos.yml chains test (macos-26) → upload (ubuntu, via the existing _binary-upload.yml). The main workflow invokes it from a matrix, so each matrix entry spawns an independent test→upload chain — per-Python gating comes for free: test-X failing blocks only upload-X, not the whole fan-out.
  • wheel-test-upload uses a sparse checkout covering only .ci/pytorch/smoke_test/ — no submodules, no wheel-unrelated sources. Checkout clocks in at ~2 seconds per matrix instance.
  • Adding a new CPython version is a one-line edit to FULL_PYTHON_VERSIONS in the generator; DESIRED_PYTHONS env + test-upload matrix regenerate automatically.

Timings

Compared 2026-04-23 nightly on main (run 24824391896) vs. this PR (run 24861993047):

Before (main) After (this PR)
macos-26-xlarge runner-minutes ~427 ~106
macos-26 runner-minutes 0 ~9
Ubuntu runner-minutes (upload) ~36 ~36
Wall-clock ~72 min ~112 min

~75% reduction in macos-26-xlarge usage, at the cost of ~1.5× wall-clock.

Before 7 parallel wheel-build jobs, each running full from-scratch build on macos-26-xlarge took roughly an hour.

After this change, checkout takes ~5 min, install libomp + uv setup + pre-fetch all Pythons: ~2 min, first full python-3.10 build ~35 min, incremental rebuilds for 3.11-... ~7 min each, parallel delocate of all 7 wheels: ~28 min(to be removed), wheel artifact uploads: <20 s

wheel-test-upload matrix (7 instances, parallel): sparse checkout ~2 s, setup-python + smoke test 1–2 min on macos-26, then upload 4–5 min on ubuntu via _binary-upload.yml.

Dependency graph

wheel-build (1 × macos-26-xlarge)
  ├─► wheel-test-upload matrix × 7
  │      └─ test (macos-26) → upload (ubuntu, via _binary-upload.yml)
  └─► libtorch-extract → libtorch-upload

Possible downsides

  • The new wheel-build runs git clean -fxd once at the start, then preserves build/ across iterations. If the [CMake] Make build folder succeed thru CPython version changes #181147 cache-invalidation regresses, we'd silently produce per-Python wheels linked against the wrong CPython ABI. wheel-test-upload smoke-tests on a fresh runner are the safety net.
  • If iteration N fails, subsequent iterations don't build — failure on early 3.10 kills the whole wheel-build job. Acceptable for nightly.
  • Wall-clock ~1.5× longer per nightly. If macOS nightly wall-clock becomes a bottleneck for release cadence, the build loop can be split back into a matrix — at which point the xlarge-minute savings would disappear.

Authored with Claude.

[ghstack-poisoned]
@malfet
malfet requested a review from a team as a code owner April 22, 2026 20:57
@pytorch-bot

pytorch-bot Bot commented Apr 22, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

⏳ 67 Pending, 1 Unrelated Failure

As of commit 2ba615f with merge base 781bb5c (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

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

@pytorch-bot pytorch-bot Bot added the topic: not user facing topic category label Apr 22, 2026
@malfet malfet added the ciflow/binaries_wheel Trigger binary build and upload jobs for wheel on the PR label Apr 22, 2026
[ghstack-poisoned]
malfet added a commit that referenced this pull request Apr 23, 2026
Instead of spawning N parallel matrix jobs that each do a from-scratch
build, run one job that iterates every Python version sequentially and
relies on the shared `build/` to skip libtorch_cpu after the first
iteration. Following the earlier cmake.py fix, each extra Python
version only recompiles libtorch_python + _C (~5 min) on top of the
initial ~80 min full build, cutting total runner-minutes substantially
for nightlies.

Per-iteration isolation: DESIRED_PYTHON is set on each build step's
env, binary_populate_env.sh is re-run to rewrite BINARY_ENV_FILE with
the version-derived fields, and PYTORCH_FINAL_PACKAGE_DIR /
MAC_PACKAGE_WORK_DIR are per-iteration subdirs so delocate's
whl_tmp_dir scan can't see a previous iteration's wheel.

Authored with Claude.

ghstack-source-id: dc0f0e1
Pull-Request: #181171
[ghstack-poisoned]
malfet added a commit that referenced this pull request Apr 23, 2026
Instead of spawning N parallel matrix jobs that each do a from-scratch
build, run one job that iterates every Python version sequentially and
relies on the shared `build/` to skip libtorch_cpu after the first
iteration. Following the earlier cmake.py fix, each extra Python
version only recompiles libtorch_python + _C (~5 min) on top of the
initial ~80 min full build, cutting total runner-minutes substantially
for nightlies.

Per-iteration isolation: DESIRED_PYTHON is set on each build step's
env, binary_populate_env.sh is re-run to rewrite BINARY_ENV_FILE with
the version-derived fields, and PYTORCH_FINAL_PACKAGE_DIR /
MAC_PACKAGE_WORK_DIR are per-iteration subdirs so delocate's
whl_tmp_dir scan can't see a previous iteration's wheel.

Authored with Claude.

ghstack-source-id: 7a1b210
Pull-Request: #181171
[ghstack-poisoned]
malfet added a commit that referenced this pull request Apr 23, 2026
Instead of spawning N parallel matrix jobs that each do a from-scratch
build, run one job that iterates every Python version sequentially and
relies on the shared `build/` to skip libtorch_cpu after the first
iteration. Following the earlier cmake.py fix, each extra Python
version only recompiles libtorch_python + _C (~5 min) on top of the
initial ~80 min full build, cutting total runner-minutes substantially
for nightlies.

Per-iteration isolation: DESIRED_PYTHON is set on each build step's
env, binary_populate_env.sh is re-run to rewrite BINARY_ENV_FILE with
the version-derived fields, and PYTORCH_FINAL_PACKAGE_DIR /
MAC_PACKAGE_WORK_DIR are per-iteration subdirs so delocate's
whl_tmp_dir scan can't see a previous iteration's wheel.

Authored with Claude.

ghstack-source-id: 73bc0fe
Pull-Request: #181171
[ghstack-poisoned]
malfet added a commit that referenced this pull request Apr 23, 2026
Instead of spawning N parallel matrix jobs that each do a from-scratch
build, run one job that iterates every Python version sequentially and
relies on the shared `build/` to skip libtorch_cpu after the first
iteration. Following the earlier cmake.py fix, each extra Python
version only recompiles libtorch_python + _C (~5 min) on top of the
initial ~80 min full build, cutting total runner-minutes substantially
for nightlies.

Per-iteration isolation: DESIRED_PYTHON is set on each build step's
env, binary_populate_env.sh is re-run to rewrite BINARY_ENV_FILE with
the version-derived fields, and PYTORCH_FINAL_PACKAGE_DIR /
MAC_PACKAGE_WORK_DIR are per-iteration subdirs so delocate's
whl_tmp_dir scan can't see a previous iteration's wheel.

Authored with Claude.

ghstack-source-id: bcb2e01
Pull-Request: #181171
[ghstack-poisoned]
malfet added a commit that referenced this pull request Apr 23, 2026
Instead of spawning N parallel matrix jobs that each do a from-scratch
build, run one job that iterates every Python version sequentially and
relies on the shared `build/` to skip libtorch_cpu after the first
iteration. Following the earlier cmake.py fix, each extra Python
version only recompiles libtorch_python + _C (~5 min) on top of the
initial ~80 min full build, cutting total runner-minutes substantially
for nightlies.

Per-iteration isolation: DESIRED_PYTHON is set on each build step's
env, binary_populate_env.sh is re-run to rewrite BINARY_ENV_FILE with
the version-derived fields, and PYTORCH_FINAL_PACKAGE_DIR /
MAC_PACKAGE_WORK_DIR are per-iteration subdirs so delocate's
whl_tmp_dir scan can't see a previous iteration's wheel.

Authored with Claude.

ghstack-source-id: 56726d8
Pull-Request: #181171
[ghstack-poisoned]
malfet added a commit that referenced this pull request Apr 23, 2026
Instead of spawning N parallel matrix jobs that each do a from-scratch
build, run one job that iterates every Python version sequentially and
relies on the shared `build/` to skip libtorch_cpu after the first
iteration. Following the earlier cmake.py fix, each extra Python
version only recompiles libtorch_python + _C (~5 min) on top of the
initial ~80 min full build, cutting total runner-minutes substantially
for nightlies.

Per-iteration isolation: DESIRED_PYTHON is set on each build step's
env, binary_populate_env.sh is re-run to rewrite BINARY_ENV_FILE with
the version-derived fields, and PYTORCH_FINAL_PACKAGE_DIR /
MAC_PACKAGE_WORK_DIR are per-iteration subdirs so delocate's
whl_tmp_dir scan can't see a previous iteration's wheel.

Authored with Claude.

ghstack-source-id: 73271e2
Pull-Request: #181171
[ghstack-poisoned]
malfet added a commit that referenced this pull request Apr 23, 2026
Instead of spawning N parallel matrix jobs that each do a from-scratch
build, run one job that iterates every Python version sequentially and
relies on the shared `build/` to skip libtorch_cpu after the first
iteration. Following the earlier cmake.py fix, each extra Python
version only recompiles libtorch_python + _C (~5 min) on top of the
initial ~80 min full build, cutting total runner-minutes substantially
for nightlies.

Per-iteration isolation: DESIRED_PYTHON is set on each build step's
env, binary_populate_env.sh is re-run to rewrite BINARY_ENV_FILE with
the version-derived fields, and PYTORCH_FINAL_PACKAGE_DIR /
MAC_PACKAGE_WORK_DIR are per-iteration subdirs so delocate's
whl_tmp_dir scan can't see a previous iteration's wheel.

Authored with Claude.

ghstack-source-id: 3cd6d25
Pull-Request: #181171
[ghstack-poisoned]
malfet added a commit that referenced this pull request Apr 23, 2026
Instead of spawning N parallel matrix jobs that each do a from-scratch
build, run one job that iterates every Python version sequentially and
relies on the shared `build/` to skip libtorch_cpu after the first
iteration. Following the earlier cmake.py fix, each extra Python
version only recompiles libtorch_python + _C (~5 min) on top of the
initial ~80 min full build, cutting total runner-minutes substantially
for nightlies.

Per-iteration isolation: DESIRED_PYTHON is set on each build step's
env, binary_populate_env.sh is re-run to rewrite BINARY_ENV_FILE with
the version-derived fields, and PYTORCH_FINAL_PACKAGE_DIR /
MAC_PACKAGE_WORK_DIR are per-iteration subdirs so delocate's
whl_tmp_dir scan can't see a previous iteration's wheel.

Authored with Claude.

ghstack-source-id: 1c9f1d5
Pull-Request: #181171
[ghstack-poisoned]
malfet added a commit that referenced this pull request Apr 23, 2026
Instead of spawning N parallel matrix jobs that each do a from-scratch
build, run one job that iterates every Python version sequentially and
relies on the shared `build/` to skip libtorch_cpu after the first
iteration. Following the earlier cmake.py fix, each extra Python
version only recompiles libtorch_python + _C (~5 min) on top of the
initial ~80 min full build, cutting total runner-minutes substantially
for nightlies.

Per-iteration isolation: DESIRED_PYTHON is set on each build step's
env, binary_populate_env.sh is re-run to rewrite BINARY_ENV_FILE with
the version-derived fields, and PYTORCH_FINAL_PACKAGE_DIR /
MAC_PACKAGE_WORK_DIR are per-iteration subdirs so delocate's
whl_tmp_dir scan can't see a previous iteration's wheel.

Authored with Claude.

ghstack-source-id: 92c0d5c
Pull-Request: #181171
@malfet

malfet commented Apr 24, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge -f "It's alive"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

malfet added a commit that referenced this pull request May 15, 2026
Mirrors #181171 (macOS) for linux-aarch64 CPU: one `linux.arm64.r7g.12xlarge.memory` job loops over all CPython versions in `DESIRED_PYTHONS` and reuses `build/` across iterations. libtorch_cpu (Python-ABI-free) is shared; only libtorch_python + `_C` are recompiled per interpreter, leveraging the cmake.py cache-invalidation fix from #181147.

cuda-aarch64 keeps its existing per-Python matrix; test+upload stay per-Python so failure on one Python only blocks that Python's upload.

Easy to extend later to (gpu_arch_type, desired_cuda) grouping for CUDA-aarch64 and x86 by adding more partitions to the template.

Authored with Claude.


ghstack-source-id: 0c35203
Pull-Request: #183931
malfet added a commit that referenced this pull request May 15, 2026
Mirrors #181171 (macOS) for linux CPU: one job per gpu_arch_type (cpu and cpu-aarch64) loops over all CPython versions in `DESIRED_PYTHONS` and reuses `build/` across iterations. libtorch_cpu (Python-ABI-free) is shared; only libtorch_python + `_C` are recompiled per interpreter, leveraging the cmake.py cache-invalidation fix from #181147.

Template's `unified_arch_types = ['cpu', 'cpu-aarch64']` is the partition switch -- add more entries (e.g. cuda variants) to fold them into the same pattern. CUDA, ROCm, and XPU stay on the per-Python matrix; test+upload stay per-Python so failure on one Python only blocks that Python's upload.

Authored with Claude.

ghstack-source-id: 618c3dd
Pull-Request: #183931
pytorchmergebot pushed a commit that referenced this pull request May 16, 2026
Mirrors #181171 (macOS) for linux CPU: one job per gpu_arch_type (cpu and cpu-aarch64) loops over all CPython versions in `DESIRED_PYTHONS` and reuses `build/` across iterations. libtorch_cpu (Python-ABI-free) is shared; only libtorch_python + `_C` are recompiled per interpreter, leveraging the cmake.py cache-invalidation fix from #181147.

Template's `unified_arch_types = ['cpu', 'cpu-aarch64']` is the partition switch -- add more entries (e.g. cuda variants) to fold them into the same pattern. CUDA, ROCm, and XPU stay on the per-Python matrix; test+upload stay per-Python so failure on one Python only blocks that Python's upload.

Authored with Claude.

## Before vs after

Comparing nightly 2026-05-15 08:28 UTC (per-Python matrix) to PR run at 18:46 UTC (unified build):

| Workflow | Stage | Before | After | Δ |
|---|---|---:|---:|---:|
| linux-aarch64-binary-manywheel | Build runner-minutes (`linux.arm64.r7g.12xlarge.memory`) | **244** (6 × ~40m) | **104** (1 × 104m) | **−57%** |
| linux-aarch64-binary-manywheel | Build wall-clock | ~41m | ~104m | +2.5× |
| linux-binary-manywheel (x86) | Build runner-minutes (`linux.12xlarge.memory.ephemeral`) | **294** (6 × ~49m) | **111** (1 × 111m) | **−62%** |
| linux-binary-manywheel (x86) | Build wall-clock | ~49m | ~111m | +2.2× |

### TTS — full CPU pipeline (build → upload, aarch64)

| Phase | Nightly (before) | PR (after) |
|---|---|---|
| Build starts | 08:30:58 | 18:50:34 |
| Build ends | 09:12:35 (last) | 20:34:47 |
| Test starts | 10:58:31 (queue ≈ 1h46m) | 21:15:20 (queue ≈ 41m) |
| Test ends | 11:06:10 (last) | 21:24:40 |
| Upload ends | 11:08:41 (last) | 21:27:53 |
| **Total TTS** | **~2h37m** | **~2h37m** |

~57–62% runner-minute reduction on the expensive build runners (~140m aarch64 / ~183m x86 saved per nightly). Build wall-clock ~2.2–2.5× longer, but end-to-end TTS unchanged on aarch64 because test queueing dominated under the old layout. x86 test+upload data pending the in-progress PR run.

Data: aarch64 nightly [25908164878](https://github.com/pytorch/pytorch/actions/runs/25908164878), aarch64 PR [25935357837](https://github.com/pytorch/pytorch/actions/runs/25935357837), x86 nightly [25908164859](https://github.com/pytorch/pytorch/actions/runs/25908164859), x86 PR [25935357836](https://github.com/pytorch/pytorch/actions/runs/25935357836).

Pull Request resolved: #183931
Approved by: https://github.com/huydhn
@github-actions
github-actions Bot deleted the gh/malfet/849/head branch May 24, 2026 02:28
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
Build all macOS arm64 wheels on a single runner, cutting `macos-26-xlarge` runner-minutes ~75%. Leverages the `cmake.py` Python-cache-invalidation fix from pytorch#181147 so iterations 2..N in the shared `build/` only recompile `libtorch_python` + `_C` (~7 min each), not the full ~35 min build.

## What changed

- `wheel-build` is now a single `macos-26-xlarge` job. It installs `uv` via `astral-sh/setup-uv@v6`, pre-fetches every CPython in `DESIRED_PYTHONS` in parallel, then runs `.ci/wheel/build_all_macos_wheels.sh` which loops through the versions and calls `.ci/wheel/build_wheel.sh` with the right interpreter first on PATH.
- Delocate moved to its own parallelized step (`.ci/wheel/delocate_wheels.sh`) that runs `delocate-wheel` across all wheels via `xargs -P $(sysctl -n hw.ncpu)`. `delocate-listdeps` (pure diagnostic, was ~11 min/wheel) is dropped.
- New reusable workflow `.github/workflows/_binary-test-macos.yml` chains test (macos-26) → upload (ubuntu, via the existing `_binary-upload.yml`). The main workflow invokes it from a matrix, so each matrix entry spawns an independent test→upload chain — per-Python gating comes for free: test-X failing blocks only upload-X, not the whole fan-out.
- `wheel-test-upload` uses a sparse checkout covering only `.ci/pytorch/smoke_test/` — no submodules, no wheel-unrelated sources. Checkout clocks in at **~2 seconds** per matrix instance.
- Adding a new CPython version is a one-line edit to `FULL_PYTHON_VERSIONS` in the generator; `DESIRED_PYTHONS` env + test-upload matrix regenerate automatically.

## Timings

Compared 2026-04-23 nightly on main (run 24824391896) vs. this PR (run 24861993047):

|                                  | Before (main) | After (this PR) |
|----------------------------------|--------------:|----------------:|
| `macos-26-xlarge` runner-minutes | **~427**      | **~106**        |
| `macos-26` runner-minutes        | 0             | ~9              |
| Ubuntu runner-minutes (upload)   | ~36           | ~36             |
| Wall-clock                       | ~72 min       | ~112 min        |

**~75% reduction in `macos-26-xlarge` usage**, at the cost of ~1.5× wall-clock.

Before 7 parallel `wheel-build` jobs, each running full from-scratch build on `macos-26-xlarge` took roughly an hour.

After this change,  checkout takes ~5 min, install libomp + uv setup + pre-fetch all Pythons: ~2 min, first full python-3.10 build ~35 min, incremental rebuilds for 3.11-... ~7 min each, parallel delocate of all 7 wheels: ~28 min(to be removed), wheel artifact uploads: <20 s

`wheel-test-upload` matrix (7 instances, parallel): sparse checkout ~2 s, setup-python + smoke test 1–2 min on `macos-26`, then upload 4–5 min on ubuntu via `_binary-upload.yml`.

## Dependency graph

```
wheel-build (1 × macos-26-xlarge)
  ├─► wheel-test-upload matrix × 7
  │      └─ test (macos-26) → upload (ubuntu, via _binary-upload.yml)
  └─► libtorch-extract → libtorch-upload
```

## Possible downsides

- The new `wheel-build` runs `git clean -fxd` once at the start, then preserves `build/` across iterations. If the pytorch#181147 cache-invalidation regresses, we'd silently produce per-Python wheels linked against the wrong CPython ABI. `wheel-test-upload` smoke-tests on a fresh runner are the safety net.
- If iteration N fails, subsequent iterations don't build — failure on early 3.10 kills the whole `wheel-build` job. Acceptable for nightly.
- Wall-clock ~1.5× longer per nightly. If macOS nightly wall-clock becomes a bottleneck for release cadence, the build loop can be split back into a matrix — at which point the xlarge-minute savings would disappear.

Authored with Claude.

Pull Request resolved: pytorch#181171
Approved by: https://github.com/seemethere, https://github.com/atalman
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
Build all macOS arm64 wheels on a single runner, cutting `macos-26-xlarge` runner-minutes ~75%. Leverages the `cmake.py` Python-cache-invalidation fix from pytorch#181147 so iterations 2..N in the shared `build/` only recompile `libtorch_python` + `_C` (~7 min each), not the full ~35 min build.

## What changed

- `wheel-build` is now a single `macos-26-xlarge` job. It installs `uv` via `astral-sh/setup-uv@v6`, pre-fetches every CPython in `DESIRED_PYTHONS` in parallel, then runs `.ci/wheel/build_all_macos_wheels.sh` which loops through the versions and calls `.ci/wheel/build_wheel.sh` with the right interpreter first on PATH.
- Delocate moved to its own parallelized step (`.ci/wheel/delocate_wheels.sh`) that runs `delocate-wheel` across all wheels via `xargs -P $(sysctl -n hw.ncpu)`. `delocate-listdeps` (pure diagnostic, was ~11 min/wheel) is dropped.
- New reusable workflow `.github/workflows/_binary-test-macos.yml` chains test (macos-26) → upload (ubuntu, via the existing `_binary-upload.yml`). The main workflow invokes it from a matrix, so each matrix entry spawns an independent test→upload chain — per-Python gating comes for free: test-X failing blocks only upload-X, not the whole fan-out.
- `wheel-test-upload` uses a sparse checkout covering only `.ci/pytorch/smoke_test/` — no submodules, no wheel-unrelated sources. Checkout clocks in at **~2 seconds** per matrix instance.
- Adding a new CPython version is a one-line edit to `FULL_PYTHON_VERSIONS` in the generator; `DESIRED_PYTHONS` env + test-upload matrix regenerate automatically.

## Timings

Compared 2026-04-23 nightly on main (run 24824391896) vs. this PR (run 24861993047):

|                                  | Before (main) | After (this PR) |
|----------------------------------|--------------:|----------------:|
| `macos-26-xlarge` runner-minutes | **~427**      | **~106**        |
| `macos-26` runner-minutes        | 0             | ~9              |
| Ubuntu runner-minutes (upload)   | ~36           | ~36             |
| Wall-clock                       | ~72 min       | ~112 min        |

**~75% reduction in `macos-26-xlarge` usage**, at the cost of ~1.5× wall-clock.

Before 7 parallel `wheel-build` jobs, each running full from-scratch build on `macos-26-xlarge` took roughly an hour.

After this change,  checkout takes ~5 min, install libomp + uv setup + pre-fetch all Pythons: ~2 min, first full python-3.10 build ~35 min, incremental rebuilds for 3.11-... ~7 min each, parallel delocate of all 7 wheels: ~28 min(to be removed), wheel artifact uploads: <20 s

`wheel-test-upload` matrix (7 instances, parallel): sparse checkout ~2 s, setup-python + smoke test 1–2 min on `macos-26`, then upload 4–5 min on ubuntu via `_binary-upload.yml`.

## Dependency graph

```
wheel-build (1 × macos-26-xlarge)
  ├─► wheel-test-upload matrix × 7
  │      └─ test (macos-26) → upload (ubuntu, via _binary-upload.yml)
  └─► libtorch-extract → libtorch-upload
```

## Possible downsides

- The new `wheel-build` runs `git clean -fxd` once at the start, then preserves `build/` across iterations. If the pytorch#181147 cache-invalidation regresses, we'd silently produce per-Python wheels linked against the wrong CPython ABI. `wheel-test-upload` smoke-tests on a fresh runner are the safety net.
- If iteration N fails, subsequent iterations don't build — failure on early 3.10 kills the whole `wheel-build` job. Acceptable for nightly.
- Wall-clock ~1.5× longer per nightly. If macOS nightly wall-clock becomes a bottleneck for release cadence, the build loop can be split back into a matrix — at which point the xlarge-minute savings would disappear.

Authored with Claude.

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

Mirrors pytorch#181171 (macOS) for linux CPU: one job per gpu_arch_type (cpu and cpu-aarch64) loops over all CPython versions in `DESIRED_PYTHONS` and reuses `build/` across iterations. libtorch_cpu (Python-ABI-free) is shared; only libtorch_python + `_C` are recompiled per interpreter, leveraging the cmake.py cache-invalidation fix from pytorch#181147.

Template's `unified_arch_types = ['cpu', 'cpu-aarch64']` is the partition switch -- add more entries (e.g. cuda variants) to fold them into the same pattern. CUDA, ROCm, and XPU stay on the per-Python matrix; test+upload stay per-Python so failure on one Python only blocks that Python's upload.

Authored with Claude.

## Before vs after

Comparing nightly 2026-05-15 08:28 UTC (per-Python matrix) to PR run at 18:46 UTC (unified build):

| Workflow | Stage | Before | After | Δ |
|---|---|---:|---:|---:|
| linux-aarch64-binary-manywheel | Build runner-minutes (`linux.arm64.r7g.12xlarge.memory`) | **244** (6 × ~40m) | **104** (1 × 104m) | **−57%** |
| linux-aarch64-binary-manywheel | Build wall-clock | ~41m | ~104m | +2.5× |
| linux-binary-manywheel (x86) | Build runner-minutes (`linux.12xlarge.memory.ephemeral`) | **294** (6 × ~49m) | **111** (1 × 111m) | **−62%** |
| linux-binary-manywheel (x86) | Build wall-clock | ~49m | ~111m | +2.2× |

### TTS — full CPU pipeline (build → upload, aarch64)

| Phase | Nightly (before) | PR (after) |
|---|---|---|
| Build starts | 08:30:58 | 18:50:34 |
| Build ends | 09:12:35 (last) | 20:34:47 |
| Test starts | 10:58:31 (queue ≈ 1h46m) | 21:15:20 (queue ≈ 41m) |
| Test ends | 11:06:10 (last) | 21:24:40 |
| Upload ends | 11:08:41 (last) | 21:27:53 |
| **Total TTS** | **~2h37m** | **~2h37m** |

~57–62% runner-minute reduction on the expensive build runners (~140m aarch64 / ~183m x86 saved per nightly). Build wall-clock ~2.2–2.5× longer, but end-to-end TTS unchanged on aarch64 because test queueing dominated under the old layout. x86 test+upload data pending the in-progress PR run.

Data: aarch64 nightly [25908164878](https://github.com/pytorch/pytorch/actions/runs/25908164878), aarch64 PR [25935357837](https://github.com/pytorch/pytorch/actions/runs/25935357837), x86 nightly [25908164859](https://github.com/pytorch/pytorch/actions/runs/25908164859), x86 PR [25935357836](https://github.com/pytorch/pytorch/actions/runs/25935357836).

Pull Request resolved: pytorch#183931
Approved by: https://github.com/huydhn
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Build all macOS arm64 wheels on a single runner, cutting `macos-26-xlarge` runner-minutes ~75%. Leverages the `cmake.py` Python-cache-invalidation fix from pytorch#181147 so iterations 2..N in the shared `build/` only recompile `libtorch_python` + `_C` (~7 min each), not the full ~35 min build.

## What changed

- `wheel-build` is now a single `macos-26-xlarge` job. It installs `uv` via `astral-sh/setup-uv@v6`, pre-fetches every CPython in `DESIRED_PYTHONS` in parallel, then runs `.ci/wheel/build_all_macos_wheels.sh` which loops through the versions and calls `.ci/wheel/build_wheel.sh` with the right interpreter first on PATH.
- Delocate moved to its own parallelized step (`.ci/wheel/delocate_wheels.sh`) that runs `delocate-wheel` across all wheels via `xargs -P $(sysctl -n hw.ncpu)`. `delocate-listdeps` (pure diagnostic, was ~11 min/wheel) is dropped.
- New reusable workflow `.github/workflows/_binary-test-macos.yml` chains test (macos-26) → upload (ubuntu, via the existing `_binary-upload.yml`). The main workflow invokes it from a matrix, so each matrix entry spawns an independent test→upload chain — per-Python gating comes for free: test-X failing blocks only upload-X, not the whole fan-out.
- `wheel-test-upload` uses a sparse checkout covering only `.ci/pytorch/smoke_test/` — no submodules, no wheel-unrelated sources. Checkout clocks in at **~2 seconds** per matrix instance.
- Adding a new CPython version is a one-line edit to `FULL_PYTHON_VERSIONS` in the generator; `DESIRED_PYTHONS` env + test-upload matrix regenerate automatically.

## Timings

Compared 2026-04-23 nightly on main (run 24824391896) vs. this PR (run 24861993047):

|                                  | Before (main) | After (this PR) |
|----------------------------------|--------------:|----------------:|
| `macos-26-xlarge` runner-minutes | **~427**      | **~106**        |
| `macos-26` runner-minutes        | 0             | ~9              |
| Ubuntu runner-minutes (upload)   | ~36           | ~36             |
| Wall-clock                       | ~72 min       | ~112 min        |

**~75% reduction in `macos-26-xlarge` usage**, at the cost of ~1.5× wall-clock.

Before 7 parallel `wheel-build` jobs, each running full from-scratch build on `macos-26-xlarge` took roughly an hour.

After this change,  checkout takes ~5 min, install libomp + uv setup + pre-fetch all Pythons: ~2 min, first full python-3.10 build ~35 min, incremental rebuilds for 3.11-... ~7 min each, parallel delocate of all 7 wheels: ~28 min(to be removed), wheel artifact uploads: <20 s

`wheel-test-upload` matrix (7 instances, parallel): sparse checkout ~2 s, setup-python + smoke test 1–2 min on `macos-26`, then upload 4–5 min on ubuntu via `_binary-upload.yml`.

## Dependency graph

```
wheel-build (1 × macos-26-xlarge)
  ├─► wheel-test-upload matrix × 7
  │      └─ test (macos-26) → upload (ubuntu, via _binary-upload.yml)
  └─► libtorch-extract → libtorch-upload
```

## Possible downsides

- The new `wheel-build` runs `git clean -fxd` once at the start, then preserves `build/` across iterations. If the pytorch#181147 cache-invalidation regresses, we'd silently produce per-Python wheels linked against the wrong CPython ABI. `wheel-test-upload` smoke-tests on a fresh runner are the safety net.
- If iteration N fails, subsequent iterations don't build — failure on early 3.10 kills the whole `wheel-build` job. Acceptable for nightly.
- Wall-clock ~1.5× longer per nightly. If macOS nightly wall-clock becomes a bottleneck for release cadence, the build loop can be split back into a matrix — at which point the xlarge-minute savings would disappear.

Authored with Claude.

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

Mirrors pytorch#181171 (macOS) for linux CPU: one job per gpu_arch_type (cpu and cpu-aarch64) loops over all CPython versions in `DESIRED_PYTHONS` and reuses `build/` across iterations. libtorch_cpu (Python-ABI-free) is shared; only libtorch_python + `_C` are recompiled per interpreter, leveraging the cmake.py cache-invalidation fix from pytorch#181147.

Template's `unified_arch_types = ['cpu', 'cpu-aarch64']` is the partition switch -- add more entries (e.g. cuda variants) to fold them into the same pattern. CUDA, ROCm, and XPU stay on the per-Python matrix; test+upload stay per-Python so failure on one Python only blocks that Python's upload.

Authored with Claude.

## Before vs after

Comparing nightly 2026-05-15 08:28 UTC (per-Python matrix) to PR run at 18:46 UTC (unified build):

| Workflow | Stage | Before | After | Δ |
|---|---|---:|---:|---:|
| linux-aarch64-binary-manywheel | Build runner-minutes (`linux.arm64.r7g.12xlarge.memory`) | **244** (6 × ~40m) | **104** (1 × 104m) | **−57%** |
| linux-aarch64-binary-manywheel | Build wall-clock | ~41m | ~104m | +2.5× |
| linux-binary-manywheel (x86) | Build runner-minutes (`linux.12xlarge.memory.ephemeral`) | **294** (6 × ~49m) | **111** (1 × 111m) | **−62%** |
| linux-binary-manywheel (x86) | Build wall-clock | ~49m | ~111m | +2.2× |

### TTS — full CPU pipeline (build → upload, aarch64)

| Phase | Nightly (before) | PR (after) |
|---|---|---|
| Build starts | 08:30:58 | 18:50:34 |
| Build ends | 09:12:35 (last) | 20:34:47 |
| Test starts | 10:58:31 (queue ≈ 1h46m) | 21:15:20 (queue ≈ 41m) |
| Test ends | 11:06:10 (last) | 21:24:40 |
| Upload ends | 11:08:41 (last) | 21:27:53 |
| **Total TTS** | **~2h37m** | **~2h37m** |

~57–62% runner-minute reduction on the expensive build runners (~140m aarch64 / ~183m x86 saved per nightly). Build wall-clock ~2.2–2.5× longer, but end-to-end TTS unchanged on aarch64 because test queueing dominated under the old layout. x86 test+upload data pending the in-progress PR run.

Data: aarch64 nightly [25908164878](https://github.com/pytorch/pytorch/actions/runs/25908164878), aarch64 PR [25935357837](https://github.com/pytorch/pytorch/actions/runs/25935357837), x86 nightly [25908164859](https://github.com/pytorch/pytorch/actions/runs/25908164859), x86 PR [25935357836](https://github.com/pytorch/pytorch/actions/runs/25935357836).

Pull Request resolved: pytorch#183931
Approved by: https://github.com/huydhn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/binaries_wheel Trigger binary build and upload jobs for wheel on the PR Merged topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants