Skip to content

feat(models): add Falcon-H1 (Mamba2 + attention parallel hybrid) - #256

Merged
inureyes merged 2 commits into
mainfrom
feature/issue-247-falcon-h1
Jun 13, 2026
Merged

inureyes merged 2 commits into
mainfrom
feature/issue-247-falcon-h1

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Port TII Falcon-H1, a hybrid that runs a Mamba2 SSM mixer and GQA attention in parallel within each decoder block: both mixers consume the same normed input, their outputs are summed with the residual, and a SwiGLU MLP follows.

Upstream reference: https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/models/falcon_h1.py

What changed

  • src/models/falcon_h1.rs (new): the parallel Mamba2 + attention block, the float32-hardened SSD scan with fused single-token decode kernel, the SwiGLU MLP, the paired per-layer cache, the directory/owned-weights loaders, and the LanguageModel impl.
  • src/models/falcon_h1_tests.rs (new): checkpoint-free config, Mamba2 geometry, tied-head-factor, gate-selection, and EOS tests.
  • Registration touchpoints: src/models/detection.rs ("falcon_h1"), src/models/mod.rs (module, re-export, ModelType::FalconH1, ALL_MODEL_TYPES, metadata "Falcon", test module), src/model_metadata.rs (Nonstandard directory + Special weight route), src/loaded_model.rs (enum variant + dispatch arm), src/loading/nonstandard.rs (directory loader), src/loading/special.rs (owned-weights loader), src/distributed/tensor_parallel/inference.rs (model-type string), src/main.rs (FAMILY_ORDER gains "Falcon").
  • docs/supported-models.md: Falcon-H1 entry in the hybrid families list.

Implementation notes

  • The Mamba2 mixer reuses the Nemotron-H SSD scan (float32-promoted to avoid M5 Max NaN) and the fused decode kernel; the one Falcon-H1 difference is the post-scan gate, which is plain SwiGLU (silu(gate) * y) when mamba_rms_norm is false (the default) and the gated RMSNorm only when true.
  • MUP channel multipliers are pre-folded into the mlx-community weights during conversion (the reference sanitize early-returns once the conv1d weight is in MLX channels-last layout), so the loader folds nothing and applies only the runtime tied-head factor lm_head_multiplier / embedding_multiplier on embed_tokens.as_linear.
  • Each layer owns a paired cache (Mamba2 conv+ssm state and a KVCache, both present) through ModelOwnedSequenceState; the family reports supports_batching == false and supports_padded_prefill == false, which keeps the recurrent SSM state out of paged batching.

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/Falcon-H1-Tiny-90M-Instruct-4bit (runs on the Metal host; no GPU in the build environment)

Closes #247

Falcon-H1 (TII) runs a Mamba2 SSM mixer and GQA attention in parallel within each decoder block, summing both mixer outputs with the residual on a single normed input, followed by a SwiGLU MLP. The Mamba2 mixer reuses the Nemotron-H float32-hardened SSD scan (in_proj -> gate/conv_input/dt split -> depthwise conv1d + SiLU -> hidden/B/C split -> scan -> gate -> out_proj) with the fused decode kernel for single-token steps. The one Falcon-H1 difference is the post-scan gate: plain SwiGLU (silu(gate) * y) when mamba_rms_norm is false (the default), versus the gated RMSNorm when true.

MUP channel multipliers are pre-folded into the mlx-community weights during conversion (the reference sanitize early-returns once the conv1d weight is in MLX channels-last layout), so the loader folds nothing and only applies the runtime tied-head factor lm_head_multiplier / embedding_multiplier on embed_tokens.as_linear. The embedding forward uses the pre-scaled weight directly.

Each layer owns a paired cache (Mamba2 conv+ssm state and a KVCache, both present) through ModelOwnedSequenceState; the family reports supports_batching == false and supports_padded_prefill == false, excluding the recurrent SSM state from paged batching.

Registered across detection, model metadata (Nonstandard directory + Special weight route), LoadedModel dispatch, the nonstandard directory loader, the special owned-weights loader, the tensor-parallel model-type string map, and the CLI family order (new "Falcon" group). Adds checkpoint-free config/geometry unit tests and a supported-models doc entry.
@inureyes inureyes added type:enhancement New features, capabilities, or significant additions priority:medium Medium priority area:models Model architectures, weights, loading, metadata status:review Under review labels Jun 13, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review: APPROVE

Intent

Port TII Falcon-H1: a hybrid decoder block running a Mamba2 SSM mixer and GQA attention in parallel on the same normed input, summing both with the residual, followed by a SwiGLU MLP. First Mamba2-hybrid port; pattern intended for reuse.

Verification against focus areas

SSD-scan correctness (prefill graph + decode kernel). ssm_step is logic-equivalent to the production-validated nemotron_h.rs::ssm_step: dt = clip(softplus(dt + dt_bias)), A = -exp(A_log), CB = matmul(C.swapaxes(1,2), B.transpose(0,2,3,1)) repeated per head, decay = exp(segsum(dtA.swapaxes(1,2))), tril(CB * decay) surrogate attention, state carry via exp(cumsum(dtA, -2)), and the + x * D skip term, all float32-promoted. segsum and repeat_axis match the nemotron helpers. ssm_step_kernel threads the shared ssm_update_kernel primitive identically. The prefill state shape [batch, num_heads, head_dim, state_dim] is consistent with the decode kernel input, the same hand-off nemotron uses.

