@@ -45,6 +45,8 @@ def _write_clip_mmproj_gguf(
4545 extra_tensor : str | None = None ,
4646 identity_name : str = "Gemma-4-E2B-It" ,
4747 identity_repo : str | None = "https://huggingface.co/google/gemma-4-E2B-it" ,
48+ patch_weight_shape : tuple [int , ...] | None = None ,
49+ audio_num_mel_bins : int | None = _NUM_MEL_BINS ,
4850) -> None :
4951 """Write a small synthetic Gemma4 ``clip`` mmproj GGUF for tests."""
5052 from gguf import GGUFWriter
@@ -75,7 +77,10 @@ def _f32(name: str, shape: tuple[int, ...]) -> None:
7577 writer .add_tensor (name , np .random .randn (* shape ).astype (np .float32 ))
7678
7779 # patch embed (conv layout) + position table + projector.
78- _f32 ("v.patch_embd.weight" , (_VISION_HIDDEN , 3 , _PATCH_SIZE , _PATCH_SIZE ))
80+ _f32 (
81+ "v.patch_embd.weight" ,
82+ patch_weight_shape or (_VISION_HIDDEN , 3 , _PATCH_SIZE , _PATCH_SIZE ),
83+ )
7984 _f32 ("v.position_embd.weight" , (2 , _POS_EMB_SIZE , _VISION_HIDDEN ))
8085 _f32 ("mm.input_projection.weight" , (_TEXT_HIDDEN , _VISION_HIDDEN ))
8186 for layer in range (_VISION_LAYERS ):
@@ -103,7 +108,8 @@ def _f32(name: str, shape: tuple[int, ...]) -> None:
103108 writer .add_uint32 ("clip.audio.feed_forward_length" , _AUDIO_FFN )
104109 writer .add_uint32 ("clip.audio.block_count" , _AUDIO_LAYERS )
105110 writer .add_uint32 ("clip.audio.attention.head_count" , _AUDIO_HEADS )
106- writer .add_uint32 ("clip.audio.num_mel_bins" , _NUM_MEL_BINS )
111+ if audio_num_mel_bins is not None :
112+ writer .add_uint32 ("clip.audio.num_mel_bins" , audio_num_mel_bins )
107113 writer .add_uint32 ("clip.audio.projection_dim" , _TEXT_HIDDEN )
108114 writer .add_float32 ("clip.audio.attention.layer_norm_epsilon" , 1e-6 )
109115 _f32 ("a.conv1d.0.weight" , (_AUDIO_CONV0 , 1 , 3 , 3 ))
@@ -511,6 +517,145 @@ def test_matching_identity_survives_file_relocation(self, tmp_path: Path):
511517 resolved = _preflight_mmproj_pair (text , mmproj , modalities = (MMProjModality .VISION ,))
512518 assert resolved [MMProjModality .VISION ].projector_type == "gemma4v"
513519
520+ @pytest .mark .parametrize (
521+ ("text_name" , "sidecar_name" ),
522+ [
523+ ("Model-A" , "ModelA" ),
524+ ("org/model" , "orgmodel" ),
525+ ("model_a" , "model-a" ),
526+ ],
527+ )
528+ def test_identity_normalization_preserves_meaningful_separators (
529+ self , tmp_path : Path , text_name : str , sidecar_name : str
530+ ):
531+ from mobius .integrations .gguf ._mmproj import _preflight_mmproj_pair
532+ from mobius .integrations .gguf ._mmproj_registry import MMProjModality
533+
534+ text_path = tmp_path / "gemma4.gguf"
535+ mmproj_path = tmp_path / "mmproj.gguf"
536+ _write_minimal_gguf (text_path , "gemma4" )
537+ _write_clip_mmproj_gguf (mmproj_path , with_audio = False )
538+ text , mmproj = self ._load_pair (text_path , mmproj_path )
539+ text .metadata ["general.name" ] = text_name
540+ mmproj .metadata ["general.name" ] = sidecar_name
541+
542+ with pytest .raises (ValueError , match = r"general\.name" ):
543+ _preflight_mmproj_pair (text , mmproj , modalities = (MMProjModality .VISION ,))
544+
545+ @pytest .mark .parametrize ("empty_value" , ["" , " \t " ])
546+ def test_empty_identity_values_are_rejected (self , tmp_path : Path , empty_value : str ):
547+ from mobius .integrations .gguf ._mmproj import _preflight_mmproj_pair
548+ from mobius .integrations .gguf ._mmproj_registry import MMProjModality
549+
550+ text_path = tmp_path / "gemma4.gguf"
551+ mmproj_path = tmp_path / "mmproj.gguf"
552+ _write_minimal_gguf (text_path , "gemma4" )
553+ _write_clip_mmproj_gguf (mmproj_path , with_audio = False )
554+ text , mmproj = self ._load_pair (text_path , mmproj_path )
555+ text .metadata ["general.name" ] = empty_value
556+ mmproj .metadata ["general.name" ] = empty_value
557+
558+ with pytest .raises (ValueError , match = r"non-empty string" ):
559+ _preflight_mmproj_pair (text , mmproj , modalities = (MMProjModality .VISION ,))
560+
561+ def test_one_sided_present_empty_optional_binding_is_not_treated_absent (
562+ self , tmp_path : Path
563+ ):
564+ from mobius .integrations .gguf ._mmproj import _preflight_mmproj_pair
565+ from mobius .integrations .gguf ._mmproj_registry import MMProjModality
566+
567+ text_path = tmp_path / "gemma4.gguf"
568+ mmproj_path = tmp_path / "mmproj.gguf"
569+ _write_minimal_gguf (text_path , "gemma4" )
570+ _write_clip_mmproj_gguf (mmproj_path , with_audio = False )
571+ text , mmproj = self ._load_pair (text_path , mmproj_path )
572+ text .metadata ["general.base_model.0.repo_url" ] = ""
573+ mmproj .metadata .pop ("general.base_model.0.repo_url" )
574+
575+ with pytest .raises (ValueError , match = r"present on only one file" ):
576+ _preflight_mmproj_pair (text , mmproj , modalities = (MMProjModality .VISION ,))
577+
578+ def test_repository_canonicalization_only_normalizes_url_syntax (self , tmp_path : Path ):
579+ from mobius .integrations .gguf ._mmproj import _preflight_mmproj_pair
580+ from mobius .integrations .gguf ._mmproj_registry import MMProjModality
581+
582+ text_path = tmp_path / "gemma4.gguf"
583+ mmproj_path = tmp_path / "mmproj.gguf"
584+ _write_minimal_gguf (text_path , "gemma4" )
585+ _write_clip_mmproj_gguf (mmproj_path , with_audio = False )
586+ text , mmproj = self ._load_pair (text_path , mmproj_path )
587+ text .metadata ["general.base_model.0.repo_url" ] = (
588+ "HTTPS://HUGGINGFACE.CO/Google/Gemma-4-E2B-It.git/"
589+ )
590+
591+ resolved = _preflight_mmproj_pair (text , mmproj , modalities = (MMProjModality .VISION ,))
592+ assert resolved [MMProjModality .VISION ].projector_type == "gemma4v"
593+
594+ @pytest .mark .parametrize (
595+ "patch_shape" ,
596+ [
597+ (_VISION_HIDDEN , 1 , 3 , _PATCH_SIZE , _PATCH_SIZE ),
598+ (_VISION_HIDDEN , 1 , _PATCH_SIZE , _PATCH_SIZE ),
599+ (_VISION_HIDDEN , 3 , _PATCH_SIZE , _PATCH_SIZE + 1 ),
600+ ],
601+ )
602+ def test_gemma4_patch_shape_rejects_before_graph_build (
603+ self , tmp_path : Path , patch_shape : tuple [int , ...]
604+ ):
605+ from mobius .integrations .gguf ._mmproj import build_gemma4_vlm_from_gguf
606+
607+ text_path = tmp_path / "gemma4.gguf"
608+ mmproj_path = tmp_path / "mmproj.gguf"
609+ _write_minimal_gguf (text_path , "gemma4" )
610+ _write_clip_mmproj_gguf (
611+ mmproj_path ,
612+ with_audio = False ,
613+ patch_weight_shape = patch_shape ,
614+ )
615+
616+ with (
617+ mock .patch ("mobius._builder.build_from_module" ) as build_graph ,
618+ pytest .raises (ValueError , match = r"graph-compatible shape" ),
619+ ):
620+ build_gemma4_vlm_from_gguf (text_path , mmproj_path , image_token_id = 0 )
621+ build_graph .assert_not_called ()
622+
623+ @pytest .mark .parametrize ("num_mel_bins" , [None , 0 ])
624+ def test_audio_num_mel_bins_rejects_before_graph_build (
625+ self , tmp_path : Path , num_mel_bins : int | None
626+ ):
627+ from mobius .integrations .gguf ._mmproj import build_gemma4_vlm_from_gguf
628+
629+ text_path = tmp_path / "gemma4.gguf"
630+ mmproj_path = tmp_path / "mmproj.gguf"
631+ _write_minimal_gguf (text_path , "gemma4" )
632+ _write_clip_mmproj_gguf (
633+ mmproj_path ,
634+ with_audio = True ,
635+ audio_num_mel_bins = num_mel_bins ,
636+ )
637+
638+ with (
639+ mock .patch ("mobius._builder.build_from_module" ) as build_graph ,
640+ pytest .raises (ValueError , match = r"clip\.audio\.num_mel_bins" ),
641+ ):
642+ build_gemma4_vlm_from_gguf (text_path , mmproj_path , image_token_id = 0 )
643+ build_graph .assert_not_called ()
644+
645+ def test_audio_num_mel_bins_wrong_type_is_rejected (self , tmp_path : Path ):
646+ from mobius .integrations .gguf ._mmproj import _preflight_mmproj_pair
647+ from mobius .integrations .gguf ._mmproj_registry import MMProjModality
648+
649+ text_path = tmp_path / "gemma4.gguf"
650+ mmproj_path = tmp_path / "mmproj.gguf"
651+ _write_minimal_gguf (text_path , "gemma4" )
652+ _write_clip_mmproj_gguf (mmproj_path , with_audio = True )
653+ text , mmproj = self ._load_pair (text_path , mmproj_path )
654+ mmproj .metadata ["clip.audio.num_mel_bins" ] = "8"
655+
656+ with pytest .raises (ValueError , match = r"positive integer" ):
657+ _preflight_mmproj_pair (text , mmproj , modalities = (MMProjModality .VISION ,))
658+
514659 def test_quantized_projector_weight_is_rejected_by_role (self , tmp_path : Path ):
515660 from types import SimpleNamespace
516661
0 commit comments