Skip to content

Add Mamba3 - #1021

Open
Goekdeniz-Guelmez wants to merge 11 commits into
ml-explore:mainfrom
Goekdeniz-Guelmez:add-mamba3
Open

Goekdeniz-Guelmez wants to merge 11 commits into
ml-explore:mainfrom
Goekdeniz-Guelmez:add-mamba3

Conversation

@Goekdeniz-Guelmez

Copy link
Copy Markdown
Contributor

No description provided.

- Implemented compute_dt_eff function for effective time delta calculation.
- Enhanced ssm_attn to support effective dt for input weighting.
- Introduced ssm_update_trap for Mamba-3 SSM updates with trapezoidal discretization.
- Added new files for exponential Euler and exponential trapezoidal SSM implementations.
- Updated existing SSM functions to accommodate new Mamba-3 features and maintain backward compatibility.
@Goekdeniz-Guelmez

Copy link
Copy Markdown
Contributor Author

Do some weights on HF exist to test?

rromenskyi pushed a commit to ipsupport-llc/mlx-lm that referenced this pull request Oct 17, 2026
Its prefill doesn't match its own decode recurrence: the next token's
input term lands on the current token's output (scalar check: prefill
1.0 vs decode 0.5), and a multi-token continuation ignores the carried
prev_Bx, so chunked prefill and prompt-cache continuation drift. Left out
until upstream fixes it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XhEwWJCoJPXFyyCzp988Bx
@Goekdeniz-Guelmez

Copy link
Copy Markdown
Contributor Author

Found an official model! aifeifei798/Mamba3-MIMO-Tiny-HF

@Goekdeniz-Guelmez

Goekdeniz-Guelmez commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Had to remove some unneeded stuff in aifeifei798/Mamba3-MIMO-Tiny-HF into Goekdeniz-Guelmez/Mamba3-MIMO-Tiny-HF. Official MIMO and SISO where used too for the config.json, but cant load them since they are non transformer compatible versions. Both SISO and MIMO use model_type="mamba3"; ssm_cfg.is_mimoselects the official variant.mamba3_customis accepted as an alias. Native configs that omitmodel_typeare recognized byssm_cfg.layer="Mamba3"`. Mamba 3 has data-dependent heavy-tail decay, softplus time steps, trapezoidal recurrence. B/C RMSNorm and per-head biases, bounded data-dependent rotary angles. MIMO uses split-half rotary pairs and learned input, gate and output projections. SISO uses adjacent rotary pairs and no MIMO projection weights.

Reference code:

Mamba3 module,
decode reference,
MIMO prefill,
residual blocks.

This vs. official torch reference.

Output FP32 max absolute error FP32 relative L2 error Mixed precision max absolute error Mixed precision relative L2 error
Layer 0 5.01e-6 6.87e-7 0.015625 0.002053
Layer 1 1.62e-5 1.89e-6 0.03125 0.004511
Final norm 6.08e-6 9.60e-7 0.028132 0.004447
Logits 7.17e-6 7.01e-7 0.028093 0.002874

Top-1 predictions agree at all 229 positions in both precision modes. The final top-10 token IDs are identical too.

inference

python -m mlx_lm generate --model Goekdeniz-Guelmez/Mamba3-MIMO-Tiny-HF --prompt "Mamba three is" --temp 0
==========
even faster with MIMO logic.
==========
Prompt: 4 tokens, 247.326 tokens-per-sec
Generation: 9 tokens, 950.353 tokens-per-sec
Peak memory: 0.108 GB

- Removed the old Mamba3 model implementation and replaced it with a new architecture that includes significant changes to the model arguments and structure.
- Updated the SSM (State Space Model) implementation to support the new Mamba3 architecture, including the removal of deprecated functions and parameters.
- Introduced a new `Backbone` class to encapsulate the model layers and embedding logic.
- Added unit tests for the new Mamba3 model, including tests for configuration loading, routing, rotary pairs, cached decoding, padding, quantization, and dimension validation.
- Updated utility functions to support the new model configuration and ensure compatibility with existing code.
@Goekdeniz-Guelmez
Goekdeniz-Guelmez marked this pull request as ready for review October 26, 2026 16:37
@Goekdeniz-Guelmez

Copy link
Copy Markdown
Contributor Author

@michalk8 should be mergeable now!

@Goekdeniz-Guelmez

Copy link
Copy Markdown
Contributor Author

Or should I also add the custom kernel too?

@michalk8
michalk8 self-requested a review October 27, 2026 19:51

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants