Skip to content

Commit 9871514

Browse files
committed
Reject unsupported serialized RoPE sidecars
Keep Pangu-Embedded restricted to ordinary RoPE and require MiniCPM LongRoPE factors to use floating-point storage so quantized sidecars cannot be silently consumed outside the packed-weight contract. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 1a957193-e630-44b2-bc50-8e506899acc2 Signed-off-by: Justin Chu <[email protected]>
1 parent aa2f191 commit 9871514

4 files changed

Lines changed: 43 additions & 8 deletions

File tree

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,10 +2038,6 @@ def _raise_for_invalid_conventional_decoder_tensor_contract(gguf_model) -> None:
20382038
required[prefix + "ffn_up.bias"] = (intermediate,)
20392039
required[prefix + "ffn_down.bias"] = (hidden,)
20402040

2041-
if architecture == "pangu-embedded":
2042-
rope_dim = int(metadata.get(f"{architecture}.rope.dimension_count", head_dim))
2043-
optional["rope_freqs.weight"] = (rope_dim // 2,)
2044-
20452041
allowed = set(required) | set(optional)
20462042
missing = sorted(set(required) - set(actual))
20472043
if architecture == "codeshell" and not {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,6 +2201,7 @@ def _pangu_embedded_postprocess(
22012201
arch = "pangu-embedded"
22022202
tensor_names = set(getattr(model, "tensor_names", ()) or ())
22032203
factor_tensors = tensor_names & {
2204+
"rope_freqs.weight",
22042205
"rope_factors_long.weight",
22052206
"rope_factors_short.weight",
22062207
}
@@ -2251,6 +2252,14 @@ def _minicpm_longrope(
22512252
if model is None or not hasattr(model, "get_tensor"):
22522253
raise ValueError("MiniCPM LongRoPE factors could not be read from the GGUF model")
22532254

2255+
raw_types = {
2256+
name: getattr(qtype, "value", qtype)
2257+
for name, _raw, qtype, _shape in model.tensor_items_raw()
2258+
if name in present
2259+
}
2260+
if any(type_id not in {0, 1, 30} for type_id in raw_types.values()):
2261+
raise ValueError("MiniCPM LongRoPE factors must use F32/F16/BF16 storage")
2262+
22542263
long_factor = np.asarray(model.get_tensor(long_name), dtype=np.float32).reshape(-1)
22552264
short_factor = np.asarray(model.get_tensor(short_name), dtype=np.float32).reshape(-1)
22562265
expected = rope_dim // 2

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,14 +333,17 @@ def test_pangu_embedded_rejects_partial_qkv_bias() -> None:
333333
_raise_for_invalid_conventional_decoder_tensor_contract(model)
334334

335335

336-
@pytest.mark.parametrize("factor", ["rope_factors_long.weight", "rope_factors_short.weight"])
337-
def test_pangu_embedded_longrope_factors_fail_closed(factor: str) -> None:
336+
@pytest.mark.parametrize(
337+
"factor",
338+
["rope_freqs.weight", "rope_factors_long.weight", "rope_factors_short.weight"],
339+
)
340+
def test_pangu_embedded_tensor_rope_factors_fail_closed(factor: str) -> None:
338341
model = _fixture("pangu-embedded")
339342
model._tensors[factor] = (4,)
340343
model.tensor_names = list(model._tensors)
341344
model.metadata["pangu-embedded.rope.scaling.type"] = "longrope"
342345

343-
with pytest.raises(ValueError, match="rope_factors"):
346+
with pytest.raises(ValueError, match=r"rope_(freqs|factors)"):
344347
_raise_for_invalid_conventional_decoder_tensor_contract(model)
345348

346349

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,20 @@ def __init__(
3636
architecture: str,
3737
metadata: dict[str, object],
3838
tensors: dict[str, np.ndarray],
39+
qtypes: dict[str, int] | None = None,
3940
):
4041
self.architecture = architecture
4142
self.metadata = metadata
4243
self._tensors = tensors
44+
self._qtypes = qtypes or {}
4345
self.tensor_names = tuple(tensors)
4446

4547
def get_tensor(self, name: str) -> np.ndarray:
4648
return self._tensors[name]
4749

4850
def tensor_items_raw(self):
4951
for name, value in self._tensors.items():
50-
yield name, memoryview(value), 0, value.shape
52+
yield name, memoryview(value), self._qtypes.get(name, 0), value.shape
5153

5254

5355
def _base_config(**overrides: object) -> ArchitectureConfig:
@@ -240,6 +242,31 @@ def test_minicpm_scales_and_longrope_are_exact() -> None:
240242
}
241243

242244

245+
def test_minicpm_quantized_longrope_factors_fail_closed() -> None:
246+
metadata = _metadata("minicpm")
247+
metadata.update(
248+
{
249+
"minicpm.embedding_scale": 12.0,
250+
"minicpm.residual_scale": 0.7,
251+
"minicpm.logit_scale": 10.0,
252+
"minicpm.rope.dimension_count": 4,
253+
"minicpm.rope.scaling.original_context_length": 8,
254+
}
255+
)
256+
tensors = _dense_tensors("minicpm")
257+
tensors["rope_factors_long.weight"] = np.array([2.0, 3.0], dtype=np.float32)
258+
tensors["rope_factors_short.weight"] = np.array([1.0, 1.5], dtype=np.float32)
259+
model = _FakeGGUF(
260+
"minicpm",
261+
metadata,
262+
tensors,
263+
qtypes={"rope_factors_long.weight": 2},
264+
)
265+
266+
with pytest.raises(ValueError, match="F32/F16/BF16"):
267+
_minicpm_postprocess(_base_config(), metadata, model)
268+
269+
243270
@pytest.mark.parametrize("bad", [0.0, -1.0, float("inf"), float("nan")])
244271
def test_minicpm_invalid_scales_fail_closed(bad: float) -> None:
245272
metadata = _metadata("minicpm")

0 commit comments

Comments
 (0)