Skip to content

Commit 240f4f7

Browse files
justinchubyCopilot
andcommitted
Validate audio heads before graph construction
Centralize standalone audio head-dimension validation and invoke it before ArchitectureConfig or projector graph construction, while retaining the weight-loader guard. Co-authored-by: Copilot App <[email protected]> Signed-off-by: Justin Chu <[email protected]>
1 parent 56f4845 commit 240f4f7

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,25 @@ def test_audio_weight_loader_rejects_invalid_attention_dimensions(
123123
_mmproj_audio_projector_to_onnx(sidecar, "ultravox")
124124

125125

126+
def test_public_audio_builder_rejects_invalid_attention_dimensions_before_graph(
127+
monkeypatch,
128+
):
129+
from mobius.integrations.gguf import _builder, _mmproj
130+
131+
sidecar = _standalone_sidecar("ultravox")
132+
sidecar.metadata["clip.audio.attention.head_count"] = 3
133+
monkeypatch.setattr(_mmproj, "_resolve_mmproj_companion_path", lambda path: path)
134+
monkeypatch.setattr(_builder, "_validate_gguf_model", lambda *args, **kwargs: None)
135+
136+
with pytest.raises(ValueError, match="positive and evenly divisible"):
137+
_mmproj.build_audio_projector_from_gguf(
138+
"synthetic.gguf",
139+
projector_type="ultravox",
140+
target_architecture="llama",
141+
_mmproj_gguf_model=sidecar,
142+
)
143+
144+
126145
def test_audio_projector_evidence_is_test_only_immutable_and_bounded():
127146
evidence = json.loads(
128147
(Path("tests") / "data" / "gguf_audio_projector_evidence.json").read_text()

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3216,6 +3216,17 @@ def _interleaved_rope_rows(values: np.ndarray, head_dim: int) -> np.ndarray:
32163216
return reshaped[:, order, :].reshape(values.shape)
32173217

32183218

3219+
def _audio_head_dim(metadata: dict[str, Any]) -> int:
3220+
head_count = int(metadata["clip.audio.attention.head_count"])
3221+
hidden_size = int(metadata["clip.audio.embedding_length"])
3222+
if head_count <= 0 or hidden_size <= 0 or hidden_size % head_count:
3223+
raise ValueError(
3224+
"clip.audio.embedding_length must be positive and evenly divisible by "
3225+
"the positive clip.audio.attention.head_count."
3226+
)
3227+
return hidden_size // head_count
3228+
3229+
32193230
def _mmproj_audio_projector_to_onnx(
32203231
mmproj_gguf: Any,
32213232
projector_type: str,
@@ -3226,14 +3237,7 @@ def _mmproj_audio_projector_to_onnx(
32263237
map_mmproj_audio_projector_to_onnx,
32273238
)
32283239

3229-
head_count = int(mmproj_gguf.metadata["clip.audio.attention.head_count"])
3230-
hidden_size = int(mmproj_gguf.metadata["clip.audio.embedding_length"])
3231-
if head_count <= 0 or hidden_size <= 0 or hidden_size % head_count:
3232-
raise ValueError(
3233-
"clip.audio.embedding_length must be positive and evenly divisible by "
3234-
"the positive clip.audio.attention.head_count."
3235-
)
3236-
head_dim = hidden_size // head_count
3240+
head_dim = _audio_head_dim(mmproj_gguf.metadata)
32373241
state_dict: dict[str, torch.Tensor] = {}
32383242
for name in mmproj_gguf.tensor_names:
32393243
mapped = map_mmproj_audio_projector_to_onnx(name, projector_type)
@@ -3317,6 +3321,7 @@ def build_audio_projector_from_gguf(
33173321
)
33183322
hidden_size = int(mmproj_gguf.metadata["clip.audio.embedding_length"])
33193323
num_heads = int(mmproj_gguf.metadata["clip.audio.attention.head_count"])
3324+
head_dim = _audio_head_dim(mmproj_gguf.metadata)
33203325
config = ArchitectureConfig(
33213326
model_type=f"gguf_{projector_type}",
33223327
vocab_size=1,
@@ -3325,7 +3330,7 @@ def build_audio_projector_from_gguf(
33253330
num_hidden_layers=int(mmproj_gguf.metadata["clip.audio.block_count"]),
33263331
num_attention_heads=num_heads,
33273332
num_key_value_heads=num_heads,
3328-
head_dim=hidden_size // num_heads,
3333+
head_dim=head_dim,
33293334
max_position_embeddings=65_536,
33303335
hidden_act="gelu",
33313336
)

0 commit comments

Comments
 (0)