MLX

0.31.1 · active · verified Sun Apr 12

MLX is an array framework for machine learning on Apple Silicon, developed by Apple machine learning research. It provides a Python API that closely follows NumPy for array operations and higher-level packages like `mlx.nn` and `mlx.optimizers` with APIs that are similar to PyTorch. MLX supports composable function transformations for automatic differentiation, automatic vectorization, and computation graph optimization. Key features include lazy computation, dynamic graph construction, and a unified memory model across CPU and GPU. It is actively developed with frequent releases, currently at version 0.31.1.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating MLX arrays, performing basic element-wise and matrix operations, and explicitly materializing computations using `mx.eval()` due to MLX's lazy evaluation.

import mlx.core as mx

# Create an MLX array
a = mx.array([1, 2, 3], mx.float32)
print(f"Array a: {a}, dtype: {a.dtype}, shape: {a.shape}")

# Perform an operation (e.g., exponential)
b = mx.exp(a)
print(f"Array b (exp(a)): {b}")

# Perform a matrix multiplication
c = mx.array([[1, 2], [3, 4]])
d = mx.array([[5, 6], [7, 8]])
e = mx.matmul(c, d)
print(f"Matrix c: {c}\nMatrix d: {d}\nMatrix product c @ d: {e}")

# Ensure computation is materialized (for lazy operations)
mx.eval(e)
print("Computation materialized.")

view raw JSON →