1818import yaml
1919
2020from mobius ._model_package import ModelPackage
21+ from mobius ._pipeline_contract import (
22+ declare_component_presence ,
23+ declare_optional_input ,
24+ )
2125from mobius .integrations .onnx_genai .inference_metadata import (
2226 SchedulerConfig ,
2327 build_diffusion_pipeline_metadata ,
@@ -118,13 +122,21 @@ def _embedding_model(
118122 outputs : list [tuple [str , ir .DataType , list [int | str ]]],
119123 * ,
120124 include_audio : bool = False ,
125+ optional_audio : bool = False ,
121126) -> ir .Model :
122127 inputs = [
123128 _value ("input_ids" , ir .DataType .INT64 , ["batch" , "sequence" ]),
124129 _value ("image_features" , ir .DataType .FLOAT , ["image_tokens" , 64 ]),
125130 ]
126131 if include_audio :
127- inputs .append (_value ("audio_features" , ir .DataType .FLOAT , ["audio_tokens" , 64 ]))
132+ audio_features = _value ("audio_features" , ir .DataType .FLOAT , ["audio_tokens" , 64 ])
133+ if optional_audio :
134+ declare_optional_input (
135+ audio_features ,
136+ presence = "audio" ,
137+ absent_shape = [0 , 64 ],
138+ )
139+ inputs .append (audio_features )
128140 return _model ("embedding" , inputs , outputs )
129141
130142
@@ -348,6 +360,7 @@ def test_gemma4_routes_all_embedding_outputs(self, tmp_path):
348360 ),
349361 ],
350362 include_audio = True ,
363+ optional_audio = True ,
351364 ),
352365 "audio_encoder" : _model (
353366 "audio_encoder" ,
@@ -367,7 +380,7 @@ def test_gemma4_routes_all_embedding_outputs(self, tmp_path):
367380 (
368381 "audio_features" ,
369382 ir .DataType .FLOAT ,
370- ["batch" , "audio_sequence " , 64 ],
383+ ["audio_tokens " , 64 ],
371384 ),
372385 (
373386 "audio_features_mask" ,
@@ -379,6 +392,7 @@ def test_gemma4_routes_all_embedding_outputs(self, tmp_path):
379392 },
380393 config = config ,
381394 )
395+ declare_component_presence (package ["audio_encoder" ].graph , "audio" )
382396
383397 source = tmp_path / "gemma"
384398 source .mkdir ()
@@ -402,6 +416,7 @@ def test_gemma4_routes_all_embedding_outputs(self, tmp_path):
402416 metadata = build_native_vlm_package_metadata (
403417 package , config = config , source = str (source )
404418 )
419+ emitted_yaml = yaml .safe_load (yaml .safe_dump (metadata , sort_keys = False ))
405420 validate_executable_closure (package , metadata )
406421 self ._validate (metadata )
407422 _assert_all_graph_ports_declared (package , metadata )
@@ -420,13 +435,38 @@ def test_gemma4_routes_all_embedding_outputs(self, tmp_path):
420435 "rank" : 3 ,
421436 "device_transfer" : False ,
422437 } in flow
423- assert not any (edge ["from" ] == "audio_encoder.audio_features" for edge in flow )
438+ assert {
439+ "from" : "audio_encoder.audio_features" ,
440+ "to" : "embedding.audio_features" ,
441+ "dtype" : "fp32" ,
442+ "rank" : 2 ,
443+ "device_transfer" : False ,
444+ } in flow
424445 embedding_audio = next (
425446 port
426447 for port in metadata ["pipeline" ]["models" ]["embedding" ]["io" ]["inputs" ]
427448 if port ["name" ] == "audio_features"
428449 )
429- assert embedding_audio ["source" ] == {"kind" : "external" , "input" : "request" }
450+ assert embedding_audio ["source" ] == {
451+ "kind" : "dataflow" ,
452+ "from" : "audio_encoder.audio_features" ,
453+ }
454+ assert emitted_yaml ["pipeline" ]["models" ]["embedding" ]["io" ]["optional_inputs" ] == {
455+ "audio_features" : {
456+ "presence" : "audio" ,
457+ "absent" : {"kind" : "zeros" , "shape" : [0 , 64 ]},
458+ }
459+ }
460+ assert emitted_yaml ["pipeline" ]["phases" ]["audio_encoder" ] == {
461+ "run_on" : "prompt_only" ,
462+ "when_present" : "audio" ,
463+ }
464+ audio_stage = next (
465+ stage
466+ for stage in metadata ["pipeline" ]["strategy" ]["stages" ]
467+ if stage ["strategy" ].get ("model" ) == "audio_encoder"
468+ )
469+ assert audio_stage ["run_on" ] == "prompt_only"
430470 assert metadata ["pipeline" ]["phases" ]["embedding" ] == {"run_on" : "every_step" }
431471 embedding_stage = next (
432472 stage
0 commit comments