Conv split offsets. in_proj split: gate [0, d_ssm), conv_input [d_ssm, d_ssm + conv_dim), dt [d_ssm + conv_dim, +n_heads). conv-output split: hidden [0, d_ssm), B [d_ssm, d_ssm + n_groups*state), C [d_ssm + n_groups*state, end). conv_dim = d_ssm + 2*n_groups*state. All match the reference exactly; slice_axis(..., -1) resolves to the axis end (Python semantics).

Per-head geometry. n_heads=24, mamba head_dim=32 (= d_ssm/n_heads = 768/24), n_groups=1, state=64 threaded consistently; attention head_dim=64 kept separate from the mamba head_dim. repeats = n_heads / n_groups.

Multiplier pre-folding. sanitize_weights only fixes conv1d orientation (no-op for the MLX channels-last checkpoint) and folds nothing else, matching the upstream early-return. The only runtime factor is the tied-head lm_head_multiplier / embedding_multiplier, applied on the tied path; the Some(lm_head) = untied / None = tied branch is correct.

Integration. Complete and consistent: detection, mod.rs (module/re-export/enum/ALL_MODEL_TYPES/metadata/test-mod), model_metadata (Nonstandard + Special), loaded_model (variant + delegate), nonstandard.rs + special.rs loaders, tensor-parallel fallback string, and main.rs FAMILY_ORDER ("Falcon", matching the metadata family). docs updated. Recurrent SSM state excluded from paged batching via supports_batching == false / supports_padded_prefill == false, mirroring LFM2/Nemotron-H. Snapshot/restore and trim_internal_caches follow the LFM2 hybrid-cache pattern.

Conventions. License header matches the in-tree pattern; upstream cited by GitHub URL; no references/ paths; no placeholders/TODOs; cross-repo ref check clean; purely additive (1427 insertions, 0 deletions).

Findings (none blocking)

  • LOW: one em-dash in a doc comment (line 275). Soft style nit only; already-merged nemotron_h.rs carries several and no CI enforces it.
  • INFO: mamba_norm_before_gate is parsed but unused (the gated-RMSNorm path hardcodes gate-before-norm). Inert for every real Falcon-H1 checkpoint (default false, and mamba_rms_norm is false).
  • INFO: rope_traditional / rope_scaling are not parsed; attention hardcodes traditional RoPE off and scale 1.0. Correct for this variant and the upstream defaults; a future scaled-RoPE Falcon-H1 checkpoint would need them.

No CRITICAL/HIGH findings; no code changes made. The implementation faithfully mirrors the validated nemotron_h scan and the LFM2 hybrid-cache pattern.

Checklist

  • All stated requirements implemented
  • No placeholder/mock code remaining
  • Integrated into project code flow
  • Project conventions followed (one LOW em-dash nit)
  • Existing modules reused (Mamba2Cache, nemotron-equivalent scan, model_owned, recurrent_snapshot, UnifiedLinear/Embedding, attention_from_ptr)
  • No unintended structural changes (additive only)
  • Tests pass (11 unit tests, per the orchestrator's Metal gate; test file reviewed statically here)

@inureyes inureyes added the status:done Completed label Jun 13, 2026
@inureyes
inureyes merged commit a247d0f into main Jun 13, 2026
4 checks passed
@inureyes
inureyes deleted the feature/issue-247-falcon-h1 branch June 13, 2026 06:50
@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
…lup#256)

* feat(models): add Falcon-H1 (Mamba2 + attention parallel hybrid)

Falcon-H1 (TII) runs a Mamba2 SSM mixer and GQA attention in parallel within each decoder block, summing both mixer outputs with the residual on a single normed input, followed by a SwiGLU MLP. The Mamba2 mixer reuses the Nemotron-H float32-hardened SSD scan (in_proj -> gate/conv_input/dt split -> depthwise conv1d + SiLU -> hidden/B/C split -> scan -> gate -> out_proj) with the fused decode kernel for single-token steps. The one Falcon-H1 difference is the post-scan gate: plain SwiGLU (silu(gate) * y) when mamba_rms_norm is false (the default), versus the gated RMSNorm when true.

MUP channel multipliers are pre-folded into the mlx-community weights during conversion (the reference sanitize early-returns once the conv1d weight is in MLX channels-last layout), so the loader folds nothing and only applies the runtime tied-head factor lm_head_multiplier / embedding_multiplier on embed_tokens.as_linear. The embedding forward uses the pre-scaled weight directly.

Each layer owns a paired cache (Mamba2 conv+ssm state and a KVCache, both present) through ModelOwnedSequenceState; the family reports supports_batching == false and supports_padded_prefill == false, excluding the recurrent SSM state from paged batching.

Registered across detection, model metadata (Nonstandard directory + Special weight route), LoadedModel dispatch, the nonstandard directory loader, the special owned-weights loader, the tensor-parallel model-type string map, and the CLI family order (new "Falcon" group). Adds checkpoint-free config/geometry unit tests and a supported-models doc entry.

* fix(models): honor mamba_norm_before_gate in Falcon-H1 gated RMSNorm
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 Falcon-H1 (Mamba2 + attention hybrid)

1 participant