Skip to content

Commit 41ed446

Browse files
justinchubyCopilot
andcommitted
Fail closed on unsupported Nemotron routing
Detect quantized stacked expert weights before policy evaluation and reject grouped Nemotron-H routing until its group-selection semantics are implemented. Co-authored-by: Copilot App <[email protected]> Signed-off-by: Justin Chu <[email protected]>
1 parent ae0c972 commit 41ed446

4 files changed

Lines changed: 56 additions & 6 deletions

File tree

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3803,11 +3803,7 @@ def _has_quantized_weights(gguf_model, gguf_arch: str) -> bool:
38033803
for name, _raw, qtype, _shape in gguf_model.tensor_items_raw():
38043804
hf_name = map_gguf_to_hf_names(name, gguf_arch)
38053805
type_id = getattr(qtype, "value", qtype)
3806-
if (
3807-
hf_name is not None
3808-
and hf_name.endswith(".weight")
3809-
and type_id not in float_type_ids
3810-
):
3806+
if hf_name is not None and name.endswith(".weight") and type_id not in float_type_ids:
38113807
return True
38123808
return False
38133809

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,7 @@ def _write_nemotron_h_moe_gguf(
11121112
extra: str | None = None,
11131113
malformed_shape: str | None = None,
11141114
mtp: bool = False,
1115+
quantized_only: str | None = None,
11151116
) -> None:
11161117
"""Write a tiny exact Nemotron-H backbone covering all four layer kinds."""
11171118
from gguf import GGMLQuantizationType, GGUFWriter
@@ -1202,7 +1203,12 @@ def add_q4(name: str, shape: tuple[int, ...]) -> None:
12021203
)
12031204
writer.add_tensor(name, raw, raw_dtype=GGMLQuantizationType.Q4_0)
12041205

1205-
projection = add_q4 if quantized else add_float
1206+
def projection(name: str, shape: tuple[int, ...]) -> None:
1207+
if quantized and (quantized_only is None or name == quantized_only):
1208+
add_q4(name, shape)
1209+
else:
1210+
add_float(name, shape)
1211+
12061212
add_float("token_embd.weight", (vocab, hidden))
12071213
add_float("output_norm.weight", (hidden,))
12081214
projection("output.weight", (vocab, hidden))
@@ -5249,6 +5255,20 @@ def test_quantized_source_requires_explicit_dequantization(self, tmp_path: Path)
52495255
== ir.DataType.FLOAT
52505256
)
52515257

5258+
def test_quantized_expert_only_requires_explicit_dequantization(
5259+
self, tmp_path: Path
5260+
) -> None:
5261+
from mobius.integrations.gguf import build_from_gguf
5262+
5263+
path = tmp_path / "nemotron-h-moe-expert-q4.gguf"
5264+
_write_nemotron_h_moe_gguf(
5265+
path,
5266+
quantized=True,
5267+
quantized_only="blk.1.ffn_up_exps.weight",
5268+
)
5269+
with pytest.raises(ValueError, match=r"keep_quantized=False"):
5270+
build_from_gguf(path, keep_quantized=True)
5271+
52525272
@pytest.mark.parametrize(
52535273
("kwargs", "match"),
52545274
[

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,14 @@ def _nemotron_h_postprocess(
15531553
raise ValueError(
15541554
f"{arch}.expert_shared_count must be exactly 1, got {shared_count}"
15551555
)
1556+
n_group = int(metadata.get(f"{arch}.expert_group_count", 1))
1557+
topk_group = int(metadata.get(f"{arch}.expert_group_used_count", 1))
1558+
if (n_group, topk_group) != (1, 1):
1559+
raise ValueError(
1560+
f"{arch} grouped expert routing is unsupported; "
1561+
f"expert_group_count and expert_group_used_count must both be 1, "
1562+
f"got {n_group} and {topk_group}"
1563+
)
15561564
latent_size = metadata.get(f"{arch}.moe_latent_size")
15571565
if latent_size is not None and int(latent_size) <= 0:
15581566
raise ValueError(f"{arch}.moe_latent_size must be greater than zero when present")
@@ -1570,6 +1578,8 @@ def _nemotron_h_postprocess(
15701578
num_experts_per_tok=top_k or None,
15711579
norm_topk_prob=bool(metadata.get(f"{arch}.expert_weights_norm", True)),
15721580
routed_scaling_factor=float(metadata.get(f"{arch}.expert_weights_scale", 1.0)),
1581+
n_group=1,
1582+
topk_group=1,
15731583
)
15741584
return NemotronHConfig(
15751585
**fields,

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,30 @@ def test_nemotron_h_moe_derives_exact_routed_schedule_and_defaults(self) -> None
192192
assert config.norm_topk_prob is True
193193
assert config.routed_scaling_factor == pytest.approx(2.5)
194194

195+
def test_nemotron_h_moe_rejects_grouped_expert_routing(self) -> None:
196+
from mobius.integrations.gguf._config_mapping import gguf_to_config
197+
198+
metadata = self._metadata("nemotron_h_moe")
199+
metadata.update(
200+
{
201+
"nemotron_h_moe.expert_count": 4,
202+
"nemotron_h_moe.expert_used_count": 2,
203+
"nemotron_h_moe.expert_feed_forward_length": 96,
204+
"nemotron_h_moe.expert_shared_count": 1,
205+
"nemotron_h_moe.expert_shared_feed_forward_length": 192,
206+
"nemotron_h_moe.expert_group_count": 2,
207+
"nemotron_h_moe.expert_group_used_count": 1,
208+
}
209+
)
210+
with pytest.raises(ValueError, match="grouped expert routing is unsupported"):
211+
gguf_to_config(
212+
_FakeDenseGGUF(
213+
"nemotron_h_moe",
214+
metadata,
215+
["token_embd.weight", "blk.1.ffn_gate_inp.weight"],
216+
)
217+
)
218+
195219
def test_dense_nemotron_h_rejects_moe_metadata(self) -> None:
196220
from mobius.integrations.gguf._config_mapping import gguf_to_config
197221

0 commit comments

Comments
 (0)