Packages

MLX bindings for Elixir via mlx-c. An Nx backend and Nx.Defn compiler for Apple's MLX machine learning framework on Apple Silicon.

Current section

Files

Jump to
elixir_mlx lib mlx device.ex
Raw

lib/mlx/device.ex

defmodule Mlx.Device do
@moduledoc """
Device management for MLX.
Satisfies: RT-2 (device management), T6 (unified memory)
MLX uses Apple's unified memory architecture, so CPU and GPU
share the same memory space. No data copies are needed when
switching devices — only the execution target changes.
"""
@doc "Returns the default device (typically GPU on Apple Silicon)."
def default do
case Mlx.NIF.default_device() do
{:ok, ref} -> ref
{:error, reason} -> raise "failed to get default device: #{reason}"
end
end
@doc "Creates a device of the given type (:cpu or :gpu)."
def new(type) when type in [:cpu, :gpu] do
case Mlx.NIF.device_new(type) do
{:ok, ref} -> ref
{:error, reason} -> raise "failed to create #{type} device: #{reason}"
end
end
@doc "Sets the default device."
def set_default(type) when type in [:cpu, :gpu] do
device = new(type)
case Mlx.NIF.set_default_device(device) do
:ok -> :ok
{:error, reason} -> raise "failed to set default device: #{reason}"
end
end
end