Repository navigation
fix: Default to the CPU device when Metal is not available - #148
Open
georgeguimaraes wants to merge 2 commits into
Open
georgeguimaraes wants to merge 2 commits into
georgeguimaraes wants to merge 2 commits into
Conversation
EMLX.default_device/0 returned :gpu when :default_device was not configured. On Linux there is no GPU worker, so a jit with compiler: EMLX and no device: option failed. Now default_device/0 returns :cpu when the application did not start a GPU worker. A configured :default_device still wins.
polvalente
reviewed
Oct 20, 2026
Comment on lines
2545
to
2554
| def default_device do | ||
| Application.get_env(:emlx, :default_device, :gpu) | ||
| case Application.get_env(:emlx, :default_device) do | ||
| nil -> | ||
| # EMLX.Application starts a GPU worker only when Metal is available. | ||
| if :persistent_term.get({EMLX, :default_worker, :gpu}, nil), do: :gpu, else: :cpu | ||
|
|
||
| device -> | ||
| device | ||
| end | ||
| end |
Member
There was a problem hiding this comment.
I think this is not the correct solution and should be solved at the runtime config level.
As this stands, there can be a race condition where an app runs EMLX.default_device before the default worker is started and therefore gets cpu instead of gpu.
We should expose mlx::core::is_available as a NIF and call it with the result of Application.get_env(:emlx, :default_device, :gpu). If the configured default device is not available, we return :cpu instead
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.
EMLX.default_device/0returns:gpuwhen:default_deviceis not configured. On Linux,EMLX.Applicationdoes not start a GPU worker, so ajitwithcompiler: EMLXand nodevice:option fails. Users have to passdevice: :cputo every compile, or set the config, even though the CPU is the only device.I found this in CI on Linux, where the same code worked on a Mac.
With this change,
default_device/0returns:cpuwhen the application did not start a GPU worker. A configured:default_devicestill wins, and on a Mac with Metal the default is still:gpu.