Skip to content

Commit 8e052dc

Browse files
xiaoyu-workCopilotCopilot
authored
Update capture onnx graph cli exporting logic (microsoft#2496)
## Describe your changes 1. mobius exporter always default to genai. 2. remove import error catch from mb pass, it can swallow the real error. ## Checklist before requesting a review - [ ] Add unit tests for this change. - [ ] Make sure all tests can pass. - [ ] Update documents if necessary. - [ ] Lint and apply fixes to your code by running `lintrunner -a` - [ ] Is this a user-facing change? If yes, give a description of this change to be included in the release notes. ## (Optional) Issue link --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent cbd18f2 commit 8e052dc

6 files changed

Lines changed: 14 additions & 44 deletions

File tree

olive/cli/capture_onnx.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def register_subcommand(parser: ArgumentParser):
130130
type=str,
131131
default="fp16",
132132
choices=["fp16", "fp32", "int4", "bf16"],
133-
help="The precision of the ONNX model. This is used by Model Builder",
133+
help="The precision of the ONNX model. Used by Model Builder and Mobius Builder.",
134134
)
135135
mb_group.add_argument(
136136
"--int4_block_size",
@@ -178,7 +178,12 @@ def register_subcommand(parser: ArgumentParser):
178178
)
179179

180180
sub_parser.add_argument(
181-
"--use_ort_genai", action="store_true", help="Use OnnxRuntime generate() API to run the model"
181+
"--use_ort_genai",
182+
action="store_true",
183+
help=(
184+
"Use OnnxRuntime generate() API to run the model. "
185+
"Ignored when --use_mobius_builder is set (mobius always emits ORT GenAI artifacts)."
186+
),
182187
)
183188

184189
add_logging_options(sub_parser)
@@ -241,15 +246,7 @@ def _get_run_config(self, tempdir: str) -> dict:
241246
)
242247
del config["passes"]["c"]
243248
del config["passes"]["m"]
244-
to_replace.extend(
245-
[
246-
(("passes", "b", "precision"), self.args.precision),
247-
(
248-
("passes", "b", "runtime"),
249-
"ort-genai" if self.args.use_ort_genai else "none",
250-
),
251-
]
252-
)
249+
to_replace.append((("passes", "b", "precision"), self.args.precision))
253250
elif is_diffusers_model:
254251
del config["passes"]["m"]
255252
del config["passes"]["b"]

olive/passes/onnx/mobius_model_builder.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ class MobiusBuilder(Pass):
5454
See https://github.com/onnxruntime/mobius
5555
"""
5656

57-
class MobiusRuntime(StrEnumBase):
58-
"""Target runtimes for genai config generation."""
59-
60-
NONE = "none"
61-
ORT_GENAI = "ort-genai"
62-
6357
class MobiusEP(StrEnumBase):
6458
"""Execution providers supported by mobius."""
6559

@@ -97,16 +91,6 @@ def _default_config(cls, accelerator_spec: AcceleratorSpec) -> dict[str, PassCon
9791
"quantization pass (e.g. OnnxMatMulNBits) after this pass."
9892
),
9993
),
100-
"runtime": PassConfigParam(
101-
type_=MobiusBuilder.MobiusRuntime,
102-
required=False,
103-
default_value=MobiusBuilder.MobiusRuntime.ORT_GENAI,
104-
description=(
105-
"Target runtime. 'ort-genai' (default) generates "
106-
"genai_config.json, tokenizer files, and processor "
107-
"configs alongside the ONNX models. 'none' to skip."
108-
),
109-
),
11094
}
11195

11296
def _run_for_config(
@@ -169,10 +153,8 @@ def _run_for_config(
169153
pkg.save(str(output_dir))
170154

171155
# Generate ORT GenAI config artifacts (genai_config.json, tokenizer
172-
# files, processor configs) when runtime is set to ort-genai.
173-
genai_artifacts = {}
174-
if config.runtime == self.MobiusRuntime.ORT_GENAI:
175-
genai_artifacts = self._write_genai_config(pkg, str(output_dir), model_id, ep_str)
156+
# files, processor configs) alongside the ONNX models.
157+
genai_artifacts = self._write_genai_config(pkg, str(output_dir), model_id, ep_str)
176158

177159
package_keys = list(pkg.keys())
178160
logger.info("MobiusBuilder: saved components %s to '%s'", package_keys, output_dir)

olive/passes/onnx/model_builder.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,8 @@ def _run_for_config(
214214
config: type[BasePassConfig],
215215
output_model_path: str,
216216
) -> ONNXModelHandler:
217-
try:
218-
from onnxruntime_genai.models.builder import create_model
219-
except ImportError as e:
220-
raise ImportError(
221-
"onnxruntime-genai package is required to run ModelBuilder pass. Please install the package"
222-
" corresponding to your onnxruntime installation using pip. cpu: onnxruntime-genai, cuda:"
223-
" onnxruntime-genai-cuda, directml: onnxruntime-genai-directml"
224-
) from e
217+
from onnxruntime_genai.models.builder import create_model
218+
225219
self.maybe_patch_quant()
226220

227221
precision = config.precision

test/cli/test_cli.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,6 @@ def test_capture_onnx_command_use_mobius_builder(_, mock_run, precision, use_ort
485485
assert "m" not in config["passes"]
486486
assert config["passes"]["b"]["type"] == "MobiusBuilder"
487487
assert config["passes"]["b"]["precision"] == precision
488-
assert config["passes"]["b"]["runtime"] == ("ort-genai" if use_ort_genai else "none")
489488
assert mock_run.call_count == 1
490489

491490

test/cli/test_cli_test_model_smoke.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ def _assert_discrepancy_mobius(self, tmp_path: Path, model_id: str):
282282
"mobius_builder": {
283283
"type": "MobiusBuilder",
284284
"precision": "fp32",
285-
"runtime": "none",
286285
},
287286
"discrepancy_check": {
288287
"type": "OnnxDiscrepancyCheck",

test/passes/onnx/test_mobius_model_builder.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def _save(directory: str, **_kwargs):
9898
def _patch_build(pkg: MagicMock):
9999
# Patch mobius.build directly — lazy import inside _run_for_config means
100100
# patching the module attribute, not the local binding.
101-
# Also patch _write_genai_config since the default runtime is ort-genai.
101+
# Also patch _write_genai_config since mobius always emits ORT GenAI artifacts.
102102
return _CombinePatches(
103103
patch("mobius.build", return_value=pkg),
104104
patch.object(MobiusBuilder, "_write_genai_config"),
@@ -127,13 +127,12 @@ def __exit__(self, *args):
127127

128128

129129
def test_default_config_params():
130-
"""MobiusBuilder must declare precision and runtime, and must not declare execution_provider or trust_remote_code."""
130+
"""MobiusBuilder must declare precision, and must not declare execution_provider or trust_remote_code."""
131131
accelerator_spec = AcceleratorSpec(
132132
accelerator_type=Device.CPU, execution_provider=ExecutionProvider.CPUExecutionProvider
133133
)
134134
config = MobiusBuilder._default_config(accelerator_spec) # pylint: disable=protected-access
135135
assert "precision" in config
136-
assert "runtime" in config
137136
assert "execution_provider" not in config
138137
assert "trust_remote_code" not in config
139138

0 commit comments

Comments
 (0)