@@ -1064,15 +1064,21 @@ def test_copies_available_files(self, tmp_path):
10641064 (fake_src / "chat_template.jinja" ).write_text ("{{ messages }}" )
10651065
10661066 with mock .patch ("huggingface_hub.hf_hub_download" ) as mock_dl :
1067- mock_dl .side_effect = lambda model_id , filename : (
1067+ from huggingface_hub .utils import LocalEntryNotFoundError
1068+
1069+ mock_dl .side_effect = lambda model_id , filename , ** _kwargs : (
10681070 str (fake_src / filename )
10691071 if (fake_src / filename ).exists ()
1070- else (_ for _ in ()).throw (OSError ("not found " ))
1072+ else (_ for _ in ()).throw (LocalEntryNotFoundError ("not cached " ))
10711073 )
10721074
10731075 dst = tmp_path / "output"
10741076 dst .mkdir ()
1075- copied = _copy_tokenizer_files ("fake/model" , str (dst ))
1077+ copied = _copy_tokenizer_files (
1078+ "fake/model" ,
1079+ str (dst ),
1080+ local_files_only = True ,
1081+ )
10761082
10771083 assert "tokenizer.json" in copied
10781084 assert (dst / "tokenizer.json" ).exists ()
@@ -1095,6 +1101,97 @@ def test_local_cache_miss_is_best_effort(self, tmp_path):
10951101 assert copied == []
10961102 assert not list (tmp_path .iterdir ())
10971103
1104+ def test_online_cache_miss_propagates (self , tmp_path ):
1105+ from huggingface_hub .utils import LocalEntryNotFoundError
1106+
1107+ with (
1108+ mock .patch (
1109+ "huggingface_hub.hf_hub_download" ,
1110+ side_effect = LocalEntryNotFoundError ("unexpected online miss" ),
1111+ ),
1112+ pytest .raises (LocalEntryNotFoundError , match = "unexpected online miss" ),
1113+ ):
1114+ _copy_tokenizer_files ("fake/model" , str (tmp_path ))
1115+
1116+ def test_online_missing_optional_file_is_skipped (self , tmp_path ):
1117+ from huggingface_hub import errors as hub_errors
1118+
1119+ remote_entry_not_found = getattr (
1120+ hub_errors ,
1121+ "RemoteEntryNotFoundError" ,
1122+ None ,
1123+ )
1124+ if remote_entry_not_found is None :
1125+ pytest .skip ("RemoteEntryNotFoundError is available in huggingface_hub >= 1.0" )
1126+
1127+ response = mock .Mock ()
1128+ response .status_code = 404
1129+ response .headers = {}
1130+ with mock .patch (
1131+ "huggingface_hub.hf_hub_download" ,
1132+ side_effect = remote_entry_not_found (
1133+ "optional file absent" ,
1134+ response = response ,
1135+ ),
1136+ ):
1137+ copied = _copy_tokenizer_files ("fake/model" , str (tmp_path ))
1138+
1139+ assert copied == []
1140+
1141+ def test_hub_zero_x_remote_missing_falls_back_to_entry_not_found (
1142+ self , tmp_path , monkeypatch
1143+ ):
1144+ from huggingface_hub import errors as hub_errors
1145+ from huggingface_hub .utils import EntryNotFoundError
1146+
1147+ monkeypatch .delattr (hub_errors , "RemoteEntryNotFoundError" )
1148+ with mock .patch (
1149+ "huggingface_hub.hf_hub_download" ,
1150+ side_effect = EntryNotFoundError ("optional file absent" ),
1151+ ):
1152+ copied = _copy_tokenizer_files ("fake/model" , str (tmp_path ))
1153+
1154+ assert copied == []
1155+
1156+ @pytest .mark .parametrize (
1157+ "error" ,
1158+ [
1159+ PermissionError ("denied" ),
1160+ OSError ("disk full" ),
1161+ RuntimeError ("network failure" ),
1162+ ],
1163+ )
1164+ def test_operational_failures_propagate (self , tmp_path , error ):
1165+ with (
1166+ mock .patch ("huggingface_hub.hf_hub_download" , side_effect = error ),
1167+ pytest .raises (type (error ), match = str (error )),
1168+ ):
1169+ _copy_tokenizer_files (
1170+ "fake/model" ,
1171+ str (tmp_path ),
1172+ local_files_only = True ,
1173+ )
1174+
1175+ def test_destination_write_failure_propagates (self , tmp_path ):
1176+ source = tmp_path / "tokenizer.json"
1177+ source .write_text ("{}" )
1178+ with (
1179+ mock .patch (
1180+ "huggingface_hub.hf_hub_download" ,
1181+ return_value = str (source ),
1182+ ),
1183+ mock .patch (
1184+ "mobius.integrations.ort_genai.auto_export.shutil.copy2" ,
1185+ side_effect = PermissionError ("destination denied" ),
1186+ ),
1187+ pytest .raises (PermissionError , match = "destination denied" ),
1188+ ):
1189+ _copy_tokenizer_files (
1190+ "fake/model" ,
1191+ str (tmp_path ),
1192+ local_files_only = True ,
1193+ )
1194+
10981195
10991196class TestCopyTokenizerFilesFromLocal :
11001197 """Tests for _copy_tokenizer_files_from_local."""
@@ -3730,6 +3827,7 @@ def test_lfm2_runtime_compatibility_is_pinned_to_released_probe(tmp_path):
37303827
37313828def test_decoder_sidecar_preserves_type_and_matching_compatibility_metadata (tmp_path ):
37323829 pkg = _make_fake_llm_pkg ("qwen2" )
3830+ pkg .config .num_nextn_predict_layers = 1
37333831 pkg ["mtp" ] = _mock_model (inputs = ["hidden_states" ], outputs = ["draft_logits" ])
37343832
37353833 result = write_ort_genai_config (pkg , str (tmp_path ))
@@ -3763,6 +3861,7 @@ def test_attached_mtp_sidecar_emits_component_qualified_cache_contract(tmp_path)
37633861 },
37643862 config = types .SimpleNamespace (
37653863 num_hidden_layers = 1 ,
3864+ num_nextn_predict_layers = 1 ,
37663865 use_dedicated_embeddings = False ,
37673866 use_dedicated_lm_head = False ,
37683867 ),
@@ -3819,6 +3918,93 @@ def test_attached_mtp_sidecar_emits_component_qualified_cache_contract(tmp_path)
38193918 assert compatibility ["graph_contract" ] is not None
38203919
38213920
3921+ def test_gguf_mtp_count_overrides_architecture_config_default_zero (tmp_path ):
3922+ from mobius ._model_package import ModelPackage
3923+ from mobius ._testing import make_config
3924+
3925+ config = make_config (num_nextn_predict_layers = 0 )
3926+ config ._gguf_nextn_predict_layers = 1
3927+ pkg = ModelPackage (
3928+ {"model" : _mock_decoder_model (layer_indices = (0 ,))},
3929+ config = config ,
3930+ )
3931+ pkg .mtp_head = ModelPackage (
3932+ {"model" : _mock_model (inputs = ["hidden_states" ], outputs = ["mtp_hidden" ])},
3933+ config = make_config (num_nextn_predict_layers = 0 ),
3934+ )
3935+
3936+ result = write_ort_genai_config (pkg , str (tmp_path ))
3937+ with open (result ["mtp_config" ], encoding = "utf-8" ) as handle :
3938+ mtp = json .load (handle )
3939+
3940+ assert mtp ["num_nextn_predict_layers" ] == 1
3941+
3942+
3943+ @pytest .mark .parametrize (
3944+ ("authoritative" , "error" ),
3945+ [
3946+ (0 , "requires positive" ),
3947+ (- 1 , "requires positive" ),
3948+ (None , "must be an integer" ),
3949+ ("one" , "must be an integer" ),
3950+ ],
3951+ )
3952+ def test_attached_mtp_rejects_invalid_authoritative_gguf_count (tmp_path , authoritative , error ):
3953+ from mobius ._model_package import ModelPackage
3954+
3955+ pkg = _make_fake_llm_pkg ("qwen2" )
3956+ pkg .config ._gguf_nextn_predict_layers = authoritative
3957+ pkg .config .num_nextn_predict_layers = 1
3958+ pkg .mtp_head = ModelPackage (
3959+ {"model" : _mock_model (inputs = ["hidden_states" ], outputs = ["mtp_hidden" ])},
3960+ config = types .SimpleNamespace (num_nextn_predict_layers = 1 ),
3961+ )
3962+
3963+ with pytest .raises ((TypeError , ValueError ), match = error ):
3964+ write_ort_genai_config (pkg , str (tmp_path ))
3965+
3966+
3967+ def test_explicit_target_mtp_count_precedes_proposer_declaration (tmp_path ):
3968+ from mobius ._model_package import ModelPackage
3969+
3970+ pkg = _make_fake_llm_pkg ("qwen2" )
3971+ pkg .config .num_nextn_predict_layers = 2
3972+ pkg .mtp_head = ModelPackage (
3973+ {"model" : _mock_model (inputs = ["hidden_states" ], outputs = ["mtp_hidden" ])},
3974+ config = types .SimpleNamespace (num_nextn_predict_layers = 1 ),
3975+ )
3976+
3977+ result = write_ort_genai_config (pkg , str (tmp_path ))
3978+ with open (result ["mtp_config" ], encoding = "utf-8" ) as handle :
3979+ mtp = json .load (handle )
3980+
3981+ assert mtp ["num_nextn_predict_layers" ] == 2
3982+
3983+
3984+ @pytest .mark .parametrize (
3985+ ("target_count" , "proposer_count" , "error" ),
3986+ [
3987+ (0 , 0 , "explicit positive" ),
3988+ (- 1 , 1 , "must be positive" ),
3989+ (0 , "one" , "must be an integer" ),
3990+ ],
3991+ )
3992+ def test_attached_mtp_requires_valid_positive_prediction_count (
3993+ tmp_path , target_count , proposer_count , error
3994+ ):
3995+ from mobius ._model_package import ModelPackage
3996+
3997+ pkg = _make_fake_llm_pkg ("qwen2" )
3998+ pkg .config .num_nextn_predict_layers = target_count
3999+ pkg .mtp_head = ModelPackage (
4000+ {"model" : _mock_model (inputs = ["hidden_states" ], outputs = ["mtp_hidden" ])},
4001+ config = types .SimpleNamespace (num_nextn_predict_layers = proposer_count ),
4002+ )
4003+
4004+ with pytest .raises ((TypeError , ValueError ), match = error ):
4005+ write_ort_genai_config (pkg , str (tmp_path ))
4006+
4007+
38224008@pytest .mark .parametrize ("order" , ["config-before-save" , "save-before-config" ])
38234009@pytest .mark .parametrize ("stale_layout" , ["same" , "different" , "extra" ])
38244010def test_attached_mtp_uses_current_package_sidecar_despite_stale_manifest (
0 commit comments