Repository navigation
feat(models): add GraniteMoeHybrid (Granite 4.x Mamba2 + attention hybrid) - #259
Merged
Merged
Conversation
Implement IBM Granite 4.x (`granitemoehybrid`), an interleaved Mamba2 (SSM) + GQA attention hybrid with the four Granite scalar multipliers (embedding, attention, residual, logits). Each layer is either a Mamba2 mixer or an attention mixer, selected by `layer_types`. Both the dense-MLP mode and the MoE mode (`block_sparse_moe` + `shared_mlp`) live in one file, selected by `num_local_experts`, completing the hybrid scope of the Granite port (the dense `granite` variant landed in #254). The Mamba2 mixer reuses the Falcon-H1 SSD scan and the fused-kernel decode path. Two Granite differences: the post-scan gate is always the gated RMSNorm (gate before a plain full-weight rms_norm, no n_groups grouping), and attention defaults to NoPE (position_embedding_type == "nope") with attention_multiplier as the SDPA scale. The model owns a per-layer Mamba or Attention enum cache and reports supports_batching == false, like Falcon-H1 / PLaMo 2. Registered as a Nonstandard + Special model across detection, model metadata, the loaded-model enum, both loaders, and the distributed name map. Adds checkpoint-free config-geometry tests and documents the family in docs/supported-models.md. The MoE-hybrid path is implemented to mirror the upstream reference but currently has no public MLX checkpoint to validate; the dense-MLP hybrid is validated against mlx-community/granite-4.0-h-350m-4bit.
inureyes
added a commit
that referenced
this pull request
Sep 7, 2026
`ssm_step` ended with `eval(&y)` and `eval(&next_state)` in both `granitemoehybrid.rs` and `falcon_h1.rs`. They arrived with the original GraniteMoeHybrid port (926f9a4, #259) with no recorded reason, and they are not the M5 NaN workaround #1685 removed, which sat in the mixer and was hardware-gated. They only ever fire on the graph path. The dispatch takes `ssm_step_kernel` when `seq_len == 1`, the SSM Metal kernel is available and a state exists, so steady-state decode never reaches them; prefill and the first token do. That predicts a prefill-only gain, and prefill is where these models were furthest behind: `granite-4.0-h-350m-4bit` was at 68% of mlx-lm on prefill against 84% on decode. Measured on M5 Max at pp512/tg128, prefill tok/s then decode tok/s: - `falcon-h1-tiny-90m-instruct-4bit` prefill 15571.52 to 28413.33, 1.82x. Decode 589.34 to 600.92. - `granite-4.0-h-350m-4bit` prefill 8901.12 to 12465.26, 1.40x. Decode 477.00 to 480.75. - `granite-4.0-h-tiny-4bit` prefill 4347.94 to 5244.57, 1.21x. Decode 210.45 to 208.48. Decode moves by 1% or less in either direction, which is the prediction the dispatch makes and the reason to trust the attribution. Validated on real checkpoints. Greedy output at `--temp 0` is byte-identical before and after on all three, for a short prompt and for a 40-repetition prompt that puts the graph path under a long prefill, compared against a build with both files reverted. `tests/mamba2_hybrid_decode_finite.rs`, the regression test #1685 added for exactly this class of change, passes on M5 with `--ignored`: `falcon_h1_decode_is_finite` and `granitemoehybrid_decode_is_finite` both ok. `cargo clippy --all-targets --features metal,accelerate -- -D warnings` and `cargo fmt --check` pass.
be-free-snowboy
pushed a commit
to freestylejs/snowbrain-mlxcel
that referenced
this pull request
Oct 27, 2026
…p#259) Implement IBM Granite 4.x (`granitemoehybrid`), an interleaved Mamba2 (SSM) + GQA attention hybrid with the four Granite scalar multipliers (embedding, attention, residual, logits). Each layer is either a Mamba2 mixer or an attention mixer, selected by `layer_types`. Both the dense-MLP mode and the MoE mode (`block_sparse_moe` + `shared_mlp`) live in one file, selected by `num_local_experts`, completing the hybrid scope of the Granite port (the dense `granite` variant landed in lablup#254). The Mamba2 mixer reuses the Falcon-H1 SSD scan and the fused-kernel decode path. Two Granite differences: the post-scan gate is always the gated RMSNorm (gate before a plain full-weight rms_norm, no n_groups grouping), and attention defaults to NoPE (position_embedding_type == "nope") with attention_multiplier as the SDPA scale. The model owns a per-layer Mamba or Attention enum cache and reports supports_batching == false, like Falcon-H1 / PLaMo 2. Registered as a Nonstandard + Special model across detection, model metadata, the loaded-model enum, both loaders, and the distributed name map. Adds checkpoint-free config-geometry tests and documents the family in docs/supported-models.md. The MoE-hybrid path is implemented to mirror the upstream reference but currently has no public MLX checkpoint to validate; the dense-MLP hybrid is validated against mlx-community/granite-4.0-h-350m-4bit.
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.
Summary
Implement IBM Granite 4.x (
granitemoehybrid), an interleaved Mamba2 (SSM) + GQA attention hybrid with the four Granite scalar multipliers (embedding, attention, residual, logits). EachGraniteMoeHybridLayeris either a Mamba2 mixer or an attention mixer, selected bylayer_types[i]. Both the dense-MLP mode and the MoE mode (block_sparse_moe+shared_mlp) are implemented in one file, selected bynum_local_experts. This completes the hybrid scope of the Granite port (the densegranitevariant merged earlier in #254).What changed
src/models/granitemoehybrid.rs(new): config plus the Mamba2 mixer (reuses the Falcon-H1 SSD scan and the fusedssm_update_kerneldecode path), the always-on gated RMSNorm (gate before a plain full-weightrms_norm, non_groupsgrouping), NoPE-aware GQA attention with theattention_multiplierSDPA scale, dense vs MoE feed-forward, the per-layerMamba | Attentionenum cache, and the model-owned sequence-state trait impl (supports_batching == false).src/models/granitemoehybrid_tests.rs(new): checkpoint-free config-geometry tests (multipliers,layer_typesinterleave,use_moe, NoPE flag, mamba dims,conv_dim/projection_size, EOS).detection.rs,models/mod.rs,model_metadata.rs,loaded_model.rs,loading/nonstandard.rs,loading/special.rs, and the distributedtensor_parallel/inference.rsname map.docs/supported-models.md: documented under the Granite family.Test plan
cargo check --release --features metal,accelerate --libcargo clippy --release --features metal,accelerate --lib -- -D warningscargo fmt --checkmlx-community/granite-4.0-h-350m-4bit(dense-MLP hybrid, NoPE), pending maintainer validation on Apple Silicon (no Metal device in CI).Notes
granite-4.1-3b-4bitcheckpoint ismodel_type: granite(the dense variant handled bygranite.rs), so it does not exercise this hybrid; the dense-MLP hybrid path is validated bygranite-4.0-h-350m-4bit.granitemoevariant stays out of scope for the same reason: no public MLX checkpoint exists to validate it.Reference: ml-explore/mlx-lm
mlx_lm/models/granitemoehybrid.py.Closes #245