Skip to content

fix(cli): make unsloth doctor MLX-aware on Apple Silicon - #14457

Merged
LeoBorcherding merged 11 commits into
unslothai:mainfrom
shadyuwugurl:fix/doctor-apple-silicon-mlx
Oct 30, 2026
Merged

LeoBorcherding merged 11 commits into
unslothai:mainfrom
shadyuwugurl:fix/doctor-apple-silicon-mlx

Conversation

@shadyuwugurl

@shadyuwugurl shadyuwugurl commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

What

unsloth doctor only knew how to probe CUDA / ROCm GPUs. Before #14374, a healthy Apple Silicon Mac, where Unsloth trains through the MLX backend (unsloth_zoo.mlx), got a false failure:

$ unsloth doctor
=== Versions ===
torch 2.14.1
CUDA (torch build) None
driver unknown
NCCL unavailable

=== GPUs (0) ===

=== Verdict: PROBLEMS FOUND ===
- No GPU is visible to torch. Check `nvidia-smi`, CUDA_VISIBLE_DEVICES, and that torch was installed with CUDA or ROCm support.
$ echo $?
1

Since #14374, main passes a Mac as soon as it sees Metal, but it still never checks whether MLX can train. With mlx_lm or mlx_vlm broken, main's unsloth doctor says OK and exits 0 while Unsloth Studio is chat-only. This PR runs the same MLX check Unsloth Studio uses, so doctor's answer matches whether training will actually work.

Why this is a real gap, not a cosmetic one

The MLX backend is a first-class path in this repo — unsloth train, unsloth chat, unsloth inference and unsloth export all detect it and run on Apple Silicon. doctor is the one command that was never taught about it, and it is the command users reach for first when something goes wrong.

This is the same machine, same install, same process — only the doctor was blind:

$ unsloth train --model <mlx-4bit-model> ...
Step 1/2 Loss 1.4718 (7 tok/s, peak 5.39 GB)
Step 2/2 Loss 2.7413 (120 tok/s, peak 5.85 GB)
...
compile_support_state='supported_verified'

The fix

doctor now asks the repository's own gate which backend is live (studio.backend.core.training.training.should_use_mlx_training_backend) and, when MLX is active, runs an MLX preflight instead of the CUDA/NCCL one:

$ unsloth doctor
=== Versions ===
platform Darwin arm64
backend mlx
mlx 0.32.4
unsloth_zoo 2026.10.7
Metal available

=== Device ===
  Apple M5  applegpu_g17g
  memory 32.0 GiB
  recommended working set 24.96 GiB

=== Verdict: OK ===
- Apple Silicon GPU through the MLX backend. Multi-GPU, NCCL and peer-copy checks do not apply to this backend, so they were skipped.
$ echo $?
0

Details:

  • mlx_backend_active(): honours UNSLOTH_FORCE_GPU_PATH=1, then defers to the repo's should_use_mlx_training_backend() gate. If the Unsloth Studio backend can't be imported, it falls back to the same check that gate makes (macOS on Apple Silicon). It doesn't require mlx to be importable, so a Mac without mlx gets the MLX verdict's "install mlx" hint instead of the CUDA path's nvidia-smi one.
  • collect_mlx_info(): collects the platform, the mlx and unsloth_zoo versions, Metal availability, and the device name, architecture and memory from mx.device_info(). unsloth_zoo.mlx only counts as present when both unsloth_zoo.mlx.trainer and unsloth_zoo.mlx.loader import. It also runs mlx_stack_blockers() from studio/backend/utils/mlx_repair.py, the same check that decides whether Unsloth Studio can train on a Mac (mlx-lm / mlx-vlm imports and minimum versions). It never raises.
  • verdict_mlx(info): a pure function, in the same style as the existing verdict(). It reports healthy only when mlx, unsloth_zoo.mlx, the MLX stack check, Metal and a device all pass. A stack check that couldn't run counts as a failure, since training needs the same modules. Every repair hint says unsloth studio update, never a plain pip install, which can move Transformers to a version Unsloth Studio can't use.
  • The CUDA path is byte-identical. _gpu_checks() is untouched; the only change is the branch that chooses between _mlx_checks() and _gpu_checks(timeout).

