Skip to content

Commit ef17fcb

Browse files
authored
Enhance embedding introspection for GenAI config (#321)
1 parent ccf96d6 commit ef17fcb

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

examples/gemma4_genai.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ def build_and_export(
7777
output_dir: str,
7878
*,
7979
dtype: str = "f32",
80+
device: str = "cpu",
8081
) -> None:
8182
"""Build ONNX model with mobius CLI and write ORT GenAI config."""
83+
ep = "cuda" if device == "cuda" else "cpu"
8284
cmd = [
8385
sys.executable,
8486
"-m",
@@ -88,6 +90,8 @@ def build_and_export(
8890
model_id,
8991
"--dtype",
9092
dtype,
93+
"--ep",
94+
ep,
9195
"--optimize",
9296
"--runtime",
9397
"ort-genai",
@@ -351,7 +355,7 @@ def main() -> None:
351355

352356
# ----- Export-only path -----
353357
if args.save_to:
354-
build_and_export(args.model, args.save_to, dtype=args.dtype)
358+
build_and_export(args.model, args.save_to, dtype=args.dtype, device=args.device)
355359
return
356360

357361
# ----- Resolve model directory -----
@@ -362,7 +366,7 @@ def main() -> None:
362366
default_dir = os.path.join("output", f"gemma4_{suffix}")
363367
model_dir = default_dir
364368
if not os.path.isfile(os.path.join(model_dir, "genai_config.json")):
365-
build_and_export(args.model, model_dir, dtype=args.dtype)
369+
build_and_export(args.model, model_dir, dtype=args.dtype, device=args.device)
366370

367371
# ----- Inference -----
368372
print("=" * 60)

src/mobius/integrations/ort_genai/auto_export.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ def _introspect_inputs(pkg: ModelPackage, key: str) -> dict[str, str] | None:
148148
return {n: n for n in _graph_input_names(model)}
149149

150150

151+
def _introspect_outputs(pkg: ModelPackage, key: str) -> dict[str, str] | None:
152+
"""Return ``{name: name}`` identity mapping for a sub-model's outputs.
153+
154+
Returns ``None`` when *key* is absent from *pkg*.
155+
"""
156+
model = pkg.get(key)
157+
if model is None:
158+
return None
159+
return {out.name: out.name for out in model.graph.outputs if out.name is not None}
160+
161+
151162
def _copy_tokenizer_files(
152163
model_id: str,
153164
output_dir: str,
@@ -713,6 +724,10 @@ def _write_genai_config(
713724
if embedding_input_mapping is not None:
714725
vision_kwargs["embedding_input_names"] = embedding_input_mapping
715726

727+
embedding_output_mapping = _introspect_outputs(pkg, "embedding")
728+
if embedding_output_mapping is not None:
729+
vision_kwargs["embedding_output_names"] = embedding_output_mapping
730+
716731
generator.with_vision(image_token_id=image_token_id, **vision_kwargs)
717732

718733
if has_speech:

src/mobius/integrations/ort_genai/genai_config.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ def with_vision(
266266
input_names: dict[str, str] | None = None,
267267
output_names: dict[str, str] | None = None,
268268
embedding_input_names: dict[str, str] | None = None,
269+
embedding_output_names: dict[str, str] | None = None,
269270
vision_start_token_id: int | None = None,
270271
video_token_id: int | None = None,
271272
) -> GenaiConfigGenerator:
@@ -288,6 +289,8 @@ def with_vision(
288289
mapping. When provided (e.g. from ONNX graph
289290
introspection), used directly. Defaults to
290291
input_ids + image_features.
292+
embedding_output_names: Override embedding model output name
293+
mapping. Defaults to inputs_embeds.
291294
vision_start_token_id: Token ID for ``<|vision_start|>``.
292295
video_token_id: Token ID for video placeholders.
293296
@@ -321,7 +324,9 @@ def with_vision(
321324
self._embedding = {
322325
"filename": embedding_filename,
323326
"inputs": embedding_input_names,
324-
"outputs": {
327+
"outputs": embedding_output_names
328+
if embedding_output_names is not None
329+
else {
325330
"inputs_embeds": "inputs_embeds",
326331
},
327332
"session_options": _make_session_options(self.ep),

0 commit comments

Comments
 (0)