Fused Metal kernels for SSM (Mamba) and GLA recurrence — MLX-Recurrence #3377
D-CSIL
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
MLX currently lacks native scan/recurrence primitives, which means
SSM (Mamba) and GLA (Gated Linear Attention) models have to fall back
to Python for-loops for the sequential scan — a major bottleneck that
makes training these architectures on Apple Silicon impractical.
I built MLX-Recurrence to fix this. Fused Metal GPU kernels for two
recurrence patterns using mx.fast.metal_kernel:
Both include custom VJP implementations so they work with
nn.value_and_grad for training — not just inference.
At seq_len=2048 on M3 Max, kernel-level benchmarks:
End-to-end training throughput improvement is ~3x, as the scan
kernels are one component of the full training step.
Repo: https://github.com/D-CSIL/mlx-recurrence
Install: pip install git+https://github.com/D-CSIL/mlx-recurrence.git
Would love to see native scan primitives in MLX eventually — happy to
take feedback from the MLX team on kernel optimization and share what
I learned about Metal kernel constraints along the way.
For context: as far as I can find, this is the first implementation of
fused Metal backward kernels for any recurrence pattern on MLX. The
existing Mamba/SSM implementations in mlx-lm and other projects use
Metal kernels for inference only — training falls back to pure MLX ops
or Python loops.
MLX-Recurrence provides full forward + backward Metal kernels with VJP
support for both SSM selective scan and GLA. The GLA implementation
appears to be the first on the MLX platform entirely.
All reactions