Tests

unsloth_cli/tests/test_doctor_mlx.py has 25 tests, all runnable without a GPU. They cover:

  • the healthy Apple Silicon case
  • each failure mode: no mlx, an unsloth_zoo without MLX, only one of the two zoo MLX modules importing, the MLX stack check reporting a problem or not running at all, no Metal, no device
  • routing to the MLX checks when mlx is missing
  • the platform fallback when the Unsloth Studio backend can't be imported (Apple Silicon Mac, Intel Mac, Linux)
  • no hint suggesting a bare pip install
  • UNSLOTH_FORCE_GPU_PATH
  • doctor exiting 0 on the MLX path and keeping the CUDA path when MLX is off

The two CUDA tests in test_doctor_gpus.py are pinned to the CUDA path so they don't pick up the MLX path on an Apple Silicon host.

Measured at 4be4c982ea:

check result
pytest unsloth_cli/tests/test_doctor_mlx.py unsloth_cli/tests/test_doctor_gpus.py 65 passed
pytest tests/security/test_lint_risky_loaders.py 46 passed
unsloth doctor on an Apple M5 (32 GB, macOS 26.6, mlx 0.32.3) exit 0, Verdict OK, about 4 s
same M5 with mlx_lm broken or structlog missing names the problem, exit 1
fresh uv pip install of this branch (no mlx, no zoo) points at unsloth studio update, exit 1
Codex (mirror) no major issues
CI (mirror) 13 passed, 8 skipped, 2 failed. Neither failure involves unsloth doctor (block-compile tests, a Windows WMI-timeout test)

Not run: Linux CUDA or ROCm hosts (the CUDA path is unchanged by this PR).

Notes

  • The MLX path is not new — unsloth_zoo.mlx has shipped MLXTrainer / FastMLXModel for a while, and this repo already routes train, chat, inference and export to it. This PR only teaches the preflight about it.
  • mx.metal.device_info() is deprecated in favour of mx.device_info(); the code prefers the new API and keeps the old one only as a fallback.

📎 Porting index: https://github.com/shadyuwugurl/mlx-lab — docs/PR_INDEX.md lists every upstream PR in this effort, docs/FINDINGS.md records the Apple Silicon platform findings, and docs/MODEL_MATRIX.md shows the verified model matrix on an M5.

`unsloth doctor` only knew how to probe CUDA/ROCm GPUs. On an Apple
Silicon Mac running the MLX backend it printed "=== GPUs (0) ===", a
false "PROBLEMS FOUND" verdict with an `nvidia-smi` hint, and exited
1 - even though `unsloth train` works there via `unsloth_zoo.mlx`.

Detect the active backend with the repository's own MLX gate
(`should_use_mlx_training_backend`) and, when it is active, report the
MLX/Metal/device information instead of the CUDA/NCCL sections. The
verdict becomes healthy (exit 0) when MLX, Metal and `unsloth_zoo.mlx`
are all present, and only reports hints for real problems.

The CUDA path is unchanged.
@LeoBorcherding

Copy link
Copy Markdown
Collaborator

Tested this on an Apple M5 (32 GB, macOS 26.6, mlx 0.32.3, unsloth_zoo 2026.10.6) at 76a526ff50, compared against its base b6effa301b.

The bug is real and this fixes it. On the base, unsloth doctor exits 1 with "No GPU is visible to torch. Check nvidia-smi". On this branch it exits 0 and shows backend mlx, the Apple M5 at 32.0 GiB, and Verdict OK. pytest unsloth_cli/tests/test_doctor_mlx.py unsloth_cli/tests/test_doctor_gpus.py gives 46 passed. UNSLOTH_FORCE_GPU_PATH=1 takes the CUDA path as described. Both versions of the command finish in about 0.57 s, and the gate doesn't load torch.

