Repository navigation
Throw meaningful error when Metal device is not found - #3428
Conversation
In headless, sandboxed, or virtualized macOS sessions (PyInstaller bundles, CI containers, automation runs) `MTL::CopyAllDevices()` returns an empty NSArray. The current code immediately calls `devices->object(0)`, which raises an Objective-C `NSRangeException` that aborts the process before the Python layer can catch it. Guard the access with a `count() > 0` check and surface a clear `std::runtime_error` if both `CopyAllDevices()` and `CreateSystemDefaultDevice()` fail. The fallback path is preserved. Fixes ml-explore#3148.
zcbenz
left a comment
There was a problem hiding this comment.
This is a very nice improvement, thanks!
It does not completely fix #3148 though: ideally no error should be thrown at all when there is no use of GPU device, i.e. following code should never throw:
import mlx.core as mx
print(mx.device_count(mx.gpu))c71e588 to
b41c754
Compare
|
The simpler |
|
Properly handling headless env would require refactoring lots of different parts of code, I wouldn’t expect to finish it in a single PR. I think this PR’s original change that improved the error message was actually a very good start that we should merge first. |
7361426 to
8ce25f0
Compare
Ok then I reverted it to initial commit. |
Proposed changes
In headless, sandboxed, or virtualized macOS sessions (PyInstaller
bundles, CI containers, automation runs)
MTL::CopyAllDevices()returnsan empty NSArray. The current code immediately calls
devices->object(0),which raises an Objective-C
NSRangeExceptionthat aborts the processbefore the Python layer can catch it.
Guard the access with a
count() > 0check and surface a clearstd::runtime_errorif bothCopyAllDevices()andCreateSystemDefaultDevice()fail. The fallback path is preserved.This is the guard @awni proposed on the issue thread.
Refs #3148.
Verified locally: GPU build still loads and the full
python/tests/suite passes (662 passed, 9007 subtests, 0 regressions); patch source
review confirms guard + fallback + clear error message.
Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes