Skip to content

Commit 5db3720

Browse files
committed
Harden Falcon-H1 policy validation
Pin Falcon-H1 in the parent branch's explicit quantized-import policy while retaining dequantize-only fail-closed behavior. Reject SSM group geometries that cannot expand across Mamba heads, add regression coverage, and align the changed files with repository lint rules.\n\nCo-authored-by: Copilot App <[email protected]> Signed-off-by: Justin Chu <[email protected]>
1 parent bb325c1 commit 5db3720

5 files changed

Lines changed: 38 additions & 28 deletions

File tree

‎src/mobius/_configs/_base.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2748,8 +2748,12 @@ def __post_init__(self) -> None:
27482748
raise ValueError("mamba_n_heads must divide mamba_d_ssm")
27492749
if self.mamba_d_head * self.mamba_n_heads != self.mamba_d_ssm:
27502750
raise ValueError("mamba_d_head * mamba_n_heads must equal mamba_d_ssm")
2751-
if self.mamba_n_groups <= 0 or self.mamba_d_ssm % self.mamba_n_groups:
2752-
raise ValueError("mamba_n_groups must divide mamba_d_ssm")
2751+
if (
2752+
self.mamba_n_groups <= 0
2753+
or self.mamba_n_heads % self.mamba_n_groups
2754+
or self.mamba_d_ssm % self.mamba_n_groups
2755+
):
2756+
raise ValueError("mamba_n_groups must divide both mamba_n_heads and mamba_d_ssm")
27532757
if self.mamba_d_state <= 0 or self.mamba_d_conv <= 0 or self.mamba_chunk_size <= 0:
27542758
raise ValueError("Falcon-H1 state, convolution, and chunk sizes must be positive")
27552759
if len(self.mlp_multipliers) != 2:

‎src/mobius/integrations/gguf/_arch_registry_test.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
"eagle3",
155155
"exaone",
156156
"falcon",
157+
"falcon-h1",
157158
"gemma",
158159
"gemma2",
159160
"gemma3",

‎src/mobius/models/falcon_h1.py‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
initialize_rope,
1919
)
2020

21+
2122
class FalconH1MLP(nn.Module):
2223
"""Falcon-H1 SwiGLU feed-forward network with MuP gate and output scaling."""
2324

@@ -42,12 +43,12 @@ def __init__(self, config: FalconH1Config):
4243

4344
def forward(self, op: OpBuilder, hidden_states: ir.Value) -> ir.Value:
4445
gate = self.gate_proj(op, hidden_states)
45-
if self.gate_multiplier != 1.0:
46+
if self.gate_multiplier != 1.0: # noqa: RUF069
4647
gate = op.Mul(gate, self.gate_multiplier)
4748
gate = op.Mul(gate, op.Sigmoid(gate))
4849
hidden_states = op.Mul(self.up_proj(op, hidden_states), gate)
4950
hidden_states = self.down_proj(op, hidden_states)
50-
if self.down_multiplier != 1.0:
51+
if self.down_multiplier != 1.0: # noqa: RUF069
5152
hidden_states = op.Mul(hidden_states, self.down_multiplier)
5253
return hidden_states
5354

@@ -117,11 +118,11 @@ def forward(
117118
ssm_state,
118119
mamba_attention_mask,
119120
)
120-
if self.ssm_out_multiplier != 1.0:
121+
if self.ssm_out_multiplier != 1.0: # noqa: RUF069
121122
mamba_output = op.Mul(mamba_output, self.ssm_out_multiplier)
122123

