feat(metal): add fused Mamba-3 SSD kernel for sub-quadratic sequence modeling#3517
Open
diysumit wants to merge 1 commit intoml-explore:mainfrom
Open
feat(metal): add fused Mamba-3 SSD kernel for sub-quadratic sequence modeling#3517diysumit wants to merge 1 commit intoml-explore:mainfrom
diysumit wants to merge 1 commit intoml-explore:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed changes
This PR introduces a custom Metal kernel for the Mamba-3 State Space Duality (SSD) architecture, providing a natively fused implementation for sub-quadratic sequence processing on Apple Silicon.
The Engineering Trade-off (Why this matters):
While the highly optimized native SDPA implementation in MLX provides excellent raw compute latency, it becomes heavily memory-bound as context lengths scale, quadratically consuming VRAM. On Apple Silicon's unified memory architecture, VRAM is often the hard ceiling for researchers running local long-context LLMs or multi-agent workflows.
This implementation prioritizes memory efficiency over raw latency. By fusing the state space updates and keeping intermediate tensors resident in threadgroup SRAM, this kernel achieves a more linear memory scaling profile. At a 32k sequence length, this implementation saves over 1.1 GB of VRAM, allowing for significantly longer context windows on machines with constrained memory (e.g., 16GB or 32GB MacBooks) without swapping to disk.
Implementation Details:
Threadgroup SRAM Fusion: Bypasses intermediate VRAM allocations for chunk-wise matrix multiplications. Using the fixed$64 \times 64$ Mamba chunk size, $Q \times K^T$ and $S \times V$ operations are resolved entirely within
threadgroupmemory.Vectorized Memory Access: Implemented
float4(128-bit) loads for the selective scan phase to better saturate M-series memory bandwidth.C++ Dispatcher (
mamba3.cpp): Integrated with the MLX backend usingget_command_encoderandset_input_arrayto ensure correct memory residency tracking.Benchmark Data (M-Series, Tested locally):
Note: Evaluated against standard MLX SDPA. While SDPA retains a latency advantage, the Mamba-3 kernel demonstrates superior memory conservation as sequence length ($L$ ) increases.
Checklist
Put an
xin the boxes that apply.I have read the CONTRIBUTING document
I have run
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesI have added tests that prove my fix is effective or that my feature works
I have updated the necessary documentation (if needed)