{"id":4633,"library":"mlx","title":"MLX","description":"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.","status":"active","version":"0.31.1","language":"en","source_language":"en","source_url":"https://github.com/ml-explore/mlx","tags":["machine-learning","apple-silicon","deep-learning","gpu","numpy-like","pytorch-like","metal"],"install":[{"cmd":"pip install mlx","lang":"bash","label":"Install MLX"}],"dependencies":[],"imports":[{"note":"Provides core array operations, similar to NumPy.","symbol":"mlx.core","correct":"import mlx.core as mx"},{"note":"Offers neural network components like layers and activation functions, akin to PyTorch's `torch.nn`.","symbol":"mlx.nn","correct":"import mlx.nn as nn"},{"note":"Contains optimization algorithms for training neural networks.","symbol":"mlx.optimizers","correct":"import mlx.optimizers as optim"}],"quickstart":{"code":"import mlx.core as mx\n\n# Create an MLX array\na = mx.array([1, 2, 3], mx.float32)\nprint(f\"Array a: {a}, dtype: {a.dtype}, shape: {a.shape}\")\n\n# Perform an operation (e.g., exponential)\nb = mx.exp(a)\nprint(f\"Array b (exp(a)): {b}\")\n\n# Perform a matrix multiplication\nc = mx.array([[1, 2], [3, 4]])\nd = mx.array([[5, 6], [7, 8]])\ne = mx.matmul(c, d)\nprint(f\"Matrix c: {c}\\nMatrix d: {d}\\nMatrix product c @ d: {e}\")\n\n# Ensure computation is materialized (for lazy operations)\nmx.eval(e)\nprint(\"Computation materialized.\")","lang":"python","description":"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."},"warnings":[{"fix":"Ensure you are running on Apple Silicon for best performance, or monitor MLX's GitHub releases for updates on CUDA backend stability and feature completeness if targeting NVIDIA GPUs. Report issues if you encounter missing operations or performance cliffs.","message":"MLX is primarily optimized for Apple Silicon. While CUDA backend support is in active development, performance and feature parity on non-Apple hardware (e.g., NVIDIA GPUs on Linux) might be limited or in beta. Expect potential performance differences and missing operations.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Reshape your input tensors to NHWC format before passing them to `mlx.nn.Conv2d`. For example, a PyTorch-style NCHW tensor `x` can be converted using `x.transpose(0, 2, 3, 1)`.","message":"`mlx.nn.Conv2d` expects input images in NHWC (batch, height, width, channels) format, which differs from the NCHW (batch, channels, height, width) format commonly used by frameworks like PyTorch. This requires transposing input tensors when porting models or using data loaders that provide NCHW data.","severity":"gotcha","affected_versions":"All versions"},{"fix":"To force immediate computation and materialize an array, use `mx.eval(array)`. Be mindful of when and where you introduce `mx.eval()` to balance performance and debugging needs.","message":"MLX uses lazy computation, meaning operations build a computation graph but do not execute immediately. Arrays are only materialized when their values are needed (e.g., printed, converted to NumPy, or explicitly evaluated with `mx.eval()`). Users expecting immediate execution might be surprised by this behavior.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Refactor functions intended for `mlx.core` transformations to accept all necessary parameters as explicit inputs. For neural networks, `mlx.nn.value_and_grad()` can simplify this by handling trainable parameters of a `Module`.","message":"When using function transformations like `mlx.core.value_and_grad()`, they operate on 'pure' functions. This means that model parameters or other state should be explicitly passed as arguments to the function being transformed, rather than relying on global mutable state or attributes, to ensure correct gradient computation and graph optimization.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Verify your Python environment using `uname -p`. If it returns `i386` or `x86_64` on an Apple Silicon Mac, switch to a native `arm` Python installation, typically managed via tools like `conda` or directly installing an `arm64` Python version.","message":"For optimal performance on Apple Silicon, it is crucial to use a native `arm` Python environment. Running MLX in an `x86` Python environment via Rosetta emulation will result in significantly degraded performance.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}