Skip to content

Studio: keep an Apple Silicon install's torch through studio update - #18659

Merged
danielhanchen merged 1 commit into
mainfrom
studio-update-freeze-mac-torch
Nov 20, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
studio-update-freeze-mac-torch

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

unsloth studio update re-resolves unsloth and unsloth-zoo with dependencies. On Linux and Windows x64, _FreezeNewTorchForCoreUpdate pins the resident torch, torchvision and torchaudio through UV_OVERRIDE while that happens, but only for torch >= 2.13. macOS had no freeze.

Apple Silicon is moving to torch 2.13 for new installs only (#18428). Existing installs keep their torch unless the user runs a manual repair, which sets UNSLOTH_TORCH_UPGRADE=1 through install.sh. Without a freeze, a released unsloth with a torch ceiling (2026.9.11 declared torch<2.13.0) could move a 2.13 Mac install back to 2.12 on studio update. A torch floor could also move a 2.11 install up.

Change

  • studio/install_python_stack.py: on Apple Silicon (darwin + arm64), _FreezeNewTorchForCoreUpdate now freezes every resident plain X.Y.Z torch release, not only >= 2.13. install.sh already freezes the trio this way for every with-deps unsloth install, on every OS. The bundled overrides-darwin-arm64.txt is folded into the frozen file, minus any torch lines, so the MLX transformers / anyio / huggingface_hub overrides still apply.
  • Linux, Windows x64, Windows on ARM and Intel macOS behave exactly as before. The predicate for those hosts is unchanged, and an absent or non-release torch still returns early.
  • While the freeze is active, a uv failure on Apple Silicon now stops instead of falling back to pip. pip ignores UV_OVERRIDE and would honour the released cap. This is the same behaviour Linux already has under the freeze.

Tests

New file: studio/backend/tests/test_macos_core_update_torch_freeze.py, CPU only.

  • Apple Silicon at 2.11.0 / 2.12.1 / 2.13.0 / 2.14.0: the trio is pinned, the MLX overrides are folded in, and UV_OVERRIDE is restored afterwards.
  • An unset UV_OVERRIDE is restored to unset.
  • A uv failure under the freeze never runs pip.
  • These hosts are not frozen: Intel macOS, Apple Silicon without a release torch, Linux aarch64 at 2.12.1, Linux arm64 at 2.11.0, Windows arm64 at 2.12.1.

Base (main) vs head: 6 tests fail on main and pass on the branch. The 6 host-unchanged cases pass on both. No head test fails.

Across all 75 test files that import install_python_stack (8.5k tests), the head's failing test IDs are a subset of main's. The failures are pre-existing and come from this host's environment. They are identical apart from one ROCm spoof test that failed only on the main arm.

Not covered

  • No real Apple Silicon run. The uv override semantics are the same ones the Linux freeze and install.sh rely on, but they were not exercised on a Mac here.

… core package resolve

The core unsloth / unsloth-zoo update re-resolves with deps, and the UV_OVERRIDE
freeze that keeps the resident torch trio was scoped to Linux and Windows x64 with
torch >= 2.13. Apple Silicon now gets torch 2.13 only on a new install or a manual
repair (UNSLOTH_TORCH_UPGRADE=1 through install.sh), so the update freezes every
resident release there, the same way install.sh freezes the trio for every with-deps
unsloth install. Linux, Windows and Intel macOS resolve exactly as before.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T18:58:17.202019Z ac4d720 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: ac4d72086d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit b7dfb9c into main Nov 20, 2026
@danielhanchen
danielhanchen deleted the studio-update-freeze-mac-torch branch November 20, 2026 19:59
danielhanchen added a commit that referenced this pull request Nov 26, 2026
…19772)

## Problem

On Apple Silicon macOS runners,
`studio/backend/tests/test_cu130_repair_torch_specs.py::test_core_update_freezes_only_torch_past_the_released_ceiling[darwin-2.13.0-False]`
fails. For example, the macos-26 leg of the cross-platform staging run
for #19762:

```
    assert mod._TORCH_FREEZE_ACTIVE is pinned
E   AssertionError: assert True is False
```

The test passes on Linux, which is why the normal CI shards never show
it.

## Cause

