Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/supported-models.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ Implemented model families include:
- ERNIE 4.5 and ERNIE 4.5 MoE
- Hunyuan dense and MoE variants (`hunyuan_v1_dense` and `hunyuan_moe`, Tencent: separate implementations sharing only the generic core layers, both with per-head Q/K RMSNorm applied after the rotation and a DynamicNTK-alpha rope base folded in once at load (`rope_theta * alpha^(d/(d-2))`) rather than recomputed per forward; the MoE adds softmax routing over `num_experts` with per-layer `moe_topk`, an optional shared MLP gated by `use_mixed_mlp_moe`, and optional cross-layer attention (`use_cla`) where a layer reuses an earlier layer's K/V but still runs its own SDPA, which is why the mask policy has to live in the attention leaf rather than in the CLA plumbing above it. Both satisfy the maskless-prefill contract by routing a multi-token prefill with no caller mask through `causal_attention`. Validation checkpoint for the dense family: `tencent/Hunyuan-1.8B-Instruct` as a 4-bit MLX conversion, token-exact against the mlx-lm 0.31.3 greedy reference on a 36-token chat-templated prompt and pinned by `tests/causal_prefill_greedy_parity.rs`. That checkpoint degenerates on raw text completion under the reference itself, so a coherent continuation needs the chat template; the parity gate is on prompt ids either way. The MoE family has no runnable reference: the only local checkpoint is `mlx-community/Hunyuan-A13B-Instruct-4bit` at 42 GB and its bf16 sibling does not fit the development machine, so it carries the reference-free causality gate only, opt-in behind `MLXCEL_TEST_HUNYUAN_MOE=1` so a 42 GB load stays off the default test run.)
- IBM Granite dense (`granite`)
- IBM Granite 4.x hybrid (`granitemoehybrid`: interleaves Mamba2 SSM and GQA attention layers by `layer_types`, applies the four Granite scalar multipliers (embedding, attention, residual, logits), and defaults to NoPE attention. The dense-MLP mode is validated against `mlx-community/granite-4.0-h-350m-4bit`; the MoE mode (`block_sparse_moe` + `shared_mlp`) is implemented but awaits a public MLX checkpoint to validate. The non-hybrid `granitemoe` variant is not yet ported.)
- IBM Granite 4.x hybrid (`granitemoehybrid`: interleaves Mamba2 SSM and GQA attention layers by `layer_types`, applies the four Granite scalar multipliers (embedding, attention, residual, logits), and defaults to NoPE attention. The dense-MLP mode is validated against `mlx-community/granite-4.0-h-350m-4bit`; the MoE mode (`block_sparse_moe` + `shared_mlp`) is implemented but awaits a public MLX checkpoint to validate.)
- IBM GraniteMoE (`granitemoe`, `GraniteMoeForCausalLM`: the Granite 3.0 / 3.1 sparse-MoE decoders. The dense Granite block and its four scalar multipliers with the MLP replaced by a top-k softmax router (softmax over only the selected logits, in float32) and a `SwitchGLU`; the original release's fused `block_sparse_moe.input_linear` is split into gate/up at load, as mlx-lm's `sanitize` does. Validated against mlx-lm 0.32.2 on 4-bit `mlx_lm.convert` exports of `ibm-granite/granite-3.0-1b-a400m-instruct` and `ibm-granite/granite-3.1-3b-a800m-instruct`, pinned by `tests/granitemoe_parity.rs`. Only the 4-bit exports are validated: on CUDA (GB10) the unquantized bf16 release decodes incoherently under mlx-lm as well, while mlx-lm in float32 is coherent; Metal was not measured. No tensor parallelism.)
- BitNet b1.58 (`bitnet`, Microsoft: a Llama-style transformer whose every projection is a `BitLinear` with 1.58-bit ternary weights ({-1, 0, +1}) packed 4-per-uint8 and scaled by a single per-tensor `weight_scale`. A custom Metal kernel (`bitlinear_matmul`) multiplies directly on the packed bytes, so the unpacked weights never materialize. Two extra sub-norms (`attn_sub_norm` before `o_proj`, `ffn_sub_norm` inside the MLP) and a squared-ReLU MLP (`relu2(gate) * up`). Runs in native bf16 (its squared-ReLU overflows f16), bypassing the Apple-Silicon f16 conversion. Validated against `mlx-community/bitnet-b1.58-2B-4T` and its `-4bit` variant, which additionally affine-quantizes the embedding/lm_head to 4-bit (the BitLinear weights stay ternary); keeping the whole model bf16 also keeps that 4-bit dequant dtype-consistent.)
- ExaOne / ExaOne 4 / ExaOne MoE / Solar Open
- OLMo / OLMo2 / OLMo3 / OLMoE
Expand Down
7 changes: 7 additions & 0 deletions src/distributed/tensor_parallel/inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ fn fallback_architecture(model_type: ModelType) -> &'static str {
ModelType::Apertus => "apertus",
ModelType::SeedOss => "seed_oss",
ModelType::Granite => "granite",
// Not shardable by the generic transformer plan: the experts live
// under `block_sparse_moe.switch_mlp` with a Granite router, neither
// of which the shard rules name. This arm only keeps the dispatch
// table total; the refusal comes from `validate_supported_runtime`,
// whose `runtime_kind_for` match has no `ModelType::GraniteMoe` arm and
// so returns `None`.
ModelType::GraniteMoe => "granitemoe",
ModelType::BitNet => "bitnet",
ModelType::ExaOne => "exaone",
ModelType::ExaOne4 => "exaone4",
Expand Down
1 change: 1 addition & 0 deletions src/execution/memory_estimate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,7 @@ fn inspect_registry_id(model_type: ModelType) -> &'static str {
ModelType::Apertus => "apertus",
ModelType::SeedOss => "seed_oss",
ModelType::Granite => "granite",
ModelType::GraniteMoe => "granitemoe",
ModelType::BitNet => "bitnet",
ModelType::ExaOne => "exaone",
ModelType::ExaOne4 => "exaone4",
Expand Down
2 changes: 2 additions & 0 deletions src/loaded_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ pub enum LoadedModel {
Apertus(models::ApertusModel),
SeedOss(models::SeedOssModel),
Granite(models::GraniteModel),
GraniteMoe(models::GraniteMoeModel),
BitNet(models::BitNetModel),
ExaOne(models::ExaOneModel),
// Sliding window model uses wrapper
Expand Down Expand Up @@ -435,6 +436,7 @@ macro_rules! delegate_language_model {
LoadedModel::IQuestLoopCoder(inner) => LanguageModel::$method(inner, $($arg),*),
LoadedModel::SeedOss(inner) => LanguageModel::$method(inner, $($arg),*),
LoadedModel::Granite(inner) => LanguageModel::$method(inner, $($arg),*),
LoadedModel::GraniteMoe(inner) => LanguageModel::$method(inner, $($arg),*),
LoadedModel::BitNet(inner) => LanguageModel::$method(inner, $($arg),*),
LoadedModel::ExaOne(inner) => LanguageModel::$method(inner, $($arg),*),
LoadedModel::ExaOne4(inner) => LanguageModel::$method(inner, $($arg),*),
Expand Down
1 change: 1 addition & 0 deletions src/model_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ macro_rules! for_each_model_registration {
Apertus => { kind: Text, directory: ConfigBacked, weight: Some(WeightLoadRoute::ConfigBacked), adapter: None, config_backed: { dir_loader: models::ApertusModel::load, args: models::apertus::ModelArgs, weight_builder: models::ApertusModel::from_weights, wrap: LoadedModel::Apertus } };
SeedOss => { kind: Text, directory: ConfigBacked, weight: Some(WeightLoadRoute::ConfigBacked), adapter: None, config_backed: { dir_loader: models::SeedOssModel::load, args: models::seed_oss::ModelArgs, weight_builder: models::SeedOssModel::from_weights, wrap: LoadedModel::SeedOss } };
Granite => { kind: Text, directory: ConfigBacked, weight: Some(WeightLoadRoute::ConfigBacked), adapter: None, config_backed: { dir_loader: models::GraniteModel::load, args: models::granite::ModelArgs, weight_builder: models::GraniteModel::from_weights, wrap: LoadedModel::Granite } };
GraniteMoe => { kind: Text, directory: ConfigBacked, weight: Some(WeightLoadRoute::ConfigBacked), adapter: None, config_backed: { dir_loader: models::GraniteMoeModel::load, args: models::granitemoe::ModelArgs, weight_builder: models::GraniteMoeModel::from_weights, wrap: LoadedModel::GraniteMoe } };
BitNet => { kind: Text, directory: ConfigBacked, weight: Some(WeightLoadRoute::ConfigBacked), adapter: None, config_backed: { dir_loader: models::BitNetModel::load, args: models::bitnet::BitNetConfig, weight_builder: models::BitNetModel::from_weights, wrap: LoadedModel::BitNet } };
ExaOne => { kind: Text, directory: ConfigBacked, weight: Some(WeightLoadRoute::ConfigBacked), adapter: None, config_backed: { dir_loader: models::ExaOneModel::load, args: models::exaone::ExaOneConfig, weight_builder: models::ExaOneModel::from_weights, wrap: LoadedModel::ExaOne } };
ExaOne4 => { kind: Text, directory: ConfigBacked, weight: Some(WeightLoadRoute::ConfigBacked), adapter: None, config_backed: { dir_loader: models::ExaOne4Model::load, args: models::exaone4::ModelArgs, weight_builder: models::ExaOne4Model::from_weights, wrap: |m| LoadedModel::ExaOne4(models::ExaOne4Wrapper::new(m)) } };
Expand Down
2 changes: 2 additions & 0 deletions src/models/batched_decode_coverage_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const EXPECTED_NATIVE: &[&str] = &[
"GptOssWrapper@src/models/gpt_oss.rs",
"GraniteModel@src/models/granite.rs",
"GraniteMoeHybridModel@src/models/granitemoehybrid.rs",
"GraniteMoeModel@src/models/granitemoe.rs",
"GriffinModel@src/models/recurrent_gemma.rs",
"HeliumModel@src/models/helium.rs",
"HunyuanMoeModel@src/models/hunyuan_moe.rs",
Expand Down Expand Up @@ -306,6 +307,7 @@ fn issue_2559_moe_families_use_the_shared_native_route_and_attention() {
"src/models/solar_open.rs",
"src/models/exaone_moe.rs",
"src/models/cohere2_moe.rs",
"src/models/granitemoe.rs",
] {
let source = fs::read_to_string(root.join(relative)).expect("model source is readable");
assert!(
Expand Down
1 change: 1 addition & 0 deletions src/models/detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,7 @@ pub(crate) fn detect_model_type_with_probes<P: ModelDetectionProbes + ?Sized>(
"apertus" => Ok(ModelType::Apertus),
"seed_oss" => Ok(ModelType::SeedOss),
"granite" => Ok(ModelType::Granite),
"granitemoe" => Ok(ModelType::GraniteMoe),
"exaone" => Ok(ModelType::ExaOne),
"exaone4" => Ok(ModelType::ExaOne4),
"exaone_moe" => Ok(ModelType::ExaOneMoe),
Expand Down
48 changes: 48 additions & 0 deletions src/models/detection_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,54 @@ fn gptj_model_type_is_detected() {
);
}

#[test]
fn granitemoe_model_type_is_detected_apart_from_granite_and_the_hybrid() {
let detected = detect_config(
"granitemoe",
json!({
"model_type": "granitemoe",
"architectures": ["GraniteMoeForCausalLM"],
"num_local_experts": 32,
"num_experts_per_tok": 8
}),
);
assert_eq!(detected, ModelType::GraniteMoe);
assert_eq!(
detect_config("granite", json!({ "model_type": "granite" })),
ModelType::Granite
);
assert_eq!(
detect_config(
"granitemoehybrid",
json!({ "model_type": "granitemoehybrid" })
),
ModelType::GraniteMoeHybrid
);

// GraniteMoeShared adds a shared expert this port does not load, so it must
// stay unsupported rather than load as `granitemoe` without that expert.
let model_dir = temp_path("granitemoeshared");
fs::create_dir_all(&model_dir).unwrap();
fs::write(
model_dir.join("config.json"),
json!({
"model_type": "granitemoeshared",
"architectures": ["GraniteMoeSharedForCausalLM"],
"num_local_experts": 32,
"num_experts_per_tok": 8,
"shared_intermediate_size": 1024
})
.to_string(),
)
.unwrap();
let err = super::detection::get_model_type(&model_dir)
.unwrap_err()
.to_string();
fs::remove_dir_all(model_dir).unwrap();
assert!(err.contains("Unsupported model type"), "{err}");
assert!(err.contains("granitemoeshared"), "{err}");
}

#[test]
fn gpt_neox_model_type_is_detected() {
// GPT-NeoX shares the `GPT` prefix in `architectures` with GPT-2 and
Expand Down
Loading