I blocked a few imports to try the failure modes, and three things came up:

  1. The "mlx is not installed" hint can never show. mlx_backend_active() returns False when find_spec("mlx") is None (doctor.py:550). So a Mac without mlx goes down the CUDA path and gets the same nvidia-smi message this PR is meant to remove. The if not info.get("mlx") branch in verdict_mlx (doctor.py:617) can't be reached from the CLI. I confirmed this with mlx blocked: exit 1, nvidia-smi hint. Suggest gating on the platform alone and letting verdict_mlx report the missing package.
  2. A broken studio backend import brings the false failure back. Importing studio.backend.core.training.training loads the studio backend (structlog, utils.hardware, and so on), and any exception is swallowed into return False. With structlog blocked, doctor gives the old exit 1 and the nvidia-smi hint. The gate it calls is only platform.system() == "Darwin" and platform.machine() == "arm64". Either check the platform directly, or fall back to that check when the import fails.
  3. zoo_mlx is True if either module imports (doctor.py:605-611). With unsloth_zoo.mlx.trainer failing and the loader fine, the verdict is still OK with exit 0. The only sign is a note: line. Requiring both imports would catch it.

One small thing in the tests: test_mlx_backend_active_false_off_apple_silicon (test_doctor_mlx.py:107) doesn't patch find_spec. On a runner without mlx, which is every hosted Linux runner, it returns at the find_spec is None check and never reaches the fake gate. That one is from reading the code, not a run.

Not tested: Linux CUDA or ROCm hosts (the CUDA branch is unchanged and the 34 existing tests still pass), Intel Macs, and an installed wheel instead of the source tree.

@LeoBorcherding

LeoBorcherding commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Pushed fixes for the three gaps and the test from my earlier comment to this branch as 838399148d:

--- a/unsloth_cli/commands/doctor.py
+++ b/unsloth_cli/commands/doctor.py
@@
 import importlib
 import importlib.metadata
-import importlib.util
 import os
@@ def mlx_backend_active() -> bool:
     if os.environ.get("UNSLOTH_FORCE_GPU_PATH") == "1":
         return False
+    # Not gated on mlx being importable: a Mac without it should get the MLX
+    # verdict's install hint, not the CUDA path's nvidia-smi one.
     try:
-        if importlib.util.find_spec("mlx") is None:
-            return False
         from studio.backend.core.training.training import should_use_mlx_training_backend
         return bool(should_use_mlx_training_backend())
     except Exception:
-        return False
+        # The repo gate is a platform check; keep it if the studio backend can't import.
+        return platform.system() == "Darwin" and platform.machine() == "arm64"
@@ def collect_mlx_info() -> dict:
+    info["zoo_mlx"] = True
     for module in ("unsloth_zoo.mlx.trainer", "unsloth_zoo.mlx.loader"):
         try:
             importlib.import_module(module)
         except Exception as e:
             info["errors"].append(f"{module}: {type(e).__name__}: {e}")
-        else:
-            info["zoo_mlx"] = True
+            info["zoo_mlx"] = False
  1. Removing the find_spec("mlx") early return means a Mac without mlx gets the MLX checks, so verdict_mlx gives the "install mlx" hint instead of the CUDA path's nvidia-smi one.
  2. If the studio backend can't be imported, the gate falls back to the same Darwin/arm64 check that should_use_mlx_training_backend() makes, instead of returning False.
  3. zoo_mlx is now True only if both modules import.
  4. With the early return gone, test_mlx_backend_active_false_off_apple_silicon reaches the gate on runners without mlx too.

Test changes in test_doctor_mlx.py:

  • The find_spec patches are gone.
  • test_mlx_backend_active_false_without_mlx becomes test_mlx_backend_active_without_mlx_installed, which now expects True.
  • New: test_mlx_backend_active_falls_back_to_platform, checking an Apple Silicon Mac, an Intel Mac and Linux.
  • New: test_collect_mlx_info_needs_every_zoo_mlx_module.
  • New: test_doctor_without_mlx_points_at_installing_it.

The three new behaviour tests fail on 76a526ff50 and pass with the fix.

Re-run on the same M5:

case 76a526ff50 with the fix
healthy M5 OK, exit 0 OK, exit 0
mlx not installed nvidia-smi hint, exit 1 "mlx is not installed", exit 1
studio backend import fails nvidia-smi hint, exit 1 OK, exit 0
unsloth_zoo.mlx.trainer fails to import OK, exit 0 "upgrade unsloth_zoo", exit 1

