Device selection#
These functions set MLX’s default device and return the selected
mlx.core.Device object. They are thin wrappers around
mx.set_default_device with an optional availability probe.
use_gpu#
- mlx_sparse.use_gpu(index=0, *, require_available=True)[source]#
Set MLX’s default device to GPU (Metal) and return the device object.
On Apple Silicon, this selects the integrated GPU via MLX’s Metal backend. Fixed-shape sparse primitives dispatch native Metal kernels for supported value and index dtypes. Operations with dynamic output structure, such as sparse-sparse products and duplicate summation, may synchronize to host for structural assembly.
- Parameters:
- Returns:
The
mlx.core.Deviceobject that was set as the default.- Raises:
RuntimeError – If
require_available=Trueand the GPU is not available or cannot be probed.- Return type:
mlx.core.Device
Example:
import mlx_sparse as ms ms.use_gpu() # pin to GPU y = A @ x # dispatches Metal csr_matvec kernel
use_cpu#
- mlx_sparse.use_cpu(index=0, *, require_available=True)[source]#
Set MLX’s default device to CPU and return the device object.
Calling this once at the start of a script or test is the recommended way to pin all subsequent MLX and mlx-sparse operations to the CPU. The setting persists for the lifetime of the Python process or until overridden by another
use_*call.- Parameters:
index (int) – CPU device index. Almost always
0. Multiple CPU devices are not typical in Apple Silicon runtimes. Default0.require_available (bool) – If
True(default), probe the device immediately with a trivialmx.evaland raiseRuntimeErrorif it fails. Set toFalseto skip the probe (e.g. in environments where eager evaluation is not yet possible).
- Returns:
The
mlx.core.Deviceobject that was set as the default.- Raises:
RuntimeError – If
require_available=Trueand the CPU device cannot be probed successfully.- Return type:
mlx.core.Device
Example:
import mlx_sparse as ms ms.use_cpu() # pin to CPU y = A @ x # runs on CPU
use_device#
- mlx_sparse.use_device(name, index=0)[source]#
Set MLX’s default device by name string.
A convenience wrapper around
use_cpu()anduse_gpu()that accepts a plain string device name. Useful when the target device is provided as a command-line argument.- Parameters:
- Returns:
The
mlx.core.Deviceobject that was set as the default.- Raises:
ValueError – If
nameis not"cpu"or"gpu".- Return type:
mlx.core.Device
Example:
import argparse import mlx_sparse as ms parser = argparse.ArgumentParser() parser.add_argument("--device", default="gpu") args = parser.parse_args() ms.use_device(args.device)
Native capabilities#
mlx-sparse exposes enum-backed runtime capability checks for native
backend dispatch. Use these when application code needs to branch on whether a
backend is actually usable, rather than checking platform strings or package
filenames directly.
import mlx_sparse as ms
if ms.capabilities.METAL:
ms.use_gpu()
if ms.capabilities.ACCELERATE:
pass # optional Apple Accelerate sparse direct solvers are available
The public capability names are "extension", "cpu", "metal",
"accelerate", "cuda", and "rocm". The corresponding uppercase
attributes on capabilities return booleans, for example
ms.capabilities.CPU and ms.capabilities.METAL. Status strings are
"available", "unavailable", and "not_built".
ms.capabilities.ACCELERATE reports Accelerate-backed sparse solver
availability, not just framework presence. Current published macOS wheels
(v0.0.4b0 onwards) are built with Accelerate sparse direct-solver support
enabled. Linux wheels are CPU-only and report "not_built" for Metal,
Accelerate, CUDA, and ROCm. Editable and source macOS builds can opt into the
same Accelerate path with MLX_SPARSE_ENABLE_ACCELERATE=ON.
- mlx_sparse.capabilities = mlx_sparse.capabilities(extension='unavailable', cpu='unavailable', metal='unavailable', accelerate='unavailable', cuda='unavailable', rocm='unavailable')#
User-facing native capability view.
mlx_sparse.capabilitiesis intentionally small and string-friendly:capability names:
"extension","cpu","metal","accelerate","cuda","rocm"statuses:
"available","unavailable","not_built"
Example:
import mlx_sparse as ms if ms.capabilities.METAL: ms.use_gpu() if ms.capabilities.status("accelerate") == "not_built": ...
Usage notes#
Call one of these functions once, early in your script, to pin execution to a specific device.
The selected device persists for the lifetime of the Python process or until another
use_*call.The Metal GPU path (
use_gpu) requires macOS with Apple Silicon and MLX ≥ 0.31. On unsupported hardwareuse_gpuraisesRuntimeErrorwhenrequire_available=True(the default).Linux builds currently support native CPU execution only. CUDA and ROCm are reserved capability names for future releases.
To check sparse native backend availability without setting the default device, prefer
ms.capabilities.
See Device selection and execution model for a full discussion of the lazy execution model and which operations run on GPU vs CPU.