Skip to content

feat(models): port GraniteMoE (granitemoe, GraniteMoeForCausalLM) - #3745

Open
Jaeyeong-CHOI wants to merge 1 commit into
lablup:mainfrom
Jaeyeong-CHOI:feat/granitemoe
Open

Jaeyeong-CHOI wants to merge 1 commit into
lablup:mainfrom
Jaeyeong-CHOI:feat/granitemoe

Conversation

@Jaeyeong-CHOI

Copy link
Copy Markdown
Contributor

Summary

Closes #3743.

Ports IBM GraniteMoE (model_type: granitemoe, GraniteMoeForCausalLM), the Granite 3.0 / 3.1 sparse-MoE decoders that #245 deferred for lack of an MLX checkpoint. On main such a checkpoint is refused:

$ mlxcel generate -m granite-3.0-1b-a400m-instruct -p Hello -n 8
Error: Unsupported model type: granitemoe

With this change granite-3.0-1b-a400m-instruct and granite-3.1-3b-a800m-instruct load in mlxcel generate and mlxcel-server and are listed by mlxcel arch, and they track mlx-lm on 4-bit mlx_lm.convert exports (greedy continuations teacher-forced, and full logit traces).

What changed

  • src/models/granitemoe.rs (new). The block is the dense Granite block with the MLP replaced by block_sparse_moe, so it reuses granite::ModelArgs (flattened) and granite::Attention unchanged (the four scalar multipliers; attention_multiplier is the SDPA scale) and the shared SwitchGLU. The router mirrors mlx-lm v0.32.2 granitemoe.py: argpartition top-k, a softmax over only the selected logits in float32, and a float32 weighted sum cast back to the expert dtype. Sanitize splits the original release's fused block_sparse_moe.input_linear [E, 2F, H] along axis 1 (gate rows first) and renames output_linear to switch_mlp.down_proj; an mlx_lm.convert export already ships switch_mlp.* and passes through. A quantized fused input_linear is a load error, since it cannot be sliced (the same rule granitemoehybrid follows). ModelArgs::validate refuses configs that would otherwise abort inside MLX: num_experts_per_tok of 0 or above num_local_experts, zero experts, zero heads, heads not a multiple of KV heads, and a non-default rope_scaling (every published GraniteMoE config has rope_scaling: null). Native batched decode through the shared split-attention path.
  • Registration: ModelType::GraniteMoe, detection of granitemoe, the registry / metadata / loaded-model tables, memory_estimate, the tensor-parallel fallback name (not shardable: the generic plan names neither the router nor switch_mlp; the refusal comes from validate_supported_runtime, whose runtime_kind_for has no GraniteMoe arm), and the batched-decode coverage pins.
  • Used by: annotations on SwitchGLU::from_weights, SwitchGLU::check_input_widths and mamba::parse_eos_token_ids; the last two also lacked existing callers (Omoikane for check_input_widths; Falcon-H1, PLaMo 2 and GraniteMoeHybrid for parse_eos_token_ids).
  • docs/supported-models.md: a GraniteMoE entry, which says only the 4-bit exports are validated; the "not yet ported" sentence in the Granite 4 entry is gone. No CHANGELOG entry: that is written at release time.
  • Tests. src/models/granitemoe_tests.rs: config parsing of the published 1b-a400m config and of a transformers-5 rope_parameters block; validate at top-k 0, 1, E and E + 1, zero experts, uneven KV heads, scaled RoPE and a missing field; sanitize split order and its two refusals; the MoE block against a host reference at fan-outs 1, 2 and E (E = 4); an untied head; native batched decode against sequential decode at different offsets; last-row logits against a full and a chunked prefill; a one-layer forward (four tokens, so RoPE and the causal softmax both matter) against a host reference with distinct values for the four multipliers (embedding 0.5, attention 2.0, residual 0.4, logits scaling 2.5), which also checks that replacing any one multiplier by its neutral value (1, or 1/sqrt(head_dim) for attention) moves the host reference far outside the tolerance. src/models/detection_tests.rs: granitemoe is detected apart from granite and granitemoehybrid, and granitemoeshared (which adds a shared expert this port does not load) stays Unsupported model type. tests/granitemoe_parity.rs: the real-checkpoint gate below.

