Skip to content

feat(models): add GraniteMoeHybrid (Granite 4.x Mamba2 + attention hybrid) - #259

Merged
inureyes merged 1 commit into
mainfrom
feat/issue-245-granitemoehybrid
Jun 13, 2026
Merged

inureyes merged 1 commit into
mainfrom
feat/issue-245-granitemoehybrid

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Implement IBM Granite 4.x (granitemoehybrid), an interleaved Mamba2 (SSM) + GQA attention hybrid with the four Granite scalar multipliers (embedding, attention, residual, logits). Each GraniteMoeHybridLayer is either a Mamba2 mixer or an attention mixer, selected by layer_types[i]. Both the dense-MLP mode and the MoE mode (block_sparse_moe + shared_mlp) are implemented in one file, selected by num_local_experts. This completes the hybrid scope of the Granite port (the dense granite variant merged earlier in #254).

What changed

  • src/models/granitemoehybrid.rs (new): config plus the Mamba2 mixer (reuses the Falcon-H1 SSD scan and the fused ssm_update_kernel decode path), the always-on gated RMSNorm (gate before a plain full-weight rms_norm, no n_groups grouping), NoPE-aware GQA attention with the attention_multiplier SDPA scale, dense vs MoE feed-forward, the per-layer Mamba | Attention enum 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_types interleave, use_moe, NoPE flag, mamba dims, conv_dim / projection_size, EOS).
  • Registered as a Nonstandard + Special model (like PLaMo 2 / Falcon-H1) across detection.rs, models/mod.rs, model_metadata.rs, loaded_model.rs, loading/nonstandard.rs, loading/special.rs, and the distributed tensor_parallel/inference.rs name map.
  • docs/supported-models.md: documented under the Granite family.

Test plan

  • cargo check --release --features metal,accelerate --lib
  • cargo clippy --release --features metal,accelerate --lib -- -D warnings
  • cargo fmt --check
  • Real-model generation against mlx-community/granite-4.0-h-350m-4bit (dense-MLP hybrid, NoPE), pending maintainer validation on Apple Silicon (no Metal device in CI).

Notes

  • The granite-4.1-3b-4bit checkpoint is model_type: granite (the dense variant handled by granite.rs), so it does not exercise this hybrid; the dense-MLP hybrid path is validated by granite-4.0-h-350m-4bit.
  • The MoE-hybrid feed-forward mirrors the upstream reference but currently has no public MLX checkpoint for end-to-end validation. The non-hybrid granitemoe variant 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

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 inureyes added status:review Under review type:enhancement New features, capabilities, or significant additions priority:high High priority area:models Model architectures, weights, loading, metadata status:done Completed labels Jun 13, 2026
@inureyes
inureyes merged commit 926f9a4 into main Jun 13, 2026
4 checks passed
@inureyes
inureyes deleted the feat/issue-245-granitemoehybrid branch June 13, 2026 08:16
@inureyes inureyes added this to the 0.3 milestone Jun 21, 2026
@inureyes inureyes self-assigned this Aug 31, 2026
@inureyes inureyes removed the status:review Under review label Sep 6, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:models Model architectures, weights, loading, metadata priority:high High priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(models): add IBM Granite family (granite, granitemoe, granitemoehybrid)

1 participant