pytest unsloth_cli/tests/test_doctor_mlx.py unsloth_cli/tests/test_doctor_gpus.py: 51 passed.

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 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 ⚠️ Failed 2026-10-23T09:56:04.588928Z
We couldn’t complete this review. Comment @codex review to try again.
936884a 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

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

An unknown error occurred
ℹ️ 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".

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

An unknown error occurred
ℹ️ 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".

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

Codex review is clean at 0920db2f0c ("Didn't find any major issues"), run on a mirror since @codex review here errored twice: LeoBorcherding#537

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

An unknown error occurred
ℹ️ 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".

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

Pushed 936884a5d8.

Changes in this push

  • Fixed a CI failure: a security lint blocks loading Python modules by a name stored in a variable, and unsloth doctor did that. The two module names are now written out directly (lint passes on the mirror).
  • Merged the latest main.

Checks at this commit

check result
Codex no issues found (review, run on a mirror because @codex review here keeps erroring)
CI (on the mirror) 18 passed, 3 failed. None of the failures involve unsloth doctor; two are in code that came in with main
Apple M5 Mac unsloth doctor exits 0 with Verdict OK, 103 related tests pass

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

Pushed 839c72dd63.

Changes since 936884a5d8

  • Doctor now uses the same MLX check that decides whether Unsloth Studio can train on a Mac (mlx_stack_blockers()). Before, it said OK even with mlx_lm or mlx_vlm broken.
  • If that check can't run at all (for example a missing dependency), doctor now reports a problem instead of OK. Training needs the same modules, so it couldn't run either.
  • Repair hints say unsloth studio update instead of pip install mlx mlx-lm. A plain pip install can move Transformers to a version Unsloth Studio can't use: a dry run on a working env would have moved transformers 5.5.0 → 5.20.2.
  • Doctor takes about 7 s on a healthy Mac now instead of under 1 s, because the check really imports mlx_lm and mlx_vlm.

Checks at this commit

check result
Apple M5, healthy Verdict OK, exit 0
Apple M5, mlx_lm broken / structlog missing names the problem, exit 1 (both said OK before)
fresh uv pip install of this branch points at unsloth studio update, exit 1
tests 111 passed; the new ones fail on 936884a5d8
Codex (mirror) no major issues
CI (mirror) 19 passed, 4 failed. None of the failures involve unsloth doctor (fp8 kernel lint, save-routing tests, an npm install, an installer test)

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

Pushed 4be4c982ea: latest main merged in, no code changes.

One thing changed on main underneath this PR: #14374 taught the CUDA path to pass when it sees Metal. So on main, a healthy Mac no longer fails unsloth doctor. What main still misses is whether MLX can actually train, which is what this PR checks. Same M5, same cases:

case main this PR
healthy Mac OK, exit 0 OK, exit 0
mlx_lm or mlx_vlm broken, or structlog missing OK, exit 0 names the problem, exit 1
fresh install with no mlx "torch is not installed; nothing to check", exit 1 "Run unsloth studio update", exit 1

Codex is clean at this commit (review). CI on the mirror: 13 passed, 8 skipped, 2 failed, and neither failure involves unsloth doctor (block-compile tests in Backend CI, a Windows WMI-timeout test in Cross-platform parity).

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

Status at 4be4c982ea (current head):

  • Codex: no issues (review on the mirror).
  • CI on the mirror: 13 passed, 8 skipped, 2 failed. Neither failure involves this PR. This head differs from main only in unsloth_cli/commands/doctor.py and its two test files:
    • Backend CI: 56 torch.compile tests fail with AttributeError: torch._dynamo.config.suppress_errors does not exist on the runner's torch 2.10. None of those test files import unsloth_cli.
    • Cross-platform parity: one Windows WMI-timeout test in the PowerShell installer suite.
  • Doctor tests: 65 passed (test_doctor_mlx.py, test_doctor_gpus.py).

Upstream CI is waiting on workflow approval.

@LeoBorcherding LeoBorcherding left a comment

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.

LGTM

@LeoBorcherding
LeoBorcherding merged commit 1cd3674 into unslothai:main Oct 30, 2026
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