Skip to content

feat(models): add PLaMo 2 (plamo2, Mamba + attention hybrid) - #257

Merged
inureyes merged 2 commits into
mainfrom
feat/issue-251-plamo2
Jun 13, 2026
Merged

inureyes merged 2 commits into
mainfrom
feat/issue-251-plamo2

Conversation

@inureyes

Copy link
Copy Markdown
Member

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)); 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.

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.
    • Offset RMSNorm + normformer double norms. Each norm is rms_norm(x, weight + offset); the offsets (pre_mixer 1.0, post_mixer 1/5, pre_mlp 1.0, post_mlp 1/(5**1.5), final model.norm 1.0) are folded into the stored weight at construction so the standard mlxcel RMSNorm reproduces them. Every block applies a POST norm to both the mixer and the MLP output.
    • Mamba mixer with a post-conv B/C/dt projection. in_proj splits into z (gate) and x; only x goes through the depthwise causal conv; then bcdt_proj(conv_x) yields B, C, dt, each plain-RMS-normed, with dt_proj mapping dt to per-head deltas. The scan output is gated by swiglu(z, y) = silu(z) * y.
    • 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) (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_embeddings default, the is_mamba interleave pattern, the EOS default (id 2), and the per-norm offsets.
  • Registration: 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_ORDER gains a PLaMo family).
  • docs/supported-models.md: added the PLaMo 2 entry.

Hybrid cache

The model owns a mixed per-layer cache (Mamba2Cache for Mamba layers, KVCache for attention layers) through ModelOwnedSequenceState, with snapshot/restore and trim_internal_caches (KV trims positionally; the recurrent conv/ssm state resets). It reports supports_batching() == false and supports_padded_prefill() == false, excluding the recurrent SSM state from paged batching like Jamba / Nemotron-H / LFM2 / Falcon-H1.

Test plan

  • cargo fmt --check
  • cargo check --release --features metal,accelerate --lib
  • cargo clippy --release --features metal,accelerate --lib -- -D warnings
  • Real-model validation on models/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 standard load_text_weights boundary.

Closes #251

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.
@inureyes inureyes added status:review Under review type:enhancement New features, capabilities, or significant additions priority:medium Medium priority area:models Model architectures, weights, loading, metadata labels Jun 13, 2026
@inureyes inureyes mentioned this pull request Jun 13, 2026
4 of 5 tasks
@inureyes

Copy link
Copy Markdown
Member Author

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 Plamo2Model and to the upstream MLX PLaMo 2 (mlx_lm.utils.load_model, no torch/tokenizer) produces the exact same 24 greedy tokens across the prefill to first-token transition and 23 decode steps (tests/plamo2_parity.rs). This exercises the interleaved Mamba/attention stack, the SSD scan (graph prefill + fused decode kernel), the post-conv B/C/dt projection, and the offset/double RMSNorms.

Caveat: PLaMo 2 ships a custom PlamoTokenizer (a numpy Unigram in tokenizer.jsonl) that mlxcel's Rust tokenizer loader does not yet read, so end-to-end CLI text generation is not available until that tokenizer is supported. The model architecture (this issue's deliverable) is complete and validated; tokenizer support is tracked as a follow-up.

@inureyes inureyes added the status:done Completed label Jun 13, 2026
@inureyes
inureyes merged commit 0b1606c into main Jun 13, 2026
4 checks passed
@inureyes
inureyes deleted the feat/issue-251-plamo2 branch June 13, 2026 07:33
@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
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
)

* 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
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:medium Medium 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 PLaMo 2 (plamo2)

1 participant