PyTorch accelerates mish on Apple Silicon with 15x faster Metal kernel
New Metal kernel for mish yields up to 15.2x speedup on strided tensors.
PyTorch’s latest commit (187906) replaces the MPSGraph-based implementation of the mish activation function on Apple Silicon with a direct Metal kernel. Previously, mish was the only elementwise activation on MPS relying on Apple’s MPSGraph framework, which rebuilds and caches a graph for every call and stages non‑contiguous data through a gather+copy. This overhead made mish significantly slower than sibling activations like silu and gelu, which already used Metal kernels. The new Metal functors mirror the silu implementation and are registered via the `activation_stub` dispatch path.
Benchmarks on an M‑series GPU show dramatic improvements: forward pass on a 2048×2048 strided tensor went from 4.518 ms to 0.297 ms (15.2× faster), and contiguous forward dropped from 1.243 ms to 0.296 ms (4.2×). Combined forward+backward benchmarks also improved by 3–10×. Correctness was validated against a CPU float64 reference, with maximum absolute errors in the range of 1e‑7 for float32 and 1e‑3 for float16. The change adds only 39 lines and removes 124, making PyTorch’s MPS backend more efficient and consistent.
- Up to 15.2x speedup on strided 2048×2048 forward pass (4.518 ms → 0.297 ms).
- 4.2x speedup on contiguous 2048×2048 forward (1.243 ms → 0.296 ms).
- Correctness verified: float32 max error 5.3e-07, float16 max error 1.9e-03 vs. CPU reference.
Why It Matters
PyTorch users on Apple Silicon get faster training/inference for mish activations with no code changes.