Verification (NVIDIA GB10, sm_121, CUDA 13; mlx-lm 0.32.2, mlx 0.32.5; Rust 1.97.1)

Checkpoints: ibm-granite/granite-3.0-1b-a400m-instruct (revision ffec3c35, 32 experts, top-8, 24 layers, tied head) and ibm-granite/granite-3.1-3b-a800m-instruct (revision a0278068, 40 experts, top-8, 32 layers, tied head), each converted with mlx_lm.convert -q (4-bit, group 64; mlx-lm's quant_predicate keeps the router, o_proj and the expert down_proj at 8 bits, so the exports are mixed-width).

Reference ids: mlx-lm greedy decode on the same export from the same prompt ids (one prefill, then single-token argmax steps with no EOS stop), on a 26/77-token chat prompt and a 534/585-token one (the 3.1 template adds a default system turn).

tests/granitemoe_parity.rs on this branch (MLX_ENABLE_TF32=0 MLXCEL_REQUIRE_MODELS=1 MLXCEL_TEST_GRANITEMOE_A400M_DIR=... MLXCEL_TEST_GRANITEMOE_A800M_DIR=... cargo test --profile test-fast --features cuda --test granitemoe_parity -- --test-threads=1 --nocapture), teacher-forced over all 64 reference tokens:

checkpoint prompt mlx-lm token is mlxcel's argmax largest deficit
1b-a400m 4-bit 26 tokens 61/64 0.375 (step 43)
1b-a400m 4-bit 534 tokens 64/64 0
3b-a800m 4-bit 77 tokens 63/64 0.125 (step 46)
3b-a800m 4-bit 585 tokens 63/64 0.25 (step 12)

The same numbers on the final tree of this PR, except that one of three runs of the 585-token case measured 0.125 at step 12 instead of 0.25.

The gate is not vacuous. With the top-k renormalisation of the gates removed (softmax over all experts, then the top-k weights kept as they are; a local edit, not part of this PR) and the test rebuilt, both cases fail on their short prompt: the 1b-a400m keeps mlx-lm's token as argmax at 32 of 64 steps with the largest deficit 12.125 (step 19), the 3b-a800m at 16 of 64 with 20.86 (step 39).

Free-running greedy against the same references matches 64/64 tokens on the 1b long prompt and parts at steps 32 (1b short), 46 (3b short) and 12 (3b long), each at a step where mlx-lm's token is not mlxcel's argmax in the teacher-forced run above.

Teacher-forced logit traces over docs/architecture.md (scripts/mlx_lm_logit_trace.py vs the logit_trace example, compared with scripts/compare_logit_traces.py):

checkpoint arm positions top-1 disagreements on decided positions (gap ≥ 2) largest gap at a disagreement perplexity mlx-lm / mlxcel
1b-a400m 4 × 256-token chunks 1024 25 (2.4%) 0 / 363 0.500 27.929 / 27.836 (-0.33%)
1b-a400m 512-token context + 256 single steps 768 17 (2.2%) 0 / 297 0.375 19.291 / 19.320 (+0.15%)
3b-a800m 4 × 256-token chunks 1024 25 (2.4%) 0 / 402 0.250 22.375 / 22.407 (+0.15%)
3b-a800m 512-token context + 256 single steps 768 27 (3.5%) 0 / 329 0.375 16.187 / 16.228 (+0.25%)

mlxcel generate -m <export> -p "Explain in three sentences why the sky is blue during the day and red at sunset." -n 64 --temp 0 prints a coherent answer on both exports ("The sky appears blue during the day due to a particular type of scattering called Rayleigh scattering. ..."). mlxcel arch lists "GraniteMoE (Granite 3.x sparse MoE)" under Granite. mlxcel-server -m <1b export> answers a /v1/chat/completions request for the same question at temperature 0 with the same opening sentence (26 prompt tokens, 48 completion tokens).

Gates. The full suite ran on an earlier revision of this branch; the final revision adds only tests, comments and documentation on top of it: the one-layer multiplier unit test, the granitemoeshared assertion in the detection test, the measured values in the MAX_REFERENCE_DEFICIT comment, the Used by: line of SwitchGLU::check_input_widths, the tensor-parallel arm's comment, the mlx-lm link in the module doc (pinned to v0.32.2), and the docs/supported-models.md wording (the CHANGELOG hunk of the gated revision was dropped). No non-test code differs from the gated revision.

  • On the final tree: cargo fmt --all -- --check clean; cargo clippy --locked --workspace --all-targets --features cuda -- -D warnings clean; python3 scripts/ci/check_used_by.py passes (used-by: OK, 78 top-level pub fn in 2 shared files, 57 annotated).
  • On the final tree, lib unit tests (--profile test-fast --features cuda, --test-threads=1) filtered by granitemoe (35 passed, including the ten models::granitemoe tests and the detection test), models::detection (118 passed), batched_decode_coverage (14 passed) and distributed::tensor_parallel (368 passed, 4 ignored); tests/granitemoe_parity.rs with both checkpoints (2 passed, numbers above) and without them (2 passed, both skip). The new multiplier test matches the host reference within 1e-4, and replacing one multiplier by its neutral value moves the host reference by 0.0227 (embedding), 0.0155 (attention), 0.0471 (residual) and 0.282 (logits scaling), against a 0.01 bound.
  • On the final tree, bash of each scripts/ci/*_test.sh (17 scripts): all pass except helm_chart_package_test.sh, which needs a helm binary the test machine does not have.
  • On the gated revision, the make verify-test-cuda suite (cargo test --workspace --profile test-fast --features cuda --no-fail-fast -- --test-threads=1), run one test target at a time with the same profile, features and flags (the mlxcel and mlxcel-core lib targets split into groups by top-level module, --exact), totals summed from libtest's per-target summaries: 200 targets, 18,898 passed, 2 failed, 568 ignored (19,468, the number of listed tests); doctests 3 passed, 10 ignored. The two failures are tests/mlx_test_guard_coverage.rs::mlx_evaluating_lib_tests_take_mlx_test_guard (it flags src/vision/encoders/nemotron_h_nano_omni_tests.rs:276) and tests/step3p7_parity.rs::text_decode_delegates_sequence_local_native_batching (assertion failed: !wrapper.supports_padded_prefill()). Both fail identically on unmodified main (9402cf8); they are the two stale tests test: fix two stale tests that keep make verify-test red #3720 fixes.
  • git merge-tree --write-tree origin/main <branch>: clean against 9402cf8.

Limits

  • Only the 4-bit exports are validated. On CUDA (GB10) the unquantized bf16 releases decode incoherently in both mlx-lm (bf16) and mlxcel, while mlx-lm with the model cast to float32 decodes the same release coherently. On a teacher-forced logit trace of the bf16 1b release (4 × 256 tokens) the two disagree on top-1 at 91 of 1024 positions (8.9%), on 2 of 675 decided positions (largest gap at a disagreement 2.188), with perplexity 16,199,537 (mlx-lm) and 16,869,820 (mlxcel, +4.1%): both are incoherent in the same way. The cause was not investigated, and Metal was not measured.
  • The 8-bit layers of an mlx-lm export load through the shape-derived quantization fallback, which logs one "quantized layout does not match declared metadata" warning per such layer (two per decoder layer here), as for any mlx-lm export with per-path bit overrides.
  • No tensor parallelism. Only the instruct 1b-a400m and 3b-a800m checkpoints were run; the base variants share the config schema.
  • This PR and the Granite 4 MoE validation PR both edit the Granite 4 line of docs/supported-models.md; whichever lands second needs a trivial rebase.

IBM's Granite 3.0 / 3.1 sparse-MoE checkpoints were refused as an unsupported
model type. The block is the dense Granite block with the MLP replaced by a
top-k softmax router and SwitchGLU experts, mirrored from mlx-lm's
granitemoe.py; the original release's fused input_linear is split into
gate/up at load. Pinned against mlx-lm on 4-bit exports of
granite-3.0-1b-a400m and granite-3.1-3b-a800m.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(models): port GraniteMoE (granitemoe, GraniteMoeForCausalLM)

1 participant