fix(Device): resolve initial default from MLX core, not hard-coded GPU#422
Merged
davidkoski merged 1 commit intoJun 11, 2026
Merged
Conversation
…coded GPU Closes ml-explore#395. `_resolveGlobalDefaultDevice()` returned `.gpu` whenever no explicit `setDefault(...)` had been called, so on a CPU-only host (Linux with no Metal / no CUDA) `Device.defaultDevice()` and `StreamOrDevice.default` both returned a device that simply doesn't exist, causing every subsequent op routed to the default stream to fail. The deprecated `Device.init()` already shows the right pattern: call `mlx_get_default_device()` and let the C++ core pick the device its build supports. Use that path instead of the hard-coded `.gpu`. On Apple platforms with Metal this still resolves to GPU — existing tests (`testUsingDevice`) cover that — and `setDefault` / `withDefaultDevice` overrides continue to win through the same `_defaultDevice` check above the new fall-through. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
davidkoski
approved these changes
Jun 11, 2026
davidkoski
left a comment
Collaborator
There was a problem hiding this comment.
Looks good, thank you.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #395.
_resolveGlobalDefaultDevice()returned.gpuwhenever no explicitsetDefault(...)had been called, so on a CPU-only host (Linux with no Metal / no CUDA)Device.defaultDevice()andStreamOrDevice.defaultboth returned a device that doesn't actually exist on that machine — every subsequent op routed to the default stream then failed.The deprecated
Device.init()already encodes the right pattern: ask the underlying C++ core withmlx_get_default_device(...)and let it pick the device its build supports. Switch_resolveGlobalDefaultDeviceto that path.private static func _resolveGlobalDefaultDevice() -> Device { _lock.withLock { - _defaultDevice ?? .gpu + if let device = _defaultDevice { + return device + } + var ctx = mlx_device_new() + mlx_get_default_device(&ctx) + return Device(ctx) } }On Apple platforms with Metal this still resolves to GPU —
testUsingDevicealready covers that and continues to pass — andsetDefault/withDefaultDeviceoverrides still win through the same_defaultDevicecheck above the fall-through.Matches the fix proposed in the issue.