Repository navigation
feat(models): add PLaMo 2 (plamo2, Mamba + attention hybrid) - #257
Conversation
Port Preferred Networks PLaMo 2, an interleaved Mamba (SSM) + GQA attention hybrid where each layer is either a Mamba mixer or an attention mixer chosen by index (is_mamba(i) = mamba_enabled && (i % mamba_step) != (mamba_step / 2)). For plamo-2-1b (mamba_step 2) the even layers are Mamba and the odd are attention. The either-or layout follows LFM2 / Nemotron-H, not the parallel both-per-layer Falcon-H1 block. Three PLaMo 2 specific pieces are implemented exactly: (1) offset RMSNorm with normformer-style double norms, where each norm is rms_norm(x, weight + offset) and the per-layer offsets (pre_mixer 1.0, post_mixer 1/5, pre_mlp 1.0, post_mlp 1/(5**1.5), final norm 1.0) are folded into the stored weight at construction so the standard mlxcel RMSNorm reproduces them; (2) the Mamba mixer that splits in_proj into z (gate) and x, runs only x through the depthwise causal conv, then derives B/C/dt from a post-conv bcdt_proj with per-branch RMSNorm and a dt_proj to per-head deltas, gating the scan output with swiglu(z, y) = silu(z) * y; (3) attention with a fused qkv_proj, a weight=None Q/K RMSNorm followed by a per-head learned scale (q_weight, k_weight), RoPE base 10000, and GQA. The SSD scan, conv-state handling, and fused single-token decode kernel dispatch are adapted from falcon_h1 (the shared mlxcel_core::ssm_update_kernel path). The SSM time-step limit is the ssm_update default (0.001, 100.0) since PLaMo 2 passes no explicit limit, not Falcon-H1's open (0, +inf). The model owns a mixed per-layer cache (Mamba2Cache for Mamba layers, KVCache for attention layers) through ModelOwnedSequenceState and reports supports_batching() == false, excluding the recurrent SSM state from paged batching like Jamba / Nemotron-H / LFM2 / Falcon-H1. Registered plamo2 across detection, the model registry (mod.rs: module, re-export, ModelType, ALL_MODEL_TYPES, metadata, test module), model_metadata (Nonstandard + Special), loaded_model (variant + delegation), both loaders (nonstandard dir loader + special owned-config), the tensor-parallel arch map, and main.rs FAMILY_ORDER (new PLaMo family). Added checkpoint-free unit tests for config parsing, the Mamba geometry, dt_dim derivation, tie_word_embeddings default, the is_mamba interleave pattern, the EOS default, and the per-norm offsets. Updated docs/supported-models.md.
|
Validation (orchestrator real-model gate): PLaMo 2 is validated against the mlx-lm 0.31.3 reference via greedy-id parity. Feeding identical input ids to mlxcel's Caveat: PLaMo 2 ships a custom |
) * feat(models): add PLaMo 2 (plamo2, Mamba + attention hybrid) Port Preferred Networks PLaMo 2, an interleaved Mamba (SSM) + GQA attention hybrid where each layer is either a Mamba mixer or an attention mixer chosen by index (is_mamba(i) = mamba_enabled && (i % mamba_step) != (mamba_step / 2)). For plamo-2-1b (mamba_step 2) the even layers are Mamba and the odd are attention. The either-or layout follows LFM2 / Nemotron-H, not the parallel both-per-layer Falcon-H1 block. Three PLaMo 2 specific pieces are implemented exactly: (1) offset RMSNorm with normformer-style double norms, where each norm is rms_norm(x, weight + offset) and the per-layer offsets (pre_mixer 1.0, post_mixer 1/5, pre_mlp 1.0, post_mlp 1/(5**1.5), final norm 1.0) are folded into the stored weight at construction so the standard mlxcel RMSNorm reproduces them; (2) the Mamba mixer that splits in_proj into z (gate) and x, runs only x through the depthwise causal conv, then derives B/C/dt from a post-conv bcdt_proj with per-branch RMSNorm and a dt_proj to per-head deltas, gating the scan output with swiglu(z, y) = silu(z) * y; (3) attention with a fused qkv_proj, a weight=None Q/K RMSNorm followed by a per-head learned scale (q_weight, k_weight), RoPE base 10000, and GQA. The SSD scan, conv-state handling, and fused single-token decode kernel dispatch are adapted from falcon_h1 (the shared mlxcel_core::ssm_update_kernel path). The SSM time-step limit is the ssm_update default (0.001, 100.0) since PLaMo 2 passes no explicit limit, not Falcon-H1's open (0, +inf). The model owns a mixed per-layer cache (Mamba2Cache for Mamba layers, KVCache for attention layers) through ModelOwnedSequenceState and reports supports_batching() == false, excluding the recurrent SSM state from paged batching like Jamba / Nemotron-H / LFM2 / Falcon-H1. Registered plamo2 across detection, the model registry (mod.rs: module, re-export, ModelType, ALL_MODEL_TYPES, metadata, test module), model_metadata (Nonstandard + Special), loaded_model (variant + delegation), both loaders (nonstandard dir loader + special owned-config), the tensor-parallel arch map, and main.rs FAMILY_ORDER (new PLaMo family). Added checkpoint-free unit tests for config parsing, the Mamba geometry, dt_dim derivation, tie_word_embeddings default, the is_mamba interleave pattern, the EOS default, and the per-norm offsets. Updated docs/supported-models.md. * test(models): add PLaMo 2 greedy-id parity vs mlx-lm; note custom-tokenizer gap
Summary
Port Preferred Networks PLaMo 2 (
plamo2), an interleaved Mamba (SSM) + GQA attention hybrid. Each layer is either a Mamba mixer or an attention mixer chosen by index (is_mamba(i) = mamba_enabled && (i % mamba_step) != (mamba_step / 2)); forplamo-2-1b(mamba_step2) the even layers are Mamba and the odd are attention. The either-or layout follows LFM2 / Nemotron-H, not the parallel both-per-layer Falcon-H1 block.Mirrors the mlx-lm reference: https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/models/plamo2.py
What changed
src/models/plamo2.rs(new): the PLaMo 2 model, implementing the three architecture-specific pieces exactly.rms_norm(x, weight + offset); the offsets (pre_mixer1.0,post_mixer1/5,pre_mlp1.0,post_mlp1/(5**1.5), finalmodel.norm1.0) are folded into the stored weight at construction so the standard mlxcelRMSNormreproduces them. Every block applies a POST norm to both the mixer and the MLP output.in_projsplits intoz(gate) andx; onlyxgoes through the depthwise causal conv; thenbcdt_proj(conv_x)yieldsB,C,dt, each plain-RMS-normed, withdt_projmappingdtto per-head deltas. The scan output is gated byswiglu(z, y) = silu(z) * y.qkv_proj, aweight=NoneQ/K RMSNorm followed by a per-head learned scale (q_weight,k_weight), RoPE base 10000, and GQA.falcon_h1(the sharedmlxcel_core::ssm_update_kernelpath). The SSM time-step limit is thessm_updatedefault(0.001, 100.0)(PLaMo 2 passes no explicit limit).src/models/plamo2_tests.rs(new): checkpoint-free unit tests for config parsing, Mamba geometry,dt_dim = max(64, hidden / 16),tie_word_embeddingsdefault, theis_mambainterleave pattern, the EOS default (id 2), and the per-norm offsets.detection.rs,models/mod.rs(module, re-export,ModelType,ALL_MODEL_TYPES, metadata, test module),model_metadata.rs(Nonstandard directory + Special weight route),loaded_model.rs(enum variant + delegation arm),loading/nonstandard.rs(directory loader),loading/special.rs(owned-config loader),distributed/tensor_parallel/inference.rs(arch map),main.rs(FAMILY_ORDERgains aPLaMofamily).docs/supported-models.md: added the PLaMo 2 entry.Hybrid cache
The model owns a mixed per-layer cache (
Mamba2Cachefor Mamba layers,KVCachefor attention layers) throughModelOwnedSequenceState, with snapshot/restore andtrim_internal_caches(KV trims positionally; the recurrent conv/ssm state resets). It reportssupports_batching() == falseandsupports_padded_prefill() == false, excluding the recurrent SSM state from paged batching like Jamba / Nemotron-H / LFM2 / Falcon-H1.Test plan
cargo fmt --checkcargo check --release --features metal,accelerate --libcargo clippy --release --features metal,accelerate --lib -- -D warningsmodels/plamo-2-1b(runs on the maintainer's Apple Silicon; no Metal device in this environment). The checkpoint is non-quantized F32, so it loads through the standardload_text_weightsboundary.Closes #251