idol-mlx: fused MLX Metal kernels for Idol image-to-3D Gaussian splats
Fused Metal kernels for Idol (image to 3D Gaussians), for MLX on Apple
silicon (an mlx-noarch package: pure Python, kernels compiled at first use by mx.fast.metal_kernel).
Every kernel reproduces, bit for bit, the arithmetic PyTorch 2.15's MPS backend performs for the op chain it
replaces, so an MLX pipeline built on them can produce output byte-identical to the official PyTorch pipeline on the same
Mac: the MLX port of Idol that uses them writes a .ply byte-identical to the official pipeline's in its parity build.
End-to-end speed, closeness and the exact scope of that claim are in the source repository (README, docs/).
The kernels also run inside mx.compile: compiling a whole sampling step that uses them gave bit-equal output and no
measurable speed change (0.99-1.005x), because the elementwise work is already fused.
Usage
# make sure `kernels` (>= 0.19, the first release that loads mlx-metal builds) and `mlx` (>= 0.32) are installed
from kernels import get_kernel
kernel_module = get_kernel("suvadityamuk/idol-mlx", version=1, trust_remote_code=True)
resid_ln_mod = kernel_module.resid_ln_mod
| op | replaces (PyTorch MPS arithmetic reproduced exactly) |
|---|---|
resid_ln_mod(x, scale1p, shift, h=None, gate=None, repo_weight=None, repo_bias=None, eps=1e-6, eps_repo=1e-5) |
x = x + h * gate (fp16); LayerNorm32(x) * (1 + scale) + shift (adaLN); RePo's F.layer_norm(x.float(), w, b); one row read |
resid_layernorm(x, h=None, weight=None, bias=None, eps=1e-6, repo_weight=None, repo_bias=None) |
x + h, LayerNorm32 (plain or affine), optional RePo LayerNorm |
repo_rope_qk(qkv, delta, ft, fd, axis, q_gamma, k_gamma, scale) |
RePo 3D rotary angles + apply_rotary_emb (complex) + MultiHeadRMSNorm for q and k |
rmsnorm_heads(src, gamma, scale, col_offset=0) |
(F.normalize(x.float(), dim=-1) * gamma * scale).half() on 64-wide heads, with PyTorch's size-dependent reduction |
silu_mul(gate, content) |
F.silu(gate) * content (fp32) |
gelu_tanh(x) |
F.gelu(x, approximate='tanh') (fp16: fast tanh on a clamped argument; fp32: precise) |
gated_residual(x, h, gate) |
x + h * gate (fp16) |
linear_gelu(x, weight, bias) |
F.gelu(F.linear(x, weight, bias), approximate='tanh'), GELU in the GEMM epilogue (Metal 4 MetalPerformancePrimitives matmul2d; falls back to two ops where it cannot compile). Bit-equal to MLX's addmm + GELU and to PyTorch's F.linear + GELU for M >= 2048 rows (smaller M: MLX/PyTorch pick other GEMM kernels) |
linear_one_row(x, weight, bias=None) |
F.linear on one row: PyTorch's gemv_nt kernel (port) |
linear_small_rows(x, weight, bias=None) |
F.linear on 5..16 fp16 rows: the summation order of MPSNDArrayMatrixMultiplication at that size (probe-derived, macOS 26 / M5). A parity op: slower than MLX's addmm (0.82x) |
sdpa_decode(q, k, v, scale) |
F.scaled_dot_product_attention for query length <= 8 and key length >= 1024: PyTorch's sdpa_vector_2pass (port). A parity op: slower than mx.fast.scaled_dot_product_attention (0.70x) |
Arguments are validated (dtype, rank, sizes); inputs are made row-contiguous before a kernel runs.
Tested hardware and limits
- Tested on one machine only: Apple M5 Pro, 64 GB, macOS 26.6.2, MLX 0.32.4, PyTorch 2.15.0. Nothing else was tested.
linear_geluneeds Metal 4 (macOS 26) for the fused path; elsewhere it falls back to two ops (untested).- "Bit-identical to PyTorch MPS" was verified on that machine only, by byte comparison against PyTorch (the package
tests do this when PyTorch is installed: 94/94 pass).
linear_small_rowsmodels a vendor library's choice on this GPU and OS (derived by probing) and is the most likely to differ elsewhere. - Loading through
kernelswas tested withget_local_kernel(kernels 0.19.0, kernel-builder's environment); the Hub path (get_kernel, kernels 0.19.0 from PyPI) passed the same 94 tests on this repository's copy. kernels benchmark(0.19) supports PyTorch kernels only;benchmarks/benchmark.pyis a standalone MLX script.- The ops are specific to Idol's shapes (width 1024, head dimension 64).
Using them for the whole model
The source repository, github.com/suvadityamuk/idol-mlx, has
scripts/infer.py: one command from an image to a .ply. Its fast build was 1.69-1.88x faster end to end than the
official PyTorch pipeline on the tested machine; its parity build writes a .ply byte-identical (sha256) to the
official pipeline's on MPS on the four fixed configs. In both, the front end (background removal and the two image
encoders) still runs the official PyTorch modules on MPS; the no-PyTorch path is experimental and outside tolerance.
Benchmarks
End to end, seconds per request (image path in, Gaussians out; load, warm-up and the PLY write excluded), for the source repository's MLX pipeline built on these kernels against the official PyTorch pipeline on MPS:
| config | official | official, torch.compile |
parity (bit-exact) | fast |
|---|---|---|---|---|
| robot 262k, round 1 | 149.9 | 126.8 | 110.6 | 88.7, 1.69x |
| avocado 262k | 167.2 | 131.5 | 112.2 | 89.6, 1.87x |
| robot 262k, round 2 | 167.4 | 132.0 | 112.3 | 88.9, 1.88x |
| robot 1M | 172.2 | 135.6 | 117.0 | 99.6, 1.73x |
| avocado 1M | 177.8 | 144.4 | 125.0 | 103.9, 1.71x |
Apple M5 Pro, 64 GB, macOS 26.6.2, MLX 0.32.4, PyTorch 2.15.0, idol-sdk 8db7003; seed 42, 50 steps, cfg 3.0. One process per row, order flipped between configs, one measurement per row; speed-up against the official pipeline in the same row (its own time drifted from 149.9 s to 167.4 s over the session, so compare within a row). Peak process footprint: 12.2-12.4 GB (official: 10.4 GB).
Per op:
python benchmarks/benchmark.py times each op against the MLX op chain it replaces at the model's shapes (Apple M5
Pro: repo_rope_qk 25x, gelu_tanh 15x, rmsnorm_heads 5.6x, resid_ln_mod 2.7x, gated_residual 1.45x,
silu_mul 1.4x, linear_one_row 1.13x faster than the MLX chain; the two parity ops above are slower).
Related
Part of Idol everywhere, built on Speri Labs' Idol.
| Hugging Face | GitHub | |
|---|---|---|
| Official | model · Space | idol-sdk |
| Diffusers port | idol-diffusers-3d | diffusers-3d |
| NVIDIA (CUDA) | kernels · Space | idol-cuda |
| Apple Silicon (PyTorch MPS + Metal) | kernels | idol-metal |
| Apple Silicon (MLX) | kernels | idol-mlx |
Source and licence
Ported from the idol-metal kernels of the same project; portions mirror PyTorch's MPS kernels (BSD-3-Clause, Copyright (c) Meta Platforms, Inc. and affiliates). Source: https://github.com/suvadityamuk/idol-mlx.
- Downloads last month
- -