- #18659 made `_FreezeNewTorchForCoreUpdate` keep an Apple Silicon
install's torch at any release: `sys.platform == "darwin" and
platform.machine() == "arm64"`.
- This test fakes `sys.platform` per row but read the real
`platform.machine()`. Its darwin row expects no freeze, so the row
really means Intel macOS.
- On an x64 Linux runner the machine is `x86_64` and the row passes. On
an arm64 macOS runner it is `arm64`, the freeze engages, and the row
fails.

The code is correct, and Apple Silicon's freeze is already covered by
`test_macos_core_update_torch_freeze.py`. The test is host-dependent.

## Fix

Pin `platform.machine()` to `x86_64` in this test, so every row
describes an x64 host and the darwin row is Intel macOS on any runner.
No product code changes.

## Verification

To reproduce an Apple Silicon host on Linux, a small plugin sets
`platform.machine = lambda: "arm64"` before the session:

| Arm | Simulated arm64 host | Native x86_64 host |
|---|---|---|
| main 6a3a230 | `1 failed, 57 passed`: the same
`darwin-2.13.0-False` row as the macos-26 job | `58 passed` |
| this branch | `58 passed` | `58 passed` |

Sabotage: making the freeze apply to every macOS machine (dropping the
`platform.machine() == "arm64"` check in
`studio/install_python_stack.py`) fails the darwin row on this branch.
The row therefore still guards that Intel macOS is not frozen.

#19762 edits the same parametrize list. Its own macOS failure is this
one, so it will pass once this lands.
danielhanchen added a commit that referenced this pull request Nov 27, 2026
… fused-MPS-attention release on re-run (#20038)

## Problem

New Apple Silicon homes get torch 2.13 (#18428), because 2.13 is the
first release with the fused MPS prefill attention kernel. Studio only
enables that kernel on torch 2.13 or newer (`mps_prefill_supported` in
`diffusion_attention.py`). An existing home keeps whatever torch it has,
from 2.4 up to 2.14, unless the user sets `UNSLOTH_TORCH_UPGRADE=1`. So
every Mac that was installed before #18428 keeps its old torch and runs
diffusion on the math-only attention path.

On a MacBook Pro M5 Max with the same Studio backend and the same
prompts, warm time per 1024x1024 image was:

| Pipeline | torch 2.10.0 (that Mac's existing home) | torch 2.14.1 |
|---|---|---|
| FLUX.2 klein, bf16 | 8.60 s | 3.69 s |
| FLUX.2 klein, GGUF | 11.30 s | 6.39 s |
| Z-Image, GGUF | 64.37 s | 18.00 s |

Peak memory was also 3 to 5 GB lower on the newer torch. A large-linear
check showed no corruption on either version. The images are not
pixel-identical across versions, because the MPS random generator stream
changed between those releases. Within a single version, repeated runs
are bit-identical.

## Change

The change is in `install.sh`, on the macOS arm64 route only. On a
re-run, an existing home whose resident torch is a plain release below
`_MACOS_TORCH_MOVE_FLOOR` (2.13.0, the release Studio's fused MPS
attention gate needs) now takes the same `torch>=2.13.0,<2.14.0` /
`torchvision>=0.28.0,<0.29.0` constraint that a new home gets.
torchaudio resolves to 2.11.x, as it does on a new home. This happens
only when every gate of the new-install route already holds:

- the package is `unsloth`;
- the index is the default cpu index (an index set with
`UNSLOTH_TORCH_INDEX_URL` or `UNSLOTH_TORCH_INDEX_FAMILY` never moves);
- macOS 14 or later, with a GIL-enabled Python 3.10 to 3.14;
- the released `unsloth` on PyPI admits torch 2.13. An unreachable or
capped PyPI keeps the resident release.

These cases are unchanged:

- A home already on 2.13 or 2.14 is kept.
- A home whose torch cannot be read, or holds a nightly, takes the same
path as before.
- `UNSLOTH_TORCH_UPGRADE=0` (any value other than empty or `1`) keeps
the resident release.
- `UNSLOTH_TORCH_UPGRADE=1` behaves as before.

The move floor is its own literal, not derived from
`_MACOS_NEW_INSTALL_TORCH`. A later bump of the new-install pin (for
example to 2.14) therefore never moves a 2.13 home. Homes below 2.13
move to whatever the pin is at that point.

The installer prints `existing install has torch X -- moving to
torch>=2.13.0,<2.14.0 for the fused MPS attention kernel (set
UNSLOTH_TORCH_UPGRADE=0 to keep it)`.

This follows the rule the installer already uses for a raised floor
(#7250): a release below a route's floor is outside the preservation
window, so it moves. On ROCm 7.2 and Strix, that floor is 2.11. Here it
is 2.13.

Linux, WSL, Windows (`install.ps1` is untouched), Intel macOS, CUDA,
ROCm and XPU take exactly the same decisions as before. The new code
runs only inside the macOS arm64 branch, and the shared pin block skips
`_previous_torch_pin` only when that branch has decided to move.

### Why existing homes were preserved, and why this is safe on Apple
Silicon

- #6959 / #7250: a re-run should keep the torch release a user has
validated. The trigger was a silent 2.10 to 2.11 move on a CUDA host.
That still holds at or above 2.13 here.
- #12150: on CUDA, the torch release is tied to prebuilt flash-attn,
causal-conv1d and mamba-ssm wheels. `studio update` can also re-resolve
torch under an older release's cap. Apple Silicon has no such prebuilt
kernels. The PyPI gate is kept, and #18659 freezes the resident torch
through `studio update` on Apple Silicon, so a moved home stays on 2.13
there.
- torchcodec is chosen from the resident torch on every
`install_python_stack.py` pass, so it follows the move.
- A re-run rebuilds the venv anyway. A failed install rolls back to the
previous environment, unless `--no-rollback` was passed.

## Tests

- `tests/sh/test_macos_torch213_route.sh`: 132 checks. 22 fail on main.
3 more fail on a version of this branch that derived the floor from the
pin: the floor literal, and a pin bumped to 2.14 still keeping 2.13.0
and 2.13.1 homes.
- Existing 2.6 / 2.10 / 2.11 / 2.12.1 homes, and 2.10.0+cpu, move to the
2.13 constraint and announce the opt-out.
- These homes are kept: 2.13.0 / 2.13.1 / 2.14.0 / 2.14.1;
`UNSLOTH_TORCH_UPGRADE=0|false|no`; a pinned index; macOS 13;
free-threaded Python; Python 3.9; another `--package`; an unreachable or
capped PyPI.
- With the pin bumped to 2.14 in the test: 2.13.0 and 2.13.1 homes are
kept, and a 2.10 home moves to the bumped pin.
- These are unchanged: a nightly, a release under the window floor,
`--no-torch`, Intel macOS, and Linux / WSL / Windows on the cpu and
cu128 indexes.
- Unchanged and passing: `test_cu130_torch213_route.sh` (104),
`test_previous_torch_pin.sh` (50), `test_get_torch_index_url.sh` (109),
`test_torch_constraint.sh`, and
`tests/python/test_macos_torch213_pins.py`.
- Whole `tests/sh` suite: all 101 scripts pass on this branch, and all
of them pass on the merge base too (the base runs its own copy of the
route test).
- Workflow lints: `scripts/lint_workflow_triggers.py` and actionlint
pass. 58 test files read the workflow files: 3655 passed. The 2 failures
(`test_cached_paths_hold_no_credentials`, `test_version_compat_bundle`
daily sweep) fail the same way on the merge base, because of this host's
environment.

## Upgrade run on macOS 15

`.github/workflows/studio-mac-torch-floor-upgrade.yml` (helper
`.github/scripts/mac-torch-floor-upgrade.sh`) runs on macos-15 with
Python 3.13 and 3.10. It starts a home with the merge base installer and
moves it to torch 2.10.0 / torchvision 0.25.0 / torchaudio 2.10.0. Then
it checks:

- A: re-running the merge base installer keeps 2.10.
- B: re-running this branch's installer moves the home to 2.13.x /
0.28.x / 2.11.x, with MPS available and MPS attention matching an fp64
CPU reference.
- B2: a second re-run, then `unsloth studio update --local`, keep 2.13.
- C: `UNSLOTH_TORCH_UPGRADE=0` keeps a 2.10 home.
- D: a 2.14.1 home is kept.

The workflow runs on dispatch, and on pull requests only outside
`unslothai/unsloth`. On this repository's pull requests the job is
skipped, so it takes no macOS runner. It also skips drafts.

Results: pending.

## Not covered

- The Desktop automatic startup repair runs the installer without
`UNSLOTH_TORCH_UPGRADE`, so a repair of a home below 2.13 now moves it
to 2.13 as well. This is intended. Set `UNSLOTH_TORCH_UPGRADE=0` in the
environment to keep the resident torch.
- The Desktop app updates through `unsloth studio update`, which never
runs `install.sh`. A Desktop home that is never repaired or re-installed
keeps its torch. It moves through Settings > Repair installation, which
already sets `UNSLOTH_TORCH_UPGRADE=1`. Follow-up: a Studio "Repair
recommended" hint for Apple Silicon homes below 2.13, like the Windows
one in #19870.
- GitHub's macOS runners are virtual machines, so they show that the
install works, not how fast it runs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant