Skip to content

Do not self-heal MLX into a --no-torch install - #10409

Merged
danielhanchen merged 12 commits into
mainfrom
no-torch-autorepair-optout
Sep 8, 2026
Merged

danielhanchen merged 12 commits into
mainfrom
no-torch-autorepair-optout

Conversation

@shimmyshimmer

Copy link
Copy Markdown
Member

Problem

install.sh --no-torch on Apple Silicon is GGUF-only: no training stack. The installer honours that and installs no MLX. The runtime self-heal then puts it back on first launch.

Observed on a clean --no-torch install, from the server log:

Apple Silicon detected but the MLX stack is incomplete or too old; Train/Export disabled (chat-only)
(mlx is not installed (needs >=0.22.0); mlx-lm is not installed (needs >=0.31.2); mlx-vlm is not installed (needs >=0.4.4))
Apple Silicon without a usable MLX stack; attempting a one-time background reinstall of mlx/mlx-lm/mlx-vlm
MLX self-heal: installing mlx==0.32.1, mlx-lm==0.31.3, mlx-vlm>=0.4.4,<0.7.0
Hardware detected: MLX - Apple Silicon (arm64)

Result: 9 mlx* packages installed, chat_only flips to false, and Train/Export turn on. The user opted out of the training stack and got it anyway, roughly 20s after first launch.

start_mlx_autorepair_if_needed() gated on Apple Silicon, the UNSLOTH_DISABLE_MLX_AUTOREPAIR kill switch, and stack availability, but never on the install mode. mlx_repair.py had no reference to no-torch at all.

Fix

Treat a recorded no-torch install exactly like the existing kill switch, so it reuses that path unchanged: no reinstall, but a correct verdict still overturns a stale one.

recorded_no_torch() returns Optional[bool], and its docstring is explicit that None must not be read as False. Only a literal True opts out, so an install predating the manifest keeps today's behaviour, and a broken or unreadable manifest never silently disables the repair.

Verification

Real installs into clean UNSLOTH_STUDIO_HOME roots on macOS arm64, each launched and observed for 45s:

install repair attempts mlx* packages hardware chat_only
--no-torch, before 3 9 MLX - Apple Silicon false
--no-torch, after 0 0 CPU training backend true
normal install, MLX deliberately uninstalled 3 9 restored MLX - Apple Silicon false

The third row is the regression check: the self-heal must still work where it is wanted. It fires, succeeds, and logs MLX self-heal succeeded; Train/Export enabled.

Added 14 unit tests covering True / False / None, manifest exceptions, and truthy non-bool returns.

tests/test_health_holds_verdict_during_mlx_repair.py and tests/test_defaults_refresh_after_redetect.py: 36 passed. One of them was inheriting the ambient manifest and would have failed on a no-torch runner, so the apple_silicon fixture now pins the install mode alongside the other ambient state it already neutralises. Both files now pass under a no-torch venv and a normal venv.

Relationship to #10403

Independent and separately mergeable. #10403 stops the installer from putting MLX into a GGUF-only install; this stops the runtime from putting it back. Either can land first.

A --no-torch install is GGUF-only, but the runtime self-heal reinstalled
mlx/mlx-lm/mlx-vlm on first launch, flipping hardware to MLX and turning
Train/Export on against the user's choice. Treat a recorded no-torch install
like the existing kill switch. Unknown stays False so installs predating the
manifest keep today's behaviour.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-08T05:46:05.952702Z 04e106a 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.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen and others added 2 commits September 7, 2026 12:44
Two gaps found while testing this branch.

The unit tests the description promises were not in the branch: the only test
change was the one fixture line in test_health_holds_verdict_during_mlx_repair.py.
test_mlx_autorepair_no_torch_optout.py adds them. It pins that only a literal True
opts out, so None (every install predating the manifest key) and a truthy non-bool
keep today's behaviour; that an unreadable manifest, a missing studio package and a
raising lookup all fail open rather than costing someone their training stack; that
a --no-torch install starts no repair even when the verdict blames MLX, without
spending the one-shot latch; that a normal install and an older install with no
recorded mode still repair; and that an opted-out venv with a usable stack still
overturns a chat-only verdict, which is the reason the opt-out was spelled into the
kill switch's condition rather than given an early return. Six of the cases run
through the shipped install_manifest.py against a real manifest on disk, so a change
to its parsing cannot pass this file while breaking the gate.

test_mlx_repair.py now neutralises the install mode in its autouse fixture, the same
way the health test does. Left ambient, the answer comes from the manifest of
whatever venv the tests run in: with a GGUF-only Studio venv, which is a real place
to run them, four of its tests fail on this branch
(test_apple_silicon_missing_mlx_starts_repair_and_redetects,
test_attempts_only_once_per_process,
test_a_stack_that_is_really_unusable_keeps_its_verdict, and
test_the_repair_worker_is_scoped_to_the_epoch_read_before_the_measurement).

91 tests pass across the three files with a no-torch manifest present and with none.
@danielhanchen

Copy link
Copy Markdown
Member

I went through this branch and pushed one commit to it. The change itself holds up; two things around it did not.

The change

Correct, and the unknown case is the right way round. recorded_no_torch() answers Optional[bool], and None is what every install predating the manifest key looks like, so reading it as False keeps today's behaviour for them. Reading it as True would have quietly disabled the self-heal for every older install.

What I pushed

The unit tests the description promises were not in the branch. The only test change here was the single fixture line in test_health_holds_verdict_during_mlx_repair.py. studio/backend/tests/test_mlx_autorepair_no_torch_optout.py adds them, 24 cases:

  • only a literal True opts out: False, None, and a truthy non-bool ("1", 1, a list, an object) all keep the repair
  • an unreadable manifest, a missing studio package and a raising lookup all fail open, so a broken manifest never costs an Apple Silicon user their training stack
  • a --no-torch install starts no repair even when the verdict blames MLX, which is the branch that carries the opt-out past the first gate, and it does not spend the one-shot latch
  • a normal install still repairs, and so does an older install with no recorded mode
  • an opted-out venv with a usable stack still overturns a chat-only verdict, which is why the opt-out belongs in the kill switch's condition rather than in an early return of its own
  • six cases run through the shipped install_manifest.py against a real manifest on disk ({"no_torch": true}, false, "1", "no", a manifest predating the key, no manifest at all, and a corrupt one), so a change to its parsing cannot pass this file while breaking the gate

test_mlx_repair.py was left reading the ambient install mode. Its autouse fixture neutralises the attempt latch, the mutation flag and the kill switch, but not the new gate, so the answer comes from the manifest of whatever venv the tests run in. Inside a GGUF-only Studio venv, which is a real place to run them, four of its tests fail on this branch:

FAILED test_mlx_repair.py::test_apple_silicon_missing_mlx_starts_repair_and_redetects
FAILED test_mlx_repair.py::test_attempts_only_once_per_process
FAILED test_mlx_repair.py::test_a_stack_that_is_really_unusable_keeps_its_verdict
FAILED test_mlx_repair.py::test_the_repair_worker_is_scoped_to_the_epoch_read_before_the_measurement

That is the same ambient-state problem the fixture in test_health_holds_verdict_during_mlx_repair.py already fixes, one file over. The fixture now pins it, and the file that owns the opt-out drives the True case.

Verification

test_mlx_repair.py, test_mlx_autorepair_no_torch_optout.py and test_health_holds_verdict_during_mlx_repair.py: 91 passed with a no_torch manifest present at the venv root and 91 passed with none, so the suite no longer depends on where it is run. With test_mlx_stack_blockers.py and test_defaults_refresh_after_redetect.py added: 99 passed on the branch as it was.

Relationship to #10431

Independent, disjoint files, mergeable in either order. #10431 fixes the opposite direction, MLX failing where it was wanted: on a normal torch install on macOS arm64 the installer leaves transformers and tokenizers pinned apart, so import mlx_lm raises and Train and Export are off on a machine that asked for them. Worth noting for this PR: the self-heal is what has been repairing that pair in the field, which is why the report reads as "Train was off until the update". This PR does not remove that rescue from anyone who needs it. A --no-torch install never installs MLX and already pins tokenizers via no-torch-runtime.txt, so the pair cannot come apart on the path this PR turns the self-heal off for.

@NilayYadav

Copy link
Copy Markdown
Collaborator

Verified this on an Apple Silicon Mac against a real no-torch venv: main installs mlx and flips chat_only at ~4.6s, this branch stays chat-only with no mlx, and normal installs still repair.

Pushed 239673b with two fixes:

  • mlx_repair_in_flight() now declines on a no-torch install too, so /api/health settles on the first poll instead of after the 30s grace (Codex's P2).
  • test_warm_window_review_fixes.py::test_the_mlx_worker_reads_its_epoch_before_start was still reading the ambient manifest and failed inside a no-torch venv; now pinned like the other two files.

225 passed across the related test files with system Python and inside the no-torch venv. ruff clean.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 679c4163ce

ℹ️ 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

Copy link
Copy Markdown
Member

Confirmed the self-heal in studio/backend/utils/mlx_repair.py has no install-mode gate today, so a --no-torch install gets MLX put back on first launch, and the follow-up commits added the unit tests and pinned the ambient manifest in the older test files. Will get this reviewed.

The runtime gate and the new no_torch verdict are both well covered now, but the
line that makes the opt-out survive an update was not: install_python_stack.py's
MLX step runs on IS_MAC_ARM and not skip_base, the updater clears
SKIP_STUDIO_BASE, and without the not NO_TORCH guard a routine
'unsloth studio update' reinstalls mlx/mlx-lm/mlx-vlm into a GGUF-only venv and
the next launch enables Train before the no_torch verdict is ever reached.

Structural, for the reason test_diffusers_pin.py is: running the installer needs
a Mac, and what has to hold is a property of the gate. Four checks: the step is
gated on both IS_MAC_ARM and not NO_TORCH; the base_total accounting uses the
same expression, so the progress bar cannot count a step that never runs; the
updater still clears SKIP_STUDIO_BASE, so the first check cannot pass vacuously;
and _infer_no_torch still consults the manifest, which is what makes NO_TORCH
true during an update, since the updater injects no environment variable.

Verified against a tree with the guard removed: the first two fail, naming the
weakened gate and the desynced accounting.
@danielhanchen

Copy link
Copy Markdown
Member

Re-checked at 679c416 after the review round. All six items look genuinely addressed, and I verified the one with the widest blast radius myself rather than taking the commit headline for it.

The update path. This was the item that mattered: gating the runtime self-heal is only half an opt-out if unsloth studio update reinstalls the stack anyway. It holds. install_python_stack.py's MLX step and its base_total accounting now both carry not NO_TORCH, and NO_TORCH resolves env, then manifest, then platform, so an update, which injects no UNSLOTH_NO_TORCH, still reads no_torch: true out of the venv's manifest and skips the step. The two gates being the same expression matters too, or the progress bar counts a step that never runs.

That line had no test, so I pushed one: tests/studio/install/test_no_torch_skips_mlx_stack.py, structural for the reason test_diffusers_pin.py is. Four checks: the step is gated on IS_MAC_ARM and not NO_TORCH; the accounting uses the same expression; the updater still clears SKIP_STUDIO_BASE, so the first check cannot pass vacuously; and _infer_no_torch still consults the manifest. Against a tree with the guard removed the first two fail and name the weakened gate.

The other five. mlx_repair_in_flight() now declines on the manifest opt-out, so the verdict settles instead of staying provisional through the grace window. The no_torch reason is distinct and reaches export_capability(). A present-but-unusable stack stays mlx_unavailable until the post-warm probe measures it, via _mlx_distribution_installed(), so a usable stack that only lost the warm's import race is still overturned. _NO_TORCH_SETTLED_EPOCH is keyed to the epoch and checked under the lock, so a worker retired by a shutdown cannot settle the lifespan that replaced it.

Two things I checked for and did not find: _MLX_BLOCKERS_MEASURED is assigned in the new branch but _detect_hardware_locked already declares it global, so no UnboundLocalError; and settle_the_no_torch_verdict takes _DETECT_LOCK then calls current_detection_epoch(), which takes _EPOCH_LOCK, but that one is a leaf everywhere and _DETECT_LOCK is an RLock, so there is no inversion.

Suites. 253 pass locally across test_mlx_repair.py, test_mlx_autorepair_no_torch_optout.py, test_health_holds_verdict_during_mlx_repair.py, test_chat_only_reason.py, test_export_capability.py, test_warm_window_review_fixes.py, test_mlx_stack_blockers.py and test_defaults_refresh_after_redetect.py, plus the four new ones.

The red check is not this PR. parity (windows-latest) fails on test_a_non_ascii_marker_survives..., reading PowerShell's stdout as the Windows ANSI code page. The same job fails identically on studio/rocm-torch-explicit-request, studio/amd-device-permissions, woa-native-arm64-cuda, uninstall-ownership-gate and studio-per-account-isolation, none of which touch this area. #10432 fixes it.

@danielhanchen

Copy link
Copy Markdown
Member

@codex security review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

_NO_TORCH_SETTLED_EPOCH was assigned before the CHAT_ONLY_REASON check, so a
settle that arrives after another same-epoch pass has already enabled training
declined, as it should, but still marked the epoch. The next transient MLX import
failure in that lifespan then published no_torch immediately, which reads as
settled, and start_mlx_autorepair_if_needed() returned early because the verdict
no longer blamed the MLX stack, leaving a usable stack unmeasured until a
restart. Narrow, but reachable on exactly the host this PR is about: a --no-torch
install carrying a manually installed MLX stack.

Moving the assignment below the check also matches what the flag is documented to
mean, which is that the post-warm probe measured this host's stack unusable.

test_a_declined_settle_does_not_mark_the_epoch covers it: with the assignment back
above the check it fails on the epoch, and a following detect_hardware() publishes
no_torch instead of mlx_unavailable. 222 tests pass across the six affected
suites.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from NilayYadav Sep 8, 2026
@unslothai unslothai deleted a comment from NilayYadav Sep 8, 2026
@unslothai unslothai deleted a comment from NilayYadav Sep 8, 2026
@unslothai unslothai deleted a comment from NilayYadav Sep 8, 2026
@unslothai unslothai deleted a comment from NilayYadav Sep 8, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 04e106a612

ℹ️ 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 12b2244 into main Sep 8, 2026
85 checks passed
@danielhanchen
danielhanchen deleted the no-torch-autorepair-optout branch September 8, 2026 06:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants