Environment Variables#
MLX uses environment variables to configure compilation, numerical precision, backend behavior, and distributed execution. Set them before starting the process. Many variables are read when the corresponding subsystem is first initialized and changing them later may have no effect.
Boolean variables use 0 to disable and a nonzero integer to enable unless
otherwise noted.
General#
- MLX_DISABLE_COMPILE#
Disable compilation globally. This variable is enabled by its presence, so setting it to
0also disables compilation. Callingmlx.core.enable_compile()overrides it.
- MLX_ENABLE_TF32#
Allow reduced-precision
float32matrix-multiplication family operations on supported hardware. The default is1. Set it to0to keep these operations in fullfloat32precision. See Numerical Precision.
Distributed#
The variables required to initialize a distributed process depend on the
backend. Distributed Communication describes their formats and how mlx.launch
sets them.
- MLX_RANK#
The zero-based rank of the current process. This is used by the Ring, JACCL, and NCCL backends.
JACCL_RANKis accepted as a higher-priority alias by JACCL.
- MLX_HOSTFILE#
The path to the JSON host file used by the Ring backend.
- MLX_RING_VERBOSE#
Enable verbose logging for the Ring backend. This variable is enabled by its presence.
- MLX_IBV_DEVICES#
The path to the JSON device-connectivity file used by JACCL.
JACCL_IBV_DEVICESis accepted as a higher-priority alias.
- MLX_JACCL_COORDINATOR#
The coordinator address in
IP:portform used to establish JACCL connections.JACCL_COORDINATORis accepted as a higher-priority alias.
- MLX_JACCL_RING#
Prefer a ring topology for JACCL. This variable is enabled by its presence.
JACCL_RINGis accepted as a higher-priority alias.
- MLX_WORLD_SIZE#
The total number of processes in an NCCL group.
- MLX_NCCL_TIMEOUT#
The timeout in milliseconds for establishing NCCL bootstrap connections. The default is
300000.
- MLX_MPI_LIBNAME#
Override the MPI dynamic-library name. The default is
libmpi.dylibon macOS andlibmpi.soon other platforms.
The NCCL backend also requires NCCL_HOST_IP and NCCL_PORT. Setting
NCCL_DEBUG=INFO enables additional logging while MLX establishes the
bootstrap connection. NCCL itself recognizes additional NCCL environment
variables.
CUDA_VISIBLE_DEVICES selects the local CUDA device for each process and is
handled by the CUDA runtime.
Metal#
- MLX_METAL_FAST_SYNCH#
Enable the faster Metal CPU/GPU synchronization path. The default is
0. This requires Metal 3.2 or later (macOS 15 or later, or iOS 18 or later).
Advanced tuning#
These variables tune MLX implementation details. They are primarily intended for development, diagnostics, and performance experiments. The defaults are selected automatically for the current hardware and are appropriate for most users. Their behavior may change as the implementation evolves.
- MLX_BFS_MAX_WIDTH#
Set the breadth-first-search width limit used when constructing an evaluation tape. The default is
20.
- MLX_MAX_OPS_PER_BUFFER#
Override the maximum number of operations encoded in one Metal command buffer or CUDA graph. The default depends on the device.
- MLX_MAX_MB_PER_BUFFER#
Override the approximate memory limit, in megabytes, for one Metal command buffer or CUDA graph. The default depends on the device.
- MLX_METAL_GPU_ARCH#
Override the Metal GPU architecture string reported to MLX. This affects architecture-specific kernel and scheduling choices, but does not change the capabilities of the physical GPU. Forcing an architecture that does not match the GPU can select incompatible kernels and produce incorrect results.
- MLX_SDPA_BLOCKS#
Override the number of reduction blocks used by the Metal scaled dot-product attention kernel. Positive values are rounded up to a multiple of
32.
CUDA#
The MLX-prefixed variables in this section are advanced CUDA backend controls.
- MLX_USE_CUDA_GRAPHS#
Enable CUDA graph capture and replay. The default is
1.
- MLX_SAVE_CUDA_GRAPHS_DOT_FILE#
Use the specified value as the filename prefix when writing captured CUDA graphs to numbered DOT files. An unset or empty value disables the output.
- MLX_PTX_CACHE_DIR#
Override the directory used to cache runtime-compiled PTX. By default MLX uses an
mlx/<version>/ptxdirectory under the system temporary directory.
- MLX_CUDA_USE_CUDNN_SDPA#
Allow the CUDA backend to use cuDNN scaled dot-product attention when the inputs and device are supported. The default is
1.
- MLX_CUDA_CONV_CACHE_SIZE#
Set the CUDA convolution cache capacity. The default is
128.
- MLX_CUDA_FFT_CACHE_SIZE#
Set the CUDA FFT plan cache capacity. The default is
128.
- MLX_CUDA_GRAPH_CACHE_SIZE#
Set the CUDA graph cache capacity. The default is
400.
- MLX_CUDA_SDPA_CACHE_SIZE#
Set the CUDA forward scaled dot-product attention cache capacity. The default is
256.
- MLX_CUDA_SDPA_BACKWARD_CACHE_SIZE#
Set the CUDA backward scaled dot-product attention cache capacity. The default is
64.
- MLX_ENABLE_CACHE_THRASHING_CHECK#
Detect repeated CUDA cache misses and raise an error suggesting a larger cache capacity. The default is
1.
MLX also uses CUDA_HOME or CUDA_PATH to locate CUDA headers for runtime
kernel compilation when they cannot be found in the Python environment.