123124
attention_input = normalized
124-
if self.attention_in_multiplier != 1.0:
125+
if self.attention_in_multiplier != 1.0: # noqa: RUF069
125126
attention_input = op.Mul(attention_input, self.attention_in_multiplier)
126127
attention_output, present_key_value = self.self_attn(
127128
op,
@@ -131,7 +132,7 @@ def forward(
131132
past_key_value=(past_key, past_value),
132133
)
133134
present_key, present_value = present_key_value
134-
if self.attention_out_multiplier != 1.0:
135+
if self.attention_out_multiplier != 1.0: # noqa: RUF069
135136
attention_output = op.Mul(
136137
attention_output,
137138
self.attention_out_multiplier,
@@ -169,7 +170,7 @@ def forward(
169170
past_key_values: tuple[ir.Value, ...],
170171
) -> tuple[ir.Value, tuple[ir.Value, ...]]:
171172
hidden_states = self.embed_tokens(op, input_ids)
172-
if self.embedding_multiplier != 1.0:
173+
if self.embedding_multiplier != 1.0: # noqa: RUF069
173174
hidden_states = op.Mul(hidden_states, self.embedding_multiplier)
174175

175176
position_embeddings = self.rotary_emb(op, position_ids)
@@ -199,9 +200,7 @@ def forward(
199200
past_key_values[offset + 2],
200201
past_key_values[offset + 3],
201202
)
202-
present_key_values.extend(
203-
[present_key, present_value, present_conv, present_ssm]
204-
)
203+
present_key_values.extend([present_key, present_value, present_conv, present_ssm])
205204
return self.final_layernorm(op, hidden_states), tuple(present_key_values)
206205

207206

@@ -243,7 +242,7 @@ def forward(
243242
past_key_values,
244243
)
245244
logits = self.lm_head(op, hidden_states)
246-
if self.lm_head_multiplier != 1.0:
245+
if self.lm_head_multiplier != 1.0: # noqa: RUF069
247246
logits = op.Mul(logits, self.lm_head_multiplier)
248247
return logits, present_key_values
249248

‎src/mobius/tasks/_falcon_h1.py‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88

99
from mobius._configs import BaseModelConfig, FalconH1Config
1010
from mobius._model_package import ModelPackage
11-
12-
from ._base import ModelTask, _make_graph, _make_model
13-
from ._cache_utils import _register_linear_attention_functions
11+
from mobius.tasks._base import ModelTask, _make_graph, _make_model
12+
from mobius.tasks._cache_utils import _register_linear_attention_functions
1413

1514

1615
class FalconH1CausalLMTask(ModelTask):
@@ -144,8 +143,7 @@ def build(
144143
state.shape = ir.Shape(
145144
[
146145
batch,
147-
config.mamba_d_ssm
148-
+ 2 * config.mamba_n_groups * config.mamba_d_state,
146+
config.mamba_d_ssm + 2 * config.mamba_n_groups * config.mamba_d_state,
149147
config.mamba_d_conv - 1,
150148
]
151149
)
@@ -166,9 +164,7 @@ def build(
166164

167165
model = _make_model(graph)
168166
_register_linear_attention_functions(model, config)
169-
model.metadata_props["mobius.cache_abi"] = (
170-
"per-layer:key,value,conv_state,ssm_state"
171-
)
167+
model.metadata_props["mobius.cache_abi"] = "per-layer:key,value,conv_state,ssm_state"
172168
model.metadata_props["mobius.runtime_support"] = (
173169
"deferred: downstream heterogeneous-state package schema is unavailable"
174170
)

‎tests/falcon_h1_test.py‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@
55

66
import numpy as np
77
import onnx_ir as ir
8-
import torch
98
import pytest
10-
11-
from mobius._configs import FalconH1Config
12-
from mobius._testing.ort_inference import OnnxModelSession
13-
from mobius.integrations._weight_loading import apply_weights
14-
9+
import torch
1510
from _test_configs import CAUSAL_LM_CONFIGS, _base_config
1611
from synthetic_parity_test import (
1712
_build_onnx_model,
@@ -20,6 +15,10 @@
2015
_fill_random_weights,
2116
)
2217

18+
from mobius._configs import FalconH1Config
19+
from mobius._testing.ort_inference import OnnxModelSession
20+
from mobius.integrations._weight_loading import apply_weights
21+
2322

2423
def _tiny_overrides() -> dict:
2524
return next(
@@ -35,6 +34,18 @@ def test_falcon_h1_rejects_invalid_attention_and_ssm_geometry() -> None:
3534
_base_config(**(base | {"head_dim": 15}))
3635
with pytest.raises(ValueError, match="mamba_d_head"):
3736
_base_config(**(base | {"mamba_d_head": 15}))
37+
with pytest.raises(ValueError, match="mamba_n_groups must divide both"):
38+
_base_config(
39+
**(
40+
base
41+
| {
42+
"mamba_d_ssm": 24,
43+
"mamba_n_heads": 3,
44+
"mamba_d_head": 8,
45+
"mamba_n_groups": 2,
46+
}
47+
)
48+
)
3849
with pytest.raises(ValueError, match="exactly five"):
3950
_base_config(**(base | {"ssm_multipliers": [1.0] * 4}))
4051
with pytest.raises(ValueError, match="ordered and non-negative"):
@@ -118,8 +129,7 @@ def test_falcon_h1_prefill_decode_logits_and_four_states_match_transformers() ->
118129
f"past_key_values.{layer}.conv_state": np.zeros(
119130
(
120131
1,
121-
config.mamba_d_ssm
122-
+ 2 * config.mamba_n_groups * config.mamba_d_state,
132+
config.mamba_d_ssm + 2 * config.mamba_n_groups * config.mamba_d_state,
123133
config.mamba_d_conv - 1,
124134
),
125135
np.float32,

0 commit comments

Comments
 (0)